diff --git a/lib/block-supports/index.php b/lib/block-supports/index.php index 4d91d6f9cc77e5..d363999b099078 100644 --- a/lib/block-supports/index.php +++ b/lib/block-supports/index.php @@ -35,6 +35,8 @@ function gutenberg_apply_block_supports( $block_content, $block ) { return $block_content; } + $block = gutenberg_apply_default_block_attribute_values( $block ); + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); // If no render_callback, assume styles have been previously handled. if ( ! $block_type || ! $block_type->render_callback ) { @@ -106,3 +108,25 @@ function gutenberg_apply_block_supports( $block_content, $block ) { function gutenberg_normalize_css_rule( $css_rule_string ) { return trim( implode( ': ', preg_split( '/\s*:\s*/', $css_rule_string, 2 ) ), ';' ); } + +/** + * Applies default values to block attributes if no values are set. + * + * @param object $block The block to apply default values to. + * @return object The block with default values applied to its attributes. + */ +function gutenberg_apply_default_block_attribute_values( $block ) { + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); + // If no render_callback, assume default values have been previously handled. + if ( ! $block_type || ! $block_type->render_callback || ! $block_type->attributes ) { + return $block; + } + + foreach ( $block_type->attributes as $attribute_name => $attribute_value ) { + if ( ! isset( $block['attrs'][ $attribute_name ] ) && isset( $attribute_value['default'] ) ) { + $block['attrs'][ $attribute_name ] = $attribute_value['default']; + } + } + + return $block; +} diff --git a/phpunit/class-block-supported-styles-test.php b/phpunit/class-block-supported-styles-test.php index 9e6e3cba767ef4..ca06c0563ff19d 100644 --- a/phpunit/class-block-supported-styles-test.php +++ b/phpunit/class-block-supported-styles-test.php @@ -606,6 +606,53 @@ function test_all_supported() { $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests that default values are applied if none are saved. + */ + function test_default_values_applied() { + $block_type_settings = array( + 'attributes' => array( + 'textColor' => array( 'default' => 'red' ), + 'backgroundColor' => array( 'default' => 'black' ), + 'style' => array( + 'default' => array( + 'color' => array( + 'link' => 'var:preset|color|red', + ), + 'typography' => array( + 'fontSize' => '10', + 'lineHeight' => '10', + ), + ), + ), + ), + 'supports' => array( + '__experimentalColor' => array( + 'gradients' => true, + 'linkColor' => true, + ), + '__experimentalFontSize' => true, + '__experimentalLineHeight' => true, + 'align' => true, + ), + 'render_callback' => true, + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array(), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + + $expected_classes = 'foo-bar-class wp-block-example has-text-color has-red-color has-link-color has-background has-black-background-color'; + $expected_styles = 'test: style; --wp--style--color--link: var(--wp--preset--color--red); font-size: 10px; line-height: 10;'; + + $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles ); + } + /** * Tests that only styles for the supported flag are added. * Verify one support enabled does not imply multiple supports enabled.