| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Core HTTP Request API 4 * 5 * Standardizes the HTTP requests for WordPress. Handles cookies, gzip encoding and decoding, chunk 6 * decoding, if HTTP 1.1 and various other difficult HTTP protocol implementations. 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() { 21 static $http = null; 22 23 if ( is_null( $http ) ) { 24 $http = new WP_Http(); 25 } 26 return $http; 27 } 28 29 /** 30 * Retrieves 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, and every URL it redirects to, are validated with wp_http_validate_url() 34 * to avoid Server Side Request Forgery attacks (SSRF). 35 * 36 * The only supported protocols are `http` and `https`. 37 * 38 * @since 3.6.0 39 * 40 * @see wp_remote_request() For more information on the response array format. 41 * @see WP_Http::request() For default arguments information. 42 * @see wp_http_validate_url() For more information about how the URL is validated. 43 * 44 * @link https://owasp.org/www-community/attacks/Server_Side_Request_Forgery 45 * 46 * @param string $url URL to retrieve. 47 * @param array $args Optional. Request arguments. Default empty array. 48 * See WP_Http::request() for information on accepted arguments. 49 * @return array|WP_Error The response or WP_Error on failure. 50 * See WP_Http::request() for information on return value. 51 */ 52 function wp_safe_remote_request( $url, $args = array() ) { 53 $args['reject_unsafe_urls'] = true; 54 $http = _wp_http_get_object(); 55 return $http->request( $url, $args ); 56 } 57 58 /** 59 * Retrieves the raw response from a safe HTTP request using the GET method. 60 * 61 * This function is ideal when the HTTP request is being made to an arbitrary 62 * URL. The URL, and every URL it redirects to, are validated with wp_http_validate_url() 63 * to avoid Server Side Request Forgery attacks (SSRF). 64 * 65 * The only supported protocols are `http` and `https`. 66 * 67 * @since 3.6.0 68 * 69 * @see wp_remote_request() For more information on the response array format. 70 * @see WP_Http::request() For default arguments information. 71 * @see wp_http_validate_url() For more information about how the URL is validated. 72 * 73 * @link https://owasp.org/www-community/attacks/Server_Side_Request_Forgery 74 * 75 * @param string $url URL to retrieve. 76 * @param array $args Optional. Request arguments. Default empty array. 77 * See WP_Http::request() for information on accepted arguments. 78 * @return array|WP_Error The response or WP_Error on failure. 79 * See WP_Http::request() for information on return value. 80 */ 81 function wp_safe_remote_get( $url, $args = array() ) { 82 $args['reject_unsafe_urls'] = true; 83 $http = _wp_http_get_object(); 84 return $http->get( $url, $args ); 85 } 86 87 /** 88 * Retrieves the raw response from a safe HTTP request using the POST method. 89 * 90 * This function is ideal when the HTTP request is being made to an arbitrary 91 * URL. The URL, and every URL it redirects to, are validated with wp_http_validate_url() 92 * to avoid Server Side Request Forgery attacks (SSRF). 93 * 94 * The only supported protocols are `http` and `https`. 95 * 96 * @since 3.6.0 97 * 98 * @see wp_remote_request() For more information on the response array format. 99 * @see WP_Http::request() For default arguments information. 100 * @see wp_http_validate_url() For more information about how the URL is validated. 101 * 102 * @link https://owasp.org/www-community/attacks/Server_Side_Request_Forgery 103 * 104 * @param string $url URL to retrieve. 105 * @param array $args Optional. Request arguments. Default empty array. 106 * See WP_Http::request() for information on accepted arguments. 107 * @return array|WP_Error The response or WP_Error on failure. 108 * See WP_Http::request() for information on return value. 109 */ 110 function wp_safe_remote_post( $url, $args = array() ) { 111 $args['reject_unsafe_urls'] = true; 112 $http = _wp_http_get_object(); 113 return $http->post( $url, $args ); 114 } 115 116 /** 117 * Retrieves the raw response from a safe HTTP request using the HEAD method. 118 * 119 * This function is ideal when the HTTP request is being made to an arbitrary 120 * URL. The URL, and every URL it redirects to, are validated with wp_http_validate_url() 121 * to avoid Server Side Request Forgery attacks (SSRF). 122 * 123 * The only supported protocols are `http` and `https`. 124 * 125 * @since 3.6.0 126 * 127 * @see wp_remote_request() For more information on the response array format. 128 * @see WP_Http::request() For default arguments information. 129 * @see wp_http_validate_url() For more information about how the URL is validated. 130 * 131 * @link https://owasp.org/www-community/attacks/Server_Side_Request_Forgery 132 * 133 * @param string $url URL to retrieve. 134 * @param array $args Optional. Request arguments. Default empty array. 135 * See WP_Http::request() for information on accepted arguments. 136 * @return array|WP_Error The response or WP_Error on failure. 137 * See WP_Http::request() for information on return value. 138 */ 139 function wp_safe_remote_head( $url, $args = array() ) { 140 $args['reject_unsafe_urls'] = true; 141 $http = _wp_http_get_object(); 142 return $http->head( $url, $args ); 143 } 144 145 /** 146 * Performs an HTTP request and returns its response. 147 * 148 * There are other API functions available which abstract away the HTTP method: 149 * 150 * - Default 'GET' for wp_remote_get() 151 * - Default 'POST' for wp_remote_post() 152 * - Default 'HEAD' for wp_remote_head() 153 * 154 * Important: If the URL is user-controlled, use `wp_safe_remote_request()` instead. 155 * 156 * @since 2.7.0 157 * 158 * @see WP_Http::request() For information on default arguments. 159 * 160 * @param string $url URL to retrieve. 161 * @param array $args Optional. Request arguments. Default empty array. 162 * See WP_Http::request() for information on accepted arguments. 163 * @return array|WP_Error The response array or a WP_Error on failure. 164 * See WP_Http::request() for information on return value. 165 */ 166 function wp_remote_request( $url, $args = array() ) { 167 $http = _wp_http_get_object(); 168 return $http->request( $url, $args ); 169 } 170 171 /** 172 * Performs an HTTP request using the GET method and returns its response. 173 * 174 * Important: If the URL is user-controlled, use `wp_safe_remote_get()` instead. 175 * 176 * @since 2.7.0 177 * 178 * @see wp_remote_request() For more information on the response array format. 179 * @see WP_Http::request() For default arguments information. 180 * 181 * @param string $url URL to retrieve. 182 * @param array $args Optional. Request arguments. Default empty array. 183 * See WP_Http::request() for information on accepted arguments. 184 * @return array|WP_Error The response or WP_Error on failure. 185 * See WP_Http::request() for information on return value. 186 */ 187 function wp_remote_get( $url, $args = array() ) { 188 $http = _wp_http_get_object(); 189 return $http->get( $url, $args ); 190 } 191 192 /** 193 * Performs an HTTP request using the POST method and returns its response. 194 * 195 * Important: If the URL is user-controlled, use `wp_safe_remote_post()` instead. 196 * 197 * @since 2.7.0 198 * 199 * @see wp_remote_request() For more information on the response array format. 200 * @see WP_Http::request() For default arguments information. 201 * 202 * @param string $url URL to retrieve. 203 * @param array $args Optional. Request arguments. Default empty array. 204 * See WP_Http::request() for information on accepted arguments. 205 * @return array|WP_Error The response or WP_Error on failure. 206 * See WP_Http::request() for information on return value. 207 */ 208 function wp_remote_post( $url, $args = array() ) { 209 $http = _wp_http_get_object(); 210 return $http->post( $url, $args ); 211 } 212 213 /** 214 * Performs an HTTP request using the HEAD method and returns its response. 215 * 216 * Important: If the URL is user-controlled, use `wp_safe_remote_head()` instead. 217 * 218 * @since 2.7.0 219 * 220 * @see wp_remote_request() For more information on the response array format. 221 * @see WP_Http::request() For default arguments information. 222 * 223 * @param string $url URL to retrieve. 224 * @param array $args Optional. Request arguments. Default empty array. 225 * See WP_Http::request() for information on accepted arguments. 226 * @return array|WP_Error The response or WP_Error on failure. 227 * See WP_Http::request() for information on return value. 228 */ 229 function wp_remote_head( $url, $args = array() ) { 230 $http = _wp_http_get_object(); 231 return $http->head( $url, $args ); 232 } 233 234 /** 235 * Retrieves only the headers from the raw response. 236 * 237 * @since 2.7.0 238 * @since 4.6.0 Return value changed from an array to an WpOrg\Requests\Utility\CaseInsensitiveDictionary instance. 239 * 240 * @see \WpOrg\Requests\Utility\CaseInsensitiveDictionary 241 * 242 * @param array|WP_Error $response HTTP response. 243 * @return \WpOrg\Requests\Utility\CaseInsensitiveDictionary|array The headers of the response, or empty array 244 * if incorrect parameter given. 245 */ 246 function wp_remote_retrieve_headers( $response ) { 247 if ( is_wp_error( $response ) || ! isset( $response['headers'] ) ) { 248 return array(); 249 } 250 251 return $response['headers']; 252 } 253 254 /** 255 * Retrieves a single header by name from the raw response. 256 * 257 * @since 2.7.0 258 * 259 * @param array|WP_Error $response HTTP response. 260 * @param string $header Header name to retrieve value from. 261 * @return array|string The header(s) value(s). Array if multiple headers with the same name are retrieved. 262 * Empty string if incorrect parameter given, or if the header doesn't exist. 263 */ 264 function wp_remote_retrieve_header( $response, $header ) { 265 if ( is_wp_error( $response ) || ! isset( $response['headers'] ) ) { 266 return ''; 267 } 268 269 return $response['headers'][ $header ] ?? ''; 270 } 271 272 /** 273 * Retrieves only the response code from the raw response. 274 * 275 * Will return an empty string if incorrect parameter value is given. 276 * 277 * @since 2.7.0 278 * 279 * @param array|WP_Error $response HTTP response. 280 * @return int|string The response code as an integer. Empty string if incorrect parameter given. 281 */ 282 function wp_remote_retrieve_response_code( $response ) { 283 if ( is_wp_error( $response ) || ! isset( $response['response'] ) || ! is_array( $response['response'] ) ) { 284 return ''; 285 } 286 287 return $response['response']['code']; 288 } 289 290 /** 291 * Retrieves only the response message from the raw response. 292 * 293 * Will return an empty string if incorrect parameter value is given. 294 * 295 * @since 2.7.0 296 * 297 * @param array|WP_Error $response HTTP response. 298 * @return string The response message. Empty string if incorrect parameter given. 299 */ 300 function wp_remote_retrieve_response_message( $response ) { 301 if ( is_wp_error( $response ) || ! isset( $response['response'] ) || ! is_array( $response['response'] ) ) { 302 return ''; 303 } 304 305 return $response['response']['message']; 306 } 307 308 /** 309 * Retrieves only the body from the raw response. 310 * 311 * @since 2.7.0 312 * 313 * @param array|WP_Error $response HTTP response. 314 * @return string The body of the response. Empty string if no body or incorrect parameter given. 315 */ 316 function wp_remote_retrieve_body( $response ) { 317 if ( is_wp_error( $response ) || ! isset( $response['body'] ) ) { 318 return ''; 319 } 320 321 return $response['body']; 322 } 323 324 /** 325 * Retrieves only the cookies from the raw response. 326 * 327 * @since 4.4.0 328 * 329 * @param array|WP_Error $response HTTP response. 330 * @return WP_Http_Cookie[] An array of `WP_Http_Cookie` objects from the response. 331 * Empty array if there are none, or the response is a WP_Error. 332 */ 333 function wp_remote_retrieve_cookies( $response ) { 334 if ( is_wp_error( $response ) || empty( $response['cookies'] ) ) { 335 return array(); 336 } 337 338 return $response['cookies']; 339 } 340 341 /** 342 * Retrieves a single cookie by name from the raw response. 343 * 344 * @since 4.4.0 345 * 346 * @param array|WP_Error $response HTTP response. 347 * @param string $name The name of the cookie to retrieve. 348 * @return WP_Http_Cookie|string The `WP_Http_Cookie` object, or empty string 349 * if the cookie is not present in the response. 350 */ 351 function wp_remote_retrieve_cookie( $response, $name ) { 352 $cookies = wp_remote_retrieve_cookies( $response ); 353 354 if ( empty( $cookies ) ) { 355 return ''; 356 } 357 358 foreach ( $cookies as $cookie ) { 359 if ( $cookie->name === $name ) { 360 return $cookie; 361 } 362 } 363 364 return ''; 365 } 366 367 /** 368 * Retrieves a single cookie's value by name from the raw response. 369 * 370 * @since 4.4.0 371 * 372 * @param array|WP_Error $response HTTP response. 373 * @param string $name The name of the cookie to retrieve. 374 * @return string The value of the cookie, or empty string 375 * if the cookie is not present in the response. 376 */ 377 function wp_remote_retrieve_cookie_value( $response, $name ) { 378 $cookie = wp_remote_retrieve_cookie( $response, $name ); 379 380 if ( ! ( $cookie instanceof WP_Http_Cookie ) ) { 381 return ''; 382 } 383 384 return $cookie->value; 385 } 386 387 /** 388 * Determines if there is an HTTP Transport that can process this request. 389 * 390 * @since 3.2.0 391 * 392 * @param array $capabilities Array of capabilities to test or a wp_remote_request() $args array. 393 * @param string $url Optional. If given, will check if the URL requires SSL and adds 394 * that requirement to the capabilities array. 395 * 396 * @return bool 397 */ 398 function wp_http_supports( $capabilities = array(), $url = null ) { 399 $capabilities = wp_parse_args( $capabilities ); 400 401 $count = count( $capabilities ); 402 403 // If we have a numeric $capabilities array, spoof a wp_remote_request() associative $args array. 404 if ( $count && count( array_filter( array_keys( $capabilities ), 'is_numeric' ) ) === $count ) { 405 $capabilities = array_combine( array_values( $capabilities ), array_fill( 0, $count, true ) ); 406 } 407 408 if ( $url && ! isset( $capabilities['ssl'] ) ) { 409 $scheme = parse_url( $url, PHP_URL_SCHEME ); 410 if ( 'https' === $scheme || 'ssl' === $scheme ) { 411 $capabilities['ssl'] = true; 412 } 413 } 414 415 return WpOrg\Requests\Requests::has_capabilities( $capabilities ); 416 } 417 418 /** 419 * Gets the HTTP Origin of the current request. 420 * 421 * @since 3.4.0 422 * 423 * @return string URL of the origin. Empty string if no origin. 424 */ 425 function get_http_origin() { 426 $origin = ''; 427 if ( ! empty( $_SERVER['HTTP_ORIGIN'] ) ) { 428 $origin = $_SERVER['HTTP_ORIGIN']; 429 } 430 431 /** 432 * Changes the origin of an HTTP request. 433 * 434 * @since 3.4.0 435 * 436 * @param string $origin The HTTP origin for the request. 437 */ 438 return apply_filters( 'http_origin', $origin ); 439 } 440 441 /** 442 * Retrieves list of allowed HTTP origins. 443 * 444 * @since 3.4.0 445 * 446 * @return string[] Array of origin URLs. 447 */ 448 function get_allowed_http_origins() { 449 $admin_origin = parse_url( admin_url() ); 450 $home_origin = parse_url( home_url() ); 451 452 // @todo Preserve port? 453 $allowed_origins = array_unique( 454 array( 455 'http://' . $admin_origin['host'], 456 'https://' . $admin_origin['host'], 457 'http://' . $home_origin['host'], 458 'https://' . $home_origin['host'], 459 ) 460 ); 461 462 /** 463 * Changes the origin types allowed for HTTP requests. 464 * 465 * @since 3.4.0 466 * 467 * @param string[] $allowed_origins Array of allowed HTTP origins. 468 */ 469 return apply_filters( 'allowed_http_origins', $allowed_origins ); 470 } 471 472 /** 473 * Determines if the HTTP origin is an authorized one. 474 * 475 * @since 3.4.0 476 * 477 * @param string|null $origin Origin URL. If not provided, the value of get_http_origin() is used. 478 * @return string Origin URL if allowed, empty string if not. 479 */ 480 function is_allowed_http_origin( $origin = null ) { 481 $origin_arg = $origin; 482 483 if ( null === $origin ) { 484 $origin = get_http_origin(); 485 } 486 487 if ( $origin && ! in_array( $origin, get_allowed_http_origins(), true ) ) { 488 $origin = ''; 489 } 490 491 /** 492 * Changes the allowed HTTP origin result. 493 * 494 * @since 3.4.0 495 * 496 * @param string $origin Origin URL if allowed, empty string if not. 497 * @param string $origin_arg Original origin string passed into is_allowed_http_origin function. 498 */ 499 return apply_filters( 'allowed_http_origin', $origin, $origin_arg ); 500 } 501 502 /** 503 * Sends Access-Control-Allow-Origin and related headers if the current request 504 * is from an allowed origin. 505 * 506 * If the request is an OPTIONS request, the script exits with either access 507 * control headers sent, or a 403 response if the origin is not allowed. For 508 * other request methods, you will receive a return value. 509 * 510 * @since 3.4.0 511 * 512 * @return string|false Returns the origin URL if headers are sent. Returns false 513 * if headers are not sent. 514 */ 515 function send_origin_headers() { 516 $origin = get_http_origin(); 517 518 if ( is_allowed_http_origin( $origin ) ) { 519 header( 'Access-Control-Allow-Origin: ' . $origin ); 520 header( 'Access-Control-Allow-Credentials: true' ); 521 if ( 'OPTIONS' === $_SERVER['REQUEST_METHOD'] ) { 522 exit; 523 } 524 return $origin; 525 } 526 527 if ( 'OPTIONS' === $_SERVER['REQUEST_METHOD'] ) { 528 status_header( 403 ); 529 exit; 530 } 531 532 return false; 533 } 534 535 /** 536 * Validates a URL as safe for use in the HTTP API. 537 * 538 * The only supported protocols are `http` and `https`. 539 * 540 * Examples of URLs that are considered unsafe: 541 * 542 * - `ftp://example.com/caniload.php` - Invalid protocol - only http and https are allowed. 543 * - `http:///example.com/caniload.php` - Malformed URL. 544 * - `http://user:pass@example.com/caniload.php` - Login information. 545 * - `http://example.invalid/caniload.php` - Invalid hostname, as the IP cannot be looked up in DNS. 546 * 547 * Examples of URLs that are considered unsafe by default but can be allowed with filters: 548 * 549 * - `http://192.168.0.1/caniload.php` - IP address from LAN network. 550 * This can be changed with the {@see 'http_request_host_is_external'} filter. 551 * - `http://198.143.164.252:81/caniload.php` - By default, only ports 80, 443, and 8080 are allowed. 552 * This can be changed with the {@see 'http_allowed_safe_ports'} filter. 553 * 554 * @since 3.5.2 555 * 556 * @param string $url Request URL. 557 * @return string|false Returns false if the URL is not safe, or the original URL if it is safe. 558 * 559 * @phpstan-return ( $url is numeric|'' ? false : string|false ) 560 */ 561 function wp_http_validate_url( $url ) { 562 if ( ! is_string( $url ) || '' === $url || is_numeric( $url ) ) { 563 return false; 564 } 565 566 $original_url = $url; 567 $url = wp_kses_bad_protocol( $url, array( 'http', 'https' ) ); 568 if ( ! $url || strtolower( $url ) !== strtolower( $original_url ) ) { 569 return false; 570 } 571 572 $parsed_url = parse_url( $url ); 573 if ( ! $parsed_url || empty( $parsed_url['host'] ) ) { 574 return false; 575 } 576 577 if ( isset( $parsed_url['user'] ) || isset( $parsed_url['pass'] ) ) { 578 return false; 579 } 580 581 if ( false !== strpbrk( $parsed_url['host'], ':#?[]' ) ) { 582 return false; 583 } 584 585 $parsed_home = parse_url( get_option( 'home' ) ); 586 $same_host = isset( $parsed_home['host'] ) && strtolower( $parsed_home['host'] ) === strtolower( $parsed_url['host'] ); 587 $host = trim( $parsed_url['host'], '.' ); 588 589 if ( ! $same_host ) { 590 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 ) ) { 591 $ip = $host; 592 } else { 593 $ip = gethostbyname( $host ); 594 if ( $ip === $host ) { // Error condition for gethostbyname(). 595 return false; 596 } 597 } 598 if ( $ip ) { 599 $parts = array_map( 'intval', explode( '.', $ip ) ); 600 601 /* 602 * These IP address ranges are not considered valid external hosts for HTTP requests. 603 * 604 * If the host resolves to an IP address in these ranges, the request will be rejected unless the 'http_request_host_is_external' filter allows it. 605 * 606 * References: 607 * 608 * - IPv4 Special-Purpose Address Space: https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml 609 * - IPv4 Multicast Address Assignments: https://www.rfc-editor.org/rfc/rfc5771.html 610 */ 611 if ( 127 === $parts[0] || 10 === $parts[0] || 0 === $parts[0] // 127.0.0.0/8 (loopback), 10.0.0.0/8 (private), 0.0.0.0/8 (this network). 612 || ( 172 === $parts[0] && 16 <= $parts[1] && 31 >= $parts[1] ) // 172.16.0.0/12 (private). 613 || ( 192 === $parts[0] && 168 === $parts[1] ) // 192.168.0.0/16 (private). 614 || ( 192 === $parts[0] && 0 === $parts[1] && 0 === $parts[2] ) // 192.0.0.0/24 (IETF protocol assignments). 615 || ( 192 === $parts[0] && 0 === $parts[1] && 2 === $parts[2] ) // 192.0.2.0/24 (TEST-NET-1). 616 || ( 192 === $parts[0] && 88 === $parts[1] && 99 === $parts[2] ) // 192.88.99.0/24 (6to4 relay anycast). 617 || ( 198 === $parts[0] && 51 === $parts[1] && 100 === $parts[2] ) // 198.51.100.0/24 (TEST-NET-2). 618 || ( 203 === $parts[0] && 0 === $parts[1] && 113 === $parts[2] ) // 203.0.113.0/24 (TEST-NET-3). 619 || ( 169 === $parts[0] && 254 === $parts[1] ) // 169.254.0.0/16 (link-local and cloud metadata). 620 || ( 100 === $parts[0] && 64 <= $parts[1] && 127 >= $parts[1] ) // 100.64.0.0/10 (CGNAT). 621 || ( 198 === $parts[0] && 18 <= $parts[1] && 19 >= $parts[1] ) // 198.18.0.0/15 (benchmarking). 622 || ( 224 <= $parts[0] && 239 >= $parts[0] ) // 224.0.0.0/4 (multicast). 623 || 240 <= $parts[0] // 240.0.0.0/4 (reserved, includes 255.255.255.255 broadcast). 624 ) { 625 // If host appears local, reject unless specifically allowed. 626 /** 627 * Checks if HTTP request is external or not. 628 * 629 * Allows to change and allow external requests for the HTTP request. 630 * 631 * @since 3.6.0 632 * 633 * @param bool $external Whether HTTP request is external or not. 634 * @param string $host Host name of the requested URL. 635 * @param string $url Requested URL. 636 */ 637 if ( ! apply_filters( 'http_request_host_is_external', false, $host, $url ) ) { 638 return false; 639 } 640 } 641 } 642 } 643 644 if ( empty( $parsed_url['port'] ) ) { 645 return $url; 646 } 647 648 $port = $parsed_url['port']; 649 650 /** 651 * Controls the list of ports considered safe in HTTP API. 652 * 653 * Allows to change and allow external requests for the HTTP request. 654 * 655 * @since 5.9.0 656 * 657 * @param int[] $allowed_ports Array of integers for valid ports. Default allowed ports 658 * are 80, 443, and 8080. 659 * @param string $host Host name of the requested URL. 660 * @param string $url Requested URL. 661 */ 662 $allowed_ports = apply_filters( 'http_allowed_safe_ports', array( 80, 443, 8080 ), $host, $url ); 663 if ( is_array( $allowed_ports ) && in_array( $port, $allowed_ports, true ) ) { 664 return $url; 665 } 666 667 if ( $parsed_home && $same_host && isset( $parsed_home['port'] ) && $parsed_home['port'] === $port ) { 668 return $url; 669 } 670 671 return false; 672 } 673 674 /** 675 * Marks allowed redirect hosts safe for HTTP requests as well. 676 * 677 * Attached to the {@see 'http_request_host_is_external'} filter. 678 * 679 * @since 3.6.0 680 * 681 * @param bool $is_external 682 * @param string $host 683 * @return bool 684 */ 685 function allowed_http_request_hosts( $is_external, $host ) { 686 if ( ! $is_external && wp_validate_redirect( 'http://' . $host ) ) { 687 $is_external = true; 688 } 689 return $is_external; 690 } 691 692 /** 693 * Adds any domain in a multisite installation for safe HTTP requests to the 694 * allowed list. 695 * 696 * Attached to the {@see 'http_request_host_is_external'} filter. 697 * 698 * @since 3.6.0 699 * 700 * @global wpdb $wpdb WordPress database abstraction object. 701 * 702 * @param bool $is_external 703 * @param string $host 704 * @return bool 705 */ 706 function ms_allowed_http_request_hosts( $is_external, $host ) { 707 global $wpdb; 708 static $queried = array(); 709 if ( $is_external ) { 710 return $is_external; 711 } 712 if ( get_network()->domain === $host ) { 713 return true; 714 } 715 if ( isset( $queried[ $host ] ) ) { 716 return $queried[ $host ]; 717 } 718 $queried[ $host ] = (bool) $wpdb->get_var( $wpdb->prepare( "SELECT domain FROM $wpdb->blogs WHERE domain = %s LIMIT 1", $host ) ); 719 return $queried[ $host ]; 720 } 721 722 /** 723 * A wrapper for PHP's parse_url() function that handles consistency in the return values 724 * across PHP versions. 725 * 726 * Across various PHP versions, schemeless URLs containing a ":" in the query 727 * are being handled inconsistently. This function works around those differences. 728 * 729 * @since 4.4.0 730 * @since 4.7.0 The `$component` parameter was added for parity with PHP's `parse_url()`. 731 * 732 * @link https://www.php.net/manual/en/function.parse-url.php 733 * 734 * @param string $url The URL to parse. 735 * @param int $component The specific component to retrieve. Use one of the PHP 736 * predefined constants to specify which one. 737 * Defaults to -1 (= return all parts as an array). 738 * @return mixed False on parse failure; Array of URL components on success; 739 * When a specific component has been requested: null if the component 740 * doesn't exist in the given URL; a string or - in the case of 741 * PHP_URL_PORT - integer when it does. See parse_url()'s return values. 742 * 743 * @phpstan-param int<-1, 7> $component 744 * @phpstan-return ( 745 * $component is -1 746 * ? false|array{ 747 * scheme?: string, 748 * host?: string, 749 * port?: int<0, 65535>, 750 * user?: string, 751 * pass?: string, 752 * path?: string, 753 * query?: string, 754 * fragment?: string, 755 * } 756 * : ( 757 * $component is 2 758 * ? int<0, 65535>|null 759 * : string|null 760 * ) 761 * ) 762 */ 763 function wp_parse_url( $url, $component = -1 ) { 764 $to_unset = array(); 765 $url = (string) $url; 766 767 if ( str_starts_with( $url, '//' ) ) { 768 $to_unset[] = 'scheme'; 769 $url = 'placeholder:' . $url; 770 } elseif ( str_starts_with( $url, '/' ) ) { 771 $to_unset[] = 'scheme'; 772 $to_unset[] = 'host'; 773 $url = 'placeholder://placeholder' . $url; 774 } 775 776 $parts = parse_url( $url ); 777 778 if ( false === $parts ) { 779 // Parsing failure. 780 return $parts; 781 } 782 783 // Remove the placeholder values. 784 foreach ( $to_unset as $key ) { 785 unset( $parts[ $key ] ); 786 } 787 788 return _get_component_from_parsed_url_array( $parts, $component ); 789 } 790 791 /** 792 * Retrieves a specific component from a parsed URL array. 793 * 794 * @internal 795 * 796 * @since 4.7.0 797 * @access private 798 * 799 * @link https://www.php.net/manual/en/function.parse-url.php 800 * 801 * @param array|false $url_parts The parsed URL. Can be false if the URL failed to parse. 802 * @param int $component The specific component to retrieve. Use one of the PHP 803 * predefined constants to specify which one. 804 * Defaults to -1 (= return all parts as an array). 805 * @return mixed False on parse failure; Array of URL components on success; 806 * When a specific component has been requested: null if the component 807 * doesn't exist in the given URL; a string or - in the case of 808 * PHP_URL_PORT - integer when it does. See parse_url()'s return values. 809 * 810 * @phpstan-param false|array{ 811 * scheme?: string, 812 * host?: string, 813 * port?: int<0, 65535>, 814 * user?: string, 815 * pass?: string, 816 * path?: string, 817 * query?: string, 818 * fragment?: string, 819 * } $url_parts 820 * @phpstan-param int<-1, 7> $component 821 * @phpstan-return ( 822 * $component is -1 823 * ? false|array{ 824 * scheme?: string, 825 * host?: string, 826 * port?: int<0, 65535>, 827 * user?: string, 828 * pass?: string, 829 * path?: string, 830 * query?: string, 831 * fragment?: string, 832 * } 833 * : ( 834 * $component is 2 835 * ? int<0, 65535>|null 836 * : string|null 837 * ) 838 * ) 839 */ 840 function _get_component_from_parsed_url_array( $url_parts, $component = -1 ) { 841 if ( -1 === $component ) { 842 return $url_parts; 843 } 844 845 $key = _wp_translate_php_url_constant_to_key( $component ); 846 if ( false !== $key && is_array( $url_parts ) && isset( $url_parts[ $key ] ) ) { 847 return $url_parts[ $key ]; 848 } else { 849 return null; 850 } 851 } 852 853 /** 854 * Translates a PHP_URL_* constant to the named array keys PHP uses. 855 * 856 * @internal 857 * 858 * @since 4.7.0 859 * @access private 860 * 861 * @link https://www.php.net/manual/en/url.constants.php 862 * 863 * @param int $constant PHP_URL_* constant. 864 * @return string|false The named key or false. 865 * 866 * @phpstan-param int<-1, 7> $constant 867 * @phpstan-return 'scheme'|'host'|'port'|'user'|'pass'|'path'|'query'|'fragment'|false 868 */ 869 function _wp_translate_php_url_constant_to_key( $constant ) { 870 $translation = array( 871 PHP_URL_SCHEME => 'scheme', 872 PHP_URL_HOST => 'host', 873 PHP_URL_PORT => 'port', 874 PHP_URL_USER => 'user', 875 PHP_URL_PASS => 'pass', 876 PHP_URL_PATH => 'path', 877 PHP_URL_QUERY => 'query', 878 PHP_URL_FRAGMENT => 'fragment', 879 ); 880 881 return $translation[ $constant ] ?? false; 882 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sun Oct 4 08:20:33 2026 | Cross-referenced by PHPXref |