Skip to content

Commit

Permalink
Framework: Respect null as a valid attribute type
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Nov 16, 2018
1 parent 8fa74be commit bc53c47
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
14 changes: 5 additions & 9 deletions lib/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,14 @@ public function prepare_attributes_for_render( $attributes ) {

$schema = $this->attributes[ $attribute_name ];

// Validate value by JSON 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 ) ) {
// Assigning `null` will trigger defaulting, if applicable.
$value = null;
unset( $attributes[ $attribute_name ] );
}

if ( is_null( $value ) && isset( $schema['default'] ) ) {
$value = $schema['default'];
}

$attributes[ $attribute_name ] = $value;
}

// Populate values of any missing attributes for which the block type
Expand Down
8 changes: 7 additions & 1 deletion phpunit/class-block-type-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ function test_prepare_attributes() {
'wrongTypeDefaulted' => 5,
/* missingDefaulted */
'undefined' => 'include',
'intendedNull' => null,
);

$block_type = new WP_Block_Type(
Expand All @@ -160,6 +161,10 @@ function test_prepare_attributes() {
'type' => 'string',
'default' => 'define',
),
'intendedNull' => array(
'type' => array( 'string', 'null' ),
'default' => 'wrong',
),
),
)
);
Expand All @@ -169,10 +174,11 @@ function test_prepare_attributes() {
$this->assertEquals(
array(
'correct' => 'include',
'wrongType' => null,
/* wrongType */
'wrongTypeDefaulted' => 'defaulted',
'missingDefaulted' => 'define',
'undefined' => 'include',
'intendedNull' => null,
),
$prepared_attributes
);
Expand Down

0 comments on commit bc53c47

Please sign in to comment.