278 | | return $wp_rest_server->dispatch( $request ); |
| 277 | return rest_get_server()->dispatch( $request ); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Make an internal REST request |
| 282 | * |
| 283 | * @since 4.5.0 |
| 284 | * |
| 285 | * @global WP_REST_Server $wp_rest_server ResponseHandler instance (usually WP_REST_Server). |
| 286 | * |
| 287 | * @param WP_REST_Request|string $request_or_method WP_REST_Request object or a request method. |
| 288 | * @param string $path Optional. Path to retrieve data from if not already specified in `$request_or_method`. |
| 289 | * Default null. |
| 290 | * @param array $data Optional. Data passed to the request. Default empty array. |
| 291 | * @return WP_Error|array Response data on success, WP_Error object on failure. |
| 292 | */ |
| 293 | function rest_internal_request( $request_or_method, $path = null, $data = array() ) { |
| 294 | if ( $request_or_method instanceof WP_REST_Request ) { |
| 295 | $request = $request_or_method; |
| 296 | } else { |
| 297 | $request = new WP_REST_Request( $request_or_method, $path ); |
| 298 | |
| 299 | foreach ( $data as $k => $v ) { |
| 300 | $request->set_param( $k, $v ); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | $result = rest_get_server()->dispatch( $request ); |
| 305 | |
| 306 | if ( $result->is_error() ) { |
| 307 | return $result->as_error(); |
| 308 | } |
| 309 | |
| 310 | return rest_get_server()->response_to_data( $result, ! empty( $data['_embed'] ) ); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Get the current REST server instance. |
| 315 | * |
| 316 | * Instantiates a new instance if none exists already. |
| 317 | * |
| 318 | * @since 4.5.0 |
| 319 | * |
| 320 | * @global WP_REST_Server $wp_rest_server REST server instance. |
| 321 | * |
| 322 | * @return WP_REST_Server REST server instance. |
| 323 | */ |
| 324 | function rest_get_server() { |
| 325 | /* @var WP_REST_Server $wp_rest_server */ |
| 326 | global $wp_rest_server; |
| 327 | |
| 328 | if ( empty( $wp_rest_server ) ) { |
| 329 | /** |
| 330 | * Filter the REST Server Class. |
| 331 | * |
| 332 | * This filter allows you to adjust the server class used by the API, using a |
| 333 | * different class to handle requests. |
| 334 | * |
| 335 | * @since 4.5.0 |
| 336 | * |
| 337 | * @param string $class_name The name of the server class. Default 'WP_REST_Server'. |
| 338 | */ |
| 339 | $wp_rest_server_class = apply_filters( 'wp_rest_server_class', 'WP_REST_Server' ); |
| 340 | |
| 341 | $wp_rest_server = new $wp_rest_server_class; |
| 342 | |
| 343 | /** |
| 344 | * Fires when preparing to serve an API request. |
| 345 | * |
| 346 | * Endpoint objects should be created and register their hooks on this action rather |
| 347 | * than another action to ensure they're only loaded when needed. |
| 348 | * |
| 349 | * @since 4.5.0 |
| 350 | * |
| 351 | * @param WP_REST_Server $wp_rest_server Server object. |
| 352 | */ |
| 353 | do_action( 'rest_api_init', $wp_rest_server ); |
| 354 | } |
| 355 | |
| 356 | return $wp_rest_server; |