<!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>[56644] trunk: Blocks: Change `traverse_and_serialize_block(s)`'s callback signature.</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/56644">56644</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/56644","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-09-21 08:32:52 +0000 (Thu, 21 Sep 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'>Blocks: Change `traverse_and_serialize_block(s)`'s callback signature.
During work on <a href="https://core.trac.wordpress.org/ticket/59399">#59399</a>, it was discovered that ''sibling'' block insertion wasn't likely going to work the way it was planned, which required devising an alternative solution. This new solution requires some changes to `traverse_and_serialize_block(s)`:
- Change the signature of the existing callback such that:
- the return value is a string that will be prepended to the result of the inner block traversal and serialization;
- the function arguments are: a ''reference'' to the current block (so it can be modified inline, which is important e.g. for `theme` attribute insertion), the parent block, and the previous block (instead of the block index and chunk index).
- Add a second callback argument to `traverse_and_serialize_block(s)`, which is called ''after'' the block is traversed and serialized.
- Its function arguments are a reference to the current block, the parent block, and the next block.
Props gziolo.
Fixes <a href="https://core.trac.wordpress.org/ticket/59412">#59412</a>. See <a href="https://core.trac.wordpress.org/ticket/59313">#59313</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesblocktemplateutilsphp">trunk/src/wp-includes/block-template-utils.php</a></li>
<li><a href="#trunksrcwpincludesblocksphp">trunk/src/wp-includes/blocks.php</a></li>
<li><a href="#trunktestsphpunittestsblocktemplateutilsphp">trunk/tests/phpunit/tests/block-template-utils.php</a></li>
<li><a href="#trunktestsphpunittestsblocksserializephp">trunk/tests/phpunit/tests/blocks/serialize.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesblocktemplateutilsphp"></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/block-template-utils.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/block-template-utils.php 2023-09-21 06:33:48 UTC (rev 56643)
+++ trunk/src/wp-includes/block-template-utils.php 2023-09-21 08:32:52 UTC (rev 56644)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -515,9 +515,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @access private
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $block a parsed block.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return array Updated block.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return void
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function _inject_theme_attribute_in_template_part_block( $block ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function _inject_theme_attribute_in_template_part_block( &$block ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> if (
</span><span class="cx" style="display: block; padding: 0 10px"> 'core/template-part' === $block['blockName'] &&
</span><span class="cx" style="display: block; padding: 0 10px"> ! isset( $block['attrs']['theme'] )
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -524,7 +524,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $block['attrs']['theme'] = get_stylesheet();
</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 $block;
</del><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>
<a id="trunksrcwpincludesblocksphp"></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/blocks.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/blocks.php 2023-09-21 06:33:48 UTC (rev 56643)
+++ trunk/src/wp-includes/blocks.php 2023-09-21 08:32:52 UTC (rev 56644)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -886,38 +886,68 @@
</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">- * Traverses the block applying transformations using the callback provided and returns the content of a block,
- * including comment delimiters, serializing all attributes from the given parsed block.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Traverses a parsed block tree and applies callbacks before and after serializing it.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * This should be used when there is a need to modify the saved block.
- * Prefer `serialize_block` when preparing a block to be saved to post content.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Recursively traverses the block and its inner blocks and applies the two callbacks provided as
+ * arguments, the first one before serializing the block, and the second one after serializing it.
+ * If either callback returns a string value, it will be prepended and appended to the serialized
+ * block markup, respectively.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * The callbacks will receive a reference to the current block as their first argument, so that they
+ * can also modify it, and the current block's parent block as second argument. Finally, the
+ * `$pre_callback` receives the previous block, whereas the `$post_callback` receives
+ * the next block as third argument.
+ *
+ * Serialized blocks are returned including comment delimiters, and with all attributes serialized.
+ *
+ * This function should be used when there is a need to modify the saved block, or to inject markup
+ * into the return value. Prefer `serialize_block` when preparing a block to be saved to post content.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @since 6.4.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @see serialize_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 A representative array of a single parsed block object. See WP_Block_Parser_Block.
- * @param callable $callback Callback to run on each block in the tree before serialization.
- * It is called with the following arguments: $block, $parent_block, $block_index, $chunk_index.
- * @return string String of rendered HTML.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param array $block A representative array of a single parsed block object. See WP_Block_Parser_Block.
+ * @param callable $pre_callback Callback to run on each block in the tree before it is traversed and serialized.
+ * It is called with the following arguments: &$block, $parent_block, $previous_block.
+ * Its string return value will be prepended to the serialized block markup.
+ * @param callable $post_callback Callback to run on each block in the tree after it is traversed and serialized.
+ * It is called with the following arguments: &$block, $parent_block, $next_block.
+ * Its string return value will be appended to the serialized block markup.
+ * @return string Serialized block markup.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function traverse_and_serialize_block( $block, $callback ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function traverse_and_serialize_block( $block, $pre_callback = null, $post_callback = null ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $block_content = '';
</span><span class="cx" style="display: block; padding: 0 10px"> $block_index = 0;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- foreach ( $block['innerContent'] as $chunk_index => $chunk ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( $block['innerContent'] as $chunk ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( is_string( $chunk ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $block_content .= $chunk;
</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">- $inner_block = call_user_func(
- $callback,
- $block['innerBlocks'][ $block_index ],
- $block,
- $block_index,
- $chunk_index
- );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $inner_block = $block['innerBlocks'][ $block_index ];
+
+ if ( is_callable( $pre_callback ) ) {
+ $prev = 0 === $block_index
+ ? null
+ : $block['innerBlocks'][ $block_index - 1 ];
+ $block_content .= call_user_func_array(
+ $pre_callback,
+ array( &$inner_block, $block, $prev )
+ );
+ }
+
+ $block_content .= traverse_and_serialize_block( $inner_block, $pre_callback, $post_callback );
+
+ if ( is_callable( $post_callback ) ) {
+ $next = count( $block['innerBlocks'] ) - 1 === $block_index
+ ? null
+ : $block['innerBlocks'][ $block_index + 1 ];
+ $block_content .= call_user_func_array(
+ $post_callback,
+ array( &$inner_block, $block, $next )
+ );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> $block_index++;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $block_content .= traverse_and_serialize_block( $inner_block, $callback );
</del><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">@@ -933,27 +963,59 @@
</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">- * Traverses the blocks applying transformations using the callback provided,
- * and returns a joined string of the aggregate serialization of the given parsed blocks.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Given an array of parsed block trees, applies callbacks before and after serializing them and
+ * returns their concatenated output.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * This should be used when there is a need to modify the saved blocks.
- * Prefer `serialize_blocks` when preparing blocks to be saved to post content.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Recursively traverses the blocks and their inner blocks and applies the two callbacks provided as
+ * arguments, the first one before serializing a block, and the second one after serializing.
+ * If either callback returns a string value, it will be prepended and appended to the serialized
+ * block markup, respectively.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * The callbacks will receive a reference to the current block as their first argument, so that they
+ * can also modify it, and the current block's parent block as second argument. Finally, the
+ * `$pre_callback` receives the previous block, whereas the `$post_callback` receives
+ * the next block as third argument.
+ *
+ * Serialized blocks are returned including comment delimiters, and with all attributes serialized.
+ *
+ * This function should be used when there is a need to modify the saved blocks, or to inject markup
+ * into the return value. Prefer `serialize_blocks` when preparing blocks to be saved to post content.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @since 6.4.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @see serialize_blocks()
</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[] $blocks An array of representative arrays of parsed block objects. See serialize_block().
- * @param callable $callback Callback to run on each block in the tree before serialization.
- * It is called with the following arguments: $block, $parent_block, $block_index, $chunk_index.
- * @return string String of rendered HTML.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param array[] $blocks An array of parsed blocks. See WP_Block_Parser_Block.
+ * @param callable $pre_callback Callback to run on each block in the tree before it is traversed and serialized.
+ * It is called with the following arguments: &$block, $parent_block, $previous_block.
+ * Its string return value will be prepended to the serialized block markup.
+ * @param callable $post_callback Callback to run on each block in the tree after it is traversed and serialized.
+ * It is called with the following arguments: &$block, $parent_block, $next_block.
+ * Its string return value will be appended to the serialized block markup.
+ * @return string Serialized block markup.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function traverse_and_serialize_blocks( $blocks, $callback ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function traverse_and_serialize_blocks( $blocks, $pre_callback = null, $post_callback = null ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $result = '';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- foreach ( $blocks as $block ) {
- // At the top level, there is no parent block, block index, or chunk index to pass to the callback.
- $block = call_user_func( $callback, $block );
- $result .= traverse_and_serialize_block( $block, $callback );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( $blocks as $index => $block ) {
+ if ( is_callable( $pre_callback ) ) {
+ $prev = 0 === $index
+ ? null
+ : $blocks[ $index - 1 ];
+ $result .= call_user_func_array(
+ $pre_callback,
+ array( &$block, null, $prev ) // At the top level, there is no parent block to pass to the callback.
+ );
+ }
+ $result .= traverse_and_serialize_block( $block, $pre_callback, $post_callback );
+ if ( is_callable( $post_callback ) ) {
+ $next = count( $blocks ) - 1 === $index
+ ? null
+ : $blocks[ $index + 1 ];
+ $result .= call_user_func_array(
+ $post_callback,
+ array( &$block, null, $next ) // At the top level, there is no parent block to pass to the callback.
+ );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> return $result;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestsblocktemplateutilsphp"></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/block-template-utils.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/block-template-utils.php 2023-09-21 06:33:48 UTC (rev 56643)
+++ trunk/tests/phpunit/tests/block-template-utils.php 2023-09-21 08:32:52 UTC (rev 56644)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -225,7 +225,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers ::_inject_theme_attribute_in_template_part_block
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_inject_theme_attribute_in_template_part_block() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $template_part_block_without_theme_attribute = array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $template_part_block = array(
</ins><span class="cx" style="display: block; padding: 0 10px"> 'blockName' => 'core/template-part',
</span><span class="cx" style="display: block; padding: 0 10px"> 'attrs' => array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'slug' => 'header',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -238,7 +238,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'innerBlocks' => array(),
</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">- $actual = _inject_theme_attribute_in_template_part_block( $template_part_block_without_theme_attribute );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ _inject_theme_attribute_in_template_part_block( $template_part_block );
</ins><span class="cx" style="display: block; padding: 0 10px"> $expected = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'blockName' => 'core/template-part',
</span><span class="cx" style="display: block; padding: 0 10px"> 'attrs' => array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -254,7 +254,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame(
</span><span class="cx" style="display: block; padding: 0 10px"> $expected,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $actual,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $template_part_block,
</ins><span class="cx" style="display: block; padding: 0 10px"> '`theme` attribute was not correctly injected in template part block.'
</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">@@ -265,7 +265,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers ::_inject_theme_attribute_in_template_part_block
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_not_inject_theme_attribute_in_template_part_block_theme_attribute_exists() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $template_part_block_with_existing_theme_attribute = array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $template_part_block = array(
</ins><span class="cx" style="display: block; padding: 0 10px"> 'blockName' => 'core/template-part',
</span><span class="cx" style="display: block; padding: 0 10px"> 'attrs' => array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'slug' => 'header',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -279,10 +279,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'innerBlocks' => array(),
</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">- $actual = _inject_theme_attribute_in_template_part_block( $template_part_block_with_existing_theme_attribute );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $expected = $template_part_block;
+ _inject_theme_attribute_in_template_part_block( $template_part_block );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $template_part_block_with_existing_theme_attribute,
- $actual,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $expected,
+ $template_part_block,
</ins><span class="cx" style="display: block; padding: 0 10px"> 'Existing `theme` attribute in template part block was not respected by attribute injection.'
</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">@@ -301,10 +302,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'innerBlocks' => array(),
</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">- $actual = _inject_theme_attribute_in_template_part_block( $non_template_part_block );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $expected = $non_template_part_block;
+ _inject_theme_attribute_in_template_part_block( $non_template_part_block );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame(
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $expected,
</ins><span class="cx" style="display: block; padding: 0 10px"> $non_template_part_block,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $actual,
</del><span class="cx" style="display: block; padding: 0 10px"> '`theme` attribute injection modified non-template-part block.'
</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="trunktestsphpunittestsblocksserializephp"></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/serialize.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/blocks/serialize.php 2023-09-21 06:33:48 UTC (rev 56643)
+++ trunk/tests/phpunit/tests/blocks/serialize.php 2023-09-21 08:32:52 UTC (rev 56644)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -58,6 +58,7 @@
</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"> * @ticket 59327
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 59412
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers ::traverse_and_serialize_blocks
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -73,15 +74,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">- public static function add_attribute_to_inner_block( $block ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public static function add_attribute_to_inner_block( &$block ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( 'core/inner' === $block['blockName'] ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $block['attrs']['myattr'] = 'myvalue';
</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 $block;
</del><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="cx" style="display: block; padding: 0 10px"> * @ticket 59327
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 59412
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers ::traverse_and_serialize_blocks
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -92,12 +93,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_traverse_and_serialize_identity_from_parsed( $original ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $blocks = parse_blocks( $original );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $actual = traverse_and_serialize_blocks(
- $blocks,
- function ( $block ) {
- return $block;
- }
- );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $actual = traverse_and_serialize_blocks( $blocks );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( $original, $actual );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>