Skip to content
Open
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
22 changes: 14 additions & 8 deletions src/wp-includes/class-wp-block-supports.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function apply_block_supports() {

if ( ! empty( $new_attributes ) ) {
foreach ( $new_attributes as $attribute_name => $attribute_value ) {
if ( empty( $output[ $attribute_name ] ) ) {
if ( ! array_key_exists( $attribute_name, $output ) || '' === (string) $output[ $attribute_name ] ) {
Copy link
Member

Choose a reason for hiding this comment

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

This is the meat of the fix, right? It looks right to me, and cover edge cases:

  • 0 (integer): (string) 0 = "0""" → treated as valid
  • "0" (string): (string) "0" = "0""" → treated as valid
  • "" (empty string): (string) "" = "" = "" → treated as empty (correct)
  • false: (string) false = "" = "" → treated as empty (acceptable for HTML attributes)
  • null: (string) null = "" = "" → treated as empty (acceptable)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's right. The main reason for using a string cast is to allow zero as a number or a string, but to exclude false or null.

$output[ $attribute_name ] = $attribute_value;
} else {
$output[ $attribute_name ] .= " $attribute_value";
Expand Down Expand Up @@ -184,26 +184,32 @@ function get_block_wrapper_attributes( $extra_attributes = array() ) {
$attributes_to_merge = array( 'style', 'class', 'id', 'aria-label' );
$attributes = array();
foreach ( $attributes_to_merge as $attribute_name ) {
if ( empty( $new_attributes[ $attribute_name ] ) && empty( $extra_attributes[ $attribute_name ] ) ) {
$new_value = array_key_exists( $attribute_name, $new_attributes ) ? (string) $new_attributes[ $attribute_name ] : '';
$extra_value = array_key_exists( $attribute_name, $extra_attributes ) ? (string) $extra_attributes[ $attribute_name ] : '';

if ( '' === $new_value && '' === $extra_value ) {
continue;
}

if ( empty( $new_attributes[ $attribute_name ] ) ) {
$attributes[ $attribute_name ] = $extra_attributes[ $attribute_name ];
if ( '' === $new_value ) {
$attributes[ $attribute_name ] = $extra_value;
continue;
}

if ( empty( $extra_attributes[ $attribute_name ] ) ) {
$attributes[ $attribute_name ] = $new_attributes[ $attribute_name ];
if ( '' === $extra_value ) {
$attributes[ $attribute_name ] = $new_value;
continue;
}

$attributes[ $attribute_name ] = $extra_attributes[ $attribute_name ] . ' ' . $new_attributes[ $attribute_name ];
$attributes[ $attribute_name ] = $extra_value . ' ' . $new_value;
}

foreach ( $extra_attributes as $attribute_name => $value ) {
if ( ! in_array( $attribute_name, $attributes_to_merge, true ) ) {
$attributes[ $attribute_name ] = $value;
$string_value = (string) $value;
if ( '' !== $string_value ) {
$attributes[ $attribute_name ] = $string_value;
}
}
}

Expand Down
121 changes: 121 additions & 0 deletions tests/phpunit/tests/blocks/getBlockWrapperAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
/**
* Tests for get_block_wrapper_attributes function.
*
* @package WordPress
* @subpackage Blocks
*
* @since 7.0.0
*
* @group blocks
* @covers ::get_block_wrapper_attributes
*/
class Tests_Blocks_GetBlockWrapperAttributes extends WP_UnitTestCase {

/**
* Tear down after each test.
*
* @since 7.0.0
*/
public function tear_down() {
$registry = WP_Block_Type_Registry::get_instance();
if ( $registry->is_registered( 'core/example' ) ) {
$registry->unregister( 'core/example' );
}
if ( $registry->is_registered( 'core/example' ) ) {
$registry->unregister( 'core/example' );
}

parent::tear_down();
}

/**
* @ticket 64452
*/
public function test_preserves_zero_values() {
WP_Block_Supports::init();
register_block_type(
'core/example',
array(
'supports' => array(
'customClassName' => true,
'ariaLabel' => true,
),
)
);
WP_Block_Supports::$block_to_render = array(
'blockName' => 'core/example',
'attrs' => array(
'className' => '0',
'ariaLabel' => 0,
),
);

$result = get_block_wrapper_attributes();
$this->assertSame( 'class="0 wp-block-example" aria-label="0"', $result );
}

/**
* @ticket 64452
*/
public function test_preserves_zero_values_from_extra_attributes() {
WP_Block_Supports::init();
register_block_type( 'core/example' );
WP_Block_Supports::$block_to_render = array( 'blockName' => 'core/example' );

$result = get_block_wrapper_attributes(
array(
'class' => '0',
'aria-label' => 0,
'data-foo' => 0,
'data-var' => '0',
)
);
$this->assertSame( 'class="0 wp-block-example" aria-label="0" data-foo="0" data-var="0"', $result );
}

/**
* @ticket 64452
*/
public function test_excludes_falsy_values_except_zero() {
WP_Block_Supports::init();
register_block_type(
'core/example',
array(
'supports' => array(
'customClassName' => true,
'ariaLabel' => true,
),
)
);
WP_Block_Supports::$block_to_render = array(
'blockName' => 'core/example',
'attrs' => array(
'className' => false,
'ariaLabel' => null,
),
);

$result = get_block_wrapper_attributes();
$this->assertSame( 'class="wp-block-example"', $result );
}

/**
* @ticket 64452
*/
public function test_excludes_falsy_values_except_zero_from_extra_attributes() {
WP_Block_Supports::init();
register_block_type( 'core/example' );
WP_Block_Supports::$block_to_render = array( 'blockName' => 'core/example' );

$result = get_block_wrapper_attributes(
array(
'class' => false,
'aria-label' => null,
'data-var' => false,
'data-baz' => null,
)
);
$this->assertSame( 'class="wp-block-example"', $result );
}
}
Loading