[ 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 $query = new WP_Term_Query(); 94 $found_terms = $query->query( $query_args ); 95 $found_ids = wp_list_pluck( $found_terms, 'term_id' ); 96 97 unset( $query_args['offset'], $query_args['number'] ); 98 99 $total = wp_count_terms( $query_args ); 100 101 // wp_count_terms() can return a falsey value when the term has no children. 102 if ( ! $total ) { 103 $total = 0; 104 } 105 106 return array( 107 self::RESULT_IDS => $found_ids, 108 self::RESULT_TOTAL => $total, 109 ); 110 } 111 112 /** 113 * Prepares the search result for a given term ID. 114 * 115 * @since 5.6.0 116 * 117 * @param int $id Term ID. 118 * @param array $fields Fields to include for the term. 119 * @return array { 120 * Associative array containing fields for the term based on the `$fields` parameter. 121 * 122 * @type int $id Optional. Term ID. 123 * @type string $title Optional. Term name. 124 * @type string $url Optional. Term permalink URL. 125 * @type string $type Optional. Term taxonomy name. 126 * } 127 */ 128 public function prepare_item( $id, array $fields ) { 129 $term = get_term( $id ); 130 131 $data = array(); 132 133 if ( in_array( WP_REST_Search_Controller::PROP_ID, $fields, true ) ) { 134 $data[ WP_REST_Search_Controller::PROP_ID ] = (int) $id; 135 } 136 if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) { 137 $data[ WP_REST_Search_Controller::PROP_TITLE ] = $term->name; 138 } 139 if ( in_array( WP_REST_Search_Controller::PROP_URL, $fields, true ) ) { 140 $data[ WP_REST_Search_Controller::PROP_URL ] = get_term_link( $id ); 141 } 142 if ( in_array( WP_REST_Search_Controller::PROP_TYPE, $fields, true ) ) { 143 $data[ WP_REST_Search_Controller::PROP_TYPE ] = $term->taxonomy; 144 } 145 146 return $data; 147 } 148 149 /** 150 * Prepares links for the search result of a given ID. 151 * 152 * @since 5.6.0 153 * 154 * @param int $id Item ID. 155 * @return array[] Array of link arrays for the given item. 156 */ 157 public function prepare_item_links( $id ) { 158 $term = get_term( $id ); 159 160 $links = array(); 161 162 $item_route = rest_get_route_for_term( $term ); 163 if ( $item_route ) { 164 $links['self'] = array( 165 'href' => rest_url( $item_route ), 166 'embeddable' => true, 167 ); 168 } 169 170 $links['about'] = array( 171 'href' => rest_url( sprintf( 'wp/v2/taxonomies/%s', $term->taxonomy ) ), 172 ); 173 174 return $links; 175 } 176 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Thu Nov 21 08:20:01 2024 | Cross-referenced by PHPXref |