Make WordPress Core

Ticket #35329: 35329.3.diff

File 35329.3.diff, 4.0 KB (added by danielbachhuber, 9 years ago)

Patch uses rest_get_server() when initializing REST server tests

  • src/wp-includes/rest-api.php

    diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php
    index 0b2da7f..1c9df71 100644
    a b function rest_api_loaded() { 
    140140         */
    141141        define( 'REST_REQUEST', true );
    142142
    143         /** @var WP_REST_Server $wp_rest_server */
    144         global $wp_rest_server;
    145 
    146         /**
    147          * Filter the REST Server Class.
    148          *
    149          * This filter allows you to adjust the server class used by the API, using a
    150          * different class to handle requests.
    151          *
    152          * @since 4.4.0
    153          *
    154          * @param string $class_name The name of the server class. Default 'WP_REST_Server'.
    155          */
    156         $wp_rest_server_class = apply_filters( 'wp_rest_server_class', 'WP_REST_Server' );
    157         $wp_rest_server = new $wp_rest_server_class;
    158 
    159         /**
    160          * Fires when preparing to serve an API request.
    161          *
    162          * Endpoint objects should be created and register their hooks on this action rather
    163          * than another action to ensure they're only loaded when needed.
    164          *
    165          * @since 4.4.0
    166          *
    167          * @param WP_REST_Server $wp_rest_server Server object.
    168          */
    169         do_action( 'rest_api_init', $wp_rest_server );
     143        // Initialize the server.
     144        $server = rest_get_server();
    170145
    171146        // Fire off the request.
    172         $wp_rest_server->serve_request( $GLOBALS['wp']->query_vars['rest_route'] );
     147        $server->serve_request( $GLOBALS['wp']->query_vars['rest_route'] );
    173148
    174149        // We're done.
    175150        die();
    function rest_url( $path = '', $scheme = 'json' ) { 
    273248 * @return WP_REST_Response REST response.
    274249 */
    275250function rest_do_request( $request ) {
    276         global $wp_rest_server;
    277251        $request = rest_ensure_request( $request );
    278         return $wp_rest_server->dispatch( $request );
     252        return rest_get_server()->dispatch( $request );
     253}
     254
     255/**
     256 * Get the current REST server instance.
     257 *
     258 * Instantiates a new instance if none exists already.
     259 *
     260 * @since 4.5.0
     261 *
     262 * @global WP_REST_Server $wp_rest_server REST server instance.
     263 *
     264 * @return  WP_REST_Server REST server instance.
     265 */
     266function rest_get_server() {
     267        /* @var WP_REST_Server $wp_rest_server */
     268        global $wp_rest_server;
     269
     270        if ( empty( $wp_rest_server ) ) {
     271                /**
     272                 * Filter the REST Server Class.
     273                 *
     274                 * This filter allows you to adjust the server class used by the API, using a
     275                 * different class to handle requests.
     276                 *
     277                 * @since 4.5.0
     278                 *
     279                 * @param string $class_name The name of the server class. Default 'WP_REST_Server'.
     280                 */
     281                $wp_rest_server_class = apply_filters( 'wp_rest_server_class', 'WP_REST_Server' );
     282                $wp_rest_server = new $wp_rest_server_class;
     283
     284                /**
     285                 * Fires when preparing to serve an API request.
     286                 *
     287                 * Endpoint objects should be created and register their hooks on this action rather
     288                 * than another action to ensure they're only loaded when needed.
     289                 *
     290                 * @since 4.5.0
     291                 *
     292                 * @param WP_REST_Server $wp_rest_server Server object.
     293                 */
     294                do_action( 'rest_api_init', $wp_rest_server );
     295        }
     296
     297        return $wp_rest_server;
    279298}
    280299
    281300/**
  • 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 33e3f24..0c148c2 100644
    a b class Tests_REST_Server extends WP_Test_REST_TestCase { 
    1515
    1616                /** @var WP_REST_Server $wp_rest_server */
    1717                global $wp_rest_server;
    18                 $this->server = $wp_rest_server = new Spy_REST_Server();
    1918
    20                 do_action( 'rest_api_init', $this->server );
     19                unset( $wp_rest_server );
     20                add_filter( 'wp_rest_server_class', array( $this, 'filter_wp_rest_server_class' ) );
     21                $this->server = rest_get_server();
     22                remove_filter( 'wp_rest_server_class', array( $this, 'filter_wp_rest_server_class' ) );
    2123        }
    2224
    2325        public function test_envelope() {
    class Tests_REST_Server extends WP_Test_REST_TestCase { 
    645647                        $this->assertFalse( isset( $headers[ $header ] ) && $headers[ $header ] === $value, sprintf( 'Header %s is set to nocache.', $header ) );
    646648                }
    647649        }
     650
     651        public function filter_wp_rest_server_class() {
     652                return 'Spy_REST_Server';
     653        }
    648654}