[ 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 * @since 3.6.0 37 * 38 * @see wp_remote_request() For more information on the response array format. 39 * @see WP_Http::request() For default arguments information. 40 * @see wp_http_validate_url() For more information about how the URL is validated. 41 * 42 * @link https://owasp.org/www-community/attacks/Server_Side_Request_Forgery 43 * 44 * @param string $url URL to retrieve. 45 * @param array $args Optional. Request arguments. Default empty array. 46 * See WP_Http::request() for information on accepted arguments. 47 * @return array|WP_Error The response or WP_Error on failure. 48 */ 49 function wp_safe_remote_request( $url, $args = array() ) { 50 $args['reject_unsafe_urls'] = true; 51 $http = _wp_http_get_object(); 52 return $http->request( $url, $args ); 53 } 54 55 /** 56 * Retrieves the raw response from a safe HTTP request using the GET method. 57 * 58 * This function is ideal when the HTTP request is being made to an arbitrary 59 * URL. The URL, and every URL it redirects to, are validated with wp_http_validate_url() 60 * to avoid Server Side Request Forgery attacks (SSRF). 61 * 62 * @since 3.6.0 63 * 64 * @see wp_remote_request() For more information on the response array format. 65 * @see WP_Http::request() For default arguments information. 66 * @see wp_http_validate_url() For more information about how the URL is validated. 67 * 68 * @link https://owasp.org/www-community/attacks/Server_Side_Request_Forgery 69 * 70 * @param string $url URL to retrieve. 71 * @param array $args Optional. Request arguments. Default empty array. 72 * See WP_Http::request() for information on accepted arguments. 73 * @return array|WP_Error The response or WP_Error on failure. 74 */ 75 function wp_safe_remote_get( $url, $args = array() ) { 76 $args['reject_unsafe_urls'] = true; 77 $http = _wp_http_get_object(); 78 return $http->get( $url, $args ); 79 } 80 81 /** 82 * Retrieves the raw response from a safe HTTP request using the POST method. 83 * 84 * This function is ideal when the HTTP request is being made to an arbitrary 85 * URL. The URL, and every URL it redirects to, are validated with wp_http_validate_url() 86 * to avoid Server Side Request Forgery attacks (SSRF). 87 * 88 * @since 3.6.0 89 * 90 * @see wp_remote_request() For more information on the response array format. 91 * @see WP_Http::request() For default arguments information. 92 * @see wp_http_validate_url() For more information about how the URL is validated. 93 * 94 * @link https://owasp.org/www-community/attacks/Server_Side_Request_Forgery 95 * 96 * @param string $url URL to retrieve. 97 * @param array $args Optional. Request arguments. Default empty array. 98 * See WP_Http::request() for information on accepted arguments. 99 * @return array|WP_Error The response or WP_Error on failure. 100 */ 101 function wp_safe_remote_post( $url, $args = array() ) { 102 $args['reject_unsafe_urls'] = true; 103 $http = _wp_http_get_object(); 104 return $http->post( $url, $args ); 105 } 106 107 /** 108 * Retrieves the raw response from a safe HTTP request using the HEAD method. 109 * 110 * This function is ideal when the HTTP request is being made to an arbitrary 111 * URL. The URL, and every URL it redirects to, are validated with wp_http_validate_url() 112 * to avoid Server Side Request Forgery attacks (SSRF). 113 * 114 * @since 3.6.0 115 * 116 * @see wp_remote_request() For more information on the response array format. 117 * @see WP_Http::request() For default arguments information. 118 * @see wp_http_validate_url() For more information about how the URL is validated. 119 * 120 * @link https://owasp.org/www-community/attacks/Server_Side_Request_Forgery 121 * 122 * @param string $url URL to retrieve. 123 * @param array $args Optional. Request arguments. Default empty array. 124 * See WP_Http::request() for information on accepted arguments. 125 * @return array|WP_Error The response or WP_Error on failure. 126 */ 127 function wp_safe_remote_head( $url, $args = array() ) { 128 $args['reject_unsafe_urls'] = true; 129 $http = _wp_http_get_object(); 130 return $http->head( $url, $args ); 131 } 132 133 /** 134 * Performs an HTTP request and returns its response. 135 * 136 * There are other API functions available which abstract away the HTTP method: 137 * 138 * - Default 'GET' for wp_remote_get() 139 * - Default 'POST' for wp_remote_post() 140 * - Default 'HEAD' for wp_remote_head() 141 * 142 * @since 2.7.0 143 * 144 * @see WP_Http::request() For information on default arguments. 145 * 146 * @param string $url URL to retrieve. 147 * @param array $args Optional. Request arguments. Default empty array. 148 * See WP_Http::request() for information on accepted arguments. 149 * @return array|WP_Error { 150 * The response array or a WP_Error on failure. 151 * 152 * @type string[] $headers Array of response headers keyed by their name. 153 * @type string $body Response body. 154 * @type array $response { 155 * Data about the HTTP response. 156 * 157 * @type int|false $code HTTP response code. 158 * @type string|false $message HTTP response message. 159 * } 160 * @type WP_HTTP_Cookie[] $cookies Array of response cookies. 161 * @type WP_HTTP_Requests_Response|null $http_response Raw HTTP response object. 162 * } 163 */ 164 function wp_remote_request( $url, $args = array() ) { 165 $http = _wp_http_get_object(); 166 return $http->request( $url, $args ); 167 } 168 169 /** 170 * Performs an HTTP request using the GET method and returns its response. 171 * 172 * @since 2.7.0 173 * 174 * @see wp_remote_request() For more information on the response array format. 175 * @see WP_Http::request() For default arguments information. 176 * 177 * @param string $url URL to retrieve. 178 * @param array $args Optional. Request arguments. Default empty array. 179 * See WP_Http::request() for information on accepted arguments. 180 * @return array|WP_Error The response or WP_Error on failure. 181 */ 182 function wp_remote_get( $url, $args = array() ) { 183 $http = _wp_http_get_object(); 184 return $http->get( $url, $args ); 185 } 186 187 /** 188 * Performs an HTTP request using the POST method and returns its response. 189 * 190 * @since 2.7.0 191 * 192 * @see wp_remote_request() For more information on the response array format. 193 * @see WP_Http::request() For default arguments information. 194 * 195 * @param string $url URL to retrieve. 196 * @param array $args Optional. Request arguments. Default empty array. 197 * See WP_Http::request() for information on accepted arguments. 198 * @return array|WP_Error The response or WP_Error on failure. 199 */ 200 function wp_remote_post( $url, $args = array() ) { 201 $http = _wp_http_get_object(); 202 return $http->post( $url, $args ); 203 } 204 205 /** 206 * Performs an HTTP request using the HEAD method and returns its response. 207 * 208 * @since 2.7.0 209 * 210 * @see wp_remote_request() For more information on the response array format. 211 * @see WP_Http::request() For default arguments information. 212 * 213 * @param string $url URL to retrieve. 214 * @param array $args Optional. Request arguments. Default empty array. 215 * See WP_Http::request() for information on accepted arguments. 216 * @return array|WP_Error The response or WP_Error on failure. 217 */ 218 function wp_remote_head( $url, $args = array() ) { 219 $http = _wp_http_get_object(); 220 return $http->head( $url, $args ); 221 } 222 223 /** 224 * Retrieves only the headers from the raw response. 225 * 226 * @since 2.7.0 227 * @since 4.6.0 Return value changed from an array to an WpOrg\Requests\Utility\CaseInsensitiveDictionary instance. 228 * 229 * @see \WpOrg\Requests\Utility\CaseInsensitiveDictionary 230 * 231 * @param array|WP_Error $response HTTP response. 232 * @return \WpOrg\Requests\Utility\CaseInsensitiveDictionary|array The headers of the response, or empty array 233 * if incorrect parameter given. 234 */ 235 function wp_remote_retrieve_headers( $response ) { 236 if ( is_wp_error( $response ) || ! isset( $response['headers'] ) ) { 237 return array(); 238 } 239 240 return $response['headers']; 241 } 242 243 /** 244 * Retrieves a single header by name from the raw response. 245 * 246 * @since 2.7.0 247 * 248 * @param array|WP_Error $response HTTP response. 249 * @param string $header Header name to retrieve value from. 250 * @return array|string The header(s) value(s). Array if multiple headers with the same name are retrieved. 251 * Empty string if incorrect parameter given, or if the header doesn't exist. 252 */ 253 function wp_remote_retrieve_header( $response, $header ) { 254 if ( is_wp_error( $response ) || ! isset( $response['headers'] ) ) { 255 return ''; 256 } 257 258 if ( isset( $response['headers'][ $header ] ) ) { 259 return $response['headers'][ $header ]; 260 } 261 262 return ''; 263 } 264 265 /** 266 * Retrieves only the response code from the raw response. 267 * 268 * Will return an empty string if incorrect parameter value is given. 269 * 270 * @since 2.7.0 271 * 272 * @param array|WP_Error $response HTTP response. 273 * @return int|string The response code as an integer. Empty string if incorrect parameter given. 274 */ 275 function wp_remote_retrieve_response_code( $response ) { 276 if ( is_wp_error( $response ) || ! isset( $response['response'] ) || ! is_array( $response['response'] ) ) { 277 return ''; 278 } 279 280 return $response['response']['code']; 281 } 282 283 /** 284 * Retrieves only the response message from the raw response. 285 * 286 * Will return an empty string if incorrect parameter value is given. 287 * 288 * @since 2.7.0 289 * 290 * @param array|WP_Error $response HTTP response. 291 * @return string The response message. Empty string if incorrect parameter given. 292 */ 293 function wp_remote_retrieve_response_message( $response ) { 294 if ( is_wp_error( $response ) || ! isset( $response['response'] ) || ! is_array( $response['response'] ) ) { 295 return ''; 296 } 297 298 return $response['response']['message']; 299 } 300 301 /** 302 * Retrieves only the body from the raw response. 303 * 304 * @since 2.7.0 305 * 306 * @param array|WP_Error $response HTTP response. 307 * @return string The body of the response. Empty string if no body or incorrect parameter given. 308 */ 309 function wp_remote_retrieve_body( $response ) { 310 if ( is_wp_error( $response ) || ! isset( $response['body'] ) ) { 311 return ''; 312 } 313 314 return $response['body']; 315 } 316 317 /** 318 * Retrieves only the cookies from the raw response. 319 * 320 * @since 4.4.0 321 * 322 * @param array|WP_Error $response HTTP response. 323 * @return WP_Http_Cookie[] An array of `WP_Http_Cookie` objects from the response. 324 * Empty array if there are none, or the response is a WP_Error. 325 */ 326 function wp_remote_retrieve_cookies( $response ) { 327 if ( is_wp_error( $response ) || empty( $response['cookies'] ) ) { 328 return array(); 329 } 330 331 return $response['cookies']; 332 } 333 334 /** 335 * Retrieves a single cookie by name from the raw response. 336 * 337 * @since 4.4.0 338 * 339 * @param array|WP_Error $response HTTP response. 340 * @param string $name The name of the cookie to retrieve. 341 * @return WP_Http_Cookie|string The `WP_Http_Cookie` object, or empty string 342 * if the cookie is not present in the response. 343 */ 344 function wp_remote_retrieve_cookie( $response, $name ) { 345 $cookies = wp_remote_retrieve_cookies( $response ); 346 347 if ( empty( $cookies ) ) { 348 return ''; 349 } 350 351 foreach ( $cookies as $cookie ) { 352 if ( $cookie->name === $name ) { 353 return $cookie; 354 } 355 } 356 357 return ''; 358 } 359 360 /** 361 * Retrieves a single cookie's value by name from the raw response. 362 * 363 * @since 4.4.0 364 * 365 * @param array|WP_Error $response HTTP response. 366 * @param string $name The name of the cookie to retrieve. 367 * @return string The value of the cookie, or empty string 368 * if the cookie is not present in the response. 369 */ 370 function wp_remote_retrieve_cookie_value( $response, $name ) { 371 $cookie = wp_remote_retrieve_cookie( $response, $name ); 372 373 if ( ! ( $cookie instanceof WP_Http_Cookie ) ) { 374 return ''; 375 } 376 377 return $cookie->value; 378 } 379 380 /** 381 * Determines if there is an HTTP Transport that can process this request. 382 * 383 * @since 3.2.0 384 * 385 * @param array $capabilities Array of capabilities to test or a wp_remote_request() $args array. 386 * @param string $url Optional. If given, will check if the URL requires SSL and adds 387 * that requirement to the capabilities array. 388 * 389 * @return bool 390 */ 391 function wp_http_supports( $capabilities = array(), $url = null ) { 392 $capabilities = wp_parse_args( $capabilities ); 393 394 $count = count( $capabilities ); 395 396 // If we have a numeric $capabilities array, spoof a wp_remote_request() associative $args array. 397 if ( $count && count( array_filter( array_keys( $capabilities ), 'is_numeric' ) ) === $count ) { 398 $capabilities = array_combine( array_values( $capabilities ), array_fill( 0, $count, true ) ); 399 } 400 401 if ( $url && ! isset( $capabilities['ssl'] ) ) { 402 $scheme = parse_url( $url, PHP_URL_SCHEME ); 403 if ( 'https' === $scheme || 'ssl' === $scheme ) { 404 $capabilities['ssl'] = true; 405 } 406 } 407 408 return WpOrg\Requests\Requests::has_capabilities( $capabilities ); 409 } 410 411 /** 412 * Gets the HTTP Origin of the current request. 413 * 414 * @since 3.4.0 415 * 416 * @return string URL of the origin. Empty string if no origin. 417 */ 418 function get_http_origin() { 419 $origin = ''; 420 if ( ! empty( $_SERVER['HTTP_ORIGIN'] ) ) { 421 $origin = $_SERVER['HTTP_ORIGIN']; 422 } 423 424 /** 425 * Changes the origin of an HTTP request. 426 * 427 * @since 3.4.0 428 * 429 * @param string $origin The original origin for the request. 430 */ 431 return apply_filters( 'http_origin', $origin ); 432 } 433 434 /** 435 * Retrieves list of allowed HTTP origins. 436 * 437 * @since 3.4.0 438 * 439 * @return string[] Array of origin URLs. 440 */ 441 function get_allowed_http_origins() { 442 $admin_origin = parse_url( admin_url() ); 443 $home_origin = parse_url( home_url() ); 444 445 // @todo Preserve port? 446 $allowed_origins = array_unique( 447 array( 448 'http://' . $admin_origin['host'], 449 'https://' . $admin_origin['host'], 450 'http://' . $home_origin['host'], 451 'https://' . $home_origin['host'], 452 ) 453 ); 454 455 /** 456 * Changes the origin types allowed for HTTP requests. 457 * 458 * @since 3.4.0 459 * 460 * @param string[] $allowed_origins { 461 * Array of default allowed HTTP origins. 462 * 463 * @type string $0 Non-secure URL for admin origin. 464 * @type string $1 Secure URL for admin origin. 465 * @type string $2 Non-secure URL for home origin. 466 * @type string $3 Secure URL for home origin. 467 * } 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 for safe use in the HTTP API. 537 * 538 * Examples of URLs that are considered unsafe: 539 * 540 * - ftp://example.com/caniload.php - Invalid protocol - only http and https are allowed. 541 * - http:///example.com/caniload.php - Malformed URL. 542 * - http://user:pass@example.com/caniload.php - Login information. 543 * - http://exampleeeee.com/caniload.php - Invalid hostname, as the IP cannot be looked up in DNS. 544 * 545 * Examples of URLs that are considered unsafe by default: 546 * 547 * - http://192.168.0.1/caniload.php - IPs from LAN networks. 548 * This can be changed with the {@see 'http_request_host_is_external'} filter. 549 * - http://198.143.164.252:81/caniload.php - By default, only 80, 443, and 8080 ports are allowed. 550 * This can be changed with the {@see 'http_allowed_safe_ports'} filter. 551 * 552 * @since 3.5.2 553 * 554 * @param string $url Request URL. 555 * @return string|false URL or false on failure. 556 */ 557 function wp_http_validate_url( $url ) { 558 if ( ! is_string( $url ) || '' === $url || is_numeric( $url ) ) { 559 return false; 560 } 561 562 $original_url = $url; 563 $url = wp_kses_bad_protocol( $url, array( 'http', 'https' ) ); 564 if ( ! $url || strtolower( $url ) !== strtolower( $original_url ) ) { 565 return false; 566 } 567 568 $parsed_url = parse_url( $url ); 569 if ( ! $parsed_url || empty( $parsed_url['host'] ) ) { 570 return false; 571 } 572 573 if ( isset( $parsed_url['user'] ) || isset( $parsed_url['pass'] ) ) { 574 return false; 575 } 576 577 if ( false !== strpbrk( $parsed_url['host'], ':#?[]' ) ) { 578 return false; 579 } 580 581 $parsed_home = parse_url( get_option( 'home' ) ); 582 $same_host = isset( $parsed_home['host'] ) && strtolower( $parsed_home['host'] ) === strtolower( $parsed_url['host'] ); 583 $host = trim( $parsed_url['host'], '.' ); 584 585 if ( ! $same_host ) { 586 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 ) ) { 587 $ip = $host; 588 } else { 589 $ip = gethostbyname( $host ); 590 if ( $ip === $host ) { // Error condition for gethostbyname(). 591 return false; 592 } 593 } 594 if ( $ip ) { 595 $parts = array_map( 'intval', explode( '.', $ip ) ); 596 if ( 127 === $parts[0] || 10 === $parts[0] || 0 === $parts[0] 597 || ( 172 === $parts[0] && 16 <= $parts[1] && 31 >= $parts[1] ) 598 || ( 192 === $parts[0] && 168 === $parts[1] ) 599 ) { 600 // If host appears local, reject unless specifically allowed. 601 /** 602 * Checks if HTTP request is external or not. 603 * 604 * Allows to change and allow external requests for the HTTP request. 605 * 606 * @since 3.6.0 607 * 608 * @param bool $external Whether HTTP request is external or not. 609 * @param string $host Host name of the requested URL. 610 * @param string $url Requested URL. 611 */ 612 if ( ! apply_filters( 'http_request_host_is_external', false, $host, $url ) ) { 613 return false; 614 } 615 } 616 } 617 } 618 619 if ( empty( $parsed_url['port'] ) ) { 620 return $url; 621 } 622 623 $port = $parsed_url['port']; 624 625 /** 626 * Controls the list of ports considered safe in HTTP API. 627 * 628 * Allows to change and allow external requests for the HTTP request. 629 * 630 * @since 5.9.0 631 * 632 * @param int[] $allowed_ports Array of integers for valid ports. 633 * @param string $host Host name of the requested URL. 634 * @param string $url Requested URL. 635 */ 636 $allowed_ports = apply_filters( 'http_allowed_safe_ports', array( 80, 443, 8080 ), $host, $url ); 637 if ( is_array( $allowed_ports ) && in_array( $port, $allowed_ports, true ) ) { 638 return $url; 639 } 640 641 if ( $parsed_home && $same_host && isset( $parsed_home['port'] ) && $parsed_home['port'] === $port ) { 642 return $url; 643 } 644 645 return false; 646 } 647 648 /** 649 * Marks allowed redirect hosts safe for HTTP requests as well. 650 * 651 * Attached to the {@see 'http_request_host_is_external'} filter. 652 * 653 * @since 3.6.0 654 * 655 * @param bool $is_external 656 * @param string $host 657 * @return bool 658 */ 659 function allowed_http_request_hosts( $is_external, $host ) { 660 if ( ! $is_external && wp_validate_redirect( 'http://' . $host ) ) { 661 $is_external = true; 662 } 663 return $is_external; 664 } 665 666 /** 667 * Adds any domain in a multisite installation for safe HTTP requests to the 668 * allowed list. 669 * 670 * Attached to the {@see 'http_request_host_is_external'} filter. 671 * 672 * @since 3.6.0 673 * 674 * @global wpdb $wpdb WordPress database abstraction object. 675 * 676 * @param bool $is_external 677 * @param string $host 678 * @return bool 679 */ 680 function ms_allowed_http_request_hosts( $is_external, $host ) { 681 global $wpdb; 682 static $queried = array(); 683 if ( $is_external ) { 684 return $is_external; 685 } 686 if ( get_network()->domain === $host ) { 687 return true; 688 } 689 if ( isset( $queried[ $host ] ) ) { 690 return $queried[ $host ]; 691 } 692 $queried[ $host ] = (bool) $wpdb->get_var( $wpdb->prepare( "SELECT domain FROM $wpdb->blogs WHERE domain = %s LIMIT 1", $host ) ); 693 return $queried[ $host ]; 694 } 695 696 /** 697 * A wrapper for PHP's parse_url() function that handles consistency in the return values 698 * across PHP versions. 699 * 700 * Across various PHP versions, schemeless URLs containing a ":" in the query 701 * are being handled inconsistently. This function works around those differences. 702 * 703 * @since 4.4.0 704 * @since 4.7.0 The `$component` parameter was added for parity with PHP's `parse_url()`. 705 * 706 * @link https://www.php.net/manual/en/function.parse-url.php 707 * 708 * @param string $url The URL to parse. 709 * @param int $component The specific component to retrieve. Use one of the PHP 710 * predefined constants to specify which one. 711 * Defaults to -1 (= return all parts as an array). 712 * @return mixed False on parse failure; Array of URL components on success; 713 * When a specific component has been requested: null if the component 714 * doesn't exist in the given URL; a string or - in the case of 715 * PHP_URL_PORT - integer when it does. See parse_url()'s return values. 716 */ 717 function wp_parse_url( $url, $component = -1 ) { 718 $to_unset = array(); 719 $url = (string) $url; 720 721 if ( str_starts_with( $url, '//' ) ) { 722 $to_unset[] = 'scheme'; 723 $url = 'placeholder:' . $url; 724 } elseif ( str_starts_with( $url, '/' ) ) { 725 $to_unset[] = 'scheme'; 726 $to_unset[] = 'host'; 727 $url = 'placeholder://placeholder' . $url; 728 } 729 730 $parts = parse_url( $url ); 731 732 if ( false === $parts ) { 733 // Parsing failure. 734 return $parts; 735 } 736 737 // Remove the placeholder values. 738 foreach ( $to_unset as $key ) { 739 unset( $parts[ $key ] ); 740 } 741 742 return _get_component_from_parsed_url_array( $parts, $component ); 743 } 744 745 /** 746 * Retrieves a specific component from a parsed URL array. 747 * 748 * @internal 749 * 750 * @since 4.7.0 751 * @access private 752 * 753 * @link https://www.php.net/manual/en/function.parse-url.php 754 * 755 * @param array|false $url_parts The parsed URL. Can be false if the URL failed to parse. 756 * @param int $component The specific component to retrieve. Use one of the PHP 757 * predefined constants to specify which one. 758 * Defaults to -1 (= return all parts as an array). 759 * @return mixed False on parse failure; Array of URL components on success; 760 * When a specific component has been requested: null if the component 761 * doesn't exist in the given URL; a string or - in the case of 762 * PHP_URL_PORT - integer when it does. See parse_url()'s return values. 763 */ 764 function _get_component_from_parsed_url_array( $url_parts, $component = -1 ) { 765 if ( -1 === $component ) { 766 return $url_parts; 767 } 768 769 $key = _wp_translate_php_url_constant_to_key( $component ); 770 if ( false !== $key && is_array( $url_parts ) && isset( $url_parts[ $key ] ) ) { 771 return $url_parts[ $key ]; 772 } else { 773 return null; 774 } 775 } 776 777 /** 778 * Translates a PHP_URL_* constant to the named array keys PHP uses. 779 * 780 * @internal 781 * 782 * @since 4.7.0 783 * @access private 784 * 785 * @link https://www.php.net/manual/en/url.constants.php 786 * 787 * @param int $constant PHP_URL_* constant. 788 * @return string|false The named key or false. 789 */ 790 function _wp_translate_php_url_constant_to_key( $constant ) { 791 $translation = array( 792 PHP_URL_SCHEME => 'scheme', 793 PHP_URL_HOST => 'host', 794 PHP_URL_PORT => 'port', 795 PHP_URL_USER => 'user', 796 PHP_URL_PASS => 'pass', 797 PHP_URL_PATH => 'path', 798 PHP_URL_QUERY => 'query', 799 PHP_URL_FRAGMENT => 'fragment', 800 ); 801 802 if ( isset( $translation[ $constant ] ) ) { 803 return $translation[ $constant ]; 804 } else { 805 return false; 806 } 807 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Sat Sep 7 08:20:01 2024 | Cross-referenced by PHPXref |