[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/rest-api/search/ -> class-wp-rest-term-search-handler.php (source)

   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  /**
  11   * Core class representing a search handler for terms in the REST API.
  12   *
  13   * @since 5.6.0
  14   *
  15   * @see WP_REST_Search_Handler
  16   */
  17  class WP_REST_Term_Search_Handler extends WP_REST_Search_Handler {
  18  
  19      /**
  20       * Constructor.
  21       *
  22       * @since 5.6.0
  23       */
  24  	public function __construct() {
  25          $this->type = 'term';
  26  
  27          $this->subtypes = array_values(
  28              get_taxonomies(
  29                  array(
  30                      'public'       => true,
  31                      'show_in_rest' => true,
  32                  ),
  33                  'names'
  34              )
  35          );
  36      }
  37  
  38      /**
  39       * Searches the object type content for a given search request.
  40       *
  41       * @since 5.6.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          $taxonomies = $request[ WP_REST_Search_Controller::PROP_SUBTYPE ];
  50          if ( in_array( WP_REST_Search_Controller::TYPE_ANY, $taxonomies, true ) ) {
  51              $taxonomies = $this->subtypes;
  52          }
  53  
  54          $page     = (int) $request['page'];
  55          $per_page = (int) $request['per_page'];
  56  
  57          $query_args = array(
  58              'taxonomy'   => $taxonomies,
  59              'hide_empty' => false,
  60              'offset'     => ( $page - 1 ) * $per_page,
  61              'number'     => $per_page,
  62          );
  63  
  64          if ( ! empty( $request['search'] ) ) {
  65              $query_args['search'] = $request['search'];
  66          }
  67  
  68          /**
  69           * Filters the query arguments for a REST API search request.
  70           *
  71           * Enables adding extra arguments or setting defaults for a term search request.
  72           *
  73           * @since 5.6.0
  74           *
  75           * @param array           $query_args Key value array of query var to query value.
  76           * @param WP_REST_Request $request    The request used.
  77           */
  78          $query_args = apply_filters( 'rest_term_search_query', $query_args, $request );
  79  
  80          $query       = new WP_Term_Query();
  81          $found_terms = $query->query( $query_args );
  82          $found_ids   = wp_list_pluck( $found_terms, 'term_id' );
  83  
  84          unset( $query_args['offset'], $query_args['number'] );
  85  
  86          $total = wp_count_terms( $query_args );
  87  
  88          // wp_count_terms() can return a falsey value when the term has no children.
  89          if ( ! $total ) {
  90              $total = 0;
  91          }
  92  
  93          return array(
  94              self::RESULT_IDS   => $found_ids,
  95              self::RESULT_TOTAL => $total,
  96          );
  97      }
  98  
  99      /**
 100       * Prepares the search result for a given ID.
 101       *
 102       * @since 5.6.0
 103       *
 104       * @param int   $id     Item ID.
 105       * @param array $fields Fields to include for the item.
 106       * @return array Associative array containing all fields for the item.
 107       */
 108  	public function prepare_item( $id, array $fields ) {
 109          $term = get_term( $id );
 110  
 111          $data = array();
 112  
 113          if ( in_array( WP_REST_Search_Controller::PROP_ID, $fields, true ) ) {
 114              $data[ WP_REST_Search_Controller::PROP_ID ] = (int) $id;
 115          }
 116          if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) {
 117              $data[ WP_REST_Search_Controller::PROP_TITLE ] = $term->name;
 118          }
 119          if ( in_array( WP_REST_Search_Controller::PROP_URL, $fields, true ) ) {
 120              $data[ WP_REST_Search_Controller::PROP_URL ] = get_term_link( $id );
 121          }
 122          if ( in_array( WP_REST_Search_Controller::PROP_TYPE, $fields, true ) ) {
 123              $data[ WP_REST_Search_Controller::PROP_TYPE ] = $term->taxonomy;
 124          }
 125  
 126          return $data;
 127      }
 128  
 129      /**
 130       * Prepares links for the search result of a given ID.
 131       *
 132       * @since 5.6.0
 133       *
 134       * @param int $id Item ID.
 135       * @return array Links for the given item.
 136       */
 137  	public function prepare_item_links( $id ) {
 138          $term = get_term( $id );
 139  
 140          $links = array();
 141  
 142          $item_route = rest_get_route_for_term( $term );
 143          if ( $item_route ) {
 144              $links['self'] = array(
 145                  'href'       => rest_url( $item_route ),
 146                  'embeddable' => true,
 147              );
 148          }
 149  
 150          $links['about'] = array(
 151              'href' => rest_url( sprintf( 'wp/v2/taxonomies/%s', $term->taxonomy ) ),
 152          );
 153  
 154          return $links;
 155      }
 156  }


Generated: Tue Mar 19 01:00:02 2024 Cross-referenced by PHPXref 0.7.1