Нема описа

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 Associative array containing an `WP_REST_Search_Handler::RESULT_IDS` containing
  41. * an array of found IDs and `WP_REST_Search_Handler::RESULT_TOTAL` containing the
  42. * total count for the matching search results.
  43. */
  44. public function search_items( WP_REST_Request $request ) {
  45. $taxonomies = $request[ WP_REST_Search_Controller::PROP_SUBTYPE ];
  46. if ( in_array( WP_REST_Search_Controller::TYPE_ANY, $taxonomies, true ) ) {
  47. $taxonomies = $this->subtypes;
  48. }
  49. $page = (int) $request['page'];
  50. $per_page = (int) $request['per_page'];
  51. $query_args = array(
  52. 'taxonomy' => $taxonomies,
  53. 'hide_empty' => false,
  54. 'offset' => ( $page - 1 ) * $per_page,
  55. 'number' => $per_page,
  56. );
  57. if ( ! empty( $request['search'] ) ) {
  58. $query_args['search'] = $request['search'];
  59. }
  60. /**
  61. * Filters the query arguments for a REST API search request.
  62. *
  63. * Enables adding extra arguments or setting defaults for a term search request.
  64. *
  65. * @since 5.6.0
  66. *
  67. * @param array $query_args Key value array of query var to query value.
  68. * @param WP_REST_Request $request The request used.
  69. */
  70. $query_args = apply_filters( 'rest_term_search_query', $query_args, $request );
  71. $query = new WP_Term_Query();
  72. $found_terms = $query->query( $query_args );
  73. $found_ids = wp_list_pluck( $found_terms, 'term_id' );
  74. unset( $query_args['offset'], $query_args['number'] );
  75. $total = wp_count_terms( $query_args );
  76. // wp_count_terms() can return a falsey value when the term has no children.
  77. if ( ! $total ) {
  78. $total = 0;
  79. }
  80. return array(
  81. self::RESULT_IDS => $found_ids,
  82. self::RESULT_TOTAL => $total,
  83. );
  84. }
  85. /**
  86. * Prepares the search result for a given ID.
  87. *
  88. * @since 5.6.0
  89. *
  90. * @param int $id Item ID.
  91. * @param array $fields Fields to include for the item.
  92. * @return array Associative array containing all fields for the item.
  93. */
  94. public function prepare_item( $id, array $fields ) {
  95. $term = get_term( $id );
  96. $data = array();
  97. if ( in_array( WP_REST_Search_Controller::PROP_ID, $fields, true ) ) {
  98. $data[ WP_REST_Search_Controller::PROP_ID ] = (int) $id;
  99. }
  100. if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) {
  101. $data[ WP_REST_Search_Controller::PROP_TITLE ] = $term->name;
  102. }
  103. if ( in_array( WP_REST_Search_Controller::PROP_URL, $fields, true ) ) {
  104. $data[ WP_REST_Search_Controller::PROP_URL ] = get_term_link( $id );
  105. }
  106. if ( in_array( WP_REST_Search_Controller::PROP_TYPE, $fields, true ) ) {
  107. $data[ WP_REST_Search_Controller::PROP_TYPE ] = $term->taxonomy;
  108. }
  109. return $data;
  110. }
  111. /**
  112. * Prepares links for the search result of a given ID.
  113. *
  114. * @since 5.6.0
  115. *
  116. * @param int $id Item ID.
  117. * @return array Links for the given item.
  118. */
  119. public function prepare_item_links( $id ) {
  120. $term = get_term( $id );
  121. $links = array();
  122. $item_route = rest_get_route_for_term( $term );
  123. if ( $item_route ) {
  124. $links['self'] = array(
  125. 'href' => rest_url( $item_route ),
  126. 'embeddable' => true,
  127. );
  128. }
  129. $links['about'] = array(
  130. 'href' => rest_url( sprintf( 'wp/v2/taxonomies/%s', $term->taxonomy ) ),
  131. );
  132. return $links;
  133. }
  134. }