class-wp-rest-post-search-handler.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. * Core class representing a search handler for posts in the REST API.
  11. *
  12. * @since 5.0.0
  13. *
  14. * @see WP_REST_Search_Handler
  15. */
  16. class WP_REST_Post_Search_Handler extends WP_REST_Search_Handler {
  17. /**
  18. * Constructor.
  19. *
  20. * @since 5.0.0
  21. */
  22. public function __construct() {
  23. $this->type = 'post';
  24. // Support all public post types except attachments.
  25. $this->subtypes = array_diff(
  26. array_values(
  27. get_post_types(
  28. array(
  29. 'public' => true,
  30. 'show_in_rest' => true,
  31. ),
  32. 'names'
  33. )
  34. ),
  35. array( 'attachment' )
  36. );
  37. }
  38. /**
  39. * Searches the object type content for a given search request.
  40. *
  41. * @since 5.0.0
  42. *
  43. * @param WP_REST_Request $request Full REST request.
  44. * @return array Associative array containing an `WP_REST_Search_Handler::RESULT_IDS` containing
  45. * an array of found IDs and `WP_REST_Search_Handler::RESULT_TOTAL` containing the
  46. * total count for the matching search results.
  47. */
  48. public function search_items( WP_REST_Request $request ) {
  49. // Get the post types to search for the current request.
  50. $post_types = $request[ WP_REST_Search_Controller::PROP_SUBTYPE ];
  51. if ( in_array( WP_REST_Search_Controller::TYPE_ANY, $post_types, true ) ) {
  52. $post_types = $this->subtypes;
  53. }
  54. $query_args = array(
  55. 'post_type' => $post_types,
  56. 'post_status' => 'publish',
  57. 'paged' => (int) $request['page'],
  58. 'posts_per_page' => (int) $request['per_page'],
  59. 'ignore_sticky_posts' => true,
  60. );
  61. if ( ! empty( $request['search'] ) ) {
  62. $query_args['s'] = $request['search'];
  63. }
  64. if ( ! empty( $request['exclude'] ) ) {
  65. $query_args['post__not_in'] = $request['exclude'];
  66. }
  67. if ( ! empty( $request['include'] ) ) {
  68. $query_args['post__in'] = $request['include'];
  69. }
  70. /**
  71. * Filters the query arguments for a REST API search request.
  72. *
  73. * Enables adding extra arguments or setting defaults for a post search request.
  74. *
  75. * @since 5.1.0
  76. *
  77. * @param array $query_args Key value array of query var to query value.
  78. * @param WP_REST_Request $request The request used.
  79. */
  80. $query_args = apply_filters( 'rest_post_search_query', $query_args, $request );
  81. $query = new WP_Query();
  82. $posts = $query->query( $query_args );
  83. // Querying the whole post object will warm the object cache, avoiding an extra query per result.
  84. $found_ids = wp_list_pluck( $posts, 'ID' );
  85. $total = $query->found_posts;
  86. return array(
  87. self::RESULT_IDS => $found_ids,
  88. self::RESULT_TOTAL => $total,
  89. );
  90. }
  91. /**
  92. * Prepares the search result for a given ID.
  93. *
  94. * @since 5.0.0
  95. *
  96. * @param int $id Item ID.
  97. * @param array $fields Fields to include for the item.
  98. * @return array Associative array containing all fields for the item.
  99. */
  100. public function prepare_item( $id, array $fields ) {
  101. $post = get_post( $id );
  102. $data = array();
  103. if ( in_array( WP_REST_Search_Controller::PROP_ID, $fields, true ) ) {
  104. $data[ WP_REST_Search_Controller::PROP_ID ] = (int) $post->ID;
  105. }
  106. if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) {
  107. if ( post_type_supports( $post->post_type, 'title' ) ) {
  108. add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
  109. $data[ WP_REST_Search_Controller::PROP_TITLE ] = get_the_title( $post->ID );
  110. remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
  111. } else {
  112. $data[ WP_REST_Search_Controller::PROP_TITLE ] = '';
  113. }
  114. }
  115. if ( in_array( WP_REST_Search_Controller::PROP_URL, $fields, true ) ) {
  116. $data[ WP_REST_Search_Controller::PROP_URL ] = get_permalink( $post->ID );
  117. }
  118. if ( in_array( WP_REST_Search_Controller::PROP_TYPE, $fields, true ) ) {
  119. $data[ WP_REST_Search_Controller::PROP_TYPE ] = $this->type;
  120. }
  121. if ( in_array( WP_REST_Search_Controller::PROP_SUBTYPE, $fields, true ) ) {
  122. $data[ WP_REST_Search_Controller::PROP_SUBTYPE ] = $post->post_type;
  123. }
  124. return $data;
  125. }
  126. /**
  127. * Prepares links for the search result of a given ID.
  128. *
  129. * @since 5.0.0
  130. *
  131. * @param int $id Item ID.
  132. * @return array Links for the given item.
  133. */
  134. public function prepare_item_links( $id ) {
  135. $post = get_post( $id );
  136. $links = array();
  137. $item_route = rest_get_route_for_post( $post );
  138. if ( ! empty( $item_route ) ) {
  139. $links['self'] = array(
  140. 'href' => rest_url( $item_route ),
  141. 'embeddable' => true,
  142. );
  143. }
  144. $links['about'] = array(
  145. 'href' => rest_url( 'wp/v2/types/' . $post->post_type ),
  146. );
  147. return $links;
  148. }
  149. /**
  150. * Overwrites the default protected title format.
  151. *
  152. * By default, WordPress will show password protected posts with a title of
  153. * "Protected: %s". As the REST API communicates the protected status of a post
  154. * in a machine readable format, we remove the "Protected: " prefix.
  155. *
  156. * @since 5.0.0
  157. *
  158. * @return string Protected title format.
  159. */
  160. public function protected_title_format() {
  161. return '%s';
  162. }
  163. /**
  164. * Attempts to detect the route to access a single item.
  165. *
  166. * @since 5.0.0
  167. * @deprecated 5.5.0 Use rest_get_route_for_post()
  168. * @see rest_get_route_for_post()
  169. *
  170. * @param WP_Post $post Post object.
  171. * @return string REST route relative to the REST base URI, or empty string if unknown.
  172. */
  173. protected function detect_rest_item_route( $post ) {
  174. _deprecated_function( __METHOD__, '5.5.0', 'rest_get_route_for_post()' );
  175. return rest_get_route_for_post( $post );
  176. }
  177. }