Make WordPress Core

Opened 5 months ago

Closed 6 weeks ago

#62574 closed task (blessed) (fixed)

Move default template types and template part areas to REST API

Reported by: gigitux's profile gigitux Owned by: mamaduka's profile Mamaduka
Milestone: 6.8 Priority: normal
Severity: normal Version:
Component: Editor Keywords: has-patch gutenberg-merge has-unit-tests 2nd-opinion
Focuses: Cc:

Description

The default template types and template part areas are currently preloaded via the settings object when the site editor is loaded (see https://github.com/WordPress/wordpress-develop/blob/1aa41dea3345c57cffce9059bbad728c86d1471a/src/wp-admin/site-editor.php#L64-L65). To make these variables accessible to other components, even when the site editor isn’t loaded, they should be moved to the REST API.

Change History (44)

This ticket was mentioned in PR #7895 on WordPress/wordpress-develop by @gigitux.


5 months ago
#1

  • Keywords has-patch added

#2 in reply to: ↑ description @gigitux
5 months ago

This work has already been merged in Gutenberg: https://github.com/WordPress/gutenberg/pull/66459

#3 @gigitux
5 months ago

This PR (https://github.com/WordPress/wordpress-develop/pull/7895) introduces the following changes:

Note:
The second change might be a breaking change. Should we reconsider its inclusion?

Last edited 5 months ago by gigitux (previous) (diff)

#4 @youknowriad
5 months ago

  • Milestone changed from Awaiting Review to 6.8

@youknowriad commented on PR #7895:


5 months ago
#5

How do you feel about adding some assertions to tests/phpunit/tests/rest-api/rest-server.php test_get_index test?

@gigitux commented on PR #7895:


5 months ago
#6

Sounds great! Thanks for the review, @youknowriad 🙇

#8 @joemcgill
3 months ago

  • Keywords gutenberg-merge added

#9 @peterwilsoncc
2 months ago

  • Keywords changed from has-patch, gutenberg-merge to has-patch gutenberg-merge

The associated PR doesn't make the fields available to the endpoint by default which is a little counter intuitive. In order to access them, a developer must include the usually optional ?_fields parameter. /wp-json/?_fields=default_template_part_areas,default_template_types

By requiring the additional parameters, it seems like it's adding a new endpoint by other means.

I'm also not convinced the site index is the best place for these, even if loaded by default, template parts and areas are very theme orientated so the themes controller seems a better place for them.

#10 @gigitux
2 months ago

We discuss this topic on the Gutenberg PR: https://github.com/WordPress/gutenberg/pull/66459#issuecomment-2457097527

cc @Mamaduka

#11 @peterwilsoncc
2 months ago

I don't really think the discussion on the Gutenberg PR was resolved beyond let's move on to avoid blocking the ticket. As the Gutenberg plugin is bleeding edge WP, that's fine for the plugin as it doesn't have the same backward compatibility promises that come with code released in WordPress stable.

A concern I have with including this data on a public endpoint is that WP can't assume that the data is intended to be public, themes and plugins can use various filters to define custom templates for data that is intended to be private.

I'm also concerned about creating a faux endpoint, as explained above, as a shortcut to creating a new endpoint, using a more appropriate endpoint.

#12 follow-up: @Mamaduka
2 months ago

The REST API index is the only option for read-only settings until #48885 is resolved.

The associated PR doesn't make the fields available to the endpoint by default which is a little counter intuitive. In order to access them, a developer must include the usually optional ?_fields parameter.

IIRC, if you request without fields, all fields are returned, including new fields proposed in this PR.

#13 in reply to: ↑ 12 @peterwilsoncc
2 months ago

Replying to Mamaduka:

The REST API index is the only option for read-only settings until #48885 is resolved.

I think it best to resolve this prior to moving these to the REST API, or to make the data available on the templates endpoint, theme endpoint or similar.

The associated PR doesn't make the fields available to the endpoint by default which is a little counter intuitive. In order to access them, a developer must include the usually optional ?_fields parameter.

IIRC, if you request without fields, all fields are returned, including new fields proposed in this PR.

I've double checked using the WP Playground for the attached PR and if you search for the relevant strings for the default rest endpoint, you'll see that they are missing.

#14 @gigitux
2 months ago

I think it best to resolve this prior to moving these to the REST API, or to make the data available on the templates endpoint, theme endpoint or similar.

Unfortunately, I don’t have the bandwidth to work on this refactor right now.

This ticket was mentioned in Slack in #core-restapi by joemcgill. View the logs.


2 months ago

#16 @joemcgill
2 months ago

To help move this forward, I think tying this to the ongoing discussion in #48885 conflates two unrelated issues.

That ticket is specifically about how to expose read only access to registered settings (i.e. options). Given that the default template types and allowed template part areas are not stored as options, but are instead defined in code, the same concerns don't apply here, as these are not "settings" in the way the wp/v2/settings endpoint defines them.

That said, I think question that does still need to be answered is where is the appropriate place to expose this data since wp/v2/settings is inappropriate and wp/v2/templates and wp/v2/template-parts are meant to return a representation of a WP_Block_Template object.

I'd be more comfortable adding this to the index if were only exposed to logged in users given that these values can be modified by themes and plugins to include data that may not be meant to be public. If doing so, I would add this as a filter on the rest_index callback, similar to how the application passwords data is added via rest_add_application_passwords_to_index().

This ticket was mentioned in Slack in #core by joemcgill. View the logs.


2 months ago

#18 @Mamaduka
8 weeks ago

Since the beta one date approaches fast, it would be nice to have final confirmation on this.

I'd be more comfortable adding this to the index if were only exposed to logged in users given that these values can be modified by themes and plugins to include data that may not be meant to be public. If doing so, I would add this as a filter on the rest_index callback, similar to how the application passwords data is added via rest_add_application_passwords_to_index().

@TimothyBlynJacobs, @spacedmonkey, what do you think going with this solution?

This ticket was mentioned in Slack in #core by joemcgill. View the logs.


8 weeks ago

This ticket was mentioned in PR #8422 on WordPress/wordpress-develop by @Mamaduka.


8 weeks ago
#20

  • Keywords has-unit-tests added

Alternative to #7895.

Limits access to the default_template_part_areas and default_template_types values. They are now only available to logged-in users viathe REST index.

Gutenberg PR: https://github.com/WordPress/gutenberg/pull/69346
Trac ticket: https://core.trac.wordpress.org/ticket/62574

#21 @Mamaduka
8 weeks ago

Created an alternative PR based on the latest discussions.

#22 @Benjamin_Zekavica
8 weeks ago

@Mamaduka I agree with Joe and support this suggestion: It's better to expose this data only to logged-in users since themes and plugins can modify these values, and they may not be meant to be public. If so, I would add it as a filter in the rest_index callback, like how application passwords are handled.

#23 @Mamaduka
7 weeks ago

@Benjamin_Zekavica, the new PR does exactly the same. See: https://github.com/WordPress/wordpress-develop/pull/8422

#24 @Benjamin_Zekavica
7 weeks ago

@Mamaduka Thank you :) For me it looks good.

Last edited 7 weeks ago by Benjamin_Zekavica (previous) (diff)

#25 @TimothyBlynJacobs
7 weeks ago

I still really do not think this is the right place for the data for the reasons previously stated. So I guess my question is what is the pressure for moving this bit of data to the REST API for 6.8? It looks like there are a number of things that are still in the custom settings array, so it's not like we are getting away from it entirely.

This ticket was mentioned in Slack in #core by audrasjb. View the logs.


7 weeks ago

#27 @audrasjb
7 weeks ago

As per today's bug scrub: just wanted to inform committers that we're still keeping this ticket open in the milestone until beta 1 tomorrow, then it will be moved to milestone 6.9 if needed.

#28 @joemcgill
7 weeks ago

  • Keywords 2nd-opinion added
  • Owner changed from gigitux to Mamaduka

@TimothyBlynJacobs my understanding is that the motivation for moving this data to the REST API was explained in the related Gutenberg PR

This PR is necessary to remove the core/editor dependency across the application. In the specific, this refactor is needed to allow the package fields to have access to the defaultTemplateTypes, defaultTemplatePartAreas without that editor settings are initialized. More details here: #65390 (comment)

The idea being that the fields package should not be dependent on whether a block editor currently rendered in order to access this data.

@Mamaduka do you have any insight into whether continuing to rely on the settings array would break the expected behavior of any features shipping in 6.8?

@TimothyBlynJacobs apologies for asking you to repeat yourself, but could you reiterate why you don't think the index is the right place for this data? It was clear why this data didn't belong on the /settings endpoint, but not why exposing it on the index should be avoided. As I read the code, this data is part of the default configuration of the WordPress application that is common to all block themes and not specific to any particular theme, so finding an appropriate place to expose this data is proving challenging.

#29 @peterwilsoncc
7 weeks ago

I'm happier with the approach of limiting the data to logged in users (I've made a caps suggestion on the PR but that is it).

