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
This commit is contained in:
Sergey Biryukov 2018-12-17 03:29:47 +00:00
parent 88d99119bc
commit ff3ea55f76
2 changed files with 11 additions and 1 deletions

View File

@ -519,6 +519,16 @@ abstract class WP_REST_Controller {
public function get_fields_for_response( $request ) { public function get_fields_for_response( $request ) {
$schema = $this->get_item_schema(); $schema = $this->get_item_schema();
$fields = isset( $schema['properties'] ) ? array_keys( $schema['properties'] ) : array(); $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'] ) ) { if ( ! isset( $request['_fields'] ) ) {
return $fields; return $fields;
} }

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @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. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.