Make WordPress Core


Ignore:
Timestamp:
10/23/2015 05:53:05 AM (9 years ago)
Author:
dd32
Message:

WP_HTTP: Promote the WP_HTTP::parse_url() method to a more generic wp_parse_url() function.

Fixes #34408

File:
1 edited

Legend:

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

    r35170 r35369  
    613613    return $queried[ $host ];
    614614}
     615
     616/**
     617 * A wrapper for PHP's parse_url() function that handles edgecases in < PHP 5.4.7
     618 *
     619 * PHP 5.4.7 expanded parse_url()'s ability to handle non-absolute url's, including
     620 * schemeless and relative url's with :// in the path, this works around those
     621 * limitations providing a standard output on PHP 5.2~5.4+.
     622 *
     623 * Error suppression is used as prior to PHP 5.3.3, an E_WARNING would be generated
     624 * when URL parsing failed.
     625 *
     626 * @since 4.4.0
     627 *
     628 * @param string $url The URL to parse.
     629 * @return bool|array False on failure; Array of URL components on success;
     630 *                    See parse_url()'s return values.
     631 */
     632function wp_parse_url( $url ) {
     633    $parts = @parse_url( $url );
     634    if ( ! $parts ) {
     635        // < PHP 5.4.7 compat, trouble with relative paths including a scheme break in the path
     636        if ( '/' == $url[0] && false !== strpos( $url, '://' ) ) {
     637            // Since we know it's a relative path, prefix with a scheme/host placeholder and try again
     638            if ( ! $parts = @parse_url( 'placeholder://placeholder' . $url ) ) {
     639                return $parts;
     640            }
     641            // Remove the placeholder values
     642            unset( $parts['scheme'], $parts['host'] );
     643        } else {
     644            return $parts;
     645        }
     646    }
     647
     648    // < PHP 5.4.7 compat, doesn't detect schemeless URL's host field
     649    if ( '//' == substr( $url, 0, 2 ) && ! isset( $parts['host'] ) ) {
     650        $path_parts = explode( '/', substr( $parts['path'], 2 ), 2 );
     651        $parts['host'] = $path_parts[0];
     652        if ( isset( $path_parts[1] ) ) {
     653            $parts['path'] = '/' . $path_parts[1];
     654        } else {
     655            unset( $parts['path'] );
     656        }
     657    }
     658
     659    return $parts;
     660}
Note: See TracChangeset for help on using the changeset viewer.