From 34e3de7b7fa28ad972f4a549539e534423f62770 Mon Sep 17 00:00:00 2001 From: Jonathan Desrosiers Date: Fri, 11 Jan 2019 18:18:41 +0000 Subject: [PATCH] REST API: Encourage proper usage of `register_rest_route()`. Calling `register_rest_route()` too early in the loading process has the potential to cause some unintentional problems and pitfalls. Because `register_rest_route()` calls `rest_get_server()` (which creates the `WP_REST_Server` instance), calling the function directly and/or before `rest_api_init` should be discouraged. For example, if `register_rest_route ()` is called on `init`, the REST API server instance is set up (and all functions added to `rest_api_init` and other related hooks are invoked), even though the current request may not be a REST request. Also, if `register_rest_route()` is called even earlier (say, in an `mu-plugin` file), required endpoints may be missing since normal plugins have not yet been loaded and have not had a chance to register their own action hooks. This adds a `_doing_it_wrong()` notice the first time `register_rest_route()` is called before `rest_api_init` in a request to encourage best practices for registering REST API routes. Props kraftbj, desrosj, timothyblynjacobs. Fixes #45265. git-svn-id: https://develop.svn.wordpress.org/trunk@44568 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/rest-api.php | 7 +++++++ tests/phpunit/tests/rest-api.php | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index e0807e7696..d0066ff0fe 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -17,7 +17,10 @@ define( 'REST_API_VERSION', '2.0' ); /** * Registers a REST API route. * + * Note: Do not use before the {@see 'rest_api_init'} hook. + * * @since 4.4.0 + * @since 5.1.0 Added a _doing_it_wrong() notice when not called on or after the rest_api_init hook. * * @param string $namespace The first URL segment after core prefix. Should be unique to your package/plugin. * @param string $route The base URL for route you are adding. @@ -41,6 +44,10 @@ function register_rest_route( $namespace, $route, $args = array(), $override = f return false; } + if ( ! did_action( 'rest_api_init' ) ) { + _doing_it_wrong( 'register_rest_route', __( 'REST API routes must be registered on the rest_api_init action.' ), '5.1.0' ); + } + if ( isset( $args['args'] ) ) { $common_args = $args['args']; unset( $args['args'] ); diff --git a/tests/phpunit/tests/rest-api.php b/tests/phpunit/tests/rest-api.php index dc94c7b6dd..7e72708d43 100644 --- a/tests/phpunit/tests/rest-api.php +++ b/tests/phpunit/tests/rest-api.php @@ -14,9 +14,11 @@ require_once ABSPATH . WPINC . '/rest-api.php'; */ class Tests_REST_API extends WP_UnitTestCase { public function setUp() { + parent::setUp(); + // Override the normal server with our spying server. $GLOBALS['wp_rest_server'] = new Spy_REST_Server(); - parent::setUp(); + do_action( 'rest_api_init', $GLOBALS['wp_rest_server'] ); } public function tearDown() {