Make WordPress Core

Ticket #48530: 48530.diff

File 48530.diff, 2.7 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 e07e214d97..e85f4a4386 100644
    a b class WP_REST_Server { 
    706706         * used as the delimiter with preg_match()
    707707         *
    708708         * @since 4.4.0
     709         * @since 5.4.0 Add $namespace parameter.
    709710         *
     711         * @param string $namespace Optionally, only return routes in the given namespace.
    710712         * @return array `'/path/regex' => array( $callback, $bitmask )` or
    711713         *               `'/path/regex' => array( array( $callback, $bitmask ), ...)`.
    712714         */
    713         public function get_routes() {
     715        public function get_routes( $namespace = '' ) {
     716                $endpoints = $this->endpoints;
     717
     718                if ( $namespace ) {
     719                        $endpoints = wp_list_filter( $endpoints, array( 'namespace' => $namespace ) );
     720                }
    714721
    715722                /**
    716723                 * Filters the array of available endpoints.
    class WP_REST_Server { 
    722729                 *                         `'/path/regex' => array( $callback, $bitmask )` or
    723730                 *                         `'/path/regex' => array( array( $callback, $bitmask ).
    724731                 */
    725                 $endpoints = apply_filters( 'rest_endpoints', $this->endpoints );
     732                $endpoints = apply_filters( 'rest_endpoints', $endpoints );
    726733
    727734                // Normalise the endpoints.
    728735                $defaults = array(
    class WP_REST_Server { 
    834841                $method = $request->get_method();
    835842                $path   = $request->get_route();
    836843
    837                 foreach ( $this->get_routes() as $route => $handlers ) {
     844                $routes = array();
     845
     846                foreach ( $this->get_namespaces() as $namespace ) {
     847                        if ( 0 === strpos( ltrim( $path, '/' ), $namespace ) ) {
     848                                $routes = $this->get_routes( $namespace );
     849                                break;
     850                        }
     851                }
     852
     853                if ( ! $routes ) {
     854                        $routes = $this->get_routes();
     855                }
     856
     857                foreach ( $routes as $route => $handlers ) {
    838858                        $match = preg_match( '@^' . $route . '$@i', $path, $matches );
    839859
    840860                        if ( ! $match ) {
  • 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 74d957cbe5..6f4ab5c0a1 100644
    a b class Tests_REST_Server extends WP_Test_REST_TestCase { 
    12111211                $this->assertEquals( '', rest_get_server()->sent_body );
    12121212        }
    12131213
     1214        /**
     1215         * @ticket 48530
     1216         */
     1217        public function test_get_routes_respects_namespace_parameter() {
     1218                $routes = rest_get_server()->get_routes( 'oembed/1.0' );
     1219
     1220                foreach ( $routes as $route => $handlers ) {
     1221                        $this->assertStringStartsWith( '/oembed/1.0', $route );
     1222                }
     1223        }
     1224
    12141225        public function _validate_as_integer_123( $value, $request, $key ) {
    12151226                if ( ! is_int( $value ) ) {
    12161227                        return new WP_Error( 'some-error', 'This is not valid!' );