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
44 changes: 44 additions & 0 deletions src/wp-includes/deprecated.php
Original file line number Diff line number Diff line change
Expand Up @@ -6424,3 +6424,47 @@ function wp_create_block_style_variation_instance_name( $block, $variation ) {
function current_user_can_for_blog( $blog_id, $capability, ...$args ) {
return current_user_can_for_site( $blog_id, $capability, ...$args );
}

/**
* Retrieves a specific component from a parsed URL array.
*
* @internal
*
* @since 4.7.0
* @deprecated 6.8.0
* @access private
*
* @link https://www.php.net/manual/en/function.parse-url.php
*
* @param array|false $url_parts The parsed URL. Can be false if the URL failed to parse.
* @param int $component The specific component to retrieve. Use one of the PHP
* predefined constants to specify which one.
* Defaults to -1 (= return all parts as an array).
* @return mixed False on parse failure; Array of URL components on success;
* When a specific component has been requested: null if the component
* doesn't exist in the given URL; a string or - in the case of
* PHP_URL_PORT - integer when it does. See parse_url()'s return values.
*/
function _get_component_from_parsed_url_array( $url_parts, $component ) {
_deprecated_function( __FUNCTION__, '6.8.0' );
return null;
}

/**
* Translates a PHP_URL_* constant to the named array keys PHP uses.
*
* @internal
*
* @since 4.7.0
* @deprecated 6.8.0
* @access private
*
* @link https://www.php.net/manual/en/url.constants.php
*
* @param int $constant PHP_URL_* constant.
* @return string|false The named key or false.
*/
function _wp_translate_php_url_constant_to_key( $constant ) {
_deprecated_function( __FUNCTION__, '6.8.0' );
return null;
}
121 changes: 42 additions & 79 deletions src/wp-includes/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -710,93 +710,56 @@ 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;

// Remove the placeholder values.
foreach ( $to_unset as $key ) {
unset( $parts[ $key ] );
}
case 'query':
$component = PHP_URL_QUERY;
break;

return _get_component_from_parsed_url_array( $parts, $component );
}
case 'scheme':
$component = PHP_URL_SCHEME;
break;

/**
* Retrieves a specific component from a parsed URL array.
*
* @internal
*
* @since 4.7.0
* @access private
*
* @link https://www.php.net/manual/en/function.parse-url.php
*
* @param array|false $url_parts The parsed URL. Can be false if the URL failed to parse.
* @param int $component The specific component to retrieve. Use one of the PHP
* predefined constants to specify which one.
* Defaults to -1 (= return all parts as an array).
* @return mixed False on parse failure; Array of URL components on success;
* When a specific component has been requested: null if the component
* doesn't exist in the given URL; a string or - in the case of
* PHP_URL_PORT - integer when it does. See parse_url()'s return values.
*/
function _get_component_from_parsed_url_array( $url_parts, $component = -1 ) {
if ( -1 === $component ) {
return $url_parts;
}
case 'host':
$component = PHP_URL_HOST;
break;

$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;
}
}
case 'port':
$component = PHP_URL_PORT;
break;

/**
* Translates a PHP_URL_* constant to the named array keys PHP uses.
*
* @internal
*
* @since 4.7.0
* @access private
*
* @link https://www.php.net/manual/en/url.constants.php
*
* @param int $constant PHP_URL_* constant.
* @return string|false The named key or false.
*/
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',
);
case 'user':
$component = PHP_URL_USER;
break;

if ( isset( $translation[ $constant ] ) ) {
return $translation[ $constant ];
} else {
return false;
case 'pass':
$component = PHP_URL_PASS;
break;

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

default:
return null;
break;
}

return parse_url( (string) $url, $component );
}
49 changes: 11 additions & 38 deletions tests/phpunit/tests/http/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,73 +331,46 @@ public function test_normalize_cookies_scalar_values() {

/**
* @ticket 36356
* @ticket 62124
*
* @dataProvider data_get_component_from_parsed_url_array
*
* @expectedDeprecated _get_component_from_parsed_url_array
*
* @covers ::wp_parse_url
* @covers ::_get_component_from_parsed_url_array
*/
public function test_get_component_from_parsed_url_array( $url, $component, $expected ) {
$parts = wp_parse_url( $url );
$actual = _get_component_from_parsed_url_array( $parts, $component );
$this->assertSame( $expected, $actual );
$this->assertNull( $actual );
}

public function data_get_component_from_parsed_url_array() {
// 0: A URL, 1: PHP URL constant, 2: The expected result.
return array(
array(
'http://example.com/',
-1,
array(
'scheme' => 'http',
'host' => 'example.com',
'path' => '/',
),
),
array(
'http://example.com/',
-1,
array(
'scheme' => 'http',
'host' => 'example.com',
'path' => '/',
),
),
array( 'http://example.com/', PHP_URL_HOST, 'example.com' ),
array( 'http://example.com/', PHP_URL_USER, null ),
array( 'http:///example.com', -1, false ), // Malformed.
array( 'http:///example.com', PHP_URL_HOST, null ), // Malformed.
array( null, null, null ),
);
}

/**
* @ticket 36356
* @ticket 62124
*
* @dataProvider data_wp_translate_php_url_constant_to_key
*
* @covers ::_wp_translate_php_url_constant_to_key
* @expectedDeprecated _wp_translate_php_url_constant_to_key
*
* @coversNothing
*/
public function test_wp_translate_php_url_constant_to_key( $input, $expected ) {
$actual = _wp_translate_php_url_constant_to_key( $input );
$this->assertSame( $expected, $actual );
$this->assertNull( $actual );
}

public function data_wp_translate_php_url_constant_to_key() {
// 0: PHP URL constant, 1: The expected result.
return array(
array( PHP_URL_SCHEME, 'scheme' ),
array( PHP_URL_HOST, 'host' ),
array( PHP_URL_PORT, 'port' ),
array( PHP_URL_USER, 'user' ),
array( PHP_URL_PASS, 'pass' ),
array( PHP_URL_PATH, 'path' ),
array( PHP_URL_QUERY, 'query' ),
array( PHP_URL_FRAGMENT, 'fragment' ),

// Test with non-PHP_URL_CONSTANT parameter.
array( 'something', false ),
array( ABSPATH, false ),
array( null, null ),
);
}

Expand Down
Loading