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 API: Update block-template-utils for first-last child template parts #6867

Open
wants to merge 12 commits into
base: trunk
Choose a base branch
from

Conversation

tjcafferkey
Copy link

@tjcafferkey tjcafferkey commented Jun 20, 2024

Currently you are unable to insert hooked blocks in first_child and last_child positions of template parts. This is because, similarly to the Navigation block the anchor block (core/navigation / core/template-part) and its children are detached in the markup from one and other.

To solve this issue we have approached it in a similar way by mocking the anchor block in such cases for the purpose of running the block hooks algorithm against it and extracting any related metadata (e.g. ignoredHookedBlocks) which are then stored in postmeta.

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


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.

Testing instructions

  1. Use both methods below to hook the core/loginout block as a first_child into template parts.
  2. Ensure it renders as expected for unmodified templates.
  3. Move/remove the block from the template part and ensure its not readded.
  4. Remove the _wp_ignored_hooked_blocks data from wp_postmeta from your site and perform steps 1-3 again for modified templates.

functions.php

function register_logout_block_as_hooked_block( $hooked_blocks, $position, $anchor_block, $context ) {
	if ( 'core/template-part' === $anchor_block && 'first_child' === $position ) {
		$hooked_blocks[] = 'core/loginout';
	}

	return $hooked_blocks;
}

add_filter( 'hooked_block_types', 'register_logout_block_as_hooked_block', 10, 4 );

block.json

"blockHooks": {
    "core/template-part": "lastChild"
}

@tjcafferkey tjcafferkey changed the title Block Hooks API: Allow first_child last_child hooked blocks into template parts. Block Hooks API: Update block-template-utils for first-last child template parts Jun 20, 2024
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.

@ockham
Copy link
Contributor

ockham commented Jun 24, 2024

This is looking good! Love to see the re-use of update_ignored_hooked_blocks_postmeta 😄

Is it possible to get away with even fewer changes (to block-template-utils.php, specifically) if we hook update_ignored_hooked_blocks_postmeta into rest_pre_insert_wp_template_part (like we already do for wp_navigation posts)?

@tjcafferkey
Copy link
Author

Is it possible to get away with even fewer changes (to block-template-utils.php, specifically) if we hook update_ignored_hooked_blocks_postmeta into rest_pre_insert_wp_template_part (like we already do for wp_navigation posts)?

As it stands using that filter in the templates controller only gives us the following information; post_name, ID, post_status and post_content so we don't know if its a wp_template or wp_template_part since we don't have the post type there.

I think we have two options to fix that:

  1. Update the controller to make sure it passes a post_type along.
  2. Use get_post() in the function if no post type is present.

@tjcafferkey
Copy link
Author

tjcafferkey commented Jun 24, 2024

Having looked at it again. If the template already has a database record we get the ID but not post_type but if the template has no database record (ie if this is the first time its being saved) then we get post_type but no ID which we cant store metadata without.

@ockham
Copy link
Contributor

ockham commented Jun 24, 2024

Is it possible to get away with even fewer changes (to block-template-utils.php, specifically) if we hook update_ignored_hooked_blocks_postmeta into rest_pre_insert_wp_template_part (like we already do for wp_navigation posts)?

As it stands using that filter in the templates controller only gives us the following information; post_name, ID, post_status and post_content so we don't know if its a wp_template or wp_template_part since we don't have the post type there.

Is that really a problem though? Can't we implicitly assume wp_template_part, since we'll be only hooking to rest_pre_insert_wp_template_part (but not to rest_pre_insert_wp_template)? 🤔

@ockham
Copy link
Contributor

ockham commented Jun 24, 2024

Another thing I realized is that on a higher level, update_ignored_hooked_blocks_postmeta only updates the ignoredHookedBlocks metadata (as the function name states 😅 ); it doesn’t actually take care of block insertion (neither in the editor, nor on the frontend). So those parts would still be missing.

For the editor (i.e. the REST API), we might be able to use insert_hooked_blocks_into_rest_response (once again like we do for wp_navigation). The frontend is a bit trickier though.

@tjcafferkey
Copy link
Author

So those parts would still be missing

Unless I'm misunderstanding what you're saying here. Those pieces are covered by the updates I made to _build_block_template... functions since these functions are used both when building the template for the REST response and the frontend. Are you suggesting I find a different way to insert the hooked blocks?

@ockham
Copy link
Contributor

ockham commented Jun 25, 2024

So those parts would still be missing

Unless I'm misunderstanding what you're saying here. Those pieces are covered by the updates I made to _build_block_template... functions since these functions are used both when building the template for the REST response and the frontend. Are you suggesting I find a different way to insert the hooked blocks?

No, that's fine! I guess I was hoping to unify first/last child insertion into wp_navigation and wp_template_part posts even more but got my wires crossed 😅

@ockham
Copy link
Contributor

ockham commented Jun 26, 2024

I've just committed #6885 to Core, so we can rebase this now. (We'll probably need to change the remaining assertions that are using get_post_meta() to get _wp_ignored_hooked_blocks to $post->meta_input['_wp_ignored_hooked_blocks'], like you did in #6885.)

@tjcafferkey tjcafferkey force-pushed the update/allow-hooked-blocks-first-last-child-into-template-part branch from bc236c9 to aaea68d Compare June 26, 2024 09:33
@tjcafferkey tjcafferkey marked this pull request as ready for review June 28, 2024 09:40
Copy link

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 tomjcafferkey, bernhard-reiter.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants