Repository navigation
Closed
Labels
[Feature] BlocksOverall functionality of blocks[Type] IterationScoped iteration of an effort from a tracking issue or overview issue ideally for a major release.
Description
- Part of Block API #41236.
- Supersedes Include Block Hooks in WordPress 6.4 #53987.
Block Hooks have been formerly know as Auto-inserting Blocks.
Plans for WordPress 6.5
- Enable for user-modified templates/parts/patterns
- No toggle shown for hooked blocks added via filter
(Will be be fixed by the enabling Block Hooks for user-modified templates/parts/patterns.)(Edit: No it won't)- Block Hooks: Display toggle for hooked blocks added via filter #59396 added it for modified templates/parts/... at least.
- Block Hooks: Sibling insertion incompatible with constrained layout
- Allow child insertion into Navigation block
- Allow passing block definitions
- Toggle removed if multiple anchor block instances exist, and one hooked block is deleted
- Block Hooks: Respect
allowedBlocksfield and control for users #53989- Originally touched upon here. We might want to give a block some level of control what blocks it allows as its (auto-inserted) children.
- There's an
allowedBlockattribute that's currently used by a number of blocks; AFAICT, it's only handled on the client side, and basically passed through to theInnerBlockscomponent as a prop. I'm a bit hesitant to use something like this as it seems currently really up to each individual block, with no standardization, especially on the server (e.g. in the shape of block-supports). - Implementing this later might amount to an API change: Blocks that were previously auto-inserted might cease to be if their parent disallows them 😕
Other
- Performance: Try refactoring
get_hooked_blocks()to check impact wordpress-develop#5399 - Block Hooks: Update the compatibility layer #58468 (Backport filter mechanism to Gutenberg.)
- Add higher-level test coverage (integration and/or e2e).
- Largely follow "Possible scenarios to cover" section in this comment.
- Add small test plugin that includes an auto-inserting block -- probably based on https://github.com/ockham/like-button.
- Respect template locks.
- I need to read up on them, but I imagine we shouldn't auto-insert into a locked template? 😬
- Respect
"multiple": false.- Blocks can stipulate they only be inserted once. Auto-insertion should probably respect that; @nerrad made a PoC PR during WCUS Contributor Day: Rough POC of respecting "supports multiple" in the auto-inserted block metadata #53925
- Some prior discussion: "It could also be that autoInsert works upon the first encountered block of the specified type, so it doesn't get repeated if there are multiple navigation blocks in a page. Or maybe this is also a flag like
useOnce." - This still doesn't give control over which navigation block that is (but will default to the first one encountered); we might want a mechanism to select that, too. See next item for a somewhat related problem.
- Finally, note that the
multiplefield was originally introduced for a post editor context; it isn't really well-defined in a template/Site Editor, as @gziolo has observed.
- Notify user that there’s a new auto-inserted block available (after activating a new plugin that includes that block).
- In the editor, or even wp-admin? Needs design.
- How it could be implemented: Make list of registered blocks before and after plugin activation, compare; see if newly added
block.jsonhasautoInsertfield? (Doesn't cover blocks that are auto-inserted programmatically.)
Reacted by Greg Ziółkowski, Aurooba Ahmed, Mario Santos and Tom Cafferkey
Metadata
Metadata
Assignees
Labels
[Feature] BlocksOverall functionality of blocks[Type] IterationScoped iteration of an effort from a tracking issue or overview issue ideally for a major release.
Activity
gziolo commentedon Sep 28, 2023
One more item to consider would be to potentially iterate over the Plugins section UI in the sidebar panel based on the comments about different expectations from folks that were testing it so far: #53987 (comment), #53987 (comment) and #53987 (comment). I expect it will come back regardless after the call for testing when more people have a chance to play with the integration in the editor.
ockham commentedon Oct 9, 2023
Update:
It was brought to @gziolo's and my attention that the performance of patterns is currently lacking in WP 6.4 (Beta), which is particularly apparent when using TT4. @dmsnell found that
traverse_and_serialize_block(s)might be a likely culprit, so @gziolo and I looked into potential ways to improve performance. We ended up filing three candidate PRs:get_hooked_blocks()to check impact wordpress-develop#5399All of them have since been benchmarked and/or profiled, and only one of them turned out to deliver actual improvements.
We'll thus land WordPress/wordpress-develop#5399 in time for Beta 3; there's also WordPress/wordpress-develop#5421 which might improve performance some more (but still needs verifying).
Other than than, @ndiego has done some preliminary testing of Block Hooks; we might try to make some further improvements (or at least file some issues/tickets) based on his results.
fabiankaegy commentedon Oct 11, 2023
Hey all 👋
I was just trying to create a demo of Block Hooks and it leaves me wondering whether I'm missing something 🤔
As far as I can tell from the documentation we can only define a block type where the additional elements should get injected. Is this correct?
I'm asking because I think that may be a pretty large limitation of the API currently. I had though this would work a bit more like CSS selectors where I could "query" for a block insertion point via a more complex selector like
core/post-template core/post-authorThe issue I am running into is that in a block based theme all the available blocks are used for so manny different things that it's almost impossible to make a blanked statement that a new block should be inserted after every author block for example. Because taking the example of the post author block it may be used very differently on a single post template compared to an archve template that renders query loops with the author of each post displayed within.
This is even more true when thinking about more generalized blocks such as the
core/groupetc.Another thing I was hoping for is to target specific variations of a block. For example if one has a custom variation of a product query it would be really cool to say append a block to the post template in the product query variation of the query loop.
Thanks in advance for any response :) And I get that this is the first MVP version of this. I just want to make sure I'm not missing something :)
fabiankaegy commentedon Oct 11, 2023
Thinking about this more, I think one of the original goals of this was to make it possible for plugins like WooCommerce to automatically inject their "Mini Cart" block into the main navigation of the site.
However, if I get this right currently, that wouldn't be possible because all you could do is say add it to all navigation menus. But since most themes have way more than one menu that doesn't seem feasible 🤔
colorful-tones commentedon Oct 11, 2023
@fabiankaegy I'm not sure I understand this statement, which makes me wonder if I'm missing a larger Block Hooks understanding/requirement for implementation altogether.
I'm currently trying to test an ACF Block by just dropping in the
blockHooksand adding in any core block to reference. Is this an unexpected use case?I'm using
register_block_type()oninitand theblock.jsonis:I'm running WP 6.4 Beta 3 through WordPress Beta Tester plugin on Local and I have Gutenberg plugin installed and activated. The Like Button plugin's block shows fine. However, my block does not display at all.
What am I doing wrong? 🤷
fabiankaegy commentedon Oct 11, 2023
@colorful-tones What I mean by that is that the API currently only supports this:
{ "blockHooks": { "core/navigation": "lastChild" } }and not
{ "blockHooks": { // core/template-part/header meaning the header variation of the template part block "core/template-part/header core/navigation": "lastChild" } }colorful-tones commentedon Oct 11, 2023
@fabiankaegy I see what you mean and that makes sense. I did not expect that level of explicitness for this first iteration of this new API, but I can certainly see how I would want it to get there quickly 😄
colorful-tones commentedon Oct 11, 2023
I've just tried
npx @wordpress/create-block@latest copyright --variant dynamicand added the following to theblock.jsonand this appeared to work fine. So, it is likely something to do with ACF's rendering. I'll try to coordinate with the ACF team on what might be happening. Thanks!lgladdy commentedon Oct 11, 2023
ACF Blocks requires a block's
savefunction to be called at least once for the block to work.In the case of a ACF Block being added via a block hook, unless the template is edited and saved, that never happens - so we abort rendering because we can't figure out the block's type or data. If you make any changes to the template,
saveis fired, so it will work.This issue is likely specific to ACF Blocks (although, there definitely could be other blocks that setup some data to the block comment on
savewhich will also cause issues when hooked).We'll work around this by inheriting more defaults from
$this'sWP_Blockinstance passed into the block renderer if they're not set in the block's attributes.ockham commentedon Oct 15, 2023
Update:
After landing WordPress/wordpress-develop#5399 (which sought to improve Block Hooks performance, see #54904 (comment)), it was unfortunately discovered that that change broke template parts. We managed to file and land a fix just in time for Beta 3; however, that fix also undid most of the original performance gains, as was found by @felixarntz 😕
After some discussion with Felix, we then filed a two-part fix seeking to avoid
themeattribute injection on the frontend altogether, in order to improve performance at least a bit. We landed both changes on Thursday (the GB fix also necessitated a package sync) in order to have a bit more time to discover potential regressions and/or side effects before RC1.ockham commentedon Oct 15, 2023
@fabiankaegy @colorful-tones @lgladdy -- Apologies for not engaging in the conversation, I was a bit busy with the aforementioned issue last week. I won't be working tomorrow but hope to catch up with y'all on Tuesday!
colorful-tones commentedon Oct 16, 2023
@ockham I appreciate the update. No apologies necessary. You’re obviously busy and I hope you enjoy your day off. ❤️
ockham commentedon Oct 18, 2023
We discussed this a bit more elsewhere, but since that issue isn't (only) about Block Hooks, I wanted to bring back that conversation here.
FWIW, I succeeded having a Mini Cart block inserted after the Navigation block. The containing Header template part doesn't have any modifications; it seems like the Navigation block falls back to a list of select pages (although not a "complete" page list; possibly a menu created by WooCommerce during setup?)
18 remaining items
ockham commentedon Jan 29, 2024
Update:
It was a pretty productive week for Block Hooks:
hooked_block_{$block_type}filter PR was also approved, so I committed it to Core.This is nicely reflected by a number of now-ticked checkboxes in the issue's TODO list 😄
As we're approaching the cut-off date for GB changes to be included in WP 6.5, my focus this week will be on finally unblocking WordPress/wordpress-develop#5726 (see).
@swissspidy suggested adding a non-dynamic version of the
hooked_block_{$block_type}filter (i.e. justhooked_block), which I'll also look into.Finally, @jsnajdr was kind enough to file a PR to introduce an
allowedBlocksfield inblock.json, which would unblock #53989, a potential stretch goal for WP 6.5.ockham commentedon Feb 5, 2024
Update:
The last week saw a bit more of experimentation, with four new PRs opened, only half of which are probably worth pursuing in the short term.
I spent some time last week trying to update GB's Block Hooks compatibility layer for WP < 6.4, which I thought might be a requirement to also add a compat layer for WP < 6.5. While it looks like I got that to work, it also turned out to be a distraction ❌
I then also tried to make hooked blocks insertion into the Navigation block work when used on WP 6.4 (rather than
trunk), as I wanted to highlight this new feature in the release notes for GB 17.6, and it seemed too strong a limitation if people had to run it against WPtrunk. The approach I chose turned out to be a dead end ❌I then finally started working on setting the
ignoredHookedBlocksattribute upon insertion of a new block. This is shaping up nicely; one problem I found is while there we have a way of settinginitialAttributesfor blocks inserted from the inserter, that mechanism doesn't apply to a container block's inner blocks, so things get a bit less elegant there 🚧While working on this, I discovered that we the client really needs a way to ask the server for a list of hooked blocks for a given anchor block in a given context (e.g. within a Single template, or a header area template part). I spent a bit of time thinking what a REST API endpoint to that effect would look like and then started working on one 🚧
GB 17.7 RC 1 will be published on Wednesday, which is the last GB version whose code will be included in WP 6.5 by default. My plan is to finish work on
ignoredHookedBlocksattribute insertion into new blocks today, and on thehooked-blocksendpoint tomorrow (as the latter can be seen as a refinement). This should then allow us to finally unblock hooked blocks insertion into modified templates/parts/patterns.fabiankaegy commentedon Feb 12, 2024
Now that we are past the Beta 1 cut off point I'm removing this tracking issue from the 6.5 Project board. From now on the board should only contain individual bugs that we want to still fix before 6.5 gets released.
ockham commentedon Feb 19, 2024
Update:
Near the beginning of the past two-week period, @tjcafferkey found and subsequently fixed a bug that had hooked blocks inserted twice into the Navigation block when running the GB plugin with WP
trunk.The focus of the last two weeks was resolving the mismatch between editor and frontend for newly inserted anchor blocks in order to unblock injection of hooked blocks into modified templates (WordPress/wordpress-develop#5726).
To that end, @tjcafferkey and I continued work on the client-side PR to set the
ignoredHookedBlocksattribute for a newly inserted block, and thehooked-blockscontroller that would inform the client which blocks were hooked to a given anchor blocks (including those added via a filter).We managed to merge the client-side PR, which was good enough to unblock hooked blocks injection into modified templates, allowing me to finally land WordPress/wordpress-develop#5726 🎉
However, it became apparent that we wouldn't be able to land the controller in time for WP 6.5's Feature Freeze. We were considering to let Core committers know about the issue and potentially seek "task (blessed)" status for it in order to continue work on it after Beta 1.
That was until last Monday when @gziolo suggested an alternative approach, which turned out much better in a number of ways (didn’t require creating a new controller, kept logic contained on the server side). I managed to get a basic PR up and running by Monday evening, and thanks to Tom’s help, review, and testing, I was able to land it just ahead of Beta 1 🙂
I also reverted the client-side PR from our previous approach that we’d already merged, since we’re not going to need that now.
There's a bit of follow-up work needed now that we've made some changes to how the
ignoredHookedBlocksattribute is set, chiefly with regard to the Navigation block, for which we already have a WIP fix. There were a few other suggestions by folks that might be too late for 6.5, but I'll file tickets and/or PRs regardless (e.g. allowing returningnullfromhooked_block_{$block_type}filter, or adding a generichooked_blockversion of that filter). Other than that, I'll stand by for bugfixes, and will start work on a Dev Note, time permitting.ockham commentedon Feb 26, 2024
Update:
We landed the fix for the Navigation block. I then filed tickets and PRs for the feature requests I'd gotten recently:
hooked_blockfilter wordpress-develop#6136 (ticket)Both were fortunately accepted for 6.5 and have already been committed 🎉
Since WordPress/wordpress-develop#6136 changed the signature of the
hooked_block_{$block_type}filter, I also released a new version of the Like Button block plugin that I use for testing Block Hooks.Tom then kindly filed a fix for a bug that was reported two weeks ago.
With WP 6.5 Beta 3 scheduled for tomorrow, I'll prioritize reviewing and landing the bugfix. That aside, I have to start writing the Dev Note for Block Hooks in 6.5. I'll also stand by for other bugs that might crop up.
ockham commentedon Mar 4, 2024
Update:
I filed and merged a fix to display the Block Hooks toggle based on the presence of the
ignoredHookedBlocksmetadata attribute. I then reviewed @tjcafferkey’s fix for the Block Hooks toggle when used to insert into the Nav block and decided to go with a simpler alternative (that I had originally advised against) after all. Finally, I filed another small PR to add a short description to the Plugins (i.e. Block Hooks) panel in the block inspector.I also published the Dev Note for Block Hooks in WP 6.5.
I was alerted to two issues that seem to have been caused by our recent changes to Block Hooks and will need to investigate them: #59516 and https://core.trac.wordpress.org/ticket/60671.
ockham commentedon Mar 11, 2024
Update:
Our major focus last week was on fixing two issues with garbled entities that were both caused by changes to Block Hooks (one of them affecting the Navigation block, the other one affecting template parts). We managed to fix them both in a similar way, by using a filter instead of an action in order to avoid an additional
wp_update_postcall (which turned out to be the problem): See #59561 and WordPress/wordpress-develop#6225 for the respective fixes.The fixes are on track for inclusion in RC2, pending a package sync for #59561, and a backport from Core
trunkto the6.5branch for WordPress/wordpress-develop#6225. I'll take care of the latter today.There's a minor follow-up work in WordPress/wordpress-develop#6244 that I'll also backport to 6.5. Other than that, I'll keep my eyes open for any other potential bugs. I might also start working/helping with some Block Hooks related documentation.
ockham commentedon Mar 18, 2024
Update:
Last week, I realized that as a side effect of using the
rest_pre_insert_{$this->post_type}filter instead of therest_after_insert_{$this->post_type}action, we were passing the wrong$contextto Block Hooks filters. I filed a ticket, and we started working on a fix (which turned out to be a lot of work).Additionally, it was reported that creating a new Navigation post object via the REST API didn't work; this was fixed by @tjcafferkey.
We've started filing a few issues to cover some of the changes we'd like to make to Block Hooks after 6.5, namely:
#60756: Toggle (re-)inserts hooked block in wrong position (if added by filter)#60759: Harmonize ignoredHookedBlocks metadata injection logic#60769: Consolidate approach to get the list of hooked blocksWith RC3 scheduled for tomorrow, we're hoping to land the fix for the wrong
$contextin time. Time permitting, we might continue to file tickets and issues for Block Hooks past 6.5.gziolo commentedon Mar 18, 2024
Do you plan to open a new epic for WP 6.6, or should we put these items in a new section under Block API overview issue?
annezazu commentedon Mar 21, 2024
Please create a new epic :D <3 and let's please close this one out when ready!
ockham commentedon Mar 27, 2024
Closing in favor of #60252.
I've carried over the open issues and tickets from this issue and applied some preliminary gardening. I'll continue to do so over the next couple of days.
@leewillis77 Apologies for not replying to your comment, I must've somehow missed it. I'll reply shortly over on the new issue!