From 1927fa1c54e1b6c9dc1ba092158e271798e0b2ed Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Fri, 28 Aug 2020 21:28:43 -0400 Subject: [PATCH 1/4] add function to apply defualt values --- lib/block-supports/index.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/block-supports/index.php b/lib/block-supports/index.php index 4d91d6f9cc77e5..8d6a2a4d706cf6 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 ) { + 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; +} From 774fb171414df5f12069db49590dbb05ac0311cc Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 31 Aug 2020 19:35:59 -0400 Subject: [PATCH 2/4] fix failing php test --- lib/block-supports/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/block-supports/index.php b/lib/block-supports/index.php index 8d6a2a4d706cf6..d363999b099078 100644 --- a/lib/block-supports/index.php +++ b/lib/block-supports/index.php @@ -118,7 +118,7 @@ function gutenberg_normalize_css_rule( $css_rule_string ) { 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 ) { + if ( ! $block_type || ! $block_type->render_callback || ! $block_type->attributes ) { return $block; } From 68cd45c4d19959fa78be9f99640981ac99ed346a Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 31 Aug 2020 19:48:54 -0400 Subject: [PATCH 3/4] add test for defaults applied --- phpunit/class-block-supported-styles-test.php | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/phpunit/class-block-supported-styles-test.php b/phpunit/class-block-supported-styles-test.php index 9e6e3cba767ef4..ebe54f65234b83 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. From 95536645fe32459cb3a6fe007f792d79e0529793 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 31 Aug 2020 20:44:19 -0400 Subject: [PATCH 4/4] php format --- phpunit/class-block-supported-styles-test.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/phpunit/class-block-supported-styles-test.php b/phpunit/class-block-supported-styles-test.php index ebe54f65234b83..ca06c0563ff19d 100644 --- a/phpunit/class-block-supported-styles-test.php +++ b/phpunit/class-block-supported-styles-test.php @@ -614,13 +614,13 @@ function test_default_values_applied() { 'attributes' => array( 'textColor' => array( 'default' => 'red' ), 'backgroundColor' => array( 'default' => 'black' ), - 'style' => array( + 'style' => array( 'default' => array( - 'color' => array( + 'color' => array( 'link' => 'var:preset|color|red', ), 'typography' => array( - 'fontSize' => '10', + 'fontSize' => '10', 'lineHeight' => '10', ), ),