<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[59662] trunk: Editor: Improve consistency of `render_block_context` filter.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/59662">59662</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/59662","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>joemcgill</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2025-01-17 21:35:50 +0000 (Fri, 17 Jan 2025)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/62046">#62046</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpblockphp">trunk/src/wp-includes/class-wp-block.php</a></li>
<li><a href="#trunktestsphpunittestsblocksrenderBlockphp">trunk/tests/phpunit/tests/blocks/renderBlock.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpblockphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-block.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-block.php  2025-01-17 14:01:37 UTC (rev 59661)
+++ trunk/src/wp-includes/class-wp-block.php    2025-01-17 21:35:50 UTC (rev 59662)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -56,7 +56,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @var array
</span><span class="cx" style="display: block; padding: 0 10px">         * @access protected
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        protected $available_context;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected $available_context = array();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Block type registry.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -140,6 +140,28 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->available_context = $available_context;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $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 );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( ! empty( $this->block_type->uses_context ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        foreach ( $this->block_type->uses_context as $context_name ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( array_key_exists( $context_name, $this->available_context ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -148,7 +170,23 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! empty( $block['innerBlocks'] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $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'] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         $child_context = $this->available_context;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( ! empty( $this->block_type->provides_context ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -159,15 +197,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->inner_blocks = new WP_Block_List( $block['innerBlocks'], $child_context, $registry );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $this->inner_blocks = new WP_Block_List( $this->parsed_block['innerBlocks'], $child_context, $this->registry );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! empty( $block['innerHTML'] ) ) {
-                       $this->inner_html = $block['innerHTML'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( ! empty( $this->parsed_block['innerHTML'] ) ) {
+                       $this->inner_html = $this->parsed_block['innerHTML'];
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! empty( $block['innerContent'] ) ) {
-                       $this->inner_content = $block['innerContent'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( ! empty( $this->parsed_block['innerContent'] ) ) {
+                       $this->inner_content = $this->parsed_block['innerContent'];
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -506,7 +544,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        if ( ! is_null( $pre_render ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                                $block_content .= $pre_render;
</span><span class="cx" style="display: block; padding: 0 10px">                                        } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                $source_block = $inner_block->parsed_block;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                         $source_block        = $inner_block->parsed_block;
+                                               $inner_block_context = $inner_block->context;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                                /** This filter is documented in wp-includes/blocks.php */
</span><span class="cx" style="display: block; padding: 0 10px">                                                $inner_block->parsed_block = apply_filters( 'render_block_data', $inner_block->parsed_block, $source_block, $parent_block );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -514,6 +553,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                                /** This filter is documented in wp-includes/blocks.php */
</span><span class="cx" style="display: block; padding: 0 10px">                                                $inner_block->context = apply_filters( 'render_block_context', $inner_block->context, $inner_block->parsed_block, $parent_block );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                                /*
+                                                * 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();
+                                               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                                                 $block_content .= $inner_block->render();
</span><span class="cx" style="display: block; padding: 0 10px">                                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunktestsphpunittestsblocksrenderBlockphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/blocks/renderBlock.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/blocks/renderBlock.php  2025-01-17 14:01:37 UTC (rev 59661)
+++ trunk/tests/phpunit/tests/blocks/renderBlock.php    2025-01-17 21:35:50 UTC (rev 59662)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -192,4 +192,132 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( array( 'example' => 'ok' ), $provided_context[0] );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * 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"' );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>