[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> http.php (source)

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


Generated: Tue Apr 23 01:00:02 2024 Cross-referenced by PHPXref 0.7.1