Make WordPress Core

Changeset 58289


Ignore:
Timestamp:
06/03/2024 07:42:57 AM (2 years ago)
Author:
isabel_brison
Message:

Editor: Add block bindings support for a __default attribute for pattern overrides.

Adds handling for a __default block binding attribute for pattern overrides that dynamically adds support for all supported block binding attributes.

Props talldanwp, petitphp, mukesh27, isabel_brison, kevin940726.
Fixes #61333.

Location:
trunk
Files:
2 edited

Legend:

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

    r58234 r58289  
    237237         *
    238238         * @since 6.5.0
     239         * @since 6.6.0 Handle the `__default` attribute for pattern overrides.
    239240         *
    240241         * @return array The computed block attributes for the provided block bindings.
    … …  
    260261                }
    261262
    262                 foreach ( $parsed_block['attrs']['metadata']['bindings'] as $attribute_name => $block_binding ) {
     263                $bindings = $parsed_block['attrs']['metadata']['bindings'];
     264
     265                /*
     266                 * If the default binding is set for pattern overrides, replace it
     267                 * with a pattern override binding for all supported attributes.
     268                 */
     269                if (
     270                        isset( $bindings['__default']['source'] ) &&
     271                        'core/pattern-overrides' === $bindings['__default']['source']
     272                ) {
     273                        $updated_bindings = array();
     274
     275                        /*
     276                         * Build a binding array of all supported attributes.
     277                         * Note that this also omits the `__default` attribute from the
     278                         * resulting array.
     279                         */
     280                        foreach ( $supported_block_attributes[ $parsed_block['blockName'] ] as $attribute_name ) {
     281                                // Retain any non-pattern override bindings that might be present.
     282                                $updated_bindings[ $attribute_name ] = isset( $bindings[ $attribute_name ] )
     283                                        ? $bindings[ $attribute_name ]
     284                                        : array( 'source' => 'core/pattern-overrides' );
     285                        }
     286                        $bindings = $updated_bindings;
     287                }
     288
     289                foreach ( $bindings as $attribute_name => $block_binding ) {
    263290                        // If the attribute is not in the supported list, process next attribute.
    264291                        if ( ! in_array( $attribute_name, $supported_block_attributes[ $this->name ], true ) ) {
    … …  
    414441                 * the rendered HTML of all its inner blocks, including any interactive block.
    415442                 */
    416                 static $root_interactive_block  = null;
     443                static $root_interactive_block = null;
    417444                /**
    418445                 * Filters whether Interactivity API should process directives.
  • trunk/tests/phpunit/tests/block-bindings/render.php

    r57754 r58289  
    235235                );
    236236        }
     237
     238        /**
     239         * Tests if the `__default` attribute is replaced with real attribues for
     240         * pattern overrides.
     241         *
     242         * @ticket 61333
     243         *
     244         * @covers WP_Block::process_block_bindings
     245         */
     246        public function test_default_binding_for_pattern_overrides() {
     247                $expected_content = 'This is the content value';
     248
     249                $block_content = <<<HTML
     250<!-- wp:paragraph {"metadata":{"bindings":{"__default":{"source":"core/pattern-overrides"}},"name":"Test"}} -->
     251<p>This should not appear</p>
     252<!-- /wp:paragraph -->
     253HTML;
     254
     255                $parsed_blocks = parse_blocks( $block_content );
     256                $block         = new WP_Block( $parsed_blocks[0], array( 'pattern/overrides' => array( 'Test' => array( 'content' => $expected_content ) ) ) );
     257                $result        = $block->render();
     258
     259                $this->assertSame(
     260                        "<p>$expected_content</p>",
     261                        trim( $result ),
     262                        'The `__default` attribute should be replaced with the real attribute prior to the callback.'
     263                );
     264        }
    237265}
Note: See TracChangeset for help on using the changeset viewer.