author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:40:08 +0200 | |
changeset 21 | 48c4eec2b7e6 |
parent 18 | be944660c56a |
child 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
9 | 1 |
<?php |
2 |
/** |
|
3 |
* REST API: WP_REST_Post_Search_Handler class |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage REST_API |
|
7 |
* @since 5.0.0 |
|
8 |
*/ |
|
9 |
||
10 |
/** |
|
11 |
* Core class representing a search handler for posts in the REST API. |
|
12 |
* |
|
13 |
* @since 5.0.0 |
|
16 | 14 |
* |
15 |
* @see WP_REST_Search_Handler |
|
9 | 16 |
*/ |
17 |
class WP_REST_Post_Search_Handler extends WP_REST_Search_Handler { |
|
18 |
||
19 |
/** |
|
20 |
* Constructor. |
|
21 |
* |
|
22 |
* @since 5.0.0 |
|
23 |
*/ |
|
24 |
public function __construct() { |
|
25 |
$this->type = 'post'; |
|
26 |
||
27 |
// Support all public post types except attachments. |
|
28 |
$this->subtypes = array_diff( |
|
29 |
array_values( |
|
30 |
get_post_types( |
|
31 |
array( |
|
32 |
'public' => true, |
|
33 |
'show_in_rest' => true, |
|
34 |
), |
|
35 |
'names' |
|
36 |
) |
|
37 |
), |
|
38 |
array( 'attachment' ) |
|
39 |
); |
|
40 |
} |
|
41 |
||
42 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
43 |
* Searches posts for a given search request. |
9 | 44 |
* |
45 |
* @since 5.0.0 |
|
46 |
* |
|
47 |
* @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
|
48 |
* @return array { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
49 |
* 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
|
50 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
51 |
* @type int[] $ids Array containing the matching post IDs. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
52 |
* @type int $total 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
|
53 |
* } |
9 | 54 |
*/ |
55 |
public function search_items( WP_REST_Request $request ) { |
|
56 |
||
57 |
// Get the post types to search for the current request. |
|
58 |
$post_types = $request[ WP_REST_Search_Controller::PROP_SUBTYPE ]; |
|
59 |
if ( in_array( WP_REST_Search_Controller::TYPE_ANY, $post_types, true ) ) { |
|
60 |
$post_types = $this->subtypes; |
|
61 |
} |
|
62 |
||
63 |
$query_args = array( |
|
64 |
'post_type' => $post_types, |
|
65 |
'post_status' => 'publish', |
|
66 |
'paged' => (int) $request['page'], |
|
67 |
'posts_per_page' => (int) $request['per_page'], |
|
68 |
'ignore_sticky_posts' => true, |
|
69 |
); |
|
70 |
||
71 |
if ( ! empty( $request['search'] ) ) { |
|
72 |
$query_args['s'] = $request['search']; |
|
73 |
} |
|
74 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
75 |
if ( ! empty( $request['exclude'] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
76 |
$query_args['post__not_in'] = $request['exclude']; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
77 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
78 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
79 |
if ( ! empty( $request['include'] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
80 |
$query_args['post__in'] = $request['include']; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
81 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
82 |
|
9 | 83 |
/** |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
84 |
* Filters the query arguments for a REST API post search request. |
9 | 85 |
* |
86 |
* Enables adding extra arguments or setting defaults for a post search request. |
|
87 |
* |
|
88 |
* @since 5.1.0 |
|
89 |
* |
|
90 |
* @param array $query_args Key value array of query var to query value. |
|
91 |
* @param WP_REST_Request $request The request used. |
|
92 |
*/ |
|
93 |
$query_args = apply_filters( 'rest_post_search_query', $query_args, $request ); |
|
94 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
95 |
$query = new WP_Query(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
96 |
$posts = $query->query( $query_args ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
97 |
// Querying the whole post object will warm the object cache, avoiding an extra query per result. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
98 |
$found_ids = wp_list_pluck( $posts, 'ID' ); |
9 | 99 |
$total = $query->found_posts; |
100 |
||
101 |
return array( |
|
102 |
self::RESULT_IDS => $found_ids, |
|
103 |
self::RESULT_TOTAL => $total, |
|
104 |
); |
|
105 |
} |
|
106 |
||
107 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
108 |
* Prepares the search result for a given post ID. |
9 | 109 |
* |
110 |
* @since 5.0.0 |
|
111 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
112 |
* @param int $id Post ID. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
113 |
* @param array $fields Fields to include for the post. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
114 |
* @return array { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
115 |
* Associative array containing fields for the post based on the `$fields` parameter. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
116 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
117 |
* @type int $id Optional. Post ID. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
118 |
* @type string $title Optional. Post title. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
119 |
* @type string $url Optional. Post permalink URL. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
120 |
* @type string $type Optional. Post type. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
121 |
* } |
9 | 122 |
*/ |
123 |
public function prepare_item( $id, array $fields ) { |
|
124 |
$post = get_post( $id ); |
|
125 |
||
126 |
$data = array(); |
|
127 |
||
128 |
if ( in_array( WP_REST_Search_Controller::PROP_ID, $fields, true ) ) { |
|
129 |
$data[ WP_REST_Search_Controller::PROP_ID ] = (int) $post->ID; |
|
130 |
} |
|
131 |
||
132 |
if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) { |
|
133 |
if ( post_type_supports( $post->post_type, 'title' ) ) { |
|
134 |
add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) ); |
|
135 |
$data[ WP_REST_Search_Controller::PROP_TITLE ] = get_the_title( $post->ID ); |
|
136 |
remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) ); |
|
137 |
} else { |
|
138 |
$data[ WP_REST_Search_Controller::PROP_TITLE ] = ''; |
|
139 |
} |
|
140 |
} |
|
141 |
||
142 |
if ( in_array( WP_REST_Search_Controller::PROP_URL, $fields, true ) ) { |
|
143 |
$data[ WP_REST_Search_Controller::PROP_URL ] = get_permalink( $post->ID ); |
|
144 |
} |
|
145 |
||
146 |
if ( in_array( WP_REST_Search_Controller::PROP_TYPE, $fields, true ) ) { |
|
147 |
$data[ WP_REST_Search_Controller::PROP_TYPE ] = $this->type; |
|
148 |
} |
|
149 |
||
150 |
if ( in_array( WP_REST_Search_Controller::PROP_SUBTYPE, $fields, true ) ) { |
|
151 |
$data[ WP_REST_Search_Controller::PROP_SUBTYPE ] = $post->post_type; |
|
152 |
} |
|
153 |
||
154 |
return $data; |
|
155 |
} |
|
156 |
||
157 |
/** |
|
158 |
* Prepares links for the search result of a given ID. |
|
159 |
* |
|
160 |
* @since 5.0.0 |
|
161 |
* |
|
162 |
* @param int $id Item ID. |
|
163 |
* @return array Links for the given item. |
|
164 |
*/ |
|
165 |
public function prepare_item_links( $id ) { |
|
166 |
$post = get_post( $id ); |
|
167 |
||
168 |
$links = array(); |
|
169 |
||
16 | 170 |
$item_route = rest_get_route_for_post( $post ); |
9 | 171 |
if ( ! empty( $item_route ) ) { |
172 |
$links['self'] = array( |
|
173 |
'href' => rest_url( $item_route ), |
|
174 |
'embeddable' => true, |
|
175 |
); |
|
176 |
} |
|
177 |
||
178 |
$links['about'] = array( |
|
179 |
'href' => rest_url( 'wp/v2/types/' . $post->post_type ), |
|
180 |
); |
|
181 |
||
182 |
return $links; |
|
183 |
} |
|
184 |
||
185 |
/** |
|
186 |
* Overwrites the default protected title format. |
|
187 |
* |
|
188 |
* By default, WordPress will show password protected posts with a title of |
|
189 |
* "Protected: %s". As the REST API communicates the protected status of a post |
|
190 |
* in a machine readable format, we remove the "Protected: " prefix. |
|
191 |
* |
|
192 |
* @since 5.0.0 |
|
193 |
* |
|
194 |
* @return string Protected title format. |
|
195 |
*/ |
|
196 |
public function protected_title_format() { |
|
197 |
return '%s'; |
|
198 |
} |
|
199 |
||
200 |
/** |
|
201 |
* Attempts to detect the route to access a single item. |
|
202 |
* |
|
203 |
* @since 5.0.0 |
|
16 | 204 |
* @deprecated 5.5.0 Use rest_get_route_for_post() |
205 |
* @see rest_get_route_for_post() |
|
9 | 206 |
* |
207 |
* @param WP_Post $post Post object. |
|
208 |
* @return string REST route relative to the REST base URI, or empty string if unknown. |
|
209 |
*/ |
|
210 |
protected function detect_rest_item_route( $post ) { |
|
16 | 211 |
_deprecated_function( __METHOD__, '5.5.0', 'rest_get_route_for_post()' ); |
9 | 212 |
|
16 | 213 |
return rest_get_route_for_post( $post ); |
9 | 214 |
} |
215 |
} |