Expose serialized template content on WP_Block_Template.#5656
Expose serialized template content on WP_Block_Template.#5656nerrad wants to merge 2 commits intoWordPress:trunkfrom
Conversation
When building the template, initialize WP_Block_Template with the serialized block content so callbacks registered to the `hooked_block_types` filter can access the content.
|
Makes sense, thank you for your contribution! If we do this, should we also drop the |
Consolidates initial read from template file into the `content` property in the `WP_Block_Template` instance removing the need for a separate `$template_content` variable.
|
Yup makes sense 👍🏻 |
|
One thing to be aware of with this change is that it might appear to extenders that it's possible to mutate |
|
Yeah, good point 🤔 Just to make sure, are you still okay if we proceed (with 9eebbbe)? Personally, I'm inclined to say that it's okay to expect an extender not to mess up 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 Anyway, mostly just curious if you think this PR is blocked by any of the above 😄 |
|
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 |
| $template->has_theme_file = true; | ||
| $template->is_custom = true; | ||
| $template->modified = null; | ||
| $template->content = file_get_contents( $template_file['path'] ); |
There was a problem hiding this comment.
One final suggestion: Let's set $template->content right below $template->theme (i.e. on R526 if I'm not mistaken).
That's the position where it originally was before we started porting Block Hooks code to Core, and it's also more consistent with _build_block_template_result_from_post:
wordpress-develop/src/wp-includes/block-template-utils.php
Lines 760 to 762 in 0b8ca16
|
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! 😊 |
|
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
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.