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
This commit is contained in:
Timothy Jacobs 2021-11-05 02:29:32 +00:00
parent 827dd20997
commit ca7450dfdd
4 changed files with 45 additions and 4 deletions

View File

@ -16,6 +16,8 @@
*/
class WP_REST_Themes_Controller extends WP_REST_Controller {
const PATTERN = '[^.\/]+(?:\/[^.\/]+)?';
/**
* Constructor.
*
@ -50,7 +52,7 @@ class WP_REST_Themes_Controller extends WP_REST_Controller {
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(

View File

@ -137,7 +137,7 @@ class WP_Test_REST_Schema_Initialization extends WP_Test_REST_TestCase {
'/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',

View File

@ -151,7 +151,10 @@ class WP_Test_REST_Themes_Controller extends WP_Test_REST_Controller_Testcase {
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
);
}
/**
@ -1281,6 +1284,42 @@ class WP_Test_REST_Themes_Controller extends WP_Test_REST_Controller_Testcase {
$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.
*/

View File

@ -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"