Skip to content
Permalink
Browse files

REST API: Preserve unknown, respect `null` in server-side block rende…

…ring.

* Skips validation where there is no attribute definition, but keeps the attribute value. Previously, the attribute would be omitted from the attributes passed to `render_callback`. Notably, this resolves an issue where `render_callback` cannot receive a block's `align` and `customClassName` attribute values, since these are defined as a client-side filter.
* Validates `null` as a proper value in its own right. Previously, a client implementation of a block could track `{"attribute":null}` as an explicitly empty value, and the server would wrongly initiate defaulting behavior. The new behavior will now only populate a default value if the attribute is not defined at all, including when unset in its being invalid per the attribute schema.

Props aduth, noisysocks, youknowriad.
See #45145 for the patch, #45098 for the original ticket.


git-svn-id: https://develop.svn.wordpress.org/branches/5.0@43918 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information...
danielbachhuber committed Nov 21, 2018
1 parent 8ef1b05 commit 9ee7a34c9f9b153103ef0d551ffb8fee8df9475e
@@ -123,38 +123,49 @@ public function is_dynamic() {
/**
* Validates attributes against the current block schema, populating
* defaulted and missing values, and omitting unknown attributes.
* defaulted and missing values.
*
* @since 5.0.0
*
* @param array $attributes Original block attributes.
* @return array Prepared block attributes.
*/
public function prepare_attributes_for_render( $attributes ) {
// If there are no attribute definitions for the block type, skip
// processing and return vebatim.
if ( ! isset( $this->attributes ) ) {
return $attributes;
}
$prepared_attributes = array();
foreach ( $attributes as $attribute_name => $value ) {
// If the attribute is not defined by the block type, it cannot be
// validated.
if ( ! isset( $this->attributes[ $attribute_name ] ) ) {
continue;
}
foreach ( $this->attributes as $attribute_name => $schema ) {
$value = null;
$schema = $this->attributes[ $attribute_name ];
if ( isset( $attributes[ $attribute_name ] ) ) {
$is_valid = rest_validate_value_from_schema( $attributes[ $attribute_name ], $schema );
if ( ! is_wp_error( $is_valid ) ) {
$value = rest_sanitize_value_from_schema( $attributes[ $attribute_name ], $schema );
}
// Validate value by JSON schema. An invalid value should revert to
// its default, if one exists. This occurs by virtue of the missing
// attributes loop immediately following. If there is not a default
// assigned, the attribute value should remain unset.
$is_valid = rest_validate_value_from_schema( $value, $schema );
if ( is_wp_error( $is_valid ) ) {
unset( $attributes[ $attribute_name ] );
}
}
if ( is_null( $value ) && isset( $schema['default'] ) ) {
$value = $schema['default'];
// Populate values of any missing attributes for which the block type
// defines a default.
$missing_schema_attributes = array_diff_key( $this->attributes, $attributes );
foreach ( $missing_schema_attributes as $attribute_name => $schema ) {
if ( isset( $schema['default'] ) ) {
$attributes[ $attribute_name ] = $schema['default'];
}
$prepared_attributes[ $attribute_name ] = $value;
}
return $prepared_attributes;
return $attributes;
}
/**
@@ -61,6 +61,7 @@ public function register_routes() {
'type' => 'object',
'additionalProperties' => false,
'properties' => $block_type->get_attributes(),
'default' => array(),
),
'post_id' => array(
'description' => __( 'ID of the post context.' ),
@@ -168,7 +168,8 @@ public function test_prepare_attributes() {
'wrongType' => 5,
'wrongTypeDefaulted' => 5,
/* missingDefaulted */
'undefined' => 'omit',
'undefined' => 'include',
'intendedNull' => null,
);
$block_type = new WP_Block_Type(
@@ -189,6 +190,10 @@ public function test_prepare_attributes() {
'type' => 'string',
'default' => 'define',
),
'intendedNull' => array(
'type' => array( 'string', 'null' ),
'default' => 'wrong',
),
),
)
);
@@ -198,14 +203,29 @@ public function test_prepare_attributes() {
$this->assertEquals(
array(
'correct' => 'include',
'wrongType' => null,
/* wrongType */
'wrongTypeDefaulted' => 'defaulted',
'missingDefaulted' => 'define',
'undefined' => 'include',
'intendedNull' => null,
),
$prepared_attributes
);
}
/**
* @ticket 45145
*/
function test_prepare_attributes_none_defined() {
$attributes = array( 'exists' => 'keep' );
$block_type = new WP_Block_Type( 'core/dummy', array() );
$prepared_attributes = $block_type->prepare_attributes_for_render( $attributes );
$this->assertEquals( $attributes, $prepared_attributes );
}
/**
* @ticket 45097
*/
@@ -319,7 +319,9 @@ public function test_get_item_default_attributes() {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( self::$block_name );
$defaults = array();
foreach ( $block_type->attributes as $key => $attribute ) {
$defaults[ $key ] = isset( $attribute['default'] ) ? $attribute['default'] : null;
if ( isset( $attribute['default'] ) ) {
$defaults[ $key ] = $attribute['default'];
}
}
$request = new WP_REST_Request( 'GET', self::$rest_api_route . self::$block_name );
@@ -4314,6 +4314,7 @@ mockedApiResponse.Schema = {
},
"attributes": {
"required": false,
"default": [],
"description": "Attributes for core/block block",
"type": "object"
},
@@ -4353,6 +4354,7 @@ mockedApiResponse.Schema = {
},
"attributes": {
"required": false,
"default": [],
"description": "Attributes for core/latest-comments block",
"type": "object"
},
@@ -4392,6 +4394,7 @@ mockedApiResponse.Schema = {
},
"attributes": {
"required": false,
"default": [],
"description": "Attributes for core/archives block",
"type": "object"
},
@@ -4431,6 +4434,7 @@ mockedApiResponse.Schema = {
},
"attributes": {
"required": false,
"default": [],
"description": "Attributes for core/categories block",
"type": "object"
},
@@ -4470,6 +4474,7 @@ mockedApiResponse.Schema = {
},
"attributes": {
"required": false,
"default": [],
"description": "Attributes for core/latest-posts block",
"type": "object"
},
@@ -4509,6 +4514,7 @@ mockedApiResponse.Schema = {
},
"attributes": {
"required": false,
"default": [],
"description": "Attributes for core/shortcode block",
"type": "object"
},

0 comments on commit 9ee7a34

Please sign in to comment.
You can’t perform that action at this time.