Commit caf8d34db8 for wordpress.org

commit caf8d34db8952acfd66c13f052e4c9d4704a3898
Author: Bernhard Reiter <Bernhard Reiter@git.wordpress.org>
Date:   Fri Apr 18 12:57:28 2025 +0000

    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 `<!-- wp:post-content -->` 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.)

    Props obenland, jorbin, gziolo, bernhard-reiter.
    Fixes #63287.
    Built from https://develop.svn.wordpress.org/trunk@60173


    git-svn-id: http://core.svn.wordpress.org/trunk@59509 1a063a9b-81f0-0310-95a4-ce76da25c4cd

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 9d5636adf3..b97decea26 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
  *
  * @global string $wp_version
  */
-$wp_version = '6.9-alpha-60172';
+$wp_version = '6.9-alpha-60173';

 /**
  * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.