I'm still unhappy about placing the data in the wp-json/ endpoint as a default because a better place can't be found. The data is strongly theme related and there aren't any significant ill-effects by waiting until an improved location can be found.

Behind a login is an improvement because there may be sensitive data in the output, depending on how a third party developer is using them. It's low but not no risk.

My big concern is that we can't use the REST index as a too hard basket when data doesn't quite belong anywhere else.

@Mamaduka commented on PR #8422:


7 weeks ago
#30

Update rest_add_templates_default_data_to_index to use edit_posts capability. The tests are now using the data provider with various roles.

#31 @Mamaduka
7 weeks ago

I've updated the PR based on the latest feedback. It should be ready for final review.

I understand that the REST index might not be the best place, but it's the best one we've got for now.

Historically, editor settings were used for the same reason, but they require coupling with those packages. Low-level packages like fields should not depend on high-level packages like editor or block-editor. This is one reason JS packages are trying to move away from editor settings bootstrapped from the server.

#32 @TimothyBlynJacobs
7 weeks ago

Thanks @joemcgill!

my understanding is that the motivation for moving this data to the REST API was explained in the related Gutenberg PR

This is helpful, and I get why we want to move things to the REST API long term, and I very much agree with that. I'm just not totally understanding the pressure for getting this to land in 6.8. Why can't the existing method of adding it to editor settings continue to work for this release?

