diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 86f0ea21a2a3c..b52ad134338dd 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -536,6 +536,7 @@ public function prepare_attributes_for_render( $attributes ) { * Sets block type properties. * * @since 5.0.0 + * @since 7.1.0 Added Support for rich-text attribute type, which is normalized to string for server-side rendering. * * @param array|string $args Array or string of arguments for registering a block type. * See WP_Block_Type::__construct() for information on accepted arguments. @@ -562,6 +563,16 @@ public function set_props( $args ) { } } + // Normalize block attribute types. The 'rich-text' type is used in + // block.json for the editor, but is not a valid JSON Schema type. + // Map it to 'string' since rich text values are always strings on + // the server side. + foreach ( $args['attributes'] as $key => $attribute ) { + if ( isset( $attribute['type'] ) && 'rich-text' === $attribute['type'] ) { + $args['attributes'][ $key ]['type'] = 'string'; + } + } + /** * Filters the arguments for registering a block type. * diff --git a/tests/phpunit/tests/blocks/wpBlockType.php b/tests/phpunit/tests/blocks/wpBlockType.php index a73efa8ce8a7d..6a7fdb72729b8 100644 --- a/tests/phpunit/tests/blocks/wpBlockType.php +++ b/tests/phpunit/tests/blocks/wpBlockType.php @@ -211,6 +211,7 @@ public function test_prepare_attributes() { /* missingDefaulted */ 'undefined' => 'include', 'intendedNull' => null, + 'richtext' => 'rich text content', ); $block_type = new WP_Block_Type( @@ -235,6 +236,9 @@ public function test_prepare_attributes() { 'type' => array( 'string', 'null' ), 'default' => 'wrong', ), + 'richtext' => array( + 'type' => 'rich-text', + ), ), ) ); @@ -249,6 +253,7 @@ public function test_prepare_attributes() { 'missingDefaulted' => 'define', 'undefined' => 'include', 'intendedNull' => null, + 'richtext' => 'rich text content', ), $prepared_attributes );