210 return $response; |
210 return $response; |
211 } |
211 } |
212 |
212 |
213 $data = (array) $response->get_data(); |
213 $data = (array) $response->get_data(); |
214 $server = rest_get_server(); |
214 $server = rest_get_server(); |
215 |
215 $links = $server->get_compact_response_links( $response ); |
216 if ( method_exists( $server, 'get_compact_response_links' ) ) { |
|
217 $links = call_user_func( array( $server, 'get_compact_response_links' ), $response ); |
|
218 } else { |
|
219 $links = call_user_func( array( $server, 'get_response_links' ), $response ); |
|
220 } |
|
221 |
216 |
222 if ( ! empty( $links ) ) { |
217 if ( ! empty( $links ) ) { |
223 $data['_links'] = $links; |
218 $data['_links'] = $links; |
224 } |
219 } |
225 |
220 |
303 * |
298 * |
304 * @return array Query parameters for the collection. |
299 * @return array Query parameters for the collection. |
305 */ |
300 */ |
306 public function get_collection_params() { |
301 public function get_collection_params() { |
307 return array( |
302 return array( |
308 'context' => $this->get_context_param(), |
303 'context' => $this->get_context_param(), |
309 'page' => array( |
304 'page' => array( |
310 'description' => __( 'Current page of the collection.' ), |
305 'description' => __( 'Current page of the collection.' ), |
311 'type' => 'integer', |
306 'type' => 'integer', |
312 'default' => 1, |
307 'default' => 1, |
313 'sanitize_callback' => 'absint', |
308 'sanitize_callback' => 'absint', |
314 'validate_callback' => 'rest_validate_request_arg', |
309 'validate_callback' => 'rest_validate_request_arg', |
315 'minimum' => 1, |
310 'minimum' => 1, |
316 ), |
311 ), |
317 'per_page' => array( |
312 'per_page' => array( |
318 'description' => __( 'Maximum number of items to be returned in result set.' ), |
313 'description' => __( 'Maximum number of items to be returned in result set.' ), |
319 'type' => 'integer', |
314 'type' => 'integer', |
320 'default' => 10, |
315 'default' => 10, |
321 'minimum' => 1, |
316 'minimum' => 1, |
322 'maximum' => 100, |
317 'maximum' => 100, |
323 'sanitize_callback' => 'absint', |
318 'sanitize_callback' => 'absint', |
324 'validate_callback' => 'rest_validate_request_arg', |
319 'validate_callback' => 'rest_validate_request_arg', |
325 ), |
320 ), |
326 'search' => array( |
321 'search' => array( |
327 'description' => __( 'Limit results to those matching a string.' ), |
322 'description' => __( 'Limit results to those matching a string.' ), |
328 'type' => 'string', |
323 'type' => 'string', |
329 'sanitize_callback' => 'sanitize_text_field', |
324 'sanitize_callback' => 'sanitize_text_field', |
330 'validate_callback' => 'rest_validate_request_arg', |
325 'validate_callback' => 'rest_validate_request_arg', |
331 ), |
326 ), |
332 ); |
327 ); |
333 } |
328 } |
334 |
329 |
335 /** |
330 /** |
342 * @param array $args Optional. Additional arguments for context parameter. Default empty array. |
337 * @param array $args Optional. Additional arguments for context parameter. Default empty array. |
343 * @return array Context parameter details. |
338 * @return array Context parameter details. |
344 */ |
339 */ |
345 public function get_context_param( $args = array() ) { |
340 public function get_context_param( $args = array() ) { |
346 $param_details = array( |
341 $param_details = array( |
347 'description' => __( 'Scope under which the request is made; determines fields present in response.' ), |
342 'description' => __( 'Scope under which the request is made; determines fields present in response.' ), |
348 'type' => 'string', |
343 'type' => 'string', |
349 'sanitize_callback' => 'sanitize_key', |
344 'sanitize_callback' => 'sanitize_key', |
350 'validate_callback' => 'rest_validate_request_arg', |
345 'validate_callback' => 'rest_validate_request_arg', |
351 ); |
346 ); |
352 |
347 |
353 $schema = $this->get_item_schema(); |
348 $schema = $this->get_item_schema(); |
354 |
349 |
355 if ( empty( $schema['properties'] ) ) { |
350 if ( empty( $schema['properties'] ) ) { |
383 */ |
378 */ |
384 protected function add_additional_fields_to_object( $object, $request ) { |
379 protected function add_additional_fields_to_object( $object, $request ) { |
385 |
380 |
386 $additional_fields = $this->get_additional_fields(); |
381 $additional_fields = $this->get_additional_fields(); |
387 |
382 |
|
383 $requested_fields = $this->get_fields_for_response( $request ); |
|
384 |
388 foreach ( $additional_fields as $field_name => $field_options ) { |
385 foreach ( $additional_fields as $field_name => $field_options ) { |
389 |
386 |
390 if ( ! $field_options['get_callback'] ) { |
387 if ( ! $field_options['get_callback'] ) { |
|
388 continue; |
|
389 } |
|
390 |
|
391 if ( ! in_array( $field_name, $requested_fields, true ) ) { |
391 continue; |
392 continue; |
392 } |
393 } |
393 |
394 |
394 $object[ $field_name ] = call_user_func( $field_options['get_callback'], $object, $field_name, $request, $this->get_object_type() ); |
395 $object[ $field_name ] = call_user_func( $field_options['get_callback'], $object, $field_name, $request, $this->get_object_type() ); |
395 } |
396 } |
516 * @return array Fields to be included in the response. |
517 * @return array Fields to be included in the response. |
517 */ |
518 */ |
518 public function get_fields_for_response( $request ) { |
519 public function get_fields_for_response( $request ) { |
519 $schema = $this->get_item_schema(); |
520 $schema = $this->get_item_schema(); |
520 $fields = isset( $schema['properties'] ) ? array_keys( $schema['properties'] ) : array(); |
521 $fields = isset( $schema['properties'] ) ? array_keys( $schema['properties'] ) : array(); |
|
522 |
|
523 $additional_fields = $this->get_additional_fields(); |
|
524 foreach ( $additional_fields as $field_name => $field_options ) { |
|
525 // For back-compat, include any field with an empty schema |
|
526 // because it won't be present in $this->get_item_schema(). |
|
527 if ( is_null( $field_options['schema'] ) ) { |
|
528 $fields[] = $field_name; |
|
529 } |
|
530 } |
|
531 |
521 if ( ! isset( $request['_fields'] ) ) { |
532 if ( ! isset( $request['_fields'] ) ) { |
522 return $fields; |
533 return $fields; |
523 } |
534 } |
524 $requested_fields = is_array( $request['_fields'] ) ? $request['_fields'] : preg_split( '/[\s,]+/', $request['_fields'] ); |
535 $requested_fields = wp_parse_list( $request['_fields'] ); |
525 if ( 0 === count( $requested_fields ) ) { |
536 if ( 0 === count( $requested_fields ) ) { |
526 return $fields; |
537 return $fields; |
527 } |
538 } |
528 // Trim off outside whitespace from the comma delimited list. |
539 // Trim off outside whitespace from the comma delimited list. |
529 $requested_fields = array_map( 'trim', $requested_fields ); |
540 $requested_fields = array_map( 'trim', $requested_fields ); |
583 // Merge in any options provided by the schema property. |
594 // Merge in any options provided by the schema property. |
584 if ( isset( $params['arg_options'] ) ) { |
595 if ( isset( $params['arg_options'] ) ) { |
585 |
596 |
586 // Only use required / default from arg_options on CREATABLE endpoints. |
597 // Only use required / default from arg_options on CREATABLE endpoints. |
587 if ( WP_REST_Server::CREATABLE !== $method ) { |
598 if ( WP_REST_Server::CREATABLE !== $method ) { |
588 $params['arg_options'] = array_diff_key( $params['arg_options'], array( 'required' => '', 'default' => '' ) ); |
599 $params['arg_options'] = array_diff_key( |
|
600 $params['arg_options'], |
|
601 array( |
|
602 'required' => '', |
|
603 'default' => '', |
|
604 ) |
|
605 ); |
589 } |
606 } |
590 |
607 |
591 $endpoint_args[ $field_id ] = array_merge( $endpoint_args[ $field_id ], $params['arg_options'] ); |
608 $endpoint_args[ $field_id ] = array_merge( $endpoint_args[ $field_id ], $params['arg_options'] ); |
592 } |
609 } |
593 } |
610 } |