Make WordPress Core

Ticket #34416: 34416.2.diff

File 34416.2.diff, 3.4 KB (added by danielbachhuber, 9 years ago)
  • src/wp-includes/rest-api/rest-functions.php

    diff --git src/wp-includes/rest-api/rest-functions.php src/wp-includes/rest-api/rest-functions.php
    index 7964bae..280967f 100644
     
    2020 *                          multiple methods. Default empty array.
    2121 * @param bool   $override  Optional. If the route already exists, should we override it? True overrides,
    2222 *                          false merges (with newer overriding if duplicate keys exist). Default false.
     23 * @return bool True on success, false on error.
    2324 */
    2425function register_rest_route( $namespace, $route, $args = array(), $override = false ) {
    2526        /** @var WP_REST_Server $wp_rest_server */
    2627        global $wp_rest_server;
    2728
     29        if ( empty( $namespace ) ) {
     30                /*
     31                 * Non-namespaced routes are not allowed, with the exception of the main
     32                 * and namespace indexes. If you really need to register a
     33                 * non-namespaced route, call `WP_REST_Server::register_route` directly.
     34                 */
     35                _doing_it_wrong( 'register_rest_route', 'Routes must be namespaced with plugin or theme name and version', '4.4.0' );
     36                return false;
     37        } else if ( empty( $route ) ) {
     38                _doing_it_wrong( 'register_rest_route', 'Route must be specified', '4.4.0' );
     39                return false;
     40        }
     41
    2842        if ( isset( $args['callback'] ) ) {
    2943                // Upgrade a single set to multiple.
    3044                $args = array( $args );
    function register_rest_route( $namespace, $route, $args = array(), $override = f 
    4458                $arg_group = array_merge( $defaults, $arg_group );
    4559        }
    4660
    47         if ( $namespace ) {
    48                 $full_route = '/' . trim( $namespace, '/' ) . '/' . trim( $route, '/' );
    49         } else {
    50                 /*
    51                  * Non-namespaced routes are not allowed, with the exception of the main
    52                  * and namespace indexes. If you really need to register a
    53                  * non-namespaced route, call `WP_REST_Server::register_route` directly.
    54                  */
    55                 _doing_it_wrong( 'register_rest_route', 'Routes must be namespaced with plugin name and version', '4.4.0' );
    56 
    57                 $full_route = '/' . trim( $route, '/' );
    58         }
    59 
     61        $full_route = '/' . trim( $namespace, '/' ) . '/' . trim( $route, '/' );
    6062        $wp_rest_server->register_route( $namespace, $full_route, $args, $override );
     63        return true;
    6164}
    6265
    6366/**
  • tests/phpunit/tests/rest-api.php

    diff --git tests/phpunit/tests/rest-api.php tests/phpunit/tests/rest-api.php
    index ff7e5cd..f417a93 100644
    class Tests_REST_API extends WP_UnitTestCase { 
    149149        }
    150150
    151151        /**
     152         * Test that we reject routes without namespaces
     153         *
     154         * @expectedIncorrectUsage register_rest_route
     155         */
     156        public function test_route_reject_empty_namespace() {
     157                register_rest_route( '', '/test-empty-namespace', array(
     158                        'methods'      => array( 'POST' ),
     159                        'callback'     => '__return_null',
     160                ), true );
     161                $endpoints = $GLOBALS['wp_rest_server']->get_routes();
     162                $this->assertFalse( isset( $endpoints['/test-empty-namespace'] ) );
     163        }
     164
     165        /**
     166         * Test that we reject empty routes
     167         *
     168         * @expectedIncorrectUsage register_rest_route
     169         */
     170        public function test_route_reject_empty_route() {
     171                register_rest_route( '/test-empty-route', '', array(
     172                        'methods'      => array( 'POST' ),
     173                        'callback'     => '__return_null',
     174                ), true );
     175                $endpoints = $GLOBALS['wp_rest_server']->get_routes();
     176                $this->assertFalse( isset( $endpoints['/test-empty-route'] ) );
     177        }
     178
     179        /**
    152180         * The rest_route query variable should be registered.
    153181         */
    154182        function test_rest_route_query_var() {