<!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>[59523] trunk: Block Hooks: Enable for post content.</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/59523">59523</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/59523","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-12-17 10:35:17 +0000 (Tue, 17 Dec 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: Enable for post content.
Block Hooks were previously only applied to layout elements such as templates, template parts, patterns, and navigation menus -- things that are edited in the Site Editor.
This changeset enables Block Hooks in post content. The parity between frontend and editor is preserved: Blocks inserted by Block Hooks are visible both on the frontend and in the editor, and any customizations made by the user are respected on the frontend.
This is possible thanks to setting the `metadata.ignoredHookedBlocks` attribute on anchor blocks (a technique first introduced in <a href="https://core.trac.wordpress.org/changeset/57594">[57594]</a>). For first child and last child insertion into a Post Content block, the corresponding post object's `_wp_ignored_hooked_blocks` post meta is set.
Props bernhard-reiter, gziolo, jonsurrell, karolmanijak, leewillis77.
Fixes <a href="https://core.trac.wordpress.org/ticket/61074">#61074</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<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="#trunktestsphpunittestsblocksapplyBlockHooksToContentphp">trunk/tests/phpunit/tests/blocks/applyBlockHooksToContent.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesblocksphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/blocks.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/blocks.php 2024-12-16 23:49:11 UTC (rev 59522)
+++ trunk/src/wp-includes/blocks.php 2024-12-17 10:35:17 UTC (rev 59523)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -912,7 +912,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $relative_position The relative position of the hooked blocks.
</span><span class="cx" style="display: block; padding: 0 10px"> * Can be one of 'before', 'after', 'first_child', or 'last_child'.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $anchor_block_type The anchor block type.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_Block_Template|WP_Post|array $context The block template, template part, `wp_navigation` post type,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param WP_Block_Template|WP_Post|array $context The block template, template part, post object,
</ins><span class="cx" style="display: block; padding: 0 10px"> * or pattern that the anchor block belongs to.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -935,7 +935,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $hooked_block_type The hooked block type name.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $relative_position The relative position of the hooked block.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $parsed_anchor_block The anchor block, in parsed block array format.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_Block_Template|WP_Post|array $context The block template, template part, `wp_navigation` post type,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param WP_Block_Template|WP_Post|array $context The block template, template part, post object,
</ins><span class="cx" style="display: block; padding: 0 10px"> * or pattern that the anchor block belongs to.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> $parsed_hooked_block = apply_filters( 'hooked_block', $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -951,7 +951,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $hooked_block_type The hooked block type name.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $relative_position The relative position of the hooked block.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $parsed_anchor_block The anchor block, in parsed block array format.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_Block_Template|WP_Post|array $context The block template, template part, `wp_navigation` post type,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param WP_Block_Template|WP_Post|array $context The block template, template part, post object,
</ins><span class="cx" style="display: block; padding: 0 10px"> * or pattern that the anchor block belongs to.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> $parsed_hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1039,17 +1039,25 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 6.6.0
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 6.7.0 Injects the `theme` attribute into Template Part blocks, even if no hooked blocks are registered.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.8.0 Have the `$context` parameter default to `null`, in which case `get_post()` will be called to use the current post as context.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @access private
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string $content Serialized content.
- * @param WP_Block_Template|WP_Post|array $context A block template, template part, `wp_navigation` post object,
- * or pattern that the blocks belong to.
- * @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'.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param string $content Serialized content.
+ * @param WP_Block_Template|WP_Post|array|null $context A block template, template part, post object, or pattern
+ * that the blocks belong to. If set to `null`, `get_post()`
+ * will be called to use the current post as context.
+ * Default: `null`.
+ * @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 string The serialized 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">-function apply_block_hooks_to_content( $content, $context, $callback = 'insert_hooked_blocks' ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function apply_block_hooks_to_content( $content, $context = null, $callback = 'insert_hooked_blocks' ) {
+ // Default to the current post if no context is provided.
+ if ( null === $context ) {
+ $context = get_post();
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $hooked_blocks = get_hooked_blocks();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $before_block_visitor = '_inject_theme_attribute_in_template_part_block';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1165,10 +1173,11 @@
</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">- * Updates the wp_postmeta with the list of ignored hooked blocks where the inner blocks are stored as post content.
- * Currently only supports `wp_navigation` post types.
</del><ins style="background-color: #dfd; text-decoration:none; 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.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 6.6.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.8.0 Support non-`wp_navigation` post types.
</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 stdClass $post Post object.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1176,7 +1185,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function update_ignored_hooked_blocks_postmeta( $post ) {
</span><span class="cx" style="display: block; padding: 0 10px"> /*
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * In this scenario the user has likely tried to create a navigation via the REST API.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * In this scenario the user has likely tried to create a new post object via the REST API.
</ins><span class="cx" style="display: block; padding: 0 10px"> * In which case we won't have a post ID to work with and store meta against.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $post->ID ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1184,7 +1193,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"> /*
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Skip meta generation when consumers intentionally update specific Navigation fields
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Skip meta generation when consumers intentionally update specific fields
</ins><span class="cx" style="display: block; padding: 0 10px"> * and omit the content update.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! isset( $post->post_content ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1192,9 +1201,9 @@
</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">- * Skip meta generation when the post content is not a navigation block.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Skip meta generation if post type is not set.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! isset( $post->post_type ) || 'wp_navigation' !== $post->post_type ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! isset( $post->post_type ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return $post;
</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">@@ -1208,8 +1217,14 @@
</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">+ if ( 'wp_navigation' === $post->post_type ) {
+ $wrapper_block_type = 'core/navigation';
+ } else {
+ $wrapper_block_type = 'core/post-content';
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $markup = get_comment_delimited_block_content(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'core/navigation',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $wrapper_block_type,
</ins><span class="cx" style="display: block; padding: 0 10px"> $attributes,
</span><span class="cx" style="display: block; padding: 0 10px"> $post->post_content
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1266,9 +1281,10 @@
</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">- * Hooks into the REST API response for the core/navigation block and adds the first and last inner blocks.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Hooks into the REST API response for the Posts endpoint and adds the first and last inner blocks.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 6.6.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.8.0 Support non-`wp_navigation` post types.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param WP_REST_Response $response The response object.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param WP_Post $post Post object.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1275,7 +1291,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return WP_REST_Response The response object.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function insert_hooked_blocks_into_rest_response( $response, $post ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! isset( $response->data['content']['raw'] ) || ! isset( $response->data['content']['rendered'] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( empty( $response->data['content']['raw'] ) || empty( $response->data['content']['rendered'] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return $response;
</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">@@ -1287,22 +1303,44 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'ignoredHookedBlocks' => $ignored_hooked_blocks,
</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 ( 'wp_navigation' === $post->post_type ) {
+ $wrapper_block_type = 'core/navigation';
+ } else {
+ $wrapper_block_type = 'core/post-content';
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $content = get_comment_delimited_block_content(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'core/navigation',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $wrapper_block_type,
</ins><span class="cx" style="display: block; padding: 0 10px"> $attributes,
</span><span class="cx" style="display: block; padding: 0 10px"> $response->data['content']['raw']
</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">- $content = apply_block_hooks_to_content( $content, $post );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $content = apply_block_hooks_to_content(
+ $content,
+ $post,
+ 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
+ );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Remove mock Navigation block wrapper.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Remove mock block wrapper.
</ins><span class="cx" style="display: block; padding: 0 10px"> $content = remove_serialized_parent_block( $content );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $response->data['content']['raw'] = $content;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // `apply_block_hooks_to_content` is called above. Ensure it is not called again as a filter.
+ $priority = has_filter( 'the_content', 'apply_block_hooks_to_content' );
+ if ( false !== $priority ) {
+ remove_filter( 'the_content', 'apply_block_hooks_to_content', $priority );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /** This filter is documented in wp-includes/post-template.php */
</span><span class="cx" style="display: block; padding: 0 10px"> $response->data['content']['rendered'] = apply_filters( 'the_content', $content );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Restore the filter if it was set initially.
+ if ( false !== $priority ) {
+ add_filter( 'the_content', 'apply_block_hooks_to_content', $priority );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> return $response;
</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">@@ -1320,7 +1358,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @access private
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $hooked_blocks An array of blocks hooked to another given block.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_Block_Template|WP_Post|array $context A block template, template part, `wp_navigation` post object,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param WP_Block_Template|WP_Post|array $context A block template, template part, post object,
</ins><span class="cx" style="display: block; padding: 0 10px"> * or pattern that the blocks belong to.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param callable $callback A function that will be called for each block to generate
</span><span class="cx" style="display: block; padding: 0 10px"> * the markup for a given list of blocks that are hooked to it.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1377,7 +1415,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @access private
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $hooked_blocks An array of blocks hooked to another block.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_Block_Template|WP_Post|array $context A block template, template part, `wp_navigation` post object,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param WP_Block_Template|WP_Post|array $context A block template, template part, post object,
</ins><span class="cx" style="display: block; padding: 0 10px"> * or pattern that the blocks belong to.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param callable $callback A function that will be called for each block to generate
</span><span class="cx" style="display: block; padding: 0 10px"> * the markup for a given list of blocks that are hooked to it.
</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-12-16 23:49:11 UTC (rev 59522)
+++ trunk/src/wp-includes/default-filters.php 2024-12-17 10:35:17 UTC (rev 59523)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -192,6 +192,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'the_title', 'convert_chars' );
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'the_title', 'trim' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+add_filter( 'the_content', 'apply_block_hooks_to_content', 8 ); // BEFORE do_blocks().
</ins><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'the_content', 'do_blocks', 9 );
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'the_content', 'wptexturize' );
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'the_content', 'convert_smilies', 20 );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -760,9 +761,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Update ignoredHookedBlocks postmeta for wp_navigation post type.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+add_filter( 'rest_pre_insert_page', 'update_ignored_hooked_blocks_postmeta' );
+add_filter( 'rest_pre_insert_post', 'update_ignored_hooked_blocks_postmeta' );
</ins><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'rest_pre_insert_wp_navigation', 'update_ignored_hooked_blocks_postmeta' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-// Inject hooked blocks into the wp_navigation post type REST response.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+// Inject hooked blocks into the Posts endpoint REST response for some given post types.
+add_filter( 'rest_prepare_page', 'insert_hooked_blocks_into_rest_response', 10, 2 );
+add_filter( 'rest_prepare_post', 'insert_hooked_blocks_into_rest_response', 10, 2 );
</ins><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'rest_prepare_wp_navigation', 'insert_hooked_blocks_into_rest_response', 10, 2 );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> unset( $filter, $action );
</span></span></pre></div>
<a id="trunktestsphpunittestsblocksapplyBlockHooksToContentphp"></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/applyBlockHooksToContent.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/blocks/applyBlockHooksToContent.php 2024-12-16 23:49:11 UTC (rev 59522)
+++ trunk/tests/phpunit/tests/blocks/applyBlockHooksToContent.php 2024-12-17 10:35:17 UTC (rev 59523)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -92,6 +92,26 @@
</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">+ * @ticket 61074
+ */
+ public function test_apply_block_hooks_to_content_with_context_set_to_null() {
+ $content = '<!-- wp:tests/anchor-block /-->';
+
+ /*
+ * apply_block_hooks_to_content() will fall back to the global $post object (via get_post())
+ * if the $context parameter is null. However, we'd also like to ensure that the function
+ * works as expected even when get_post() returns null.
+ */
+ $this->assertNull( get_post() );
+
+ $actual = apply_block_hooks_to_content( $content, null, 'insert_hooked_blocks' );
+ $this->assertSame(
+ '<!-- wp:tests/anchor-block /--><!-- wp:tests/hooked-block /-->',
+ $actual
+ );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * @ticket 61902
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_apply_block_hooks_to_content_respect_multiple_false() {
</span></span></pre>
</div>
</div>
</body>
</html>