Make WordPress Core


Ignore:
Timestamp:
09/30/2016 09:46:43 PM (7 years ago)
Author:
johnbillion
Message:

HTTP API: Add a $component parameter to wp_parse_url() to give it parity with PHP's parse_url() function.

Fixes #36356
Props jrf

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/http.php

    r38456 r38694  
    634634 *
    635635 * @since 4.4.0
    636  *
    637  * @param string $url The URL to parse.
    638  * @return bool|array False on failure; Array of URL components on success;
    639  *                    See parse_url()'s return values.
    640  */
    641 function wp_parse_url( $url ) {
    642     $parts = @parse_url( $url );
    643     if ( ! $parts ) {
    644         // < PHP 5.4.7 compat, trouble with relative paths including a scheme break in the path
     636 * @since 4.7.0 The $component parameter was added for parity with PHP's parse_url().
     637 *
     638 * @param string $url       The URL to parse.
     639 * @param int    $component The specific component to retrieve. Use one of the PHP
     640 *                          predefined constants to specify which one.
     641 *                          Defaults to -1 (= return all parts as an array).
     642 *                          @see http://php.net/manual/en/function.parse-url.php
     643 * @return mixed False on failure; Array of URL components on success;
     644 *               When a specific component has been requested: null if the component doesn't
     645 *               exist in the given URL; a sting or - in the case of PHP_URL_PORT - integer
     646 *               when it does; See parse_url()'s return values.
     647 */
     648function wp_parse_url( $url, $component = -1 ) {
     649    $parts = @parse_url( $url, $component );
     650
     651    if ( version_compare( PHP_VERSION, '5.4.7', '>=' ) ) {
     652        return $parts;
     653    }
     654
     655    if ( false === $parts ) {
     656        // < PHP 5.4.7 compat, trouble with relative paths including a scheme break in the path.
    645657        if ( '/' == $url[0] && false !== strpos( $url, '://' ) ) {
    646             // Since we know it's a relative path, prefix with a scheme/host placeholder and try again
    647             if ( ! $parts = @parse_url( 'placeholder://placeholder' . $url ) ) {
     658            if ( in_array( $component, array( PHP_URL_SCHEME, PHP_URL_HOST ), true ) ) {
     659                return null;
     660            }
     661            // Since we know it's a relative path, prefix with a scheme/host placeholder and try again.
     662            if ( ! $parts = @parse_url( 'placeholder://placeholder' . $url, $component ) ) {
    648663                return $parts;
    649664            }
    650             // Remove the placeholder values
    651             unset( $parts['scheme'], $parts['host'] );
     665            // Remove the placeholder values.
     666            if ( -1 === $component ) {
     667                unset( $parts['scheme'], $parts['host'] );
     668            }
    652669        } else {
    653670            return $parts;
     
    655672    }
    656673
    657     // < PHP 5.4.7 compat, doesn't detect schemeless URL's host field
    658     if ( '//' == substr( $url, 0, 2 ) && ! isset( $parts['host'] ) ) {
    659         $path_parts = explode( '/', substr( $parts['path'], 2 ), 2 );
    660         $parts['host'] = $path_parts[0];
    661         if ( isset( $path_parts[1] ) ) {
    662             $parts['path'] = '/' . $path_parts[1];
    663         } else {
    664             unset( $parts['path'] );
     674    // < PHP 5.4.7 compat, doesn't detect a schemeless URL's host field.
     675    if ( '//' == substr( $url, 0, 2 ) ) {
     676        if ( -1 === $component && ! isset( $parts['host'] ) ) {
     677            $path_parts = explode( '/', substr( $parts['path'], 2 ), 2 );
     678            $parts['host'] = $path_parts[0];
     679            if ( isset( $path_parts[1] ) ) {
     680                $parts['path'] = '/' . $path_parts[1];
     681            } else {
     682                unset( $parts['path'] );
     683            }
     684        } elseif ( PHP_URL_HOST === $component || PHP_URL_PATH === $component ) {
     685            $all_parts = @parse_url( $url );
     686            if ( ! isset( $all_parts['host'] ) ) {
     687                $path_parts = explode( '/', substr( $all_parts['path'], 2 ), 2 );
     688                if ( PHP_URL_PATH === $component ) {
     689                    if ( isset( $path_parts[1] ) ) {
     690                        $parts = '/' . $path_parts[1];
     691                    } else {
     692                        $parts = null;
     693                    }
     694                } elseif ( PHP_URL_HOST === $component ) {
     695                    $parts = $path_parts[0];
     696                }
     697            }
    665698        }
    666699    }
Note: See TracChangeset for help on using the changeset viewer.