Make WordPress Core

Opened 9 hours ago

Last modified 9 hours ago

#66381 new enhancement

REST API: Template and template part collections fetch every template again to build its links

Reported by: nofearinc Owned by:
Priority: normal Milestone: Awaiting Review
Component: REST API Version: 6.4
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses: performance

Description

WP_REST_Templates_Controller::prepare_item_for_response() passes only the template ID to prepare_links(). When the post type supports revisions, prepare_links() calls get_block_template() for every item, only to read wp_id for the revision links (added in [56819], #56922). That is another WP_Query per item, and for theme file templates another file read and Block Hooks pass, and the result is discarded.

Measured on 7.2-beta1 with Ipsum, as admin, rest_do_request(), median of 5:

Endpoint Queries Time
/wp/v2/template-parts (8 items) 9 to 1 12.7 to 6.5 ms
/wp/v2/templates (11 items) 17 to 6 23.3 to 17.2 ms

The patch hands the template that is being prepared to prepare_links() through a private property, so the prepare_links( $id ) signature stays the same for subclasses, and falls back to get_block_template() otherwise. The response is unchanged; a test checks that the revision links are still there. PR: ​https://github.com/WordPress/wordpress-develop/pull/14849

AI assistance: Claude Code (Claude Opus 5.5) for finding this, drafting the patch and tests, and measuring. I reviewed the patch.

Change History (1)

This ticket was mentioned in ​PR #14849 on ​WordPress/wordpress-develop by ​@nofearinc.


9 hours ago
#1

WP_REST_Templates_Controller::prepare_links() receives only the template ID. When the post type supports revisions, it called get_block_template() for every item only to read wp_id for the revision links ([56819]): another WP_Query per item, and for theme file templates another file read and Block Hooks pass, all discarded.

This reuses the template that prepare_item_for_response() is already preparing, handed over through a private property so the prepare_links( $id ) signature stays the same for subclasses.

Measured on 7.2-beta1 with Ipsum, as admin, rest_do_request(), median of 5:

Endpoint Queries Time
/wp/v2/template-parts (8 items) 9 to 1 12.7 to 6.5 ms
/wp/v2/templates (11 items) 17 to 6 23.3 to 17.2 ms

Testing

  • vendor/bin/phpunit --filter test_get_items_should_not_refetch_each_template_for_links fails without the patch and passes with it; a second test checks that the revision links are unchanged.
  • vendor/bin/phpunit --filter Tests_REST_WpRestTemplatesController (52 tests) and --group block-templates (130 tests) pass.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: finding the issue, drafting the fix and tests, and measuring. I reviewed every line.

---
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.

🤖 Generated with ​Claude Code

Note: See TracTickets for help on using tickets.