REST API: Don't add fields to object when not included in ?_fields=.

In [43087], we improved REST API performance by only rendering the fields specified in the request. Similarly, any fields registered with `register_rest_field()` should only be rendered when included in `?_fields=`.

Props dlh.
Fixes #45099.

Built from https://develop.svn.wordpress.org/branches/5.0@43736


git-svn-id: http://core.svn.wordpress.org/branches/5.0@43565 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
danielbachhuber 2018-10-17 02:41:27 +00:00
parent 9cafddd841
commit 12b674fb3a
2 changed files with 7 additions and 1 deletions

View File

@ -385,12 +385,18 @@ abstract class WP_REST_Controller {
$additional_fields = $this->get_additional_fields();
$requested_fields = $this->get_fields_for_response( $request );
foreach ( $additional_fields as $field_name => $field_options ) {
if ( ! $field_options['get_callback'] ) {
continue;
}
if ( ! in_array( $field_name, $requested_fields, true ) ) {
continue;
}
$object[ $field_name ] = call_user_func( $field_options['get_callback'], $object, $field_name, $request, $this->get_object_type() );
}

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.0-alpha-43735';
$wp_version = '5.0-alpha-43736';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.