diff --git a/src/wp-includes/class-wp-block-supports.php b/src/wp-includes/class-wp-block-supports.php index ec5bc9c8d6846..a752d413a810a 100644 --- a/src/wp-includes/class-wp-block-supports.php +++ b/src/wp-includes/class-wp-block-supports.php @@ -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 ] ) { $output[ $attribute_name ] = $attribute_value; } else { $output[ $attribute_name ] .= " $attribute_value"; @@ -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; + } } } diff --git a/tests/phpunit/tests/blocks/getBlockWrapperAttributes.php b/tests/phpunit/tests/blocks/getBlockWrapperAttributes.php new file mode 100644 index 0000000000000..c3c31cd92e877 --- /dev/null +++ b/tests/phpunit/tests/blocks/getBlockWrapperAttributes.php @@ -0,0 +1,121 @@ +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 ); + } +}