#59882 closed defect (bug) (fixed)
Expose serialized template content to callbacks registered to the `hooked_block_types` filter.
| Reported by: | nerrad | Owned by: | Bernhard Reiter |
|---|---|---|---|
| Priority: | normal | Milestone: | 6.4.2 |
| Component: | Editor | Version: | |
| Severity: | normal | Keywords: | has-patch fixed-major commit |
| Cc: | Focuses: | template |
Description
The recently introduced Block Hooks API exposes a filter (hooked_block_types) which exposes $context as an argument. When a โtemplate file is built, WP_Block_Template::content โis not exposed to the callbacks registered on hooked_block_types.
This would be useful for callbacks that might want to detect the presence of a serialized block instance (or potentially in the future utilize the HTML API) to restrict where the block is injected (before the template is rendered).
Addressing this would also achieve parity with the structure of $context when it represents a pattern (where pattern serialized content is present).
Change History (14)
This ticket was mentioned in โPR #5656 on โWordPress/wordpress-develop by โnerrad.
3 years ago
#1
- Keywords has-patch added; needs-patch removed
โ@Bernhard Reiter commented on โPR #5656:
3 years ago
#2
Makes sense, thank you for your contribution!
If we do this, should we also drop the $template_content variable altogether? It's currently โparsed for the actual hooked blocks insertion; it might be less confusing for people if we changed that to also used $template->content.
โnerrad commented on โPR #5656:
3 years ago
#3
Yup makes sense ๐๐ป
โnerrad commented on โPR #5656:
3 years ago
#4
One thing to be aware of with this change is that it might appear to extenders that it's possible to mutate $template->content. The blocks are already parsed from content by this point but one extension mutating the property could mess it up for every other callback utilizing block_hooks and checking this property (the content wouldn't match the rendered blocks). Ideally, $template->content would be a private property exposed via getter so that extenders can't mutate it.
โ@Bernhard Reiter commented on โPR #5656:
3 years ago
#5
Yeah, good point ๐ค
Just to make sure, are you still okay if we proceed (with โhttps://github.com/WordPress/wordpress-develop/pull/5656/commits/9eebbbed451cd63ac52f3a04ad52dfd5df6a3b20)? Personally, I'm inclined to say that it's okay to expect an extender _not_ to mess up $template->content, or at least I don't think it's a blocker.
Also agree that a getter would be nice to have for the template content, but OTOH, we're also โoverriding it with new markup returned from `traverse_and_serialize_blocks` (with hooked blocks inserted) so I'm not sure we can make the content property private, as โPHP doesn't seem to have a `friend` concept like C++ does ๐ค
Anyway, mostly just curious if you think this PR is blocked by any of the above ๐
โnerrad commented on โPR #5656:
3 years ago
#6
Yup, I'm okay with the latest commit. I just wanted to flag it as a potential issue to get further thoughts. The only way (I can currently think of) that we'd be able to lock this down is to require a new object constructed and use something the โvalue object pattern to keep WP_Block_Template immutable. Practically, I'm not sure how big of a deal that is.
โ@Bernhard Reiter commented on โPR #5656:
3 years ago
#10
I've taken the liberty of โmoving the line and committed this change to Core: https://core.trac.wordpress.org/changeset/57118.
Hope that's okay! ๐
#11
@
3 years ago
- Keywords fixed-major commit added
- Resolution fixed
- Status closed โ reopened
Reopening for backporting to the 6.4 branch.
โ@Bernhard Reiter commented on โPR #5656:
3 years ago
#13
Backported to the 6.4 branch in https://core.trac.wordpress.org/changeset/57119.
When building the template, initialize WP_Block_Template with the serialized block content so callbacks registered to the
hooked_block_typesfilter can access the content.If you registered a callback to
hooked_block_typeswhen the callback receives an instance of a template or template_part via the$contextargument, the$contentproperty will be null. Minimally reproducible example:In the above example, the code is checking to see if the
$contextis an instance ofWP_Block_Templateand if it is, looks in the$contentproperty for the serialized representation of thecore/post-titleblock. If it's present, then bail early and don't inject navigation. For the pages template, I'd _expect_ this to not injectcore/navigationbecause thecore/post-titleblock is present in the content, but in fact, it does get injected becauseWP_Block_Template::$content === null.Screenshots:
Trac ticket: https://core.trac.wordpress.org/ticket/59882