From 805b9333f2de80c3db981e1a2a5755045d9e7782 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=83=C2=B3=C3=85=E2=80=9Akowski?= Date: Mon, 23 Sep 2024 12:33:14 +0000 Subject: [PATCH] Block Bindings: Adds context needed by sources during its processing Extends block context during block bindings processing. This implies that the context is extended ONLY for the blocks where bindings are defined and only when rendered on the page. Props santosguillamot, gziolo, artemiosans, cbravobernal. Fixes #61642. git-svn-id: https://develop.svn.wordpress.org/trunk@59080 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-block-bindings-registry.php | 14 ----- src/wp-includes/class-wp-block.php | 9 +++ tests/phpunit/tests/block-bindings/render.php | 62 +++++++++++++++++++ .../wpBlockBindingsRegistry.php | 47 -------------- 4 files changed, 71 insertions(+), 61 deletions(-) diff --git a/src/wp-includes/class-wp-block-bindings-registry.php b/src/wp-includes/class-wp-block-bindings-registry.php index 14c1b89229..dc065356c5 100644 --- a/src/wp-includes/class-wp-block-bindings-registry.php +++ b/src/wp-includes/class-wp-block-bindings-registry.php @@ -189,20 +189,6 @@ final class WP_Block_Bindings_Registry { $this->sources[ $source_name ] = $source; - // Adds `uses_context` defined by block bindings sources. - add_filter( - 'get_block_type_uses_context', - function ( $uses_context, $block_type ) use ( $source ) { - if ( ! in_array( $block_type->name, $this->supported_blocks, true ) || empty( $source->uses_context ) ) { - return $uses_context; - } - // Use array_values to reset the array keys. - return array_values( array_unique( array_merge( $uses_context, $source->uses_context ) ) ); - }, - 10, - 2 - ); - return $source; } diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index f7fd53dfc9..d4481a68a7 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -301,6 +301,15 @@ class WP_Block { continue; } + // Adds the necessary context defined by the source. + if ( ! empty( $block_binding_source->uses_context ) ) { + foreach ( $block_binding_source->uses_context as $context_name ) { + if ( array_key_exists( $context_name, $this->available_context ) ) { + $this->context[ $context_name ] = $this->available_context[ $context_name ]; + } + } + } + $source_args = ! empty( $block_binding['args'] ) && is_array( $block_binding['args'] ) ? $block_binding['args'] : array(); $source_value = $block_binding_source->get_value( $source_args, $this, $attribute_name ); diff --git a/tests/phpunit/tests/block-bindings/render.php b/tests/phpunit/tests/block-bindings/render.php index 914de02c3c..6c53ddf994 100644 --- a/tests/phpunit/tests/block-bindings/render.php +++ b/tests/phpunit/tests/block-bindings/render.php @@ -157,6 +157,68 @@ HTML; ); } + /** + * Tests that blocks can only access the context from the specific source. + * + * @ticket 61642 + * + * @covers ::register_block_bindings_source + */ + public function test_blocks_can_just_access_the_specific_uses_context() { + register_block_bindings_source( + 'test/source-one', + array( + 'label' => 'Test Source One', + 'get_value_callback' => function () { + return; + }, + 'uses_context' => array( 'contextOne' ), + ) + ); + + register_block_bindings_source( + 'test/source-two', + array( + 'label' => 'Test Source Two', + 'get_value_callback' => function ( $source_args, $block_instance, $attribute_name ) { + $value = $block_instance->context['contextTwo']; + // Try to use the context from source one, which shouldn't be available. + if ( ! empty( $block_instance->context['contextOne'] ) ) { + $value = $block_instance->context['contextOne']; + } + return "Value: $value"; + }, + 'uses_context' => array( 'contextTwo' ), + ) + ); + + $block_content = << +

Default content

+ +HTML; + $parsed_blocks = parse_blocks( $block_content ); + $block = new WP_Block( + $parsed_blocks[0], + array( + 'contextOne' => 'source one context value', + 'contextTwo' => 'source two context value', + ) + ); + $result = $block->render(); + + $this->assertSame( + 'Value: source two context value', + $block->attributes['content'], + "The 'content' should be updated with the value of the second source context value." + ); + $this->assertSame( + '

Value: source two context value

', + trim( $result ), + 'The block content should be updated with the value of the source context.' + ); + } + /** * Tests if the block content is updated with the value returned by the source * for the Image block in the placeholder state. diff --git a/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php b/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php index e4aa415e9a..9beebaa2c3 100644 --- a/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php +++ b/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php @@ -344,51 +344,4 @@ class Tests_Blocks_wpBlockBindingsRegistry extends WP_UnitTestCase { $result = $this->registry->is_registered( self::$test_source_name ); $this->assertTrue( $result ); } - - /** - * Tests merging `uses_context` from multiple sources. - * - * @ticket 60525 - * - * @covers ::register_block_bindings_source - * @covers WP_Block_Type::get_uses_context - */ - public function test_merging_uses_context_from_multiple_sources() { - $get_value_callback = function () { - return 'Anything'; - }; - - $block_registry = WP_Block_Type_Registry::get_instance(); - $original_uses_context = $block_registry->get_registered( 'core/paragraph' )->uses_context; - - register_block_bindings_source( - 'test/source-one', - array( - 'label' => 'Test Source One', - 'get_value_callback' => $get_value_callback, - 'uses_context' => array( 'commonContext', 'sourceOneContext' ), - ) - ); - - register_block_bindings_source( - 'test/source-two', - array( - 'label' => 'Test Source Two', - 'get_value_callback' => $get_value_callback, - 'uses_context' => array( 'commonContext', 'sourceTwoContext' ), - ) - ); - - $new_uses_context = $block_registry->get_registered( 'core/paragraph' )->uses_context; - unregister_block_bindings_source( 'test/source-one' ); - unregister_block_bindings_source( 'test/source-two' ); - // Checks that the resulting `uses_context` contains the values from both sources. - $this->assertContains( 'commonContext', $new_uses_context ); - $this->assertContains( 'sourceOneContext', $new_uses_context ); - $this->assertContains( 'sourceTwoContext', $new_uses_context ); - // Checks that the resulting `uses_context` added 3 unique items. - $this->assertSame( count( $original_uses_context ) + 3, count( $new_uses_context ) ); - // Checks that the array isn't sparse to prevent issues in the editor. - $this->assertSame( array_key_last( $new_uses_context ), count( $new_uses_context ) - 1 ); - } }