From a5b0312f1e263772cdcdf2c03b3fa25acaf7acf0 Mon Sep 17 00:00:00 2001 From: desrosj Date: Wed, 12 Dec 2018 20:51:25 +0000 Subject: [PATCH] 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, danielbachhuber. Merges [43736] to trunk. Fixes #45099. Built from https://develop.svn.wordpress.org/trunk@43986 git-svn-id: http://core.svn.wordpress.org/trunk@43818 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/rest-api/endpoints/class-wp-rest-controller.php | 6 ++++++ wp-includes/version.php | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-controller.php index 1b73881e96..dbd129cc43 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-controller.php @@ -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() ); } diff --git a/wp-includes/version.php b/wp-includes/version.php index 0f5082bc6f..6707a3cb06 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.1-alpha-43985'; +$wp_version = '5.1-alpha-43986'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.