<!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>[57039] branches/6.4/src/wp-includes/blocks.php: Block Hooks: Allow traversal callbacks to modify parent block.</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/57039">57039</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/57039","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>Bernhard Reiter</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-10-31 19:38:08 +0000 (Tue, 31 Oct 2023)</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'>Block Hooks: Allow traversal callbacks to modify parent block.
The callbacks returned by `make_before_block_visitor` and `make_after_block_visitor`, respectively, (which are passed as arguments to `traverse_and_serialize_block(s)`) currently accept three arguments, all of which are block arrays (i.e. with properties `blockName`, `attrs`, etc.):
- A ''reference'' to the block they're currently visiting, `&$block`;
- the block's `$parent_block`; and
- the `$prev`ious block (for `make_before_block_visitor`), or the `$next` block (for `make_after_block_visitor`), respectively.
Those arguments are passed to the "block visitor" callbacks by `traverse_and_serialize_block(s)` during traversal. The block that the callback is currently visiting is passed ''by reference'' to allow modifying it, which is e.g. used to inject the `theme` attribute into Template Part blocks.
One major limitation of Block Hooks is that they currently only work with templates, parts, and patterns that ''don't have any user modifications'' (i.e. that come straight from the corresponding theme files, rather than from the database). For WordPress 6.5, it is planned to change that to make Block Hooks work for templates, parts, and patterns that ''do'' have user modifications: <a href="https://core.trac.wordpress.org/ticket/59646">#59646</a>.
This will be implemented by storing an attribute on the "anchor" block. While working on that feature, it was found that the aforementioned callbacks will need to modify not only the currently visited `$block`, but also the `$parent_block` -- i.e. that the latter argument needs to be passed by reference as well. This is consistent with the requirement of adding an attribute to an anchor block, as it's not only the currently visited block that can serve as an anchor block (in the case of `before` or `after` sibling insertion), but also its parent (for `first_child` and `last_child` insertion).
If the `$parent_block` argument were to be changed to become a reference in a later WordPress version, this could be considered a backwards-compatibility breaking change. For this reason, this change is instead proposed for 6.4 already, which is the cycle during which the relevant functions were first introduced. This should have no impact on existing code, since nothing currently relies on `$parent_block` remaining unmodified by the respective callback, nor is anything currently modifying that argument.
Reviewed by hellofromTonya.
Merges <a href="https://core.trac.wordpress.org/changeset/57038">[57038]</a> to the 6.4 branch.
Props hellofromTonya.
Fixes <a href="https://core.trac.wordpress.org/ticket/59776">#59776</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#branches64srcwpincludesblocksphp">branches/6.4/src/wp-includes/blocks.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="branches64srcwpincludesblocksphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/6.4/src/wp-includes/blocks.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/6.4/src/wp-includes/blocks.php 2023-10-31 19:23:25 UTC (rev 57038)
+++ branches/6.4/src/wp-includes/blocks.php 2023-10-31 19:38:08 UTC (rev 57039)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -773,12 +773,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Furthermore, prepend the markup for any blocks hooked `before` the given block and as its parent's
</span><span class="cx" style="display: block; padding: 0 10px"> * `first_child`, respectively, to the serialized markup for the given block.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param array $block The block to inject the theme attribute into, and hooked blocks before.
- * @param array $parent_block The parent block of the given block.
- * @param array $prev The previous sibling block of the given block.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param array $block The block to inject the theme attribute into, and hooked blocks before. Passed by reference.
+ * @param array $parent_block The parent block of the given block. Passed by reference. Default null.
+ * @param array $prev The previous sibling block of the given block. Default null.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return string The serialized markup for the given block, with the markup for any hooked blocks prepended to it.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return function ( &$block, $parent_block = null, $prev = null ) use ( $hooked_blocks, $context ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return function ( &$block, &$parent_block = null, $prev = null ) use ( $hooked_blocks, $context ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> _inject_theme_attribute_in_template_part_block( $block );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $markup = '';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -846,12 +846,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Append the markup for any blocks hooked `after` the given block and as its parent's
</span><span class="cx" style="display: block; padding: 0 10px"> * `last_child`, respectively, to the serialized markup for the given block.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param array $block The block to inject the hooked blocks after.
- * @param array $parent_block The parent block of the given block.
- * @param array $next The next sibling block of the given block.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param array $block The block to inject the hooked blocks after. Passed by reference.
+ * @param array $parent_block The parent block of the given block. Passed by reference. Default null.
+ * @param array $next The next sibling block of the given block. Default null.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return string The serialized markup for the given block, with the markup for any hooked blocks appended to it.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return function ( &$block, $parent_block = null, $next = null ) use ( $hooked_blocks, $context ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return function ( &$block, &$parent_block = null, $next = null ) use ( $hooked_blocks, $context ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $markup = '';
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $relative_position = 'after';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1058,7 +1058,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $block_content .= call_user_func_array(
</span><span class="cx" style="display: block; padding: 0 10px"> $pre_callback,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- array( &$inner_block, $block, $prev )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ array( &$inner_block, &$block, $prev )
</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">@@ -1071,7 +1071,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $block_content .= call_user_func_array(
</span><span class="cx" style="display: block; padding: 0 10px"> $post_callback,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- array( &$inner_block, $block, $next )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ array( &$inner_block, &$block, $next )
</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_index;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1122,7 +1122,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return string Serialized block markup.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_callback = null ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $result = '';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $result = '';
+ $parent_block = null; // At the top level, there is no parent block to pass to the callbacks; yet the callbacks expect a reference.
+
</ins><span class="cx" style="display: block; padding: 0 10px"> foreach ( $blocks as $index => $block ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_callable( $pre_callback ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $prev = 0 === $index
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1131,7 +1133,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $result .= call_user_func_array(
</span><span class="cx" style="display: block; padding: 0 10px"> $pre_callback,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- array( &$block, null, $prev ) // At the top level, there is no parent block to pass to the callback.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ array( &$block, &$parent_block, $prev )
</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">@@ -1144,7 +1146,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $result .= call_user_func_array(
</span><span class="cx" style="display: block; padding: 0 10px"> $post_callback,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- array( &$block, null, $next ) // At the top level, there is no parent block to pass to the callback.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ array( &$block, &$parent_block, $next )
</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></pre>
</div>
</div>
</body>
</html>