[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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


Generated: Tue Oct 22 08:20:01 2019 Cross-referenced by PHPXref 0.7