Skip to content

Commit

Permalink
Fix blank site editor when theme name contains a period (#37167)
Browse files Browse the repository at this point in the history
* Add \. as valid character in rest route regex

* Fix regex for stylesheet vs. id

* Add slash to regex to support subdir themes

* Include subdir in regex

* Update regex to match core trac update

* Sync route regex for theme directory characters

Sync the updates in core from Trac https://core.trac.wordpress.org/changeset/52376
that support theme directories with special characters. Confirm site
editor loads properly.

* Update global styles phpunit test from trac ticket.

The core ticket added additional fixtures and tests that are not in the
plugin. So those are not being copied over. Just updating the existing
test and adding one for /themes endpoint.
  • Loading branch information
mkaz committed Dec 16, 2021
1 parent f2614c7 commit 0407251
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function register_routes() {
// List themes global styles.
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/themes/(?P<stylesheet>[^.\/]+(?:\/[^.\/]+)?)',
'/' . $this->rest_base . '/themes/(?P<stylesheet>[\/\s%\w\.\(\)\[\]\@_\-]+)',
array(
array(
'methods' => WP_REST_Server::READABLE,
Expand All @@ -46,16 +46,17 @@ public function register_routes() {
// Lists/updates a single global style variation based on the given id.
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\/\w-]+)',
'/' . $this->rest_base . '/(?P<id>[\/\s%\w\.\(\)\[\]\@_\-]+)',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => array(
'id' => array(
'description' => __( 'The id of the global style variation', 'gutenberg' ),
'type' => 'string',
'description' => __( 'The id of the global style variation', 'gutenberg' ),
'type' => 'string',
'sanitize_callback' => array( $this, '_sanitize_global_styles_callback' ),
),
),
),
Expand All @@ -70,6 +71,20 @@ public function register_routes() {
);
}

/**
* Sanitize the global styles ID or stylesheet to decode endpoint.
* For example, `wp/v2/global-styles/templatetwentytwo%200.4.0`
* would be decoded to `templatetwentytwo 0.4.0`.
*
* @since 5.9.0
*
* @param string $id_or_stylesheet Global styles ID or stylesheet.
* @return string Sanitized global styles ID or stylesheet.
*/
public function _sanitize_global_styles_callback( $id_or_stylesheet ) {
return urldecode( $id_or_stylesheet );
}

/**
* Checks if the user has permissions to make the request.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function register_routes() {
// Lists/updates a single template based on the given id.
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\/\w-]+)',
'/' . $this->rest_base . '/(?P<id>[\/\s%\w\.\(\)\[\]\@_\-]+)',
array(
array(
'methods' => WP_REST_Server::READABLE,
Expand Down Expand Up @@ -131,7 +131,10 @@ protected function permissions_check() {
* @return string Sanitized template ID.
*/
public function _sanitize_template_id( $id ) {
// Decode empty space.
$last_slash_pos = strrpos( $id, '/' );
$id = urldecode( $id );

if ( false === $last_slash_pos ) {
return $id;
}
Expand Down
11 changes: 10 additions & 1 deletion phpunit/class-gutenberg-rest-global-styles-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,16 @@ public static function wpSetupBeforeClass( $factory ) {

public function test_register_routes() {
$routes = rest_get_server()->get_routes();
$this->assertArrayHasKey( '/wp/v2/global-styles/(?P<id>[\/\w-]+)', $routes );
$this->assertArrayHasKey(
'/wp/v2/global-styles/(?P<id>[\/\s%\w\.\(\)\[\]\@_\-]+)',
$routes,
'Single global style based on the given ID route does not exist'
);
$this->assertArrayHasKey(
'/wp/v2/global-styles/themes/(?P<stylesheet>[\/\s%\w\.\(\)\[\]\@_\-]+)',
$routes,
'Theme global styles route does not exist'
);
}

public function test_context_param() {
Expand Down

0 comments on commit 0407251

Please sign in to comment.