Repository navigation
Edit Site: Extract gutenberg_find_template_post helper, use in edit-site-page - #21959
Merged
Merged
Conversation
…name the latter to gutenberg_template_include_filter
ockham
requested review from
Addison-Stavlo,
epiqueras,
felixarntz,
noahtallen,
vindl and
youknowriad
April 28, 2020 20:41
|
Size Change: +55 B (0%) Total Size: 816 kB
ℹ️ View Unchanged
|
epiqueras
reviewed
Apr 28, 2020
epiqueras
left a comment
Contributor
There was a problem hiding this comment.
Looks good. I just have one minor question.
Also, as a follow-up, we should also return template part IDs from the new function.
Co-Authored-By: Enrique Piqueras <epiqueras@users.noreply.github.com>
Co-Authored-By: Enrique Piqueras <epiqueras@users.noreply.github.com>
Contributor
Author
Yep, planning to work on that 👍 |
Contributor
Author
|
I addressed all feedback -- could I get approval so I can merge? 😄 |
5 of 6 tasks
ockham
added a commit
that referenced
this pull request
May 5, 2020
…emplate_include_filter() (#21980) Previously, the template loader made gratuitous use of filters and globals in order to communicate data from one function to another. Specifically: 1. The `gutenberg_override_query_template` filter was added to `${type}_template`. It served two purposed: 1. It recorded the current template hierarchy in the `_wp_current_template_hierarchy` global. 1. It returned an empty string to indicate that no old-school theme template should be rendered. 2. The `gutenberg_template_include_filter` filter was added to `template_include`. That filter determined the relevant template post from `_wp_current_template_hierarchy`, and used it to render the template via the template canvas. 3. In order to populate the `$settings` variable (which is passed to the client), `edit-site-page.php` iterated over all template getters, calling each one of them, in order to trigger the `${type}_template` filters (and thus, `gutenberg_override_query_template`), to obtain the template hierarchies, and subsequently, the relevant template IDs. During each iteration, care had to be taken to evaluate and then reset the relevant globals. Due to the implicit nature of globals and filters, this is harder to follow and reason about than a call stack of functions with explicit arguments and return values. Hence, this PR makes the following changes: - Introduce a new `get_template_hierachy()` function that determines the template hierarchy for a given template type. This encapsulates adding and removing a filter (which is still required) so it doesn't have to leak a global, as the previous approach did. - Use that function to get the template hierarchy in `edit-site-page.php` (see item 3. above), getting rid of most globals involved in that file. - Now that determining the template hierarchy doesn't need to be a concern of `gutenberg_override_query_template` anymore (see item 1.1 above), re-purpose it to actually render the template canvas, cutting out the now-superfluous `gutenberg_template_include_filter` (item 2.), which is thus removed. This still requires two globals (`_wp_current_template_id` and `_wp_current_template_content`), to communicate the template that needs to be rendered to the canvas. However, their use is much more limited. This PR still does not touch the `_wp_current_template_part_ids` global, in order to ensure that the auto-draft saving behavior isn't changed. I'm planning to work on this in another follow-up. This is a follow-up to #21959, and another preparatory step for https://github.com/WordPress/gutenberg/pull/21877/files#r416075231. Co-authored-by: Enrique Piqueras <epiqueras@users.noreply.github.com>
This was referenced May 6, 2020
10 of 12 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Extract a
gutenberg_find_template_posthelper function fromgutenberg_find_template, and rename the latter togutenberg_template_include_filter. Unlike the original function,gutenberg_find_template_postmakes no references to globals, and can thus be used inedit-site-page.phpwithout resorting to as many globals as before. (I've previously found the code inedit-site-page.phpsomewhat hard to read, since it wasn't immediately clear where those globals where coming from.)This is a preparatory step to better encapsulate the logic that determines the relevant template post for a given page, so it can be more easily reused. Specifically, it's meant as a preparation for implementing an endpoint flag that would make the
wp_templatecollection endpoint return only relevant templates, as discussed here.(In a follow-up, I might try to get rid of some other
$_wp_current_template_*globals.)How has this been tested?
Verify that Full Site Editing still works as before. Specifically, verify that the template switcher still holds the same templates as on
master, and that loading them (both front-end and editor) still works.Types of changes
Small refactor.
Checklist: