equal
deleted
inserted
replaced
66 $this->namespace = 'wp/v2'; |
66 $this->namespace = 'wp/v2'; |
67 $this->rest_base = 'search'; |
67 $this->rest_base = 'search'; |
68 |
68 |
69 foreach ( $search_handlers as $search_handler ) { |
69 foreach ( $search_handlers as $search_handler ) { |
70 if ( ! $search_handler instanceof WP_REST_Search_Handler ) { |
70 if ( ! $search_handler instanceof WP_REST_Search_Handler ) { |
71 |
71 _doing_it_wrong( |
72 /* translators: %s: PHP class name */ |
72 __METHOD__, |
73 _doing_it_wrong( __METHOD__, sprintf( __( 'REST search handlers must extend the %s class.' ), 'WP_REST_Search_Handler' ), '5.0.0' ); |
73 /* translators: %s: PHP class name. */ |
|
74 sprintf( __( 'REST search handlers must extend the %s class.' ), 'WP_REST_Search_Handler' ), |
|
75 '5.0.0' |
|
76 ); |
74 continue; |
77 continue; |
75 } |
78 } |
76 |
79 |
77 $this->search_handlers[ $search_handler->get_type() ] = $search_handler; |
80 $this->search_handlers[ $search_handler->get_type() ] = $search_handler; |
78 } |
81 } |
128 } |
131 } |
129 |
132 |
130 $result = $handler->search_items( $request ); |
133 $result = $handler->search_items( $request ); |
131 |
134 |
132 if ( ! isset( $result[ WP_REST_Search_Handler::RESULT_IDS ] ) || ! is_array( $result[ WP_REST_Search_Handler::RESULT_IDS ] ) || ! isset( $result[ WP_REST_Search_Handler::RESULT_TOTAL ] ) ) { |
135 if ( ! isset( $result[ WP_REST_Search_Handler::RESULT_IDS ] ) || ! is_array( $result[ WP_REST_Search_Handler::RESULT_IDS ] ) || ! isset( $result[ WP_REST_Search_Handler::RESULT_TOTAL ] ) ) { |
133 return new WP_Error( 'rest_search_handler_error', __( 'Internal search handler error.' ), array( 'status' => 500 ) ); |
136 return new WP_Error( |
|
137 'rest_search_handler_error', |
|
138 __( 'Internal search handler error.' ), |
|
139 array( 'status' => 500 ) |
|
140 ); |
134 } |
141 } |
135 |
142 |
136 $ids = array_map( 'absint', $result[ WP_REST_Search_Handler::RESULT_IDS ] ); |
143 $ids = array_map( 'absint', $result[ WP_REST_Search_Handler::RESULT_IDS ] ); |
137 |
144 |
138 $results = array(); |
145 $results = array(); |
|
146 |
139 foreach ( $ids as $id ) { |
147 foreach ( $ids as $id ) { |
140 $data = $this->prepare_item_for_response( $id, $request ); |
148 $data = $this->prepare_item_for_response( $id, $request ); |
141 $results[] = $this->prepare_response_for_collection( $data ); |
149 $results[] = $this->prepare_response_for_collection( $data ); |
142 } |
150 } |
143 |
151 |
145 $page = (int) $request['page']; |
153 $page = (int) $request['page']; |
146 $per_page = (int) $request['per_page']; |
154 $per_page = (int) $request['per_page']; |
147 $max_pages = ceil( $total / $per_page ); |
155 $max_pages = ceil( $total / $per_page ); |
148 |
156 |
149 if ( $page > $max_pages && $total > 0 ) { |
157 if ( $page > $max_pages && $total > 0 ) { |
150 return new WP_Error( 'rest_search_invalid_page_number', __( 'The page number requested is larger than the number of pages available.' ), array( 'status' => 400 ) ); |
158 return new WP_Error( |
|
159 'rest_search_invalid_page_number', |
|
160 __( 'The page number requested is larger than the number of pages available.' ), |
|
161 array( 'status' => 400 ) |
|
162 ); |
151 } |
163 } |
152 |
164 |
153 $response = rest_ensure_response( $results ); |
165 $response = rest_ensure_response( $results ); |
154 $response->header( 'X-WP-Total', $total ); |
166 $response->header( 'X-WP-Total', $total ); |
155 $response->header( 'X-WP-TotalPages', $max_pages ); |
167 $response->header( 'X-WP-TotalPages', $max_pages ); |
209 * @since 5.0.0 |
221 * @since 5.0.0 |
210 * |
222 * |
211 * @return array Item schema data. |
223 * @return array Item schema data. |
212 */ |
224 */ |
213 public function get_item_schema() { |
225 public function get_item_schema() { |
|
226 if ( $this->schema ) { |
|
227 return $this->add_additional_fields_schema( $this->schema ); |
|
228 } |
|
229 |
214 $types = array(); |
230 $types = array(); |
215 $subtypes = array(); |
231 $subtypes = array(); |
|
232 |
216 foreach ( $this->search_handlers as $search_handler ) { |
233 foreach ( $this->search_handlers as $search_handler ) { |
217 $types[] = $search_handler->get_type(); |
234 $types[] = $search_handler->get_type(); |
218 $subtypes = array_merge( $subtypes, $search_handler->get_subtypes() ); |
235 $subtypes = array_merge( $subtypes, $search_handler->get_subtypes() ); |
219 } |
236 } |
220 |
237 |
260 'readonly' => true, |
277 'readonly' => true, |
261 ), |
278 ), |
262 ), |
279 ), |
263 ); |
280 ); |
264 |
281 |
265 return $this->add_additional_fields_schema( $schema ); |
282 $this->schema = $schema; |
|
283 |
|
284 return $this->add_additional_fields_schema( $this->schema ); |
266 } |
285 } |
267 |
286 |
268 /** |
287 /** |
269 * Retrieves the query params for the search results collection. |
288 * Retrieves the query params for the search results collection. |
270 * |
289 * |
273 * @return array Collection parameters. |
292 * @return array Collection parameters. |
274 */ |
293 */ |
275 public function get_collection_params() { |
294 public function get_collection_params() { |
276 $types = array(); |
295 $types = array(); |
277 $subtypes = array(); |
296 $subtypes = array(); |
|
297 |
278 foreach ( $this->search_handlers as $search_handler ) { |
298 foreach ( $this->search_handlers as $search_handler ) { |
279 $types[] = $search_handler->get_type(); |
299 $types[] = $search_handler->get_type(); |
280 $subtypes = array_merge( $subtypes, $search_handler->get_subtypes() ); |
300 $subtypes = array_merge( $subtypes, $search_handler->get_subtypes() ); |
281 } |
301 } |
282 |
302 |
349 */ |
369 */ |
350 protected function get_search_handler( $request ) { |
370 protected function get_search_handler( $request ) { |
351 $type = $request->get_param( self::PROP_TYPE ); |
371 $type = $request->get_param( self::PROP_TYPE ); |
352 |
372 |
353 if ( ! $type || ! isset( $this->search_handlers[ $type ] ) ) { |
373 if ( ! $type || ! isset( $this->search_handlers[ $type ] ) ) { |
354 return new WP_Error( 'rest_search_invalid_type', __( 'Invalid type parameter.' ), array( 'status' => 400 ) ); |
374 return new WP_Error( |
|
375 'rest_search_invalid_type', |
|
376 __( 'Invalid type parameter.' ), |
|
377 array( 'status' => 400 ) |
|
378 ); |
355 } |
379 } |
356 |
380 |
357 return $this->search_handlers[ $type ]; |
381 return $this->search_handlers[ $type ]; |
358 } |
382 } |
359 } |
383 } |