wp/wp-includes/rest-api/search/class-wp-rest-post-search-handler.php
changeset 9 177826044cd9
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
       
     1 <?php
       
     2 /**
       
     3  * REST API: WP_REST_Post_Search_Handler class
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage REST_API
       
     7  * @since 5.0.0
       
     8  */
       
     9 
       
    10 /**
       
    11  * Core class representing a search handler for posts in the REST API.
       
    12  *
       
    13  * @since 5.0.0
       
    14  */
       
    15 class WP_REST_Post_Search_Handler extends WP_REST_Search_Handler {
       
    16 
       
    17 	/**
       
    18 	 * Constructor.
       
    19 	 *
       
    20 	 * @since 5.0.0
       
    21 	 */
       
    22 	public function __construct() {
       
    23 		$this->type = 'post';
       
    24 
       
    25 		// Support all public post types except attachments.
       
    26 		$this->subtypes = array_diff(
       
    27 			array_values(
       
    28 				get_post_types(
       
    29 					array(
       
    30 						'public'       => true,
       
    31 						'show_in_rest' => true,
       
    32 					),
       
    33 					'names'
       
    34 				)
       
    35 			),
       
    36 			array( 'attachment' )
       
    37 		);
       
    38 	}
       
    39 
       
    40 	/**
       
    41 	 * Searches the object type content for a given search request.
       
    42 	 *
       
    43 	 * @since 5.0.0
       
    44 	 *
       
    45 	 * @param WP_REST_Request $request Full REST request.
       
    46 	 * @return array Associative array containing an `WP_REST_Search_Handler::RESULT_IDS` containing
       
    47 	 *               an array of found IDs and `WP_REST_Search_Handler::RESULT_TOTAL` containing the
       
    48 	 *               total count for the matching search results.
       
    49 	 */
       
    50 	public function search_items( WP_REST_Request $request ) {
       
    51 
       
    52 		// Get the post types to search for the current request.
       
    53 		$post_types = $request[ WP_REST_Search_Controller::PROP_SUBTYPE ];
       
    54 		if ( in_array( WP_REST_Search_Controller::TYPE_ANY, $post_types, true ) ) {
       
    55 			$post_types = $this->subtypes;
       
    56 		}
       
    57 
       
    58 		$query_args = array(
       
    59 			'post_type'           => $post_types,
       
    60 			'post_status'         => 'publish',
       
    61 			'paged'               => (int) $request['page'],
       
    62 			'posts_per_page'      => (int) $request['per_page'],
       
    63 			'ignore_sticky_posts' => true,
       
    64 			'fields'              => 'ids',
       
    65 		);
       
    66 
       
    67 		if ( ! empty( $request['search'] ) ) {
       
    68 			$query_args['s'] = $request['search'];
       
    69 		}
       
    70 
       
    71 		/**
       
    72 		 * Filters the query arguments for a search request.
       
    73 		 *
       
    74 		 * Enables adding extra arguments or setting defaults for a post search request.
       
    75 		 *
       
    76 		 * @since 5.1.0
       
    77 		 *
       
    78 		 * @param array           $query_args Key value array of query var to query value.
       
    79 		 * @param WP_REST_Request $request    The request used.
       
    80 		 */
       
    81 		$query_args = apply_filters( 'rest_post_search_query', $query_args, $request );
       
    82 
       
    83 		$query     = new WP_Query();
       
    84 		$found_ids = $query->query( $query_args );
       
    85 		$total     = $query->found_posts;
       
    86 
       
    87 		return array(
       
    88 			self::RESULT_IDS   => $found_ids,
       
    89 			self::RESULT_TOTAL => $total,
       
    90 		);
       
    91 	}
       
    92 
       
    93 	/**
       
    94 	 * Prepares the search result for a given ID.
       
    95 	 *
       
    96 	 * @since 5.0.0
       
    97 	 *
       
    98 	 * @param int   $id     Item ID.
       
    99 	 * @param array $fields Fields to include for the item.
       
   100 	 * @return array Associative array containing all fields for the item.
       
   101 	 */
       
   102 	public function prepare_item( $id, array $fields ) {
       
   103 		$post = get_post( $id );
       
   104 
       
   105 		$data = array();
       
   106 
       
   107 		if ( in_array( WP_REST_Search_Controller::PROP_ID, $fields, true ) ) {
       
   108 			$data[ WP_REST_Search_Controller::PROP_ID ] = (int) $post->ID;
       
   109 		}
       
   110 
       
   111 		if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) {
       
   112 			if ( post_type_supports( $post->post_type, 'title' ) ) {
       
   113 				add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
       
   114 				$data[ WP_REST_Search_Controller::PROP_TITLE ] = get_the_title( $post->ID );
       
   115 				remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
       
   116 			} else {
       
   117 				$data[ WP_REST_Search_Controller::PROP_TITLE ] = '';
       
   118 			}
       
   119 		}
       
   120 
       
   121 		if ( in_array( WP_REST_Search_Controller::PROP_URL, $fields, true ) ) {
       
   122 			$data[ WP_REST_Search_Controller::PROP_URL ] = get_permalink( $post->ID );
       
   123 		}
       
   124 
       
   125 		if ( in_array( WP_REST_Search_Controller::PROP_TYPE, $fields, true ) ) {
       
   126 			$data[ WP_REST_Search_Controller::PROP_TYPE ] = $this->type;
       
   127 		}
       
   128 
       
   129 		if ( in_array( WP_REST_Search_Controller::PROP_SUBTYPE, $fields, true ) ) {
       
   130 			$data[ WP_REST_Search_Controller::PROP_SUBTYPE ] = $post->post_type;
       
   131 		}
       
   132 
       
   133 		return $data;
       
   134 	}
       
   135 
       
   136 	/**
       
   137 	 * Prepares links for the search result of a given ID.
       
   138 	 *
       
   139 	 * @since 5.0.0
       
   140 	 *
       
   141 	 * @param int $id Item ID.
       
   142 	 * @return array Links for the given item.
       
   143 	 */
       
   144 	public function prepare_item_links( $id ) {
       
   145 		$post = get_post( $id );
       
   146 
       
   147 		$links = array();
       
   148 
       
   149 		$item_route = $this->detect_rest_item_route( $post );
       
   150 		if ( ! empty( $item_route ) ) {
       
   151 			$links['self'] = array(
       
   152 				'href'       => rest_url( $item_route ),
       
   153 				'embeddable' => true,
       
   154 			);
       
   155 		}
       
   156 
       
   157 		$links['about'] = array(
       
   158 			'href' => rest_url( 'wp/v2/types/' . $post->post_type ),
       
   159 		);
       
   160 
       
   161 		return $links;
       
   162 	}
       
   163 
       
   164 	/**
       
   165 	 * Overwrites the default protected title format.
       
   166 	 *
       
   167 	 * By default, WordPress will show password protected posts with a title of
       
   168 	 * "Protected: %s". As the REST API communicates the protected status of a post
       
   169 	 * in a machine readable format, we remove the "Protected: " prefix.
       
   170 	 *
       
   171 	 * @since 5.0.0
       
   172 	 *
       
   173 	 * @return string Protected title format.
       
   174 	 */
       
   175 	public function protected_title_format() {
       
   176 		return '%s';
       
   177 	}
       
   178 
       
   179 	/**
       
   180 	 * Attempts to detect the route to access a single item.
       
   181 	 *
       
   182 	 * @since 5.0.0
       
   183 	 *
       
   184 	 * @param WP_Post $post Post object.
       
   185 	 * @return string REST route relative to the REST base URI, or empty string if unknown.
       
   186 	 */
       
   187 	protected function detect_rest_item_route( $post ) {
       
   188 		$post_type = get_post_type_object( $post->post_type );
       
   189 		if ( ! $post_type ) {
       
   190 			return '';
       
   191 		}
       
   192 
       
   193 		// It's currently impossible to detect the REST URL from a custom controller.
       
   194 		if ( ! empty( $post_type->rest_controller_class ) && 'WP_REST_Posts_Controller' !== $post_type->rest_controller_class ) {
       
   195 			return '';
       
   196 		}
       
   197 
       
   198 		$namespace = 'wp/v2';
       
   199 		$rest_base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
       
   200 
       
   201 		return sprintf( '%s/%s/%d', $namespace, $rest_base, $post->ID );
       
   202 	}
       
   203 
       
   204 }