diff --git a/src/wp-includes/class-wp-block-bindings-registry.php b/src/wp-includes/class-wp-block-bindings-registry.php index 14c1b89229c30..dc065356c5ce4 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 @@ public function register( string $source_name, array $source_properties ) { $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 f7fd53dfc9710..d4481a68a7bde 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -301,6 +301,15 @@ private function process_block_bindings() { 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 914de02c3c09f..6c53ddf99411e 100644 --- a/tests/phpunit/tests/block-bindings/render.php +++ b/tests/phpunit/tests/block-bindings/render.php @@ -157,6 +157,68 @@ public function test_passing_uses_context_to_source() { ); } + /** + * 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 e4aa415e9af96..9beebaa2c3108 100644 --- a/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php +++ b/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php @@ -344,51 +344,4 @@ public function test_is_registered_for_known_source() { $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 ); - } }