286 /** |
287 /** |
287 * Filters a response based on the context defined in the schema. |
288 * Filters a response based on the context defined in the schema. |
288 * |
289 * |
289 * @since 4.7.0 |
290 * @since 4.7.0 |
290 * |
291 * |
291 * @param array $data Response data to filter. |
292 * @param array $response_data Response data to filter. |
292 * @param string $context Context defined in the schema. |
293 * @param string $context Context defined in the schema. |
293 * @return array Filtered response. |
294 * @return array Filtered response. |
294 */ |
295 */ |
295 public function filter_response_by_context( $data, $context ) { |
296 public function filter_response_by_context( $response_data, $context ) { |
296 |
297 |
297 $schema = $this->get_item_schema(); |
298 $schema = $this->get_item_schema(); |
298 |
299 |
299 return rest_filter_response_by_context( $data, $schema, $context ); |
300 return rest_filter_response_by_context( $response_data, $schema, $context ); |
300 } |
301 } |
301 |
302 |
302 /** |
303 /** |
303 * Retrieves the item's schema, conforming to JSON Schema. |
304 * Retrieves the item's schema, conforming to JSON Schema. |
304 * |
305 * |
409 /** |
410 /** |
410 * Adds the values from additional fields to a data object. |
411 * Adds the values from additional fields to a data object. |
411 * |
412 * |
412 * @since 4.7.0 |
413 * @since 4.7.0 |
413 * |
414 * |
414 * @param array $prepared Prepared response array. |
415 * @param array $response_data Prepared response array. |
415 * @param WP_REST_Request $request Full details about the request. |
416 * @param WP_REST_Request $request Full details about the request. |
416 * @return array Modified data object with additional fields. |
417 * @return array Modified data object with additional fields. |
417 */ |
418 */ |
418 protected function add_additional_fields_to_object( $prepared, $request ) { |
419 protected function add_additional_fields_to_object( $response_data, $request ) { |
419 |
420 |
420 $additional_fields = $this->get_additional_fields(); |
421 $additional_fields = $this->get_additional_fields(); |
421 |
422 |
422 $requested_fields = $this->get_fields_for_response( $request ); |
423 $requested_fields = $this->get_fields_for_response( $request ); |
423 |
424 |
428 |
429 |
429 if ( ! rest_is_field_included( $field_name, $requested_fields ) ) { |
430 if ( ! rest_is_field_included( $field_name, $requested_fields ) ) { |
430 continue; |
431 continue; |
431 } |
432 } |
432 |
433 |
433 $prepared[ $field_name ] = call_user_func( $field_options['get_callback'], $prepared, $field_name, $request, $this->get_object_type() ); |
434 $response_data[ $field_name ] = call_user_func( |
434 } |
435 $field_options['get_callback'], |
435 |
436 $response_data, |
436 return $prepared; |
437 $field_name, |
|
438 $request, |
|
439 $this->get_object_type() |
|
440 ); |
|
441 } |
|
442 |
|
443 return $response_data; |
437 } |
444 } |
438 |
445 |
439 /** |
446 /** |
440 * Updates the values of additional fields added to a data object. |
447 * Updates the values of additional fields added to a data object. |
441 * |
448 * |
442 * @since 4.7.0 |
449 * @since 4.7.0 |
443 * |
450 * |
444 * @param object $object Data model like WP_Term or WP_Post. |
451 * @param object $data_object Data model like WP_Term or WP_Post. |
445 * @param WP_REST_Request $request Full details about the request. |
452 * @param WP_REST_Request $request Full details about the request. |
446 * @return true|WP_Error True on success, WP_Error object if a field cannot be updated. |
453 * @return true|WP_Error True on success, WP_Error object if a field cannot be updated. |
447 */ |
454 */ |
448 protected function update_additional_fields_for_object( $object, $request ) { |
455 protected function update_additional_fields_for_object( $data_object, $request ) { |
449 $additional_fields = $this->get_additional_fields(); |
456 $additional_fields = $this->get_additional_fields(); |
450 |
457 |
451 foreach ( $additional_fields as $field_name => $field_options ) { |
458 foreach ( $additional_fields as $field_name => $field_options ) { |
452 if ( ! $field_options['update_callback'] ) { |
459 if ( ! $field_options['update_callback'] ) { |
453 continue; |
460 continue; |
456 // Don't run the update callbacks if the data wasn't passed in the request. |
463 // Don't run the update callbacks if the data wasn't passed in the request. |
457 if ( ! isset( $request[ $field_name ] ) ) { |
464 if ( ! isset( $request[ $field_name ] ) ) { |
458 continue; |
465 continue; |
459 } |
466 } |
460 |
467 |
461 $result = call_user_func( $field_options['update_callback'], $request[ $field_name ], $object, $field_name, $request, $this->get_object_type() ); |
468 $result = call_user_func( |
|
469 $field_options['update_callback'], |
|
470 $request[ $field_name ], |
|
471 $data_object, |
|
472 $field_name, |
|
473 $request, |
|
474 $this->get_object_type() |
|
475 ); |
462 |
476 |
463 if ( is_wp_error( $result ) ) { |
477 if ( is_wp_error( $result ) ) { |
464 return $result; |
478 return $result; |
465 } |
479 } |
466 } |
480 } |
560 $properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); |
574 $properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); |
561 |
575 |
562 $additional_fields = $this->get_additional_fields(); |
576 $additional_fields = $this->get_additional_fields(); |
563 |
577 |
564 foreach ( $additional_fields as $field_name => $field_options ) { |
578 foreach ( $additional_fields as $field_name => $field_options ) { |
565 // For back-compat, include any field with an empty schema |
579 /* |
566 // because it won't be present in $this->get_item_schema(). |
580 * For back-compat, include any field with an empty schema |
|
581 * because it won't be present in $this->get_item_schema(). |
|
582 */ |
567 if ( is_null( $field_options['schema'] ) ) { |
583 if ( is_null( $field_options['schema'] ) ) { |
568 $properties[ $field_name ] = $field_options; |
584 $properties[ $field_name ] = $field_options; |
569 } |
585 } |
570 } |
586 } |
571 |
587 |
579 } |
595 } |
580 } |
596 } |
581 |
597 |
582 $fields = array_keys( $properties ); |
598 $fields = array_keys( $properties ); |
583 |
599 |
|
600 /* |
|
601 * '_links' and '_embedded' are not typically part of the item schema, |
|
602 * but they can be specified in '_fields', so they are added here as a |
|
603 * convenience for checking with rest_is_field_included(). |
|
604 */ |
|
605 $fields[] = '_links'; |
|
606 if ( $request->has_param( '_embed' ) ) { |
|
607 $fields[] = '_embedded'; |
|
608 } |
|
609 |
|
610 $fields = array_unique( $fields ); |
|
611 |
584 if ( ! isset( $request['_fields'] ) ) { |
612 if ( ! isset( $request['_fields'] ) ) { |
585 return $fields; |
613 return $fields; |
586 } |
614 } |
587 $requested_fields = wp_parse_list( $request['_fields'] ); |
615 $requested_fields = wp_parse_list( $request['_fields'] ); |
588 if ( 0 === count( $requested_fields ) ) { |
616 if ( 0 === count( $requested_fields ) ) { |
595 $requested_fields[] = 'id'; |
623 $requested_fields[] = 'id'; |
596 } |
624 } |
597 // Return the list of all requested fields which appear in the schema. |
625 // Return the list of all requested fields which appear in the schema. |
598 return array_reduce( |
626 return array_reduce( |
599 $requested_fields, |
627 $requested_fields, |
600 static function( $response_fields, $field ) use ( $fields ) { |
628 static function ( $response_fields, $field ) use ( $fields ) { |
601 if ( in_array( $field, $fields, true ) ) { |
629 if ( in_array( $field, $fields, true ) ) { |
602 $response_fields[] = $field; |
630 $response_fields[] = $field; |
603 return $response_fields; |
631 return $response_fields; |
604 } |
632 } |
605 // Check for nested fields if $field is not a direct match. |
633 // Check for nested fields if $field is not a direct match. |
606 $nested_fields = explode( '.', $field ); |
634 $nested_fields = explode( '.', $field ); |
607 // A nested field is included so long as its top-level property |
635 /* |
608 // is present in the schema. |
636 * A nested field is included so long as its top-level property |
|
637 * is present in the schema. |
|
638 */ |
609 if ( in_array( $nested_fields[0], $fields, true ) ) { |
639 if ( in_array( $nested_fields[0], $fields, true ) ) { |
610 $response_fields[] = $field; |
640 $response_fields[] = $field; |
611 } |
641 } |
612 return $response_fields; |
642 return $response_fields; |
613 }, |
643 }, |