From f9bfaef59daafe98c3ba8ebd1780906561173515 Mon Sep 17 00:00:00 2001 From: Max Schmeling Date: Fri, 20 Sep 2024 11:08:56 -0700 Subject: [PATCH 1/5] Add filter and test --- src/wp-includes/class-wp-block.php | 12 +++++ tests/phpunit/tests/block-bindings/render.php | 44 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index f7fd53dfc9710..885bede3aa308 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -250,6 +250,18 @@ private function process_block_bindings() { 'core/button' => array( 'url', 'text', 'linkTarget', 'rel' ), ); + /** + * Filters the supported block attributes for block bindings. + * + * @since 6.6.0 + * + * @param array $supported_block_attributes The default supported block attributes. + */ + $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 ( diff --git a/tests/phpunit/tests/block-bindings/render.php b/tests/phpunit/tests/block-bindings/render.php index 914de02c3c09f..78495d8b24cb3 100644 --- a/tests/phpunit/tests/block-bindings/render.php +++ b/tests/phpunit/tests/block-bindings/render.php @@ -299,6 +299,50 @@ public function test_default_binding_for_pattern_overrides() { ); } + /** + * Tests that filter `block_bindings_supported_block_attributes` is applied. + * + * @ticket 61181 + */ + public function test_filter_block_bindings_supported_block_attributes() { + $filter_supported_attributes = function( $supported_block_attributes ) { + $supported_block_attributes['core/custom-block'] = array( 'custom_attribute' ); + return $supported_block_attributes; + }; + + add_filter( 'block_bindings_supported_block_attributes', $filter_supported_attributes ); + + $get_value_callback = function() { + return 'Custom value'; + }; + + register_block_bindings_source( + self::SOURCE_NAME, + array( + 'label' => self::SOURCE_LABEL, + 'get_value_callback' => $get_value_callback, + ) + ); + + $block_content = << +
Default content
+ +HTML; + + $parsed_blocks = parse_blocks( $block_content ); + $block = new WP_Block( $parsed_blocks[0] ); + $result = $block->render(); + + remove_filter( 'block_bindings_supported_block_attributes', $filter_supported_attributes ); + + $this->assertSame( + '
Custom value
', + trim( $result ), + 'The block content should show the custom attribute value after applying the filter.' + ); + } + /** * Tests that filter `block_bindings_source_value` is applied. * From ffdf3372a0f082c3e9ce6931c7e05096b6848e0c Mon Sep 17 00:00:00 2001 From: Max Schmeling Date: Fri, 20 Sep 2024 11:14:32 -0700 Subject: [PATCH 2/5] Fix formatting issues and comments --- src/wp-includes/class-wp-block.php | 5 ++++- tests/phpunit/tests/block-bindings/render.php | 6 +++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index 885bede3aa308..b2268b5300717 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -237,6 +237,7 @@ public function __get( $name ) { * * @since 6.5.0 * @since 6.6.0 Handle the `__default` attribute for pattern overrides. + * @since 6.7.0 Add filter for supported block attributes for block bindings. * * @return array The computed block attributes for the provided block bindings. */ @@ -253,9 +254,11 @@ private function process_block_bindings() { /** * Filters the supported block attributes for block bindings. * - * @since 6.6.0 + * @since 6.7.0 * * @param array $supported_block_attributes The default supported block attributes. + * + * @return array The updated supported block attributes. */ $supported_block_attributes = apply_filters( 'block_bindings_supported_block_attributes', diff --git a/tests/phpunit/tests/block-bindings/render.php b/tests/phpunit/tests/block-bindings/render.php index 78495d8b24cb3..31dd79cfa5c37 100644 --- a/tests/phpunit/tests/block-bindings/render.php +++ b/tests/phpunit/tests/block-bindings/render.php @@ -302,17 +302,17 @@ public function test_default_binding_for_pattern_overrides() { /** * Tests that filter `block_bindings_supported_block_attributes` is applied. * - * @ticket 61181 + * @ticket 62090 */ public function test_filter_block_bindings_supported_block_attributes() { - $filter_supported_attributes = function( $supported_block_attributes ) { + $filter_supported_attributes = function ( $supported_block_attributes ) { $supported_block_attributes['core/custom-block'] = array( 'custom_attribute' ); return $supported_block_attributes; }; add_filter( 'block_bindings_supported_block_attributes', $filter_supported_attributes ); - $get_value_callback = function() { + $get_value_callback = function () { return 'Custom value'; }; From 2895bb45a1391b71d95355c103f43ca8c9adfd22 Mon Sep 17 00:00:00 2001 From: Carlos Bravo <37012961+cbravobernal@users.noreply.github.com> Date: Fri, 20 Sep 2024 13:35:20 -0700 Subject: [PATCH 3/5] Update unit test --- tests/phpunit/tests/block-bindings/render.php | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/tests/phpunit/tests/block-bindings/render.php b/tests/phpunit/tests/block-bindings/render.php index 31dd79cfa5c37..ca8d299b1394d 100644 --- a/tests/phpunit/tests/block-bindings/render.php +++ b/tests/phpunit/tests/block-bindings/render.php @@ -305,15 +305,31 @@ public function test_default_binding_for_pattern_overrides() { * @ticket 62090 */ public function test_filter_block_bindings_supported_block_attributes() { + register_block_type( + 'plugin/custom-block', + array( + 'attributes' => array( + 'content' => array( + 'type' => 'string', + 'default' => '', + ), + ), + 'render_callback' => function ( $attributes, $content ) { + return '

' . ( $attributes['content'] ?? 'Default content' ) . '

'; + }, + ) + ); + $filter_supported_attributes = function ( $supported_block_attributes ) { - $supported_block_attributes['core/custom-block'] = array( 'custom_attribute' ); + $supported_block_attributes['plugin/custom-block'] = array( 'content' ); return $supported_block_attributes; }; add_filter( 'block_bindings_supported_block_attributes', $filter_supported_attributes ); - $get_value_callback = function () { - return 'Custom value'; + $get_value_callback = function ( $source_args ) { + $key = $source_args['key']; + return "Source value: $key"; }; register_block_bindings_source( @@ -325,9 +341,9 @@ public function test_filter_block_bindings_supported_block_attributes() { ); $block_content = << -
Default content
- + +

Default content

+ HTML; $parsed_blocks = parse_blocks( $block_content ); @@ -337,7 +353,7 @@ public function test_filter_block_bindings_supported_block_attributes() { remove_filter( 'block_bindings_supported_block_attributes', $filter_supported_attributes ); $this->assertSame( - '
Custom value
', + '

Source value: custom_attribute

', trim( $result ), 'The block content should show the custom attribute value after applying the filter.' ); From 59332ddb31fe92a27fc8dae032450d7c77b9e75d Mon Sep 17 00:00:00 2001 From: Max Schmeling Date: Mon, 23 Sep 2024 00:21:50 -0500 Subject: [PATCH 4/5] Add test for custom attribute binding --- tests/phpunit/tests/block-bindings/render.php | 59 ++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/block-bindings/render.php b/tests/phpunit/tests/block-bindings/render.php index ca8d299b1394d..968bcf5fb6671 100644 --- a/tests/phpunit/tests/block-bindings/render.php +++ b/tests/phpunit/tests/block-bindings/render.php @@ -304,7 +304,7 @@ public function test_default_binding_for_pattern_overrides() { * * @ticket 62090 */ - public function test_filter_block_bindings_supported_block_attributes() { + public function test_filter_block_bindings_supported_block_attributes_custom_block_content_attribute() { register_block_type( 'plugin/custom-block', array( @@ -359,6 +359,63 @@ public function test_filter_block_bindings_supported_block_attributes() { ); } + /** + * Tests that the 'block_bindings_supported_block_attributes' filter works for custom blocks and attributes. + * + * @ticket 61181 + */ + public function test_filter_block_bindings_supported_block_attributes_custom_block_custom_attribute() { + register_block_type( + 'plugin/custom-block', + array( + 'attributes' => array( + 'customAttribute' => array( + 'type' => 'string', + 'default' => '', + ), + ), + 'render_callback' => function ( $attributes ) { + return '
static content
'; + }, + ) + ); + + $filter_supported_attributes = function ( $supported_block_attributes ) { + $supported_block_attributes['plugin/custom-block'] = array( 'customAttribute' ); + return $supported_block_attributes; + }; + + add_filter( 'block_bindings_supported_block_attributes', $filter_supported_attributes ); + + register_block_bindings_source( + self::SOURCE_NAME, + array( + 'label' => self::SOURCE_LABEL, + 'get_value_callback' => function ( $source_args ) { + return "Bound value: {$source_args['key']}"; + }, + ) + ); + + $block_content = << +
Default value
+ +HTML; + + $parsed_blocks = parse_blocks( $block_content ); + $block = new WP_Block( $parsed_blocks[0] ); + $result = $block->render(); + + remove_filter( 'block_bindings_supported_block_attributes', $filter_supported_attributes ); + + $this->assertSame( + '
static content
', + trim( $result ), + 'The block content should show the bound value for the custom attribute after applying the filter.' + ); + } + /** * Tests that filter `block_bindings_source_value` is applied. * From ee83e4596bda775f69c581f439a89efc6bd7e303 Mon Sep 17 00:00:00 2001 From: Max Schmeling Date: Mon, 23 Sep 2024 01:37:47 -0500 Subject: [PATCH 5/5] Use unique name for custom block --- tests/phpunit/tests/block-bindings/render.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/tests/block-bindings/render.php b/tests/phpunit/tests/block-bindings/render.php index 968bcf5fb6671..e70b8800b373c 100644 --- a/tests/phpunit/tests/block-bindings/render.php +++ b/tests/phpunit/tests/block-bindings/render.php @@ -366,7 +366,7 @@ public function test_filter_block_bindings_supported_block_attributes_custom_blo */ public function test_filter_block_bindings_supported_block_attributes_custom_block_custom_attribute() { register_block_type( - 'plugin/custom-block', + 'plugin/custom-block-with-custom-attr', array( 'attributes' => array( 'customAttribute' => array( @@ -381,7 +381,7 @@ public function test_filter_block_bindings_supported_block_attributes_custom_blo ); $filter_supported_attributes = function ( $supported_block_attributes ) { - $supported_block_attributes['plugin/custom-block'] = array( 'customAttribute' ); + $supported_block_attributes['plugin/custom-block-with-custom-attr'] = array( 'customAttribute' ); return $supported_block_attributes; }; @@ -398,9 +398,8 @@ public function test_filter_block_bindings_supported_block_attributes_custom_blo ); $block_content = << -
Default value
- + + HTML; $parsed_blocks = parse_blocks( $block_content );