REST API: Support `.` in theme directory names in `WP_REST_Global_Styles_Controller`, `WP_REST_Templates_Controller`, and `WP_REST_Themes_Controller`.

Regex changes from [52376] are reverted to restore the original regex patterns. Why? [52376] used an include characters pattern, which was too limiting. It did not account for localized characters, such as `é`, or other valid directory name characters.

The original theme directory regex pattern, i.e. `[^.\/]+(?:\/[^.\/]+)?` excluded the period `.` character. Removing the `.` character resolves the reported issue by allowing matching for `themes/theme-dirname-1.0/` or `themes/<subdirname>/theme-dirname-1.0/`.

As the pattern used an exclude approach, all characters are valid for matching except for `/`. However, not all characters are cross-platform valid for directory names. For example, the characters `/:<>*?"|` are not valid on Windows OS. The pattern now excludes those characters.

The theme's directory (or subdirectory) name pattern matching is now used in `WP_REST_Global_Styles_Controller`, `WP_REST_Templates_Controller`, and `WP_REST_Themes_Controller`.

Follow-up to [51003], [52051], [52275], [52376].

Props costdev, hellofromTonya, spacedmonkey, TimothyBlynJacobs, bijayyadav, kafleg.
Fixes #54596.
Built from https://develop.svn.wordpress.org/trunk@52399


git-svn-id: http://core.svn.wordpress.org/trunk@51991 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
hellofromTonya 2021-12-21 04:14:00 +00:00
parent 28b0710360
commit 84229df562
4 changed files with 42 additions and 10 deletions

View File

@ -41,7 +41,14 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
// List themes global styles. // List themes global styles.
register_rest_route( register_rest_route(
$this->namespace, $this->namespace,
'/' . $this->rest_base . '/themes/(?P<stylesheet>[\/\s%\w\.\(\)\[\]\@_\-]+)', // The route.
sprintf(
'/%s/themes/(?P<stylesheet>%s)',
$this->rest_base,
// Matches theme's directory: `/themes/<subdirectory>/<theme>/` or `/themes/<theme>/`.
// Excludes invalid directory name characters: `/:<>*?"|`.
'[^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?'
),
array( array(
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => WP_REST_Server::READABLE,
@ -61,7 +68,7 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
// Lists/updates a single global style variation based on the given id. // Lists/updates a single global style variation based on the given id.
register_rest_route( register_rest_route(
$this->namespace, $this->namespace,
'/' . $this->rest_base . '/(?P<id>[\/\s%\w\.\(\)\[\]\@_\-]+)', '/' . $this->rest_base . '/(?P<id>[\/\w-]+)',
array( array(
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => WP_REST_Server::READABLE,
@ -88,8 +95,8 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
/** /**
* Sanitize the global styles ID or stylesheet to decode endpoint. * Sanitize the global styles ID or stylesheet to decode endpoint.
* For example, `wp/v2/global-styles/templatetwentytwo%200.4.0` * For example, `wp/v2/global-styles/twentytwentytwo%200.4.0`
* would be decoded to `templatetwentytwo 0.4.0`. * would be decoded to `twentytwentytwo 0.4.0`.
* *
* @since 5.9.0 * @since 5.9.0
* *

View File

@ -68,7 +68,16 @@ class WP_REST_Templates_Controller extends WP_REST_Controller {
// Lists/updates a single template based on the given id. // Lists/updates a single template based on the given id.
register_rest_route( register_rest_route(
$this->namespace, $this->namespace,
'/' . $this->rest_base . '/(?P<id>[\/\s%\w\.\(\)\[\]\@_\-]+)', // The route.
sprintf(
'/%s/(?P<id>%s%s)',
$this->rest_base,
// Matches theme's directory: `/themes/<subdirectory>/<theme>/` or `/themes/<theme>/`.
// Excludes invalid directory name characters: `/:<>*?"|`.
'([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)',
// Matches the template name.
'[\/\w-]+'
),
array( array(
'args' => array( 'args' => array(
'id' => array( 'id' => array(
@ -149,7 +158,6 @@ class WP_REST_Templates_Controller extends WP_REST_Controller {
* @return string Sanitized template ID. * @return string Sanitized template ID.
*/ */
public function _sanitize_template_id( $id ) { public function _sanitize_template_id( $id ) {
// Decode empty space.
$id = urldecode( $id ); $id = urldecode( $id );
$last_slash_pos = strrpos( $id, '/' ); $last_slash_pos = strrpos( $id, '/' );

View File

@ -16,7 +16,11 @@
*/ */
class WP_REST_Themes_Controller extends WP_REST_Controller { class WP_REST_Themes_Controller extends WP_REST_Controller {
const PATTERN = '[^.\/]+(?:\/[^.\/]+)?'; /**
* Matches theme's directory: `/themes/<subdirectory>/<theme>/` or `/themes/<theme>/`.
* Excludes invalid directory name characters: `/:<>*?"|`.
*/
const PATTERN = '[^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?';
/** /**
* Constructor. * Constructor.
@ -58,6 +62,7 @@ class WP_REST_Themes_Controller extends WP_REST_Controller {
'stylesheet' => array( 'stylesheet' => array(
'description' => __( "The theme's stylesheet. This uniquely identifies the theme." ), 'description' => __( "The theme's stylesheet. This uniquely identifies the theme." ),
'type' => 'string', 'type' => 'string',
'sanitize_callback' => array( $this, '_sanitize_stylesheet_callback' ),
), ),
), ),
array( array(
@ -70,6 +75,18 @@ class WP_REST_Themes_Controller extends WP_REST_Controller {
); );
} }
/**
* Sanitize the stylesheet to decode endpoint.
*
* @since 5.9.0
*
* @param string $stylesheet The stylesheet name.
* @return string Sanitized stylesheet.
*/
public function _sanitize_stylesheet_callback( $stylesheet ) {
return urldecode( $stylesheet );
}
/** /**
* Checks if a given request has access to read the theme. * Checks if a given request has access to read the theme.
* *

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.9-beta3-52398'; $wp_version = '5.9-beta3-52399';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.