Created
November 28, 2017 20:41
-
-
Save JRGould/65b482128610a295e56509b9bc5867c3 to your computer and use it in GitHub Desktop.
Place this file in wp-content/mu-plugins/ to patch missing wp_parse_url() function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if( ! function_exists( 'wp_parse_url' ) ) { | |
function wp_parse_url( $url, $component = -1 ) { | |
$to_unset = array(); | |
$url = strval( $url ); | |
if ( '//' === substr( $url, 0, 2 ) ) { | |
$to_unset[] = 'scheme'; | |
$url = 'placeholder:' . $url; | |
} elseif ( '/' === substr( $url, 0, 1 ) ) { | |
$to_unset[] = 'scheme'; | |
$to_unset[] = 'host'; | |
$url = 'placeholder://placeholder' . $url; | |
} | |
$parts = @parse_url( $url ); | |
if ( false === $parts ) { | |
// Parsing failure. | |
return $parts; | |
} | |
// Remove the placeholder values. | |
foreach ( $to_unset as $key ) { | |
unset( $parts[ $key ] ); | |
} | |
return _get_component_from_parsed_url_array( $parts, $component ); | |
} | |
if( ! function_exists( '_get_component_from_parsed_url_array' ) ) { | |
function _get_component_from_parsed_url_array( $url_parts, $component = -1 ) { | |
if ( -1 === $component ) { | |
return $url_parts; | |
} | |
$key = _wp_translate_php_url_constant_to_key( $component ); | |
if ( false !== $key && is_array( $url_parts ) && isset( $url_parts[ $key ] ) ) { | |
return $url_parts[ $key ]; | |
} else { | |
return null; | |
} | |
} | |
} | |
if( ! function_exists( ' _wp_translate_php_url_constant_to_key' ) ) { | |
function _wp_translate_php_url_constant_to_key( $constant ) { | |
$translation = array( | |
PHP_URL_SCHEME => 'scheme', | |
PHP_URL_HOST => 'host', | |
PHP_URL_PORT => 'port', | |
PHP_URL_USER => 'user', | |
PHP_URL_PASS => 'pass', | |
PHP_URL_PATH => 'path', | |
PHP_URL_QUERY => 'query', | |
PHP_URL_FRAGMENT => 'fragment', | |
); | |
if ( isset( $translation[ $constant ] ) ) { | |
return $translation[ $constant ]; | |
} else { | |
return false; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment