X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/03f2fa83c13c1b532284205fa7efcab9b8b2c41f..784f914b1e4b1c62d6657e86397c2e83bcee4295:/wp-includes/rest-api/class-wp-rest-request.php?ds=sidebyside diff --git a/wp-includes/rest-api/class-wp-rest-request.php b/wp-includes/rest-api/class-wp-rest-request.php index fdf13c46..6e0720e5 100644 --- a/wp-includes/rest-api/class-wp-rest-request.php +++ b/wp-includes/rest-api/class-wp-rest-request.php @@ -16,6 +16,13 @@ * used in that manner. It does not use ArrayObject (as we cannot rely on SPL), * so be aware it may have non-array behaviour in some cases. * + * Note: When using features provided by ArrayAccess, be aware that WordPress deliberately + * does not distinguish between arguments of the same name for different request methods. + * For instance, in a request with `GET id=1` and `POST id=2`, `$request['id']` will equal + * 2 (`POST`) not 1 (`GET`). For more precision between request methods, use + * {@see WP_REST_Request::get_body_params()}, {@see WP_REST_Request::get_url_params()}, + * etc. + * * @since 4.4.0 * * @see ArrayAccess @@ -851,13 +858,13 @@ class WP_REST_Request implements ArrayAccess { } if ( is_wp_error( $valid_check ) ) { - $invalid_params[] = sprintf( '%s (%s)', $key, $valid_check->get_error_message() ); + $invalid_params[ $key ] = $valid_check->get_error_message(); } } } if ( $invalid_params ) { - return new WP_Error( 'rest_invalid_param', sprintf( __( 'Invalid parameter(s): %s' ), implode( ', ', $invalid_params ) ), array( 'status' => 400, 'params' => $invalid_params ) ); + return new WP_Error( 'rest_invalid_param', sprintf( __( 'Invalid parameter(s): %s' ), implode( ', ', array_keys( $invalid_params ) ) ), array( 'status' => 400, 'params' => $invalid_params ) ); } return true; @@ -927,4 +934,51 @@ class WP_REST_Request implements ArrayAccess { unset( $this->params[ $type ][ $offset ] ); } } + + /** + * Retrieves a WP_REST_Request object from a full URL. + * + * @static + * @since 4.5.0 + * @access public + * + * @param string $url URL with protocol, domain, path and query args. + * @return WP_REST_Request|false WP_REST_Request object on success, false on failure. + */ + public static function from_url( $url ) { + $bits = parse_url( $url ); + $query_params = array(); + + if ( ! empty( $bits['query'] ) ) { + wp_parse_str( $bits['query'], $query_params ); + } + + $api_root = rest_url(); + if ( get_option( 'permalink_structure' ) && 0 === strpos( $url, $api_root ) ) { + // Pretty permalinks on, and URL is under the API root + $api_url_part = substr( $url, strlen( untrailingslashit( $api_root ) ) ); + $route = parse_url( $api_url_part, PHP_URL_PATH ); + } elseif ( ! empty( $query_params['rest_route'] ) ) { + // ?rest_route=... set directly + $route = $query_params['rest_route']; + unset( $query_params['rest_route'] ); + } + + $request = false; + if ( ! empty( $route ) ) { + $request = new WP_REST_Request( 'GET', $route ); + $request->set_query_params( $query_params ); + } + + /** + * Filter the request generated from a URL. + * + * @since 4.5.0 + * + * @param WP_REST_Request|false $request Generated request object, or false if URL + * could not be parsed. + * @param string $url URL the request was generated from. + */ + return apply_filters( 'rest_request_from_url', $request, $url ); + } }