From 7d0e751ffbea99583e2dda8518ff5cc915bf0bd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=83=C2=B3=C3=85=E2=80=9Akowski?= Date: Thu, 26 Sep 2024 12:45:41 +0000 Subject: [PATCH] Editor: Default attribute value not used with `get_block_wrapper_attributes` Ensures that the default values defined in the schema for block attributes are used when rendering the output of the block with `get_block_wrapper_attributes` helper. Props gziolo, jonsurrell, youknowriad, ryelle. Fixes #62114. git-svn-id: https://develop.svn.wordpress.org/trunk@59093 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-block-supports.php | 2 +- tests/phpunit/tests/blocks/render.php | 50 +++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-block-supports.php b/src/wp-includes/class-wp-block-supports.php index c90b5e0c54..71d6b49691 100644 --- a/src/wp-includes/class-wp-block-supports.php +++ b/src/wp-includes/class-wp-block-supports.php @@ -104,7 +104,7 @@ class WP_Block_Supports { } $block_attributes = array_key_exists( 'attrs', self::$block_to_render ) && is_array( self::$block_to_render['attrs'] ) - ? self::$block_to_render['attrs'] + ? $block_type->prepare_attributes_for_render( self::$block_to_render['attrs'] ) : array(); $output = array(); diff --git a/tests/phpunit/tests/blocks/render.php b/tests/phpunit/tests/blocks/render.php index cb06030282..84b2382a4a 100644 --- a/tests/phpunit/tests/blocks/render.php +++ b/tests/phpunit/tests/blocks/render.php @@ -286,6 +286,56 @@ class Tests_Blocks_Render extends WP_UnitTestCase { ); } + /** + * @ticket 62114 + */ + public function test_dynamic_block_with_default_attributes() { + $settings = array( + 'attributes' => array( + 'content' => array( + 'type' => 'string', + 'default' => 'Default content.', + ), + 'align' => array( + 'type' => 'string', + 'default' => 'full', + ), + 'backgroundColor' => array( + 'type' => 'string', + 'default' => 'blueberry-1', + ), + 'textColor' => array( + 'type' => 'string', + 'default' => 'white', + ), + ), + 'supports' => array( + 'align' => array( 'wide', 'full' ), + 'color' => array( + 'background' => true, + 'text' => true, + ), + ), + 'render_callback' => function ( $attributes ) { + return '

' . $attributes['content'] . '

'; + }, + ); + register_block_type( 'core/dynamic', $settings ); + + $post_content = + 'before' . + '' . + 'after'; + + $updated_post_content = do_blocks( $post_content ); + $this->assertSame( + $updated_post_content, + 'before' . + '

Default content.

' . + 'after' + ); + } + /** * @ticket 45109 */