-
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
Add required logic for Block template themes #1267
Conversation
## Description Discovered while working on WordPress/wordpress-develop#1267. First introduced in `lib/template-loader.php` in #25739. No longer used per #26650 (where its callsites started using [`_gutenberg_get_template_paths`](https://github.com/WordPress/gutenberg/pull/26650/files#diff-f5b03c388f81fea69d0ababd289047e20deaad43084ad6e00ec14a5613e25136R60) in `lib/templates-sync.php` -- now in [`lib/full-site-editing/block-templates.php`](https://github.com/WordPress/gutenberg/blob/d7714aa8adf19277da1f0ea83b20be1cf234e50c/lib/full-site-editing/block-templates.php#L17)).
Some updates here:
The next step for me is going to make everything work as it should in the UI before attacking the code cleaning. |
@ockham I removed all the logic that fetches templates from files (block based themes) from the code and left just the part about custom template (the CPT). I wonder if |
@ockham I wanted to add that I had to change the "prepare_for_database" function in the REST API controller because the create_item unit test was failing (compare it with Gutenberg). I think the failure was legitimate and we probably should fix that function similarly in Gutenberg as well, though not sure why the unit test was not failing there. (Just wanted to note this comment somewhere so I don't forget about it :P ) |
// environment. Unfortunately using `get_the_terms` for the 'wp-theme' | ||
// term does not work in the case of new entities since is too early in | ||
// the process to have been saved to the entity. So for now we use the | ||
// currently activated theme for creation. |
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 it really such a good idea to merge this to core with an unresolved todo note? I have WordPress/gutenberg#31971 open, which basically solves this fully and will eliminate a lot of future headaches. Although it must be thoroughly tested, it's not intended to change any existing behaviour.
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.
Today, is the last day for feature freeze so we need a solution in. I think the current solution is decent but your approach in the PR look promising potentially. We can look at it at ease and make the right call for beta1...
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 current solution is scalable because taxonomies and slugs offer too many features that are not suitable for this purpose, nor are those systems aware of the constraints that are needed here. For example, the use of get_the_terms()[0]
is problematic, because if a template somehow has two theme terms, and one is deleted, it can suddenly change theme. That could then trigger a slug conflict that causes the front-end to render differently.
If the taxonomy makes it into 5.8, I can see a potentially complicated and risky database migration needing to be done in a later version, and it would be nice to avoid that.
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.
@carlomanf I think Riad's point is that we need to merge this PR today in order to get the template infrastructure into Core, since it's the last possible date (due to feature freeze) for 5.8. A PR like your WordPress/gutenberg#31971 (which looks quite good to me at first glance BTW!) could be considered a bug fix that we can merge even after feature freeze, so it makes it into 5.8.
… wp-includes/theme.php
src/wp-includes/post.php
Outdated
@@ -310,6 +310,68 @@ function create_initial_post_types() { | |||
) | |||
); | |||
|
|||
if ( theme_supports_block_templates() ) { |
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.
@youknowriad I was wondering about these checks (especially here and in taxonomy.php
where we register the wp_theme
taxonomy). There doesn't seem to be any precedent in Core for conditionally registering a post type or taxonomy; and I was wondering if this made sense UX-wise.
I take it that the main reason for this is to hide the Template Editor for themes that don't support block templates. But this seems to make less sense if we're not even actually using any block template files from a given theme for now -- but only wp_template
CPTs. In other words, why should the theme decide whether or not the user can create custom templates, if it's not even providing the block template file fallback?
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 should the theme decide whether or not the user can create custom templates, if it's not even providing the block template file fallback?
Because we want themes to have the possibility to opt-out of this feature and stay as they are today. Whether or not the CPT should always be registered or not is another concern though.
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.
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.
src/wp-includes/block-template.php
Outdated
* @param string $template_file Template file name. | ||
* @return string Template file name without extension. | ||
*/ | ||
function _strip_php_suffix( $template_file ) { |
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.
The naming isn't accurate here, since the line below also removes .html
suffixes. We should either change the name, or the implementation. Since I only see one callsite which seems .php
specific, it might be okay to only remove .php
?
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 internal anyway, your call
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.
Ah, I think the .html
case is needed for custom page block templates. Renaming to _strip_template_file_suffix
in bf8d81e.
src/wp-includes/theme.php
Outdated
function theme_supports_block_templates() { | ||
return current_theme_supports( 'block-templates' ); |
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.
How about just inlining this? The check is super simple now; if we need more criteria in the future, we can always add them later, but maybe it's best to start simple (YAGNI!)
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 do need more criteria in the future (like the Gutenberg plugin). Again, I'm not very opinionated here.
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.
Inlining in e4ecc6b. My rationale is that we don't add more API surface (even if only internal) for now; and in some places, we might need a slightly different check anyway later, so it might make sense to keep individual checks separate. (Feel free to revert tho.)
Pushed a few changes; feel free to revert if you disagree. This LGTM overall. I haven't reviewed the controller closely, but I think this PR should be in good enough shape to merge. |
Working on a fix here: WordPress/gutenberg#32233 |
Trac ticket: https://core.trac.wordpress.org/ticket/53176
List of files in Gutenberg, and where to map them to in Core
Attempting to fit into Core's existing file hierarchy and naming scheme.
lib/template-canvas.php
->src/wp-includes/
lib/full-site-editing/
:block-templates.php
->src/wp-includes/block-template-loader.php
(to avoid confusion withblock-template.php
)wp_template
object.class-wp-block-template.php
->src/wp-includes/
class-wp-rest-templates-controller.php
->src/wp-includes/rest-api/endpoints/
default-template-types.php
->src/wp-includes/
Not needed for WP 5.8.edit-site-export.php
- REST endpoint for exporting the contents of the Site Editor.Not needed for WP 5.8.edit-site-page.php
-> maybesrc/wp-admin/
? The Post Editor entry point is there, insrc/wp-admin/edit-form-blocks.php
full-site-editing.php
src/wp-includes/theme.php
plus some wp-admin customizations to remove legacy theme screens and sidebar menu items if the Site Editor is enabledNot needed for WP 5.8.page-templates.php
->src/wp-includes/class-wp-theme.php
gutenberg_get_block_templates
, which is defined in the block template loader.theme_templates
filter, which is applied in Core'ssrc/wp-includes/class-wp-theme.php
. Move there?template-loader.php
->src/wp-includes/block-template.php
{type}_template
filter) to include block templates.get_query_template()
for now.Not needed for WP 5.8.template-parts.php
->src/wp-includes/post.php
? (It seems like pretty much all of Core's post types are registered there, including e.g.wp_block
.)wp_template_part
CPT.templates-utils.php
->src/wp-admin/
orsrc/wp-admin/includes/
?wp_template
admin list. Hooked into actions and filters intemplates.php
andtemplate-parts.php
for those two post types.templates.php
wp_template
CPT ->src/wp-includes/post.php
(It seems like pretty much all of Core's post types are registered there, including e.g.wp_block
.)wp_theme
taxonomy ->src/wp-includes/taxonomy.php
wp_template
post type to the user ->src/wp-includes/post.php
src/wp-includes/theme-templates.php
wp_footer
->src/wp-includes/default_filters.php
phpunit/
:class-block-templates-test.php
->tests/phpunit/tests/block-template-loader.php
wp_template
, respectively).Not needed for WP 5.8.class-edit-site-export-test.php
edit-site-export.php
.class-template-loader-test.php
->tests/phpunit/tests/block-template.php
class-wp-rest-template-controller-test.php
->tests/phpunit/tests/rest-api/rest-templates-controller.php
phpunit/fixtures/themes/
:test-theme/
->tests/phpunit/data/themedir1/block-template-theme/
test-theme-child/
->tests/phpunit/data/themedir1/block-template-theme-child/
TODO
tests/phpunit/tests/block-template.php
.tests/phpunit/tests/rest-api/rest-templates-controller.php
(i.e. not TT1 Blocks, since it's not included with Core) to make it pass. Should ideally be a test block template theme fixture that we're including withwordpress-develop
. Maybe we can usetests/phpunit/data/themedir1/block-template-theme/
(or extend it a bit)._gutenberg_
andgutenberg_
prefixes.src/wp-settings.php
is fine.if ( gutenberg_supports_block_templates() )
conditionals (e.g. for post type registration in inpost.php
)@since
items where needed.Testing instructions
To run unit tests locally:
(or just omit the file args altogether to run all unit tests)
Furthermore, try in your browser: Activate a block template theme (e.g. TT1 Blocks), and verify that it renders properly on the frontend. (Make sure that the GB plugin isn't active!)
Any necessary customizations to GB can be done after the fact and shouldn't be a blocker to this PR.
There's a chance that this list isn't exhaustive -- I might've missed some required files.
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.