Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Hooks: Add apply_block_hooks_to_content_from_post_object #8212

Closed

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Jan 29, 2025

Port of WordPress/gutenberg#68926 and WordPress/gutenberg#69241.

introduc[e] a new function, apply_block_hooks_to_content_from_post_object, to colocate the logic used to temporarily wrap content in a parent block (with ignoredHookedBlocks information fetched from post meta) alongside the call to apply_block_hooks_to_content.

Allow insertion of a hooked block as the first or last child of the Post Content block (and, less importantly, of the Synced Pattern and Navigation blocks).

Fixes #61074, #62716.

Trac ticket: https://core.trac.wordpress.org/ticket/62716


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@ockham ockham self-assigned this Jan 29, 2025
Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@Mamaduka
Copy link
Member

Adding a unit test for unhappy paths might be a good idea. When post_content returns something different due to the filter.

@Mamaduka
Copy link
Member

Mamaduka commented Feb 4, 2025

@ockham, is this ready for review?

@ockham
Copy link
Contributor Author

ockham commented Feb 5, 2025

@ockham, is this ready for review?

Not quite yet -- I still need to increase test coverage, and audit other callsites of apply_block_hooks_to_content to see if they should be changed to call apply_block_hooks_to_content_from_post_object instead.

I won't be able to work on this PR this week, but I'll continue work next week. I hope that works for you time-wise, @Mamaduka?

@Mamaduka
Copy link
Member

Mamaduka commented Feb 5, 2025

Thanks, @ockham!

That timeline should be okay for now. We just need to make sure this is merged before Beta 1.

@ockham ockham force-pushed the fix/remove-serialized-parent-block branch from eafd22b to 7c9240f Compare February 17, 2025 22:06
@ockham ockham marked this pull request as ready for review February 18, 2025 13:05
@ockham ockham requested a review from gziolo February 18, 2025 13:05
Copy link

github-actions bot commented Feb 18, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props bernhard-reiter, gziolo, mamaduka.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@ockham ockham marked this pull request as draft February 18, 2025 14:20
@ockham
Copy link
Contributor Author

ockham commented Feb 18, 2025

Switching back to draft mode since I might've encountered a bug while testing.

Edit: Figured it out, see below.

@ockham
Copy link
Contributor Author

ockham commented Feb 18, 2025

Note that when testing this with a block theme (e.g. TT5), hooked blocks in the first_child or last_child positions of the Post Content block will be inserted twice. E.g. when using this plugin for testing:

image

This problem will go away once changes to the Post Content block that were made in WordPress/gutenberg#68926 are synced over to Core.

You can simulate this by applying this patch.
diff --git a/src/wp-includes/blocks/post-content.php b/src/wp-includes/blocks/post-content.php
index e0a06b7217..25be880cc4 100644
--- a/src/wp-includes/blocks/post-content.php
+++ b/src/wp-includes/blocks/post-content.php
@@ -46,33 +46,10 @@ function render_block_core_post_content( $attributes, $content, $block ) {
 		$content .= wp_link_pages( array( 'echo' => 0 ) );
 	}
 
-	$ignored_hooked_blocks = get_post_meta( $post_id, '_wp_ignored_hooked_blocks', true );
-	if ( ! empty( $ignored_hooked_blocks ) ) {
-		$ignored_hooked_blocks  = json_decode( $ignored_hooked_blocks, true );
-		$attributes['metadata'] = array(
-			'ignoredHookedBlocks' => $ignored_hooked_blocks,
-		);
-	}
-
-	// Wrap in Post Content block so the Block Hooks algorithm can insert blocks
-	// that are hooked as first or last child of `core/post-content`.
-	$content = get_comment_delimited_block_content(
-		'core/post-content',
-		$attributes,
-		$content
-	);
-
-	// We need to remove the `core/post-content` block wrapper after the Block Hooks algorithm,
-	// but before `do_blocks` runs, as it would otherwise attempt to render the same block again --
-	// thus recursing infinitely.
-	add_filter( 'the_content', 'remove_serialized_parent_block', 8 );
-
 	/** This filter is documented in wp-includes/post-template.php */
 	$content = apply_filters( 'the_content', str_replace( ']]>', ']]>', $content ) );
 	unset( $seen_ids[ $post_id ] );
 
-	remove_filter( 'the_content', 'remove_serialized_parent_block', 8 );
-
 	if ( empty( $content ) ) {
 		return '';
 	}

image

@ockham ockham marked this pull request as ready for review February 18, 2025 15:03
@ockham
Copy link
Contributor Author

ockham commented Feb 19, 2025

Heads-up, I'm thinking to include changes from WordPress/gutenberg#69241 in this. (Should take a couple of minutes only.)

@ockham
Copy link
Contributor Author

ockham commented Feb 19, 2025

Heads-up, I'm thinking to include changes from WordPress/gutenberg#69241 in this. (Should take a couple of minutes only.)

Done: 970eef5

Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started with reviewing and testing WordPress/gutenberg#69241. In effect, I gained confidence these changes can be safely included here as well. The changes mirror the edits applied in the Gutenberg plugin and they all look good to me.

Copy link

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 59838
GitHub commit: d455334

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions bot closed this Feb 19, 2025
@ockham ockham deleted the fix/remove-serialized-parent-block branch February 19, 2025 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants