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: Fetch template parts in Template Switcher from REST API #21878

Merged
merged 1 commit into from
Jun 9, 2020

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Apr 24, 2020

Description

Instead of iterating over templateIds and fetching each corresponding template from the REST API, use one network fetch.

This is to decouple edit-site even further from the settings variable that is at the moment directly passed from PHP, and will use the REST API instead. #21877 is complementary to this PR.

How has this been tested?

Verify that the templates and template parts listed in the template switcher are the same as before.

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 24, 2020

Size Change: +24 B (0%)

Total Size: 1.13 MB

Filename Size Change
build/edit-site/index.js 15.5 kB +24 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.14 kB 0 B
build/annotations/index.js 3.62 kB 0 B
build/api-fetch/index.js 3.4 kB 0 B
build/autop/index.js 2.83 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/index.js 6.77 kB 0 B
build/block-directory/style-rtl.css 892 B 0 B
build/block-directory/style.css 892 B 0 B
build/block-editor/index.js 106 kB 0 B
build/block-editor/style-rtl.css 11.8 kB 0 B
build/block-editor/style.css 11.8 kB 0 B
build/block-library/editor-rtl.css 7.88 kB 0 B
build/block-library/editor.css 7.89 kB 0 B
build/block-library/index.js 127 kB 0 B
build/block-library/style-rtl.css 7.72 kB 0 B
build/block-library/style.css 7.72 kB 0 B
build/block-library/theme-rtl.css 684 B 0 B
build/block-library/theme.css 686 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/index.js 194 kB 0 B
build/components/style-rtl.css 19.5 kB 0 B
build/components/style.css 19.5 kB 0 B
build/compose/index.js 9.31 kB 0 B
build/core-data/index.js 11.4 kB 0 B
build/data-controls/index.js 1.29 kB 0 B
build/data/index.js 8.45 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.17 kB 0 B
build/edit-navigation/index.js 8.25 kB 0 B
build/edit-navigation/style-rtl.css 918 B 0 B
build/edit-navigation/style.css 919 B 0 B
build/edit-post/index.js 303 kB 0 B
build/edit-post/style-rtl.css 5.6 kB 0 B
build/edit-post/style.css 5.6 kB 0 B
build/edit-site/style-rtl.css 2.96 kB 0 B
build/edit-site/style.css 2.96 kB 0 B
build/edit-widgets/index.js 9.34 kB 0 B
build/edit-widgets/style-rtl.css 2.4 kB 0 B
build/edit-widgets/style.css 2.4 kB 0 B
build/editor/editor-styles-rtl.css 425 B 0 B
build/editor/editor-styles.css 428 B 0 B
build/editor/index.js 44.8 kB 0 B
build/editor/style-rtl.css 4.26 kB 0 B
build/editor/style.css 4.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/index.js 7.72 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 621 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.52 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.3 kB 0 B
build/notices/index.js 1.79 kB 0 B
build/nux/index.js 3.41 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.56 kB 0 B
build/primitives/index.js 1.5 kB 0 B
build/priority-queue/index.js 789 B 0 B
build/redux-routine/index.js 2.85 kB 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.06 kB 0 B
build/viewport/index.js 1.85 kB 0 B
build/warning/index.js 1.14 kB 0 B
build/wordcount/index.js 1.17 kB 0 B

compressed-size-action

