Make WordPress Core


Ignore:
Timestamp:
03/06/2026 06:02:38 PM (7 months 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.