Make WordPress Core

Changeset 58282


Ignore:
Timestamp:
06/02/2024 07:25:03 PM (2 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Add stylesheet and template URI fields to the Themes API.

Props andrewserong, timothyblynjacobs, noisysocks, ramonopoly, peterwilsoncc, Dharm1025.
Fixes #61021.

Location:
trunk
Files:
2 edited

Legend:

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

    r56586 r58282  
    225225         * @since 5.0.0
    226226         * @since 5.9.0 Renamed `$theme` to `$item` to match parent class for PHP 8 named parameter support.
     227         * @since 6.6.0 Added `stylesheet_uri` and `template_uri` fields.
    227228         *
    228229         * @param WP_Theme        $item    Theme object.
     
    330331                if ( rest_is_field_included( 'is_block_theme', $fields ) ) {
    331332                        $data['is_block_theme'] = $theme->is_block_theme();
     333                }
     334
     335                if ( rest_is_field_included( 'stylesheet_uri', $fields ) ) {
     336                        if ( $this->is_same_theme( $theme, $current_theme ) ) {
     337                                $data['stylesheet_uri'] = get_stylesheet_directory_uri();
     338                        } else {
     339                                $data['stylesheet_uri'] = $theme->get_stylesheet_directory_uri();
     340                        }
     341                }
     342
     343                if ( rest_is_field_included( 'template_uri', $fields ) ) {
     344                        if ( $this->is_same_theme( $theme, $current_theme ) ) {
     345                                $data['template_uri'] = get_template_directory_uri();
     346                        } else {
     347                                $data['template_uri'] = $theme->get_template_directory_uri();
     348                        }
    332349                }
    333350
     
    448465                                        'readonly'    => true,
    449466                                ),
     467                                'stylesheet_uri' => array(
     468                                        'description' => __( 'The uri for the theme\'s stylesheet directory.' ),
     469                                        'type'        => 'string',
     470                                        'format'      => 'uri',
     471                                        'readonly'    => true,
     472                                ),
    450473                                'template'       => array(
    451474                                        'description' => __( 'The theme\'s template. If this is a child theme, this refers to the parent theme, otherwise this is the same as the theme\'s stylesheet.' ),
    452475                                        'type'        => 'string',
     476                                        'readonly'    => true,
     477                                ),
     478                                'template_uri'       => array(
     479                                        'description' => __( 'The uri for the theme\'s template directory. If this is a child theme, this refers to the parent theme, otherwise this is the same as the theme\'s stylesheet directory.' ),
     480                                        'type'        => 'string',
     481                                        'format'      => 'uri',
    453482                                        'readonly'    => true,
    454483                                ),
  • trunk/tests/phpunit/tests/rest-api/rest-themes-controller.php

    r57987 r58282  
    163163         *
    164164         * @ticket 45016
     165         * @ticket 61021
    165166         */
    166167        public function test_get_items() {
     
    183184                        'status',
    184185                        'stylesheet',
     186                        'stylesheet_uri',
    185187                        'tags',
    186188                        'template',
     189                        'template_uri',
    187190                        'textdomain',
    188191                        'theme_supports',
     
    199202         *
    200203         * @ticket 50152
     204         * @ticket 61021
    201205         */
    202206        public function test_get_items_inactive() {
     
    222226                        'status',
    223227                        'stylesheet',
     228                        'stylesheet_uri',
    224229                        'tags',
    225230                        'template',
     231                        'template_uri',
    226232                        'textdomain',
    227233                        'theme_uri',
     
    348354         *
    349355         * @ticket 45016
     356         * @ticket 61021
    350357         */
    351358        public function test_get_item_schema() {
     
    353360                $data       = $response->get_data();
    354361                $properties = $data['schema']['properties'];
    355                 $this->assertCount( 16, $properties );
     362                $this->assertCount( 18, $properties );
    356363
    357364                $this->assertArrayHasKey( 'author', $properties );
     
    378385                $this->assertArrayHasKey( 'status', $properties );
    379386                $this->assertArrayHasKey( 'stylesheet', $properties );
     387                $this->assertArrayHasKey( 'stylesheet_uri', $properties );
    380388
    381389                $this->assertArrayHasKey( 'tags', $properties );
     
    385393
    386394                $this->assertArrayHasKey( 'template', $properties );
     395                $this->assertArrayHasKey( 'template_uri', $properties );
    387396                $this->assertArrayHasKey( 'textdomain', $properties );
    388397                $this->assertArrayHasKey( 'theme_supports', $properties );
     
    536545
    537546        /**
     547         * @ticket 61021
     548         */
     549        public function test_theme_stylesheet_uri() {
     550                wp_set_current_user( self::$admin_id );
     551                $request = new WP_REST_Request( 'GET', self::$themes_route );
     552                $request->set_param( 'status', array( 'active', 'inactive' ) );
     553
     554                $response      = rest_get_server()->dispatch( $request );
     555                $result        = $response->get_data();
     556                $current_theme = wp_get_theme();
     557
     558                foreach ( $result as $theme_result ) {
     559                        $this->assertArrayHasKey( 'stylesheet_uri', $theme_result );
     560                        if ( 'active' === $theme_result['status'] ) {
     561                                $this->assertSame(
     562                                        get_stylesheet_directory_uri(),
     563                                        $theme_result['stylesheet_uri'],
     564                                        'stylesheet_uri for an active theme should be the same as the global get_stylesheet_directory_uri()'
     565                                );
     566                        } else {
     567                                $theme = wp_get_theme( $theme_result['stylesheet'] );
     568                                $this->assertSame(
     569                                        $theme->get_stylesheet_directory_uri(),
     570                                        $theme_result['stylesheet_uri'],
     571                                        "stylesheet_uri for an inactive theme should be the same as the theme's get_stylesheet_directory_uri() method"
     572                                );
     573                        }
     574                }
     575        }
     576
     577        /**
    538578         * @ticket 49906
    539579         */
     
    554594                $this->assertArrayHasKey( 'template', $result[0] );
    555595                $this->assertSame( 'default', $result[0]['template'] );
     596        }
     597
     598        /**
     599         * @ticket 61021
     600         */
     601        public function test_theme_template_uri() {
     602                wp_set_current_user( self::$admin_id );
     603                $request = new WP_REST_Request( 'GET', self::$themes_route );
     604                $request->set_param( 'status', array( 'active', 'inactive' ) );
     605
     606                $response      = rest_get_server()->dispatch( $request );
     607                $result        = $response->get_data();
     608                $current_theme = wp_get_theme();
     609
     610                foreach ( $result as $theme_result ) {
     611                        $this->assertArrayHasKey( 'template_uri', $theme_result );
     612                        if ( 'active' === $theme_result['status'] ) {
     613                                $this->assertSame(
     614                                        get_template_directory_uri(),
     615                                        $theme_result['template_uri'],
     616                                        'template_uri for an active theme should be the same as the global get_template_directory_uri()'
     617                                );
     618                        } else {
     619                                $theme = wp_get_theme( $theme_result['stylesheet'] );
     620                                $this->assertSame(
     621                                        $theme->get_template_directory_uri(),
     622                                        $theme_result['template_uri'],
     623                                        "template_uri for an inactive theme should be the same as the theme's get_template_directory_uri() method"
     624                                );
     625                        }
     626                }
    556627        }
    557628
     
    12741345                        'status',
    12751346                        'stylesheet',
     1347                        'stylesheet_uri',
    12761348                        'tags',
    12771349                        'template',
     1350                        'template_uri',
    12781351                        'textdomain',
    12791352                        'theme_uri',
Note: See TracChangeset for help on using the changeset viewer.