-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Expose serialized template content on WP_Block_Template. #5656
Expose serialized template content on WP_Block_Template. #5656
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 |
@@ -532,6 +531,7 @@ function _build_block_template_result_from_file( $template_file, $template_type | |||
$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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
$template->theme = $theme; | |
$template->content = $post->post_content; | |
$template->slug = $post->post_name; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One final note; otherwise LGTM!
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_types
filter can access the content.If you registered a callback to
hooked_block_types
when the callback receives an instance of a template or template_part via the$context
argument, the$content
property will be null. Minimally reproducible example:In the above example, the code is checking to see if the
$context
is an instance ofWP_Block_Template
and if it is, looks in the$content
property for the serialized representation of thecore/post-title
block. If it's present, then bail early and don't inject navigation. For the pages template, I'd expect this to not injectcore/navigation
because thecore/post-title
block 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.