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

Edit Site: Extract gutenberg_find_template_post helper, use in edit-site-page #21959

Merged
merged 7 commits into from
Apr 29, 2020

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Apr 28, 2020

Description

Extract a gutenberg_find_template_post helper function from gutenberg_find_template, and rename the latter to gutenberg_template_include_filter. Unlike the original function, gutenberg_find_template_post makes no references to globals, and can thus be used in edit-site-page.php without resorting to as many globals as before. (I've previously found the code in edit-site-page.php somewhat 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_template collection 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:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

@github-actions
Copy link

github-actions bot commented Apr 28, 2020

Size Change: +55 B (0%)

Total Size: 816 kB

Filename Size Change
build/block-directory/index.js 6.23 kB -1 B
build/block-editor/index.js 106 kB +52 B (0%)
build/block-library/index.js 114 kB -1 B
build/components/index.js 179 kB +3 B (0%)
build/data/index.js 8.44 kB -1 B
build/edit-site/index.js 10.9 kB +1 B
build/editor/index.js 43.6 kB +4 B (0%)
build/format-library/index.js 7.63 kB -1 B
build/redux-routine/index.js 2.85 kB -1 B
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.02 kB 0 B
build/annotations/index.js 3.62 kB 0 B
build/api-fetch/index.js 4.08 kB 0 B
build/autop/index.js 2.82 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/style-rtl.css 760 B 0 B
build/block-directory/style.css 761 B 0 B
build/block-editor/style-rtl.css 10.2 kB 0 B
build/block-editor/style.css 10.2 kB 0 B
build/block-library/editor-rtl.css 7.03 kB 0 B
build/block-library/editor.css 7.03 kB 0 B
build/block-library/style-rtl.css 7.14 kB 0 B
build/block-library/style.css 7.14 kB 0 B
build/block-library/theme-rtl.css 683 B 0 B
build/block-library/theme.css 685 B 0 B
build/block-serialization-default-parser/index.js 1.88 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/blocks/index.js 48.1 kB 0 B
build/components/style-rtl.css 16.9 kB 0 B
build/components/style.css 16.9 kB 0 B
build/compose/index.js 6.66 kB 0 B
build/core-data/index.js 11.4 kB 0 B
build/data-controls/index.js 1.29 kB 0 B
build/date/index.js 5.47 kB 0 B
build/deprecated/index.js 772 B 0 B
build/dom-ready/index.js 568 B 0 B
build/dom/index.js 3.1 kB 0 B
build/edit-navigation/index.js 3.54 kB 0 B
build/edit-navigation/style-rtl.css 485 B 0 B
build/edit-navigation/style.css 485 B 0 B
build/edit-post/index.js 27.6 kB 0 B
build/edit-post/style-rtl.css 12.2 kB 0 B
build/edit-post/style.css 12.2 kB 0 B
build/edit-site/style-rtl.css 5.11 kB 0 B
build/edit-site/style.css 5.11 kB 0 B
build/edit-widgets/index.js 7.5 kB 0 B
build/edit-widgets/style-rtl.css 4.67 kB 0 B
build/edit-widgets/style.css 4.66 kB 0 B
build/editor/editor-styles-rtl.css 428 B 0 B
build/editor/editor-styles.css 431 B 0 B
build/editor/style-rtl.css 3.27 kB 0 B
build/editor/style.css 3.27 kB 0 B
build/element/index.js 4.65 kB 0 B
build/escape-html/index.js 733 B 0 B
build/format-library/style-rtl.css 502 B 0 B
build/format-library/style.css 502 B 0 B
build/hooks/index.js 2.13 kB 0 B
build/html-entities/index.js 622 B 0 B
build/i18n/index.js 3.56 kB 0 B
build/is-shallow-equal/index.js 710 B 0 B
build/keyboard-shortcuts/index.js 2.51 kB 0 B
build/keycodes/index.js 1.94 kB 0 B
build/list-reusable-blocks/index.js 3.12 kB 0 B
build/list-reusable-blocks/style-rtl.css 226 B 0 B
build/list-reusable-blocks/style.css 226 B 0 B
build/media-utils/index.js 5.29 kB 0 B
build/notices/index.js 1.79 kB 0 B
build/nux/index.js 3.4 kB 0 B
build/nux/style-rtl.css 616 B 0 B
build/nux/style.css 613 B 0 B
build/plugins/index.js 2.67 kB 0 B
build/primitives/index.js 1.5 kB 0 B
build/priority-queue/index.js 789 B 0 B
build/rich-text/index.js 14.8 kB 0 B
build/server-side-render/index.js 2.68 kB 0 B
build/shortcode/index.js 1.7 kB 0 B
build/token-list/index.js 1.28 kB 0 B
build/url/index.js 4.02 kB 0 B
build/viewport/index.js 1.84 kB 0 B
build/warning/index.js 1.14 kB 0 B
build/wordcount/index.js 1.18 kB 0 B

compressed-size-action

Copy link
Contributor

@epiqueras epiqueras left a comment

Choose a reason for hiding this comment

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

Looks good. I just have one minor question.

Also, as a follow-up, we should also return template part IDs from the new function.

lib/edit-site-page.php Outdated Show resolved Hide resolved
lib/template-loader.php Outdated Show resolved Hide resolved
lib/template-loader.php Outdated Show resolved Hide resolved
ockham and others added 3 commits April 29, 2020 12:23
@ockham
Copy link
Contributor Author

ockham commented Apr 29, 2020

Looks good. I just have one minor question.

Also, as a follow-up, we should also return template part IDs from the new function.

Yep, planning to work on that 👍

@ockham
Copy link
Contributor Author

ockham commented Apr 29, 2020

I addressed all feedback -- could I get approval so I can merge? 😄

@epiqueras epiqueras mentioned this pull request Apr 29, 2020
6 tasks
Copy link
Contributor

@epiqueras epiqueras left a comment

Choose a reason for hiding this comment

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

LGTM

@epiqueras epiqueras merged commit 5eda467 into master Apr 29, 2020
@epiqueras epiqueras deleted the add/gutenberg-find-template-post-helper branch April 29, 2020 18:14
@github-actions github-actions bot added this to the Gutenberg 8.1 milestone Apr 29, 2020
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 <[email protected]>
@ellatrix ellatrix mentioned this pull request Jun 16, 2020
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants