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/tests/phpunit/tests/rest-api/rest-themes-controller.php

    r52017 r52399  
    12861286
    12871287    /**
     1288     * @dataProvider data_get_item_non_subdir_theme
     1289     * @ticket 54596
     1290     * @covers WP_REST_Themes_Controller::get_item
     1291     *
     1292     * @param string $theme_dir     Theme directory to test.
     1293     * @param string $expected_name Expected theme name.
     1294     */
     1295    public function test_get_item_non_subdir_theme( $theme_dir, $expected_name ) {
     1296        wp_set_current_user( self::$admin_id );
     1297        $request  = new WP_REST_Request( 'GET', self::$themes_route . $theme_dir );
     1298        $response = rest_do_request( $request );
     1299
     1300        $this->assertSame( 200, $response->get_status() );
     1301        $this->assertSame( $expected_name, $response->get_data()['name']['raw'] );
     1302    }
     1303
     1304    /**
     1305     * Data provider.
     1306     *
     1307     * @return array
     1308     */
     1309    public function data_get_item_non_subdir_theme() {
     1310        return array(
     1311            'parent theme'                => array(
     1312                'theme_dir'     => '/block-theme',
     1313                'expected_name' => 'Block Theme',
     1314            ),
     1315            'child theme'                 => array(
     1316                'theme_dir'     => '/block-theme-child',
     1317                'expected_name' => 'Block Theme Child Theme',
     1318            ),
     1319            'theme with _-[]. characters' => array(
     1320                'theme_dir'     => '/block_theme-[0.4.0]',
     1321                'expected_name' => 'Block Theme [0.4.0]',
     1322            ),
     1323        );
     1324    }
     1325
     1326    /**
     1327     * @dataProvider data_get_item_subdirectory_theme
    12881328     * @ticket 54349
    1289      */
    1290     public function test_get_item_subdirectory_theme() {
     1329     * @ticket 54596
     1330     * @covers WP_REST_Themes_Controller::get_item
     1331     *
     1332     * @param string $theme_dir     Theme directory to test.
     1333     * @param string $expected_name Expected theme name.
     1334     */
     1335    public function test_get_item_subdirectory_theme( $theme_dir, $expected_name ) {
    12911336        wp_set_current_user( self::$admin_id );
    1292         $request  = new WP_REST_Request( 'GET', self::$themes_route . '/subdir/theme2' );
     1337        $request  = new WP_REST_Request( 'GET', self::$themes_route . $theme_dir );
    12931338        $response = rest_do_request( $request );
    12941339
    1295         $this->assertSame( 200, $response->get_status() );
    1296         $this->assertSame( 'My Subdir Theme', $response->get_data()['name']['raw'] );
     1340        $this->assertSame(
     1341            200,
     1342            $response->get_status(),
     1343            'A 200 OK status was not returned.'
     1344        );
     1345        $this->assertSame(
     1346            $expected_name,
     1347            $response->get_data()['name']['raw'],
     1348            'The actual theme name was not the expected theme name.'
     1349        );
     1350    }
     1351
     1352    /**
     1353     * Data provider.
     1354     *
     1355     * @return array
     1356     */
     1357    public function data_get_item_subdirectory_theme() {
     1358        return array(
     1359            'theme2'                      => array(
     1360                'theme_dir'     => '/subdir/theme2',
     1361                'expected_name' => 'My Subdir Theme',
     1362            ),
     1363            'theme with _-[]. characters' => array(
     1364                'theme_dir'     => '/subdir/block_theme-[1.0.0]',
     1365                'expected_name' => 'Block Theme [1.0.0] in subdirectory',
     1366            ),
     1367        );
    12971368    }
    12981369
Note: See TracChangeset for help on using the changeset viewer.