@ockham ockham force-pushed the update/edit-site-fetch-templates-client-side branch 3 times, most recently from f6123d7 to cfd7708 Compare May 13, 2020 22:21
@ockham ockham force-pushed the update/edit-site-fetch-templates-client-side branch 2 times, most recently from 79d43c2 to 72d292a Compare May 20, 2020 13:14
@ockham ockham marked this pull request as ready for review May 20, 2020 16:32
@ockham ockham requested a review from epiqueras May 20, 2020 16:33
@@ -321,7 +321,7 @@ function gutenberg_find_template_post_and_parts( $template_type, $template_hiera

if ( $current_template_post ) {
$template_part_ids = array();
if ( is_admin() ) {
if ( is_admin() || defined( 'REST_REQUEST' ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

REST_REQUEST isn't defined for internal requests done with rest_do_request and generally it isn't encouraged. I see that it is being used elsewhere in this function, but I think it'd be better to change this conditional to use a parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, I was indeed copy-pasting here. If permissible, I'd like to address this in a later iteration/separate PR, since it's pre-existing, and I'd have to read up a bit on this (and would rather not block this PR by that issue 😅 )

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I think it's just something to be looked at some point.

lib/template-parts.php Outdated Show resolved Hide resolved

const homeId = getEntityRecords( 'postType', 'wp_template', {
resolved: true,
slug: 'front-page',
Copy link
Contributor

Choose a reason for hiding this comment

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

front-page is not necessarily the home template. You need to make the same API call navigate-to-link makes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in ff313c4. Tho in the long run, I think we should modify the wp_template endpoint, or altenatively write a new endpoint that returns the template for a given entity or query.

@ockham ockham force-pushed the update/edit-site-fetch-templates-client-side branch from 583b19a to d8749dc Compare May 25, 2020 17:17
@ockham ockham requested a review from spacedmonkey as a code owner May 25, 2020 17:17
@ockham
Copy link
Contributor Author

ockham commented May 27, 2020

I'll pause work on this until next week, when I'm back from my maintenance rotation. Switching in an out of context for this PR clearly doesn't give me enough headspace to tackle the issues related to asynchronicity.

@ockham ockham force-pushed the update/edit-site-fetch-templates-client-side branch from 6e26ef7 to f51da73 Compare June 2, 2020 12:26
@ockham
Copy link
Contributor Author

ockham commented Jun 2, 2020

Going to rebase.

@ockham ockham force-pushed the update/edit-site-fetch-templates-client-side branch 2 times, most recently from 73101af to fd2c805 Compare June 2, 2020 13:05
@ockham ockham changed the title Edit Site: Fetch templates in Template Switcher from REST API Edit Site: Fetch template parts in Template Switcher from REST API Jun 2, 2020
@ockham
Copy link
Contributor Author

ockham commented Jun 2, 2020

Well, that was a big rebase 😅

Should be ready for another look 🤞

@ockham ockham requested a review from epiqueras June 3, 2020 19:22
@epiqueras
Copy link
Contributor

#22844 (comment)

@ockham
Copy link
Contributor Author

ockham commented Jun 3, 2020

#22844 (comment)

Thanks! As discussed per DM, I think that the natural progression for this PR would now be to move the template part fetching logic into a getTemplatePartId resolver. (Might be worth filing a fresh PR for that.)

Edit: As @epiqueras clarified via DM, we'll stick with this PR as-is (i.e. we won't move logic to resolvers.)

@ockham ockham force-pushed the update/edit-site-fetch-templates-client-side branch 2 times, most recently from 6c0fc8c to 91b0423 Compare June 4, 2020 12:19
@ockham
Copy link
Contributor Author

ockham commented Jun 4, 2020

Rebased. I ended up reverting the changes to lib/edit-site-page.php. The reason is that these now serve as initial state for the edit-site store, so we also need to remove a bunch of reducers/actions/selectors before we can remove them. Since that will touch a lot of files, I'm opting to do it in a follow-up PR.

Anyway, this PR should be ready for another look.

@ockham ockham force-pushed the update/edit-site-fetch-templates-client-side branch from ba283da to 59d0688 Compare June 4, 2020 20:02
@ockham ockham requested a review from epiqueras June 4, 2020 20:51
Comment on lines +76 to +82
findTemplate(
'/',
registry.__experimentalResolveSelect( 'core' ).getEntityRecords
).then(
( newHomeId ) => setHomeId( newHomeId ),
() => setHomeId( null )
);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's great that we now have the findTemplate util, but TBH, its signature (specifically, the need to pass in a selector as an argument) and async nature (requiring local component state) are among the reasons why I was opting for moving this kind of stuff into resolvers.

Not relevant for this PR, but I wanted to point it out in case we'd be willing to reconsider later.

Copy link
Contributor

Choose a reason for hiding this comment

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

We could wrap it in a custom hook too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Resolvers need to be tied to the store state, and it would be awkward to have these temporary values there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wouldn't introduce a dedicated resolver for findTemplate; I was specifically thinking of one for getTemplateParts( path ) that would call const templateId = yield findTemplate( path ) internally, and use that to fetch the template parts for that template.

Or we could even modify the entity endpoints for templates and template parts some more to accept a path arg, and do the template lookup on the server side.

Copy link
Contributor

Choose a reason for hiding this comment

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

Or we could even modify the entity endpoints for templates and template parts some more to accept a path arg, and do the template lookup on the server side.

That makes more sense don't you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Possibly, yeah. I'm not sure if it'd maybe bend the entities abstraction a bit much (if we keep adding rather specific custom args to it that change the query significantly), but maybe that's alright.

Think it still makes sense to merge this PR as a first step towards fetching data from the REST API (rather than from a server-passed JS variable)? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd rather avoid adding templateId as a parameter only to remove it later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense.

Unfortunately, doing the path-to-template resolution on the server side will be non-trivial AFAICS -- the get_{$type}_template() functions don't accept any args to pass entity specifiers (e.g. post/page IDs, category slugs, etc) but infer those from context (e.g. via get_queried_id()). Maybe it'll be possible to hook a filter into those, but there's a chance that we'll end up replicating a lot of that logic 🙁

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's leave it for later then.

@ockham
Copy link
Contributor Author

ockham commented Jun 5, 2020

Getting some weird (ES)Lint errors. I'll try rebasing.

@ockham ockham force-pushed the update/edit-site-fetch-templates-client-side branch from 6623b9f to c4d0ca3 Compare June 5, 2020 17:28
*
* @param array $args The query arguments.
* @param WP_REST_Request $request The request object.
* @return array Filtered $args.
*/
function filter_rest_wp_template_part_query( $args, $request ) {
if ( $request['resolved'] ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if ( $request['resolved'] ) {
if ( $request['resolved'] || $request['template'] ) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FTR, I was trying to retain consistency with lib/templates.php here, where we only check for resolved:

if ( $request['resolved'] ) {

...but also take the template slug into account:

$template_types = $request['slug'] ? $request['slug'] : get_template_types();

The difference is that in the case of templates, we can use the slug field that already exists (as part of the entities endpoint), so we need the resolved flag to convey the different semantics (only return 'resolved' templates that match the slug vs return all templates that match it (e.g. including all auto-drafts)).


We could theoretically assign a different meaning to a template parts query that has a template arg but not a resolved one -- it'd be all template parts for all templates matching that template slug (including auto-drafts etc) -- but I doubt that'd be very useful in practice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll apply this change and add a comment with the gist of the above.

Copy link
Contributor

Choose a reason for hiding this comment

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

For template parts, template implies resolved. I can't think of a case where the opposite would be useful.

template: _template,
templateParts: _template
? getEntityRecords( 'postType', 'wp_template_part', {
resolved: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
resolved: true,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ockham ockham force-pushed the update/edit-site-fetch-templates-client-side branch from df5546a to e3e9763 Compare June 8, 2020 20:44
@ockham ockham force-pushed the update/edit-site-fetch-templates-client-side branch from 00800f0 to 3419ed0 Compare June 9, 2020 10:26
@ockham ockham merged commit 670d800 into master Jun 9, 2020
@ockham ockham deleted the update/edit-site-fetch-templates-client-side branch June 9, 2020 13:00
@github-actions github-actions bot added this to the Gutenberg 8.4 milestone Jun 9, 2020
@ellatrix ellatrix mentioned this pull request Jun 16, 2020
12 tasks
This was referenced Jun 24, 2020
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.

3 participants