Make WordPress Core

Changeset 59095


Ignore:
Timestamp:
09/26/2024 02:49:13 PM (7 months ago)
Author:
cbravobernal
Message:

Block bindings: Ensure block receives default bindings when render.

Fixes an issue with the image block when using pattern overrides, where the image block with overrides enabled was not outputting all the expected image attributes. Ensures that the process_block_bindings method returns any updates to the block's binding metadata along with other computed attributes.

Props talldanwp, cbravobernal, santosguillamot, mukesh27, gziolo.

Fixes #62069.

Location:
trunk
Files:
2 edited

Legend:

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

    r59080 r59095  
    238238     * @since 6.5.0
    239239     * @since 6.6.0 Handle the `__default` attribute for pattern overrides.
     240     * @since 6.7.0 Return any updated bindings metadata in the computed attributes.
    240241     *
    241242     * @return array The computed block attributes for the provided block bindings.
     
    285286            }
    286287            $bindings = $updated_bindings;
     288            /*
     289             * Update the bindings metadata of the computed attributes.
     290             * This ensures the block receives the expanded __default binding metadata when it renders.
     291             */
     292            $computed_attributes['metadata'] = array_merge(
     293                $parsed_block['attrs']['metadata'],
     294                array( 'bindings' => $bindings )
     295            );
    287296        }
    288297
  • trunk/tests/phpunit/tests/block-bindings/render.php

    r59080 r59095  
    335335
    336336    /**
    337      * Tests if the `__default` attribute is replaced with real attribues for
     337     * Tests if the `__default` attribute is replaced with real attributes for
    338338     * pattern overrides.
    339339     *
    340340     * @ticket 61333
     341     * @ticket 62069
    341342     *
    342343     * @covers WP_Block::process_block_bindings
    343344     */
    344345    public function test_default_binding_for_pattern_overrides() {
    345         $expected_content = 'This is the content value';
    346 
    347346        $block_content = <<<HTML
    348347<!-- wp:paragraph {"metadata":{"bindings":{"__default":{"source":"core/pattern-overrides"}},"name":"Test"}} -->
     
    351350HTML;
    352351
    353         $parsed_blocks = parse_blocks( $block_content );
    354         $block         = new WP_Block( $parsed_blocks[0], array( 'pattern/overrides' => array( 'Test' => array( 'content' => $expected_content ) ) ) );
    355         $result        = $block->render();
     352        $expected_content = 'This is the content value';
     353        $parsed_blocks    = parse_blocks( $block_content );
     354        $block            = new WP_Block( $parsed_blocks[0], array( 'pattern/overrides' => array( 'Test' => array( 'content' => $expected_content ) ) ) );
     355
     356        $result = $block->render();
    356357
    357358        $this->assertSame(
     
    359360            trim( $result ),
    360361            'The `__default` attribute should be replaced with the real attribute prior to the callback.'
     362        );
     363
     364        $expected_bindings_metadata = array(
     365            'content' => array( 'source' => 'core/pattern-overrides' ),
     366        );
     367        $this->assertSame(
     368            $expected_bindings_metadata,
     369            $block->attributes['metadata']['bindings'],
     370            'The __default binding should be updated with the individual binding attributes in the block metadata.'
    361371        );
    362372    }
Note: See TracChangeset for help on using the changeset viewer.