<!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>[58614] trunk: Block Hooks: Allow child insertion into Template Part 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/58614">58614</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/58614","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-07-02 10:01:17 +0000 (Tue, 02 Jul 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: Allow child insertion into Template Part block.

The Block Hooks mechanism was previously extended to allow insertion of a block as a Navigation block's first or last child. This was implemented by storing the `ignoredHookedBlocks` array in the corresponding `wp_navigation` post's post meta (instead of a metadata attribute on the anchor block).

This changeset extends that mechanism to Template Part blocks, by storing said metadata in the corresponding `wp_template_part` post's post meta, thus allowing extenders to use Block Hooks to insert a block as a Template Part block's first or last child, respectively.

Props tomjcafferkey, bernhard-reiter.
Fixes <a href="https://core.trac.wordpress.org/ticket/60854">#60854</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="#trunktestsphpunittestsblocktemplatesbuildBlockTemplateResultFromFilephp">trunk/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromFile.php</a></li>
<li><a href="#trunktestsphpunittestsblocktemplatesbuildBlockTemplateResultFromPostphp">trunk/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php</a></li>
<li><a href="#trunktestsphpunittestsblocktemplatesinjectIgnoredHookedBlocksMetadataAttributesphp">trunk/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.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    2024-07-01 23:34:19 UTC (rev 58613)
+++ trunk/src/wp-includes/block-template-utils.php      2024-07-02 10:01:17 UTC (rev 58614)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -606,16 +606,36 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $template->area = $template_file['area'];
</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">+        $hooked_blocks        = get_hooked_blocks();
+       $has_hooked_blocks    = ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' );
</ins><span class="cx" style="display: block; padding: 0 10px">         $before_block_visitor = '_inject_theme_attribute_in_template_part_block';
</span><span class="cx" style="display: block; padding: 0 10px">        $after_block_visitor  = null;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $hooked_blocks        = get_hooked_blocks();
-       if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       if ( $has_hooked_blocks ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
</span><span class="cx" style="display: block; padding: 0 10px">                $after_block_visitor  = make_after_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $blocks            = parse_blocks( $template->content );
-       $template->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        if ( 'wp_template_part' === $template->type && $has_hooked_blocks ) {
+               /**
+                * In order for hooked blocks to be inserted at positions first_child and last_child in a template part,
+                * we need to wrap its content a mock template part block and traverse it.
+                */
+               $content           = get_comment_delimited_block_content(
+                       'core/template-part',
+                       array(),
+                       $template->content
+               );
+               $content           = traverse_and_serialize_blocks( parse_blocks( $content ), $before_block_visitor, $after_block_visitor );
+               $template->content = remove_serialized_parent_block( $content );
+       } else {
+               $template->content = traverse_and_serialize_blocks(
+                       parse_blocks( $template->content ),
+                       $before_block_visitor,
+                       $after_block_visitor
+               );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         return $template;
</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">@@ -998,8 +1018,28 @@
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
</span><span class="cx" style="display: block; padding: 0 10px">                $after_block_visitor  = make_after_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $blocks               = parse_blocks( $template->content );
-               $template->content    = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( 'wp_template_part' === $template->type ) {
+                       $existing_ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true );
+                       $attributes                     = ! empty( $existing_ignored_hooked_blocks ) ? array( 'metadata' => array( 'ignoredHookedBlocks' => json_decode( $existing_ignored_hooked_blocks, true ) ) ) : array();
+
+                       /**
+                        * In order for hooked blocks to be inserted at positions first_child and last_child in a template part,
+                        * we need to wrap its content a mock template part block and traverse it.
+                        */
+                       $content           = get_comment_delimited_block_content(
+                               'core/template-part',
+                               $attributes,
+                               $template->content
+                       );
+                       $content           = traverse_and_serialize_blocks( parse_blocks( $content ), $before_block_visitor, $after_block_visitor );
+                       $template->content = remove_serialized_parent_block( $content );
+               } else {
+                       $template->content = traverse_and_serialize_blocks(
+                               parse_blocks( $template->content ),
+                               $before_block_visitor,
+                               $after_block_visitor
+                       );
+               }
</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 $template;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1611,7 +1651,36 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return $template;
</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">-        $changes->post_content = apply_block_hooks_to_content( $changes->post_content, $template, 'set_ignored_hooked_blocks_metadata' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( 'wp_template_part' === $post->post_type ) {
+               $attributes                     = array();
+               $existing_ignored_hooked_blocks = isset( $post->ID ) ? get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true ) : '';
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( ! empty( $existing_ignored_hooked_blocks ) ) {
+                       $attributes['metadata'] = array(
+                               'ignoredHookedBlocks' => json_decode( $existing_ignored_hooked_blocks, true ),
+                       );
+               }
+
+               $content = get_comment_delimited_block_content(
+                       'core/template-part',
+                       $attributes,
+                       $changes->post_content
+               );
+               $content = apply_block_hooks_to_content( $content, $template, 'set_ignored_hooked_blocks_metadata' );
+               $changes->post_content = remove_serialized_parent_block( $content );
+
+               $wrapper_block_markup  = extract_serialized_parent_block( $content );
+               $wrapper_block         = parse_blocks( $wrapper_block_markup )[0];
+               $ignored_hooked_blocks = $wrapper_block['attrs']['metadata']['ignoredHookedBlocks'] ?? array();
+               if ( ! empty( $ignored_hooked_blocks ) ) {
+                       if ( ! isset( $changes->meta_input ) ) {
+                               $changes->meta_input = array();
+                       }
+                       $changes->meta_input['_wp_ignored_hooked_blocks'] = wp_json_encode( $ignored_hooked_blocks );
+               }
+       } else {
+               $changes->post_content = apply_block_hooks_to_content( $changes->post_content, $template, 'set_ignored_hooked_blocks_metadata' );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         return $changes;
</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  2024-07-01 23:34:19 UTC (rev 58613)
+++ trunk/src/wp-includes/blocks.php    2024-07-02 10:01:17 UTC (rev 58614)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1046,6 +1046,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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Accepts the serialized markup of a block and its inner blocks, and returns serialized markup of the wrapper block.
+ *
+ * @since 6.7.0
+ * @access private
+ *
+ * @see remove_serialized_parent_block()
+ *
+ * @param string $serialized_block The serialized markup of a block and its inner blocks.
+ * @return string The serialized markup of the wrapper block.
+ */
+function extract_serialized_parent_block( $serialized_block ) {
+       $start = strpos( $serialized_block, '-->' ) + strlen( '-->' );
+       $end   = strrpos( $serialized_block, '<!--' );
+       return substr( $serialized_block, 0, $start ) . substr( $serialized_block, $end );
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Updates the wp_postmeta with the list of ignored hooked blocks where the inner blocks are stored as post content.
</span><span class="cx" style="display: block; padding: 0 10px">  * Currently only supports `wp_navigation` post types.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span></span></pre></div>
<a id="trunktestsphpunittestsblocktemplatesbuildBlockTemplateResultFromFilephp"></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-templates/buildBlockTemplateResultFromFile.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromFile.php    2024-07-01 23:34:19 UTC (rev 58613)
+++ trunk/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromFile.php      2024-07-02 10:01:17 UTC (rev 58614)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7,7 +7,21 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @covers ::_build_block_template_result_from_file
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> class Tests_Block_Templates_BuildBlockTemplateResultFromFile extends WP_Block_Templates_UnitTestCase {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * Tear down each test method.
+        *
+        * @since 6.7.0
+        */
+       public function tear_down() {
+               $registry = WP_Block_Type_Registry::get_instance();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( $registry->is_registered( 'tests/my-block' ) ) {
+                       $registry->unregister( 'tests/my-block' );
+               }
+
+               parent::tear_down();
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 54335
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -178,4 +192,78 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEmpty( $template->post_types );
</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 60506
+        */
+       public function test_should_inject_hooked_block_into_template_part() {
+               register_block_type(
+                       'tests/my-block',
+                       array(
+                               'block_hooks' => array(
+                                       'core/paragraph' => 'after',
+                               ),
+                       )
+               );
+
+               $template_part = _build_block_template_result_from_file(
+                       array(
+                               'slug'      => 'header',
+                               'postTypes' => array( 'post' ),
+                               'path'      => DIR_TESTDATA . '/templates/template.html',
+                       ),
+                       'wp_template_part'
+               );
+               $this->assertStringEndsWith( '<!-- wp:tests/my-block /-->', $template_part->content );
+       }
+
+       /*
+        * @ticket 60506
+        * @ticket 60854
+        */
+       public function test_should_injected_hooked_block_into_template_part_first_child() {
+               register_block_type(
+                       'tests/my-block',
+                       array(
+                               'block_hooks' => array(
+                                       'core/template-part' => 'first_child',
+                               ),
+                       )
+               );
+
+               $template_part = _build_block_template_result_from_file(
+                       array(
+                               'slug'      => 'header',
+                               'postTypes' => array( 'post' ),
+                               'path'      => DIR_TESTDATA . '/templates/template.html',
+                       ),
+                       'wp_template_part'
+               );
+               $this->assertStringStartsWith( '<!-- wp:tests/my-block /-->', $template_part->content );
+       }
+
+       /*
+        * @ticket 60506
+        * @ticket 60854
+        */
+       public function test_should_injected_hooked_block_into_template_part_last_child() {
+               register_block_type(
+                       'tests/my-block',
+                       array(
+                               'block_hooks' => array(
+                                       'core/template-part' => 'last_child',
+                               ),
+                       )
+               );
+
+               $template_part = _build_block_template_result_from_file(
+                       array(
+                               'slug'      => 'header',
+                               'postTypes' => array( 'post' ),
+                               'path'      => DIR_TESTDATA . '/templates/template.html',
+                       ),
+                       'wp_template_part'
+               );
+               $this->assertStringEndsWith( '<!-- wp:tests/my-block /-->', $template_part->content );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestsblocktemplatesbuildBlockTemplateResultFromPostphp"></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-templates/buildBlockTemplateResultFromPost.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php    2024-07-01 23:34:19 UTC (rev 58613)
+++ trunk/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php      2024-07-02 10:01:17 UTC (rev 58614)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -111,6 +111,50 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertStringEndsWith( '<!-- wp:tests/my-block /-->', $template_part->content );
</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 59646
+        * @ticket 60506
+        * @ticket 60854
+        */
+       public function test_should_injected_hooked_block_into_template_part_first_child() {
+               register_block_type(
+                       'tests/my-block',
+                       array(
+                               'block_hooks' => array(
+                                       'core/template-part' => 'first_child',
+                               ),
+                       )
+               );
+
+               $template_part = _build_block_template_result_from_post(
+                       self::$template_part_post,
+                       'wp_template_part'
+               );
+               $this->assertStringStartsWith( '<!-- wp:tests/my-block /-->', $template_part->content );
+       }
+
+       /*
+        * @ticket 59646
+        * @ticket 60506
+        * @ticket 60854
+        */
+       public function test_should_injected_hooked_block_into_template_part_last_child() {
+               register_block_type(
+                       'tests/my-block',
+                       array(
+                               'block_hooks' => array(
+                                       'core/template-part' => 'last_child',
+                               ),
+                       )
+               );
+
+               $template_part = _build_block_template_result_from_post(
+                       self::$template_part_post,
+                       'wp_template_part'
+               );
+               $this->assertStringEndsWith( '<!-- wp:tests/my-block /-->', $template_part->content );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 59646
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 60506
</span></span></pre></div>
<a id="trunktestsphpunittestsblocktemplatesinjectIgnoredHookedBlocksMetadataAttributesphp"></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-templates/injectIgnoredHookedBlocksMetadataAttributes.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php 2024-07-01 23:34:19 UTC (rev 58613)
+++ trunk/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php   2024-07-02 10:01:17 UTC (rev 58614)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -17,6 +17,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                if ( WP_Block_Type_Registry::get_instance()->is_registered( 'tests/hooked-block' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        unregister_block_type( 'tests/hooked-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">+                delete_post_meta( self::$template_part_post->ID, '_wp_ignored_hooked_blocks' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                parent::tear_down();
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -38,15 +39,36 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                inject_ignored_hooked_blocks_metadata_attributes( $changes );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $args              = $action->get_args();
-               $anchor_block_type = end( $args )[2];
-               $context           = end( $args )[3];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $args               = $action->get_args();
+               $relative_positions = array_column( $args, 1 );
+               $anchor_block_types = array_column( $args, 2 );
+               $contexts           = array_column( $args, 3 );
</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->assertSame( 'tests/anchor-block', $anchor_block_type );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame(
+                       array(
+                               'before',
+                               'after',
+                       ),
+                       $relative_positions,
+                       'The relative positions passed to the hooked_block_types filter are incorrect.'
+               );
</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->assertInstanceOf( 'WP_Block_Template', $context );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame(
+                       array(
+                               'tests/anchor-block',
+                               'tests/anchor-block',
+                       ),
+                       $anchor_block_types,
+                       'The anchor block types passed to the hooked_block_types filter are incorrect.'
+               );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $context = $contexts[0];
</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">+                        array_fill( 0, count( $contexts ), $context ),
+                       $contexts,
+                       'The context passed to the hooked_block_types filter should be the same for all calls.'
+               );
+               $this->assertSame(
</ins><span class="cx" style="display: block; padding: 0 10px">                         $changes->post_type,
</span><span class="cx" style="display: block; padding: 0 10px">                        $context->type,
</span><span class="cx" style="display: block; padding: 0 10px">                        'The type field of the context passed to the hooked_block_types filter doesn\'t match the template changes.'
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -69,6 +91,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 60754
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @ticket 60854
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_hooked_block_types_filter_with_newly_created_template_part() {
</span><span class="cx" style="display: block; padding: 0 10px">                $action = new MockAction();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -85,15 +108,49 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                inject_ignored_hooked_blocks_metadata_attributes( $changes );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $args              = $action->get_args();
-               $anchor_block_type = end( $args )[2];
-               $context           = end( $args )[3];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $args               = $action->get_args();
+               $relative_positions = array_column( $args, 1 );
+               $anchor_block_types = array_column( $args, 2 );
+               $contexts           = array_column( $args, 3 );
</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->assertSame( 'tests/anchor-block', $anchor_block_type );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame(
+                       array(
+                               'before',
+                               'after',
+                               'first_child',
+                               'before',
+                               'after',
+                               'last_child',
+                       ),
+                       $relative_positions,
+                       'The relative positions passed to the hooked_block_types filter are incorrect.'
+               );
</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->assertInstanceOf( 'WP_Block_Template', $context );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame(
+                       array(
+                               'core/template-part',
+                               'core/template-part',
+                               'core/template-part',
+                               'tests/anchor-block',
+                               'tests/anchor-block',
+                               'core/template-part',
+                       ),
+                       $anchor_block_types,
+                       'The anchor block types passed to the hooked_block_types filter are incorrect.'
+               );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $context = $contexts[0];
</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">+                        array_fill( 0, count( $contexts ), $context ),
+                       $contexts,
+                       'The context passed to the hooked_block_types filter should be the same for all calls.'
+               );
+               $this->assertInstanceOf(
+                       'WP_Block_Template',
+                       $context,
+                       'The context passed to the hooked_block_types filter is not an instance of WP_Block_Template.'
+               );
+               $this->assertSame(
</ins><span class="cx" style="display: block; padding: 0 10px">                         $changes->post_type,
</span><span class="cx" style="display: block; padding: 0 10px">                        $context->type,
</span><span class="cx" style="display: block; padding: 0 10px">                        'The type field of the context passed to the hooked_block_types filter doesn\'t match the template changes.'
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -140,15 +197,36 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                inject_ignored_hooked_blocks_metadata_attributes( $changes );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $args              = $action->get_args();
-               $anchor_block_type = end( $args )[2];
-               $context           = end( $args )[3];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $args               = $action->get_args();
+               $relative_positions = array_column( $args, 1 );
+               $anchor_block_types = array_column( $args, 2 );
+               $contexts           = array_column( $args, 3 );
</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->assertSame( 'tests/anchor-block', $anchor_block_type );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame(
+                       array(
+                               'before',
+                               'after',
+                       ),
+                       $relative_positions,
+                       'The relative positions passed to the hooked_block_types filter are incorrect.'
+               );
</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->assertInstanceOf( 'WP_Block_Template', $context );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame(
+                       array(
+                               'tests/anchor-block',
+                               'tests/anchor-block',
+                       ),
+                       $anchor_block_types,
+                       'The anchor block types passed to the hooked_block_types filter are incorrect.'
+               );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $context = $contexts[0];
</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">+                        array_fill( 0, count( $contexts ), $context ),
+                       $contexts,
+                       'The context passed to the hooked_block_types filter should be the same for all calls.'
+               );
+               $this->assertSame(
</ins><span class="cx" style="display: block; padding: 0 10px">                         $changes->post_name,
</span><span class="cx" style="display: block; padding: 0 10px">                        $context->slug,
</span><span class="cx" style="display: block; padding: 0 10px">                        'The slug field of the context passed to the hooked_block_types filter doesn\'t match the template changes.'
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -181,6 +259,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 60754
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @ticket 60854
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_hooked_block_types_filter_with_existing_template_part_file() {
</span><span class="cx" style="display: block; padding: 0 10px">                $action = new MockAction();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -201,15 +280,49 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                inject_ignored_hooked_blocks_metadata_attributes( $changes );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $args              = $action->get_args();
-               $anchor_block_type = end( $args )[2];
-               $context           = end( $args )[3];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $args               = $action->get_args();
+               $relative_positions = array_column( $args, 1 );
+               $anchor_block_types = array_column( $args, 2 );
+               $contexts           = array_column( $args, 3 );
</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->assertSame( 'tests/anchor-block', $anchor_block_type );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame(
+                       array(
+                               'before',
+                               'after',
+                               'first_child',
+                               'before',
+                               'after',
+                               'last_child',
+                       ),
+                       $relative_positions,
+                       'The relative positions passed to the hooked_block_types filter are incorrect.'
+               );
</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->assertInstanceOf( 'WP_Block_Template', $context );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame(
+                       array(
+                               'core/template-part',
+                               'core/template-part',
+                               'core/template-part',
+                               'tests/anchor-block',
+                               'tests/anchor-block',
+                               'core/template-part',
+                       ),
+                       $anchor_block_types,
+                       'The anchor block types passed to the hooked_block_types filter are incorrect.'
+               );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $context = $contexts[0];
</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">+                        array_fill( 0, count( $contexts ), $context ),
+                       $contexts,
+                       'The context passed to the hooked_block_types filter should be the same for all calls.'
+               );
+               $this->assertInstanceOf(
+                       'WP_Block_Template',
+                       $context,
+                       'The context passed to the hooked_block_types filter is not an instance of WP_Block_Template.'
+               );
+               $this->assertSame(
</ins><span class="cx" style="display: block; padding: 0 10px">                         $changes->post_name,
</span><span class="cx" style="display: block; padding: 0 10px">                        $context->slug,
</span><span class="cx" style="display: block; padding: 0 10px">                        'The slug field of the context passed to the hooked_block_types filter doesn\'t match the template changes.'
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -259,15 +372,36 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                inject_ignored_hooked_blocks_metadata_attributes( $changes );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $args              = $action->get_args();
-               $anchor_block_type = end( $args )[2];
-               $context           = end( $args )[3];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $args               = $action->get_args();
+               $relative_positions = array_column( $args, 1 );
+               $anchor_block_types = array_column( $args, 2 );
+               $contexts           = array_column( $args, 3 );
</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->assertSame( 'tests/anchor-block', $anchor_block_type );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame(
+                       array(
+                               'before',
+                               'after',
+                       ),
+                       $relative_positions,
+                       'The relative positions passed to the hooked_block_types filter are incorrect.'
+               );
</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->assertInstanceOf( 'WP_Block_Template', $context );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame(
+                       array(
+                               'tests/anchor-block',
+                               'tests/anchor-block',
+                       ),
+                       $anchor_block_types,
+                       'The anchor block types passed to the hooked_block_types filter are incorrect.'
+               );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $context = $contexts[0];
</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">+                        array_fill( 0, count( $contexts ), $context ),
+                       $contexts,
+                       'The context passed to the hooked_block_types filter should be the same for all calls.'
+               );
+               $this->assertSame(
</ins><span class="cx" style="display: block; padding: 0 10px">                         $changes->post_name,
</span><span class="cx" style="display: block; padding: 0 10px">                        $context->slug,
</span><span class="cx" style="display: block; padding: 0 10px">                        'The slug field of the context passed to the hooked_block_types filter doesn\'t match the template changes.'
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -302,6 +436,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 60754
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @ticket 60854
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_hooked_block_types_filter_with_existing_template_part_post() {
</span><span class="cx" style="display: block; padding: 0 10px">                $action = new MockAction();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -318,15 +453,49 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                inject_ignored_hooked_blocks_metadata_attributes( $changes );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $args              = $action->get_args();
-               $anchor_block_type = end( $args )[2];
-               $context           = end( $args )[3];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $args               = $action->get_args();
+               $relative_positions = array_column( $args, 1 );
+               $anchor_block_types = array_column( $args, 2 );
+               $contexts           = array_column( $args, 3 );
</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->assertSame( 'tests/anchor-block', $anchor_block_type );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame(
+                       array(
+                               'before',
+                               'after',
+                               'first_child',
+                               'before',
+                               'after',
+                               'last_child',
+                       ),
+                       $relative_positions,
+                       'The relative positions passed to the hooked_block_types filter are incorrect.'
+               );
</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->assertInstanceOf( 'WP_Block_Template', $context );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame(
+                       array(
+                               'core/template-part',
+                               'core/template-part',
+                               'core/template-part',
+                               'tests/anchor-block',
+                               'tests/anchor-block',
+                               'core/template-part',
+                       ),
+                       $anchor_block_types,
+                       'The anchor block types passed to the hooked_block_types filter are incorrect.'
+               );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $context = $contexts[0];
</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">+                        array_fill( 0, count( $contexts ), $context ),
+                       $contexts,
+                       'The context passed to the hooked_block_types filter should be the same for all calls.'
+               );
+               $this->assertInstanceOf(
+                       'WP_Block_Template',
+                       $context,
+                       'The context passed to the hooked_block_types filter is not an instance of WP_Block_Template.'
+               );
+               $this->assertSame(
</ins><span class="cx" style="display: block; padding: 0 10px">                         $changes->post_name,
</span><span class="cx" style="display: block; padding: 0 10px">                        $context->slug,
</span><span class="cx" style="display: block; padding: 0 10px">                        'The slug field of the context passed to the hooked_block_types filter doesn\'t match the template changes.'
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -419,4 +588,37 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'The hooked block was not injected into the anchor block\'s ignoredHookedBlocks metadata.'
</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 60854
+        */
+       public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_part_postmeta() {
+               register_block_type(
+                       'tests/hooked-block',
+                       array(
+                               'block_hooks' => array(
+                                       'core/template-part' => 'last_child',
+                               ),
+                       )
+               );
+
+               $id       = self::TEST_THEME . '//' . 'my_template_part';
+               $template = get_block_template( $id, 'wp_template_part' );
+
+               $changes               = new stdClass();
+               $changes->ID           = $template->wp_id;
+               $changes->post_content = '<!-- wp:tests/anchor-block -->Hello<!-- /wp:tests/anchor-block -->';
+
+               $post = inject_ignored_hooked_blocks_metadata_attributes( $changes );
+               $this->assertSame(
+                       array( 'tests/hooked-block' ),
+                       json_decode( $post->meta_input['_wp_ignored_hooked_blocks'], true ),
+                       'The hooked block was not injected into the wp_template_part\'s _wp_ignored_hooked_blocks postmeta.'
+               );
+               $this->assertSame(
+                       $changes->post_content,
+                       $post->post_content,
+                       'The template part\'s post content was modified.'
+               );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>