| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 terms for a given search request. 40 * 41 * @since 5.6.0 42 * 43 * @param WP_REST_Request $request Full REST request. 44 * @return array { 45 * Associative array containing found IDs and total count for the matching search results. 46 * 47 * @type int[] $ids Found term IDs. 48 * @type string|int|WP_Error $total Numeric string containing the number of terms in that 49 * taxonomy, 0 if there are no results, or WP_Error if 50 * the requested taxonomy does not exist. 51 * } 52 */ 53 public function search_items( WP_REST_Request $request ) { 54 $taxonomies = $request[ WP_REST_Search_Controller::PROP_SUBTYPE ]; 55 if ( in_array( WP_REST_Search_Controller::TYPE_ANY, $taxonomies, true ) ) { 56 $taxonomies = $this->subtypes; 57 } 58 59 $page = (int) $request['page']; 60 $per_page = (int) $request['per_page']; 61 62 $query_args = array( 63 'taxonomy' => $taxonomies, 64 'hide_empty' => false, 65 'offset' => ( $page - 1 ) * $per_page, 66 'number' => $per_page, 67 ); 68 69 if ( ! empty( $request['search'] ) ) { 70 $query_args['search'] = $request['search']; 71 } 72 73 if ( ! empty( $request['exclude'] ) ) { 74 $query_args['exclude'] = $request['exclude']; 75 } 76 77 if ( ! empty( $request['include'] ) ) { 78 $query_args['include'] = $request['include']; 79 } 80 81 /** 82 * Filters the query arguments for a REST API term search request. 83 * 84 * Enables adding extra arguments or setting defaults for a term search request. 85 * 86 * @since 5.6.0 87 * 88 * @param array $query_args Key value array of query var to query value. 89 * @param WP_REST_Request $request The request used. 90 */ 91 $query_args = apply_filters( 'rest_term_search_query', $query_args, $request ); 92 93 if ( empty( $query_args['taxonomy'] ) ) { 94 return array( 95 self::RESULT_IDS => array(), 96 self::RESULT_TOTAL => 0, 97 ); 98 } 99 100 $query = new WP_Term_Query(); 101 $found_terms = $query->query( $query_args ); 102 $found_ids = wp_list_pluck( $found_terms, 'term_id' ); 103 104 unset( $query_args['offset'], $query_args['number'] ); 105 106 $total = wp_count_terms( $query_args ); 107 108 // wp_count_terms() can return a falsey value when the term has no children. 109 if ( ! $total ) { 110 $total = 0; 111 } 112 113 return array( 114 self::RESULT_IDS => $found_ids, 115 self::RESULT_TOTAL => $total, 116 ); 117 } 118 119 /** 120 * Prepares the search result for a given term ID. 121 * 122 * @since 5.6.0 123 * 124 * @param int $id Term ID. 125 * @param array $fields Fields to include for the term. 126 * @return array { 127 * Associative array containing fields for the term based on the `$fields` parameter. 128 * 129 * @type int $id Optional. Term ID. 130 * @type string $title Optional. Term name. 131 * @type string $url Optional. Term permalink URL. 132 * @type string $type Optional. Term taxonomy name. 133 * } 134 */ 135 public function prepare_item( $id, array $fields ) { 136 $term = get_term( $id ); 137 138 $data = array(); 139 140 if ( in_array( WP_REST_Search_Controller::PROP_ID, $fields, true ) ) { 141 $data[ WP_REST_Search_Controller::PROP_ID ] = (int) $id; 142 } 143 if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) { 144 $data[ WP_REST_Search_Controller::PROP_TITLE ] = $term->name; 145 } 146 if ( in_array( WP_REST_Search_Controller::PROP_URL, $fields, true ) ) { 147 $data[ WP_REST_Search_Controller::PROP_URL ] = get_term_link( $id ); 148 } 149 if ( in_array( WP_REST_Search_Controller::PROP_TYPE, $fields, true ) ) { 150 $data[ WP_REST_Search_Controller::PROP_TYPE ] = $term->taxonomy; 151 } 152 153 return $data; 154 } 155 156 /** 157 * Prepares links for the search result of a given ID. 158 * 159 * @since 5.6.0 160 * 161 * @param int $id Item ID. 162 * @return array[] Array of link arrays for the given item. 163 */ 164 public function prepare_item_links( $id ) { 165 $term = get_term( $id ); 166 167 $links = array(); 168 169 $item_route = rest_get_route_for_term( $term ); 170 if ( $item_route ) { 171 $links['self'] = array( 172 'href' => rest_url( $item_route ), 173 'embeddable' => true, 174 ); 175 } 176 177 $links['about'] = array( 178 'href' => rest_url( sprintf( 'wp/v2/taxonomies/%s', $term->taxonomy ) ), 179 ); 180 181 return $links; 182 } 183 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Tue Nov 24 08:20:01 2026 | Cross-referenced by PHPXref |