Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update wp_parse_url() for performance and legacy code removal #7743

Open
wants to merge 13 commits into
base: trunk
Choose a base branch
from
65 changes: 46 additions & 19 deletions src/wp-includes/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -710,31 +710,58 @@ function ms_allowed_http_request_hosts( $is_external, $host ) {
* PHP_URL_PORT - integer when it does. See parse_url()'s return values.
*/
function wp_parse_url( $url, $component = -1 ) {
$to_unset = array();
$url = (string) $url;

if ( str_starts_with( $url, '//' ) ) {
$to_unset[] = 'scheme';
$url = 'placeholder:' . $url;
} elseif ( str_starts_with( $url, '/' ) ) {
$to_unset[] = 'scheme';
$to_unset[] = 'host';
$url = 'placeholder://placeholder' . $url;
}
switch ( $component ) {

$parts = parse_url( $url );
case -1:
case PHP_URL_PATH:
case PHP_URL_QUERY:
case PHP_URL_SCHEME:
case PHP_URL_HOST:
case PHP_URL_PORT:
case PHP_URL_USER:
case PHP_URL_PASS:
case PHP_URL_FRAGMENT:
break;

if ( false === $parts ) {
// Parsing failure.
return $parts;
}
case 'path':
$component = PHP_URL_PATH;
break;

case 'query':
$component = PHP_URL_QUERY;
break;

case 'scheme':
$component = PHP_URL_SCHEME;
break;

case 'host':
$component = PHP_URL_HOST;
break;

case 'port':
$component = PHP_URL_PORT;
break;

case 'user':
$component = PHP_URL_USER;
break;

case 'pass':
$component = PHP_URL_PASS;
break;

case 'fragment':
$component = PHP_URL_FRAGMENT;
break;

// Remove the placeholder values.
foreach ( $to_unset as $key ) {
unset( $parts[ $key ] );
default:
return null;
break;
}

return _get_component_from_parsed_url_array( $parts, $component );
return parse_url( (string) $url, $component );
}

/**
Expand Down
Loading