Make WordPress Core


Ignore:
Timestamp:
08/27/2025 03:41:36 PM (2 months ago)
Author:
Bernhard Reiter
Message:

Block Bindings: Allow generically setting rich-text block attributes.

Replace the existing block-specific, hard-coded, logic in the WP_Block class with more generic code that is able to locate and replace a rich-text sourced attribute based on the selector definition in its block.json.

This should make it easier to add block bindings support for more block attributes.

Props bernhard-reiter, jonsurrell, gziolo, cbravobernal, dmsnell.
Fixes #63840.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/block-bindings/render.php

    r60611 r60684  
    6262    }
    6363
     64    public function data_update_block_with_value_from_source() {
     65        return array(
     66            'paragraph block' => array(
     67                'content',
     68                <<<HTML
     69<!-- wp:paragraph -->
     70<p>This should not appear</p>
     71<!-- /wp:paragraph -->
     72HTML
     73                ,
     74                '<p>test source value</p>',
     75            ),
     76            'button block'    => array(
     77                'text',
     78                <<<HTML
     79<!-- wp:button -->
     80<div class="wp-block-button"><a class="wp-block-button__link wp-element-button">This should not appear</a></div>
     81<!-- /wp:button -->
     82HTML
     83                ,
     84                '<div class="wp-block-button"><a class="wp-block-button__link wp-element-button">test source value</a></div>',
     85            ),
     86        );
     87    }
     88
    6489    /**
    6590     * Test if the block content is updated with the value returned by the source.
     
    6893     *
    6994     * @covers ::register_block_bindings_source
    70      */
    71     public function test_update_block_with_value_from_source() {
     95     *
     96     * @dataProvider data_update_block_with_value_from_source
     97     */
     98    public function test_update_block_with_value_from_source( $bound_attribute, $block_content, $expected_result ) {
    7299        $get_value_callback = function () {
    73100            return 'test source value';
     
    82109        );
    83110
    84         $block_content = <<<HTML
    85 <!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"test/source"}}}} -->
    86 <p>This should not appear</p>
    87 <!-- /wp:paragraph -->
    88 HTML;
    89         $parsed_blocks = parse_blocks( $block_content );
    90         $block         = new WP_Block( $parsed_blocks[0] );
    91         $result        = $block->render();
     111        $parsed_blocks = parse_blocks( $block_content );
     112
     113        $parsed_blocks[0]['attrs']['metadata'] = array(
     114            'bindings' => array(
     115                $bound_attribute => array(
     116                    'source' => self::SOURCE_NAME,
     117                ),
     118            ),
     119        );
     120
     121        $block  = new WP_Block( $parsed_blocks[0] );
     122        $result = $block->render();
    92123
    93124        $this->assertSame(
    94125            'test source value',
    95             $block->attributes['content'],
    96             "The 'content' attribute should be updated with the value returned by the source."
    97         );
    98         $this->assertSame(
    99             '<p>test source value</p>',
     126            $block->attributes[ $bound_attribute ],
     127            "The '{$bound_attribute}' attribute should be updated with the value returned by the source."
     128        );
     129        $this->assertSame(
     130            $expected_result,
    100131            trim( $result ),
    101132            'The block content should be updated with the value returned by the source.'
Note: See TracChangeset for help on using the changeset viewer.