Skip to content

Commit

Permalink
Code Modernization: Pass correct value to parse_url() in `WP_Custom…
Browse files Browse the repository at this point in the history
…ize_Manager::get_return_url()`.

This particular code block only makes sense to run when `$this->return_url` is not null. Previously, it caused a "passing null to non-nullable" deprecation notice on PHP 8.1.

By moving the code into the `if ( $this->return_url )` condition block, the code will only be run when `$this->return_url` contains a non-falsey/non-null value.

No additional tests added as this issue was found via the existing tests for the function containing the bug.

This solves the following two PHP 8.1 test errors:
{{{
1) Tests_WP_Customize_Manager::test_return_url
parse_url(): Passing null to parameter #1 ($url) of type string is deprecated

/var/www/src/wp-includes/class-wp-customize-manager.php:4696
/var/www/tests/phpunit/tests/customize/manager.php:2975
/var/www/vendor/bin/phpunit:123

2) Tests_WP_Customize_Manager::test_customize_pane_settings
parse_url(): Passing null to parameter #1 ($url) of type string is deprecated

/var/www/src/wp-includes/class-wp-customize-manager.php:4696
/var/www/src/wp-includes/class-wp-customize-manager.php:4898
/var/www/tests/phpunit/tests/customize/manager.php:3085
/var/www/vendor/bin/phpunit:123
}}}

Follow-up to [46754].

Props jrf, costdev.
See #55656.

git-svn-id: https://develop.svn.wordpress.org/trunk@54135 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Sep 12, 2022
1 parent 2d98ba4 commit 4fc5f0e
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/wp-includes/class-wp-customize-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4686,6 +4686,21 @@ public function get_return_url() {

if ( $this->return_url ) {
$return_url = $this->return_url;

$return_url_basename = wp_basename( parse_url( $this->return_url, PHP_URL_PATH ) );
$return_url_query = parse_url( $this->return_url, PHP_URL_QUERY );

if ( 'themes.php' === $return_url_basename && $return_url_query ) {
parse_str( $return_url_query, $query_vars );

/*
* If the return URL is a page added by a theme to the Appearance menu via add_submenu_page(),
* verify that it belongs to the active theme, otherwise fall back to the Themes screen.
*/
if ( isset( $query_vars['page'] ) && ! isset( $_registered_pages[ "appearance_page_{$query_vars['page']}" ] ) ) {
$return_url = admin_url( 'themes.php' );
}
}
} elseif ( $referer && ! in_array( wp_basename( parse_url( $referer, PHP_URL_PATH ) ), $excluded_referer_basenames, true ) ) {
$return_url = $referer;
} elseif ( $this->preview_url ) {
Expand All @@ -4694,21 +4709,6 @@ public function get_return_url() {
$return_url = home_url( '/' );
}

$return_url_basename = wp_basename( parse_url( $this->return_url, PHP_URL_PATH ) );
$return_url_query = parse_url( $this->return_url, PHP_URL_QUERY );

if ( 'themes.php' === $return_url_basename && $return_url_query ) {
parse_str( $return_url_query, $query_vars );

/*
* If the return URL is a page added by a theme to the Appearance menu via add_submenu_page(),
* verify that it belongs to the active theme, otherwise fall back to the Themes screen.
*/
if ( isset( $query_vars['page'] ) && ! isset( $_registered_pages[ "appearance_page_{$query_vars['page']}" ] ) ) {
$return_url = admin_url( 'themes.php' );
}
}

return $return_url;
}

Expand Down

0 comments on commit 4fc5f0e

Please sign in to comment.