Editor: Improve consistency of render_block_context filter.
Some checks failed
Cleanup Pull Requests / Clean up pull requests (push) Waiting to run
Coding Standards / PHP coding standards (push) Waiting to run
Coding Standards / JavaScript coding standards (push) Waiting to run
Coding Standards / Slack Notifications (push) Blocked by required conditions
Coding Standards / Failed workflow tasks (push) Blocked by required conditions
End-to-end Tests / Test with SCRIPT_DEBUG disabled (push) Waiting to run
End-to-end Tests / Test with SCRIPT_DEBUG enabled (push) Waiting to run
End-to-end Tests / Slack Notifications (push) Blocked by required conditions
End-to-end Tests / Failed workflow tasks (push) Blocked by required conditions
JavaScript Tests / QUnit Tests (push) Waiting to run
JavaScript Tests / Slack Notifications (push) Blocked by required conditions
JavaScript Tests / Failed workflow tasks (push) Blocked by required conditions
Performance Tests / Single site (push) Waiting to run
Performance Tests / Multisite (push) Waiting to run
Performance Tests / Slack Notifications (push) Blocked by required conditions
Performance Tests / Failed workflow tasks (push) Blocked by required conditions
PHP Compatibility / Check PHP compatibility (push) Waiting to run
PHP Compatibility / Slack Notifications (push) Blocked by required conditions
PHP Compatibility / Failed workflow tasks (push) Blocked by required conditions
Test Build Processes / Core running from build (push) Waiting to run
Test Build Processes / Core running from src (push) Waiting to run
Test Build Processes / Gutenberg running from build (push) Waiting to run
Test Build Processes / Gutenberg running from src (push) Waiting to run
Test Build Processes / Slack Notifications (push) Blocked by required conditions
Test Build Processes / Failed workflow tasks (push) Blocked by required conditions
PHPUnit Tests / PHP 7.2 (push) Has been cancelled
PHPUnit Tests / PHP 7.3 (push) Has been cancelled
PHPUnit Tests / PHP 7.4 (push) Has been cancelled
PHPUnit Tests / PHP 8.0 (push) Has been cancelled
PHPUnit Tests / PHP 8.1 (push) Has been cancelled
PHPUnit Tests / PHP 8.2 (push) Has been cancelled
PHPUnit Tests / PHP 8.3 (push) Has been cancelled
PHPUnit Tests / PHP 8.4 (push) Has been cancelled
PHPUnit Tests / html-api-html5lib-tests (push) Has been cancelled
PHPUnit Tests / Slack Notifications (push) Has been cancelled
PHPUnit Tests / Failed workflow tasks (push) Has been cancelled

This ensures that when block context is filtered via `render_block_context`, the filtered value is provided as available context to inner blocks.

For backwards compatibility reasons, filtered context is added to inner block context regardless of whether that block has declared support via the `uses_context` property.

Props mukesh27, flixos90, gziolo, dlh, joemcgill, santosguillamot.
Fixes #62046.


git-svn-id: https://develop.svn.wordpress.org/trunk@59662 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Joe McGill 2025-01-17 21:35:50 +00:00
parent 5e74d3416f
commit 2f654881e4
2 changed files with 185 additions and 8 deletions

View File

