<!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>[56673] trunk: Blocks: Introduce filter to allow easy addition of hooked blocks.</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/56673">56673</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/56673","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>Bernhard Reiter</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-09-25 08:42:45 +0000 (Mon, 25 Sep 2023)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Blocks: Introduce filter to allow easy addition of hooked blocks.
Introduce a `hooked_block_types` filter that allows easier conditional addition (or removal) of hooked blocks for a given anchor block and relative position.
{{{#!php
function insert_shopping_cart_hooked_block( $hooked_blocks, $position, $anchor_block, $context ) {
if ( 'after' === $position && 'core/navigation' === $anchor_block && /** $context is header template part **/ ) {
$hooked_blocks[] = 'mycommerce/shopping-cart';
}
return $hooked_blocks;
}
add_filter( 'hooked_block_types', 'insert_shopping_cart_hooked_block', 10, 4 );
}}}
Props gziolo, nerrad, dmsnell, ndiego.
Fixes <a href="https://core.trac.wordpress.org/ticket/59424">#59424</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesblocksphp">trunk/src/wp-includes/blocks.php</a></li>
<li><a href="#trunktestsphpunittestsblocksblockHooksphp">trunk/tests/phpunit/tests/blocks/blockHooks.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesblocksphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/blocks.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/blocks.php 2023-09-25 00:05:31 UTC (rev 56672)
+++ trunk/src/wp-includes/blocks.php 2023-09-25 08:42:45 UTC (rev 56673)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -744,10 +744,11 @@
</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><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string $name Block type name including namespace.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param string $name Block type name including namespace.
+ * @param string $relative_position Optional. Relative position of the hooked block. Default empty string.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return array Associative array of `$block_type_name => $position` pairs.
</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 get_hooked_blocks( $name ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function get_hooked_blocks( $name, $relative_position = '' ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
</span><span class="cx" style="display: block; padding: 0 10px"> $hooked_blocks = array();
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $block_types as $block_type ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -754,10 +755,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! property_exists( $block_type, 'block_hooks' ) || ! is_array( $block_type->block_hooks ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> continue;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- foreach ( $block_type->block_hooks as $anchor_block_type => $relative_position ) {
- if ( $anchor_block_type === $name ) {
- $hooked_blocks[ $block_type->name ] = $relative_position;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( $block_type->block_hooks as $anchor_block_type => $position ) {
+ if ( $anchor_block_type !== $name ) {
+ continue;
</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 ( $relative_position && $relative_position !== $position ) {
+ continue;
+ }
+ $hooked_blocks[ $block_type->name ] = $position;
</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 $hooked_blocks;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -797,34 +802,48 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $parent && ! $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">- $hooked_blocks_for_parent = get_hooked_blocks( $parent['blockName'] );
- foreach ( $hooked_blocks_for_parent as $hooked_block_type => $relative_position ) {
- if ( 'first_child' === $relative_position ) {
- $hooked_block_markup = get_comment_delimited_block_content( $hooked_block_type, array(), '' );
- /** This filter is documented in wp-includes/blocks.php */
- $markup .= apply_filters( 'inject_hooked_block_markup', $hooked_block_markup, $hooked_block_type, $relative_position, $parent, $context );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $relative_position = 'first_child';
+ $anchor_block_type = $parent['blockName'];
+ $hooked_block_types = array_keys( get_hooked_blocks( $anchor_block_type, $relative_position ) );
+ /**
+ * Filters the list of hooked block types for a given anchor block type and relative position.
+ *
+ * @since 6.4.0
+ *
+ * @param string[] $hooked_block_types The list of hooked block types.
+ * @param string $relative_position The relative position of the hooked blocks.
+ * Can be one of 'before', 'after', 'first_child', or 'last_child'.
+ * @param string $anchor_block_type The anchor block type.
+ * @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to.
+ */
+ $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
+ foreach ( $hooked_block_types as $hooked_block_type ) {
+ $hooked_block_markup = get_comment_delimited_block_content( $hooked_block_type, array(), '' );
+ /** This filter is documented in wp-includes/blocks.php */
+ $markup .= apply_filters( 'inject_hooked_block_markup', $hooked_block_markup, $hooked_block_type, $relative_position, $parent, $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">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $hooked_blocks = get_hooked_blocks( $block['blockName'] );
- foreach ( $hooked_blocks as $hooked_block_type => $relative_position ) {
- if ( 'before' === $relative_position ) {
- $hooked_block_markup = get_comment_delimited_block_content( $hooked_block_type, array(), '' );
- /**
- * Filters the serialized markup of a hooked block.
- *
- * @since 6.4.0
- *
- * @param string $hooked_block_markup The serialized markup of the hooked block.
- * @param string $hooked_block_type The type of the hooked block.
- * @param string $relative_position The relative position of the hooked block.
- * Can be one of 'before', 'after', 'first_child', or 'last_child'.
- * @param array $block The anchor block.
- * @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to.
- */
- $markup .= apply_filters( 'inject_hooked_block_markup', $hooked_block_markup, $hooked_block_type, $relative_position, $block, $context );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $relative_position = 'before';
+ $anchor_block_type = $block['blockName'];
+ $hooked_block_types = array_keys( get_hooked_blocks( $anchor_block_type, $relative_position ) );
+ /** This filter is documented in wp-includes/blocks.php */
+ $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
+ foreach ( $hooked_block_types as $hooked_block_type ) {
+ $hooked_block_markup = get_comment_delimited_block_content( $hooked_block_type, array(), '' );
+ /**
+ * Filters the serialized markup of a hooked block.
+ *
+ * @since 6.4.0
+ *
+ * @param string $hooked_block_markup The serialized markup of the hooked block.
+ * @param string $hooked_block_type The type of the hooked block.
+ * @param string $relative_position The relative position of the hooked block.
+ * Can be one of 'before', 'after', 'first_child', or 'last_child'.
+ * @param array $block The anchor block.
+ * @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to.
+ */
+ $markup .= apply_filters( 'inject_hooked_block_markup', $hooked_block_markup, $hooked_block_type, $relative_position, $block, $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 class="lines" style="display: block; padding: 0 10px; color: #888">@@ -860,24 +879,28 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return function( &$block, $parent = null, $next = null ) use ( $context ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $markup = '';
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $hooked_blocks = get_hooked_blocks( $block['blockName'] );
- foreach ( $hooked_blocks as $hooked_block_type => $relative_position ) {
- if ( 'after' === $relative_position ) {
- $hooked_block_markup = get_comment_delimited_block_content( $hooked_block_type, array(), '' );
- /** This filter is documented in wp-includes/blocks.php */
- $markup .= apply_filters( 'inject_hooked_block_markup', $hooked_block_markup, $hooked_block_type, $relative_position, $block, $context );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $relative_position = 'after';
+ $anchor_block_type = $block['blockName'];
+ $hooked_block_types = array_keys( get_hooked_blocks( $anchor_block_type, $relative_position ) );
+ /** This filter is documented in wp-includes/blocks.php */
+ $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
+ foreach ( $hooked_block_types as $hooked_block_type ) {
+ $hooked_block_markup = get_comment_delimited_block_content( $hooked_block_type, array(), '' );
+ /** This filter is documented in wp-includes/blocks.php */
+ $markup .= apply_filters( 'inject_hooked_block_markup', $hooked_block_markup, $hooked_block_type, $relative_position, $block, $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"> if ( $parent && ! $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">- $hooked_blocks_for_parent = get_hooked_blocks( $parent['blockName'] );
- foreach ( $hooked_blocks_for_parent as $hooked_block_type => $relative_position ) {
- if ( 'last_child' === $relative_position ) {
- $hooked_block_markup = get_comment_delimited_block_content( $hooked_block_type, array(), '' );
- /** This filter is documented in wp-includes/blocks.php */
- $markup .= apply_filters( 'inject_hooked_block_markup', $hooked_block_markup, $hooked_block_type, $relative_position, $parent, $context );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $relative_position = 'last_child';
+ $anchor_block_type = $parent['blockName'];
+ $hooked_block_types = array_keys( get_hooked_blocks( $anchor_block_type, $relative_position ) );
+ /** This filter is documented in wp-includes/blocks.php */
+ $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
+ foreach ( $hooked_block_types as $hooked_block_type ) {
+ $hooked_block_markup = get_comment_delimited_block_content( $hooked_block_type, array(), '' );
+ /** This filter is documented in wp-includes/blocks.php */
+ $markup .= apply_filters( 'inject_hooked_block_markup', $hooked_block_markup, $hooked_block_type, $relative_position, $parent, $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">
</span></span></pre></div>
<a id="trunktestsphpunittestsblocksblockHooksphp"></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/blockHooks.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/blocks/blockHooks.php 2023-09-25 00:05:31 UTC (rev 56672)
+++ trunk/tests/phpunit/tests/blocks/blockHooks.php 2023-09-25 08:42:45 UTC (rev 56673)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -49,8 +49,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'tests/injected-one',
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'block_hooks' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'tests/hooked-at-before' => 'before',
- 'tests/hooked-at-after' => 'after',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'tests/hooked-at-before' => 'before',
+ 'tests/hooked-at-after' => 'after',
+ 'tests/hooked-at-before-and-after' => 'before',
</ins><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -58,10 +59,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'tests/injected-two',
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'block_hooks' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'tests/hooked-at-before' => 'before',
- 'tests/hooked-at-after' => 'after',
- 'tests/hooked-at-first-child' => 'first_child',
- 'tests/hooked-at-last-child' => 'last_child',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'tests/hooked-at-before' => 'before',
+ 'tests/hooked-at-after' => 'after',
+ 'tests/hooked-at-before-and-after' => 'after',
+ 'tests/hooked-at-first-child' => 'first_child',
+ 'tests/hooked-at-last-child' => 'last_child',
</ins><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -96,5 +98,20 @@
</span><span class="cx" style="display: block; padding: 0 10px"> get_hooked_blocks( 'tests/hooked-at-last-child' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'block hooked at the last child position'
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame(
+ array(
+ 'tests/injected-one' => 'before',
+ 'tests/injected-two' => 'after',
+ ),
+ get_hooked_blocks( 'tests/hooked-at-before-and-after' ),
+ 'block hooked before one block and after another'
+ );
+ $this->assertSame(
+ array(
+ 'tests/injected-one' => 'before',
+ ),
+ get_hooked_blocks( 'tests/hooked-at-before-and-after', 'before' ),
+ 'block hooked before one block and after another, filtered for before'
+ );
</ins><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>