Skip to content

Optimize themes collection response when querying active theme#11032

Open
aduth wants to merge 7 commits intoWordPress:trunkfrom
aduth:update/rest-themes-active
Open

Optimize themes collection response when querying active theme#11032
aduth wants to merge 7 commits intoWordPress:trunkfrom
aduth:update/rest-themes-active

Conversation

@aduth
Copy link
Member

@aduth aduth commented Feb 24, 2026

Updates WP_REST_Themes_Controller#get_items to shortcut returning the current theme when the request explicitly queries for the active theme, avoiding expensive call to wp_get_themes.

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

Use of AI Tools

No AI was used in the generating any of the code changes here, though I did use Claude Opus 4.6 as part of the research process in investigating whether an optimization was possible.


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

Hi @aduth! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description.

Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making.

More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook.

Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook.

If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook.

The Developer Hub also documents the various coding standards that are followed:

Thank you,
The WordPress Project

@github-actions
Copy link

github-actions bot commented Feb 24, 2026

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 aduth, westonruter.

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

Comment on lines +209 to +212
$theme_status = ( $this->is_same_theme( $theme, $current_theme ) ) ? 'active' : 'inactive';
if ( is_array( $status ) && ! in_array( $theme_status, $status, true ) ) {
continue;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

There might be some refactoring we could do here to avoid handling the active case, now that it's handled above.

Copy link
Member

Choose a reason for hiding this comment

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

It's not completely though, right? The $status can have both active and inactive:

'status' => array(
'description' => __( 'Limit result set to themes assigned one or more statuses.' ),
'type' => 'array',
'items' => array(
'enum' => array( 'active', 'inactive' ),
'type' => 'string',
),
),

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I wrote the comment at the time that I thought $request['status'] was a string value and not an array (prior to a695820), so it's less clear now that there's much refactor opportunity here.

@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

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

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

Minor suggestion, but otherwise looks like a great improvement.

Comment on lines +209 to +212
$theme_status = ( $this->is_same_theme( $theme, $current_theme ) ) ? 'active' : 'inactive';
if ( is_array( $status ) && ! in_array( $theme_status, $status, true ) ) {
continue;
}
Copy link
Member

Choose a reason for hiding this comment

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

It's not completely though, right? The $status can have both active and inactive:

'status' => array(
'description' => __( 'Limit result set to themes assigned one or more statuses.' ),
'type' => 'array',
'items' => array(
'enum' => array( 'active', 'inactive' ),
'type' => 'string',
),
),

Comment on lines 207 to 209
$active_themes = wp_get_themes();

foreach ( $active_themes as $theme ) {
Copy link
Member

Choose a reason for hiding this comment

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

Minor suggestion for conciseness and because $active_themes appears to have been misnamed, since it is all themes whether active or inactive:

Suggested change
$active_themes = wp_get_themes();
foreach ( $active_themes as $theme ) {
foreach ( wp_get_themes() as $theme ) {

Copy link
Member Author

Choose a reason for hiding this comment

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

Minor suggestion for conciseness and because $active_themes appears to have been misnamed, since it is all themes whether active or inactive:

Good idea 👍 Updated in 9bb7db9.

@westonruter
Copy link
Member

It might be good to add a unit test or assertion to which verifies that when the provided status is just active that only one theme ever is returned:

https://github.com/WordPress/wordpress-develop/blob/6727d1872251f5a0a5a96ce801cdbebdace318ad/tests/phpunit/tests/rest-api/rest-themes-controller.php

Concise, and avoids misnomer of "active" themes (vs. more accurately, all themes)

Co-Authored-By: Weston Ruter <134745+westonruter@users.noreply.github.com>
@aduth
Copy link
Member Author

aduth commented Feb 25, 2026

It might be good to add a unit test or assertion to which verifies that when the provided status is just active that only one theme ever is returned:

Yeah, I see there's a fair bit of indirect coverage of status=active because perform_active_theme_request is the primary helper used in the tests and sets this query. Though it could be good to have an explicit test for it, and also to your point about returning a single entry. We could alternatively make the assertion in test_get_items.

I had considering adding test coverage for the specific optimization here, especially after realizing with a695820 that I was "doing it wrong" and the condition never actually matched. I decided against it since this kind of change feels very much the "refactor" stage of TDD red-green-refactor optimizing within the bounds of existing test coverage, and that testing the optimization would ultimately lead to mocking or testing the implementation.

Indirectly tested in other tests via `perform_active_theme_request`, but this has additional coverage:
- Verify fields unique to active themes (default_template_part_areas, default_template_types, theme_supports)
- Verify that the returned array is exactly the one we expect
@aduth
Copy link
Member Author

aduth commented Feb 25, 2026

I added a test case for active themes request in 6d4f4e6 . Although I do feel like there's some redundancy with test_get_items. For example, there are some fields that are returned only for active theme (default_template_part_areas, default_template_types, theme_supports), which is already tested there. I wonder if it would be better to just move the $this->assertEquals( array( 'rest-api' ), wp_list_pluck( $data, 'stylesheet' ) ); into that existing test case to verify the single returned theme.

Co-authored-by: Weston Ruter <westonruter@gmail.com>
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.

2 participants