From ff3ea55f76d0c000a203e769361e323a4308748e Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 17 Dec 2018 03:29:47 +0000 Subject: [PATCH] REST API: Include fields with null schema in `get_fields_for_response()`. In [43736], we prevented rendering fields when not present in `?_fields=`. However, because `get_fields_for_response()` is dependent on `get_item_schema()`, any custom fields registered with a null schema would be incorrectly excluded from the response. Because the REST API permits a null schema for `register_rest_field()`, those fields should be included in the available fields for a response. Props danielbachhuber. Merges [43908] to trunk. Fixes #45220. Built from https://develop.svn.wordpress.org/trunk@44254 git-svn-id: http://core.svn.wordpress.org/trunk@44084 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../rest-api/endpoints/class-wp-rest-controller.php | 10 ++++++++++ wp-includes/version.php | 2 +- 2 files changed, 11 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 389262274f..6dc971517d 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-controller.php @@ -519,6 +519,16 @@ abstract class WP_REST_Controller { public function get_fields_for_response( $request ) { $schema = $this->get_item_schema(); $fields = isset( $schema['properties'] ) ? array_keys( $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; + } + } + if ( ! isset( $request['_fields'] ) ) { return $fields; } diff --git a/wp-includes/version.php b/wp-includes/version.php index 74f6dd8723..30b4a4d53d 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.1-alpha-44253'; +$wp_version = '5.1-alpha-44254'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.