Make WordPress Core

Ticket #48530: 48530.4.diff

File 48530.4.diff, 2.1 KB (added by TimothyBlynJacobs, 6 years ago)
  • src/wp-includes/rest-api/class-wp-rest-server.php

    diff --git a/src/wp-includes/rest-api/class-wp-rest-server.php b/src/wp-includes/rest-api/class-wp-rest-server.php
    index efda2321b7..2a7cb268a1 100644
    a b class WP_REST_Server { 
    879879                $method = $request->get_method();
    880880                $path   = $request->get_route();
    881881
    882                 $routes = array();
     882                $with_namespace = array();
    883883
    884884                foreach ( $this->get_namespaces() as $namespace ) {
    885                         if ( 0 === strpos( ltrim( $path, '/' ), $namespace ) ) {
    886                                 $routes = $this->get_routes( $namespace );
    887                                 break;
     885                        if ( 0 === strpos( trailingslashit( ltrim( $path, '/' ) ), $namespace ) ) {
     886                                $with_namespace[] = $this->get_routes( $namespace );
    888887                        }
    889888                }
    890889
    891                 if ( ! $routes ) {
     890                if ( $with_namespace ) {
     891                        $routes = array_merge( ...$with_namespace );
     892                } else {
    892893                        $routes = $this->get_routes();
    893894                }
    894895
  • tests/phpunit/tests/rest-api/rest-server.php

    diff --git a/tests/phpunit/tests/rest-api/rest-server.php b/tests/phpunit/tests/rest-api/rest-server.php
    index 755f0296ea..16838bd724 100644
    a b class Tests_REST_Server extends WP_Test_REST_TestCase { 
    14421442                }
    14431443        }
    14441444
     1445        /**
     1446         * @ticket 48530
     1447         */
     1448        public function test_get_routes_no_namespace_overriding() {
     1449                register_rest_route(
     1450                        'test-ns',
     1451                        '/test',
     1452                        array(
     1453                                'methods'  => array( 'GET' ),
     1454                                'callback' => function() {
     1455                                        return new WP_REST_Response( 'data', 204 );
     1456                                },
     1457                        )
     1458                );
     1459                register_rest_route(
     1460                        'test-ns/v1',
     1461                        '/test',
     1462                        array(
     1463                                'methods'  => array( 'GET' ),
     1464                                'callback' => function() {
     1465                                        return new WP_REST_Response( 'data', 204 );
     1466                                },
     1467                        )
     1468                );
     1469
     1470                $request  = new WP_REST_Request( 'GET', '/test-ns/v1/test' );
     1471                $response = rest_get_server()->dispatch( $request );
     1472
     1473                $this->assertEquals( 204, $response->get_status(), '/test-ns/v1/test' );
     1474        }
     1475
    14451476        public function _validate_as_integer_123( $value, $request, $key ) {
    14461477                if ( ! is_int( $value ) ) {
    14471478                        return new WP_Error( 'some-error', 'This is not valid!' );