Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default attribute values on server side. #24909

Closed
wants to merge 4 commits into from
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
24 changes: 24 additions & 0 deletions lib/block-supports/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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;
}
47 changes: 47 additions & 0 deletions phpunit/class-block-supported-styles-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down