Navigation Menu

Skip to content
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

Closed
wants to merge 57 commits into from
Closed

Add required logic for Block template themes #1267

wants to merge 57 commits into from

Conversation

ockham
Copy link
Contributor

@ockham ockham commented May 19, 2021

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 with block-template.php)
    • Loads block templates from HTML block template file or wp_template object.
  • class-wp-block-template.php -> src/wp-includes/
    • Class representing a block template. Used by the block template loader, and the REST API controller.
  • class-wp-rest-templates-controller.php -> src/wp-includes/rest-api/endpoints/
    • The REST API controller for templates.
  • default-template-types.php -> src/wp-includes/
    • Titles and descriptions of template types. Arguably not block template specific, but OTOH not used outside of FSE.
  • edit-site-export.php - REST endpoint for exporting the contents of the Site Editor. Not needed for WP 5.8.
  • edit-site-page.php -> maybe src/wp-admin/? The Post Editor entry point is there, in src/wp-admin/edit-form-blocks.php Not needed for WP 5.8.
    • Site Editor entrypoint.
  • full-site-editing.php
    • Some helper functions to determine is the current theme supports block templates -> src/wp-includes/theme.php
    • plus some wp-admin customizations to remove legacy theme screens and sidebar menu items if the Site Editor is enabled Not needed for WP 5.8.
  • page-templates.php -> src/wp-includes/class-wp-theme.php
    • Allow to select a block template as custom page template for a given page. See Add support for custom templates in FSE themes gutenberg#27778 for details.
    • Uses gutenberg_get_block_templates, which is defined in the block template loader.
    • Hooks into the theme_templates filter, which is applied in Core's src/wp-includes/class-wp-theme.php. Move there?
  • template-loader.php -> src/wp-includes/block-template.php
    • Modify the template hierarchy resolution algorithm (by hooking into the {type}_template filter) to include block templates.
    • Should probably be integrated into get_query_template() for now.
  • 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.) Not needed for WP 5.8.
    • Register the wp_template_part CPT.
  • templates-utils.php -> src/wp-admin/ or src/wp-admin/includes/?
    • Utilities to customize the wp_template admin list. Hooked into actions and filters in templates.php and template-parts.php for those two post types.
  • templates.php
    • Register the 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.)
    • Register the wp_theme taxonomy -> src/wp-includes/taxonomy.php
    • Grant capabilities for the wp_template post type to the user -> src/wp-includes/post.php
    • Add the Templates wp-admin screen and menu entry (under Themes) -> src/wp-includes/theme-templates.php
    • Set unique slug upon auto-draft creation
    • Add the skip link to wp_footer -> src/wp-includes/default_filters.php

phpunit/:

  • class-block-templates-test.php -> tests/phpunit/tests/block-template-loader.php
    • Unit test coverage for the block-template loading functions (load from file or wp_template, respectively).
  • class-edit-site-export-test.php Not needed for WP 5.8.
    • Test coverage for edit-site-export.php.
  • class-template-loader-test.php -> tests/phpunit/tests/block-template.php
    • Unit test coverage for the block-template relevant modifications for the template resolution algorithm.
  • 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

  • Fix theme loading in tests/phpunit/tests/block-template.php.
  • Use different theme in 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 with wordpress-develop. Maybe we can use tests/phpunit/data/themedir1/block-template-theme/ (or extend it a bit).
  • Remove template parts specific bits from the templates REST API controller and its unit test (unless we decide to include template parts in WP 5.8 after all).
  • Remove _gutenberg_ and gutenberg_ prefixes.
  • Make sure the loading order in src/wp-settings.php is fine.
  • Revisit if we need if ( gutenberg_supports_block_templates() ) conditionals (e.g. for post type registration in in post.php)
  • Add/update PHPDocs to cover added/changed functionality; add @since items where needed.

Testing instructions

To run unit tests locally:

npm run test:php tests/phpunit/tests/block-template.php
npm run test:php tests/phpunit/tests/block-template-loader.php
npm run test:php tests/phpunit/tests/rest-api/rest-templates-controller.php

(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.

ockham added a commit to WordPress/gutenberg that referenced this pull request May 21, 2021
## 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)).
@youknowriad
Copy link
Contributor

Some updates here:

  • Removed all changes related to the WP-Admin menus for the CPT (these are not going to be shipped).
  • Moved some files that are not "admin" into "wp-includes".
  • Enable the block-templates support by default.

The next step for me is going to make everything work as it should in the UI before attacking the code cleaning.

@youknowriad
Copy link
Contributor

@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 resolve_block_template is still necessary. At least I think there's a few things we can remove from the resolution logic (as we don't care about theme files)

@youknowriad youknowriad marked this pull request as ready for review May 24, 2021 16:58
@youknowriad
Copy link
Contributor

@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 )

src/wp-includes/post.php Outdated Show resolved Hide resolved
// 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.

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.

Copy link
Contributor

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...

Copy link

@carlomanf carlomanf May 25, 2021

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.

Copy link
Contributor Author

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.

@@ -310,6 +310,68 @@ function create_initial_post_types() {
)
);

if ( theme_supports_block_templates() ) {
Copy link
Contributor Author

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?

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making CPT and taxonomy registration unconditional in 3471404 and cba7648, respectively.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making CPT and taxonomy registration unconditional in 3471404 and cba7648, respectively.

👍 This is a good call, as it prevents problems with the post type and taxonomy disappearing when importing/exporting because the current theme doesn't happen to use it.

* @param string $template_file Template file name.
* @return string Template file name without extension.
*/
function _strip_php_suffix( $template_file ) {
Copy link
Contributor Author

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?

Copy link
Contributor

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

Copy link
Contributor Author

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.

Comment on lines 3240 to 3241
function theme_supports_block_templates() {
return current_theme_supports( 'block-templates' );
Copy link
Contributor Author

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!)

Copy link
Contributor

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.

Copy link
Contributor Author

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.)

@ockham
Copy link
Contributor Author

ockham commented May 25, 2021

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.

@ockham
Copy link
Contributor Author

ockham commented May 26, 2021

@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 )

Working on a fix here: WordPress/gutenberg#32233

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants