| 937 | |
| 938 | /** |
| 939 | * Gets a WP_REST_Request object from a full URL. |
| 940 | * |
| 941 | * @since 4.5.0 |
| 942 | * |
| 943 | * @param string $url URL with protocol, domain, path and query args. |
| 944 | * @return WP_REST_Request|false WP_REST_Request object on success, false on failure. |
| 945 | */ |
| 946 | public static function from_url( $url ) { |
| 947 | $bits = parse_url( $url ); |
| 948 | $route = null; |
| 949 | $query_params = array(); |
| 950 | |
| 951 | if ( ! empty( $bits['query'] ) ) { |
| 952 | wp_parse_str( $bits['query'], $query_params ); |
| 953 | } |
| 954 | |
| 955 | if ( get_option( 'permalink_structure' ) ) { |
| 956 | $api_root = untrailingslashit( rest_url() ); |
| 957 | $api_url_part = substr( $url, strlen( $api_root ) ); |
| 958 | $parsed = parse_url( $api_url_part ); |
| 959 | if ( ! empty( $parsed['path'] ) ) { |
| 960 | $route = $parsed['path']; |
| 961 | } |
| 962 | } elseif ( ! empty( $query_params['rest_route'] ) ) { |
| 963 | $route = $query_params['rest_route']; |
| 964 | unset( $query_params['rest_route'] ); |
| 965 | } |
| 966 | |
| 967 | /** |
| 968 | * Filter the route parsed from a full API URL. |
| 969 | * |
| 970 | * `$route` may be null if the route could not be parsed from the |
| 971 | * provided URL. An empty value for `$route` will shortcircuit the |
| 972 | * method and cause it to return `false`. |
| 973 | * |
| 974 | * @param string|null $route API route if URL was valid, null if route could not be parsed. |
| 975 | * @param $string $url URL to generate request from. |
| 976 | */ |
| 977 | $route = apply_filters( 'rest_request_route_from_url', $route, $url ); |
| 978 | if ( empty( $route ) ) { |
| 979 | return false; |
| 980 | } |
| 981 | |
| 982 | $request = new WP_REST_Request( 'GET', $route ); |
| 983 | $request->set_query_params( $query_params ); |
| 984 | |
| 985 | /** |
| 986 | * Filter the request generated from a URL. |
| 987 | * |
| 988 | * @param WP_REST_Request $request Generated request object. |
| 989 | * @param string $url URL the request was generated from. |
| 990 | */ |
| 991 | return apply_filters( 'rest_request_from_url', $request, $url ); |
| 992 | } |