-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Full Site Editing: Add a Template Part block #32581
Conversation
This PR does not affect the size of JS and CSS bundles shipped to the user's browser. Generated by performance advisor bot at iscalypsofastyet.com. |
f6d8ab7
to
0c206af
Compare
c0680e1
to
e78ec79
Compare
0c206af
to
965fdb4
Compare
e78ec79
to
03c2638
Compare
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.
Looks solid overall - left a few small change requests
} ); | ||
}; | ||
|
||
const edit = withState( { |
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.
It's conventional in core for a block's edit
method to live in its own file. We should probably try to follow those when needed for future core contribution possibilities.
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.
This is a good point.
I'll also update the Content Preview block accordingly.
if ( ! $attributes['selectedPostId'] || ! is_int( $attributes['selectedPostId'] ) ) { | ||
return; | ||
} | ||
$align = $attributes['align'] ? ' align' . $attributes['selectedPostId'] : ''; |
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.
This needs an isset
check otherwise we get undefined index warnings
$align = $attributes['align'] ? ' align' . $attributes['selectedPostId'] : ''; | ||
$post = get_post( $attributes['selectedPostId'] ); | ||
setup_postdata( $post ); | ||
$content = '<div class="a8c-content-renderer'. $align . '">' . get_the_content() . '</div>'; |
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.
Need to do apply_filters( 'the_content', get_the_content() )
so we get do_blocks
and other expected formatting. Try rendering a post with a dynamic block (eg Latest Posts) in the post you're rendering and it won't render right now.
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.
TIL!
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.
Damn this has also fixed an error I was struggling to figure out.
Post X contains a renderer rendering post Y which contains another renderer for post Z.
On the front-end, post X shows Y correctly, but not Z.
This was because I wasn't do_blocks
-ing Y!
TWISTED! 🧶
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.
To extend this a bit further, we can also create circular dependencies now. For example: Post X contains a renderer for Post Y, and Post Y contains a rendered for Post X. When you try to save this the updating will fail, and if you try to reload the editor it will get into an infinite loop (Allowed memory size of 268435456 bytes exhausted (tried to allocate 262144 bytes) in /var/www/html/wp-includes/plugin.php on line 465
)
Anyway, this is and edge case that we can tackle in follow-up PRs. 😄
Top of the file is pretty normal when it's something that's always going to be required. The block will need to be registered on every load so it's fine. When it's more discrete functionality, we usually load it inside the function it will be used in. |
03c2638
to
e69b7a2
Compare
A couple of PHP warnings are dumped to the top of
|
instructions={ __( 'Select something to render' ) } | ||
> | ||
<div className="a8c-content-renderer-block__selector"> | ||
<PostAutocomplete |
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.
Should we limit the results here to wp_template
CPTs? I know this hasn't been merged yet but just wanted to check whether you think this is something that we should mark as a future todo item.
To continue this line of reasoning a bit, what do you all think about imposing the following constraints initially:
- we should allow inserting both Content Preview and Content Renderer blocks only in
wp_template
CPT context. The former will serve as a placeholderpost_content
for the post that consumes the given template, and the second as a method for reusing template parts (e.g. header, footer) across multiple templates. wp_template
can contain only one Content Preview block (we'll permit rendering only one post within a template for now; no mixing of content and template parts).- Content Preview block will only show pages in its preview suggestions (or more broadly, not show
wp_template
CPTs). - Content Renderer block will show only
wp_template
CPTs in its post suggestions.
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.
I think that's something we might want to discuss more extensively.
On one hand, this block intent is to render layout elements.
On the other, what if I want to render a post inside a template?
A simple example that comes to mind would be a dynamic piece of content in the sidebar that wouldn't require me to update the entire template to change. (basically a widget of some sort).
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.
As per the next steps (I think we can tackle 3 out of 4 in follow ups, and the remaining in the other PR):
- we should allow inserting both Content Preview and Content Renderer blocks only in wp_template CPT context. The former will serve as a placeholder post_content for the post that consumes the given template, and the second as a method for reusing template parts (e.g. header, footer) across multiple templates.
This makes sense, at least initially.
But then, I'm up for max flexibility, and so I'd be ok letting folks inserting a Renderer inside any post type.
(Thinking about it, Preview wouldn't make much sense outside a Template.
- wp_template can contain only one Content Preview block (we'll permit rendering only one post within a template for now; no mixing of content and template parts).
Yes indeed! This is a 1-liner change: just add multiple: false
to the block support properties. (I will actually do this in #32529 asap. No need to open an whole new PR for this).
- Content Preview block will only show pages in its preview suggestions (or more broadly, not show wp_template CPTs).
Ok for not including Templates, but I'd leave it open to any kind of (publicly searchable) CPT.
Say, for a Woo product, or a JP portfolio page, etc.
- Content Renderer block will show only wp_template CPTs in its post suggestions.
Ok, but see my previous comment. 🙂
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.
A simple example that comes to mind would be a dynamic piece of content in the sidebar that wouldn't require me to update the entire template to change. (basically a widget of some sort).
Sounds like something that better fits a reusable block use-case?
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.
Yes, unless you also want to display it as a normal post. 😄
I mean, folks use WP for the weirdest thing, and I'd love to keep giving them such freedom.
You're definitely right in scoping this down, just, let's not exclude this completely for future iterations.
Nice work @Copons! I left a few comments, but their purpose is mainly to chart out some future work, and I would consider them non-blocking (except maybe for #32581 (comment) if it can be resolved quickly). |
69e11bc
to
5073d86
Compare
5073d86
to
a49bc53
Compare
Changes proposed in this Pull Request
Note: this is based off #32529, so it will need to be rebased when that's merged.
It roughly works like the Content Slot block but, whereas that's a sort of placeholder for the template to be filled with a post or page, the Template Part is used to render a layout element into the template.
The screenshot is basically the same (of course with a different title and label):
The main visible difference is that this renders the selected post's content on the front-end.
Testing instructions
npm distclean
andnpm ci
first).Fixes #32493