wp/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
    48 
    48 
    49 	/**
    49 	/**
    50 	 * Search handlers used by the controller.
    50 	 * Search handlers used by the controller.
    51 	 *
    51 	 *
    52 	 * @since 5.0.0
    52 	 * @since 5.0.0
    53 	 * @var array
    53 	 * @var WP_REST_Search_Handler[]
    54 	 */
    54 	 */
    55 	protected $search_handlers = array();
    55 	protected $search_handlers = array();
    56 
    56 
    57 	/**
    57 	/**
    58 	 * Constructor.
    58 	 * Constructor.
   184 	/**
   184 	/**
   185 	 * Prepares a single search result for response.
   185 	 * Prepares a single search result for response.
   186 	 *
   186 	 *
   187 	 * @since 5.0.0
   187 	 * @since 5.0.0
   188 	 * @since 5.6.0 The `$id` parameter can accept a string.
   188 	 * @since 5.6.0 The `$id` parameter can accept a string.
   189 	 *
   189 	 * @since 5.9.0 Renamed `$id` to `$item` to match parent class for PHP 8 named parameter support.
   190 	 * @param int|string      $id      ID of the item to prepare.
   190 	 *
       
   191 	 * @param int|string      $item    ID of the item to prepare.
   191 	 * @param WP_REST_Request $request Request object.
   192 	 * @param WP_REST_Request $request Request object.
   192 	 * @return WP_REST_Response Response object.
   193 	 * @return WP_REST_Response Response object.
   193 	 */
   194 	 */
   194 	public function prepare_item_for_response( $id, $request ) {
   195 	public function prepare_item_for_response( $item, $request ) {
       
   196 		// Restores the more descriptive, specific name for use within this method.
       
   197 		$item_id = $item;
   195 		$handler = $this->get_search_handler( $request );
   198 		$handler = $this->get_search_handler( $request );
   196 		if ( is_wp_error( $handler ) ) {
   199 		if ( is_wp_error( $handler ) ) {
   197 			return new WP_REST_Response();
   200 			return new WP_REST_Response();
   198 		}
   201 		}
   199 
   202 
   200 		$fields = $this->get_fields_for_response( $request );
   203 		$fields = $this->get_fields_for_response( $request );
   201 
   204 
   202 		$data = $handler->prepare_item( $id, $fields );
   205 		$data = $handler->prepare_item( $item_id, $fields );
   203 		$data = $this->add_additional_fields_to_object( $data, $request );
   206 		$data = $this->add_additional_fields_to_object( $data, $request );
   204 
   207 
   205 		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
   208 		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
   206 		$data    = $this->filter_response_by_context( $data, $context );
   209 		$data    = $this->filter_response_by_context( $data, $context );
   207 
   210 
   208 		$response = rest_ensure_response( $data );
   211 		$response = rest_ensure_response( $data );
   209 
   212 
   210 		$links               = $handler->prepare_item_links( $id );
   213 		$links               = $handler->prepare_item_links( $item_id );
   211 		$links['collection'] = array(
   214 		$links['collection'] = array(
   212 			'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
   215 			'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
   213 		);
   216 		);
   214 		$response->add_links( $links );
   217 		$response->add_links( $links );
   215 
   218