Skip to content
Closed
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
23 changes: 22 additions & 1 deletion src/wp-includes/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,14 +501,16 @@ public function prepare_attributes_for_render( $attributes ) {
return $attributes;
}

$attribute_schemas = $this->get_attributes_for_rest_schema();

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;
}

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

// Validate value by JSON schema. An invalid value should revert to
// its default, if one exists. This occurs by virtue of the missing
Expand Down Expand Up @@ -590,6 +592,25 @@ public function get_attributes() {
array();
}

/**
* Gets block attributes normalized for REST schema validation.
*
* @since 7.1.0
*
* @return array Block attributes with REST-compatible types.
*/
public function get_attributes_for_rest_schema() {
$attributes = $this->get_attributes();

foreach ( $attributes as $attribute_name => $attribute_schema ) {
if ( isset( $attribute_schema['type'] ) && 'rich-text' === $attribute_schema['type'] ) {
$attributes[ $attribute_name ]['type'] = 'string';
}
}

return $attributes;
}

/**
* Get block variations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function register_routes() {

$schema = array(
'type' => 'object',
'properties' => $block->get_attributes(),
'properties' => $block->get_attributes_for_rest_schema(),
'additionalProperties' => false,
);

Expand All @@ -80,7 +80,7 @@ public function register_routes() {

$schema = array(
'type' => 'object',
'properties' => $block->get_attributes(),
'properties' => $block->get_attributes_for_rest_schema(),
'additionalProperties' => false,
);

Expand Down
32 changes: 32 additions & 0 deletions tests/phpunit/tests/rest-api/rest-block-renderer-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@
'type' => 'integer',
),
),
'rich_text' => array(
'type' => 'rich-text',
),
),
'render_callback' => array( $this, 'render_test_block' ),
)
Expand Down Expand Up @@ -421,6 +424,35 @@
);
}

/**
* Tests rendering a block with a rich text attribute.
*
* @covers WP_REST_Block_Renderer_Controller::register_routes
*/
public function test_get_item_with_rich_text_attribute() {
wp_set_current_user( self::$user_id );

$request = new WP_REST_Request( 'GET', self::$rest_api_route . self::$block_name );
$request->set_param( 'context', 'edit' );
$request->set_param(
'attributes',
array(
'some_string' => 'some_value',
'rich_text' => '<strong>Example</strong>',

Check warning on line 441 in tests/phpunit/tests/rest-api/rest-block-renderer-controller.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Array double arrow not aligned correctly; expected 3 space(s) between "'rich_text'" and double arrow, but found 1.
)
);
$response = rest_get_server()->dispatch( $request );

$this->assertSame( 200, $response->get_status() );
$this->assertSame(
array(
'some_string' => 'some_value',
'rich_text' => '<strong>Example</strong>',

Check warning on line 450 in tests/phpunit/tests/rest-api/rest-block-renderer-controller.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Array double arrow not aligned correctly; expected 3 space(s) between "'rich_text'" and double arrow, but found 1.
),
json_decode( $response->get_data()['rendered'], true )
);
}

/**
* Check filtering block output using the pre_render_block filter.
*
Expand Down
Loading