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. 'fields' => 'ids',
  61. );
  62. if ( ! empty( $request['search'] ) ) {
  63. $query_args['s'] = $request['search'];
  64. }
  65. /**
  66. * Filters the query arguments for a search request.
  67. *
  68. * Enables adding extra arguments or setting defaults for a post search request.
  69. *
  70. * @since 5.1.0
  71. *
  72. * @param array $query_args Key value array of query var to query value.
  73. * @param WP_REST_Request $request The request used.
  74. */
  75. $query_args = apply_filters( 'rest_post_search_query', $query_args, $request );
  76. $query = new WP_Query();
  77. $found_ids = $query->query( $query_args );
  78. $total = $query->found_posts;
  79. return array(
  80. self::RESULT_IDS => $found_ids,
  81. self::RESULT_TOTAL => $total,
  82. );
  83. }
  84. /**
  85. * Prepares the search result for a given ID.
  86. *
  87. * @since 5.0.0
  88. *
  89. * @param int $id Item ID.
  90. * @param array $fields Fields to include for the item.
  91. * @return array Associative array containing all fields for the item.
  92. */
  93. public function prepare_item( $id, array $fields ) {
  94. $post = get_post( $id );
  95. $data = array();
  96. if ( in_array( WP_REST_Search_Controller::PROP_ID, $fields, true ) ) {
  97. $data[ WP_REST_Search_Controller::PROP_ID ] = (int) $post->ID;
  98. }
  99. if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) {
  100. if ( post_type_supports( $post->post_type, 'title' ) ) {
  101. add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
  102. $data[ WP_REST_Search_Controller::PROP_TITLE ] = get_the_title( $post->ID );
  103. remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
  104. } else {
  105. $data[ WP_REST_Search_Controller::PROP_TITLE ] = '';
  106. }
  107. }
  108. if ( in_array( WP_REST_Search_Controller::PROP_URL, $fields, true ) ) {
  109. $data[ WP_REST_Search_Controller::PROP_URL ] = get_permalink( $post->ID );
  110. }
  111. if ( in_array( WP_REST_Search_Controller::PROP_TYPE, $fields, true ) ) {
  112. $data[ WP_REST_Search_Controller::PROP_TYPE ] = $this->type;
  113. }
  114. if ( in_array( WP_REST_Search_Controller::PROP_SUBTYPE, $fields, true ) ) {
  115. $data[ WP_REST_Search_Controller::PROP_SUBTYPE ] = $post->post_type;
  116. }
  117. return $data;
  118. }
  119. /**
  120. * Prepares links for the search result of a given ID.
  121. *
  122. * @since 5.0.0
  123. *
  124. * @param int $id Item ID.
  125. * @return array Links for the given item.
  126. */
  127. public function prepare_item_links( $id ) {
  128. $post = get_post( $id );
  129. $links = array();
  130. $item_route = $this->detect_rest_item_route( $post );
  131. if ( ! empty( $item_route ) ) {
  132. $links['self'] = array(
  133. 'href' => rest_url( $item_route ),
  134. 'embeddable' => true,
  135. );
  136. }
  137. $links['about'] = array(
  138. 'href' => rest_url( 'wp/v2/types/' . $post->post_type ),
  139. );
  140. return $links;
  141. }
  142. /**
  143. * Overwrites the default protected title format.
  144. *
  145. * By default, WordPress will show password protected posts with a title of
  146. * "Protected: %s". As the REST API communicates the protected status of a post
  147. * in a machine readable format, we remove the "Protected: " prefix.
  148. *
  149. * @since 5.0.0
  150. *
  151. * @return string Protected title format.
  152. */
  153. public function protected_title_format() {
  154. return '%s';
  155. }
  156. /**
  157. * Attempts to detect the route to access a single item.
  158. *
  159. * @since 5.0.0
  160. *
  161. * @param WP_Post $post Post object.
  162. * @return string REST route relative to the REST base URI, or empty string if unknown.
  163. */
  164. protected function detect_rest_item_route( $post ) {
  165. $post_type = get_post_type_object( $post->post_type );
  166. if ( ! $post_type ) {
  167. return '';
  168. }
  169. // It's currently impossible to detect the REST URL from a custom controller.
  170. if ( ! empty( $post_type->rest_controller_class ) && 'WP_REST_Posts_Controller' !== $post_type->rest_controller_class ) {
  171. return '';
  172. }
  173. $namespace = 'wp/v2';
  174. $rest_base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
  175. return sprintf( '%s/%s/%d', $namespace, $rest_base, $post->ID );
  176. }
  177. }