Skip to content
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
57b548b
get_block_wrapper_attributes: Ensures that user-provided attributes o…
t-hamano Feb 6, 2026
dd6a4a5
Remove double blank lines
t-hamano Feb 9, 2026
68fab57
Update PHPDoc
t-hamano Feb 9, 2026
bd3be96
Update PHPDoc
t-hamano Feb 9, 2026
d5dcbb8
Remove space
t-hamano Feb 9, 2026
5cbc4b5
Simplify logic
t-hamano Feb 12, 2026
ba375fe
Merge branch 'trunk' into 64564-get-block-wrapper-attributes
t-hamano Mar 10, 2026
46877b9
Add more safeguards
t-hamano Mar 10, 2026
517f69c
Fix PHPCS error
t-hamano Mar 10, 2026
acf25f6
Fix PHPCS error
t-hamano Mar 10, 2026
11e1150
unnecessary
t-hamano Mar 10, 2026
9445bd3
Remove unnecessary test
t-hamano Mar 10, 2026
beb3a42
Merge branch 'trunk' into 64564-get-block-wrapper-attributes
t-hamano Mar 11, 2026
40caa8e
Add static modifier for style callback function
t-hamano Mar 12, 2026
0ea5ebf
Add static modifier for class callback function
t-hamano Mar 12, 2026
d68e980
Add static modifier for id callback function
t-hamano Mar 12, 2026
810d5ac
Add static modifier for aria-label callback function
t-hamano Mar 12, 2026
eff534e
Use null coalescing operator
t-hamano Mar 12, 2026
2030226
Merge branch 'trunk' into 64564-get-block-wrapper-attributes
t-hamano Mar 12, 2026
66a9169
Don't split on commas
t-hamano Mar 13, 2026
1deffb5
Merge branch 'trunk' into 64564-get-block-wrapper-attributes
t-hamano Mar 19, 2026
d7878da
Add type for data_get_block_wrapper_attributes_merge_or_override
t-hamano Mar 19, 2026
4c9fe19
Remove unnecessary indent
t-hamano Mar 19, 2026
06bae0c
Improve type for data_get_block_wrapper_attributes_merge_or_override
t-hamano Mar 19, 2026
a5955f0
Use PHPStan array shape syntax
t-hamano Mar 19, 2026
54740cf
Priorize extra attributes
t-hamano Mar 19, 2026
8bc645f
Merge branch 'trunk' into 64564-get-block-wrapper-attributes
t-hamano Mar 19, 2026
fd1f9b3
use PHPStan array shape syntax
t-hamano Mar 19, 2026
2aeb26d
Explicitly convert to array
t-hamano Mar 19, 2026
21f404f
Remove extra array wrapper from data provider
t-hamano Mar 19, 2026
0e8d485
Remove extra indentation after 21f404f
westonruter Mar 19, 2026
1ea701e
Use assertSame to catch additional expected attributes
westonruter Mar 19, 2026
da5ac94
Merge branch 'trunk' into 64564-get-block-wrapper-attributes
t-hamano Mar 19, 2026
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
19 changes: 14 additions & 5 deletions src/wp-includes/class-wp-block-supports.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,14 @@ function get_block_wrapper_attributes( $extra_attributes = array() ) {

// This is hardcoded on purpose.
// We only support a fixed list of attributes.
$attributes_to_merge = array( 'style', 'class', 'id', 'aria-label' );
$attributes = array();
foreach ( $attributes_to_merge as $attribute_name ) {
$handled_attributes = array(
Comment thread
westonruter marked this conversation as resolved.
Outdated
'style' => true,
'class' => true,
'id' => false,
'aria-label' => false,
);
$attributes = array();
foreach ( $handled_attributes as $attribute_name => $is_merged ) {
if ( empty( $new_attributes[ $attribute_name ] ) && empty( $extra_attributes[ $attribute_name ] ) ) {
continue;
}
Expand All @@ -198,11 +203,15 @@ function get_block_wrapper_attributes( $extra_attributes = array() ) {
continue;
}

$attributes[ $attribute_name ] = $extra_attributes[ $attribute_name ] . ' ' . $new_attributes[ $attribute_name ];
if ( $is_merged ) {
$attributes[ $attribute_name ] = $extra_attributes[ $attribute_name ] . ' ' . $new_attributes[ $attribute_name ];
Copy link
Copy Markdown
Member

@westonruter westonruter Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For posterity, #10877 (comment):

In the case of merging style, should special handling be added to ensure that $extra_attributes[ $attribute_name ] ends in a semicolon? The current empty space is only really appropriate for class.

} else {
$attributes[ $attribute_name ] = $extra_attributes[ $attribute_name ];
}
}

foreach ( $extra_attributes as $attribute_name => $value ) {
if ( ! in_array( $attribute_name, $attributes_to_merge, true ) ) {
if ( ! isset( $handled_attributes[ $attribute_name ] ) ) {
$attributes[ $attribute_name ] = $value;
}
}
Expand Down
128 changes: 102 additions & 26 deletions tests/phpunit/tests/blocks/supportedStyles.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,37 +90,17 @@ private function get_content_from_block( $block ) {
/**
* Returns the rendered output for the current block.
*
* @param array $block Block to render.
* @param array $block Block to render.
* @param array $extra_attributes Extra attributes to pass to get_block_wrapper_attributes().
* @return string Rendered output for the current block.
*/
private function render_example_block( $block ) {
private function render_example_block( $block, $extra_attributes = array() ) {
WP_Block_Supports::init();
WP_Block_Supports::$block_to_render = $block;
$wrapper_attributes = get_block_wrapper_attributes(
array(
'class' => 'foo-bar-class',
'style' => 'test: style;',
)
);
$wrapper_attributes = get_block_wrapper_attributes( $extra_attributes );
return '<div ' . $wrapper_attributes . '>' . self::BLOCK_CONTENT . '</div>';
}

/**
* Runs assertions that the rendered output has expected class/style attrs.
*
* @param array $block Block to render.
* @param string $expected_classes Expected output class attr string.
* @param string $expected_styles Expected output styles attr string.
*/
private function assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ) {
$styled_block = $this->render_example_block( $block );
$class_list = $this->get_attribute_from_block( 'class', $styled_block );
$style_list = $this->get_attribute_from_block( 'style', $styled_block );

$this->assertSame( $expected_classes, $class_list, 'Class list does not match expected classes' );
$this->assertSame( $expected_styles, $style_list, 'Style list does not match expected styles' );
}

/**
* Runs assertions that the rendered output has expected content and class/style attrs.
*
Expand All @@ -129,7 +109,13 @@ private function assert_styles_and_classes_match( $block, $expected_classes, $ex
* @param string $expected_styles Expected output styles attr string.
*/
private function assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles ) {
$styled_block = $this->render_example_block( $block );
$styled_block = $this->render_example_block(
$block,
array(
'class' => 'foo-bar-class',
'style' => 'test: style;',
)
);

// Ensure blocks to not add extra whitespace.
$this->assertSame( $styled_block, trim( $styled_block ) );
Expand Down Expand Up @@ -158,7 +144,13 @@ private function assert_content_and_styles_and_classes_match( $block, $expected_
* @param string $expected_aria_label Expected output aria-label attr string.
*/
private function assert_content_and_aria_label_match( $block, $expected_aria_label ) {
$styled_block = $this->render_example_block( $block );
$styled_block = $this->render_example_block(
$block,
array(
'class' => 'foo-bar-class',
'style' => 'test: style;',
)
);
$content = $this->get_content_from_block( $styled_block );

$this->assertSame( self::BLOCK_CONTENT, $content, 'Block content does not match expected content' );
Expand Down Expand Up @@ -201,6 +193,43 @@ public function test_named_color_support() {
$this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles );
}

/**
* Ensures that class and style attributes from extra attributes and block supports are merged.
*
* @ticket 64603
* @covers WP_Block_Supports::get_block_wrapper_attributes()
*/
public function test_block_wrapper_attributes_merge() {
$block_type_settings = array(
'attributes' => array(),
'supports' => array(
'color' => true,
),
'render_callback' => true,
);
$this->register_block_type( 'core/example-merge', $block_type_settings );

$block = array(
'blockName' => 'core/example-merge',
'attrs' => array(
'style' => array(
'color' => array(
'text' => '#000',
'background' => '#fff',
),
),
),
'innerBlock' => array(),
'innerContent' => array(),
'innerHTML' => array(),
);

$expected_styles = 'test: style;color:#000;background-color:#fff;';
$expected_classes = 'foo-bar-class wp-block-example-merge has-text-color has-background';

$this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles );
}

/**
* Tests color support for custom colors.
*/
Expand Down Expand Up @@ -728,6 +757,53 @@ public function test_aria_label_support() {
$this->assert_content_and_aria_label_match( $block, 'Label' );
}

/**
* Ensures that user-provided attributes override the attributes generated by block supports.
*
* @ticket 64603
* @covers WP_Block_Supports::get_block_wrapper_attributes()
*/
public function test_block_wrapper_attributes_prefer_user_id_and_aria_label_over_generated() {
$block_type_settings = array(
'attributes' => array(),
'supports' => array(
'ariaLabel' => true,
'anchor' => true,
),
);
$this->register_block_type( 'core/example-override', $block_type_settings );

$block = array(
'blockName' => 'core/example-override',
'attrs' => array(
'ariaLabel' => 'Generated label',
'anchor' => 'generated-id',
),
'innerBlock' => array(),
'innerContent' => array(),
'innerHTML' => array(),
);

$styled_block = $this->render_example_block(
$block,
array(
'aria-label' => 'User label',
'id' => 'user-id',
)
);

$this->assertSame(
'User label',
$this->get_attribute_from_block( 'aria-label', $styled_block ),
'Aria-label should prefer user-provided value over generated one'
);
$this->assertSame(
'user-id',
$this->get_attribute_from_block( 'id', $styled_block ),
'Id should prefer user-provided value over generated one'
);
}

/**
* Ensures libxml_internal_errors is being used instead of @ warning suppression
*/
Expand Down
Loading