From 335ae000c00784fdfddc083845ef91531e9c5864 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 5 Aug 2025 16:53:37 +0200 Subject: [PATCH 01/10] Block Bindings: Allow filtering supported block attributes --- src/wp-includes/class-wp-block.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index 085931beb42e7..8f934850c58de 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -288,6 +288,19 @@ private function process_block_bindings() { 'core/post-date' => array( 'datetime' ), ); + /** + * Filters the supported block attributes for block bindings. + * + * @since 6.9.0 + * + * @param array $supported_block_attributes The supported block attributes for block bindings. + * The keys are block names and the values are arrays of attribute names. + */ + $supported_block_attributes = apply_filters( + 'block_bindings_supported_block_attributes', + $supported_block_attributes + ); + // If the block doesn't have the bindings property, isn't one of the supported // block types, or the bindings property is not an array, return the block content. if ( From 859f579fccffbf16ab610360653ebb3d60f2208f Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 6 Aug 2025 11:11:02 +0200 Subject: [PATCH 02/10] Add block name as an argument --- src/wp-includes/class-wp-block.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index 8f934850c58de..1f72ea36b0bdc 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -280,7 +280,8 @@ public function __get( $name ) { private function process_block_bindings() { $parsed_block = $this->parsed_block; $computed_attributes = array(); - $supported_block_attributes = array( + + $all_supported_block_attributes = array( 'core/paragraph' => array( 'content' ), 'core/heading' => array( 'content' ), 'core/image' => array( 'id', 'url', 'title', 'alt' ), @@ -288,23 +289,26 @@ private function process_block_bindings() { 'core/post-date' => array( 'datetime' ), ); + $supported_block_attributes = $all_supported_block_attributes[ $this->name ] ?? array(); + /** * Filters the supported block attributes for block bindings. * * @since 6.9.0 * - * @param array $supported_block_attributes The supported block attributes for block bindings. - * The keys are block names and the values are arrays of attribute names. + * @param string[] $supported_block_attributes The block's attributes that are supported by block bindings. + * @param string $block_name The name of the block whose attributes are being filtered. */ $supported_block_attributes = apply_filters( 'block_bindings_supported_block_attributes', - $supported_block_attributes + $supported_block_attributes, + $this->name ); // If the block doesn't have the bindings property, isn't one of the supported // block types, or the bindings property is not an array, return the block content. if ( - ! isset( $supported_block_attributes[ $this->name ] ) || + empty( $supported_block_attributes ) || empty( $parsed_block['attrs']['metadata']['bindings'] ) || ! is_array( $parsed_block['attrs']['metadata']['bindings'] ) ) { @@ -328,7 +332,7 @@ private function process_block_bindings() { * Note that this also omits the `__default` attribute from the * resulting array. */ - foreach ( $supported_block_attributes[ $parsed_block['blockName'] ] as $attribute_name ) { + foreach ( $supported_block_attributes as $attribute_name ) { // Retain any non-pattern override bindings that might be present. $updated_bindings[ $attribute_name ] = isset( $bindings[ $attribute_name ] ) ? $bindings[ $attribute_name ] @@ -347,7 +351,7 @@ private function process_block_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 ) ) { + if ( ! in_array( $attribute_name, $supported_block_attributes, true ) ) { continue; } // If no source is provided, or that source is not registered, process next attribute. From 943cb064da179760b0bf2f4df1fde3246d02a4be Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 6 Aug 2025 11:15:59 +0200 Subject: [PATCH 03/10] Include block type name in the filter name --- src/wp-includes/class-wp-block.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index 1f72ea36b0bdc..0c5252b0cac80 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -278,6 +278,7 @@ public function __get( $name ) { * @return array The computed block attributes for the provided block bindings. */ private function process_block_bindings() { + $block_type = $this->name; $parsed_block = $this->parsed_block; $computed_attributes = array(); @@ -289,20 +290,21 @@ private function process_block_bindings() { 'core/post-date' => array( 'datetime' ), ); - $supported_block_attributes = $all_supported_block_attributes[ $this->name ] ?? array(); + $supported_block_attributes = $all_supported_block_attributes[ $block_type ] ?? array(); /** * Filters the supported block attributes for block bindings. * + * The dynamic portion of the hook name, `$block_type`, refers to the block type + * whose attributes are being filtered. + * * @since 6.9.0 * * @param string[] $supported_block_attributes The block's attributes that are supported by block bindings. - * @param string $block_name The name of the block whose attributes are being filtered. */ $supported_block_attributes = apply_filters( - 'block_bindings_supported_block_attributes', - $supported_block_attributes, - $this->name + "block_bindings_supported_attributes_{$block_type}", + $supported_block_attributes ); // If the block doesn't have the bindings property, isn't one of the supported From ac90799b51612a980a37ef6ec1e5bf9356c38a0c Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 6 Aug 2025 11:30:57 +0200 Subject: [PATCH 04/10] Turn into class variable --- src/wp-includes/class-wp-block.php | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index 0c5252b0cac80..3c854f2cf4f93 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -98,6 +98,22 @@ class WP_Block { */ public $inner_content = array(); + /** + * List of supported block attributes for block bindings. + * + * @since 6.9.0 + * @var array + * + * @see WP_Block::process_block_bindings() + */ + private const BLOCK_BINDINGS_SUPPORTED_ATTRIBUTES = array( + 'core/paragraph' => array( 'content' ), + 'core/heading' => array( 'content' ), + 'core/image' => array( 'id', 'url', 'title', 'alt' ), + 'core/button' => array( 'url', 'text', 'linkTarget', 'rel' ), + 'core/post-date' => array( 'datetime' ), + ); + /** * Constructor. * @@ -282,15 +298,9 @@ private function process_block_bindings() { $parsed_block = $this->parsed_block; $computed_attributes = array(); - $all_supported_block_attributes = array( - 'core/paragraph' => array( 'content' ), - 'core/heading' => array( 'content' ), - 'core/image' => array( 'id', 'url', 'title', 'alt' ), - 'core/button' => array( 'url', 'text', 'linkTarget', 'rel' ), - 'core/post-date' => array( 'datetime' ), - ); - - $supported_block_attributes = $all_supported_block_attributes[ $block_type ] ?? array(); + $supported_block_attributes = + self::BLOCK_BINDINGS_SUPPORTED_ATTRIBUTES[ $block_type ] ?? + array(); /** * Filters the supported block attributes for block bindings. From 9be42e67f6bdd280b660c6e664cd0b87a800f80a Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 6 Aug 2025 11:55:12 +0200 Subject: [PATCH 05/10] Add test coverage --- tests/phpunit/tests/block-bindings/render.php | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/phpunit/tests/block-bindings/render.php b/tests/phpunit/tests/block-bindings/render.php index 19ee2c412d32f..90a336b498f04 100644 --- a/tests/phpunit/tests/block-bindings/render.php +++ b/tests/phpunit/tests/block-bindings/render.php @@ -72,6 +72,64 @@ public function test_update_block_with_value_from_source() { ); } + /** + * Test if the block_bindings_supported_attributes_{$block_type} filter is applied correctly. + * + * @ticket 62090 + */ + public function test_filter_block_bindings_supported_attributes() { + $get_value_callback = function () { + return 'test source value'; + }; + + register_block_bindings_source( + self::SOURCE_NAME, + array( + 'label' => self::SOURCE_LABEL, + 'get_value_callback' => $get_value_callback, + ) + ); + + register_block_type( + 'test/block', + array( + 'attributes' => array( + 'myAttribute' => array( + 'type' => 'string', + ), + ), + 'render_callback' => function ( $attributes ) { + return '

