WordPress.org

Make WordPress Core

Changeset 36529


Ignore:
Timestamp:
02/16/2016 01:11:31 AM (5 years ago)
Author:
rmccue
Message:

REST API: Add helper function to get server instance.

This allows using rest_do_request() outside of the API itself easily.

Props danielbachhuber, swissspidy.

Location:
trunk
Files:
2 edited

Legend:

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

    r36086 r36529  
    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.
     
    274249 */
    275250function rest_do_request( $request ) {
     251    $request = rest_ensure_request( $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 */
    276268    global $wp_rest_server;
    277     $request = rest_ensure_request( $request );
    278     return $wp_rest_server->dispatch( $request );
     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.4.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.4.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
  • trunk/tests/phpunit/tests/rest-api/rest-server.php

    r35773 r36529  
    1616        /** @var WP_REST_Server $wp_rest_server */
    1717        global $wp_rest_server;
    18         $this->server = $wp_rest_server = new Spy_REST_Server();
    19 
    20         do_action( 'rest_api_init', $this->server );
     18
     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
     
    646648        }
    647649    }
     650
     651    public function filter_wp_rest_server_class() {
     652        return 'Spy_REST_Server';
     653    }
    648654}
Note: See TracChangeset for help on using the changeset viewer.