class-wp-rest-term-search-handler.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * REST API: WP_REST_Term_Search_Handler class
  4. *
  5. * @package WordPress
  6. * @subpackage REST_API
  7. * @since 5.6.0
  8. */
  9. /**
  10. * Core class representing a search handler for terms in the REST API.
  11. *
  12. * @since 5.6.0
  13. *
  14. * @see WP_REST_Search_Handler
  15. */
  16. class WP_REST_Term_Search_Handler extends WP_REST_Search_Handler {
  17. /**
  18. * Constructor.
  19. *
  20. * @since 5.6.0
  21. */
  22. public function __construct() {
  23. $this->type = 'term';
  24. $this->subtypes = array_values(
  25. get_taxonomies(
  26. array(
  27. 'public' => true,
  28. 'show_in_rest' => true,
  29. ),
  30. 'names'
  31. )
  32. );
  33. }
  34. /**
  35. * Searches the object type content for a given search request.
  36. *
  37. * @since 5.6.0
  38. *
  39. * @param WP_REST_Request $request Full REST request.
  40. * @return array {
  41. * Associative array containing found IDs and total count for the matching search results.
  42. *
  43. * @type int[] $ids Found IDs.
  44. * @type string|int|WP_Error $total Numeric string containing the number of terms in that
  45. * taxonomy, 0 if there are no results, or WP_Error if
  46. * the requested taxonomy does not exist.
  47. * }
  48. */
  49. public function search_items( WP_REST_Request $request ) {
  50. $taxonomies = $request[ WP_REST_Search_Controller::PROP_SUBTYPE ];
  51. if ( in_array( WP_REST_Search_Controller::TYPE_ANY, $taxonomies, true ) ) {
  52. $taxonomies = $this->subtypes;
  53. }
  54. $page = (int) $request['page'];
  55. $per_page = (int) $request['per_page'];
  56. $query_args = array(
  57. 'taxonomy' => $taxonomies,
  58. 'hide_empty' => false,
  59. 'offset' => ( $page - 1 ) * $per_page,
  60. 'number' => $per_page,
  61. );
  62. if ( ! empty( $request['search'] ) ) {
  63. $query_args['search'] = $request['search'];
  64. }
  65. if ( ! empty( $request['exclude'] ) ) {
  66. $query_args['exclude'] = $request['exclude'];
  67. }
  68. if ( ! empty( $request['include'] ) ) {
  69. $query_args['include'] = $request['include'];
  70. }
  71. /**
  72. * Filters the query arguments for a REST API search request.
  73. *
  74. * Enables adding extra arguments or setting defaults for a term search request.
  75. *
  76. * @since 5.6.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_term_search_query', $query_args, $request );
  82. $query = new WP_Term_Query();
  83. $found_terms = $query->query( $query_args );
  84. $found_ids = wp_list_pluck( $found_terms, 'term_id' );
  85. unset( $query_args['offset'], $query_args['number'] );
  86. $total = wp_count_terms( $query_args );
  87. // wp_count_terms() can return a falsey value when the term has no children.
  88. if ( ! $total ) {
  89. $total = 0;
  90. }
  91. return array(
  92. self::RESULT_IDS => $found_ids,
  93. self::RESULT_TOTAL => $total,
  94. );
  95. }
  96. /**
  97. * Prepares the search result for a given ID.
  98. *
  99. * @since 5.6.0
  100. *
  101. * @param int $id Item ID.
  102. * @param array $fields Fields to include for the item.
  103. * @return array Associative array containing all fields for the item.
  104. */
  105. public function prepare_item( $id, array $fields ) {
  106. $term = get_term( $id );
  107. $data = array();
  108. if ( in_array( WP_REST_Search_Controller::PROP_ID, $fields, true ) ) {
  109. $data[ WP_REST_Search_Controller::PROP_ID ] = (int) $id;
  110. }
  111. if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) {
  112. $data[ WP_REST_Search_Controller::PROP_TITLE ] = $term->name;
  113. }
  114. if ( in_array( WP_REST_Search_Controller::PROP_URL, $fields, true ) ) {
  115. $data[ WP_REST_Search_Controller::PROP_URL ] = get_term_link( $id );
  116. }
  117. if ( in_array( WP_REST_Search_Controller::PROP_TYPE, $fields, true ) ) {
  118. $data[ WP_REST_Search_Controller::PROP_TYPE ] = $term->taxonomy;
  119. }
  120. return $data;
  121. }
  122. /**
  123. * Prepares links for the search result of a given ID.
  124. *
  125. * @since 5.6.0
  126. *
  127. * @param int $id Item ID.
  128. * @return array[] Array of link arrays for the given item.
  129. */
  130. public function prepare_item_links( $id ) {
  131. $term = get_term( $id );
  132. $links = array();
  133. $item_route = rest_get_route_for_term( $term );
  134. if ( $item_route ) {
  135. $links['self'] = array(
  136. 'href' => rest_url( $item_route ),
  137. 'embeddable' => true,
  138. );
  139. }
  140. $links['about'] = array(
  141. 'href' => rest_url( sprintf( 'wp/v2/taxonomies/%s', $term->taxonomy ) ),
  142. );
  143. return $links;
  144. }
  145. }