Make WordPress Core

Changeset 61856


Ignore:
Timestamp:
03/06/2026 06:02:38 PM (31 hours ago)
Author:
westonruter
Message:

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 https://github.com/WordPress/wordpress-develop/pull/11032

Follow up to r49925.

Props aduth, mukesh27, westonruter.
See #50152.
Fixes #64719.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php

    r61429 r61856  
    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
  • trunk/tests/phpunit/tests/rest-api/rest-themes-controller.php

    r59965 r61856  
    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
     
    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
Note: See TracChangeset for help on using the changeset viewer.