X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/138998bbd8f7a1ac38b2f1eacbdf7cd522be4b13..8d3bb1a5dcfdea9857d3c88c3751f09593e34dc8:/wp-includes/rest-api.php diff --git a/wp-includes/rest-api.php b/wp-includes/rest-api.php index eb41c48f..13bfa596 100644 --- a/wp-includes/rest-api.php +++ b/wp-includes/rest-api.php @@ -39,10 +39,10 @@ function register_rest_route( $namespace, $route, $args = array(), $override = f * 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; } @@ -140,36 +140,11 @@ function rest_api_loaded() { */ 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(); @@ -184,7 +159,7 @@ function rest_api_loaded() { */ function rest_get_url_prefix() { /** - * Filter the REST URL prefix. + * Filters the REST URL prefix. * * @since 4.4.0 * @@ -231,9 +206,9 @@ function get_rest_url( $blog_id = null, $path = '/', $scheme = 'rest' ) { } /** - * 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 * @@ -273,9 +248,53 @@ function rest_url( $path = '', $scheme = 'json' ) { * @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; } /** @@ -324,8 +343,8 @@ function rest_ensure_response( $response ) { * * @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 ) { @@ -345,14 +364,14 @@ 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 ); @@ -412,10 +431,9 @@ function rest_handle_options_request( $response, $handler, $request ) { } $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; @@ -530,10 +548,12 @@ function rest_output_link_header() { * * @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 ) { @@ -541,7 +561,7 @@ 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 @@ -574,6 +594,9 @@ function rest_cookie_check_errors( $result ) { 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; } @@ -601,14 +624,14 @@ function rest_cookie_collect_status() { } /** - * 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 ) {