author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
18 | 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 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
39 |
* Searches terms for a given search request. |
18 | 40 |
* |
41 |
* @since 5.6.0 |
|
42 |
* |
|
43 |
* @param WP_REST_Request $request Full REST request. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
44 |
* @return array { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
45 |
* Associative array containing found IDs and total count for the matching search results. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
46 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
47 |
* @type int[] $ids Found term IDs. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
48 |
* @type string|int|WP_Error $total Numeric string containing the number of terms in that |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
49 |
* taxonomy, 0 if there are no results, or WP_Error if |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
50 |
* the requested taxonomy does not exist. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
51 |
* } |
18 | 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 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
73 |
if ( ! empty( $request['exclude'] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
74 |
$query_args['exclude'] = $request['exclude']; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
75 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
76 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
77 |
if ( ! empty( $request['include'] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
78 |
$query_args['include'] = $request['include']; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
79 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
80 |
|
18 | 81 |
/** |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
82 |
* Filters the query arguments for a REST API term search request. |
18 | 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 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
113 |
* Prepares the search result for a given term ID. |
18 | 114 |
* |
115 |
* @since 5.6.0 |
|
116 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
117 |
* @param int $id Term ID. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
118 |
* @param array $fields Fields to include for the term. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
119 |
* @return array { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
120 |
* Associative array containing fields for the term based on the `$fields` parameter. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
121 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
122 |
* @type int $id Optional. Term ID. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
123 |
* @type string $title Optional. Term name. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
124 |
* @type string $url Optional. Term permalink URL. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
125 |
* @type string $type Optional. Term taxonomy name. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
126 |
* } |
18 | 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. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
155 |
* @return array[] Array of link arrays for the given item. |
18 | 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 |
} |