Skip to content

Conversation

@ellatrix
Copy link
Member

@ellatrix ellatrix commented Dec 30, 2024

Note: about 80% of the line changes are REST API fixture updates.

This backports the PHP changes from:

Summary:

  • Adds the active_templates setting, which is an object holding the template slug as a key and template post ID as the value.
  • To maintain backwards compatibility, any wp_template (post type) not created through the new API will be activated.
  • get_block_template and get_block_templates have been adjusted to check active_templates. These functions should never return inactive templates, just like before, to maintain backwards compatibility.
  • The pre-existing /templates endpoint and sub-endpoints remain and work exactly as before.
  • A new endpoint /wp_template has been added, but this is just a regular posts controller (WP_REST_Posts_Controller). We do register an additional theme field and expose the is_wp_suggestion meta.
  • Another new endpoint /wp_registered_template has been added, which is read-only and lists the registered templates from themes and plugin (un-edited, without activations applied).

Note: one thing to fix is creating a "fake" post type for wp_registered_template to create the entity client-side. We can address this after Beta 1. We should manually create the entity and manually register the endpoint.

Trac ticket: https://core.trac.wordpress.org/ticket/62755


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@github-actions
Copy link

github-actions bot commented Dec 30, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props ellatrix, shailu25, ntsekouras.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@ellatrix ellatrix force-pushed the backport/active-templates branch 4 times, most recently from e7fa52b to 3f3f40f Compare October 20, 2025 20:31
* @since 4.7.0
*/
function create_initial_rest_routes() {
global $wp_post_types;
Copy link
Member

Choose a reason for hiding this comment

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

Could you please add global documentation for global $wp_post_types ?

Copy link
Member Author

Choose a reason for hiding this comment

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

What would that documentation be? Looking through other instances of global $wp_post_types, I'm not seeing anything for those. 🤔

Copy link
Member

@shail-mehta shail-mehta Oct 21, 2025

Choose a reason for hiding this comment

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

Like this @global array $wp_post_types List of post types. ?

Reference PR: #72020

Copy link
Member Author

Choose a reason for hiding this comment

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

If it's ok I will leave this for a follow-up because we're very close to code freeze.

@ellatrix ellatrix force-pushed the backport/active-templates branch from e3d29b3 to 7b12619 Compare October 21, 2025 08:44
@ellatrix ellatrix force-pushed the backport/active-templates branch from 7b12619 to 0f85749 Compare October 21, 2025 13:06
Copy link

@ntsekouras ntsekouras left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

Comment on lines +52 to +56
// For wp_template, slugs no longer have to be unique within the same theme.
if ( 'wp_template' !== $post_type ) {
return $override_slug;
}

Copy link

Choose a reason for hiding this comment

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

This condition overrides the one above it. Should the one above be removed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I was too conservative in touching existing code

$template_files = _get_block_templates_files( 'wp_template', $query );
$query_result = array();

// _get_block_templates_files seems broken, it does not obey the query.
Copy link

Choose a reason for hiding this comment

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

Can we mark this somehow to see later if that function needs fixing?

pento pushed a commit that referenced this pull request Oct 21, 2025
* Adds the `active_templates` setting, which is an object holding the template slug as a key and template post ID as the value.
* To maintain backwards compatibility, any `wp_template` (post type) not created through the new API will be activated.
* `get_block_template` and `get_block_templates` have been adjusted to check `active_templates`. These functions should never return inactive templates, just like before, to maintain backwards compatibility.
* The pre-existing `/templates` endpoint and sub-endpoints remain and work exactly as before.
* A new endpoint `/wp_template` has been added, but this is just a regular posts controller (`WP_REST_Posts_Controller`). We do register an additional `theme` field and expose the `is_wp_suggestion` meta.
* Another new endpoint `/wp_registered_template` has been added, which is read-only and lists the registered templates from themes and plugin (un-edited, without activations applied).

These changes are to be iterated on.

See #8063.

Props ellatrix, shailu25, ntsekouras.
Fixes #62755.

git-svn-id: https://develop.svn.wordpress.org/trunk@61029 602fd350-edb4-49c9-b593-d223f7449a82
@github-actions
Copy link

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 61029
GitHub commit: 8e2c2db

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions bot closed this Oct 21, 2025
markjaquith pushed a commit to markjaquith/WordPress that referenced this pull request Oct 21, 2025
* Adds the `active_templates` setting, which is an object holding the template slug as a key and template post ID as the value.
* To maintain backwards compatibility, any `wp_template` (post type) not created through the new API will be activated.
* `get_block_template` and `get_block_templates` have been adjusted to check `active_templates`. These functions should never return inactive templates, just like before, to maintain backwards compatibility.
* The pre-existing `/templates` endpoint and sub-endpoints remain and work exactly as before.
* A new endpoint `/wp_template` has been added, but this is just a regular posts controller (`WP_REST_Posts_Controller`). We do register an additional `theme` field and expose the `is_wp_suggestion` meta.
* Another new endpoint `/wp_registered_template` has been added, which is read-only and lists the registered templates from themes and plugin (un-edited, without activations applied).

These changes are to be iterated on.

See WordPress/wordpress-develop#8063.

Props ellatrix, shailu25, ntsekouras.
Fixes #62755.
Built from https://develop.svn.wordpress.org/trunk@61029


git-svn-id: http://core.svn.wordpress.org/trunk@60365 1a063a9b-81f0-0310-95a4-ce76da25c4cd
return $changes;
}

