-
Notifications
You must be signed in to change notification settings - Fork 4.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
Site Editor: Resolve homepage template on server-side #38817
Conversation
} | ||
|
||
$hierarchy = array( 'front-page', 'home', 'index' ); | ||
$template = gutenberg_resolve_template( 'home', $hierarchy, '' ); |
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.
Omitting the third parameter here prevents error when a high-level PHP template exists in a hierarchy - #37236 (comment).
I don't think resolving to PHP templates is helpful for Site Editor. So instead, we fall back to the closest block template.
Size Change: -278 B (0%) Total Size: 1.15 MB
ℹ️ View Unchanged
|
@@ -76,6 +76,35 @@ function gutenberg_get_editor_styles() { | |||
return $styles; | |||
} | |||
|
|||
/** |
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.
Can we put the PHP into lib/compat
? wordpress-5.9
if we know it will be backported or wordpress-6.0
otherwise.
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 this should be internal function/logic that lives in the site-editor.php
file, but we can use compat dir for the plugin.
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.
Done :) I'm not sure if it's the correct file, but I couldn't find anything specific for the site editor.
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.
Thank you! I wonder if doing full server-side redirection is possible though? Do you mind share your study on this?
@@ -119,6 +148,7 @@ static function( $classes ) { | |||
'styles' => gutenberg_get_editor_styles(), | |||
'defaultTemplateTypes' => $indexed_template_types, | |||
'defaultTemplatePartAreas' => get_allowed_block_template_part_areas(), | |||
'__experimentalHomeTemplate' => gutenberg_edit_site_resolve_home_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.
Why do we want to mark it as experimental
? Maybe we should mark it as internal
instead?
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 don't have an internal
prefix. Generally __unstable
is for things we know we'll never make public.
packages/edit-site/src/index.js
Outdated
@@ -41,7 +41,7 @@ export async function reinitializeEditor( target, settings ) { | |||
// won't be present. Do a client side redirect to the 'homepage' if that's | |||
// the case. | |||
try { | |||
await redirectToHomepage( settings.siteUrl ); | |||
await redirectToHomepage( settings.__experimentalHomeTemplate ); |
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 wonder if we could just do the redirection on server-side instead, so we don't need to do anything on the client side (except for exception handling I guess).
@kevin940726, I've not tested full server-side redirection, but I can give it a try next week :) |
7eec148
to
45574b4
Compare
@@ -119,6 +119,7 @@ static function( $classes ) { | |||
'styles' => gutenberg_get_editor_styles(), | |||
'defaultTemplateTypes' => $indexed_template_types, | |||
'defaultTemplatePartAreas' => get_allowed_block_template_part_areas(), | |||
'__unstableHomeTemplate' => gutenberg_resolve_home_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.
You can comment out this line to test the warning when the method cannot resolve the template.
I've updated the code to do server-side redirection. Let me know what do you think about this new method. |
Folks, this is ready for another review. |
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.
Tested and it worked as expected! I don't know much about the PHP side of the code though so I'm just going to trust you or defer it to @talldan maybe 😛 ?
packages/edit-site/src/index.js
Outdated
@@ -36,19 +35,13 @@ import ErrorBoundaryWarning from './components/error-boundary/warning'; | |||
* @param {?Object} settings Editor settings object. | |||
*/ | |||
export async function reinitializeEditor( target, settings ) { |
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 guess we don't need this to be an async function anymore?
45574b4
to
3dd34d9
Compare
I'm going to merge this, and I can do a follow-up if any issues are discovered. |
$template = gutenberg_resolve_home_template(); | ||
if ( ! $template ) { | ||
return; | ||
} | ||
|
||
$redirect_url = add_query_arg( | ||
$template, | ||
admin_url( 'themes.php?page=gutenberg-edit-site' ) | ||
); | ||
wp_safe_redirect( $redirect_url ); |
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 looks like whenever we find a template with this function, we are redirecting.
Is there a case where the template this function returns is the same as the template the editor would initially resolve in the first place, causing the redirect to be unnecessary?
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.
Is there a case where the template this function returns is the same as the template the editor would initially resolve in the first place, causing the redirect to be unnecessary?
I don't think so. Site Editor needs these query args to resolve the template.
Backports change from Gutenberg to support server-side home template resolution in the Site Editor. Original PR WordPress/gutenberg#38817. Props Mamaduka. See #55505. git-svn-id: https://develop.svn.wordpress.org/trunk@53093 602fd350-edb4-49c9-b593-d223f7449a82
Backports change from Gutenberg to support server-side home template resolution in the Site Editor. Original PR WordPress/gutenberg#38817. Props Mamaduka. See #55505. Built from https://develop.svn.wordpress.org/trunk@53093 git-svn-id: http://core.svn.wordpress.org/trunk@52682 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Backports change from Gutenberg to support server-side home template resolution in the Site Editor. Original PR WordPress/gutenberg#38817. Props Mamaduka. See #55505. Built from https://develop.svn.wordpress.org/trunk@53093 git-svn-id: https://core.svn.wordpress.org/trunk@52682 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Description
Closes #36873.
PR introduces a new
gutenberg_resolve_home_template
function, which will try to resolve the home template on the server-side. The resolved template is available via__unstableHomeTemplate
editor settings.Notes
gutenberg_resolve_home_template
function doesn't use PHP template fallbacks. So if a theme hasfront-page.php
andindex.html,
the editor will displayindex.html
while the site displaysfront-page.php.
Previously, this caused an error in the editor since we can't render PHP templates.Testing Instructions
Tested using TT2 theme.
front-page.html
in the templates directory.Types of changes
Enhancement
Checklist:
*.native.js
files for terms that need renaming or removal).