wp/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
   150 		}
   150 		}
   151 
   151 
   152 		$total     = (int) $result[ WP_REST_Search_Handler::RESULT_TOTAL ];
   152 		$total     = (int) $result[ WP_REST_Search_Handler::RESULT_TOTAL ];
   153 		$page      = (int) $request['page'];
   153 		$page      = (int) $request['page'];
   154 		$per_page  = (int) $request['per_page'];
   154 		$per_page  = (int) $request['per_page'];
   155 		$max_pages = ceil( $total / $per_page );
   155 		$max_pages = (int) ceil( $total / $per_page );
   156 
   156 
   157 		if ( $page > $max_pages && $total > 0 ) {
   157 		if ( $page > $max_pages && $total > 0 ) {
   158 			return new WP_Error(
   158 			return new WP_Error(
   159 				'rest_search_invalid_page_number',
   159 				'rest_search_invalid_page_number',
   160 				__( 'The page number requested is larger than the number of pages available.' ),
   160 				__( 'The page number requested is larger than the number of pages available.' ),
   193 	 * @return WP_REST_Response Response object.
   193 	 * @return WP_REST_Response Response object.
   194 	 */
   194 	 */
   195 	public function prepare_item_for_response( $item, $request ) {
   195 	public function prepare_item_for_response( $item, $request ) {
   196 		// Restores the more descriptive, specific name for use within this method.
   196 		// Restores the more descriptive, specific name for use within this method.
   197 		$item_id = $item;
   197 		$item_id = $item;
       
   198 
   198 		$handler = $this->get_search_handler( $request );
   199 		$handler = $this->get_search_handler( $request );
   199 		if ( is_wp_error( $handler ) ) {
   200 		if ( is_wp_error( $handler ) ) {
   200 			return new WP_REST_Response();
   201 			return new WP_REST_Response();
   201 		}
   202 		}
   202 
   203 
   208 		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
   209 		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
   209 		$data    = $this->filter_response_by_context( $data, $context );
   210 		$data    = $this->filter_response_by_context( $data, $context );
   210 
   211 
   211 		$response = rest_ensure_response( $data );
   212 		$response = rest_ensure_response( $data );
   212 
   213 
   213 		$links               = $handler->prepare_item_links( $item_id );
   214 		if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
   214 		$links['collection'] = array(
   215 			$links               = $handler->prepare_item_links( $item_id );
   215 			'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
   216 			$links['collection'] = array(
   216 		);
   217 				'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
   217 		$response->add_links( $links );
   218 			);
       
   219 			$response->add_links( $links );
       
   220 		}
   218 
   221 
   219 		return $response;
   222 		return $response;
   220 	}
   223 	}
   221 
   224 
   222 	/**
   225 	/**
   327 				'type' => 'string',
   330 				'type' => 'string',
   328 			),
   331 			),
   329 			'sanitize_callback' => array( $this, 'sanitize_subtypes' ),
   332 			'sanitize_callback' => array( $this, 'sanitize_subtypes' ),
   330 		);
   333 		);
   331 
   334 
       
   335 		$query_params['exclude'] = array(
       
   336 			'description' => __( 'Ensure result set excludes specific IDs.' ),
       
   337 			'type'        => 'array',
       
   338 			'items'       => array(
       
   339 				'type' => 'integer',
       
   340 			),
       
   341 			'default'     => array(),
       
   342 		);
       
   343 
       
   344 		$query_params['include'] = array(
       
   345 			'description' => __( 'Limit result set to specific IDs.' ),
       
   346 			'type'        => 'array',
       
   347 			'items'       => array(
       
   348 				'type' => 'integer',
       
   349 			),
       
   350 			'default'     => array(),
       
   351 		);
       
   352 
   332 		return $query_params;
   353 		return $query_params;
   333 	}
   354 	}
   334 
   355 
   335 	/**
   356 	/**
   336 	 * Sanitizes the list of subtypes, to ensure only subtypes of the passed type are included.
   357 	 * Sanitizes the list of subtypes, to ensure only subtypes of the passed type are included.
   338 	 * @since 5.0.0
   359 	 * @since 5.0.0
   339 	 *
   360 	 *
   340 	 * @param string|array    $subtypes  One or more subtypes.
   361 	 * @param string|array    $subtypes  One or more subtypes.
   341 	 * @param WP_REST_Request $request   Full details about the request.
   362 	 * @param WP_REST_Request $request   Full details about the request.
   342 	 * @param string          $parameter Parameter name.
   363 	 * @param string          $parameter Parameter name.
   343 	 * @return array|WP_Error List of valid subtypes, or WP_Error object on failure.
   364 	 * @return string[]|WP_Error List of valid subtypes, or WP_Error object on failure.
   344 	 */
   365 	 */
   345 	public function sanitize_subtypes( $subtypes, $request, $parameter ) {
   366 	public function sanitize_subtypes( $subtypes, $request, $parameter ) {
   346 		$subtypes = wp_parse_slug_list( $subtypes );
   367 		$subtypes = wp_parse_slug_list( $subtypes );
   347 
   368 
   348 		$subtypes = rest_parse_request_arg( $subtypes, $request, $parameter );
   369 		$subtypes = rest_parse_request_arg( $subtypes, $request, $parameter );
   372 	 * @return WP_REST_Search_Handler|WP_Error Search handler for the request type, or WP_Error object on failure.
   393 	 * @return WP_REST_Search_Handler|WP_Error Search handler for the request type, or WP_Error object on failure.
   373 	 */
   394 	 */
   374 	protected function get_search_handler( $request ) {
   395 	protected function get_search_handler( $request ) {
   375 		$type = $request->get_param( self::PROP_TYPE );
   396 		$type = $request->get_param( self::PROP_TYPE );
   376 
   397 
   377 		if ( ! $type || ! isset( $this->search_handlers[ $type ] ) ) {
   398 		if ( ! $type || ! is_string( $type ) || ! isset( $this->search_handlers[ $type ] ) ) {
   378 			return new WP_Error(
   399 			return new WP_Error(
   379 				'rest_search_invalid_type',
   400 				'rest_search_invalid_type',
   380 				__( 'Invalid type parameter.' ),
   401 				__( 'Invalid type parameter.' ),
   381 				array( 'status' => 400 )
   402 				array( 'status' => 400 )
   382 			);
   403 			);