Optimize themes collection response when querying active theme#11032
Optimize themes collection response when querying active theme#11032aduth wants to merge 7 commits intoWordPress:trunkfrom
Conversation
|
Hi @aduth! 👋 Thank you for your contribution to WordPress! 💖 It looks like this is your first pull request to 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 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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
| $theme_status = ( $this->is_same_theme( $theme, $current_theme ) ) ? 'active' : 'inactive'; | ||
| if ( is_array( $status ) && ! in_array( $theme_status, $status, true ) ) { | ||
| continue; | ||
| } |
There was a problem hiding this comment.
There might be some refactoring we could do here to avoid handling the active case, now that it's handled above.
There was a problem hiding this comment.
It's not completely though, right? The $status can have both active and inactive:
There was a problem hiding this comment.
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.
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
westonruter
left a comment
There was a problem hiding this comment.
Minor suggestion, but otherwise looks like a great improvement.
| $theme_status = ( $this->is_same_theme( $theme, $current_theme ) ) ? 'active' : 'inactive'; | ||
| if ( is_array( $status ) && ! in_array( $theme_status, $status, true ) ) { | ||
| continue; | ||
| } |
There was a problem hiding this comment.
It's not completely though, right? The $status can have both active and inactive:
| $active_themes = wp_get_themes(); | ||
|
|
||
| foreach ( $active_themes as $theme ) { |
There was a problem hiding this comment.
Minor suggestion for conciseness and because $active_themes appears to have been misnamed, since it is all themes whether active or inactive:
| $active_themes = wp_get_themes(); | |
| foreach ( $active_themes as $theme ) { | |
| foreach ( wp_get_themes() as $theme ) { |
There was a problem hiding this comment.
Minor suggestion for conciseness and because
$active_themesappears to have been misnamed, since it is all themes whether active or inactive:
Good idea 👍 Updated in 9bb7db9.
|
It might be good to add a unit test or assertion to which verifies that when the provided status is just |
Concise, and avoids misnomer of "active" themes (vs. more accurately, all themes) Co-Authored-By: Weston Ruter <134745+westonruter@users.noreply.github.com>
Yeah, I see there's a fair bit of indirect coverage of 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
|
I added a test case for active themes request in 6d4f4e6 . Although I do feel like there's some redundancy with |
Co-authored-by: Weston Ruter <westonruter@gmail.com>
Updates
WP_REST_Themes_Controller#get_itemsto shortcut returning the current theme when the request explicitly queries for the active theme, avoiding expensive call towp_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.