From fcc15f9cecf378b853293b12c1a28bcd4aeb3eda Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Fri, 31 May 2024 17:13:19 +0800 Subject: [PATCH 1/8] Add handling for __default attribute to block bindings for pattern overrides --- src/wp-includes/class-wp-block.php | 26 ++++++++++++- tests/phpunit/tests/block-bindings/render.php | 37 +++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index 3d395255fe74d..99717b29e05e9 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -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. */ @@ -259,7 +260,28 @@ 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'] + ) { + // Build an binding array of all supported attributes. + // Note that this also omits the `__default` attribute from the + // resulting array. + foreach ( $supported_block_attrs[ $parsed_block['blockName'] ] as $attribute_name ) { + $updated_bindings = array(); + // 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; @@ -413,7 +435,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. * diff --git a/tests/phpunit/tests/block-bindings/render.php b/tests/phpunit/tests/block-bindings/render.php index aac4c417fd43f..c1a9fc4737536 100644 --- a/tests/phpunit/tests/block-bindings/render.php +++ b/tests/phpunit/tests/block-bindings/render.php @@ -234,4 +234,41 @@ 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 block content is sanitized when unsafe HTML is passed. + * + * @ticket 61333 + * + * @covers ::register_block_bindings_source + */ + public function test_default_binding_for_pattern_overrides() { + $get_value_callback = function ( $source_args, $block_instance, $attribute_name ) { + $value = $source_args['key']; + return "The attribute name is '$attribute_name'"; + }; + + register_block_bindings_source( + 'core/pattern-overrides', + array( + 'label' => 'Pattern overrides', + 'get_value_callback' => $get_value_callback, + ) + ); + + $block_content = << +

This should not appear

