From dfdd84fc2e948f2bf544c7614ed87f49e84ebd60 Mon Sep 17 00:00:00 2001 From: Aaron Jorbin Date: Fri, 25 Apr 2025 19:23:28 +0000 Subject: [PATCH] Block Hooks: Suppress insertion next to post content wrapper block. As of [59523], Block Hooks are applied to post content. In order to allow for insertion of a hooked block as first_child or last_child of the containing Post Content block, we wrap the block's post content (as obtained from the DB) in a temporary wrapper block, apply the Block Hooks algorithm to the resulting markup, and remove the wrapper block. (The same technique is applied for the Synced Pattern block -- see [59543] -- as well as the Navigation block.) However, this caused a problem when a hooked block was marked for insertion before before or after a Post Content block: The logic that's supposed to remove the temporary wrapper block after the Block Hooks algorithm runs erroneously removed that hooked block's delimiter instead of the wrapper block, producing garbled markup as a result. This changeset fixes the issue by adding a hooked_block_types filter (with PHP_INT_MAX priority) that removes any blocks hooked before or after a Post Content block, if the current context is a post object. This prevents any blocks hooked that way from being "absorbed" into the corresponding post object's content; it retains the ability to hook blocks before and after a Post Content block in any other context (e.g. a template). (The same principle is applied to Synced Pattern and Navigation blocks.) Reviewed by Jorbin. Merges [60173] to 6.8 branch. Props obenland, jorbin, gziolo, bernhard-reiter. Fixes #63287. Built from https://develop.svn.wordpress.org/branches/6.8@60189 git-svn-id: http://core.svn.wordpress.org/branches/6.8@59525 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/blocks.php | 17 +++++++++++++++++ wp-includes/version.php | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/wp-includes/blocks.php b/wp-includes/blocks.php index 31f1b22469..838cd84a19 100644 --- a/wp-includes/blocks.php +++ b/wp-includes/blocks.php @@ -1248,8 +1248,25 @@ function apply_block_hooks_to_content_from_post_object( $content, $post = null, $content ); + /* + * We need to avoid inserting any blocks hooked into the `before` and `after` positions + * of the temporary wrapper block that we create to wrap the content. + * See https://core.trac.wordpress.org/ticket/63287 for more details. + */ + $suppress_blocks_from_insertion_before_and_after_wrapper_block = static function ( $hooked_block_types, $relative_position, $anchor_block_type ) use ( $wrapper_block_type ) { + if ( + $wrapper_block_type === $anchor_block_type && + in_array( $relative_position, array( 'before', 'after' ), true ) + ) { + return array(); + } + return $hooked_block_types; + }; + // Apply Block Hooks. + add_filter( 'hooked_block_types', $suppress_blocks_from_insertion_before_and_after_wrapper_block, PHP_INT_MAX, 3 ); $content = apply_block_hooks_to_content( $content, $post, $callback ); + remove_filter( 'hooked_block_types', $suppress_blocks_from_insertion_before_and_after_wrapper_block, PHP_INT_MAX ); // Finally, we need to remove the temporary wrapper block. $content = remove_serialized_parent_block( $content ); diff --git a/wp-includes/version.php b/wp-includes/version.php index b500e4d430..bc32483e8f 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.8.1-alpha-60188'; +$wp_version = '6.8.1-alpha-60189'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.