[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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 search request.
  70           *
  71           * Enables adding extra arguments or setting defaults for a term search request.
  72           *
  73           * @param array           $query_args Key value array of query var to query value.
  74           * @param WP_REST_Request $request    The request used.
  75           */
  76          $query_args = apply_filters( 'rest_term_search_query', $query_args, $request );
  77  
  78          $query       = new WP_Term_Query();
  79          $found_terms = $query->query( $query_args );
  80          $found_ids   = wp_list_pluck( $found_terms, 'term_id' );
  81  
  82          unset( $query_args['offset'], $query_args['number'] );
  83  
  84          $total = wp_count_terms( $query_args );
  85  
  86          // wp_count_terms() can return a falsey value when the term has no children.
  87          if ( ! $total ) {
  88              $total = 0;
  89          }
  90  
  91          return array(
  92              self::RESULT_IDS   => $found_ids,
  93              self::RESULT_TOTAL => $total,
  94          );
  95      }
  96  
  97      /**
  98       * Prepares the search result for a given ID.
  99       *
 100       * @since 5.6.0
 101       *
 102       * @param int   $id     Item ID.
 103       * @param array $fields Fields to include for the item.
 104       * @return array Associative array containing all fields for the item.
 105       */
 106  	public function prepare_item( $id, array $fields ) {
 107          $term = get_term( $id );
 108  
 109          $data = array();
 110  
 111          if ( in_array( WP_REST_Search_Controller::PROP_ID, $fields, true ) ) {
 112              $data[ WP_REST_Search_Controller::PROP_ID ] = (int) $id;
 113          }
 114          if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) {
 115              $data[ WP_REST_Search_Controller::PROP_TITLE ] = $term->name;
 116          }
 117          if ( in_array( WP_REST_Search_Controller::PROP_URL, $fields, true ) ) {
 118              $data[ WP_REST_Search_Controller::PROP_URL ] = get_term_link( $id );
 119          }
 120          if ( in_array( WP_REST_Search_Controller::PROP_TYPE, $fields, true ) ) {
 121              $data[ WP_REST_Search_Controller::PROP_TYPE ] = $term->taxonomy;
 122          }
 123  
 124          return $data;
 125      }
 126  
 127      /**
 128       * Prepares links for the search result of a given ID.
 129       *
 130       * @since 5.6.0
 131       *
 132       * @param int $id Item ID.
 133       * @return array Links for the given item.
 134       */
 135  	public function prepare_item_links( $id ) {
 136          $term = get_term( $id );
 137  
 138          $links = array();
 139  
 140          $item_route = rest_get_route_for_term( $term );
 141          if ( $item_route ) {
 142              $links['self'] = array(
 143                  'href'       => rest_url( $item_route ),
 144                  'embeddable' => true,
 145              );
 146          }
 147  
 148          $links['about'] = array(
 149              'href' => rest_url( sprintf( 'wp/v2/taxonomies/%s', $term->taxonomy ) ),
 150          );
 151  
 152          return $links;
 153      }
 154  }


Generated : Wed Dec 2 08:20:02 2020 Cross-referenced by PHPXref