' . esc_html( $attributes['myAttribute'] ) . '

'; + }, + ) + ); + + add_filter( 'block_bindings_supported_attributes_test/block', function ( $supported_attributes ) { + $supported_attributes[] = 'myAttribute'; + return $supported_attributes; + } ); + + $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( + 'test source value', + $block->attributes['myAttribute'], + "The 'content' attribute should be updated with the value returned by the source." + ); + $this->assertSame( + '

test source value

', + trim( $result ), + 'The block content should be updated with the value returned by the source.' + ); + } + /** * Test passing arguments to the source. * From 493e94b3953ec011e4595a8ba9e94ca0f88bab9a Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 6 Aug 2025 11:58:22 +0200 Subject: [PATCH 06/10] WPCS --- tests/phpunit/tests/block-bindings/render.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/block-bindings/render.php b/tests/phpunit/tests/block-bindings/render.php index 90a336b498f04..bc6c07fd585c1 100644 --- a/tests/phpunit/tests/block-bindings/render.php +++ b/tests/phpunit/tests/block-bindings/render.php @@ -104,10 +104,13 @@ public function test_filter_block_bindings_supported_attributes() { ) ); - add_filter( 'block_bindings_supported_attributes_test/block', function ( $supported_attributes ) { - $supported_attributes[] = 'myAttribute'; - return $supported_attributes; - } ); + add_filter( + 'block_bindings_supported_attributes_test/block', + function ( $supported_attributes ) { + $supported_attributes[] = 'myAttribute'; + return $supported_attributes; + } + ); $block_content = << From 53bcc4eda429d2ed237ce694dd38f2cda12b1466 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 6 Aug 2025 12:02:08 +0200 Subject: [PATCH 07/10] WPCS --- tests/phpunit/tests/block-bindings/render.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/block-bindings/render.php b/tests/phpunit/tests/block-bindings/render.php index bc6c07fd585c1..857bcd44215e1 100644 --- a/tests/phpunit/tests/block-bindings/render.php +++ b/tests/phpunit/tests/block-bindings/render.php @@ -93,7 +93,7 @@ public function test_filter_block_bindings_supported_attributes() { register_block_type( 'test/block', array( - 'attributes' => array( + 'attributes' => array( 'myAttribute' => array( 'type' => 'string', ), From 04bbc04ea88b4fa3eac740fc3e05057f36277eb0 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 6 Aug 2025 12:19:41 +0200 Subject: [PATCH 08/10] Unregister test block after tests --- tests/phpunit/tests/block-bindings/render.php | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/tests/phpunit/tests/block-bindings/render.php b/tests/phpunit/tests/block-bindings/render.php index 857bcd44215e1..1d5f49a26a734 100644 --- a/tests/phpunit/tests/block-bindings/render.php +++ b/tests/phpunit/tests/block-bindings/render.php @@ -16,6 +16,27 @@ class WP_Block_Bindings_Render extends WP_UnitTestCase { 'label' => 'Test source', ); + /** + * Sets up shared fixtures. + * + * @since 6.9.0 + */ + public static function wpSetUpBeforeClass() { + register_block_type( + 'test/block', + array( + 'attributes' => array( + 'myAttribute' => array( + 'type' => 'string', + ), + ), + 'render_callback' => function ( $attributes ) { + return '

