-
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: Theme and Front-end Rendering #32865
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. |
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.
Found one big problem and a couple of smaller things. I also found that my page wasn't rendering properly on the front end (nothing came out of render_post_content_block
). I can help dig into that later but this has enough problems for me that I'm going to stop for now
get_header(); | ||
|
||
$post_id = get_the_ID(); | ||
$template_id = get_post_meta( $post_id, 'wp_template_id', true ); |
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.
We should use a leading underscore on this meta like core does for choosing templates: _wp_page_template
. Any meta with a leading underscore is hidden by default from the Custom Fields metabox so that users can't mess them up.
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 might be messing up something else, but if I register _wp_template_id
, it doesn't let me update it via REST:
{
"code": "rest_cannot_update",
"message": "Sorry, you are not allowed to edit the _wp_template_id custom field.",
"data": {
"key": "_wp_template_id",
"status": 403
}
}
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 (untested) should address that issue:
register_post_meta( '', '_wp_template_id', array( 'show_in_rest' => true );
$template_id = get_post_meta( $post_id, 'wp_template_id', true ); | ||
$template = get_post( $template_id ); | ||
|
||
if ( $template_id !== $post_id && $template ) { |
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'd probably also add a post type check, like 'wp_template' === $template->post_type
here. I would not be surprised if we wind up wanting something like a is_template()
function that will accept an ID or WP_Post
object.
apps/full-site-editing/full-site-editing-plugin/blocks/post-content/index.php
Show resolved
Hide resolved
9b6fd8a
to
7a1a553
Compare
); | ||
} ); | ||
|
||
if ( 'wp_template' === fullSiteEditing.editorPostType ) { |
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 don't think the component file is the right place for this. I get it (don't like it either) with blocks
, but I expect components to export
. How do we feel about moving this (and import { registerPlugin } from '@wordpress/plugins';
) to full-site-editing-plugin/index.js
??
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've thought about it, and tbh I might even go as far as creating a new folder (plugins
?), and keep the index as clean as possible.
); | ||
} ); | ||
|
||
if ( 'wp_template' !== fullSiteEditing.editorPostType ) { |
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.
} | ||
} | ||
|
||
$template = get_post( $template_id ); |
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.
EDIT: nevermind, hadn't brought in the latest changes. The bottom paragraph may still may be worth considering. (Tried this a bit again and I'm still getting infinite loops when 1) saving a As a semi-aside, I think we're going to need some kind of dedicated function for rendering templates and/or post content that keeps track of the stack that's been rendered so far so as to try to prevent nesting the same template, and also we probably need to go away from just calling |
Being able to detect a cycle here would be lovely, but for now let's add a max depth for rendering before we give up. For our initial use case limiting this to say a depth of 10 would probably be more than enough, and we can add logging in this case to help figure out what's happening. Edit: Thinking on this more, do we need more than a depth of two? The page template will need a content slot, and may contain template parts. Any other template parts, probably don't need to nest further. I think it's fine to constrain this, and revisit later if we find a use case. |
@@ -1,11 +1,37 @@ | |||
<?php | |||
|
|||
function render_post_content_block( $attributes, $content ) { | |||
// Early return to avoid infinite loops in the REST API | |||
if ( is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { |
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.
Out of curiosity what does the trace look like? Are there similar rendering conditions we need to be aware of?
This PR was opened to back my exploration on FSE front end rendering (paAmJe-lZ-p2), and so it's purpose is now complete.
@gwwar We probably don't, but I don't see a reason why we can't.
I'm not sure if @mattwiebe got the same, but in my case the Content Slot render callback ran out of memory, while also being called continuously by the element re-rendering in React (at least, this is my suspicion). Now it should be fixed (and also @mattwiebe said that he wasn't running the latest commit), but we totally need to keep an eye out for infinite loops, because in FSE they might happen in the most unexpected ways (for example, I didn't consider REST requests!). |
WARNING
This is a prototype! The code is rough!
It's mostly intended to be installed and tested.
Hopefully there are no infinite loops anymore, but you never know.
Changes proposed in this Pull Request
Template Selection
Users can choose the template for Posts and Pages through the new Template sidebar.
It contains a
wp_template_id
meta box with an autocomplete input searching for Templates.Notes:
viewable
CPTs, not just Posts and Pages.wp_template_id
autocomplete should only search for Templates containing a Content Slot block.Template Hierarchy
Add barebone and very rough support for template hierarchy.
Think of it like an abstraction/simplification of the Core hierarchy.
Users can assign a Template to a "screen" (for lack of a better term) through the new Template Hierarchy sidebar.
It contains a list of checkbox for all the screens that could use a template.
Currently:
index
: the default fallback template for all screens.singular
: the default foris_singular()
screens.viewable
post types (e.g.post
,page
,media
): the default for non-singular screens of the given post type (e.g. archives, or posts list page).Blank Theme
Add a very minimalistic Blank Theme, which works very roughly like the Core template selection.
It has two behaviours, depending if the screen
is_singular
or otherwise.Singular screens are straightforward: render the first available template; then the Content Slot block inside it will render the post content.
The template selection order for singular screens is:
wp_template_id
.$template_hierarchy->singular
.$template_hierarchy->index
.Non-singular screens are a bit more complicated: select the first available template; then the Content Slot block inside it will render the entire posts list.
The template selection order for non-singular screens is:
$template_hierarchy->$post_type
.$template_hierarchy->index
.Notes:
Screenshots
Notes:
flex
in the editor, and so it awkwardly wraps the columns when rendered in preview, but looks just fine when rendered in the front-end.Testing instructions
Fixes #32496
Fixes #32617