@ -56,7 +56,7 @@ class WP_Block {
* @var array
* @access protected
*/
protected $available_context;
protected $available_context = array();
/**
* Block type registry.
@ -140,6 +140,28 @@ class WP_Block {
$this->available_context = $available_context;
$this->refresh_context_dependents();
}
/**
* Updates the context for the current block and its inner blocks.
*
* The method updates the context of inner blocks, if any, by passing down
* any context values the block provides (`provides_context`).
*
* If the block has inner blocks, the method recursively processes them by creating new instances of `WP_Block`
* for each inner block and updating their context based on the block's `provides_context` property.
*
* @since 6.8.0
*/
public function refresh_context_dependents() {
/*
* Merging the `$context` property here is not ideal, but for now needs to happen because of backward compatibility.
* Ideally, the `$context` property itself would not be filterable directly and only the `$available_context` would be filterable.
* However, this needs to be separately explored whether it's possible without breakage.
*/
$this->available_context = array_merge( $this->available_context, $this->context );
if ( ! empty( $this->block_type->uses_context ) ) {
foreach ( $this->block_type->uses_context as $context_name ) {
if ( array_key_exists( $context_name, $this->available_context ) ) {
@ -148,7 +170,23 @@ class WP_Block {
}
}
if ( ! empty( $block['innerBlocks'] ) ) {
$this->refresh_parsed_block_dependents();
}
/**
* Updates the parsed block content for the current block and its inner blocks.
*
* This method sets the `inner_html` and `inner_content` properties of the block based on the parsed
* block content provided during initialization. It ensures that the block instance reflects the
* most up-to-date content for both the inner HTML and any string fragments around inner blocks.
*
* If the block has inner blocks, this method initializes a new `WP_Block_List` for them, ensuring the
* correct content and context are updated for each nested block.
*
* @since 6.8.0
*/
public function refresh_parsed_block_dependents() {
if ( ! empty( $this->parsed_block['innerBlocks'] ) ) {
$child_context = $this->available_context;
if ( ! empty( $this->block_type->provides_context ) ) {
@ -159,15 +197,15 @@ class WP_Block {
}
}
$this->inner_blocks = new WP_Block_List( $block['innerBlocks'], $child_context, $registry );
$this->inner_blocks = new WP_Block_List( $this->parsed_block['innerBlocks'], $child_context, $this->registry );
}
if ( ! empty( $block['innerHTML'] ) ) {
$this->inner_html = $block['innerHTML'];
if ( ! empty( $this->parsed_block['innerHTML'] ) ) {
$this->inner_html = $this->parsed_block['innerHTML'];
}
if ( ! empty( $block['innerContent'] ) ) {
$this->inner_content = $block['innerContent'];
if ( ! empty( $this->parsed_block['innerContent'] ) ) {
$this->inner_content = $this->parsed_block['innerContent'];
}
}
@ -506,7 +544,8 @@ class WP_Block {
if ( ! is_null( $pre_render ) ) {
$block_content .= $pre_render;
} else {
$source_block = $inner_block->parsed_block;
$source_block = $inner_block->parsed_block;
$inner_block_context = $inner_block->context;
/** This filter is documented in wp-includes/blocks.php */
$inner_block->parsed_block = apply_filters( 'render_block_data', $inner_block->parsed_block, $source_block, $parent_block );
@ -514,6 +553,16 @@ class WP_Block {
/** This filter is documented in wp-includes/blocks.php */
$inner_block->context = apply_filters( 'render_block_context', $inner_block->context, $inner_block->parsed_block, $parent_block );
/*
* The `refresh_context_dependents()` method already calls `refresh_parsed_block_dependents()`.
* Therefore the second condition is irrelevant if the first one is satisfied.
*/
if ( $inner_block->context !== $inner_block_context ) {
$inner_block->refresh_context_dependents();
} elseif ( $inner_block->parsed_block !== $source_block ) {
$inner_block->refresh_parsed_block_dependents();
}
$block_content .= $inner_block->render();
}

View File

@ -192,4 +192,132 @@ class Tests_Blocks_RenderBlock extends WP_UnitTestCase {
$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
<!-- wp:tests/context-provider -->
<!-- wp:tests/context-consumer /-->
<!-- /wp:tests/context-provider -->
HTML
);
$this->assertArrayHasKey( 'example', $provided_context, '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
<!-- wp:group {"layout":{"type":"constrained"}} -->
<!-- wp:tests/context-provider -->
<!-- wp:tests/context-consumer /-->
<!-- /wp:tests/context-provider -->
<!-- /wp:group -->
HTML
);
$this->assertArrayHasKey( 'example', $provided_context, '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"' );
}
/**
* Tests that the 'render_block_context' filter arbitrary context.
*
* @ticket 62046
*/
public function test_render_block_context_allowed_context() {
$provided_context = array();
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 to the test block.
add_filter(
'render_block_context',
function ( $context, $parsed_block ) {
if ( isset( $parsed_block['blockName'] ) && 'tests/context-consumer' === $parsed_block['blockName'] ) {
$context['arbitrary'] = 'ok';
}
return $context;
},
10,
2
);
do_blocks(
<<<HTML
<!-- wp:tests/context-consumer /-->
HTML
);
$this->assertArrayNotHasKey( 'arbitrary', $provided_context, 'Test block is top-level block: Block context should not include "arbitrary"' );
do_blocks(
<<<HTML
<!-- wp:group {"layout":{"type":"constrained"}} -->
<!-- wp:tests/context-consumer /-->
<!-- /wp:group -->
HTML
);
/*
* These assertions assert something that ideally should not be the case: Inner blocks should respect the
* `uses_context` value just like top-level blocks do. However, due to logic in `WP_Block::render()`, the
* `context` property value itself is filterable when it should rather only apply to the `available_context`
* property.
* However, changing this behavior now would be a backward compatibility break, hence the assertion here.
* Potentially it can be reconsidered in the future, so that these two assertions could be replaced with an
* `assertArrayNotHasKey( 'arbitrary', $provided_context )`.
*/
$this->assertArrayHasKey( 'arbitrary', $provided_context, 'Test block is inner block: Block context should include "arbitrary"' );
$this->assertSame( 'ok', $provided_context['arbitrary'], 'Test block is inner block: "arbitrary" in context should be "ok"' );
}
}