Skip to content

Commit

Permalink
REST API: Support subdirectory themes in the Themes controller.
Browse files Browse the repository at this point in the history
This allows for themes that are included inside of a subdirectory, for example `subdir/my-theme`, to be accessed via the single item route of the `/wp/v2/themes` controller.

Fixes #54349.


git-svn-id: https://develop.svn.wordpress.org/trunk@52017 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
TimothyBJacobs committed Nov 5, 2021
1 parent 827dd20 commit ca7450d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
class WP_REST_Themes_Controller extends WP_REST_Controller {

const PATTERN = '[^.\/]+(?:\/[^.\/]+)?';

/**
* Constructor.
*
Expand Down Expand Up @@ -50,7 +52,7 @@ public function register_routes() {

register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<stylesheet>[\w-]+)',
sprintf( '/%s/(?P<stylesheet>%s)', $this->rest_base, self::PATTERN ),
array(
'args' => array(
'stylesheet' => array(
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/rest-api/rest-schema-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function test_expected_routes_in_schema() {
'/wp/v2/templates/(?P<parent>[\d]+)/revisions',
'/wp/v2/templates/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)',
'/wp/v2/themes',
'/wp/v2/themes/(?P<stylesheet>[\w-]+)',
'/wp/v2/themes/(?P<stylesheet>[^.\/]+(?:\/[^.\/]+)?)',
'/wp/v2/plugins',
'/wp/v2/plugins/(?P<plugin>[^.\/]+(?:\/[^.\/]+)?)',
'/wp/v2/block-directory/search',
Expand Down
41 changes: 40 additions & 1 deletion tests/phpunit/tests/rest-api/rest-themes-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ public function set_up() {
public function test_register_routes() {
$routes = rest_get_server()->get_routes();
$this->assertArrayHasKey( self::$themes_route, $routes );
$this->assertArrayHasKey( self::$themes_route . '/(?P<stylesheet>[\\w-]+)', $routes );
$this->assertArrayHasKey(
sprintf( '%s/(?P<stylesheet>%s)', self::$themes_route, WP_REST_Themes_Controller::PATTERN ),
$routes
);
}

/**
Expand Down Expand Up @@ -1281,6 +1284,42 @@ public function test_get_active_item_as_contributor() {
$this->assertSame( 200, $response->get_status() );
}

/**
* @ticket 54349
*/
public function test_get_item_subdirectory_theme() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', self::$themes_route . '/subdir/theme2' );
$response = rest_do_request( $request );

$this->assertSame( 200, $response->get_status() );
$this->assertSame( 'My Subdir Theme', $response->get_data()['name']['raw'] );
}

/**
* @ticket 54349
*/
public function test_can_support_further_routes() {
register_rest_route(
'wp/v2',
sprintf( '/themes/(?P<stylesheet>%s)//test', WP_REST_Themes_Controller::PATTERN ),
array(
'callback' => function ( WP_REST_Request $request ) {
return $request['stylesheet'];
},
'permission_callback' => '__return_true',
)
);

wp_set_current_user( self::$admin_id );

$response = rest_do_request( self::$themes_route . '/default//test' );
$this->assertSame( 'default', $response->get_data() );

$response = rest_do_request( self::$themes_route . '/subdir/theme2//test' );
$this->assertSame( 'subdir/theme2', $response->get_data() );
}

/**
* The delete_item() method does not exist for themes.
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/qunit/fixtures/wp-api-generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -6709,7 +6709,7 @@ mockedApiResponse.Schema = {
"self": "http://example.org/index.php?rest_route=/wp/v2/themes"
}
},
"/wp/v2/themes/(?P<stylesheet>[\\w-]+)": {
"/wp/v2/themes/(?P<stylesheet>[^.\\/]+(?:\\/[^.\\/]+)?)": {
"namespace": "wp/v2",
"methods": [
"GET"
Expand Down

0 comments on commit ca7450d

Please sign in to comment.