<!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>[57354] trunk: Block Hooks: Introduce a new `hooked_block_{$block_type}` 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/57354">57354</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/57354","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>2024-01-25 13:46:49 +0000 (Thu, 25 Jan 2024)</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: Introduce a new `hooked_block_{$block_type}` filter.

Add a new `hooked_block_{$block_type}` filter that allows modifying a hooked block (in parsed block format) prior to insertion, while providing read access to its anchor block (in the same format).

This allows block authors to e.g. set a hooked block's attributes, or its inner blocks; the filter can peruse information about the anchor block when doing so. As such, this filter provides a solution to both <a href="https://core.trac.wordpress.org/ticket/59572">#59572</a> and <a href="https://core.trac.wordpress.org/ticket/60126">#60126</a>.

The new filter is designed to strike a good balance and separation of concerns with regard to the existing [https://developer.wordpress.org/reference/hooks/hooked_block_types/ `hooked_block_types` filter], which allows addition or removal of a block to the list of hooked blocks for a given anchor block -- all of which are identified only by their block ''types''. This new filter, on the other hand, only applies to ''one'' hooked block at a time, and allows modifying the entire (parsed) hooked block; it also gives (read) access to the parsed anchor block.

Props gziolo, tomjcafferkey, andrewserong, isabel_brison, timbroddin, yansern.
Fixes <a href="https://core.trac.wordpress.org/ticket/59572">#59572</a>, <a href="https://core.trac.wordpress.org/ticket/60126">#60126</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesblocksphp">trunk/src/wp-includes/blocks.php</a></li>
<li><a href="#trunktestsphpunittestsblocksgetHookedBlockMarkupphp">trunk/tests/phpunit/tests/blocks/getHookedBlockMarkup.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsblocksinsertHookedBlocksphp">trunk/tests/phpunit/tests/blocks/insertHookedBlocks.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<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  2024-01-25 12:46:04 UTC (rev 57353)
+++ trunk/src/wp-includes/blocks.php    2024-01-25 13:46:49 UTC (rev 57354)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -758,22 +758,28 @@
</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">- * Conditionally returns the markup for a given hooked block type.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Conditionally returns the markup for a given hooked block.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Accepts two arguments: A reference to an anchor block, and the name of a hooked block type.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Accepts three arguments: A hooked block, its type, and a reference to an anchor block.
</ins><span class="cx" style="display: block; padding: 0 10px">  * If the anchor block has already been processed, and the given hooked block type is in the list
</span><span class="cx" style="display: block; padding: 0 10px">  * of ignored hooked blocks, an empty string is returned.
</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 hooked block type is specified separately as it's possible that a filter might've modified
+ * the hooked block such that `$hooked_block['blockName']` does no longer reflect the original type.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px">  * This function is meant for internal use only.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 6.5.0
</span><span class="cx" style="display: block; padding: 0 10px">  * @access private
</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   $anchor_block      The anchor block. Passed by reference.
- * @param string  $hooked_block_type The name of the hooked block type.
- * @return string The markup for the given hooked block type, or an empty string if the block is ignored.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param array  $hooked_block      The hooked block, represented as a parsed block array.
+ * @param string $hooked_block_type The type of the hooked block. This could be different from
+ *                                  $hooked_block['blockName'], as a filter might've modified the latter.
+ * @param array  $anchor_block      The anchor block, represented as a parsed block array.
+ *                                  Passed by reference.
+ * @return string The markup for the given hooked block, or an empty string if the block is ignored.
</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 get_hooked_block_markup( &$anchor_block, $hooked_block_type ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function get_hooked_block_markup( $hooked_block, $hooked_block_type, &$anchor_block ) {
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( ! isset( $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] = array();
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -786,10 +792,71 @@
</span><span class="cx" style="display: block; padding: 0 10px">        // However, its presence does not affect the frontend.
</span><span class="cx" style="display: block; padding: 0 10px">        $anchor_block['attrs']['metadata']['ignoredHookedBlocks'][] = $hooked_block_type;
</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 get_comment_delimited_block_content( $hooked_block_type, array(), '' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return serialize_block( $hooked_block );
</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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Returns the markup for blocks hooked to the given anchor block in a specific relative position.
+ *
+ * @since 6.5.0
+ * @access private
+ *
+ * @param array                   $parsed_anchor_block The anchor block, in parsed block array format.
+ * @param string                  $relative_position   The relative position of the hooked blocks.
+ *                                                     Can be one of 'before', 'after', 'first_child', or 'last_child'.
+ * @param array                   $hooked_blocks       An array of hooked block types, grouped by anchor block and relative position.
+ * @param WP_Block_Template|array $context             The block template, template part, or pattern that the anchor block belongs to.
+ * @return string
+ */
+function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooked_blocks, $context ) {
+       $anchor_block_type  = $parsed_anchor_block['blockName'];
+       $hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] )
+               ? $hooked_blocks[ $anchor_block_type ][ $relative_position ]
+               : array();
+
+       /**
+        * Filters the list of hooked block types for a given anchor block type and relative position.
+        *
+        * @since 6.4.0
+        *
+        * @param string[]                $hooked_block_types The list of hooked block types.
+        * @param string                  $relative_position  The relative position of the hooked blocks.
+        *                                                    Can be one of 'before', 'after', 'first_child', or 'last_child'.
+        * @param string                  $anchor_block_type  The anchor block type.
+        * @param WP_Block_Template|array $context            The block template, template part, or pattern that the anchor block belongs to.
+        */
+       $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
+
+       $markup = '';
+       foreach ( $hooked_block_types as $hooked_block_type ) {
+               $parsed_hooked_block = array(
+                       'blockName'    => $hooked_block_type,
+                       'attrs'        => array(),
+                       'innerBlocks'  => array(),
+                       'innerContent' => array(),
+               );
+
+               /**
+                * Filters the parsed block array for a given hooked block.
+                *
+                * @since 6.5.0
+                *
+                * @param array                   $parsed_hooked_block The parsed block array for the given hooked block type.
+                * @param string                  $relative_position   The relative position of the hooked block.
+                * @param array                   $parsed_anchor_block The anchor block, in parsed block array format.
+                * @param WP_Block_Template|array $context             The block template, template part, or pattern that the anchor block belongs to.
+                */
+               $parsed_hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $parsed_hooked_block, $relative_position, $parsed_anchor_block, $context );
+
+               // It's possible that the `hooked_block_{$hooked_block_type}` filter returned a block of a different type,
+               // so we need to pass the original $hooked_block_type as well.
+               $markup .= get_hooked_block_markup( $parsed_hooked_block, $hooked_block_type, $parsed_anchor_block );
+       }
+
+       return $markup;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Returns a function that injects the theme attribute into, and hooked blocks before, a given block.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * The returned function can be used as `$pre_callback` argument to `traverse_and_serialize_block(s)`,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -826,41 +893,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( $parent_block && ! $prev ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        // Candidate for first-child insertion.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $relative_position  = 'first_child';
-                       $anchor_block_type  = $parent_block['blockName'];
-                       $hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] )
-                               ? $hooked_blocks[ $anchor_block_type ][ $relative_position ]
-                               : array();
-
-                       /**
-                        * Filters the list of hooked block types for a given anchor block type and relative position.
-                        *
-                        * @since 6.4.0
-                        *
-                        * @param string[]                $hooked_block_types  The list of hooked block types.
-                        * @param string                  $relative_position   The relative position of the hooked blocks.
-                        *                                                     Can be one of 'before', 'after', 'first_child', or 'last_child'.
-                        * @param string                  $anchor_block_type   The anchor block type.
-                        * @param WP_Block_Template|array $context             The block template, template part, or pattern that the anchor block belongs to.
-                        */
-                       $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
-                       foreach ( $hooked_block_types as $hooked_block_type ) {
-                               $markup .= get_hooked_block_markup( $parent_block, $hooked_block_type );
-                       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $markup .= insert_hooked_blocks( $parent_block, 'first_child', $hooked_blocks, $context );
</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">-                $relative_position  = 'before';
-               $anchor_block_type  = $block['blockName'];
-               $hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] )
-                       ? $hooked_blocks[ $anchor_block_type ][ $relative_position ]
-                       : array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $markup .= insert_hooked_blocks( $block, 'before', $hooked_blocks, $context );
</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 filter is documented in wp-includes/blocks.php */
-               $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
-               foreach ( $hooked_block_types as $hooked_block_type ) {
-                       $markup .= get_hooked_block_markup( $block, $hooked_block_type );
-               }
-
</del><span class="cx" style="display: block; padding: 0 10px">                 return $markup;
</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">@@ -895,33 +932,11 @@
</span><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><span class="cx" style="display: block; padding: 0 10px">        return function ( &$block, &$parent_block = null, $next = null ) use ( $hooked_blocks, $context ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $markup = '';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $markup = insert_hooked_blocks( $block, 'after', $hooked_blocks, $context );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $relative_position  = 'after';
-               $anchor_block_type  = $block['blockName'];
-               $hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] )
-                               ? $hooked_blocks[ $anchor_block_type ][ $relative_position ]
-                               : array();
-
-               /** This filter is documented in wp-includes/blocks.php */
-               $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
-               foreach ( $hooked_block_types as $hooked_block_type ) {
-                       $markup .= get_hooked_block_markup( $block, $hooked_block_type );
-               }
-
</del><span class="cx" style="display: block; padding: 0 10px">                 if ( $parent_block && ! $next ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        // Candidate for last-child insertion.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $relative_position  = 'last_child';
-                       $anchor_block_type  = $parent_block['blockName'];
-                       $hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] )
-                               ? $hooked_blocks[ $anchor_block_type ][ $relative_position ]
-                               : array();
-
-                       /** This filter is documented in wp-includes/blocks.php */
-                       $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
-                       foreach ( $hooked_block_types as $hooked_block_type ) {
-                               $markup .= get_hooked_block_markup( $parent_block, $hooked_block_type );
-                       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $markup .= insert_hooked_blocks( $parent_block, 'last_child', $hooked_blocks, $context );
</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">                return $markup;
</span></span></pre></div>
<a id="trunktestsphpunittestsblocksgetHookedBlockMarkupphp"></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/getHookedBlockMarkup.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/blocks/getHookedBlockMarkup.php 2024-01-25 12:46:04 UTC (rev 57353)
+++ trunk/tests/phpunit/tests/blocks/getHookedBlockMarkup.php   2024-01-25 13:46:49 UTC (rev 57354)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -11,8 +11,17 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @group block-hooks
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> class Tests_Blocks_GetHookedBlockMarkup extends WP_UnitTestCase {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        const HOOKED_BLOCK_TYPE = 'tests/hooked-block';
+       const HOOKED_BLOCK      = array(
+               'blockName'    => 'tests/different-hooked-block',
+               'attrs'        => array(),
+               'innerContent' => array(),
+       );
+
</ins><span class="cx" style="display: block; padding: 0 10px">         /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @ticket 59572
</ins><span class="cx" style="display: block; padding: 0 10px">          * @ticket 60008
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @ticket 60126
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @covers ::get_hooked_block_markup
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -21,13 +30,23 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'blockName' => 'tests/anchor-block',
</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 = get_hooked_block_markup( $anchor_block, 'tests/hooked-block' );
-               $this->assertSame( array( 'tests/hooked-block' ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] );
-               $this->assertSame( '<!-- wp:tests/hooked-block /-->', $actual );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $actual = get_hooked_block_markup( self::HOOKED_BLOCK, self::HOOKED_BLOCK_TYPE, $anchor_block );
+               $this->assertSame(
+                       array( self::HOOKED_BLOCK_TYPE ),
+                       $anchor_block['attrs']['metadata']['ignoredHookedBlocks'],
+                       "Hooked block type wasn't added to ignoredHookedBlocks metadata."
+               );
+               $this->assertSame(
+                       '<!-- wp:' . self::HOOKED_BLOCK['blockName'] . ' /-->',
+                       $actual,
+                       "Markup for hooked block wasn't generated correctly."
+               );
</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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @ticket 59572
</ins><span class="cx" style="display: block; padding: 0 10px">          * @ticket 60008
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @ticket 60126
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @covers ::get_hooked_block_markup
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -36,33 +55,58 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'blockName' => 'tests/anchor-block',
</span><span class="cx" style="display: block; padding: 0 10px">                        'attrs'     => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'metadata' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        'ignoredHookedBlocks' => array( 'tests/hooked-block' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 'ignoredHookedBlocks' => array( self::HOOKED_BLOCK_TYPE ),
</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="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $actual = get_hooked_block_markup( $anchor_block, 'tests/hooked-block' );
-               $this->assertSame( array( 'tests/hooked-block' ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] );
-               $this->assertSame( '', $actual );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $actual = get_hooked_block_markup( self::HOOKED_BLOCK, self::HOOKED_BLOCK_TYPE, $anchor_block );
+               $this->assertSame(
+                       array( self::HOOKED_BLOCK_TYPE ),
+                       $anchor_block['attrs']['metadata']['ignoredHookedBlocks'],
+                       "ignoredHookedBlocks metadata shouldn't have been modified."
+               );
+               $this->assertSame(
+                       '',
+                       $actual,
+                       "No markup should've been generated for ignored hooked block."
+               );
</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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @ticket 59572
</ins><span class="cx" style="display: block; padding: 0 10px">          * @ticket 60008
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @ticket 60126
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @covers ::get_hooked_block_markup
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_get_hooked_block_markup_adds_to_ignored_hooked_blocks() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $other_hooked_block_type = 'tests/other-hooked-block';
+               $other_hooked_block      = array(
+                       'blockName'    => $other_hooked_block_type,
+                       'attrs'        => array(),
+                       'innerContent' => array(),
+               );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $anchor_block = array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'blockName' => 'tests/anchor-block',
</span><span class="cx" style="display: block; padding: 0 10px">                        'attrs'     => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'metadata' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        'ignoredHookedBlocks' => array( 'tests/hooked-block' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 'ignoredHookedBlocks' => array( self::HOOKED_BLOCK_TYPE ),
</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="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $actual = get_hooked_block_markup( $anchor_block, 'tests/other-hooked-block' );
-               $this->assertSame( array( 'tests/hooked-block', 'tests/other-hooked-block' ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] );
-               $this->assertSame( '<!-- wp:tests/other-hooked-block /-->', $actual );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $actual = get_hooked_block_markup( $other_hooked_block, $other_hooked_block_type, $anchor_block );
+               $this->assertSame(
+                       array( self::HOOKED_BLOCK_TYPE, $other_hooked_block_type ),
+                       $anchor_block['attrs']['metadata']['ignoredHookedBlocks'],
+                       "Newly hooked block should've been added to ignoredHookedBlocks metadata while retaining previously ignored one."
+               );
+               $this->assertSame(
+                       '<!-- wp:' . $other_hooked_block_type . ' /-->',
+                       $actual,
+                       "Markup for newly hooked block should've been generated."
+               );
</ins><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="trunktestsphpunittestsblocksinsertHookedBlocksphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/blocks/insertHookedBlocks.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/blocks/insertHookedBlocks.php                           (rev 0)
+++ trunk/tests/phpunit/tests/blocks/insertHookedBlocks.php     2024-01-25 13:46:49 UTC (rev 57354)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,203 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Tests for the insert_hooked_blocks function.
+ *
+ * @package WordPress
+ * @subpackage Blocks
+ *
+ * @since 6.5.0
+ *
+ * @group blocks
+ * @group block-hooks
+ */
+class Tests_Blocks_InsertHookedBlocks extends WP_UnitTestCase {
+       const ANCHOR_BLOCK_TYPE       = 'tests/anchor-block';
+       const HOOKED_BLOCK_TYPE       = 'tests/hooked-block';
+       const OTHER_HOOKED_BLOCK_TYPE = 'tests/other-hooked-block';
+
+       const HOOKED_BLOCKS = array(
+               self::ANCHOR_BLOCK_TYPE => array(
+                       'after'  => array( self::HOOKED_BLOCK_TYPE ),
+                       'before' => array( self::OTHER_HOOKED_BLOCK_TYPE ),
+               ),
+       );
+
+       /**
+        * @ticket 59572
+        * @ticket 60126
+        *
+        * @covers ::insert_hooked_blocks
+        */
+       public function test_insert_hooked_blocks_adds_metadata() {
+               $anchor_block = array(
+                       'blockName' => self::ANCHOR_BLOCK_TYPE,
+               );
+
+               $actual = insert_hooked_blocks( $anchor_block, 'after', self::HOOKED_BLOCKS, array() );
+               $this->assertSame(
+                       array( self::HOOKED_BLOCK_TYPE ),
+                       $anchor_block['attrs']['metadata']['ignoredHookedBlocks'],
+                       "Hooked block type wasn't added to ignoredHookedBlocks metadata."
+               );
+               $this->assertSame(
+                       '<!-- wp:' . self::HOOKED_BLOCK_TYPE . ' /-->',
+                       $actual,
+                       "Markup for hooked block wasn't generated correctly."
+               );
+       }
+
+       /**
+        * @ticket 59572
+        * @ticket 60126
+        *
+        * @covers ::insert_hooked_blocks
+        */
+       public function test_insert_hooked_blocks_if_block_is_already_hooked() {
+               $anchor_block = array(
+                       'blockName' => 'tests/anchor-block',
+                       'attrs'     => array(
+                               'metadata' => array(
+                                       'ignoredHookedBlocks' => array( self::HOOKED_BLOCK_TYPE ),
+                               ),
+                       ),
+               );
+
+               $actual = insert_hooked_blocks( $anchor_block, 'after', self::HOOKED_BLOCKS, array() );
+               $this->assertSame(
+                       array( self::HOOKED_BLOCK_TYPE ),
+                       $anchor_block['attrs']['metadata']['ignoredHookedBlocks'],
+                       "ignoredHookedBlocks metadata shouldn't have been modified."
+               );
+               $this->assertSame(
+                       '',
+                       $actual,
+                       "No markup should've been generated for ignored hooked block."
+               );
+       }
+
+       /**
+        * @ticket 59572
+        * @ticket 60126
+        *
+        * @covers ::insert_hooked_blocks
+        */
+       public function test_insert_hooked_blocks_adds_to_ignored_hooked_blocks() {
+               $anchor_block = array(
+                       'blockName' => 'tests/anchor-block',
+                       'attrs'     => array(
+                               'metadata' => array(
+                                       'ignoredHookedBlocks' => array( self::HOOKED_BLOCK_TYPE ),
+                               ),
+                       ),
+               );
+
+               $actual = insert_hooked_blocks( $anchor_block, 'before', self::HOOKED_BLOCKS, array() );
+               $this->assertSame(
+                       array( self::HOOKED_BLOCK_TYPE, self::OTHER_HOOKED_BLOCK_TYPE ),
+                       $anchor_block['attrs']['metadata']['ignoredHookedBlocks'],
+                       "Newly hooked block should've been added to ignoredHookedBlocks metadata while retaining previously ignored one."
+               );
+               $this->assertSame(
+                       '<!-- wp:' . self::OTHER_HOOKED_BLOCK_TYPE . ' /-->',
+                       $actual,
+                       "Markup for newly hooked block should've been generated."
+               );
+       }
+
+       /**
+        * @ticket 59572
+        * @ticket 60126
+        *
+        * @covers ::insert_hooked_blocks
+        */
+       public function test_insert_hooked_blocks_filter_can_set_attributes() {
+               $anchor_block = array(
+                       'blockName'    => self::ANCHOR_BLOCK_TYPE,
+                       'attrs'        => array(
+                               'layout' => array(
+                                       'type' => 'constrained',
+                               ),
+                       ),
+                       'innerContent' => array(),
+               );
+
+               $filter = function ( $parsed_hooked_block, $relative_position, $parsed_anchor_block ) {
+                       // Is the hooked block adjacent to the anchor block?
+                       if ( 'before' !== $relative_position && 'after' !== $relative_position ) {
+                               return $parsed_hooked_block;
+                       }
+
+                       // Does the anchor block have a layout attribute?
+                       if ( isset( $parsed_anchor_block['attrs']['layout'] ) ) {
+                               // Copy the anchor block's layout attribute to the hooked block.
+                               $parsed_hooked_block['attrs']['layout'] = $parsed_anchor_block['attrs']['layout'];
+                       }
+
+                       return $parsed_hooked_block;
+               };
+               add_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 3 );
+               $actual = insert_hooked_blocks( $anchor_block, 'after', self::HOOKED_BLOCKS, array() );
+               remove_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 3 );
+
+               $this->assertSame(
+                       array( self::HOOKED_BLOCK_TYPE ),
+                       $anchor_block['attrs']['metadata']['ignoredHookedBlocks'],
+                       "Hooked block type wasn't added to ignoredHookedBlocks metadata."
+               );
+               $this->assertSame(
+                       '<!-- wp:' . self::HOOKED_BLOCK_TYPE . ' {"layout":{"type":"constrained"}} /-->',
+                       $actual,
+                       "Markup wasn't generated correctly for hooked block with attribute set by filter."
+               );
+       }
+
+       /**
+        * @ticket 59572
+        * @ticket 60126
+        *
+        * @covers ::insert_hooked_blocks
+        */
+       public function test_insert_hooked_blocks_filter_can_wrap_block() {
+               $anchor_block = array(
+                       'blockName'    => self::ANCHOR_BLOCK_TYPE,
+                       'attrs'        => array(
+                               'layout' => array(
+                                       'type' => 'constrained',
+                               ),
+                       ),
+                       'innerContent' => array(),
+               );
+
+               $filter = function ( $parsed_hooked_block ) {
+                       if ( self::HOOKED_BLOCK_TYPE !== $parsed_hooked_block['blockName'] ) {
+                               return $parsed_hooked_block;
+                       }
+
+                       // Wrap the block in a Group block.
+                       return array(
+                               'blockName'    => 'core/group',
+                               'attrs'        => array(),
+                               'innerBlocks'  => array( $parsed_hooked_block ),
+                               'innerContent' => array(
+                                       '<div class="wp-block-group">',
+                                       null,
+                                       '</div>',
+                               ),
+                       );
+               };
+               add_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 3 );
+               $actual = insert_hooked_blocks( $anchor_block, 'after', self::HOOKED_BLOCKS, array() );
+               remove_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 3 );
+
+               $this->assertSame(
+                       array( self::HOOKED_BLOCK_TYPE ),
+                       $anchor_block['attrs']['metadata']['ignoredHookedBlocks'],
+                       "Hooked block type wasn't added to ignoredHookedBlocks metadata."
+               );
+               $this->assertSame(
+                       '<!-- wp:group --><div class="wp-block-group"><!-- wp:' . self::HOOKED_BLOCK_TYPE . ' /--></div><!-- /wp:group -->',
+                       $actual,
+                       "Markup wasn't generated correctly for hooked block wrapped in Group block by filter."
+               );
+       }
+}
</ins></span></pre>
</div>
</div>

</body>
</html>