Make WordPress Core

Changeset 52017


Ignore:
Timestamp:
11/05/2021 02:29:32 AM (2 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Support subdirectory themes in the Themes controller.

This allows for themes that are included inside of a subdirectory, for example subdir/my-theme, to be accessed via the single item route of the /wp/v2/themes controller.

Fixes #54349.

Location:
trunk
Files:
4 edited

Legend:

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

    r51786 r52017  
    1616 */
    1717class WP_REST_Themes_Controller extends WP_REST_Controller {
     18
     19    const PATTERN = '[^.\/]+(?:\/[^.\/]+)?';
    1820
    1921    /**
     
    5153        register_rest_route(
    5254            $this->namespace,
    53             '/' . $this->rest_base . '/(?P<stylesheet>[\w-]+)',
     55            sprintf( '/%s/(?P<stylesheet>%s)', $this->rest_base, self::PATTERN ),
    5456            array(
    5557                'args'   => array(
  • trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php

    r51568 r52017  
    138138            '/wp/v2/templates/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)',
    139139            '/wp/v2/themes',
    140             '/wp/v2/themes/(?P<stylesheet>[\w-]+)',
     140            '/wp/v2/themes/(?P<stylesheet>[^.\/]+(?:\/[^.\/]+)?)',
    141141            '/wp/v2/plugins',
    142142            '/wp/v2/plugins/(?P<plugin>[^.\/]+(?:\/[^.\/]+)?)',
  • trunk/tests/phpunit/tests/rest-api/rest-themes-controller.php

    r51568 r52017  
    152152        $routes = rest_get_server()->get_routes();
    153153        $this->assertArrayHasKey( self::$themes_route, $routes );
    154         $this->assertArrayHasKey( self::$themes_route . '/(?P<stylesheet>[\\w-]+)', $routes );
     154        $this->assertArrayHasKey(
     155            sprintf( '%s/(?P<stylesheet>%s)', self::$themes_route, WP_REST_Themes_Controller::PATTERN ),
     156            $routes
     157        );
    155158    }
    156159
     
    12831286
    12841287    /**
     1288     * @ticket 54349
     1289     */
     1290    public function test_get_item_subdirectory_theme() {
     1291        wp_set_current_user( self::$admin_id );
     1292        $request  = new WP_REST_Request( 'GET', self::$themes_route . '/subdir/theme2' );
     1293        $response = rest_do_request( $request );
     1294
     1295        $this->assertSame( 200, $response->get_status() );
     1296        $this->assertSame( 'My Subdir Theme', $response->get_data()['name']['raw'] );
     1297    }
     1298
     1299    /**
     1300     * @ticket 54349
     1301     */
     1302    public function test_can_support_further_routes() {
     1303        register_rest_route(
     1304            'wp/v2',
     1305            sprintf( '/themes/(?P<stylesheet>%s)//test', WP_REST_Themes_Controller::PATTERN ),
     1306            array(
     1307                'callback'            => function ( WP_REST_Request $request ) {
     1308                    return $request['stylesheet'];
     1309                },
     1310                'permission_callback' => '__return_true',
     1311            )
     1312        );
     1313
     1314        wp_set_current_user( self::$admin_id );
     1315
     1316        $response = rest_do_request( self::$themes_route . '/default//test' );
     1317        $this->assertSame( 'default', $response->get_data() );
     1318
     1319        $response = rest_do_request( self::$themes_route . '/subdir/theme2//test' );
     1320        $this->assertSame( 'subdir/theme2', $response->get_data() );
     1321    }
     1322
     1323    /**
    12851324     * The delete_item() method does not exist for themes.
    12861325     */
  • trunk/tests/qunit/fixtures/wp-api-generated.js

    r52016 r52017  
    67106710            }
    67116711        },
    6712         "/wp/v2/themes/(?P<stylesheet>[\\w-]+)": {
     6712        "/wp/v2/themes/(?P<stylesheet>[^.\\/]+(?:\\/[^.\\/]+)?)": {
    67136713            "namespace": "wp/v2",
    67146714            "methods": [
Note: See TracChangeset for help on using the changeset viewer.