From 5f9ba92af4c111e174293eac52684725f422d0cb Mon Sep 17 00:00:00 2001 From: "K. Adam White" Date: Wed, 31 Jul 2019 20:21:56 +0000 Subject: [PATCH] REST API: Skip processing fields which are not present in the selected context. In `WP_REST_Controller::get_fields_for_response()`, exclude fields which are not registered to appear in the request's context. In conjunction with r45705 this prevents the unnecessary computation of the sample permalink when making a request that is not context=edit. Props dlh. Fixes #45605. Built from https://develop.svn.wordpress.org/trunk@45706 git-svn-id: http://core.svn.wordpress.org/trunk@45517 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../endpoints/class-wp-rest-controller.php | 18 +++++++++++++++--- wp-includes/version.php | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) 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 6188bff380..cd976f3357 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-controller.php @@ -517,18 +517,30 @@ abstract class WP_REST_Controller { * @return array Fields to be included in the response. */ public function get_fields_for_response( $request ) { - $schema = $this->get_item_schema(); - $fields = isset( $schema['properties'] ) ? array_keys( $schema['properties'] ) : array(); + $schema = $this->get_item_schema(); + $properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); $additional_fields = $this->get_additional_fields(); foreach ( $additional_fields as $field_name => $field_options ) { // For back-compat, include any field with an empty schema // because it won't be present in $this->get_item_schema(). if ( is_null( $field_options['schema'] ) ) { - $fields[] = $field_name; + $properties[ $field_name ] = $field_options; } } + // Exclude fields that specify a different context than the request context. + $context = $request['context']; + if ( $context ) { + foreach ( $properties as $name => $options ) { + if ( ! empty( $options['context'] ) && ! in_array( $context, $options['context'], true ) ) { + unset( $properties[ $name ] ); + } + } + } + + $fields = array_keys( $properties ); + if ( ! isset( $request['_fields'] ) ) { return $fields; } diff --git a/wp-includes/version.php b/wp-includes/version.php index efaaf3a7fd..618a91244d 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.3-alpha-45705'; +$wp_version = '5.3-alpha-45706'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.