Wordpress/tests/phpunit/includes/class-wp-rest-test-search-handler.php
Gary Pendergast 5c9c54239d REST API: Introduce controller for searching across post types.
Introduces a `WP_REST_Search_Controller` class which registers a `/wp/v2/search` endpoint. Search types are handled by extending `WP_REST_Search_Handler`. The default search type is `WP_REST_Post_Search_Handler` but can be filtered by plugins or a theme.

Merges [43739,43741] from the 5.0 branch to trunk.

Props danielbachhuber, flixos90, pento, rmccue, joehoyle.
Fixes #39965.



git-svn-id: https://develop.svn.wordpress.org/trunk@44107 602fd350-edb4-49c9-b593-d223f7449a82
2018-12-13 09:37:05 +00:00

92 lines
2.4 KiB
PHP

<?php
/**
* REST API: WP_REST_Test_Search_Handler class
*
* @package WordPress
* @subpackage REST_API
*/
/**
* Test class extending WP_REST_Search_Handler
*/
class WP_REST_Test_Search_Handler extends WP_REST_Search_Handler {
protected $items = array();
public function __construct( $amount = 10 ) {
$this->type = 'test';
$this->subtypes = array( 'test_first_type', 'test_second_type' );
$this->items = array();
for ( $i = 1; $i <= $amount; $i++ ) {
$subtype = $i > $amount / 2 ? 'test_second_type' : 'test_first_type';
$this->items[ $i ] = (object) array(
'test_id' => $i,
'test_title' => sprintf( 'Title %d', $i ),
'test_url' => sprintf( home_url( '/tests/%d' ), $i ),
'test_type' => $subtype,
);
}
}
public function search_items( WP_REST_Request $request ) {
$subtypes = $request[ WP_REST_Search_Controller::PROP_SUBTYPE ];
if ( in_array( WP_REST_Search_Controller::TYPE_ANY, $subtypes, true ) ) {
$subtypes = $this->subtypes;
}
$results = array();
foreach ( $subtypes as $subtype ) {
$results = array_merge( $results, wp_list_filter( array_values( $this->items ), array( 'test_type' => $subtype ) ) );
}
$results = wp_list_sort( $results, 'test_id', 'DESC' );
$number = (int) $request['per_page'];
$offset = (int) $request['per_page'] * ( (int) $request['page'] - 1 );
$total = count( $results );
$results = array_slice( $results, $offset, $number );
return array(
self::RESULT_IDS => wp_list_pluck( $results, 'test_id' ),
self::RESULT_TOTAL => $total,
);
}
public function prepare_item( $id, array $fields ) {
$test = $this->items[ $id ];
$data = array();
if ( in_array( WP_REST_Search_Controller::PROP_ID, $fields, true ) ) {
$data[ WP_REST_Search_Controller::PROP_ID ] = (int) $test->test_id;
}
if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) {
$data[ WP_REST_Search_Controller::PROP_TITLE ] = $test->test_title;
}
if ( in_array( WP_REST_Search_Controller::PROP_URL, $fields, true ) ) {
$data[ WP_REST_Search_Controller::PROP_URL ] = $test->test_url;
}
if ( in_array( WP_REST_Search_Controller::PROP_TYPE, $fields, true ) ) {
$data[ WP_REST_Search_Controller::PROP_TYPE ] = $this->type;
}
if ( in_array( WP_REST_Search_Controller::PROP_SUBTYPE, $fields, true ) ) {
$data[ WP_REST_Search_Controller::PROP_SUBTYPE ] = $test->test_type;
}
return $data;
}
public function prepare_item_links( $id ) {
return array();
}
}