function wp_maybe_activate_template( $post_id ) {
Copy link

Choose a reason for hiding this comment

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

Aside from documenting, we should audit these new functions to determine which ones to make private.

github-actions bot pushed a commit to platformsh/wordpress-performance that referenced this pull request Oct 21, 2025
* Adds the `active_templates` setting, which is an object holding the template slug as a key and template post ID as the value.
* To maintain backwards compatibility, any `wp_template` (post type) not created through the new API will be activated.
* `get_block_template` and `get_block_templates` have been adjusted to check `active_templates`. These functions should never return inactive templates, just like before, to maintain backwards compatibility.
* The pre-existing `/templates` endpoint and sub-endpoints remain and work exactly as before.
* A new endpoint `/wp_template` has been added, but this is just a regular posts controller (`WP_REST_Posts_Controller`). We do register an additional `theme` field and expose the `is_wp_suggestion` meta.
* Another new endpoint `/wp_registered_template` has been added, which is read-only and lists the registered templates from themes and plugin (un-edited, without activations applied).

These changes are to be iterated on.

See WordPress/wordpress-develop#8063.

Props ellatrix, shailu25, ntsekouras.
Fixes #62755.
Built from https://develop.svn.wordpress.org/trunk@61029


git-svn-id: https://core.svn.wordpress.org/trunk@60365 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Comment on lines +1361 to +1363
} elseif ( false === $active_templates[ $slug ] ) {
return null;
}
Copy link

@justlevine justlevine Oct 24, 2025

Choose a reason for hiding this comment

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

Is this nested correctly? We can only get here via the ! empty( $active_templates[ #slug ] ) on L1351 (maybe we want an isset there? )

Comment on lines +268 to +273
// Register the registered templates endpoint. For that we need to copy the
// wp_template post type so that it's available as an entity in core-data.
$wp_post_types['wp_registered_template'] = clone $wp_post_types['wp_template'];
$wp_post_types['wp_registered_template']->name = 'wp_registered_template';
$wp_post_types['wp_registered_template']->rest_base = 'wp_registered_template';
$wp_post_types['wp_registered_template']->rest_controller_class = 'WP_REST_Registered_Templates_Controller';
Copy link
Member

Choose a reason for hiding this comment

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

@ellatrix We need to pick a shorter post type slug which is 20 characters or less, for example wp_registered_tmpl.

I discovered that this causes a very esoteric error in unit tests. It was difficult to debug, but it comes down to this: wp_registered_template is too long as a post type slug. The maximum length of posts.post_type in the database schema is 20 characters. When attempting to insert a post with this wp_registered_template post type, a database error ensues:

WordPress database error: Processing the value for the following field failed: post_type. The supplied value may be too long or contains invalid data.

This causes the unit tests in the WordPress/performance repo to start to fail. For example:

1) Test_OD_Storage_Post_Type::test_delete_all_posts
Failed asserting that false is identical to 'bar'.

/var/www/html/wp-content/plugins/performance/plugins/optimization-detective/tests/storage/test-class-od-url-metrics-post-type.php:464

The unit test doesn't fail when run in isolation. But it runs when running with all the other unit tests. This is because the Test_OD_REST_URL_Metrics_Store_Endpoint tests call rest_get_server() which now has the effect of running this code here to add wp_registered_template to $wp_post_types. The Test_OD_REST_URL_Metrics_Store_Endpoint tests aren't resetting the global $wp_post_types back to their original value (as this wasn't clear it was needed), so later when Test_OD_Storage_Post_Type runs it is looping over all get_post_types() to create some test posts of each post type to ensure their data remains intact when OD_URL_Metrics_Post_Type::delete_all_posts() is called. However, this ends up failing in the unit test when $post_type is wp_registered_template with the above database error:

$other_post_ids = array_merge(
	$other_post_ids,
	self::factory()->post->create_many( 10, compact( 'post_type' ) )
);

Copy link
Member

Choose a reason for hiding this comment

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

Ah, it seems this is being removed in #10425

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.

6 participants