WordPress.org

Make WordPress Core

Changeset 41238


Ignore:
Timestamp:
08/10/17 01:37:30 (2 months ago)
Author:
jnylen0
Message:

REST API: Always call rest_get_server() instead of accessing the $wp_rest_server global.

This is a consistency improvement and also a bug fix for fairly obscure cases involving modified WP load order.

Fixes #41555.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api.php

    r41139 r41238  
    1919 * 
    2020 * @since 4.4.0 
    21  * 
    22  * @global WP_REST_Server $wp_rest_server ResponseHandler instance (usually WP_REST_Server). 
    2321 * 
    2422 * @param string $namespace The first URL segment after core prefix. Should be unique to your package/plugin. 
     
    3129 */ 
    3230function register_rest_route( $namespace, $route, $args = array(), $override = false ) { 
    33     /** @var WP_REST_Server $wp_rest_server */ 
    34     global $wp_rest_server; 
    35  
    3631    if ( empty( $namespace ) ) { 
    3732        /* 
     
    7570 
    7671    $full_route = '/' . trim( $namespace, '/' ) . '/' . trim( $route, '/' ); 
    77     $wp_rest_server->register_route( $namespace, $full_route, $args, $override ); 
     72    rest_get_server()->register_route( $namespace, $full_route, $args, $override ); 
    7873    return true; 
    7974} 
     
    246241 * 
    247242 * @global WP             $wp             Current WordPress environment instance. 
    248  * @global WP_REST_Server $wp_rest_server ResponseHandler instance (usually WP_REST_Server). 
    249243 */ 
    250244function rest_api_loaded() { 
     
    388382 * @since 4.4.0 
    389383 * 
    390  * @global WP_REST_Server $wp_rest_server ResponseHandler instance (usually WP_REST_Server). 
    391  * 
    392384 * @param WP_REST_Request|string $request Request. 
    393385 * @return WP_REST_Response REST response. 
     
    705697 * 
    706698 * @global mixed          $wp_rest_auth_cookie 
    707  * @global WP_REST_Server $wp_rest_server      REST server instance. 
    708699 * 
    709700 * @param WP_Error|mixed $result Error from another authentication handler, 
     
    717708    } 
    718709 
    719     global $wp_rest_auth_cookie, $wp_rest_server; 
     710    global $wp_rest_auth_cookie; 
    720711 
    721712    /* 
     
    751742 
    752743    // Send a refreshed nonce in header. 
    753     $wp_rest_server->send_header( 'X-WP-Nonce', wp_create_nonce( 'wp_rest' ) ); 
     744    rest_get_server()->send_header( 'X-WP-Nonce', wp_create_nonce( 'wp_rest' ) ); 
    754745 
    755746    return true; 
  • trunk/tests/phpunit/tests/rest-api.php

    r41139 r41238  
    1717        // Override the normal server with our spying server. 
    1818        $GLOBALS['wp_rest_server'] = new Spy_REST_Server(); 
    19         parent::setup(); 
     19        parent::setUp(); 
     20    } 
     21 
     22    public function tearDown() { 
     23        remove_filter( 'wp_rest_server_class', array( $this, 'filter_wp_rest_server_class' ) ); 
     24        parent::tearDown(); 
    2025    } 
    2126 
     
    447452        $this->assertEquals( $value, rest_parse_date( $string, true ) ); 
    448453    } 
     454 
     455    public function filter_wp_rest_server_class( $class_name ) { 
     456        return 'Spy_REST_Server'; 
     457    } 
     458 
     459    public function test_register_rest_route_without_server() { 
     460        $GLOBALS['wp_rest_server'] = null; 
     461        add_filter( 'wp_rest_server_class', array( $this, 'filter_wp_rest_server_class' ) ); 
     462 
     463        register_rest_route( 'test-ns', '/test', array( 
     464            'methods'  => array( 'GET' ), 
     465            'callback' => '__return_null', 
     466        ) ); 
     467 
     468        $routes = $GLOBALS['wp_rest_server']->get_routes(); 
     469        $this->assertEquals( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true ) ); 
     470    } 
    449471} 
Note: See TracChangeset for help on using the changeset viewer.