Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Enable first/last child insertion next to Classic block
  • Loading branch information
ockham committed Feb 19, 2025
commit 970eef515bb910bf8c68a1414a8637263a67763b
30 changes: 30 additions & 0 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,25 @@ function apply_block_hooks_to_content_from_post_object( $content, WP_Post $post
return apply_block_hooks_to_content( $content, $post, $callback );
}

/*
* If the content was created using the classic editor or using a single Classic block
* (`core/freeform`), it might not contain any block markup at all.
* However, we still might need to inject hooked blocks in the first child or last child
* positions of the parent block. To be able to apply the Block Hooks algorithm, we wrap
* the content in a `core/freeform` wrapper block.
*/
if ( ! has_blocks( $content ) ) {
$original_content = $content;

$content_wrapped_in_classic_block = get_comment_delimited_block_content(
'core/freeform',
array(),
$content
);

$content = $content_wrapped_in_classic_block;
}

$attributes = array();

// If context is a post object, `ignoredHookedBlocks` information is stored in its post meta.
Expand Down Expand Up @@ -1205,6 +1224,17 @@ function apply_block_hooks_to_content_from_post_object( $content, WP_Post $post
// Finally, we need to remove the temporary wrapper block.
$content = remove_serialized_parent_block( $content );

// If we wrapped the content in a `core/freeform` block, we also need to remove that.
if ( ! empty( $content_wrapped_in_classic_block ) ) {
/*
* We cannot simply use remove_serialized_parent_block() here,
* as that function assumes that the block wrapper is at the top level.
* However, there might now be a hooked block inserted next to it
* (as first or last child of the parent).
*/
$content = str_replace( $content_wrapped_in_classic_block, $original_content, $content );
}

return $content;
}

Expand Down
Loading