Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/wp-includes/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,20 @@ public function prepare_attributes_for_render( $attributes ) {

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

// Block metadata supports "rich-text" for editor attributes, but
// REST validation expects a valid JSON Schema type.
if ( isset( $schema['type'] ) ) {
if ( 'rich-text' === $schema['type'] ) {
$schema['type'] = 'string';
} elseif ( is_array( $schema['type'] ) ) {
foreach ( $schema['type'] as $type_index => $type ) {
if ( 'rich-text' === $type ) {
$schema['type'][ $type_index ] = 'string';
}
}
}
}

// 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
Expand Down
55 changes: 55 additions & 0 deletions tests/phpunit/tests/blocks/wpBlockType.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,61 @@ public function test_prepare_attributes() {
);
}

/**
* @ticket 61406
*/
public function test_prepare_attributes_validates_rich_text_attributes_as_strings() {
$doing_it_wrong = array();
$callback = static function ( $function_name ) use ( &$doing_it_wrong ) {
if ( 'rest_validate_value_from_schema' === $function_name ) {
$doing_it_wrong[] = $function_name;
}
};

add_action( 'doing_it_wrong_run', $callback );

try {
$block_type = new WP_Block_Type(
'core/fake',
array(
'attributes' => array(
'content' => array(
'type' => 'rich-text',
),
'caption' => array(
'type' => 'rich-text',
'default' => 'Default caption',
),
'nullableContent' => array(
'type' => array( 'rich-text', 'null' ),
),
),
)
);

$prepared_attributes = $block_type->prepare_attributes_for_render(
array(
'content' => '<strong>include</strong>',
'caption' => 5,
'nullableContent' => 'nullable include',
)
);
} finally {
remove_action( 'doing_it_wrong_run', $callback );
}

$this->assertSame( '<strong>include</strong>', $prepared_attributes['content'] );
$this->assertSame( 'Default caption', $prepared_attributes['caption'] );
$this->assertSame( 'nullable include', $prepared_attributes['nullableContent'] );
$this->assertSame(
array(),
$doing_it_wrong,
'rest_validate_value_from_schema() should not emit _doing_it_wrong() for rich-text attributes.'
);
$this->assertSame( 'rich-text', $block_type->attributes['content']['type'] );
$this->assertSame( array( 'rich-text', 'null' ), $block_type->attributes['nullableContent']['type'] );
}

/**
* @ticket 45145
*/
Expand Down
Loading