diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index 3b158ea07f..06bb9f9f80 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -19,8 +19,6 @@ define( 'REST_API_VERSION', '2.0' ); * * @since 4.4.0 * - * @global WP_REST_Server $wp_rest_server ResponseHandler instance (usually WP_REST_Server). - * * @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. * @param array $args Optional. Either an array of options for the endpoint, or an array of arrays for @@ -30,9 +28,6 @@ define( 'REST_API_VERSION', '2.0' ); * @return bool True on success, false on error. */ function register_rest_route( $namespace, $route, $args = array(), $override = false ) { - /** @var WP_REST_Server $wp_rest_server */ - global $wp_rest_server; - if ( empty( $namespace ) ) { /* * Non-namespaced routes are not allowed, with the exception of the main @@ -74,7 +69,7 @@ function register_rest_route( $namespace, $route, $args = array(), $override = f } $full_route = '/' . trim( $namespace, '/' ) . '/' . trim( $route, '/' ); - $wp_rest_server->register_route( $namespace, $full_route, $args, $override ); + rest_get_server()->register_route( $namespace, $full_route, $args, $override ); return true; } @@ -245,7 +240,6 @@ function create_initial_rest_routes() { * @since 4.4.0 * * @global WP $wp Current WordPress environment instance. - * @global WP_REST_Server $wp_rest_server ResponseHandler instance (usually WP_REST_Server). */ function rest_api_loaded() { if ( empty( $GLOBALS['wp']->query_vars['rest_route'] ) ) { @@ -387,8 +381,6 @@ function rest_url( $path = '', $scheme = 'json' ) { * * @since 4.4.0 * - * @global WP_REST_Server $wp_rest_server ResponseHandler instance (usually WP_REST_Server). - * * @param WP_REST_Request|string $request Request. * @return WP_REST_Response REST response. */ @@ -704,7 +696,6 @@ function rest_output_link_header() { * @since 4.4.0 * * @global mixed $wp_rest_auth_cookie - * @global WP_REST_Server $wp_rest_server REST server instance. * * @param WP_Error|mixed $result Error from another authentication handler, * null if we should handle it, or another value @@ -716,7 +707,7 @@ function rest_cookie_check_errors( $result ) { return $result; } - global $wp_rest_auth_cookie, $wp_rest_server; + global $wp_rest_auth_cookie; /* * Is cookie authentication being used? (If we get an auth @@ -750,7 +741,7 @@ function rest_cookie_check_errors( $result ) { } // Send a refreshed nonce in header. - $wp_rest_server->send_header( 'X-WP-Nonce', wp_create_nonce( 'wp_rest' ) ); + rest_get_server()->send_header( 'X-WP-Nonce', wp_create_nonce( 'wp_rest' ) ); return true; } diff --git a/tests/phpunit/tests/rest-api.php b/tests/phpunit/tests/rest-api.php index 689cbdfbae..90a480d641 100644 --- a/tests/phpunit/tests/rest-api.php +++ b/tests/phpunit/tests/rest-api.php @@ -16,7 +16,12 @@ class Tests_REST_API extends WP_UnitTestCase { public function setUp() { // Override the normal server with our spying server. $GLOBALS['wp_rest_server'] = new Spy_REST_Server(); - parent::setup(); + parent::setUp(); + } + + public function tearDown() { + remove_filter( 'wp_rest_server_class', array( $this, 'filter_wp_rest_server_class' ) ); + parent::tearDown(); } /** @@ -446,4 +451,21 @@ class Tests_REST_API extends WP_UnitTestCase { public function test_rest_parse_date_force_utc( $string, $value ) { $this->assertEquals( $value, rest_parse_date( $string, true ) ); } + + public function filter_wp_rest_server_class( $class_name ) { + return 'Spy_REST_Server'; + } + + public function test_register_rest_route_without_server() { + $GLOBALS['wp_rest_server'] = null; + add_filter( 'wp_rest_server_class', array( $this, 'filter_wp_rest_server_class' ) ); + + register_rest_route( 'test-ns', '/test', array( + 'methods' => array( 'GET' ), + 'callback' => '__return_null', + ) ); + + $routes = $GLOBALS['wp_rest_server']->get_routes(); + $this->assertEquals( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true ) ); + } }