Make WordPress Core


Ignore:
Timestamp:
12/21/2021 04:12:06 AM (3 years ago)
Author:
hellofromTonya
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php

    r52376 r52399  
    4242        register_rest_route(
    4343            $this->namespace,
    44             '/' . $this->rest_base . '/themes/(?P<stylesheet>[\/\s%\w\.\(\)\[\]\@_\-]+)',
     44            // The route.
     45            sprintf(
     46                '/%s/themes/(?P<stylesheet>%s)',
     47                $this->rest_base,
     48                // Matches theme's directory: `/themes/<subdirectory>/<theme>/` or `/themes/<theme>/`.
     49                // Excludes invalid directory name characters: `/:<>*?"|`.
     50                '[^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?'
     51            ),
    4552            array(
    4653                array(
     
    6269        register_rest_route(
    6370            $this->namespace,
    64             '/' . $this->rest_base . '/(?P<id>[\/\s%\w\.\(\)\[\]\@_\-]+)',
     71            '/' . $this->rest_base . '/(?P<id>[\/\w-]+)',
    6572            array(
    6673                array(
     
    8996    /**
    9097     * Sanitize the global styles ID or stylesheet to decode endpoint.
    91      * For example, `wp/v2/global-styles/templatetwentytwo%200.4.0`
    92      * would be decoded to `templatetwentytwo 0.4.0`.
     98     * For example, `wp/v2/global-styles/twentytwentytwo%200.4.0`
     99     * would be decoded to `twentytwentytwo 0.4.0`.
    93100     *
    94101     * @since 5.9.0
Note: See TracChangeset for help on using the changeset viewer.