Make WordPress Core


Ignore:
Timestamp:
08/06/2025 06:20:22 PM (9 months ago)
Author:
Bernhard Reiter
Message:

Block Bindings: Add filter to set supported block attributes.

Add a new filter, block_bindings_supported_attributes_{$block_type}, that allows customizing which of a block's attributes can be connected to a Block Bindings source.

Props bernhard-reiter, gziolo, maxschmeling.
Closes #62090.

File:
1 edited

Legend:

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

    r59095 r60611  
    1818
    1919    /**
     20     * Sets up shared fixtures.
     21     *
     22     * @since 6.9.0
     23     */
     24    public static function wpSetUpBeforeClass() {
     25        register_block_type(
     26            'test/block',
     27            array(
     28                'attributes'      => array(
     29                    'myAttribute' => array(
     30                        'type' => 'string',
     31                    ),
     32                ),
     33                'render_callback' => function ( $attributes ) {
     34                    return '<p>' . esc_html( $attributes['myAttribute'] ) . '</p>';
     35                },
     36            )
     37        );
     38    }
     39
     40    /**
    2041     * Tear down after each test.
    2142     *
     
    3354
    3455    /**
     56     * Tear down after class.
     57     *
     58     * @since 6.9.0
     59     */
     60    public static function wpTearDownAfterClass() {
     61        unregister_block_type( 'test/block' );
     62    }
     63
     64    /**
    3565     * Test if the block content is updated with the value returned by the source.
    3666     *
     
    6595            $block->attributes['content'],
    6696            "The 'content' attribute should be updated with the value returned by the source."
     97        );
     98        $this->assertSame(
     99            '<p>test source value</p>',
     100            trim( $result ),
     101            'The block content should be updated with the value returned by the source.'
     102        );
     103    }
     104
     105    /**
     106     * Test if the block_bindings_supported_attributes_{$block_type} filter is applied correctly.
     107     *
     108     * @ticket 62090
     109     */
     110    public function test_filter_block_bindings_supported_attributes() {
     111        $get_value_callback = function () {
     112            return 'test source value';
     113        };
     114
     115        register_block_bindings_source(
     116            self::SOURCE_NAME,
     117            array(
     118                'label'              => self::SOURCE_LABEL,
     119                'get_value_callback' => $get_value_callback,
     120            )
     121        );
     122
     123        add_filter(
     124            'block_bindings_supported_attributes_test/block',
     125            function ( $supported_attributes ) {
     126                $supported_attributes[] = 'myAttribute';
     127                return $supported_attributes;
     128            }
     129        );
     130
     131        $block_content = <<<HTML
     132<!-- wp:test/block {"metadata":{"bindings":{"myAttribute":{"source":"test/source"}}}} -->
     133<p>This should not appear</p>
     134<!-- /wp:test/block -->
     135HTML;
     136        $parsed_blocks = parse_blocks( $block_content );
     137        $block         = new WP_Block( $parsed_blocks[0] );
     138        $result        = $block->render();
     139
     140        $this->assertSame(
     141            'test source value',
     142            $block->attributes['myAttribute'],
     143            "The 'myAttribute' attribute should be updated with the value returned by the source."
    67144        );
    68145        $this->assertSame(
Note: See TracChangeset for help on using the changeset viewer.