<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[43739] branches/5.0: REST API: Introduce controller for searching across post types.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/43739">43739</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/43739","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>danielbachhuber</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2018-10-17 17:02:04 +0000 (Wed, 17 Oct 2018)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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.

Props danielbachhuber, flixos90, pento, rmccue.
Fixes <a href="https://core.trac.wordpress.org/ticket/39965">#39965</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#branches50srcwpincludesrestapiphp">branches/5.0/src/wp-includes/rest-api.php</a></li>
<li><a href="#branches50srcwpsettingsphp">branches/5.0/src/wp-settings.php</a></li>
<li><a href="#branches50testsphpunitincludesbootstrapphp">branches/5.0/tests/phpunit/includes/bootstrap.php</a></li>
<li><a href="#branches50testsphpunittestsrestapirestschemasetupphp">branches/5.0/tests/phpunit/tests/rest-api/rest-schema-setup.php</a></li>
<li><a href="#branches50testsqunitfixtureswpapigeneratedjs">branches/5.0/tests/qunit/fixtures/wp-api-generated.js</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#branches50srcwpincludesrestapiendpointsclasswprestsearchcontrollerphp">branches/5.0/src/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php</a></li>
<li>branches/5.0/src/wp-includes/rest-api/search/</li>
<li><a href="#branches50srcwpincludesrestapisearchclasswprestpostsearchhandlerphp">branches/5.0/src/wp-includes/rest-api/search/class-wp-rest-post-search-handler.php</a></li>
<li><a href="#branches50srcwpincludesrestapisearchclasswprestsearchhandlerphp">branches/5.0/src/wp-includes/rest-api/search/class-wp-rest-search-handler.php</a></li>
<li><a href="#branches50testsphpunitincludesclasswpresttestsearchhandlerphp">branches/5.0/tests/phpunit/includes/class-wp-rest-test-search-handler.php</a></li>
<li><a href="#branches50testsphpunittestsrestapirestsearchcontrollerphp">branches/5.0/tests/phpunit/tests/rest-api/rest-search-controller.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="branches50srcwpincludesrestapiendpointsclasswprestsearchcontrollerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: branches/5.0/src/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.0/src/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php                               (rev 0)
+++ branches/5.0/src/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php 2018-10-17 17:02:04 UTC (rev 43739)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,359 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * REST API: WP_REST_Search_Controller class
+ *
+ * @package WordPress
+ * @subpackage REST_API
+ * @since 5.0.0
+ */
+
+/**
+ * Core class to search through all WordPress content via the REST API.
+ *
+ * @since 5.0.0
+ *
+ * @see WP_REST_Controller
+ */
+class WP_REST_Search_Controller extends WP_REST_Controller {
+
+       /**
+        * ID property name.
+        */
+       const PROP_ID = 'id';
+
+       /**
+        * Title property name.
+        */
+       const PROP_TITLE = 'title';
+
+       /**
+        * URL property name.
+        */
+       const PROP_URL = 'url';
+
+       /**
+        * Type property name.
+        */
+       const PROP_TYPE = 'type';
+
+       /**
+        * Subtype property name.
+        */
+       const PROP_SUBTYPE = 'subtype';
+
+       /**
+        * Identifier for the 'any' type.
+        */
+       const TYPE_ANY = 'any';
+
+       /**
+        * Search handlers used by the controller.
+        *
+        * @since 5.0.0
+        * @var array
+        */
+       protected $search_handlers = array();
+
+       /**
+        * Constructor.
+        *
+        * @since 5.0.0
+        *
+        * @param array $search_handlers List of search handlers to use in the controller. Each search
+        *                               handler instance must extend the `WP_REST_Search_Handler` class.
+        */
+       public function __construct( array $search_handlers ) {
+               $this->namespace = 'wp/v2';
+               $this->rest_base = 'search';
+
+               foreach ( $search_handlers as $search_handler ) {
+                       if ( ! $search_handler instanceof WP_REST_Search_Handler ) {
+
+                               /* translators: %s: PHP class name */
+                               _doing_it_wrong( __METHOD__, sprintf( __( 'REST search handlers must extend the %s class.' ), 'WP_REST_Search_Handler' ), '5.5.0' );
+                               continue;
+                       }
+
+                       $this->search_handlers[ $search_handler->get_type() ] = $search_handler;
+               }
+       }
+
+       /**
+        * Registers the routes for the objects of the controller.
+        *
+        * @since 5.0.0
+        *
+        * @see register_rest_route()
+        */
+       public function register_routes() {
+               register_rest_route(
+                       $this->namespace,
+                       '/' . $this->rest_base,
+                       array(
+                               array(
+                                       'methods'             => WP_REST_Server::READABLE,
+                                       'callback'            => array( $this, 'get_items' ),
+                                       'permission_callback' => array( $this, 'get_items_permission_check' ),
+                                       'args'                => $this->get_collection_params(),
+                               ),
+                               'schema' => array( $this, 'get_public_item_schema' ),
+                       )
+               );
+       }
+
+       /**
+        * Checks if a given request has access to search content.
+        *
+        * @since 5.0.0
+        *
+        * @param WP_REST_Request $request Full details about the request.
+        * @return true|WP_Error True if the request has search access, WP_Error object otherwise.
+        */
+       public function get_items_permission_check( $request ) {
+               return true;
+       }
+
+       /**
+        * Retrieves a collection of search results.
+        *
+        * @since 5.0.0
+        *
+        * @param WP_REST_Request $request Full details about the request.
+        * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
+        */
+       public function get_items( $request ) {
+               $handler = $this->get_search_handler( $request );
+               if ( is_wp_error( $handler ) ) {
+                       return $handler;
+               }
+
+               $result = $handler->search_items( $request );
+
+               if ( ! isset( $result[ WP_REST_Search_Handler::RESULT_IDS ] ) || ! is_array( $result[ WP_REST_Search_Handler::RESULT_IDS ] ) || ! isset( $result[ WP_REST_Search_Handler::RESULT_TOTAL ] ) ) {
+                       return new WP_Error( 'rest_search_handler_error', __( 'Internal search handler error.' ), array( 'status' => 500 ) );
+               }
+
+               $ids = array_map( 'absint', $result[ WP_REST_Search_Handler::RESULT_IDS ] );
+
+               $results = array();
+               foreach ( $ids as $id ) {
+                       $data      = $this->prepare_item_for_response( $id, $request );
+                       $results[] = $this->prepare_response_for_collection( $data );
+               }
+
+               $total     = (int) $result[ WP_REST_Search_Handler::RESULT_TOTAL ];
+               $page      = (int) $request['page'];
+               $per_page  = (int) $request['per_page'];
+               $max_pages = ceil( $total / $per_page );
+
+               if ( $page > $max_pages && $total > 0 ) {
+                       return new WP_Error( 'rest_search_invalid_page_number', __( 'The page number requested is larger than the number of pages available.' ), array( 'status' => 400 ) );
+               }
+
+               $response = rest_ensure_response( $results );
+               $response->header( 'X-WP-Total', $total );
+               $response->header( 'X-WP-TotalPages', $max_pages );
+
+               $request_params = $request->get_query_params();
+               $base           = add_query_arg( $request_params, rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );
+
+               if ( $page > 1 ) {
+                       $prev_link = add_query_arg( 'page', $page - 1, $base );
+                       $response->link_header( 'prev', $prev_link );
+               }
+               if ( $page < $max_pages ) {
+                       $next_link = add_query_arg( 'page', $page + 1, $base );
+                       $response->link_header( 'next', $next_link );
+               }
+
+               return $response;
+       }
+
+       /**
+        * Prepares a single search result for response.
+        *
+        * @since 5.0.0
+        *
+        * @param int             $id      ID of the item to prepare.
+        * @param WP_REST_Request $request Request object.
+        * @return WP_REST_Response Response object.
+        */
+       public function prepare_item_for_response( $id, $request ) {
+               $handler = $this->get_search_handler( $request );
+               if ( is_wp_error( $handler ) ) {
+                       return new WP_REST_Response();
+               }
+
+               $fields = $this->get_fields_for_response( $request );
+
+               $data = $handler->prepare_item( $id, $fields );
+               $data = $this->add_additional_fields_to_object( $data, $request );
+
+               $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
+               $data    = $this->filter_response_by_context( $data, $context );
+
+               $response = rest_ensure_response( $data );
+
+               $links               = $handler->prepare_item_links( $id );
+               $links['collection'] = array(
+                       'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
+               );
+               $response->add_links( $links );
+
+               return $response;
+       }
+
+       /**
+        * Retrieves the item schema, conforming to JSON Schema.
+        *
+        * @since 5.0.0
+        *
+        * @return array Item schema data.
+        */
+       public function get_item_schema() {
+               $types    = array();
+               $subtypes = array();
+               foreach ( $this->search_handlers as $search_handler ) {
+                       $types[]  = $search_handler->get_type();
+                       $subtypes = array_merge( $subtypes, $search_handler->get_subtypes() );
+               }
+
+               $types    = array_unique( $types );
+               $subtypes = array_unique( $subtypes );
+
+               $schema = array(
+                       '$schema'    => 'http://json-schema.org/draft-04/schema#',
+                       'title'      => 'search-result',
+                       'type'       => 'object',
+                       'properties' => array(
+                               self::PROP_ID      => array(
+                                       'description' => __( 'Unique identifier for the object.' ),
+                                       'type'        => 'integer',
+                                       'context'     => array( 'view', 'embed' ),
+                                       'readonly'    => true,
+                               ),
+                               self::PROP_TITLE   => array(
+                                       'description' => __( 'The title for the object.' ),
+                                       'type'        => 'string',
+                                       'context'     => array( 'view', 'embed' ),
+                                       'readonly'    => true,
+                               ),
+                               self::PROP_URL     => array(
+                                       'description' => __( 'URL to the object.' ),
+                                       'type'        => 'string',
+                                       'format'      => 'uri',
+                                       'context'     => array( 'view', 'embed' ),
+                                       'readonly'    => true,
+                               ),
+                               self::PROP_TYPE    => array(
+                                       'description' => __( 'Object type.' ),
+                                       'type'        => 'string',
+                                       'enum'        => $types,
+                                       'context'     => array( 'view', 'embed' ),
+                                       'readonly'    => true,
+                               ),
+                               self::PROP_SUBTYPE => array(
+                                       'description' => __( 'Object subtype.' ),
+                                       'type'        => 'string',
+                                       'enum'        => $subtypes,
+                                       'context'     => array( 'view', 'embed' ),
+                                       'readonly'    => true,
+                               ),
+                       ),
+               );
+
+               return $this->add_additional_fields_schema( $schema );
+       }
+
+       /**
+        * Retrieves the query params for the search results collection.
+        *
+        * @since 5.0.0
+        *
+        * @return array Collection parameters.
+        */
+       public function get_collection_params() {
+               $types    = array();
+               $subtypes = array();
+               foreach ( $this->search_handlers as $search_handler ) {
+                       $types[]  = $search_handler->get_type();
+                       $subtypes = array_merge( $subtypes, $search_handler->get_subtypes() );
+               }
+
+               $types    = array_unique( $types );
+               $subtypes = array_unique( $subtypes );
+
+               $query_params = parent::get_collection_params();
+
+               $query_params['context']['default'] = 'view';
+
+               $query_params[ self::PROP_TYPE ] = array(
+                       'default'     => $types[0],
+                       'description' => __( 'Limit results to items of an object type.' ),
+                       'type'        => 'string',
+                       'enum'        => $types,
+               );
+
+               $query_params[ self::PROP_SUBTYPE ] = array(
+                       'default'           => self::TYPE_ANY,
+                       'description'       => __( 'Limit results to items of one or more object subtypes.' ),
+                       'type'              => 'array',
+                       'items'             => array(
+                               'enum' => array_merge( $subtypes, array( self::TYPE_ANY ) ),
+                               'type' => 'string',
+                       ),
+                       'sanitize_callback' => array( $this, 'sanitize_subtypes' ),
+               );
+
+               return $query_params;
+       }
+
+       /**
+        * Sanitizes the list of subtypes, to ensure only subtypes of the passed type are included.
+        *
+        * @since 5.0.0
+        *
+        * @param string|array    $subtypes  One or more subtypes.
+        * @param WP_REST_Request $request   Full details about the request.
+        * @param string          $parameter Parameter name.
+        * @return array|WP_Error List of valid subtypes, or WP_Error object on failure.
+        */
+       public function sanitize_subtypes( $subtypes, $request, $parameter ) {
+               $subtypes = wp_parse_slug_list( $subtypes );
+
+               $subtypes = rest_parse_request_arg( $subtypes, $request, $parameter );
+               if ( is_wp_error( $subtypes ) ) {
+                       return $subtypes;
+               }
+
+               // 'any' overrides any other subtype.
+               if ( in_array( self::TYPE_ANY, $subtypes, true ) ) {
+                       return array( self::TYPE_ANY );
+               }
+
+               $handler = $this->get_search_handler( $request );
+               if ( is_wp_error( $handler ) ) {
+                       return $handler;
+               }
+
+               return array_intersect( $subtypes, $handler->get_subtypes() );
+       }
+
+       /**
+        * Gets the search handler to handle the current request.
+        *
+        * @since 5.0.0
+        *
+        * @param WP_REST_Request $request Full details about the request.
+        * @return WP_REST_Search_Handler|WP_Error Search handler for the request type, or WP_Error object on failure.
+        */
+       protected function get_search_handler( $request ) {
+               $type = $request->get_param( self::PROP_TYPE );
+
+               if ( ! $type || ! isset( $this->search_handlers[ $type ] ) ) {
+                       return new WP_Error( 'rest_search_invalid_type', __( 'Invalid type parameter.' ), array( 'status' => 400 ) );
+               }
+
+               return $this->search_handlers[ $type ];
+       }
+}
</ins></span></pre></div>
<a id="branches50srcwpincludesrestapisearchclasswprestpostsearchhandlerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: branches/5.0/src/wp-includes/rest-api/search/class-wp-rest-post-search-handler.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.0/src/wp-includes/rest-api/search/class-wp-rest-post-search-handler.php                                (rev 0)
+++ branches/5.0/src/wp-includes/rest-api/search/class-wp-rest-post-search-handler.php  2018-10-17 17:02:04 UTC (rev 43739)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,192 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * REST API: WP_REST_Post_Search_Handler class
+ *
+ * @package WordPress
+ * @subpackage REST_API
+ * @since 5.0.0
+ */
+
+/**
+ * Core class representing a search handler for posts in the REST API.
+ *
+ * @since 5.0.0
+ */
+class WP_REST_Post_Search_Handler extends WP_REST_Search_Handler {
+
+       /**
+        * Constructor.
+        *
+        * @since 5.0.0
+        */
+       public function __construct() {
+               $this->type = 'post';
+
+               // Support all public post types except attachments.
+               $this->subtypes = array_diff(
+                       array_values(
+                               get_post_types(
+                                       array(
+                                               'public'       => true,
+                                               'show_in_rest' => true,
+                                       ),
+                                       'names'
+                               )
+                       ),
+                       array( 'attachment' )
+               );
+       }
+
+       /**
+        * Searches the object type content for a given search request.
+        *
+        * @since 5.0.0
+        *
+        * @param WP_REST_Request $request Full REST request.
+        * @return array Associative array containing an `WP_REST_Search_Handler::RESULT_IDS` containing
+        *               an array of found IDs and `WP_REST_Search_Handler::RESULT_TOTAL` containing the
+        *               total count for the matching search results.
+        */
+       public function search_items( WP_REST_Request $request ) {
+
+               // Get the post types to search for the current request.
+               $post_types = $request[ WP_REST_Search_Controller::PROP_SUBTYPE ];
+               if ( in_array( WP_REST_Search_Controller::TYPE_ANY, $post_types, true ) ) {
+                       $post_types = $this->subtypes;
+               }
+
+               $query_args = array(
+                       'post_type'           => $post_types,
+                       'post_status'         => 'publish',
+                       'paged'               => (int) $request['page'],
+                       'posts_per_page'      => (int) $request['per_page'],
+                       'ignore_sticky_posts' => true,
+                       'fields'              => 'ids',
+               );
+
+               if ( ! empty( $request['search'] ) ) {
+                       $query_args['s'] = $request['search'];
+               }
+
+               $query     = new WP_Query();
+               $found_ids = $query->query( $query_args );
+               $total     = $query->found_posts;
+
+               return array(
+                       self::RESULT_IDS   => $found_ids,
+                       self::RESULT_TOTAL => $total,
+               );
+       }
+
+       /**
+        * Prepares the search result for a given ID.
+        *
+        * @since 5.0.0
+        *
+        * @param int   $id     Item ID.
+        * @param array $fields Fields to include for the item.
+        * @return array Associative array containing all fields for the item.
+        */
+       public function prepare_item( $id, array $fields ) {
+               $post = get_post( $id );
+
+               $data = array();
+
+               if ( in_array( WP_REST_Search_Controller::PROP_ID, $fields, true ) ) {
+                       $data[ WP_REST_Search_Controller::PROP_ID ] = (int) $post->ID;
+               }
+
+               if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) {
+                       if ( post_type_supports( $post->post_type, 'title' ) ) {
+                               add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
+                               $data[ WP_REST_Search_Controller::PROP_TITLE ] = get_the_title( $post->ID );
+                               remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
+                       } else {
+                               $data[ WP_REST_Search_Controller::PROP_TITLE ] = '';
+                       }
+               }
+
+               if ( in_array( WP_REST_Search_Controller::PROP_URL, $fields, true ) ) {
+                       $data[ WP_REST_Search_Controller::PROP_URL ] = get_permalink( $post->ID );
+               }
+
+               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 ] = $post->post_type;
+               }
+
+               return $data;
+       }
+
+       /**
+        * Prepares links for the search result of a given ID.
+        *
+        * @since 5.0.0
+        *
+        * @param int $id Item ID.
+        * @return array Links for the given item.
+        */
+       public function prepare_item_links( $id ) {
+               $post = get_post( $id );
+
+               $links = array();
+
+               $item_route = $this->detect_rest_item_route( $post );
+               if ( ! empty( $item_route ) ) {
+                       $links['self'] = array(
+                               'href'       => rest_url( $item_route ),
+                               'embeddable' => true,
+                       );
+               }
+
+               $links['about'] = array(
+                       'href' => rest_url( 'wp/v2/types/' . $post->post_type ),
+               );
+
+               return $links;
+       }
+
+       /**
+        * Overwrites the default protected title format.
+        *
+        * By default, WordPress will show password protected posts with a title of
+        * "Protected: %s". As the REST API communicates the protected status of a post
+        * in a machine readable format, we remove the "Protected: " prefix.
+        *
+        * @since 5.0.0
+        *
+        * @return string Protected title format.
+        */
+       public function protected_title_format() {
+               return '%s';
+       }
+
+       /**
+        * Attempts to detect the route to access a single item.
+        *
+        * @since 5.0.0
+        *
+        * @param WP_Post $post Post object.
+        * @return string REST route relative to the REST base URI, or empty string if unknown.
+        */
+       protected function detect_rest_item_route( $post ) {
+               $post_type = get_post_type_object( $post->post_type );
+               if ( ! $post_type ) {
+                       return '';
+               }
+
+               // It's currently impossible to detect the REST URL from a custom controller.
+               if ( ! empty( $post_type->rest_controller_class ) && 'WP_REST_Posts_Controller' !== $post_type->rest_controller_class ) {
+                       return '';
+               }
+
+               $namespace = 'wp/v2';
+               $rest_base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
+
+               return sprintf( '%s/%s/%d', $namespace, $rest_base, $post->ID );
+       }
+
+}
</ins></span></pre></div>
<a id="branches50srcwpincludesrestapisearchclasswprestsearchhandlerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: branches/5.0/src/wp-includes/rest-api/search/class-wp-rest-search-handler.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.0/src/wp-includes/rest-api/search/class-wp-rest-search-handler.php                             (rev 0)
+++ branches/5.0/src/wp-includes/rest-api/search/class-wp-rest-search-handler.php       2018-10-17 17:02:04 UTC (rev 43739)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,97 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * REST API: WP_REST_Search_Handler class
+ *
+ * @package WordPress
+ * @subpackage REST_API
+ * @since 5.0.0
+ */
+
+/**
+ * Core base class representing a search handler for an object type in the REST API.
+ *
+ * @since 5.0.0
+ */
+abstract class WP_REST_Search_Handler {
+
+       /**
+        * Field containing the IDs in the search result.
+        */
+       const RESULT_IDS = 'ids';
+
+       /**
+        * Field containing the total count in the search result.
+        */
+       const RESULT_TOTAL = 'total';
+
+       /**
+        * Object type managed by this search handler.
+        *
+        * @since 5.0.0
+        * @var string
+        */
+       protected $type = '';
+
+       /**
+        * Object subtypes managed by this search handler.
+        *
+        * @since 5.0.0
+        * @var array
+        */
+       protected $subtypes = array();
+
+       /**
+        * Gets the object type managed by this search handler.
+        *
+        * @since 5.0.0
+        *
+        * @return string Object type identifier.
+        */
+       public function get_type() {
+               return $this->type;
+       }
+
+       /**
+        * Gets the object subtypes managed by this search handler.
+        *
+        * @since 5.0.0
+        *
+        * @return array Array of object subtype identifiers.
+        */
+       public function get_subtypes() {
+               return $this->subtypes;
+       }
+
+       /**
+        * Searches the object type content for a given search request.
+        *
+        * @since 5.0.0
+        *
+        * @param WP_REST_Request $request Full REST request.
+        * @return array Associative array containing an `WP_REST_Search_Handler::RESULT_IDS` containing
+        *               an array of found IDs and `WP_REST_Search_Handler::RESULT_TOTAL` containing the
+        *               total count for the matching search results.
+        */
+       abstract public function search_items( WP_REST_Request $request );
+
+       /**
+        * Prepares the search result for a given ID.
+        *
+        * @since 5.0.0
+        *
+        * @param int   $id     Item ID.
+        * @param array $fields Fields to include for the item.
+        * @return array Associative array containing all fields for the item.
+        */
+       abstract public function prepare_item( $id, array $fields );
+
+       /**
+        * Prepares links for the search result of a given ID.
+        *
+        * @since 5.0.0
+        *
+        * @param int $id Item ID.
+        * @return array Links for the given item.
+        */
+       abstract public function prepare_item_links( $id );
+}
</ins></span></pre></div>
<a id="branches50srcwpincludesrestapiphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/5.0/src/wp-includes/rest-api.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.0/src/wp-includes/rest-api.php 2018-10-17 15:28:33 UTC (rev 43738)
+++ branches/5.0/src/wp-includes/rest-api.php   2018-10-17 17:02:04 UTC (rev 43739)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -230,6 +230,20 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $controller = new WP_REST_Comments_Controller;
</span><span class="cx" style="display: block; padding: 0 10px">        $controller->register_routes();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * Filters the search handlers to use in the REST search controller.
+        *
+        * @since 5.0.0
+        *
+        * @param array $search_handlers List of search handlers to use in the controller. Each search
+        *                               handler instance must extend the `WP_REST_Search_Handler` class.
+        *                               Default is only a handler for posts.
+        */
+       $search_handlers = apply_filters( 'wp_rest_search_handlers', array( new WP_REST_Post_Search_Handler() ) );
+
+       $controller = new WP_REST_Search_Controller( $search_handlers );
+       $controller->register_routes();
+
</ins><span class="cx" style="display: block; padding: 0 10px">         // Settings.
</span><span class="cx" style="display: block; padding: 0 10px">        $controller = new WP_REST_Settings_Controller;
</span><span class="cx" style="display: block; padding: 0 10px">        $controller->register_routes();
</span></span></pre></div>
<a id="branches50srcwpsettingsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/5.0/src/wp-settings.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.0/src/wp-settings.php  2018-10-17 15:28:33 UTC (rev 43738)
+++ branches/5.0/src/wp-settings.php    2018-10-17 17:02:04 UTC (rev 43739)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -233,6 +233,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-terms-controller.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-users-controller.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-comments-controller.php' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require( ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-search-controller.php' );
</ins><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-settings-controller.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-themes-controller.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-meta-fields.php' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -240,6 +241,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-post-meta-fields.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-term-meta-fields.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-user-meta-fields.php' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require( ABSPATH . WPINC . '/rest-api/search/class-wp-rest-search-handler.php' );
+require( ABSPATH . WPINC . '/rest-api/search/class-wp-rest-post-search-handler.php' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> $GLOBALS['wp_embed'] = new WP_Embed();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="branches50testsphpunitincludesbootstrapphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/5.0/tests/phpunit/includes/bootstrap.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.0/tests/phpunit/includes/bootstrap.php 2018-10-17 15:28:33 UTC (rev 43738)
+++ branches/5.0/tests/phpunit/includes/bootstrap.php   2018-10-17 17:02:04 UTC (rev 43739)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -117,6 +117,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require dirname( __FILE__ ) . '/exceptions.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require dirname( __FILE__ ) . '/utils.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require dirname( __FILE__ ) . '/spy-rest-server.php';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require dirname( __FILE__ ) . '/class-wp-rest-test-search-handler.php';
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px">  * A child class of the PHP test runner.
</span></span></pre></div>
<a id="branches50testsphpunitincludesclasswpresttestsearchhandlerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: branches/5.0/tests/phpunit/includes/class-wp-rest-test-search-handler.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.0/tests/phpunit/includes/class-wp-rest-test-search-handler.php                         (rev 0)
+++ branches/5.0/tests/phpunit/includes/class-wp-rest-test-search-handler.php   2018-10-17 17:02:04 UTC (rev 43739)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,91 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?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();
+       }
+}
</ins></span></pre></div>
<a id="branches50testsphpunittestsrestapirestschemasetupphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/5.0/tests/phpunit/tests/rest-api/rest-schema-setup.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.0/tests/phpunit/tests/rest-api/rest-schema-setup.php   2018-10-17 15:28:33 UTC (rev 43738)
+++ branches/5.0/tests/phpunit/tests/rest-api/rest-schema-setup.php     2018-10-17 17:02:04 UTC (rev 43739)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -110,6 +110,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        '/wp/v2/users/me',
</span><span class="cx" style="display: block; padding: 0 10px">                        '/wp/v2/comments',
</span><span class="cx" style="display: block; padding: 0 10px">                        '/wp/v2/comments/(?P<id>[\\d]+)',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        '/wp/v2/search',
</ins><span class="cx" style="display: block; padding: 0 10px">                         '/wp/v2/settings',
</span><span class="cx" style="display: block; padding: 0 10px">                        '/wp/v2/themes',
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span></span></pre></div>
<a id="branches50testsphpunittestsrestapirestsearchcontrollerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: branches/5.0/tests/phpunit/tests/rest-api/rest-search-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.0/tests/phpunit/tests/rest-api/rest-search-controller.php                              (rev 0)
+++ branches/5.0/tests/phpunit/tests/rest-api/rest-search-controller.php        2018-10-17 17:02:04 UTC (rev 43739)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,525 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * WP_REST_Search_Controller tests
+ *
+ * @package WordPress
+ * @subpackage REST_API
+ */
+
+/**
+ * Tests for WP_REST_Search_Controller.
+ *
+ * @group restapi
+ */
+class WP_Test_REST_Search_Controller extends WP_Test_REST_Controller_Testcase {
+
+       /**
+        * Posts with title 'my-footitle'.
+        *
+        * @var array
+        */
+       private static $my_title_post_ids = array();
+
+       /**
+        * Pages with title 'my-footitle'.
+        *
+        * @var array
+        */
+       private static $my_title_page_ids = array();
+
+       /**
+        * Posts with content 'my-foocontent'.
+        *
+        * @var array
+        */
+       private static $my_content_post_ids = array();
+
+       /**
+        * Create fake data before our tests run.
+        *
+        * @param WP_UnitTest_Factory $factory Helper that lets us create fake data.
+        */
+       public static function wpSetUpBeforeClass( $factory ) {
+               self::$my_title_post_ids = $factory->post->create_many(
+                       4,
+                       array(
+                               'post_title' => 'my-footitle',
+                               'post_type'  => 'post',
+                       )
+               );
+
+               self::$my_title_page_ids = $factory->post->create_many(
+                       4,
+                       array(
+                               'post_title' => 'my-footitle',
+                               'post_type'  => 'page',
+                       )
+               );
+
+               self::$my_content_post_ids = $factory->post->create_many(
+                       6,
+                       array(
+                               'post_content' => 'my-foocontent',
+                       )
+               );
+       }
+
+       /**
+        * Delete our fake data after our tests run.
+        */
+       public static function wpTearDownAfterClass() {
+               $post_ids = array_merge(
+                       self::$my_title_post_ids,
+                       self::$my_title_page_ids,
+                       self::$my_content_post_ids
+               );
+
+               foreach ( $post_ids as $post_id ) {
+                       wp_delete_post( $post_id, true );
+               }
+       }
+
+       /**
+        * Check that our routes get set up properly.
+        */
+       public function test_register_routes() {
+               $routes = rest_get_server()->get_routes();
+
+               $this->assertArrayHasKey( '/wp/v2/search', $routes );
+               $this->assertCount( 1, $routes['/wp/v2/search'] );
+       }
+
+       /**
+        * Check the context parameter.
+        */
+       public function test_context_param() {
+               $response = $this->do_request_with_params( array(), 'OPTIONS' );
+               $data     = $response->get_data();
+
+               $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
+               $this->assertEquals( array( 'view', 'embed' ), $data['endpoints'][0]['args']['context']['enum'] );
+       }
+
+       /**
+        * Search through all content.
+        */
+       public function test_get_items() {
+               $response = $this->do_request_with_params(
+                       array(
+                               'per_page' => 100,
+                       )
+               );
+
+               $this->assertEquals( 200, $response->get_status() );
+               $this->assertEqualSets(
+                       array_merge(
+                               self::$my_title_post_ids,
+                               self::$my_title_page_ids,
+                               self::$my_content_post_ids
+                       ),
+                       wp_list_pluck( $response->get_data(), 'id' )
+               );
+       }
+
+       /**
+        * Search through all content with a low limit.
+        */
+       public function test_get_items_with_limit() {
+               $response = $this->do_request_with_params(
+                       array(
+                               'per_page' => 3,
+                       )
+               );
+
+               $this->assertEquals( 200, $response->get_status() );
+               $this->assertEquals( 3, count( $response->get_data() ) );
+       }
+
+       /**
+        * Search through posts of any post type.
+        */
+       public function test_get_items_search_type_post() {
+               $response = $this->do_request_with_params(
+                       array(
+                               'per_page' => 100,
+                               'type'     => 'post',
+                       )
+               );
+
+               $this->assertEquals( 200, $response->get_status() );
+               $this->assertEqualSets(
+                       array_merge(
+                               self::$my_title_post_ids,
+                               self::$my_title_page_ids,
+                               self::$my_content_post_ids
+                       ),
+                       wp_list_pluck( $response->get_data(), 'id' )
+               );
+       }
+
+       /**
+        * Search through posts of post type 'post'.
+        */
+       public function test_get_items_search_type_post_subtype_post() {
+               $response = $this->do_request_with_params(
+                       array(
+                               'per_page' => 100,
+                               'type'     => 'post',
+                               'subtype'  => 'post',
+                       )
+               );
+
+               $this->assertEquals( 200, $response->get_status() );
+               $this->assertEqualSets(
+                       array_merge(
+                               self::$my_title_post_ids,
+                               self::$my_content_post_ids
+                       ),
+                       wp_list_pluck( $response->get_data(), 'id' )
+               );
+       }
+
+       /**
+        * Search through posts of post type 'page'.
+        */
+       public function test_get_items_search_type_post_subtype_page() {
+               $response = $this->do_request_with_params(
+                       array(
+                               'per_page' => 100,
+                               'type'     => 'post',
+                               'subtype'  => 'page',
+                       )
+               );
+
+               $this->assertEquals( 200, $response->get_status() );
+               $this->assertEqualSets(
+                       self::$my_title_page_ids,
+                       wp_list_pluck( $response->get_data(), 'id' )
+               );
+       }
+
+       /**
+        * Search through an invalid type
+        */
+       public function test_get_items_search_type_invalid() {
+               $response = $this->do_request_with_params(
+                       array(
+                               'per_page' => 100,
+                               'type'     => 'invalid',
+                       )
+               );
+
+               $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
+       }
+
+       /**
+        * Search through posts of an invalid post type.
+        */
+       public function test_get_items_search_type_post_subtype_invalid() {
+               $response = $this->do_request_with_params(
+                       array(
+                               'per_page' => 100,
+                               'type'     => 'post',
+                               'subtype'  => 'invalid',
+                       )
+               );
+
+               $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
+       }
+
+       /**
+        * Search through posts and pages.
+        */
+       public function test_get_items_search_posts_and_pages() {
+               $response = $this->do_request_with_params(
+                       array(
+                               'per_page' => 100,
+                               'type'     => 'post',
+                               'subtype'  => 'post,page',
+                       )
+               );
+
+               $this->assertEquals( 200, $response->get_status() );
+               $this->assertEqualSets(
+                       array_merge(
+                               self::$my_title_post_ids,
+                               self::$my_title_page_ids,
+                               self::$my_content_post_ids
+                       ),
+                       wp_list_pluck( $response->get_data(), 'id' )
+               );
+       }
+
+       /**
+        * Search through all that matches a 'footitle' search.
+        */
+       public function test_get_items_search_for_footitle() {
+               $response = $this->do_request_with_params(
+                       array(
+                               'per_page' => 100,
+                               'search'   => 'footitle',
+                       )
+               );
+
+               $this->assertEquals( 200, $response->get_status() );
+               $this->assertEqualSets(
+                       array_merge(
+                               self::$my_title_post_ids,
+                               self::$my_title_page_ids
+                       ),
+                       wp_list_pluck( $response->get_data(), 'id' )
+               );
+       }
+
+       /**
+        * Search through all that matches a 'foocontent' search.
+        */
+       public function test_get_items_search_for_foocontent() {
+               $response = $this->do_request_with_params(
+                       array(
+                               'per_page' => 100,
+                               'search'   => 'foocontent',
+                       )
+               );
+
+               $this->assertEquals( 200, $response->get_status() );
+               $this->assertEqualSets(
+                       self::$my_content_post_ids,
+                       wp_list_pluck( $response->get_data(), 'id' )
+               );
+       }
+
+       /**
+        * Test retrieving a single item isn't possible.
+        */
+       public function test_get_item() {
+               /** The search controller does not allow getting individual item content */
+               $request  = new WP_REST_Request( 'GET', '/wp/v2/search' . self::$my_title_post_ids[0] );
+               $response = rest_get_server()->dispatch( $request );
+               $this->assertEquals( 404, $response->get_status() );
+       }
+
+       /**
+        * Test creating an item isn't possible.
+        */
+       public function test_create_item() {
+               /** The search controller does not allow creating content */
+               $request  = new WP_REST_Request( 'POST', '/wp/v2/search' );
+               $response = rest_get_server()->dispatch( $request );
+               $this->assertEquals( 404, $response->get_status() );
+       }
+
+       /**
+        * Test updating an item isn't possible.
+        */
+       public function test_update_item() {
+               /** The search controller does not allow upading content */
+               $request  = new WP_REST_Request( 'POST', '/wp/v2/search' . self::$my_title_post_ids[0] );
+               $response = rest_get_server()->dispatch( $request );
+               $this->assertEquals( 404, $response->get_status() );
+       }
+
+       /**
+        * Test deleting an item isn't possible.
+        */
+       public function test_delete_item() {
+               /** The search controller does not allow deleting content */
+               $request  = new WP_REST_Request( 'DELETE', '/wp/v2/search' . self::$my_title_post_ids[0] );
+               $response = rest_get_server()->dispatch( $request );
+               $this->assertEquals( 404, $response->get_status() );
+       }
+
+       /**
+        * Test preparing the data contains the correct fields.
+        */
+       public function test_prepare_item() {
+               $response = $this->do_request_with_params();
+               $this->assertEquals( 200, $response->get_status() );
+
+               $data = $response->get_data();
+               $this->assertEquals(
+                       array(
+                               'id',
+                               'title',
+                               'url',
+                               'type',
+                               'subtype',
+                               '_links',
+                       ),
+                       array_keys( $data[0] )
+               );
+       }
+
+       /**
+        * Test preparing the data with limited fields contains the correct fields.
+        */
+       public function test_prepare_item_limit_fields() {
+               if ( ! method_exists( 'WP_REST_Controller', 'get_fields_for_response' ) ) {
+                       $this->markTestSkipped( 'Limiting fields requires the WP_REST_Controller::get_fields_for_response() method.' );
+               }
+
+               $response = $this->do_request_with_params(
+                       array(
+                               '_fields' => 'id,title',
+                       )
+               );
+               $this->assertEquals( 200, $response->get_status() );
+
+               $data = $response->get_data();
+               $this->assertEquals(
+                       array(
+                               'id',
+                               'title',
+                               '_links',
+                       ),
+                       array_keys( $data[0] )
+               );
+       }
+
+       /**
+        * Tests the item schema is correct.
+        */
+       public function test_get_item_schema() {
+               $request    = new WP_REST_Request( 'OPTIONS', '/wp/v2/search' );
+               $response   = rest_get_server()->dispatch( $request );
+               $data       = $response->get_data();
+               $properties = $data['schema']['properties'];
+
+               $this->assertArrayHasKey( 'id', $properties );
+               $this->assertArrayHasKey( 'title', $properties );
+               $this->assertArrayHasKey( 'url', $properties );
+               $this->assertArrayHasKey( 'type', $properties );
+               $this->assertArrayHasKey( 'subtype', $properties );
+       }
+
+       /**
+        * Tests that non-public post types are not allowed.
+        */
+       public function test_non_public_post_type() {
+               $response = $this->do_request_with_params(
+                       array(
+                               'type'    => 'post',
+                               'subtype' => 'post,nav_menu_item',
+                       )
+               );
+               $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
+       }
+
+       /**
+        * Test getting items directly with a custom search handler.
+        */
+       public function test_custom_search_handler_get_items() {
+               $controller = new WP_REST_Search_Controller( array( new WP_REST_Test_Search_Handler( 10 ) ) );
+
+               $request  = $this->get_request(
+                       array(
+                               'page'     => 1,
+                               'per_page' => 10,
+                               'type'     => 'test',
+                               'subtype'  => array( WP_REST_Search_Controller::TYPE_ANY ),
+                       )
+               );
+               $response = $controller->get_items( $request );
+               $this->assertEqualSets( range( 1, 10 ), wp_list_pluck( $response->get_data(), 'id' ) );
+
+               $request  = $this->get_request(
+                       array(
+                               'page'     => 1,
+                               'per_page' => 10,
+                               'type'     => 'test',
+                               'subtype'  => array( 'test_first_type' ),
+                       )
+               );
+               $response = $controller->get_items( $request );
+               $this->assertEqualSets( range( 1, 5 ), wp_list_pluck( $response->get_data(), 'id' ) );
+       }
+
+       /**
+        * Test preparing an item directly with a custom search handler.
+        */
+       public function test_custom_search_handler_prepare_item() {
+               $controller = new WP_REST_Search_Controller( array( new WP_REST_Test_Search_Handler( 10 ) ) );
+
+               $request  = $this->get_request(
+                       array(
+                               'type'    => 'test',
+                               'subtype' => array( WP_REST_Search_Controller::TYPE_ANY ),
+                       )
+               );
+               $response = $controller->prepare_item_for_response( 1, $request );
+               $data     = $response->get_data();
+               $this->assertEquals(
+                       array(
+                               'id',
+                               'title',
+                               'url',
+                               'type',
+                               'subtype',
+                       ),
+                       array_keys( $data )
+               );
+       }
+
+       /**
+        * Test preparing an item directly with a custom search handler with limited fields.
+        */
+       public function test_custom_search_handler_prepare_item_limit_fields() {
+               if ( ! method_exists( 'WP_REST_Controller', 'get_fields_for_response' ) ) {
+                       $this->markTestSkipped( 'Limiting fields requires the WP_REST_Controller::get_fields_for_response() method.' );
+               }
+
+               $controller = new WP_REST_Search_Controller( array( new WP_REST_Test_Search_Handler( 10 ) ) );
+
+               $request  = $this->get_request(
+                       array(
+                               'type'    => 'test',
+                               'subtype' => array( WP_REST_Search_Controller::TYPE_ANY ),
+                               '_fields' => 'id,title',
+                       )
+               );
+               $response = $controller->prepare_item_for_response( 1, $request );
+               $data     = $response->get_data();
+               $this->assertEquals(
+                       array(
+                               'id',
+                               'title',
+                       ),
+                       array_keys( $data )
+               );
+       }
+
+       /**
+        * Test getting the collection params directly with a custom search handler.
+        */
+       public function test_custom_search_handler_get_collection_params() {
+               $controller = new WP_REST_Search_Controller( array( new WP_REST_Test_Search_Handler( 10 ) ) );
+
+               $params = $controller->get_collection_params();
+               $this->assertEquals( 'test', $params[ WP_REST_Search_Controller::PROP_TYPE ]['default'] );
+               $this->assertEqualSets( array( 'test' ), $params[ WP_REST_Search_Controller::PROP_TYPE ]['enum'] );
+               $this->assertEqualSets( array( 'test_first_type', 'test_second_type', WP_REST_Search_Controller::TYPE_ANY ), $params[ WP_REST_Search_Controller::PROP_SUBTYPE ]['items']['enum'] );
+       }
+
+       /**
+        * Perform a REST request to our search endpoint with given parameters.
+        */
+       private function do_request_with_params( $params = array(), $method = 'GET' ) {
+               $request = $this->get_request( $params, $method );
+
+               return rest_get_server()->dispatch( $request );
+       }
+
+       /**
+        * Get a REST request object for given parameters.
+        */
+       private function get_request( $params = array(), $method = 'GET' ) {
+               $request = new WP_REST_Request( $method, '/wp/v2/search' );
+
+               foreach ( $params as $param => $value ) {
+                       $request->set_param( $param, $value );
+               }
+
+               return $request;
+       }
+
+}
</ins></span></pre></div>
<a id="branches50testsqunitfixtureswpapigeneratedjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/5.0/tests/qunit/fixtures/wp-api-generated.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/5.0/tests/qunit/fixtures/wp-api-generated.js     2018-10-17 15:28:33 UTC (rev 43738)
+++ branches/5.0/tests/qunit/fixtures/wp-api-generated.js       2018-10-17 17:02:04 UTC (rev 43739)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3406,6 +3406,74 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px">             ]
</span><span class="cx" style="display: block; padding: 0 10px">         },
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        "/wp/v2/search": {
+            "namespace": "wp/v2",
+            "methods": [
+                "GET"
+            ],
+            "endpoints": [
+                {
+                    "methods": [
+                        "GET"
+                    ],
+                    "args": {
+                        "context": {
+                            "required": false,
+                            "default": "view",
+                            "enum": [
+                                "view",
+                                "embed"
+                            ],
+                            "description": "Scope under which the request is made; determines fields present in response.",
+                            "type": "string"
+                        },
+                        "page": {
+                            "required": false,
+                            "default": 1,
+                            "description": "Current page of the collection.",
+                            "type": "integer"
+                        },
+                        "per_page": {
+                            "required": false,
+                            "default": 10,
+                            "description": "Maximum number of items to be returned in result set.",
+                            "type": "integer"
+                        },
+                        "search": {
+                            "required": false,
+                            "description": "Limit results to those matching a string.",
+                            "type": "string"
+                        },
+                        "type": {
+                            "required": false,
+                            "default": "post",
+                            "enum": [
+                                "post"
+                            ],
+                            "description": "Limit results to items of an object type.",
+                            "type": "string"
+                        },
+                        "subtype": {
+                            "required": false,
+                            "default": "any",
+                            "description": "Limit results to items of one or more object subtypes.",
+                            "type": "array",
+                            "items": {
+                                "enum": [
+                                    "post",
+                                    "page",
+                                    "any"
+                                ],
+                                "type": "string"
+                            }
+                        }
+                    }
+                }
+            ],
+            "_links": {
+                "self": "http://example.org/index.php?rest_route=/wp/v2/search"
+            }
+        },
</ins><span class="cx" style="display: block; padding: 0 10px">         "/wp/v2/settings": {
</span><span class="cx" style="display: block; padding: 0 10px">             "namespace": "wp/v2",
</span><span class="cx" style="display: block; padding: 0 10px">             "methods": [
</span></span></pre>
</div>
</div>

</body>
</html>