Completed
Push — master ( e24efa...68396e )
by LA
03:42 queued 01:47
created

compability.php ➔ wp_parse_url()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 17
nc 8
nop 1
dl 0
loc 29
rs 5.3846
c 0
b 0
f 0
1
<?php
2
/**
3
 * Adds compability for old WordPress versions
4
 *
5
 * @package imgix
6
 */
7
8
if ( ! function_exists( 'wp_parse_url' ) ) {
9
	/**
10
	 * A wrapper for PHP's parse_url() function that handles edgecases in < PHP 5.4.7
11
	 *
12
	 * PHP 5.4.7 expanded parse_url()'s ability to handle non-absolute url's, including
13
	 * schemeless and relative url's with :// in the path, this works around those
14
	 * limitations providing a standard output on PHP 5.2~5.4+.
15
	 *
16
	 * Error suppression is used as prior to PHP 5.3.3, an E_WARNING would be generated
17
	 * when URL parsing failed.
18
	 *
19
	 * @since 4.4.0
20
	 *
21
	 * @param string $url The URL to parse.
22
	 *
23
	 * @return bool|array False on failure; Array of URL components on success;
24
	 *                    See parse_url()'s return values.
25
	 */
26
	function wp_parse_url( $url ) {
27
		$parts = @parse_url( $url );
28
		if ( ! $parts ) {
29
			// < PHP 5.4.7 compat, trouble with relative paths including a scheme break in the path
30
			if ( '/' == $url[0] && false !== strpos( $url, '://' ) ) {
31
				// Since we know it's a relative path, prefix with a scheme/host placeholder and try again
32
				if ( ! $parts = @parse_url( 'placeholder://placeholder' . $url ) ) {
33
					return $parts;
34
				}
35
				// Remove the placeholder values
36
				unset( $parts['scheme'], $parts['host'] );
37
			} else {
38
				return $parts;
39
			}
40
		}
41
42
		// < PHP 5.4.7 compat, doesn't detect schemeless URL's host field
43
		if ( '//' == substr( $url, 0, 2 ) && ! isset( $parts['host'] ) ) {
44
			$path_parts    = explode( '/', substr( $parts['path'], 2 ), 2 );
45
			$parts['host'] = $path_parts[0];
46
			if ( isset( $path_parts[1] ) ) {
47
				$parts['path'] = '/' . $path_parts[1];
48
			} else {
49
				unset( $parts['path'] );
50
			}
51
		}
52
53
		return $parts;
54
	}
55
}