Make WordPress Core

Changeset 60727

Timestamp:
09/10/2025 08:25:46 PM (2 months ago)
Author:
dmsnell
Message:

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 https://github.com/WordPress/wordpress-develop/pull/5486
Discussed in https://core.trac.wordpress.org/ticket/59622

Props dmsnell, jonsurrell, hellofromtonya, peterwilsoncc.
Fixes #59622.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-supports/background.php

    r58936 r60727  
    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' );
  • trunk/tests/phpunit/tests/block-supports/layout.php

    r60038 r60727  
    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
  • trunk/tests/phpunit/tests/block-supports/wpRenderBackgroundSupport.php

    r58936 r60727  
    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',
Note: See TracChangeset for help on using the changeset viewer.