apologies for asking you to repeat yourself, but could you reiterate why you don't think the index is the right place for this data?

No worries at all. I have similar thoughts to Peter.

It seems like it would be fairly straightforward to drop this in to the Themes controller? We'd add it only to the active theme, similar to how we do Theme Supports for instance.

This ticket was mentioned in Slack in #core by joemcgill. View the logs.


7 weeks ago

#34 @joemcgill
7 weeks ago

  • Type changed from enhancement to task (blessed)

Thanks all. After discussing with @Mamaduka in Slack, we are not going to move forward with the current PR for 6.8 Beta 1.

My understanding is that this currently results in the current @wordpress/fields package synced to trunk being broken when used outside an editor where this data is being bootstrapped by editor settings, so some follow-up will be needed during this release regardless. I'm going to convert this to a task ticket in the meantime, so this stays visible.

@peterwilsoncc opened this related issue where we can follow-up on how to handle the package updates. If we are able to resolve the situation by including this data in the REST API, I think that would be preferable, so this could be resolved in this release.

#35 @Mamaduka
7 weeks ago

Thanks for the summary, @joemcgill!

I'm going to prepare PRs for the following solutions:

  1. Expose/consume this data via themes endpoint for the current active theme, as suggested by @TimothyBlynJacobs.
  2. Revert data consumption to editor settings by reverting all client-side changes from https://github.com/WordPress/gutenberg/pull/68781. The plugin will experiment with different solutions for consuming data via REST API (for future WP releases). This is just in case plan A is rejected, and we need something 100% functional for WP 6.8.
Last edited 7 weeks ago by Mamaduka (previous) (diff)

This ticket was mentioned in Slack in #core-editor by mamaduka. View the logs.


7 weeks ago

#37 @Mamaduka
7 weeks ago

Here's the Gutenberg PR for exposing this data via themes endpoint - https://github.com/WordPress/gutenberg/pull/69417.

This ticket was mentioned in Slack in #core by joemcgill. View the logs.


7 weeks ago

#39 @peterwilsoncc
7 weeks ago

I've approved the PR on the GB repo, thanks folks for working through the decision of where the data was best placed.

@Mamaduka commented on PR #8422:


6 weeks ago
#41

Closing in favor of #8480.

@Mamaduka commented on PR #7895:


6 weeks ago
#42

Closing in favor of #8480.

@joemcgill commented on PR #8480:


6 weeks ago
#43

Refreshed this PR to include the package updates synced to trunk for better testing.

#44 @Mamaduka
6 weeks ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 59965:

REST API: Add additional default template data fields for the active theme.

The active theme(s) now return two additional properties, default_template_types and default_template_part_areas, in the REST response.

Props mamaduka, joemcgill, timothyblynjacobs, audrasjb, gigitux, peterwilsoncc, youknowriad, jorbin.
Fixes #62574.

Note: See TracTickets for help on using tickets.