' . esc_html( $attributes['myAttribute'] ) . '

'; + }, + ) + ); + } + /** * Tear down after each test. * @@ -31,6 +52,15 @@ public function tear_down() { parent::tear_down(); } + /** + * Tear down after class. + * + * @since 6.9.0 + */ + public static function wpTearDownAfterClass() { + unregister_block_type( 'test/block' ); + } + /** * Test if the block content is updated with the value returned by the source. * @@ -90,20 +120,6 @@ public function test_filter_block_bindings_supported_attributes() { ) ); - register_block_type( - 'test/block', - array( - 'attributes' => array( - 'myAttribute' => array( - 'type' => 'string', - ), - ), - 'render_callback' => function ( $attributes ) { - return '

' . esc_html( $attributes['myAttribute'] ) . '

'; - }, - ) - ); - add_filter( 'block_bindings_supported_attributes_test/block', function ( $supported_attributes ) { From dd1eaf0f8f6ea9a5eb5842a1909494fdcb49a556 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 6 Aug 2025 12:21:40 +0200 Subject: [PATCH 09/10] WPCS --- tests/phpunit/tests/block-bindings/render.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/block-bindings/render.php b/tests/phpunit/tests/block-bindings/render.php index 1d5f49a26a734..6b091a311ee84 100644 --- a/tests/phpunit/tests/block-bindings/render.php +++ b/tests/phpunit/tests/block-bindings/render.php @@ -18,7 +18,7 @@ class WP_Block_Bindings_Render extends WP_UnitTestCase { /** * Sets up shared fixtures. - * + * * @since 6.9.0 */ public static function wpSetUpBeforeClass() { From 5860fe90b222728d01c4e8f4f37a8b1e5d1341d7 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 6 Aug 2025 12:24:22 +0200 Subject: [PATCH 10/10] Fix assertion error msg --- tests/phpunit/tests/block-bindings/render.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/block-bindings/render.php b/tests/phpunit/tests/block-bindings/render.php index 6b091a311ee84..007aeace1f2bf 100644 --- a/tests/phpunit/tests/block-bindings/render.php +++ b/tests/phpunit/tests/block-bindings/render.php @@ -140,7 +140,7 @@ function ( $supported_attributes ) { $this->assertSame( 'test source value', $block->attributes['myAttribute'], - "The 'content' attribute should be updated with the value returned by the source." + "The 'myAttribute' attribute should be updated with the value returned by the source." ); $this->assertSame( '

test source value

',