Make WordPress Core


Ignore:
Timestamp:
12/14/2021 06:22:07 PM (3 years ago)
Author:
hellofromTonya
Message:

REST API: Add block theme support for valid non-alphanumeric characters in theme's directory name.

Themes whose wp-content/themes/<dirname> include valid non-alphanumeric (cross-platform) characters work for non-block themes, but did not previously resolve for block themes. For example, a block theme in wp-content/themes/twentytwentytwo-0.4.0/ directory resulted a 404 "No route was found matching the URL and request method" response when attempting to customize it in the Site Editor.

This commit adds support for the following characters in a theme's root directory: _, ., @, [, ], (, and ). Subdirectory themes and - are already supported.

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

Props mkaz, costdev, hellofromTonya, jffng, justinahinon, peterwilsoncc, spacedmonkey, TimothyBlynJacobs.
Fixes #54596.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-global-styles-controller.php

    r52374 r52376  
    9494    /**
    9595     * @covers WP_REST_Global_Styles_Controller::register_routes
     96     * @ticket 54596
    9697     */
    9798    public function test_register_routes() {
    9899        $routes = rest_get_server()->get_routes();
    99         $this->assertArrayHasKey( '/wp/v2/global-styles/(?P<id>[\/\w-]+)', $routes );
    100         $this->assertCount( 2, $routes['/wp/v2/global-styles/(?P<id>[\/\w-]+)'] );
    101         $this->assertArrayHasKey( '/wp/v2/global-styles/themes/(?P<stylesheet>[^.\/]+(?:\/[^.\/]+)?)', $routes );
    102         $this->assertCount( 1, $routes['/wp/v2/global-styles/themes/(?P<stylesheet>[^.\/]+(?:\/[^.\/]+)?)'] );
     100        $this->assertArrayHasKey(
     101            '/wp/v2/global-styles/(?P<id>[\/\s%\w\.\(\)\[\]\@_\-]+)',
     102            $routes,
     103            'Single global style based on the given ID route does not exist'
     104        );
     105        $this->assertCount(
     106            2,
     107            $routes['/wp/v2/global-styles/(?P<id>[\/\s%\w\.\(\)\[\]\@_\-]+)'],
     108            'Single global style based on the given ID route does not have exactly two elements'
     109        );
     110        $this->assertArrayHasKey(
     111            '/wp/v2/global-styles/themes/(?P<stylesheet>[\/\s%\w\.\(\)\[\]\@_\-]+)',
     112            $routes,
     113            'Theme global styles route does not exist'
     114        );
     115        $this->assertCount(
     116            1,
     117            $routes['/wp/v2/global-styles/themes/(?P<stylesheet>[\/\s%\w\.\(\)\[\]\@_\-]+)'],
     118            'Theme global styles route does not have exactly one element'
     119        );
    103120    }
    104121
     
    133150    }
    134151
    135 
    136152    /**
    137153     * @covers WP_REST_Global_Styles_Controller::get_theme_item
     
    146162
    147163    /**
    148      * @covers WP_REST_Global_Styles_Controller::get_theme_item
    149      */
    150     public function test_get_theme_item() {
    151         wp_set_current_user( self::$admin_id );
    152         $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/tt1-blocks' );
     164     * @dataProvider data_get_theme_item_invalid_theme_dirname
     165     * @covers WP_REST_Global_Styles_Controller::get_theme_item
     166     * @ticket 54596
     167     */
     168    public function test_get_theme_item_invalid_theme_dirname( $theme_dirname ) {
     169        wp_set_current_user( self::$admin_id );
     170        switch_theme( $theme_dirname );
     171
     172        $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/' . $theme_dirname );
     173        $response = rest_get_server()->dispatch( $request );
     174        $this->assertErrorResponse( 'rest_no_route', $response, 404 );
     175    }
     176
     177    /**
     178     * Data provider.
     179     *
     180     * @return array
     181     */
     182    public function data_get_theme_item_invalid_theme_dirname() {
     183        return array(
     184            'with |'                 => array( 'my|theme' ),
     185            'with +'                 => array( 'my+theme' ),
     186            'with {}'                => array( 'my{theme}' ),
     187            'with #'                 => array( 'my#theme' ),
     188            'with !'                 => array( 'my!theme' ),
     189            'multiple invalid chars' => array( 'mytheme-[_(+@)]#! v4.0' ),
     190        );
     191    }
     192
     193    /**
     194     * @dataProvider data_get_theme_item
     195     * @covers WP_REST_Global_Styles_Controller::get_theme_item
     196     * @ticket 54596
     197     */
     198    public function test_get_theme_item( $theme ) {
     199        wp_set_current_user( self::$admin_id );
     200        switch_theme( $theme );
     201
     202        $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/' . $theme );
    153203        $response = rest_get_server()->dispatch( $request );
    154204        $data     = $response->get_data();
    155205        $links    = $response->get_links();
    156         $this->assertArrayHasKey( 'settings', $data );
    157         $this->assertArrayHasKey( 'styles', $data );
    158         $this->assertArrayHasKey( 'self', $links );
    159         $this->assertStringContainsString( '/wp/v2/global-styles/themes/tt1-blocks', $links['self'][0]['href'] );
     206        $this->assertArrayHasKey( 'settings', $data, 'Data does not have "settings" key' );
     207        $this->assertArrayHasKey( 'styles', $data, 'Data does not have "styles" key' );
     208        $this->assertArrayHasKey( 'self', $links, 'Links do not have a "self" key' );
     209        $this->assertStringContainsString( '/wp/v2/global-styles/themes/' . $theme, $links['self'][0]['href'] );
     210    }
     211
     212    /**
     213     * Data provider.
     214     *
     215     * @return array
     216     */
     217    public function data_get_theme_item() {
     218        return array(
     219            'alphabetic chars'   => array( 'mytheme' ),
     220            'alphanumeric chars' => array( 'mythemev1' ),
     221            'space'              => array( 'my theme' ),
     222            '-'                  => array( 'my-theme' ),
     223            '_'                  => array( 'my_theme' ),
     224            '.'                  => array( 'mytheme0.1' ),
     225            '- and .'            => array( 'my-theme-0.1' ),
     226            'space and .'        => array( 'mytheme v0.1' ),
     227            'space, -, _, .'     => array( 'my-theme-v0.1' ),
     228            '[]'                 => array( 'my[theme]' ),
     229            '()'                 => array( 'my(theme)' ),
     230            '@'                  => array( 'my@theme' ),
     231        );
    160232    }
    161233
Note: See TracChangeset for help on using the changeset viewer.