wp/wp-includes/rest-api/search/class-wp-rest-post-search-handler.php
changeset 21 48c4eec2b7e6
parent 18 be944660c56a
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    38 			array( 'attachment' )
    38 			array( 'attachment' )
    39 		);
    39 		);
    40 	}
    40 	}
    41 
    41 
    42 	/**
    42 	/**
    43 	 * Searches the object type content for a given search request.
    43 	 * Searches posts for a given search request.
    44 	 *
    44 	 *
    45 	 * @since 5.0.0
    45 	 * @since 5.0.0
    46 	 *
    46 	 *
    47 	 * @param WP_REST_Request $request Full REST request.
    47 	 * @param WP_REST_Request $request Full REST request.
    48 	 * @return array Associative array containing an `WP_REST_Search_Handler::RESULT_IDS` containing
    48 	 * @return array {
    49 	 *               an array of found IDs and `WP_REST_Search_Handler::RESULT_TOTAL` containing the
    49 	 *     Associative array containing found IDs and total count for the matching search results.
    50 	 *               total count for the matching search results.
    50 	 *
       
    51 	 *     @type int[] $ids   Array containing the matching post IDs.
       
    52 	 *     @type int   $total Total count for the matching search results.
       
    53 	 * }
    51 	 */
    54 	 */
    52 	public function search_items( WP_REST_Request $request ) {
    55 	public function search_items( WP_REST_Request $request ) {
    53 
    56 
    54 		// Get the post types to search for the current request.
    57 		// Get the post types to search for the current request.
    55 		$post_types = $request[ WP_REST_Search_Controller::PROP_SUBTYPE ];
    58 		$post_types = $request[ WP_REST_Search_Controller::PROP_SUBTYPE ];
    61 			'post_type'           => $post_types,
    64 			'post_type'           => $post_types,
    62 			'post_status'         => 'publish',
    65 			'post_status'         => 'publish',
    63 			'paged'               => (int) $request['page'],
    66 			'paged'               => (int) $request['page'],
    64 			'posts_per_page'      => (int) $request['per_page'],
    67 			'posts_per_page'      => (int) $request['per_page'],
    65 			'ignore_sticky_posts' => true,
    68 			'ignore_sticky_posts' => true,
    66 			'fields'              => 'ids',
       
    67 		);
    69 		);
    68 
    70 
    69 		if ( ! empty( $request['search'] ) ) {
    71 		if ( ! empty( $request['search'] ) ) {
    70 			$query_args['s'] = $request['search'];
    72 			$query_args['s'] = $request['search'];
    71 		}
    73 		}
    72 
    74 
       
    75 		if ( ! empty( $request['exclude'] ) ) {
       
    76 			$query_args['post__not_in'] = $request['exclude'];
       
    77 		}
       
    78 
       
    79 		if ( ! empty( $request['include'] ) ) {
       
    80 			$query_args['post__in'] = $request['include'];
       
    81 		}
       
    82 
    73 		/**
    83 		/**
    74 		 * Filters the query arguments for a REST API search request.
    84 		 * Filters the query arguments for a REST API post search request.
    75 		 *
    85 		 *
    76 		 * Enables adding extra arguments or setting defaults for a post search request.
    86 		 * Enables adding extra arguments or setting defaults for a post search request.
    77 		 *
    87 		 *
    78 		 * @since 5.1.0
    88 		 * @since 5.1.0
    79 		 *
    89 		 *
    80 		 * @param array           $query_args Key value array of query var to query value.
    90 		 * @param array           $query_args Key value array of query var to query value.
    81 		 * @param WP_REST_Request $request    The request used.
    91 		 * @param WP_REST_Request $request    The request used.
    82 		 */
    92 		 */
    83 		$query_args = apply_filters( 'rest_post_search_query', $query_args, $request );
    93 		$query_args = apply_filters( 'rest_post_search_query', $query_args, $request );
    84 
    94 
    85 		$query     = new WP_Query();
    95 		$query = new WP_Query();
    86 		$found_ids = $query->query( $query_args );
    96 		$posts = $query->query( $query_args );
       
    97 		// Querying the whole post object will warm the object cache, avoiding an extra query per result.
       
    98 		$found_ids = wp_list_pluck( $posts, 'ID' );
    87 		$total     = $query->found_posts;
    99 		$total     = $query->found_posts;
    88 
   100 
    89 		return array(
   101 		return array(
    90 			self::RESULT_IDS   => $found_ids,
   102 			self::RESULT_IDS   => $found_ids,
    91 			self::RESULT_TOTAL => $total,
   103 			self::RESULT_TOTAL => $total,
    92 		);
   104 		);
    93 	}
   105 	}
    94 
   106 
    95 	/**
   107 	/**
    96 	 * Prepares the search result for a given ID.
   108 	 * Prepares the search result for a given post ID.
    97 	 *
   109 	 *
    98 	 * @since 5.0.0
   110 	 * @since 5.0.0
    99 	 *
   111 	 *
   100 	 * @param int   $id     Item ID.
   112 	 * @param int   $id     Post ID.
   101 	 * @param array $fields Fields to include for the item.
   113 	 * @param array $fields Fields to include for the post.
   102 	 * @return array Associative array containing all fields for the item.
   114 	 * @return array {
       
   115 	 *     Associative array containing fields for the post based on the `$fields` parameter.
       
   116 	 *
       
   117 	 *     @type int    $id    Optional. Post ID.
       
   118 	 *     @type string $title Optional. Post title.
       
   119 	 *     @type string $url   Optional. Post permalink URL.
       
   120 	 *     @type string $type  Optional. Post type.
       
   121 	 * }
   103 	 */
   122 	 */
   104 	public function prepare_item( $id, array $fields ) {
   123 	public function prepare_item( $id, array $fields ) {
   105 		$post = get_post( $id );
   124 		$post = get_post( $id );
   106 
   125 
   107 		$data = array();
   126 		$data = array();
   191 	protected function detect_rest_item_route( $post ) {
   210 	protected function detect_rest_item_route( $post ) {
   192 		_deprecated_function( __METHOD__, '5.5.0', 'rest_get_route_for_post()' );
   211 		_deprecated_function( __METHOD__, '5.5.0', 'rest_get_route_for_post()' );
   193 
   212 
   194 		return rest_get_route_for_post( $post );
   213 		return rest_get_route_for_post( $post );
   195 	}
   214 	}
   196 
       
   197 }
   215 }