Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add handling for __default attribute to block bindings for pattern overrides #6694

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/wp-includes/class-wp-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ public function __get( $name ) {
* block with the values of the `text_custom_field` and `url_custom_field` post meta.
*
* @since 6.5.0
* @since 6.6.0 Handle the `__default` attribute for pattern overrides.
*
* @return array The computed block attributes for the provided block bindings.
*/
Expand All @@ -259,7 +260,33 @@ private function process_block_bindings() {
return $computed_attributes;
}

foreach ( $parsed_block['attrs']['metadata']['bindings'] as $attribute_name => $block_binding ) {
$bindings = $parsed_block['attrs']['metadata']['bindings'];

/*
* If the default binding is set for pattern overrides, replace it
* with a pattern override binding for all supported attributes.
*/
if (
isset( $bindings['__default']['source'] ) &&
'core/pattern-overrides' === $bindings['__default']['source']
) {
$updated_bindings = array();

/*
* Build a binding array of all supported attributes.
* Note that this also omits the `__default` attribute from the
* resulting array.
*/
foreach ( $supported_block_attributes[ $parsed_block['blockName'] ] as $attribute_name ) {
// Retain any non-pattern override bindings that might be present.
$updated_bindings[ $attribute_name ] = isset( $bindings[ $attribute_name ] )
? $bindings[ $attribute_name ]
: array( 'source' => 'core/pattern-overrides' );
}
$bindings = $updated_bindings;
}

foreach ( $bindings as $attribute_name => $block_binding ) {
// If the attribute is not in the supported list, process next attribute.
if ( ! in_array( $attribute_name, $supported_block_attributes[ $this->name ], true ) ) {
continue;
Expand Down Expand Up @@ -413,7 +440,7 @@ public function render( $options = array() ) {
* There can be only one root interactive block at a time because the rendered HTML of that block contains
* the rendered HTML of all its inner blocks, including any interactive block.
*/
static $root_interactive_block = null;
static $root_interactive_block = null;
/**
* Filters whether Interactivity API should process directives.
*
Expand Down
28 changes: 28 additions & 0 deletions tests/phpunit/tests/block-bindings/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,32 @@ public function test_source_value_with_unsafe_html_is_sanitized() {
'The block content should be updated with the value returned by the source.'
);
}

/**
* Tests if the `__default` attribute is replaced with real attribues for
* pattern overrides.
*
* @ticket 61333
*
* @covers WP_Block::process_block_bindings
*/
public function test_default_binding_for_pattern_overrides() {
$expected_content = 'This is the content value';

$block_content = <<<HTML
<!-- wp:paragraph {"metadata":{"bindings":{"__default":{"source":"core/pattern-overrides"}},"name":"Test"}} -->
<p>This should not appear</p>
<!-- /wp:paragraph -->
HTML;

$parsed_blocks = parse_blocks( $block_content );
$block = new WP_Block( $parsed_blocks[0], array( 'pattern/overrides' => array( 'Test' => array( 'content' => $expected_content ) ) ) );
$result = $block->render();

$this->assertSame(
"<p>$expected_content</p>",
trim( $result ),
'The `__default` attribute should be replaced with the real attribute prior to the callback.'
);
}
}
Loading