Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Themes REST API endpoint: Add stylesheet_uri and template_uri fields to the response #6399

Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ public function get_items( $request ) {
*
* @since 5.0.0
* @since 5.9.0 Renamed `$theme` to `$item` to match parent class for PHP 8 named parameter support.
* @since 6.6.0 Added `stylesheet_uri` and `template_uri` fields.
*
* @param WP_Theme $item Theme object.
* @param WP_REST_Request $request Request object.
Expand Down Expand Up @@ -331,6 +332,22 @@ public function prepare_item_for_response( $item, $request ) {
$data['is_block_theme'] = $theme->is_block_theme();
}

if ( rest_is_field_included( 'stylesheet_uri', $fields ) ) {
if ( $this->is_same_theme( $theme, $current_theme ) ) {
$data['stylesheet_uri'] = get_stylesheet_directory_uri();
} else {
$data['stylesheet_uri'] = $theme->get_stylesheet_directory_uri();
}
peterwilsoncc marked this conversation as resolved.
Show resolved Hide resolved
}

if ( rest_is_field_included( 'template_uri', $fields ) ) {
if ( $this->is_same_theme( $theme, $current_theme ) ) {
$data['template_uri'] = get_template_directory_uri();
} else {
$data['template_uri'] = $theme->get_template_directory_uri();
}
}

$data = $this->add_additional_fields_to_object( $data, $request );

// Wrap the data in a response object.
Expand Down Expand Up @@ -447,11 +464,23 @@ public function get_item_schema() {
'type' => 'string',
'readonly' => true,
),
'stylesheet_uri' => array(
'description' => __( 'The uri for the theme\'s stylesheet directory.' ),
'type' => 'string',
TimothyBJacobs marked this conversation as resolved.
Show resolved Hide resolved
'format' => 'uri',
'readonly' => true,
),
'template' => array(
'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.' ),
'type' => 'string',
'readonly' => true,
),
'template_uri' => array(
'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.' ),
'type' => 'string',
'format' => 'uri',
'readonly' => true,
),
'author' => array(
'description' => __( 'The theme author.' ),
'type' => 'object',
Expand Down
75 changes: 74 additions & 1 deletion tests/phpunit/tests/rest-api/rest-themes-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public function test_register_routes() {
* Test retrieving a collection of themes.
*
* @ticket 45016
* @ticket 61021
*/
public function test_get_items() {
$response = self::perform_active_theme_request();
Expand All @@ -182,8 +183,10 @@ public function test_get_items() {
'screenshot',
'status',
'stylesheet',
'stylesheet_uri',
'tags',
'template',
'template_uri',
'textdomain',
'theme_supports',
'theme_uri',
Expand All @@ -198,6 +201,7 @@ public function test_get_items() {
* Test retrieving a collection of inactive themes.
*
* @ticket 50152
* @ticket 61021
*/
public function test_get_items_inactive() {
wp_set_current_user( self::$admin_id );
Expand All @@ -221,8 +225,10 @@ public function test_get_items_inactive() {
'screenshot',
'status',
'stylesheet',
'stylesheet_uri',
'tags',
'template',
'template_uri',
'textdomain',
'theme_uri',
'version',
Expand Down Expand Up @@ -347,12 +353,13 @@ public function test_prepare_item() {
* Verify the theme schema.
*
* @ticket 45016
* @ticket 61021
*/
public function test_get_item_schema() {
$response = self::perform_active_theme_request( 'OPTIONS' );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertCount( 16, $properties );
$this->assertCount( 18, $properties );

$this->assertArrayHasKey( 'author', $properties );
$this->assertArrayHasKey( 'raw', $properties['author']['properties'] );
Expand All @@ -377,13 +384,15 @@ public function test_get_item_schema() {
$this->assertArrayHasKey( 'screenshot', $properties );
$this->assertArrayHasKey( 'status', $properties );
$this->assertArrayHasKey( 'stylesheet', $properties );
$this->assertArrayHasKey( 'stylesheet_uri', $properties );

$this->assertArrayHasKey( 'tags', $properties );
$this->assertArrayHasKey( 'raw', $properties['tags']['properties'] );
$this->assertArrayHasKey( 'items', $properties['tags']['properties']['raw'] );
$this->assertArrayHasKey( 'rendered', $properties['tags']['properties'] );

$this->assertArrayHasKey( 'template', $properties );
$this->assertArrayHasKey( 'template_uri', $properties );
$this->assertArrayHasKey( 'textdomain', $properties );
$this->assertArrayHasKey( 'theme_supports', $properties );

Expand Down Expand Up @@ -534,6 +543,37 @@ public function test_theme_stylesheet() {
$this->assertSame( 'rest-api', $result[0]['stylesheet'] );
}

/**
* @ticket 61021
*/
public function test_theme_stylesheet_uri() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', self::$themes_route );
$request->set_param( 'status', array( 'active', 'inactive' ) );

$response = rest_get_server()->dispatch( $request );
$result = $response->get_data();
$current_theme = wp_get_theme();

foreach ( $result as $theme_result ) {
$this->assertArrayHasKey( 'stylesheet_uri', $theme_result );
if ( 'active' === $theme_result['status'] ) {
$this->assertSame(
get_stylesheet_directory_uri(),
$theme_result['stylesheet_uri'],
'stylesheet_uri for an active theme should be the same as the global get_stylesheet_directory_uri()'
);
} else {
$theme = wp_get_theme( $theme_result['stylesheet'] );
$this->assertSame(
$theme->get_stylesheet_directory_uri(),
$theme_result['stylesheet_uri'],
"stylesheet_uri for an inactive theme should be the same as the theme's get_stylesheet_directory_uri() method"
);
}
}
}

/**
* @ticket 49906
*/
Expand All @@ -555,6 +595,37 @@ public function test_theme_template() {
$this->assertSame( 'default', $result[0]['template'] );
}

/**
* @ticket 61021
*/
public function test_theme_template_uri() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', self::$themes_route );
$request->set_param( 'status', array( 'active', 'inactive' ) );

$response = rest_get_server()->dispatch( $request );
$result = $response->get_data();
$current_theme = wp_get_theme();

foreach ( $result as $theme_result ) {
$this->assertArrayHasKey( 'template_uri', $theme_result );
if ( 'active' === $theme_result['status'] ) {
$this->assertSame(
get_template_directory_uri(),
$theme_result['template_uri'],
'template_uri for an active theme should be the same as the global get_template_directory_uri()'
);
} else {
$theme = wp_get_theme( $theme_result['stylesheet'] );
$this->assertSame(
$theme->get_template_directory_uri(),
$theme_result['template_uri'],
"template_uri for an inactive theme should be the same as the theme's get_template_directory_uri() method"
);
}
}
}

/**
* @ticket 49906
*/
Expand Down Expand Up @@ -1273,8 +1344,10 @@ public function test_get_item() {
'screenshot',
'status',
'stylesheet',
'stylesheet_uri',
'tags',
'template',
'template_uri',
'textdomain',
'theme_uri',
'version',
Expand Down
Loading