* and namespace indexes. If you really need to register a
* non-namespaced route, call `WP_REST_Server::register_route` directly.
*/
- _doing_it_wrong( 'register_rest_route', 'Routes must be namespaced with plugin or theme name and version.', '4.4.0' );
+ _doing_it_wrong( 'register_rest_route', __( 'Routes must be namespaced with plugin or theme name and version.' ), '4.4.0' );
return false;
} else if ( empty( $route ) ) {
- _doing_it_wrong( 'register_rest_route', 'Route must be specified.', '4.4.0' );
+ _doing_it_wrong( 'register_rest_route', __( 'Route must be specified.' ), '4.4.0' );
return false;
}
*/
define( 'REST_REQUEST', true );
- /** @var WP_REST_Server $wp_rest_server */
- global $wp_rest_server;
-
- /**
- * Filter the REST Server Class.
- *
- * This filter allows you to adjust the server class used by the API, using a
- * different class to handle requests.
- *
- * @since 4.4.0
- *
- * @param string $class_name The name of the server class. Default 'WP_REST_Server'.
- */
- $wp_rest_server_class = apply_filters( 'wp_rest_server_class', 'WP_REST_Server' );
- $wp_rest_server = new $wp_rest_server_class;
-
- /**
- * Fires when preparing to serve an API request.
- *
- * Endpoint objects should be created and register their hooks on this action rather
- * than another action to ensure they're only loaded when needed.
- *
- * @since 4.4.0
- *
- * @param WP_REST_Server $wp_rest_server Server object.
- */
- do_action( 'rest_api_init', $wp_rest_server );
+ // Initialize the server.
+ $server = rest_get_server();
// Fire off the request.
- $wp_rest_server->serve_request( $GLOBALS['wp']->query_vars['rest_route'] );
+ $server->serve_request( $GLOBALS['wp']->query_vars['rest_route'] );
// We're done.
die();
*/
function rest_get_url_prefix() {
/**
- * Filter the REST URL prefix.
+ * Filters the REST URL prefix.
*
* @since 4.4.0
*
}
/**
- * Filter the REST URL.
+ * Filters the REST URL.
*
- * Use this filter to adjust the url returned by the `get_rest_url` function.
+ * Use this filter to adjust the url returned by the get_rest_url() function.
*
* @since 4.4.0
*
* @return WP_REST_Response REST response.
*/
function rest_do_request( $request ) {
- global $wp_rest_server;
$request = rest_ensure_request( $request );
- return $wp_rest_server->dispatch( $request );
+ return rest_get_server()->dispatch( $request );
+}
+
+/**
+ * Retrieves the current REST server instance.
+ *
+ * Instantiates a new instance if none exists already.
+ *
+ * @since 4.5.0
+ *
+ * @global WP_REST_Server $wp_rest_server REST server instance.
+ *
+ * @return WP_REST_Server REST server instance.
+ */
+function rest_get_server() {
+ /* @var WP_REST_Server $wp_rest_server */
+ global $wp_rest_server;
+
+ if ( empty( $wp_rest_server ) ) {
+ /**
+ * Filters the REST Server Class.
+ *
+ * This filter allows you to adjust the server class used by the API, using a
+ * different class to handle requests.
+ *
+ * @since 4.4.0
+ *
+ * @param string $class_name The name of the server class. Default 'WP_REST_Server'.
+ */
+ $wp_rest_server_class = apply_filters( 'wp_rest_server_class', 'WP_REST_Server' );
+ $wp_rest_server = new $wp_rest_server_class;
+
+ /**
+ * Fires when preparing to serve an API request.
+ *
+ * Endpoint objects should be created and register their hooks on this action rather
+ * than another action to ensure they're only loaded when needed.
+ *
+ * @since 4.4.0
+ *
+ * @param WP_REST_Server $wp_rest_server Server object.
+ */
+ do_action( 'rest_api_init', $wp_rest_server );
+ }
+
+ return $wp_rest_server;
}
/**
*
* @since 4.4.0
*
- * @param string $function Function name.
- * @param string $replacement Replacement function name.
+ * @param string $function The function that was called.
+ * @param string $replacement The function that should have been called.
* @param string $version Version.
*/
function rest_handle_deprecated_function( $function, $replacement, $version ) {
*
* @since 4.4.0
*
- * @param string $function Function name.
- * @param string $replacement Replacement function name.
+ * @param string $function The function that was called.
+ * @param string $message A message regarding the change.
* @param string $version Version.
*/
-function rest_handle_deprecated_argument( $function, $replacement, $version ) {
- if ( ! empty( $replacement ) ) {
- /* translators: 1: function name, 2: WordPress version number, 3: new argument name */
- $string = sprintf( __( '%1$s (since %2$s; %3$s)' ), $function, $version, $replacement );
+function rest_handle_deprecated_argument( $function, $message, $version ) {
+ if ( ! empty( $message ) ) {
+ /* translators: 1: function name, 2: WordPress version number, 3: error message */
+ $string = sprintf( __( '%1$s (since %2$s; %3$s)' ), $function, $version, $message );
} else {
/* translators: 1: function name, 2: WordPress version number */
$string = sprintf( __( '%1$s (since %2$s; no alternative available)' ), $function, $version );
}
$data = $handler->get_data_for_route( $route, $endpoints, 'help' );
- $accept = array_merge( $accept, $data['methods'] );
+ $response->set_matched_route( $route );
break;
}
- $response->header( 'Accept', implode( ', ', $accept ) );
$response->set_data( $data );
return $response;
*
* @since 4.4.0
*
- * @global mixed $wp_rest_auth_cookie
+ * @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 if not.
+ * @param WP_Error|mixed $result Error from another authentication handler,
+ * null if we should handle it, or another value
+ * if not.
* @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
*/
function rest_cookie_check_errors( $result ) {
return $result;
}
- global $wp_rest_auth_cookie;
+ global $wp_rest_auth_cookie, $wp_rest_server;
/*
* Is cookie authentication being used? (If we get an auth
return new WP_Error( 'rest_cookie_invalid_nonce', __( 'Cookie nonce is invalid' ), array( 'status' => 403 ) );
}
+ // Send a refreshed nonce in header.
+ $wp_rest_server->send_header( 'X-WP-Nonce', wp_create_nonce( 'wp_rest' ) );
+
return true;
}
}
/**
- * Parses an RFC3339 timestamp into a DateTime.
+ * Parses an RFC3339 time into a Unix timestamp.
*
* @since 4.4.0
*
* @param string $date RFC3339 timestamp.
* @param bool $force_utc Optional. Whether to force UTC timezone instead of using
* the timestamp's timezone. Default false.
- * @return DateTime DateTime instance.
+ * @return int Unix timestamp.
*/
function rest_parse_date( $date, $force_utc = false ) {
if ( $force_utc ) {