Skip to content

Commit 2b6cd61

Browse files
committed
REST API: Optimize themes collection response when querying active theme.
This 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()`. Developed in WordPress#11032 Follow up to r49925. Props aduth, mukesh27, westonruter. See #50152. Fixes #64719. git-svn-id: https://develop.svn.wordpress.org/trunk@61856 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 667ef1f commit 2b6cd61

2 files changed

Lines changed: 59 additions & 10 deletions

File tree

src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,18 +197,22 @@ public function get_item( $request ) {
197197
public function get_items( $request ) {
198198
$themes = array();
199199

200-
$active_themes = wp_get_themes();
201200
$current_theme = wp_get_theme();
202201
$status = $request['status'];
203202

204-
foreach ( $active_themes as $theme ) {
205-
$theme_status = ( $this->is_same_theme( $theme, $current_theme ) ) ? 'active' : 'inactive';
206-
if ( is_array( $status ) && ! in_array( $theme_status, $status, true ) ) {
207-
continue;
208-
}
209-
210-
$prepared = $this->prepare_item_for_response( $theme, $request );
203+
if ( array( 'active' ) === $status ) {
204+
$prepared = $this->prepare_item_for_response( $current_theme, $request );
211205
$themes[] = $this->prepare_response_for_collection( $prepared );
206+
} else {
207+
foreach ( wp_get_themes() as $theme ) {
208+
$theme_status = ( $this->is_same_theme( $theme, $current_theme ) ) ? 'active' : 'inactive';
209+
if ( is_array( $status ) && ! in_array( $theme_status, $status, true ) ) {
210+
continue;
211+
}
212+
213+
$prepared = $this->prepare_item_for_response( $theme, $request );
214+
$themes[] = $this->prepare_response_for_collection( $prepared );
215+
}
212216
}
213217

214218
$response = rest_ensure_response( $themes );

tests/phpunit/tests/rest-api/rest-themes-controller.php

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,53 @@ public function test_register_routes() {
163163
*
164164
* @ticket 45016
165165
* @ticket 61021
166-
* @ticket 62574.
166+
* @ticket 62574
167167
*/
168168
public function test_get_items() {
169+
wp_set_current_user( self::$admin_id );
170+
$request = new WP_REST_Request( 'GET', self::$themes_route );
171+
172+
$response = rest_get_server()->dispatch( $request );
173+
174+
$this->assertSame( 200, $response->get_status() );
175+
$data = $response->get_data();
176+
177+
$fields = array(
178+
'_links',
179+
'author',
180+
'author_uri',
181+
'description',
182+
'is_block_theme',
183+
'name',
184+
'requires_php',
185+
'requires_wp',
186+
'screenshot',
187+
'status',
188+
'stylesheet',
189+
'stylesheet_uri',
190+
'tags',
191+
'template',
192+
'template_uri',
193+
'textdomain',
194+
'theme_uri',
195+
'version',
196+
);
197+
$this->assertIsArray( $data );
198+
$this->assertNotEmpty( $data );
199+
$this->assertSameSets( $fields, array_keys( $data[0] ) );
200+
201+
$this->assertContains( 'twentytwenty', wp_list_pluck( $data, 'stylesheet' ) );
202+
$this->assertContains( get_stylesheet(), wp_list_pluck( $data, 'stylesheet' ) );
203+
}
204+
205+
/**
206+
* Test retrieving a collection of active themes.
207+
*
208+
* @ticket 64719
209+
*/
210+
public function test_get_items_active() {
211+
wp_set_current_user( self::$admin_id );
212+
169213
$response = self::perform_active_theme_request();
170214

171215
$this->assertSame( 200, $response->get_status() );
@@ -196,8 +240,9 @@ public function test_get_items() {
196240
'version',
197241
);
198242
$this->assertIsArray( $data );
199-
$this->assertNotEmpty( $data );
243+
$this->assertCount( 1, $data );
200244
$this->assertSameSets( $fields, array_keys( $data[0] ) );
245+
$this->assertEquals( array( 'rest-api' ), wp_list_pluck( $data, 'stylesheet' ) );
201246
}
202247

203248
/**

0 commit comments

Comments
 (0)