+ +HTML; + $parsed_blocks = parse_blocks( $block_content ); + $block = new WP_Block( $parsed_blocks[0] ); + $result = $block->render(); + + $this->assertSame( + "

The attribute name is 'content'

", + trim( $result ), + 'The `__default` attribute should be replaced with the real attribute prior to the callback.' + ); + } } From 0b4dde4944881be7d5dbd5ff538643d6afc9b72f Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Fri, 31 May 2024 17:22:38 +0800 Subject: [PATCH 2/8] Fix undefined variable error --- src/wp-includes/class-wp-block.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index 99717b29e05e9..299b6c72f4728 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -271,7 +271,7 @@ private function process_block_bindings() { // Build an binding array of all supported attributes. // Note that this also omits the `__default` attribute from the // resulting array. - foreach ( $supported_block_attrs[ $parsed_block['blockName'] ] as $attribute_name ) { + foreach ( $supported_block_attributes[ $parsed_block['blockName'] ] as $attribute_name ) { $updated_bindings = array(); // Retain any non-pattern override bindings that might be present. $updated_bindings[ $attribute_name ] = isset( $bindings[ $attribute_name ] ) From 5f41050e32ad95a490efcf7f37e041b32c01f4a9 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Fri, 31 May 2024 17:44:43 +0800 Subject: [PATCH 3/8] Try changing label used in test --- tests/phpunit/tests/block-bindings/render.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/phpunit/tests/block-bindings/render.php b/tests/phpunit/tests/block-bindings/render.php index c1a9fc4737536..a9264c0b44b94 100644 --- a/tests/phpunit/tests/block-bindings/render.php +++ b/tests/phpunit/tests/block-bindings/render.php @@ -244,14 +244,13 @@ public function test_source_value_with_unsafe_html_is_sanitized() { */ public function test_default_binding_for_pattern_overrides() { $get_value_callback = function ( $source_args, $block_instance, $attribute_name ) { - $value = $source_args['key']; return "The attribute name is '$attribute_name'"; }; register_block_bindings_source( 'core/pattern-overrides', array( - 'label' => 'Pattern overrides', + 'label' => self::SOURCE_LABEL, 'get_value_callback' => $get_value_callback, ) ); From c91460a3bd71ac218500fe60c1d66cab9ccc58fc Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Fri, 31 May 2024 17:59:33 +0800 Subject: [PATCH 4/8] Maybe pattern overrides is registered for the test? --- tests/phpunit/tests/block-bindings/render.php | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/tests/phpunit/tests/block-bindings/render.php b/tests/phpunit/tests/block-bindings/render.php index a9264c0b44b94..acdeec98bc9d0 100644 --- a/tests/phpunit/tests/block-bindings/render.php +++ b/tests/phpunit/tests/block-bindings/render.php @@ -243,29 +243,20 @@ public function test_source_value_with_unsafe_html_is_sanitized() { * @covers ::register_block_bindings_source */ public function test_default_binding_for_pattern_overrides() { - $get_value_callback = function ( $source_args, $block_instance, $attribute_name ) { - return "The attribute name is '$attribute_name'"; - }; - - register_block_bindings_source( - 'core/pattern-overrides', - array( - 'label' => self::SOURCE_LABEL, - 'get_value_callback' => $get_value_callback, - ) - ); + $expected_content = 'This is the content value'; $block_content = << +

This should not appear

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

The attribute name is 'content'

", + "

$expected_content

", trim( $result ), 'The `__default` attribute should be replaced with the real attribute prior to the callback.' ); From 4d5e601aee80517b9a055061123ccd7f110a9726 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Mon, 3 Jun 2024 10:43:12 +0800 Subject: [PATCH 5/8] Fix test description --- tests/phpunit/tests/block-bindings/render.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/block-bindings/render.php b/tests/phpunit/tests/block-bindings/render.php index acdeec98bc9d0..087abb1bdf30c 100644 --- a/tests/phpunit/tests/block-bindings/render.php +++ b/tests/phpunit/tests/block-bindings/render.php @@ -236,17 +236,18 @@ public function test_source_value_with_unsafe_html_is_sanitized() { } /** - * Tests if the block content is sanitized when unsafe HTML is passed. + * Tests if the `__default` attribute is replaced with real attribues for + * pattern overrides. * * @ticket 61333 * - * @covers ::register_block_bindings_source + * @covers WP_Block::process_block_bindings */ public function test_default_binding_for_pattern_overrides() { $expected_content = 'This is the content value'; $block_content = << +

This should not appear

HTML; From ef2f760a828624b6a4a2d51c7060c1323053526a Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Mon, 3 Jun 2024 10:44:05 +0800 Subject: [PATCH 6/8] Fix incorrectly reinitializing array during loop --- src/wp-includes/class-wp-block.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index 299b6c72f4728..cb5a6b28ab754 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -268,11 +268,12 @@ private function process_block_bindings() { isset( $bindings['__default']['source'] ) && 'core/pattern-overrides' === $bindings['__default']['source'] ) { + $updated_bindings = array(); + // Build an 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 ) { - $updated_bindings = array(); // Retain any non-pattern override bindings that might be present. $updated_bindings[ $attribute_name ] = isset( $bindings[ $attribute_name ] ) ? $bindings[ $attribute_name ] From 45b91d65ca1fd360fe32c6387a1230173fda38fc Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Mon, 3 Jun 2024 10:46:09 +0800 Subject: [PATCH 7/8] Use multiline comments --- src/wp-includes/class-wp-block.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index cb5a6b28ab754..e4503c148c230 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -262,17 +262,21 @@ private function process_block_bindings() { $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 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 an binding array of all supported attributes. - // Note that this also omits the `__default` attribute from the - // resulting array. + /* + * Build an 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 ] ) From cbc99b5aaaa8a2cdc974fba8591c841f4d05008f Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Mon, 3 Jun 2024 13:52:40 +0800 Subject: [PATCH 8/8] Fix docblock typo --- src/wp-includes/class-wp-block.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index e4503c148c230..cd0128f1f8183 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -273,7 +273,7 @@ private function process_block_bindings() { $updated_bindings = array(); /* - * Build an binding array of all supported attributes. + * Build a binding array of all supported attributes. * Note that this also omits the `__default` attribute from the * resulting array. */