<!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>[57627] trunk: Block Hooks: Set `ignoredHookedBlocks` metadata upon saving.</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/57627">57627</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/57627","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-02-13 15:10:21 +0000 (Tue, 13 Feb 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: Set `ignoredHookedBlocks` metadata upon saving.
Decouple hooked blocks insertion from setting the `metadata.ignoredHookedBlocks` attribute on anchor blocks, and perform the latter step upon saving a template or template part to the database. This ensures that anchor blocks that have been newly added to a template (or part) on the client side will also get `ignoredHookedBlocks` metadata set correctly, thus preserving editor/front-end parity. Hooked block insertion, on the other hand, will continue to happen upon ''reading'' a template (or part).
Props gziolo, tomjcafferkey.
Fixes <a href="https://core.trac.wordpress.org/ticket/60506">#60506</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="#trunksrcwpincludesdefaultfiltersphp">trunk/src/wp-includes/default-filters.php</a></li>
<li><a href="#trunktestsphpunittestsblocktemplatesbasephp">trunk/tests/phpunit/tests/block-templates/base.php</a></li>
<li><a href="#trunktestsphpunittestsblocktemplatesbuildBlockTemplateResultFromPostphp">trunk/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php</a></li>
<li><a href="#trunktestsphpunittestsblocksgetHookedBlocksphp">trunk/tests/phpunit/tests/blocks/getHookedBlocks.php</a></li>
<li><a href="#trunktestsphpunittestsblocksinsertHookedBlocksphp">trunk/tests/phpunit/tests/blocks/insertHookedBlocks.php</a></li>
<li><a href="#trunktestsphpunittestsblockswpBlockPatternsRegistryphp">trunk/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsblockssetIgnoredHookedBlocksMetadataphp">trunk/tests/phpunit/tests/blocks/setIgnoredHookedBlocksMetadata.php</a></li>
</ul>
<h3>Removed Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsblocksgetHookedBlockMarkupphp">trunk/tests/phpunit/tests/blocks/getHookedBlockMarkup.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-02-13 14:34:17 UTC (rev 57626)
+++ trunk/src/wp-includes/block-template-utils.php 2024-02-13 15:10:21 UTC (rev 57627)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1432,3 +1432,39 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $template_hierarchy[] = 'index';
</span><span class="cx" style="display: block; padding: 0 10px"> return $template_hierarchy;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/**
+ * Inject ignoredHookedBlocks metadata attributes into a template or template part.
+ *
+ * Given a `wp_template` or `wp_template_part` post object, locate all blocks that have
+ * hooked blocks, and inject a `metadata.ignoredHookedBlocks` attribute into the anchor
+ * blocks to reflect the latter.
+ *
+ * @param WP_Post $post A post object with post type set to `wp_template` or `wp_template_part`.
+ * @return WP_Post The updated post object.
+ */
+function inject_ignored_hooked_blocks_metadata_attributes( $post ) {
+ $hooked_blocks = get_hooked_blocks();
+ if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) {
+ return;
+ }
+
+ // At this point, the post has already been created.
+ // We need to build the corresponding `WP_Block_Template` object as context argument for the visitor.
+ // To that end, we need to suppress hooked blocks from getting inserted into the template.
+ add_filter( 'hooked_block_types', '__return_empty_array', 99999, 0 );
+ $template = _build_block_template_result_from_post( $post );
+ remove_filter( 'hooked_block_types', '__return_empty_array', 99999 );
+
+ $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' );
+ $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' );
+
+ $blocks = parse_blocks( $template->content );
+ $content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
+
+ wp_update_post(
+ array(
+ 'ID' => $post->ID,
+ 'post_content' => $content,
+ )
+ );
+}
</ins></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-02-13 14:34:17 UTC (rev 57626)
+++ trunk/src/wp-includes/blocks.php 2024-02-13 15:10:21 UTC (rev 57627)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -851,44 +851,6 @@
</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.
- *
- * Accepts three arguments: A hooked block, its type, and a reference to an anchor block.
- * If the anchor block has already been processed, and the given hooked block type is in the list
- * of ignored hooked blocks, an empty string is returned.
- *
- * 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.
- *
- * This function is meant for internal use only.
- *
- * @since 6.5.0
- * @access private
- *
- * @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.
- */
-function get_hooked_block_markup( $hooked_block, $hooked_block_type, &$anchor_block ) {
- if ( ! isset( $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ) {
- $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] = array();
- }
-
- if ( in_array( $hooked_block_type, $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], true ) ) {
- return '';
- }
-
- // The following is only needed for the REST API endpoint.
- // However, its presence does not affect the frontend.
- $anchor_block['attrs']['metadata']['ignoredHookedBlocks'][] = $hooked_block_type;
-
- return serialize_block( $hooked_block );
-}
-
-/**
</del><span class="cx" style="display: block; padding: 0 10px"> * Returns the markup for blocks hooked to the given anchor block in a specific relative position.
</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="lines" style="display: block; padding: 0 10px; color: #888">@@ -946,8 +908,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $parsed_hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $parsed_hooked_block, $relative_position, $parsed_anchor_block, $context );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // It's possible that the `hooked_block_{$hooked_block_type}` filter returned a block of a different type,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // 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 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // so we explicitly look for the original `$hooked_block_type` in the `ignoredHookedBlocks` metadata.
+ if (
+ ! isset( $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ||
+ ! in_array( $hooked_block_type, $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'], true )
+ ) {
+ $markup .= serialize_block( $parsed_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"> return $markup;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -954,6 +921,48 @@
</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">+ * Adds a list of hooked block types to an anchor block's ignored hooked block types.
+ *
+ * This function is meant for internal use only.
+ *
+ * @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 An empty string.
+ */
+function set_ignored_hooked_blocks_metadata( &$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();
+
+ /** 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 );
+ if ( empty( $hooked_block_types ) ) {
+ return '';
+ }
+
+ $previously_ignored_hooked_blocks = isset( $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] )
+ ? $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks']
+ : array();
+
+ $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] = array_unique(
+ array_merge(
+ $previously_ignored_hooked_blocks,
+ $hooked_block_types
+ )
+ );
+
+ // Markup for the hooked blocks has already been created (in `insert_hooked_blocks`).
+ return '';
+}
+
+/**
</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">@@ -963,15 +972,19 @@
</span><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.4.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.5.0 Added $callback argument.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @access private
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $hooked_blocks An array of blocks hooked to another given block.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param WP_Block_Template|WP_Post|array $context A block template, template part, `wp_navigation` post object,
</span><span class="cx" style="display: block; padding: 0 10px"> * or pattern that the blocks belong to.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param callable $callback A function that will be called for each block to generate
+ * the markup for a given list of blocks that are hooked to it.
+ * Default: 'insert_hooked_blocks'.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return callable A function that returns the serialized markup for the given block,
</span><span class="cx" style="display: block; padding: 0 10px"> * including the markup for any hooked blocks before it.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function make_before_block_visitor( $hooked_blocks, $context ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function make_before_block_visitor( $hooked_blocks, $context, $callback = 'insert_hooked_blocks' ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Injects hooked blocks before the given block, injects the `theme` attribute into Template Part blocks, and returns the serialized markup.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -984,7 +997,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $prev The previous sibling block of the given block. Default null.
</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 prepended to it.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return function ( &$block, &$parent_block = null, $prev = null ) use ( $hooked_blocks, $context ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return function ( &$block, &$parent_block = null, $prev = null ) use ( $hooked_blocks, $context, $callback ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> _inject_theme_attribute_in_template_part_block( $block );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $markup = '';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -991,10 +1004,16 @@
</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">- $markup .= insert_hooked_blocks( $parent_block, 'first_child', $hooked_blocks, $context );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $markup .= call_user_func_array(
+ $callback,
+ array( &$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">- $markup .= insert_hooked_blocks( $block, 'before', $hooked_blocks, $context );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $markup .= call_user_func_array(
+ $callback,
+ array( &$block, 'before', $hooked_blocks, $context )
+ );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return $markup;
</span><span class="cx" style="display: block; padding: 0 10px"> };
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1010,15 +1029,19 @@
</span><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.4.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.5.0 Added $callback argument.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @access private
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $hooked_blocks An array of blocks hooked to another block.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param WP_Block_Template|WP_Post|array $context A block template, template part, `wp_navigation` post object,
</span><span class="cx" style="display: block; padding: 0 10px"> * or pattern that the blocks belong to.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param callable $callback A function that will be called for each block to generate
+ * the markup for a given list of blocks that are hooked to it.
+ * Default: 'insert_hooked_blocks'.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return callable A function that returns the serialized markup for the given block,
</span><span class="cx" style="display: block; padding: 0 10px"> * including the markup for any hooked blocks after it.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function make_after_block_visitor( $hooked_blocks, $context ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function make_after_block_visitor( $hooked_blocks, $context, $callback = 'insert_hooked_blocks' ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Injects hooked blocks after the given block, and returns the serialized markup.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1030,12 +1053,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $next The next sibling block of the given block. Default null.
</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><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return function ( &$block, &$parent_block = null, $next = null ) use ( $hooked_blocks, $context ) {
- $markup = insert_hooked_blocks( $block, 'after', $hooked_blocks, $context );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return function ( &$block, &$parent_block = null, $next = null ) use ( $hooked_blocks, $context, $callback ) {
+ $markup = call_user_func_array(
+ $callback,
+ array( &$block, 'after', $hooked_blocks, $context )
+ );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><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">- $markup .= insert_hooked_blocks( $parent_block, 'last_child', $hooked_blocks, $context );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $markup .= call_user_func_array(
+ $callback,
+ array( &$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="trunksrcwpincludesdefaultfiltersphp"></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/default-filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/default-filters.php 2024-02-13 14:34:17 UTC (rev 57626)
+++ trunk/src/wp-includes/default-filters.php 2024-02-13 15:10:21 UTC (rev 57627)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -752,4 +752,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'before_delete_post', '_wp_before_delete_font_face', 10, 2 );
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'init', '_wp_register_default_font_collections' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+// It might be nice to use a filter instead of an action, but the `WP_REST_Templates_Controller` doesn't
+// provide one (unlike e.g. `WP_REST_Posts_Controller`, which has `rest_pre_insert_{$this->post_type}`).
+add_action( 'rest_after_insert_wp_template', 'inject_ignored_hooked_blocks_metadata_attributes', 10, 3 );
+add_action( 'rest_after_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes', 10, 3 );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> unset( $filter, $action );
</span></span></pre></div>
<a id="trunktestsphpunittestsblocktemplatesbasephp"></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/base.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/block-templates/base.php 2024-02-13 14:34:17 UTC (rev 57626)
+++ trunk/tests/phpunit/tests/block-templates/base.php 2024-02-13 15:10:21 UTC (rev 57627)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -39,7 +39,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'post_type' => 'wp_template',
</span><span class="cx" style="display: block; padding: 0 10px"> 'post_name' => 'my_template',
</span><span class="cx" style="display: block; padding: 0 10px"> 'post_title' => 'My Template',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'post_content' => '<!-- wp:heading {"level":1} --><h1>Template</h1><!-- /wp:heading -->',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'post_content' => '<!-- wp:heading {"level":1,"metadata":{"ignoredHookedBlocks":["tests/ignored"]}} --><h1>Template</h1><!-- /wp:heading -->',
</ins><span class="cx" style="display: block; padding: 0 10px"> 'post_excerpt' => 'Description of my template',
</span><span class="cx" style="display: block; padding: 0 10px"> 'tax_input' => array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'wp_theme' => array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -57,7 +57,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'post_type' => 'wp_template_part',
</span><span class="cx" style="display: block; padding: 0 10px"> 'post_name' => 'my_template_part',
</span><span class="cx" style="display: block; padding: 0 10px"> 'post_title' => 'My Template Part',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'post_content' => '<!-- wp:heading {"level":2} --><h2>Template Part</h2><!-- /wp:heading -->',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'post_content' => '<!-- wp:heading {"level":2,"metadata":{"ignoredHookedBlocks":["tests/ignored"]}} --><h2>Template Part</h2><!-- /wp:heading -->',
</ins><span class="cx" style="display: block; padding: 0 10px"> 'post_excerpt' => 'Description of my template part',
</span><span class="cx" style="display: block; padding: 0 10px"> 'tax_input' => array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'wp_theme' => array(
</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-02-13 14:34:17 UTC (rev 57626)
+++ trunk/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php 2024-02-13 15:10:21 UTC (rev 57627)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -20,6 +20,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $registry->unregister( 'tests/my-block' );
</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">+ if ( $registry->is_registered( 'tests/ignored' ) ) {
+ $registry->unregister( 'tests/ignored' );
+ }
+
</ins><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="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -67,6 +71,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 59646
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 60506
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_should_inject_hooked_block_into_template() {
</span><span class="cx" style="display: block; padding: 0 10px"> register_block_type(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -83,11 +88,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'wp_template'
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertStringStartsWith( '<!-- wp:tests/my-block /-->', $template->content );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertStringContainsString( '"metadata":{"ignoredHookedBlocks":["tests/my-block"]}', $template->content );
</del><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 59646
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 60506
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_should_inject_hooked_block_into_template_part() {
</span><span class="cx" style="display: block; padding: 0 10px"> register_block_type(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -104,6 +109,47 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'wp_template_part'
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertStringEndsWith( '<!-- wp:tests/my-block /-->', $template_part->content );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertStringContainsString( '"metadata":{"ignoredHookedBlocks":["tests/my-block"]}', $template_part->content );
</del><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
+ */
+ public function test_should_not_inject_ignored_hooked_block_into_template() {
+ register_block_type(
+ 'tests/ignored',
+ array(
+ 'block_hooks' => array(
+ 'core/heading' => 'after',
+ ),
+ )
+ );
+
+ $template = _build_block_template_result_from_post(
+ self::$template_post,
+ 'wp_template'
+ );
+ $this->assertStringNotContainsString( '<!-- wp:tests/ignored /-->', $template->content );
+ }
+
+ /**
+ * @ticket 59646
+ * @ticket 60506
+ */
+ public function test_should_not_inject_ignored_hooked_block_into_template_part() {
+ register_block_type(
+ 'tests/ignored',
+ array(
+ 'block_hooks' => array(
+ 'core/heading' => 'after',
+ ),
+ )
+ );
+
+ $template_part = _build_block_template_result_from_post(
+ self::$template_part_post,
+ 'wp_template_part'
+ );
+ $this->assertStringNotContainsString( '<!-- wp:tests/ignored /-->', $template_part->content );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestsblocksgetHookedBlockMarkupphp"></a>
<div class="delfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Deleted: 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-02-13 14:34:17 UTC (rev 57626)
+++ trunk/tests/phpunit/tests/blocks/getHookedBlockMarkup.php 2024-02-13 15:10:21 UTC (rev 57627)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,112 +0,0 @@
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-<?php
-/**
- * Tests for the get_hooked_block_markup function.
- *
- * @package WordPress
- * @subpackage Blocks
- *
- * @since 6.5.0
- *
- * @group blocks
- * @group block-hooks
- */
-class Tests_Blocks_GetHookedBlockMarkup extends WP_UnitTestCase {
- const HOOKED_BLOCK_TYPE = 'tests/hooked-block';
- const HOOKED_BLOCK = array(
- 'blockName' => 'tests/different-hooked-block',
- 'attrs' => array(),
- 'innerContent' => array(),
- );
-
- /**
- * @ticket 59572
- * @ticket 60008
- * @ticket 60126
- *
- * @covers ::get_hooked_block_markup
- */
- public function test_get_hooked_block_markup_adds_metadata() {
- $anchor_block = array(
- 'blockName' => 'tests/anchor-block',
- );
-
- $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."
- );
- }
-
- /**
- * @ticket 59572
- * @ticket 60008
- * @ticket 60126
- *
- * @covers ::get_hooked_block_markup
- */
- public function test_get_hooked_block_markup_if_block_is_already_hooked() {
- $anchor_block = array(
- 'blockName' => 'tests/anchor-block',
- 'attrs' => array(
- 'metadata' => array(
- 'ignoredHookedBlocks' => array( self::HOOKED_BLOCK_TYPE ),
- ),
- ),
- );
-
- $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."
- );
- }
-
- /**
- * @ticket 59572
- * @ticket 60008
- * @ticket 60126
- *
- * @covers ::get_hooked_block_markup
- */
- public function test_get_hooked_block_markup_adds_to_ignored_hooked_blocks() {
- $other_hooked_block_type = 'tests/other-hooked-block';
- $other_hooked_block = array(
- 'blockName' => $other_hooked_block_type,
- 'attrs' => array(),
- 'innerContent' => array(),
- );
-
- $anchor_block = array(
- 'blockName' => 'tests/anchor-block',
- 'attrs' => array(
- 'metadata' => array(
- 'ignoredHookedBlocks' => array( self::HOOKED_BLOCK_TYPE ),
- ),
- ),
- );
-
- $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."
- );
- }
-}
</del></span></pre></div>
<a id="trunktestsphpunittestsblocksgetHookedBlocksphp"></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/getHookedBlocks.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/blocks/getHookedBlocks.php 2024-02-13 14:34:17 UTC (rev 57626)
+++ trunk/tests/phpunit/tests/blocks/getHookedBlocks.php 2024-02-13 15:10:21 UTC (rev 57627)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -136,6 +136,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 59313
</span><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 60506
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers ::get_hooked_blocks
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers ::get_block_file_template
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -150,7 +151,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $template->content
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertStringContainsString(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- '<!-- wp:post-content {"layout":{"type":"constrained"},"metadata":{"ignoredHookedBlocks":["tests/hooked-after"]}} /-->'
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ '<!-- wp:post-content {"layout":{"type":"constrained"}} /-->'
</ins><span class="cx" style="display: block; padding: 0 10px"> . '<!-- wp:tests/hooked-after /-->',
</span><span class="cx" style="display: block; padding: 0 10px"> $template->content
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -167,6 +168,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 59313
</span><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 60506
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers ::get_hooked_blocks
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers ::get_block_file_template
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -178,7 +180,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertStringContainsString(
</span><span class="cx" style="display: block; padding: 0 10px"> '<!-- wp:tests/hooked-before /-->'
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- . '<!-- wp:navigation {"layout":{"type":"flex","setCascadingProperties":true,"justifyContent":"right"},"metadata":{"ignoredHookedBlocks":["tests/hooked-before"]}} /-->',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ . '<!-- wp:navigation {"layout":{"type":"flex","setCascadingProperties":true,"justifyContent":"right"}} /-->',
</ins><span class="cx" style="display: block; padding: 0 10px"> $template->content
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertStringNotContainsString(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -198,6 +200,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 59313
</span><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 60506
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers ::get_hooked_blocks
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers WP_Block_Patterns_Registry::get_registered
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -218,7 +221,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $pattern['content']
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertStringContainsString(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- '<!-- wp:comments {"metadata":{"ignoredHookedBlocks":["tests/hooked-first-child"]}} -->'
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ '<!-- wp:comments -->'
</ins><span class="cx" style="display: block; padding: 0 10px"> . '<div class="wp-block-comments">'
</span><span class="cx" style="display: block; padding: 0 10px"> . '<!-- wp:tests/hooked-first-child /-->',
</span><span class="cx" style="display: block; padding: 0 10px"> str_replace( array( "\n", "\t" ), '', $pattern['content'] )
</span></span></pre></div>
<a id="trunktestsphpunittestsblocksinsertHookedBlocksphp"></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/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 2024-02-13 14:34:17 UTC (rev 57626)
+++ trunk/tests/phpunit/tests/blocks/insertHookedBlocks.php 2024-02-13 15:10:21 UTC (rev 57627)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -25,10 +25,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 59572
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 60126
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 60506
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers ::insert_hooked_blocks
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_insert_hooked_blocks_adds_metadata() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_insert_hooked_blocks_returns_correct_markup() {
</ins><span class="cx" style="display: block; padding: 0 10px"> $anchor_block = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'blockName' => self::ANCHOR_BLOCK_TYPE,
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -35,11 +36,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $actual = insert_hooked_blocks( $anchor_block, 'after', self::HOOKED_BLOCKS, array() );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- array( self::HOOKED_BLOCK_TYPE ),
- $anchor_block['attrs']['metadata']['ignoredHookedBlocks'],
- "Hooked block type wasn't added to ignoredHookedBlocks metadata."
- );
- $this->assertSame(
</del><span class="cx" style="display: block; padding: 0 10px"> '<!-- wp:' . self::HOOKED_BLOCK_TYPE . ' /-->',
</span><span class="cx" style="display: block; padding: 0 10px"> $actual,
</span><span class="cx" style="display: block; padding: 0 10px"> "Markup for hooked block wasn't generated correctly."
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -49,10 +45,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 59572
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 60126
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 60506
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers ::insert_hooked_blocks
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_insert_hooked_blocks_if_block_is_already_hooked() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_insert_hooked_blocks_if_block_is_ignored() {
</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="lines" style="display: block; padding: 0 10px; color: #888">@@ -64,11 +61,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $actual = insert_hooked_blocks( $anchor_block, 'after', self::HOOKED_BLOCKS, array() );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- array( self::HOOKED_BLOCK_TYPE ),
- $anchor_block['attrs']['metadata']['ignoredHookedBlocks'],
- "ignoredHookedBlocks metadata shouldn't have been modified."
- );
- $this->assertSame(
</del><span class="cx" style="display: block; padding: 0 10px"> '',
</span><span class="cx" style="display: block; padding: 0 10px"> $actual,
</span><span class="cx" style="display: block; padding: 0 10px"> "No markup should've been generated for ignored hooked block."
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -78,10 +70,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 59572
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 60126
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 60506
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers ::insert_hooked_blocks
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_insert_hooked_blocks_adds_to_ignored_hooked_blocks() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_insert_hooked_blocks_if_other_block_is_ignored() {
</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="lines" style="display: block; padding: 0 10px; color: #888">@@ -93,11 +86,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $actual = insert_hooked_blocks( $anchor_block, 'before', self::HOOKED_BLOCKS, array() );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 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(
</del><span class="cx" style="display: block; padding: 0 10px"> '<!-- wp:' . self::OTHER_HOOKED_BLOCK_TYPE . ' /-->',
</span><span class="cx" style="display: block; padding: 0 10px"> $actual,
</span><span class="cx" style="display: block; padding: 0 10px"> "Markup for newly hooked block should've been generated."
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -107,6 +95,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 59572
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 60126
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 60506
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers ::insert_hooked_blocks
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -140,11 +129,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> remove_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 3 );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- array( self::HOOKED_BLOCK_TYPE ),
- $anchor_block['attrs']['metadata']['ignoredHookedBlocks'],
- "Hooked block type wasn't added to ignoredHookedBlocks metadata."
- );
- $this->assertSame(
</del><span class="cx" style="display: block; padding: 0 10px"> '<!-- wp:' . self::HOOKED_BLOCK_TYPE . ' {"layout":{"type":"constrained"}} /-->',
</span><span class="cx" style="display: block; padding: 0 10px"> $actual,
</span><span class="cx" style="display: block; padding: 0 10px"> "Markup wasn't generated correctly for hooked block with attribute set by filter."
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -154,6 +138,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 59572
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 60126
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 60506
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers ::insert_hooked_blocks
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -190,11 +175,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> remove_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 3 );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- array( self::HOOKED_BLOCK_TYPE ),
- $anchor_block['attrs']['metadata']['ignoredHookedBlocks'],
- "Hooked block type wasn't added to ignoredHookedBlocks metadata."
- );
- $this->assertSame(
</del><span class="cx" style="display: block; padding: 0 10px"> '<!-- wp:group --><div class="wp-block-group"><!-- wp:' . self::HOOKED_BLOCK_TYPE . ' /--></div><!-- /wp:group -->',
</span><span class="cx" style="display: block; padding: 0 10px"> $actual,
</span><span class="cx" style="display: block; padding: 0 10px"> "Markup wasn't generated correctly for hooked block wrapped in Group block by filter."
</span></span></pre></div>
<a id="trunktestsphpunittestsblockssetIgnoredHookedBlocksMetadataphp"></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/setIgnoredHookedBlocksMetadata.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/blocks/setIgnoredHookedBlocksMetadata.php (rev 0)
+++ trunk/tests/phpunit/tests/blocks/setIgnoredHookedBlocksMetadata.php 2024-02-13 15:10:21 UTC (rev 57627)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,147 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Tests for the set_ignored_hooked_blocks_metadata function.
+ *
+ * @package WordPress
+ * @subpackage Blocks
+ *
+ * @since 6.5.0
+ *
+ * @group blocks
+ * @group block-hooks
+ */
+class Tests_Blocks_SetIgnoredHookedBlocksMetadata extends WP_UnitTestCase {
+ /**
+ * @ticket 60506
+ */
+ private static function create_block_template_object() {
+ $template = new WP_Block_Template();
+ $template->type = 'wp_template';
+ $template->theme = 'test-theme';
+ $template->slug = 'single';
+ $template->id = $template->theme . '//' . $template->slug;
+ $template->title = 'Single';
+ $template->content = '<!-- wp:tests/anchor-block /-->';
+ $template->description = 'Description of my template';
+
+ return $template;
+ }
+
+ /**
+ * @ticket 60506
+ *
+ * @covers ::set_ignored_hooked_blocks_metadata
+ */
+ public function test_set_ignored_hooked_blocks_metadata() {
+ $anchor_block = array(
+ 'blockName' => 'tests/anchor-block',
+ );
+
+ $hooked_blocks = array(
+ 'tests/anchor-block' => array(
+ 'after' => array( 'tests/hooked-block' ),
+ ),
+ );
+
+ set_ignored_hooked_blocks_metadata( $anchor_block, 'after', $hooked_blocks, null );
+ $this->assertSame( $anchor_block['attrs']['metadata']['ignoredHookedBlocks'], array( 'tests/hooked-block' ) );
+ }
+
+ /**
+ * @ticket 60506
+ *
+ * @covers ::set_ignored_hooked_blocks_metadata
+ */
+ public function test_set_ignored_hooked_blocks_metadata_retains_existing_items() {
+ $anchor_block = array(
+ 'blockName' => 'tests/anchor-block',
+ 'attrs' => array(
+ 'metadata' => array(
+ 'ignoredHookedBlocks' => array( 'tests/other-ignored-block' ),
+ ),
+ ),
+ );
+
+ $hooked_blocks = array(
+ 'tests/anchor-block' => array(
+ 'after' => array( 'tests/hooked-block' ),
+ ),
+ );
+
+ set_ignored_hooked_blocks_metadata( $anchor_block, 'after', $hooked_blocks, null );
+ $this->assertSame(
+ $anchor_block['attrs']['metadata']['ignoredHookedBlocks'],
+ array( 'tests/other-ignored-block', 'tests/hooked-block' )
+ );
+ }
+
+ /**
+ * @ticket 60506
+ *
+ * @covers ::set_ignored_hooked_blocks_metadata
+ */
+ public function test_set_ignored_hooked_blocks_metadata_for_block_added_by_filter() {
+ $anchor_block = array(
+ 'blockName' => 'tests/anchor-block',
+ 'attrs' => array(),
+ );
+
+ $hooked_blocks = array();
+
+ $filter = function ( $hooked_block_types, $relative_position, $anchor_block_type ) {
+ if ( 'tests/anchor-block' === $anchor_block_type && 'after' === $relative_position ) {
+ $hooked_block_types[] = 'tests/hooked-block-added-by-filter';
+ }
+
+ return $hooked_block_types;
+ };
+
+ add_filter( 'hooked_block_types', $filter, 10, 3 );
+ set_ignored_hooked_blocks_metadata( $anchor_block, 'after', $hooked_blocks, null );
+ remove_filter( 'hooked_block_types', $filter, 10 );
+
+ $this->assertSame(
+ $anchor_block['attrs']['metadata']['ignoredHookedBlocks'],
+ array( 'tests/hooked-block-added-by-filter' )
+ );
+ }
+
+ /**
+ * @ticket 60506
+ *
+ * @covers ::set_ignored_hooked_blocks_metadata
+ */
+ public function test_set_ignored_hooked_blocks_metadata_for_block_added_by_context_aware_filter() {
+ $anchor_block = array(
+ 'blockName' => 'tests/anchor-block',
+ 'attrs' => array(),
+ );
+
+ $filter = function ( $hooked_block_types, $relative_position, $anchor_block_type, $context ) {
+ if (
+ ! $context instanceof WP_Block_Template ||
+ ! property_exists( $context, 'slug' ) ||
+ 'single' !== $context->slug
+ ) {
+ return $hooked_block_types;
+ }
+
+ if ( 'tests/anchor-block' === $anchor_block_type && 'after' === $relative_position ) {
+ $hooked_block_types[] = 'tests/hooked-block-added-by-filter';
+ }
+
+ return $hooked_block_types;
+ };
+
+ $template = self::create_block_template_object();
+
+ add_filter( 'hooked_block_types', $filter, 10, 4 );
+ set_ignored_hooked_blocks_metadata( $anchor_block, 'after', array(), $template );
+ remove_filter( 'hooked_block_types', $filter, 10 );
+
+ $this->assertSame(
+ $anchor_block['attrs']['metadata']['ignoredHookedBlocks'],
+ array( 'tests/hooked-block-added-by-filter' )
+ );
+ }
+}
</ins></span></pre></div>
<a id="trunktestsphpunittestsblockswpBlockPatternsRegistryphp"></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/wpBlockPatternsRegistry.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php 2024-02-13 14:34:17 UTC (rev 57626)
+++ trunk/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php 2024-02-13 15:10:21 UTC (rev 57627)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -344,6 +344,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 59476
</span><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 60506
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers WP_Block_Patterns_Registry::register
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers WP_Block_Patterns_Registry::get_all_registered
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -385,9 +386,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $registered = $this->registry->get_all_registered();
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertCount( 3, $registered );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertStringEndsWith( '<!-- wp:tests/my-block /-->', $registered[1]['content'] );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertStringContainsString( '"metadata":{"ignoredHookedBlocks":["tests/my-block"]}', $registered[1]['content'] );
</del><span class="cx" style="display: block; padding: 0 10px"> $this->assertStringEndsWith( '<!-- wp:tests/my-block /-->', $registered[2]['content'] );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertStringContainsString( '"metadata":{"ignoredHookedBlocks":["tests/my-block"]}', $registered[2]['content'] );
</del><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -418,6 +417,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 59476
</span><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 60506
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers WP_Block_Patterns_Registry::register
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers WP_Block_Patterns_Registry::get_registered
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -446,7 +446,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $pattern = $this->registry->get_registered( 'test/one' );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertStringStartsWith( '<!-- wp:tests/my-block /-->', $pattern['content'] );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertStringContainsString( '"metadata":{"ignoredHookedBlocks":["tests/my-block"]}', $pattern['content'] );
</del><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre>
</div>
</div>
</body>
</html>