From fd0ccaa1c0970c0689a8d41d37099e54f2a8df10 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Thu, 19 Sep 2024 17:28:17 +0530 Subject: [PATCH 1/2] Update renderBlock.php --- tests/phpunit/tests/blocks/renderBlock.php | 66 ++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/tests/phpunit/tests/blocks/renderBlock.php b/tests/phpunit/tests/blocks/renderBlock.php index 0c1c4ce10a1ee..6e2923a429bb2 100644 --- a/tests/phpunit/tests/blocks/renderBlock.php +++ b/tests/phpunit/tests/blocks/renderBlock.php @@ -192,4 +192,70 @@ public function test_default_context_is_filterable() { $this->assertSame( array( 'example' => 'ok' ), $provided_context[0] ); } + + /** + * Tests the behavior of the 'render_block_context' filter based on the location of the filtered block. + * + * @ticket 62046 + */ + public function test_render_block_context_inner_blocks() { + $provided_context = array(); + + register_block_type( + 'tests/context-provider', + array( + 'provides_context' => array( 'example' ), + ) + ); + + register_block_type( + 'tests/context-consumer', + array( + 'uses_context' => array( 'example' ), + 'render_callback' => static function ( $attributes, $content, $block ) use ( &$provided_context ) { + $provided_context = $block->context; + + return ''; + }, + ) + ); + + // Filter the context provided by the test block. + add_filter( + 'render_block_context', + function ( $context, $parsed_block ) { + if ( isset( $parsed_block['blockName'] ) && 'tests/context-provider' === $parsed_block['blockName'] ) { + $context['example'] = 'ok'; + } + + return $context; + }, + 10, + 2 + ); + + // Test inner block context when the provider block is a top-level block. + do_blocks( + << + + +HTML + ); + $this->assertTrue( isset( $provided_context['example'] ), 'Test block is top-level block: Context should include "example"' ); + $this->assertSame( 'ok', $provided_context['example'], 'Test block is top-level block: "example" in context should be "ok"' ); + + // Test inner block context when the provider block is an inner block. + do_blocks( + << + + + + +HTML + ); + $this->assertTrue( isset( $provided_context['example'] ), 'Test block is inner block: Block context should include "example"' ); + $this->assertSame( 'ok', $provided_context['example'], 'Test block is inner block: "example" in context should be "ok"' ); + } } From e3878da2321f2f5c7d190572256effd2a72a44e4 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Thu, 19 Sep 2024 17:28:52 +0530 Subject: [PATCH 2/2] Update class-wp-block.php --- src/wp-includes/class-wp-block.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index f7fd53dfc9710..30e9a7d8b9a49 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -494,6 +494,10 @@ public function render( $options = array() ) { $inner_block->parsed_block = apply_filters( 'render_block_data', $inner_block->parsed_block, $source_block, $parent_block ); /** This filter is documented in wp-includes/blocks.php */ + if ( $parent_block->context ) { + $inner_block->context = array_merge( $parent_block->context, $inner_block->context ); + } + $inner_block->context = apply_filters( 'render_block_context', $inner_block->context, $inner_block->parsed_block, $parent_block ); $block_content .= $inner_block->render();