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

Template Parts: Improve resolution and saving behavior. #21766

Merged
merged 2 commits into from
Apr 23, 2020

Conversation

epiqueras
Copy link
Contributor

Closes #21512

Description

Template part resolution works by looking for a postId attribute on the block and falling back to looking for the theme's provided template part .html file using the theme and slug attributes. The postId is only added to a template part instance after it is customized and saved. This meant that after customizing a template part, templates that referenced it only by slug and theme would not resolve to the customized template part.

This was fixed by adding an intermediate check between the postId and .html file lookup where we check for a published template part for the appropriate theme and slug.

Additionally, there was an issue wherein customizing a template part sometimes the API appended counters (-2) to slugs. This was fixed by using the wp_unique_post_slug filter, just like for templates.

How to test this?

  • Activate the full site editing experiment and demo templates.
  • Edit and save the header template part in isolation.
  • Verify that the front-page template now renders the edited header both in the editor and front end.

Types of Changes

Bug Fix: Template part resolution now works as expected in templates referencing customized template parts.

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.

@epiqueras epiqueras self-assigned this Apr 21, 2020
@epiqueras epiqueras added [Feature] Full Site Editing [Type] Bug An existing feature does not function as intended labels Apr 21, 2020
@github-actions
Copy link

github-actions bot commented Apr 21, 2020

Size Change: +3.07 kB (0%)

Total Size: 845 kB

Filename Size Change
build/annotations/index.js 3.62 kB -1 B
build/block-directory/index.js 6.24 kB -1 B
build/block-editor/index.js 105 kB +1 B
build/block-editor/style-rtl.css 10.1 kB -21 B (0%)
build/block-editor/style.css 10.1 kB -22 B (0%)
build/block-library/editor-rtl.css 7.13 kB +33 B (0%)
build/block-library/editor.css 7.13 kB +30 B (0%)
build/block-library/index.js 112 kB +248 B (0%)
build/block-library/style-rtl.css 7.19 kB +15 B (0%)
build/block-library/style.css 7.19 kB +14 B (0%)
build/blocks/index.js 57.7 kB -1 B
build/components/index.js 198 kB +1 B
build/data-controls/index.js 1.25 kB +1 B
build/edit-post/index.js 28.2 kB +252 B (0%)
build/edit-post/style-rtl.css 12.5 kB +141 B (1%)
build/edit-post/style.css 12.5 kB +141 B (1%)
build/edit-site/index.js 10.8 kB +334 B (3%)
build/edit-site/style-rtl.css 5.25 kB +202 B (3%)
build/edit-site/style.css 5.24 kB +203 B (3%)
build/edit-widgets/index.js 8.33 kB +838 B (10%) ⚠️
build/edit-widgets/style-rtl.css 5 kB +339 B (6%) 🔍
build/edit-widgets/style.css 5 kB +342 B (6%) 🔍
build/editor/style-rtl.css 3.27 kB -10 B (0%)
build/editor/style.css 3.27 kB -10 B (0%)
build/keyboard-shortcuts/index.js 2.51 kB -1 B
build/list-reusable-blocks/index.js 3.12 kB -1 B
build/media-utils/index.js 5.29 kB -1 B
build/rich-text/index.js 14.8 kB -1 B
build/server-side-render/index.js 2.67 kB +2 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.02 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-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/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/index.js 8.43 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 569 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/editor/editor-styles-rtl.css 428 B 0 B
build/editor/editor-styles.css 431 B 0 B
build/editor/index.js 43.3 kB 0 B
build/element/index.js 4.65 kB 0 B
build/escape-html/index.js 733 B 0 B
build/format-library/index.js 7.32 kB 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 711 B 0 B
build/keycodes/index.js 1.94 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/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.49 kB 0 B
build/priority-queue/index.js 789 B 0 B
build/redux-routine/index.js 2.84 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.17 kB 0 B

compressed-size-action

Copy link
Contributor

@Addison-Stavlo Addison-Stavlo left a comment

Choose a reason for hiding this comment

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

Testing these instructions on:

  • 2020 (w/ demo templates) - works as expected.
  • 2020-blocks (w/ demo templates) seems to have some issues, but that may be expected given conflictions between what is supplied by the demo templates and what is supplied by the block based theme?
  • 2020-blocks (w/o demo templates) seems to have issues inconsistently. Half the time I try from a clean slate it seems to work as expected, the other half of the time (see below)....

Editing the header in isolation prompts saving 2 copies of the header template part:
Screen Shot 2020-04-22 at 4 13 14 PM

Once saved, the changes are not persistent to the template it is used in nor the front-end. Loading the site-editor and editing the template part in isolation again then ends up giving the expected result. I have had this happen ~4 times or so, but have also had it work perfectly a couple times... 🤷‍♀️ so I'm not really sure what to say about that other than noting what I have seen.

P.S. It's good to see you around these parts again! 🥳

