Commit 89adfd4acc for wordpress.org
commit 89adfd4acc245b67dae14d4e45f3a9de1516c620
Author: Joe McGill <joemcgill@gmail.com>
Date: Fri Jan 17 21:37:21 2025 +0000
Editor: Improve consistency of `render_block_context` filter.
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.
Built from https://develop.svn.wordpress.org/trunk@59662
git-svn-id: http://core.svn.wordpress.org/trunk@59005 1a063a9b-81f0-0310-95a4-ce76da25c4cd
diff --git a/wp-includes/class-wp-block.php b/wp-includes/class-wp-block.php
index a1c52019c2..2533379fcd 100644
--- a/wp-includes/class-wp-block.php
+++ b/wp-includes/class-wp-block.php
@@ -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();
}
diff --git a/wp-includes/version.php b/wp-includes/version.php
index 5bccdd67a7..4f4ea25d29 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
-$wp_version = '6.8-alpha-59661';
+$wp_version = '6.8-alpha-59662';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.