Skip to content

Commit ca7450d

Browse files
REST API: Support subdirectory themes in the Themes controller.
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
1 parent 827dd20 commit ca7450d

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
class WP_REST_Themes_Controller extends WP_REST_Controller {
1818

19+
const PATTERN = '[^.\/]+(?:\/[^.\/]+)?';
20+
1921
/**
2022
* Constructor.
2123
*
@@ -50,7 +52,7 @@ public function register_routes() {
5052

5153
register_rest_route(
5254
$this->namespace,
53-
'/' . $this->rest_base . '/(?P<stylesheet>[\w-]+)',
55+
sprintf( '/%s/(?P<stylesheet>%s)', $this->rest_base, self::PATTERN ),
5456
array(
5557
'args' => array(
5658
'stylesheet' => array(

tests/phpunit/tests/rest-api/rest-schema-setup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function test_expected_routes_in_schema() {
137137
'/wp/v2/templates/(?P<parent>[\d]+)/revisions',
138138
'/wp/v2/templates/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)',
139139
'/wp/v2/themes',
140-
'/wp/v2/themes/(?P<stylesheet>[\w-]+)',
140+
'/wp/v2/themes/(?P<stylesheet>[^.\/]+(?:\/[^.\/]+)?)',
141141
'/wp/v2/plugins',
142142
'/wp/v2/plugins/(?P<plugin>[^.\/]+(?:\/[^.\/]+)?)',
143143
'/wp/v2/block-directory/search',

tests/phpunit/tests/rest-api/rest-themes-controller.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,10 @@ public function set_up() {
151151
public function test_register_routes() {
152152
$routes = rest_get_server()->get_routes();
153153
$this->assertArrayHasKey( self::$themes_route, $routes );
154-
$this->assertArrayHasKey( self::$themes_route . '/(?P<stylesheet>[\\w-]+)', $routes );
154+
$this->assertArrayHasKey(
155+
sprintf( '%s/(?P<stylesheet>%s)', self::$themes_route, WP_REST_Themes_Controller::PATTERN ),
156+
$routes
157+
);
155158
}
156159

157160
/**
@@ -1281,6 +1284,42 @@ public function test_get_active_item_as_contributor() {
12811284
$this->assertSame( 200, $response->get_status() );
12821285
}
12831286

1287+
/**
1288+
* @ticket 54349
1289+
*/
1290+
public function test_get_item_subdirectory_theme() {
1291+
wp_set_current_user( self::$admin_id );
1292+
$request = new WP_REST_Request( 'GET', self::$themes_route . '/subdir/theme2' );
1293+
$response = rest_do_request( $request );
1294+
1295+
$this->assertSame( 200, $response->get_status() );
1296+
$this->assertSame( 'My Subdir Theme', $response->get_data()['name']['raw'] );
1297+
}
1298+
1299+
/**
1300+
* @ticket 54349
1301+
*/
1302+
public function test_can_support_further_routes() {
1303+
register_rest_route(
1304+
'wp/v2',
1305+
sprintf( '/themes/(?P<stylesheet>%s)//test', WP_REST_Themes_Controller::PATTERN ),
1306+
array(
1307+
'callback' => function ( WP_REST_Request $request ) {
1308+
return $request['stylesheet'];
1309+
},
1310+
'permission_callback' => '__return_true',
1311+
)
1312+
);
1313+
1314+
wp_set_current_user( self::$admin_id );
1315+
1316+
$response = rest_do_request( self::$themes_route . '/default//test' );
1317+
$this->assertSame( 'default', $response->get_data() );
1318+
1319+
$response = rest_do_request( self::$themes_route . '/subdir/theme2//test' );
1320+
$this->assertSame( 'subdir/theme2', $response->get_data() );
1321+
}
1322+
12841323
/**
12851324
* The delete_item() method does not exist for themes.
12861325
*/

tests/qunit/fixtures/wp-api-generated.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6709,7 +6709,7 @@ mockedApiResponse.Schema = {
67096709
"self": "http://example.org/index.php?rest_route=/wp/v2/themes"
67106710
}
67116711
},
6712-
"/wp/v2/themes/(?P<stylesheet>[\\w-]+)": {
6712+
"/wp/v2/themes/(?P<stylesheet>[^.\\/]+(?:\\/[^.\\/]+)?)": {
67136713
"namespace": "wp/v2",
67146714
"methods": [
67156715
"GET"

0 commit comments

Comments
 (0)