-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Template Loader: Introduce get_template_hierarchy(), drop gutenberg_t…
…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 <[email protected]>
- Loading branch information
Showing
2 changed files
with
86 additions
and
98 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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