author | ymh <ymh.work@gmail.com> |
Wed, 21 Sep 2022 18:19:35 +0200 | |
changeset 18 | be944660c56a |
parent 16 | a86126ab1dd4 |
child 19 | 3d72ae0968f4 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3 |
* Core HTTP Request API |
0 | 4 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5 |
* Standardizes the HTTP requests for WordPress. Handles cookies, gzip encoding and decoding, chunk |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6 |
* decoding, if HTTP 1.1 and various other difficult HTTP protocol implementations. |
0 | 7 |
* |
8 |
* @package WordPress |
|
9 |
* @subpackage HTTP |
|
10 |
*/ |
|
11 |
||
12 |
/** |
|
13 |
* Returns the initialized WP_Http Object |
|
14 |
* |
|
15 |
* @since 2.7.0 |
|
16 |
* @access private |
|
17 |
* |
|
18 |
* @return WP_Http HTTP Transport object. |
|
19 |
*/ |
|
20 |
function _wp_http_get_object() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
21 |
static $http = null; |
0 | 22 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
23 |
if ( is_null( $http ) ) { |
0 | 24 |
$http = new WP_Http(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
25 |
} |
0 | 26 |
return $http; |
27 |
} |
|
28 |
||
29 |
/** |
|
30 |
* Retrieve the raw response from a safe HTTP request. |
|
31 |
* |
|
32 |
* This function is ideal when the HTTP request is being made to an arbitrary |
|
33 |
* URL. The URL is validated to avoid redirection and request forgery attacks. |
|
34 |
* |
|
35 |
* @since 3.6.0 |
|
36 |
* |
|
5 | 37 |
* @see wp_remote_request() For more information on the response array format. |
38 |
* @see WP_Http::request() For default arguments information. |
|
39 |
* |
|
16 | 40 |
* @param string $url URL to retrieve. |
5 | 41 |
* @param array $args Optional. Request arguments. Default empty array. |
16 | 42 |
* @return array|WP_Error The response or WP_Error on failure. |
0 | 43 |
*/ |
44 |
function wp_safe_remote_request( $url, $args = array() ) { |
|
45 |
$args['reject_unsafe_urls'] = true; |
|
9 | 46 |
$http = _wp_http_get_object(); |
0 | 47 |
return $http->request( $url, $args ); |
48 |
} |
|
49 |
||
50 |
/** |
|
51 |
* Retrieve the raw response from a safe HTTP request using the GET method. |
|
52 |
* |
|
53 |
* This function is ideal when the HTTP request is being made to an arbitrary |
|
54 |
* URL. The URL is validated to avoid redirection and request forgery attacks. |
|
55 |
* |
|
56 |
* @since 3.6.0 |
|
57 |
* |
|
5 | 58 |
* @see wp_remote_request() For more information on the response array format. |
59 |
* @see WP_Http::request() For default arguments information. |
|
60 |
* |
|
16 | 61 |
* @param string $url URL to retrieve. |
5 | 62 |
* @param array $args Optional. Request arguments. Default empty array. |
16 | 63 |
* @return array|WP_Error The response or WP_Error on failure. |
0 | 64 |
*/ |
65 |
function wp_safe_remote_get( $url, $args = array() ) { |
|
66 |
$args['reject_unsafe_urls'] = true; |
|
9 | 67 |
$http = _wp_http_get_object(); |
0 | 68 |
return $http->get( $url, $args ); |
69 |
} |
|
70 |
||
71 |
/** |
|
72 |
* Retrieve the raw response from a safe HTTP request using the POST method. |
|
73 |
* |
|
74 |
* This function is ideal when the HTTP request is being made to an arbitrary |
|
75 |
* URL. The URL is validated to avoid redirection and request forgery attacks. |
|
76 |
* |
|
77 |
* @since 3.6.0 |
|
78 |
* |
|
5 | 79 |
* @see wp_remote_request() For more information on the response array format. |
80 |
* @see WP_Http::request() For default arguments information. |
|
81 |
* |
|
16 | 82 |
* @param string $url URL to retrieve. |
5 | 83 |
* @param array $args Optional. Request arguments. Default empty array. |
16 | 84 |
* @return array|WP_Error The response or WP_Error on failure. |
0 | 85 |
*/ |
86 |
function wp_safe_remote_post( $url, $args = array() ) { |
|
87 |
$args['reject_unsafe_urls'] = true; |
|
9 | 88 |
$http = _wp_http_get_object(); |
0 | 89 |
return $http->post( $url, $args ); |
90 |
} |
|
91 |
||
92 |
/** |
|
93 |
* Retrieve the raw response from a safe HTTP request using the HEAD method. |
|
94 |
* |
|
95 |
* This function is ideal when the HTTP request is being made to an arbitrary |
|
96 |
* URL. The URL is validated to avoid redirection and request forgery attacks. |
|
97 |
* |
|
98 |
* @since 3.6.0 |
|
99 |
* |
|
5 | 100 |
* @see wp_remote_request() For more information on the response array format. |
101 |
* @see WP_Http::request() For default arguments information. |
|
102 |
* |
|
16 | 103 |
* @param string $url URL to retrieve. |
104 |
* @param array $args Optional. Request arguments. Default empty array. |
|
105 |
* @return array|WP_Error The response or WP_Error on failure. |
|
0 | 106 |
*/ |
107 |
function wp_safe_remote_head( $url, $args = array() ) { |
|
108 |
$args['reject_unsafe_urls'] = true; |
|
9 | 109 |
$http = _wp_http_get_object(); |
0 | 110 |
return $http->head( $url, $args ); |
111 |
} |
|
112 |
||
113 |
/** |
|
16 | 114 |
* Performs an HTTP request and returns its response. |
0 | 115 |
* |
16 | 116 |
* There are other API functions available which abstract away the HTTP method: |
0 | 117 |
* |
118 |
* - Default 'GET' for wp_remote_get() |
|
119 |
* - Default 'POST' for wp_remote_post() |
|
120 |
* - Default 'HEAD' for wp_remote_head() |
|
121 |
* |
|
122 |
* @since 2.7.0 |
|
123 |
* |
|
16 | 124 |
* @see WP_Http::request() For information on default arguments. |
125 |
* |
|
126 |
* @param string $url URL to retrieve. |
|
127 |
* @param array $args Optional. Request arguments. Default empty array. |
|
128 |
* @return array|WP_Error { |
|
129 |
* The response array or a WP_Error on failure. |
|
5 | 130 |
* |
16 | 131 |
* @type string[] $headers Array of response headers keyed by their name. |
132 |
* @type string $body Response body. |
|
133 |
* @type array $response { |
|
134 |
* Data about the HTTP response. |
|
135 |
* |
|
136 |
* @type int|false $code HTTP response code. |
|
137 |
* @type string|false $message HTTP response message. |
|
138 |
* } |
|
139 |
* @type WP_HTTP_Cookie[] $cookies Array of response cookies. |
|
140 |
* @type WP_HTTP_Requests_Response|null $http_response Raw HTTP response object. |
|
141 |
* } |
|
0 | 142 |
*/ |
9 | 143 |
function wp_remote_request( $url, $args = array() ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
144 |
$http = _wp_http_get_object(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
145 |
return $http->request( $url, $args ); |
0 | 146 |
} |
147 |
||
148 |
/** |
|
16 | 149 |
* Performs an HTTP request using the GET method and returns its response. |
0 | 150 |
* |
151 |
* @since 2.7.0 |
|
152 |
* |
|
5 | 153 |
* @see wp_remote_request() For more information on the response array format. |
154 |
* @see WP_Http::request() For default arguments information. |
|
155 |
* |
|
16 | 156 |
* @param string $url URL to retrieve. |
5 | 157 |
* @param array $args Optional. Request arguments. Default empty array. |
16 | 158 |
* @return array|WP_Error The response or WP_Error on failure. |
0 | 159 |
*/ |
9 | 160 |
function wp_remote_get( $url, $args = array() ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
161 |
$http = _wp_http_get_object(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
162 |
return $http->get( $url, $args ); |
0 | 163 |
} |
164 |
||
165 |
/** |
|
16 | 166 |
* Performs an HTTP request using the POST method and returns its response. |
0 | 167 |
* |
168 |
* @since 2.7.0 |
|
169 |
* |
|
5 | 170 |
* @see wp_remote_request() For more information on the response array format. |
171 |
* @see WP_Http::request() For default arguments information. |
|
172 |
* |
|
16 | 173 |
* @param string $url URL to retrieve. |
5 | 174 |
* @param array $args Optional. Request arguments. Default empty array. |
16 | 175 |
* @return array|WP_Error The response or WP_Error on failure. |
0 | 176 |
*/ |
9 | 177 |
function wp_remote_post( $url, $args = array() ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
178 |
$http = _wp_http_get_object(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
179 |
return $http->post( $url, $args ); |
0 | 180 |
} |
181 |
||
182 |
/** |
|
16 | 183 |
* Performs an HTTP request using the HEAD method and returns its response. |
0 | 184 |
* |
185 |
* @since 2.7.0 |
|
186 |
* |
|
5 | 187 |
* @see wp_remote_request() For more information on the response array format. |
188 |
* @see WP_Http::request() For default arguments information. |
|
189 |
* |
|
16 | 190 |
* @param string $url URL to retrieve. |
5 | 191 |
* @param array $args Optional. Request arguments. Default empty array. |
16 | 192 |
* @return array|WP_Error The response or WP_Error on failure. |
0 | 193 |
*/ |
9 | 194 |
function wp_remote_head( $url, $args = array() ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
195 |
$http = _wp_http_get_object(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
196 |
return $http->head( $url, $args ); |
0 | 197 |
} |
198 |
||
199 |
/** |
|
200 |
* Retrieve only the headers from the raw response. |
|
201 |
* |
|
202 |
* @since 2.7.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
203 |
* @since 4.6.0 Return value changed from an array to an Requests_Utility_CaseInsensitiveDictionary instance. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
204 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
205 |
* @see \Requests_Utility_CaseInsensitiveDictionary |
0 | 206 |
* |
16 | 207 |
* @param array|WP_Error $response HTTP response. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
208 |
* @return array|\Requests_Utility_CaseInsensitiveDictionary The headers of the response. Empty array if incorrect parameter given. |
0 | 209 |
*/ |
5 | 210 |
function wp_remote_retrieve_headers( $response ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
211 |
if ( is_wp_error( $response ) || ! isset( $response['headers'] ) ) { |
0 | 212 |
return array(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
213 |
} |
0 | 214 |
|
215 |
return $response['headers']; |
|
216 |
} |
|
217 |
||
218 |
/** |
|
219 |
* Retrieve a single header by name from the raw response. |
|
220 |
* |
|
221 |
* @since 2.7.0 |
|
222 |
* |
|
16 | 223 |
* @param array|WP_Error $response HTTP response. |
224 |
* @param string $header Header name to retrieve value from. |
|
0 | 225 |
* @return string The header value. Empty string on if incorrect parameter given, or if the header doesn't exist. |
226 |
*/ |
|
5 | 227 |
function wp_remote_retrieve_header( $response, $header ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
228 |
if ( is_wp_error( $response ) || ! isset( $response['headers'] ) ) { |
0 | 229 |
return ''; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
230 |
} |
0 | 231 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
232 |
if ( isset( $response['headers'][ $header ] ) ) { |
9 | 233 |
return $response['headers'][ $header ]; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
234 |
} |
0 | 235 |
|
236 |
return ''; |
|
237 |
} |
|
238 |
||
239 |
/** |
|
240 |
* Retrieve only the response code from the raw response. |
|
241 |
* |
|
242 |
* Will return an empty array if incorrect parameter value is given. |
|
243 |
* |
|
244 |
* @since 2.7.0 |
|
245 |
* |
|
16 | 246 |
* @param array|WP_Error $response HTTP response. |
5 | 247 |
* @return int|string The response code as an integer. Empty string on incorrect parameter given. |
0 | 248 |
*/ |
5 | 249 |
function wp_remote_retrieve_response_code( $response ) { |
9 | 250 |
if ( is_wp_error( $response ) || ! isset( $response['response'] ) || ! is_array( $response['response'] ) ) { |
0 | 251 |
return ''; |
9 | 252 |
} |
0 | 253 |
|
254 |
return $response['response']['code']; |
|
255 |
} |
|
256 |
||
257 |
/** |
|
258 |
* Retrieve only the response message from the raw response. |
|
259 |
* |
|
260 |
* Will return an empty array if incorrect parameter value is given. |
|
261 |
* |
|
262 |
* @since 2.7.0 |
|
263 |
* |
|
16 | 264 |
* @param array|WP_Error $response HTTP response. |
0 | 265 |
* @return string The response message. Empty string on incorrect parameter given. |
266 |
*/ |
|
5 | 267 |
function wp_remote_retrieve_response_message( $response ) { |
9 | 268 |
if ( is_wp_error( $response ) || ! isset( $response['response'] ) || ! is_array( $response['response'] ) ) { |
0 | 269 |
return ''; |
9 | 270 |
} |
0 | 271 |
|
272 |
return $response['response']['message']; |
|
273 |
} |
|
274 |
||
275 |
/** |
|
276 |
* Retrieve only the body from the raw response. |
|
277 |
* |
|
278 |
* @since 2.7.0 |
|
279 |
* |
|
16 | 280 |
* @param array|WP_Error $response HTTP response. |
0 | 281 |
* @return string The body of the response. Empty string if no body or incorrect parameter given. |
282 |
*/ |
|
5 | 283 |
function wp_remote_retrieve_body( $response ) { |
9 | 284 |
if ( is_wp_error( $response ) || ! isset( $response['body'] ) ) { |
0 | 285 |
return ''; |
9 | 286 |
} |
0 | 287 |
|
288 |
return $response['body']; |
|
289 |
} |
|
290 |
||
291 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
292 |
* Retrieve only the cookies from the raw response. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
293 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
294 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
295 |
* |
16 | 296 |
* @param array|WP_Error $response HTTP response. |
297 |
* @return WP_Http_Cookie[] An array of `WP_Http_Cookie` objects from the response. Empty array if there are none, or the response is a WP_Error. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
298 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
299 |
function wp_remote_retrieve_cookies( $response ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
300 |
if ( is_wp_error( $response ) || empty( $response['cookies'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
301 |
return array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
302 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
303 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
304 |
return $response['cookies']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
305 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
306 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
307 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
308 |
* Retrieve a single cookie by name from the raw response. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
309 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
310 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
311 |
* |
16 | 312 |
* @param array|WP_Error $response HTTP response. |
313 |
* @param string $name The name of the cookie to retrieve. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
314 |
* @return WP_Http_Cookie|string The `WP_Http_Cookie` object. Empty string if the cookie isn't present in the response. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
315 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
316 |
function wp_remote_retrieve_cookie( $response, $name ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
317 |
$cookies = wp_remote_retrieve_cookies( $response ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
318 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
319 |
if ( empty( $cookies ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
320 |
return ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
321 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
322 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
323 |
foreach ( $cookies as $cookie ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
324 |
if ( $cookie->name === $name ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
325 |
return $cookie; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
326 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
327 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
328 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
329 |
return ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
330 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
331 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
332 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
333 |
* Retrieve a single cookie's value by name from the raw response. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
334 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
335 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
336 |
* |
16 | 337 |
* @param array|WP_Error $response HTTP response. |
338 |
* @param string $name The name of the cookie to retrieve. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
339 |
* @return string The value of the cookie. Empty string if the cookie isn't present in the response. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
340 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
341 |
function wp_remote_retrieve_cookie_value( $response, $name ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
342 |
$cookie = wp_remote_retrieve_cookie( $response, $name ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
343 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
344 |
if ( ! is_a( $cookie, 'WP_Http_Cookie' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
345 |
return ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
346 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
347 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
348 |
return $cookie->value; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
349 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
350 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
351 |
/** |
0 | 352 |
* Determines if there is an HTTP Transport that can process this request. |
353 |
* |
|
354 |
* @since 3.2.0 |
|
355 |
* |
|
356 |
* @param array $capabilities Array of capabilities to test or a wp_remote_request() $args array. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
357 |
* @param string $url Optional. If given, will check if the URL requires SSL and adds |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
358 |
* that requirement to the capabilities array. |
0 | 359 |
* |
360 |
* @return bool |
|
361 |
*/ |
|
362 |
function wp_http_supports( $capabilities = array(), $url = null ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
363 |
$http = _wp_http_get_object(); |
0 | 364 |
|
365 |
$capabilities = wp_parse_args( $capabilities ); |
|
366 |
||
367 |
$count = count( $capabilities ); |
|
368 |
||
16 | 369 |
// If we have a numeric $capabilities array, spoof a wp_remote_request() associative $args array. |
0 | 370 |
if ( $count && count( array_filter( array_keys( $capabilities ), 'is_numeric' ) ) == $count ) { |
371 |
$capabilities = array_combine( array_values( $capabilities ), array_fill( 0, $count, true ) ); |
|
372 |
} |
|
373 |
||
9 | 374 |
if ( $url && ! isset( $capabilities['ssl'] ) ) { |
0 | 375 |
$scheme = parse_url( $url, PHP_URL_SCHEME ); |
16 | 376 |
if ( 'https' === $scheme || 'ssl' === $scheme ) { |
0 | 377 |
$capabilities['ssl'] = true; |
378 |
} |
|
379 |
} |
|
380 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
381 |
return (bool) $http->_get_first_available_transport( $capabilities ); |
0 | 382 |
} |
383 |
||
384 |
/** |
|
385 |
* Get the HTTP Origin of the current request. |
|
386 |
* |
|
387 |
* @since 3.4.0 |
|
388 |
* |
|
389 |
* @return string URL of the origin. Empty string if no origin. |
|
390 |
*/ |
|
391 |
function get_http_origin() { |
|
392 |
$origin = ''; |
|
9 | 393 |
if ( ! empty( $_SERVER['HTTP_ORIGIN'] ) ) { |
394 |
$origin = $_SERVER['HTTP_ORIGIN']; |
|
395 |
} |
|
0 | 396 |
|
397 |
/** |
|
398 |
* Change the origin of an HTTP request. |
|
399 |
* |
|
400 |
* @since 3.4.0 |
|
401 |
* |
|
402 |
* @param string $origin The original origin for the request. |
|
403 |
*/ |
|
404 |
return apply_filters( 'http_origin', $origin ); |
|
405 |
} |
|
406 |
||
407 |
/** |
|
408 |
* Retrieve list of allowed HTTP origins. |
|
409 |
* |
|
410 |
* @since 3.4.0 |
|
411 |
* |
|
16 | 412 |
* @return string[] Array of origin URLs. |
0 | 413 |
*/ |
414 |
function get_allowed_http_origins() { |
|
415 |
$admin_origin = parse_url( admin_url() ); |
|
9 | 416 |
$home_origin = parse_url( home_url() ); |
0 | 417 |
|
16 | 418 |
// @todo Preserve port? |
9 | 419 |
$allowed_origins = array_unique( |
420 |
array( |
|
421 |
'http://' . $admin_origin['host'], |
|
422 |
'https://' . $admin_origin['host'], |
|
423 |
'http://' . $home_origin['host'], |
|
424 |
'https://' . $home_origin['host'], |
|
425 |
) |
|
426 |
); |
|
0 | 427 |
|
428 |
/** |
|
429 |
* Change the origin types allowed for HTTP requests. |
|
430 |
* |
|
431 |
* @since 3.4.0 |
|
432 |
* |
|
16 | 433 |
* @param string[] $allowed_origins { |
434 |
* Array of default allowed HTTP origins. |
|
435 |
* |
|
436 |
* @type string $0 Non-secure URL for admin origin. |
|
437 |
* @type string $1 Secure URL for admin origin. |
|
438 |
* @type string $2 Non-secure URL for home origin. |
|
439 |
* @type string $3 Secure URL for home origin. |
|
0 | 440 |
* } |
441 |
*/ |
|
9 | 442 |
return apply_filters( 'allowed_http_origins', $allowed_origins ); |
0 | 443 |
} |
444 |
||
445 |
/** |
|
446 |
* Determines if the HTTP origin is an authorized one. |
|
447 |
* |
|
448 |
* @since 3.4.0 |
|
449 |
* |
|
5 | 450 |
* @param null|string $origin Origin URL. If not provided, the value of get_http_origin() is used. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
451 |
* @return string Origin URL if allowed, empty string if not. |
0 | 452 |
*/ |
453 |
function is_allowed_http_origin( $origin = null ) { |
|
454 |
$origin_arg = $origin; |
|
455 |
||
9 | 456 |
if ( null === $origin ) { |
0 | 457 |
$origin = get_http_origin(); |
9 | 458 |
} |
0 | 459 |
|
16 | 460 |
if ( $origin && ! in_array( $origin, get_allowed_http_origins(), true ) ) { |
0 | 461 |
$origin = ''; |
9 | 462 |
} |
0 | 463 |
|
464 |
/** |
|
465 |
* Change the allowed HTTP origin result. |
|
466 |
* |
|
467 |
* @since 3.4.0 |
|
468 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
469 |
* @param string $origin Origin URL if allowed, empty string if not. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
470 |
* @param string $origin_arg Original origin string passed into is_allowed_http_origin function. |
0 | 471 |
*/ |
472 |
return apply_filters( 'allowed_http_origin', $origin, $origin_arg ); |
|
473 |
} |
|
474 |
||
475 |
/** |
|
476 |
* Send Access-Control-Allow-Origin and related headers if the current request |
|
477 |
* is from an allowed origin. |
|
478 |
* |
|
479 |
* If the request is an OPTIONS request, the script exits with either access |
|
480 |
* control headers sent, or a 403 response if the origin is not allowed. For |
|
481 |
* other request methods, you will receive a return value. |
|
482 |
* |
|
483 |
* @since 3.4.0 |
|
484 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
485 |
* @return string|false Returns the origin URL if headers are sent. Returns false |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
486 |
* if headers are not sent. |
0 | 487 |
*/ |
488 |
function send_origin_headers() { |
|
489 |
$origin = get_http_origin(); |
|
490 |
||
491 |
if ( is_allowed_http_origin( $origin ) ) { |
|
16 | 492 |
header( 'Access-Control-Allow-Origin: ' . $origin ); |
493 |
header( 'Access-Control-Allow-Credentials: true' ); |
|
9 | 494 |
if ( 'OPTIONS' === $_SERVER['REQUEST_METHOD'] ) { |
0 | 495 |
exit; |
9 | 496 |
} |
0 | 497 |
return $origin; |
498 |
} |
|
499 |
||
500 |
if ( 'OPTIONS' === $_SERVER['REQUEST_METHOD'] ) { |
|
501 |
status_header( 403 ); |
|
502 |
exit; |
|
503 |
} |
|
504 |
||
505 |
return false; |
|
506 |
} |
|
507 |
||
508 |
/** |
|
509 |
* Validate a URL for safe use in the HTTP API. |
|
510 |
* |
|
511 |
* @since 3.5.2 |
|
512 |
* |
|
16 | 513 |
* @param string $url Request URL. |
514 |
* @return string|false URL or false on failure. |
|
0 | 515 |
*/ |
516 |
function wp_http_validate_url( $url ) { |
|
5 | 517 |
$original_url = $url; |
9 | 518 |
$url = wp_kses_bad_protocol( $url, array( 'http', 'https' ) ); |
519 |
if ( ! $url || strtolower( $url ) !== strtolower( $original_url ) ) { |
|
0 | 520 |
return false; |
9 | 521 |
} |
0 | 522 |
|
16 | 523 |
$parsed_url = parse_url( $url ); |
9 | 524 |
if ( ! $parsed_url || empty( $parsed_url['host'] ) ) { |
0 | 525 |
return false; |
9 | 526 |
} |
0 | 527 |
|
9 | 528 |
if ( isset( $parsed_url['user'] ) || isset( $parsed_url['pass'] ) ) { |
0 | 529 |
return false; |
9 | 530 |
} |
0 | 531 |
|
9 | 532 |
if ( false !== strpbrk( $parsed_url['host'], ':#?[]' ) ) { |
0 | 533 |
return false; |
9 | 534 |
} |
0 | 535 |
|
16 | 536 |
$parsed_home = parse_url( get_option( 'home' ) ); |
0 | 537 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
538 |
if ( isset( $parsed_home['host'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
539 |
$same_host = strtolower( $parsed_home['host'] ) === strtolower( $parsed_url['host'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
540 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
541 |
$same_host = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
542 |
} |
0 | 543 |
|
544 |
if ( ! $same_host ) { |
|
545 |
$host = trim( $parsed_url['host'], '.' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
546 |
if ( preg_match( '#^(([1-9]?\d|1\d\d|25[0-5]|2[0-4]\d)\.){3}([1-9]?\d|1\d\d|25[0-5]|2[0-4]\d)$#', $host ) ) { |
0 | 547 |
$ip = $host; |
548 |
} else { |
|
549 |
$ip = gethostbyname( $host ); |
|
16 | 550 |
if ( $ip === $host ) { // Error condition for gethostbyname(). |
13 | 551 |
return false; |
9 | 552 |
} |
0 | 553 |
} |
554 |
if ( $ip ) { |
|
555 |
$parts = array_map( 'intval', explode( '.', $ip ) ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
556 |
if ( 127 === $parts[0] || 10 === $parts[0] || 0 === $parts[0] |
0 | 557 |
|| ( 172 === $parts[0] && 16 <= $parts[1] && 31 >= $parts[1] ) |
558 |
|| ( 192 === $parts[0] && 168 === $parts[1] ) |
|
559 |
) { |
|
560 |
// If host appears local, reject unless specifically allowed. |
|
561 |
/** |
|
562 |
* Check if HTTP request is external or not. |
|
563 |
* |
|
564 |
* Allows to change and allow external requests for the HTTP request. |
|
565 |
* |
|
566 |
* @since 3.6.0 |
|
567 |
* |
|
16 | 568 |
* @param bool $external Whether HTTP request is external or not. |
569 |
* @param string $host Host name of the requested URL. |
|
570 |
* @param string $url Requested URL. |
|
0 | 571 |
*/ |
9 | 572 |
if ( ! apply_filters( 'http_request_host_is_external', false, $host, $url ) ) { |
0 | 573 |
return false; |
9 | 574 |
} |
0 | 575 |
} |
576 |
} |
|
577 |
} |
|
578 |
||
9 | 579 |
if ( empty( $parsed_url['port'] ) ) { |
0 | 580 |
return $url; |
9 | 581 |
} |
0 | 582 |
|
583 |
$port = $parsed_url['port']; |
|
9 | 584 |
if ( 80 === $port || 443 === $port || 8080 === $port ) { |
0 | 585 |
return $url; |
9 | 586 |
} |
0 | 587 |
|
9 | 588 |
if ( $parsed_home && $same_host && isset( $parsed_home['port'] ) && $parsed_home['port'] === $port ) { |
0 | 589 |
return $url; |
9 | 590 |
} |
0 | 591 |
|
592 |
return false; |
|
593 |
} |
|
594 |
||
595 |
/** |
|
16 | 596 |
* Mark allowed redirect hosts safe for HTTP requests as well. |
0 | 597 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
598 |
* Attached to the {@see 'http_request_host_is_external'} filter. |
0 | 599 |
* |
600 |
* @since 3.6.0 |
|
601 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
602 |
* @param bool $is_external |
0 | 603 |
* @param string $host |
604 |
* @return bool |
|
605 |
*/ |
|
606 |
function allowed_http_request_hosts( $is_external, $host ) { |
|
9 | 607 |
if ( ! $is_external && wp_validate_redirect( 'http://' . $host ) ) { |
0 | 608 |
$is_external = true; |
9 | 609 |
} |
0 | 610 |
return $is_external; |
611 |
} |
|
612 |
||
613 |
/** |
|
16 | 614 |
* Adds any domain in a multisite installation for safe HTTP requests to the |
615 |
* allowed list. |
|
0 | 616 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
617 |
* Attached to the {@see 'http_request_host_is_external'} filter. |
0 | 618 |
* |
619 |
* @since 3.6.0 |
|
620 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
621 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
622 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
623 |
* @param bool $is_external |
0 | 624 |
* @param string $host |
625 |
* @return bool |
|
626 |
*/ |
|
627 |
function ms_allowed_http_request_hosts( $is_external, $host ) { |
|
5 | 628 |
global $wpdb; |
0 | 629 |
static $queried = array(); |
9 | 630 |
if ( $is_external ) { |
0 | 631 |
return $is_external; |
9 | 632 |
} |
16 | 633 |
if ( get_network()->domain === $host ) { |
0 | 634 |
return true; |
9 | 635 |
} |
636 |
if ( isset( $queried[ $host ] ) ) { |
|
0 | 637 |
return $queried[ $host ]; |
9 | 638 |
} |
0 | 639 |
$queried[ $host ] = (bool) $wpdb->get_var( $wpdb->prepare( "SELECT domain FROM $wpdb->blogs WHERE domain = %s LIMIT 1", $host ) ); |
640 |
return $queried[ $host ]; |
|
641 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
642 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
643 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
644 |
* A wrapper for PHP's parse_url() function that handles consistency in the return |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
645 |
* values across PHP versions. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
646 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
647 |
* PHP 5.4.7 expanded parse_url()'s ability to handle non-absolute url's, including |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
648 |
* schemeless and relative url's with :// in the path. This function works around |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
649 |
* those limitations providing a standard output on PHP 5.2~5.4+. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
650 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
651 |
* Secondly, across various PHP versions, schemeless URLs starting containing a ":" |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
652 |
* in the query are being handled inconsistently. This function works around those |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
653 |
* differences as well. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
654 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
655 |
* @since 4.4.0 |
9 | 656 |
* @since 4.7.0 The `$component` parameter was added for parity with PHP's `parse_url()`. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
657 |
* |
16 | 658 |
* @link https://www.php.net/manual/en/function.parse-url.php |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
659 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
660 |
* @param string $url The URL to parse. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
661 |
* @param int $component The specific component to retrieve. Use one of the PHP |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
662 |
* predefined constants to specify which one. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
663 |
* Defaults to -1 (= return all parts as an array). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
664 |
* @return mixed False on parse failure; Array of URL components on success; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
665 |
* When a specific component has been requested: null if the component |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
666 |
* doesn't exist in the given URL; a string or - in the case of |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
667 |
* PHP_URL_PORT - integer when it does. See parse_url()'s return values. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
668 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
669 |
function wp_parse_url( $url, $component = -1 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
670 |
$to_unset = array(); |
18 | 671 |
$url = (string) $url; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
672 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
673 |
if ( '//' === substr( $url, 0, 2 ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
674 |
$to_unset[] = 'scheme'; |
9 | 675 |
$url = 'placeholder:' . $url; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
676 |
} elseif ( '/' === substr( $url, 0, 1 ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
677 |
$to_unset[] = 'scheme'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
678 |
$to_unset[] = 'host'; |
9 | 679 |
$url = 'placeholder://placeholder' . $url; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
680 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
681 |
|
16 | 682 |
$parts = parse_url( $url ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
683 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
684 |
if ( false === $parts ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
685 |
// Parsing failure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
686 |
return $parts; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
687 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
688 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
689 |
// Remove the placeholder values. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
690 |
foreach ( $to_unset as $key ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
691 |
unset( $parts[ $key ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
692 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
693 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
694 |
return _get_component_from_parsed_url_array( $parts, $component ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
695 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
696 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
697 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
698 |
* Retrieve a specific component from a parsed URL array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
699 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
700 |
* @internal |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
701 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
702 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
703 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
704 |
* |
16 | 705 |
* @link https://www.php.net/manual/en/function.parse-url.php |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
706 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
707 |
* @param array|false $url_parts The parsed URL. Can be false if the URL failed to parse. |
16 | 708 |
* @param int $component The specific component to retrieve. Use one of the PHP |
709 |
* predefined constants to specify which one. |
|
710 |
* Defaults to -1 (= return all parts as an array). |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
711 |
* @return mixed False on parse failure; Array of URL components on success; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
712 |
* When a specific component has been requested: null if the component |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
713 |
* doesn't exist in the given URL; a string or - in the case of |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
714 |
* PHP_URL_PORT - integer when it does. See parse_url()'s return values. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
715 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
716 |
function _get_component_from_parsed_url_array( $url_parts, $component = -1 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
717 |
if ( -1 === $component ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
718 |
return $url_parts; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
719 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
720 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
721 |
$key = _wp_translate_php_url_constant_to_key( $component ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
722 |
if ( false !== $key && is_array( $url_parts ) && isset( $url_parts[ $key ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
723 |
return $url_parts[ $key ]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
724 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
725 |
return null; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
726 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
727 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
728 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
729 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
730 |
* Translate a PHP_URL_* constant to the named array keys PHP uses. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
731 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
732 |
* @internal |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
733 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
734 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
735 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
736 |
* |
16 | 737 |
* @link https://www.php.net/manual/en/url.constants.php |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
738 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
739 |
* @param int $constant PHP_URL_* constant. |
16 | 740 |
* @return string|false The named key or false. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
741 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
742 |
function _wp_translate_php_url_constant_to_key( $constant ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
743 |
$translation = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
744 |
PHP_URL_SCHEME => 'scheme', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
745 |
PHP_URL_HOST => 'host', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
746 |
PHP_URL_PORT => 'port', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
747 |
PHP_URL_USER => 'user', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
748 |
PHP_URL_PASS => 'pass', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
749 |
PHP_URL_PATH => 'path', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
750 |
PHP_URL_QUERY => 'query', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
751 |
PHP_URL_FRAGMENT => 'fragment', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
752 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
753 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
754 |
if ( isset( $translation[ $constant ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
755 |
return $translation[ $constant ]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
756 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
757 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
758 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
759 |
} |