Block Hooks: Suppress insertion of hooked blocks in before/after position into post object content#8700
Conversation
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
We should probably check what happens if the user modifies a template in the Site Editor that contains a Post Content block that has a block hooked |
|
There are some reported coding style issues. Otherwise, all tests pass including newly added coverage for the advanced use case of using |
There was a problem hiding this comment.
Copilot wasn't able to review any files in this pull request.
Files not reviewed (3)
- src/wp-includes/blocks.php: Language not supported
- tests/phpunit/tests/blocks/applyBlockHooksToContent.php: Language not supported
- tests/phpunit/tests/blocks/applyBlockHooksToContentFromPostObject.php: Language not supported
As of WordPress 6.8, Block Hooks are also applied to post content. In order to allow for first/last child insertion of a hooked block into the Post Content block, we wrap the block's post content (as obtained from the DB) in a temporary
<!-- wp:post-content -->wrapper block, run theapply_block_hooks_to_content_from_post_objectfunction on the resulting markup, and remove the wrapper block.In
#63287, @obenland reported that there was a problem when using Block Hooks to insert a block before or after a Post Content block. See that ticket for more details about how the bug manifested itself.After some thought, I believe that we need to exempt hooked blocks that are meant to be inserted
beforeoraftera Post Content block from theapply_block_hooks_to_content_from_post_objectalgorithm when it is run on a post object's content wrapped in a temporary wrapper block. Arguably, a block that is supposed to be before or after a given block should not be absorbed into the latter's content; instead, it should remain outside. (This is in contrast to first/last child insertion, where it makes sense to absorb the child block inside of the anchor block.)(Instead, if a block is supposed to be inserted before or after a Post Content block, it should be the job of the containing markup entity -- typically a template -- to absorb it.)
The present PR implements this idea by adding logic to a pre-existing filter that is run with "latest" priority on the
hooked_block_typeshook. Specifically, that logic checks if the given context is a post object, and if so, verifies that the anchor block matches the post object's corresponding block type. (This means that it isn't limited to the Post Content block, but also applies to the Synced Pattern and Navigation blocks.)Includes test coverage for the following scenarios:
#63287.hooked_block_typesfilter. 6e12f44Props @obenland @aaronjorbin @gziolo.
Trac ticket: https://core.trac.wordpress.org/ticket/63287
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.