Make WordPress Core

Ticket #45265: 45265.3.diff

File 45265.3.diff, 3.7 KB (added by desrosj, 6 years ago)
  • src/wp-includes/rest-api.php

     
    1717/**
    1818 * Registers a REST API route.
    1919 *
     20 * Note: Do not use before the {@see 'rest_api_init'} hook.
     21 *
    2022 * @since 4.4.0
     23 * @since 5.1.0 Added a _doing_it_wrong() notice when not called on the rest_api_init hook.
    2124 *
    2225 * @param string $namespace The first URL segment after core prefix. Should be unique to your package/plugin.
    2326 * @param string $route     The base URL for route you are adding.
     
    4144                return false;
    4245        }
    4346
     47        if ( ! did_action( 'rest_api_init' ) && ! doing_action( 'rest_api_init' ) ) {
     48                _doing_it_wrong( 'register_rest_route', __( 'REST API routes must be registered on the rest_api_init action.' ), '5.1.0' );
     49        }
     50
    4451        if ( isset( $args['args'] ) ) {
    4552                $common_args = $args['args'];
    4653                unset( $args['args'] );
  • tests/phpunit/tests/rest-api.php

     
    7575         * Check that a single route is canonicalized.
    7676         *
    7777         * Ensures that single and multiple routes are handled correctly.
     78         *
     79         * @expectedIncorrectUsage register_rest_route
    7880         */
    7981        public function test_route_canonicalized() {
    8082                register_rest_route(
     
    110112         * Check that a single route is canonicalized.
    111113         *
    112114         * Ensures that single and multiple routes are handled correctly.
     115         *
     116         * @expectedIncorrectUsage register_rest_route
    113117         */
    114118        public function test_route_canonicalized_multiple() {
    115119                register_rest_route(
     
    151155
    152156        /**
    153157         * Check that routes are merged by default.
     158         *
     159         * @expectedIncorrectUsage register_rest_route
    154160         */
    155161        public function test_route_merge() {
    156162                register_rest_route(
     
    178184
    179185        /**
    180186         * Check that we can override routes.
     187         *
     188         * @expectedIncorrectUsage register_rest_route
    181189         */
    182190        public function test_route_override() {
    183191                register_rest_route(
     
    256264                $this->assertTrue( in_array( 'rest_route', $GLOBALS['wp']->public_query_vars ) );
    257265        }
    258266
     267        /**
     268         * @expectedIncorrectUsage register_rest_route
     269         */
    259270        public function test_route_method() {
    260271                register_rest_route(
    261272                        'test-ns',
     
    273284
    274285        /**
    275286         * The 'methods' arg should accept a single value as well as array.
     287         *
     288         * @expectedIncorrectUsage register_rest_route
    276289         */
    277290        public function test_route_method_string() {
    278291                register_rest_route(
     
    291304
    292305        /**
    293306         * The 'methods' arg should accept a single value as well as array.
     307         *
     308         * @expectedIncorrectUsage register_rest_route
    294309         */
    295310        public function test_route_method_array() {
    296311                register_rest_route(
     
    315330
    316331        /**
    317332         * The 'methods' arg should a comma seperated string.
     333         *
     334         * @expectedIncorrectUsage register_rest_route
    318335         */
    319336        public function test_route_method_comma_seperated() {
    320337                register_rest_route(
     
    337354                );
    338355        }
    339356
     357        /**
     358         * @expectedIncorrectUsage register_rest_route
     359         */
    340360        public function test_options_request() {
    341361                register_rest_route(
    342362                        'test-ns',
     
    358378
    359379        /**
    360380         * Ensure that the OPTIONS handler doesn't kick in for non-OPTIONS requests.
     381         *
     382         * @expectedIncorrectUsage register_rest_route
    361383         */
    362384        public function test_options_request_not_options() {
    363385                register_rest_route(
     
    696718                return 'Spy_REST_Server';
    697719        }
    698720
     721        /**
     722         * @expectedIncorrectUsage register_rest_route2
     723         */
    699724        public function test_register_rest_route_without_server() {
    700725                $GLOBALS['wp_rest_server'] = null;
    701726                add_filter( 'wp_rest_server_class', array( $this, 'filter_wp_rest_server_class' ) );