Skip to content

Commit 1ebc4c5

Browse files
committed
Block Supports: Avoid throwing warning when checking for class attribute as string.
When encountering HTML tags with boolean or missing tags, the get_attribute() method in the HTML API returns true and null, respectively. If these returned values are sent directly into string comparison functions then as of PHP 8.0 they will throw E_DEPRECATED errors. In this patch, block supports is enhanced to check that the class value is a string before it performs string operations on it. Also in this patch: using `assertEqualHTML()` in background support test instead of `assertSame()` Developed in #5486 Discussed in https://core.trac.wordpress.org/ticket/59622 Props dmsnell, jonsurrell, hellofromtonya, peterwilsoncc. Fixes #59622. git-svn-id: https://develop.svn.wordpress.org/trunk@60727 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 00b3b63 commit 1ebc4c5

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

src/wp-includes/block-supports/background.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,13 @@ function wp_render_background_support( $block_content, $block ) {
8787

8888
if ( $tags->next_tag() ) {
8989
$existing_style = $tags->get_attribute( 'style' );
90-
$updated_style = '';
91-
92-
if ( ! empty( $existing_style ) ) {
93-
$updated_style = $existing_style;
94-
if ( ! str_ends_with( $existing_style, ';' ) ) {
95-
$updated_style .= ';';
96-
}
90+
if ( is_string( $existing_style ) && '' !== $existing_style ) {
91+
$separator = str_ends_with( $existing_style, ';' ) ? '' : ';';
92+
$updated_style = "{$existing_style}{$separator}{$styles['css']}";
93+
} else {
94+
$updated_style = $styles['css'];
9795
}
9896

99-
$updated_style .= $styles['css'];
10097
$tags->set_attribute( 'style', $updated_style );
10198
$tags->add_class( 'has-background' );
10299
}

tests/phpunit/tests/block-supports/layout.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public function test_outer_container_not_restored_for_aligned_image_block_with_t
184184
public function test_layout_support_flag_renders_classnames_on_wrapper( $args, $expected_output ) {
185185
switch_theme( 'default' );
186186
$actual_output = wp_render_layout_support_flag( $args['block_content'], $args['block'] );
187-
$this->assertSame( $expected_output, $actual_output );
187+
$this->assertEqualHTML( $expected_output, $actual_output );
188188
}
189189

190190
/**

tests/phpunit/tests/block-supports/wpRenderBackgroundSupport.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,21 @@ public function data_background_block_support() {
186186
'expected_wrapper' => '<div class="wp-block-test has-background" style="color: red;font-size: 15px;background-image:url(&#039;https://example.com/image.jpg&#039;);background-size:cover;">Content</div>',
187187
'wrapper' => '<div class="wp-block-test" style="color: red;font-size: 15px;">Content</div>',
188188
),
189+
'background image style is appended if a boolean style attribute already exists' => array(
190+
'theme_name' => 'block-theme-child-with-fluid-typography',
191+
'block_name' => 'test/background-rules-are-output',
192+
'background_settings' => array(
193+
'backgroundImage' => true,
194+
),
195+
'background_style' => array(
196+
'backgroundImage' => array(
197+
'url' => 'https://example.com/image.jpg',
198+
'source' => 'file',
199+
),
200+
),
201+
'expected_wrapper' => '<div class="has-background" classname="wp-block-test" style="background-image:url(&#039;https://example.com/image.jpg&#039;);background-size:cover;">Content</div>',
202+
'wrapper' => '<div classname="wp-block-test" style>Content</div>',
203+
),
189204
'background image style is not applied if the block does not support background image' => array(
190205
'theme_name' => 'block-theme-child-with-fluid-typography',
191206
'block_name' => 'test/background-rules-are-not-output',

0 commit comments

Comments
 (0)