@@ -26,20 +26,25 @@ export default function useTemplatePartPost( postId, slug, theme ) {
'postType',
'wp_template_part',
{
status: 'auto-draft',
status: [ 'publish', 'auto-draft' ],
Copy link
Contributor

Choose a reason for hiding this comment

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

This now allows the TP placeholder block to find matches for CPT TPs. 🎉

I have a very small PR open (#21623) where I removed this status field from the query as well as a css rule noted below in order to get CPT TPs previewable/insertable through the placeholder. It may make sense for me to close that out in favor of what you are doing here, but in the process of looking into that I found a few other things that may be worth noting while we are in this area (but not necessarily anything blocking this PR):

  1. Inserting 'broke' recently. In the PR I mentioned inserting currently works as expected. As soon as I rebase it with master (or try it on this branch since it is more recent), inserting now inserts a blank version of the TP instead (which may load properly if the editor is reloaded after saving its parent entity). I looked around a little to see what change caused this but did not really have any luck.

  2. As far as the placeholder/inserter is concerned, I can't seem to get this to find any matches for theme supplied template parts. After testing a little, it seems getEntityRecord with the provided Id will retrieve the theme supplied TP while getEntityRecords with a query will not. I could find the twentytwenty header in 'auto-draft' status on the singular selector after knowing the Id for it, but the results from the plural selector with this query seem to ignore it. 😞

  3. It seems the preview of the template parts from the placeholder/inserter is cut off at the bottom. You can really notice it if you create one that is only a couple words in a paragraph block and then try to insert it with the placeholder component. Removing

    .block-editor-block-preview__content {
    position: initial;
    }
    seems to fix the issue for me. Maybe we could consider removing that here (if that change makes sense to you) if Im closing out that other PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Inserting 'broke' recently. In the PR I mentioned inserting currently works as expected. As soon as I rebase it with master (or try it on this branch since it is more recent), inserting now inserts a blank version of the TP instead (which may load properly if the editor is reloaded after saving its parent entity). I looked around a little to see what change caused this but did not really have any luck.

This appears to be an inner blocks regression. #21804

As far as the placeholder/inserter is concerned, I can't seem to get this to find any matches for theme supplied template parts. After testing a little, it seems getEntityRecord with the provided Id will retrieve the theme supplied TP while getEntityRecords with a query will not. I could find the twentytwenty header in 'auto-draft' status on the singular selector after knowing the Id for it, but the results from the plural selector with this query seem to ignore it. 😞

I just tried header and twentytwenty-blocks in this PR and it worked as expected.

It seems the preview of the template parts from the placeholder/inserter is cut off at the bottom. You can really notice it if you create one that is only a couple words in a paragraph block and then try to insert it with the placeholder component...

I think it makes more sense to update your PR to be just that; it's unrelated to this PR.

Copy link
Contributor

@Addison-Stavlo Addison-Stavlo Apr 23, 2020

Choose a reason for hiding this comment

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

Thanks for the clarification!

I just tried header and twentytwenty-blocks in this PR and it worked as expected.

Oh it is great to see this working! I may have been testing it wrong, I saw a record for 'header' and 'twentytwenty' when I was looking before that I got though the Id, so I had been trying to get that one to show up (i was probably trying to get the demo template header 🤣 ). But testing on this PR with the block based themes works! 🎉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice!

I just found the cause of the insertion regression. I'll ping you on the write-up :)

@epiqueras
Copy link
Contributor Author

Thanks, @Addison-Stavlo, it's nice to be back 😄 🎉

2020 (w/ demo templates) - works as expected.

🚀

2020-blocks (w/ demo templates) seems to have some issues, but that may be expected given conflictions between what is supplied by the demo templates and what is supplied by the block based theme?

You can't test this on the Twenty Twenty Blocks theme with demo templates enabled, because the front-page demo template overrides the theme's index and front-page references the regular Twenty Twenty.

<!-- wp:template-part {"slug":"header","theme":"twentytwenty"} /-->

In short, demo templates are meant to be used with the regular Twenty Twenty for testing.

2020-blocks (w/o demo templates) seems to have issues inconsistently. Half the time I try from a clean slate it seems to work as expected, the other half of the time (see below)....

This was a separate issue, but I worked on a fix because it touches the same logic. What was happening was that sometimes when preloading, all the template loading code would run a second time, creating auto-drafts for non-customized template parts again. The template part IDs sent back to the site editor were from the first run, but when loading the main template, which includes the template parts, the blocks would resolve the IDs from the second run. Then, when using the template switcher, you would switch to the IDs from the first run, which resulted in having edits for two different auto-drafts.

Thanks for catching this. Now it will reuse posts across runs, giving priority to published posts over auto-drafts in case the template part has been customized elsewhere just like the block does.

Copy link
Contributor

@Addison-Stavlo Addison-Stavlo left a comment

Choose a reason for hiding this comment

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

This was a separate issue, but I worked on a fix because it touches the same logic.

Nice! Thanks for fixing that as well, this all works very well for me now and looks great.

I don't know if you'l want some other eyes on this or not, but you got an approval from my end!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Template parts saving behavior is confusing
2 participants