From 3b54ab37edece2287495461afceff4d0960642a4 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 20 Nov 2019 18:47:08 +0000 Subject: [PATCH] Customize: If the return URL for Close button is a page added to the Appearance menu by a deactivated theme, fall back to the Themes screen. Props JarretC, Jean-David, SergeyBiryukov. Fixes #46686. git-svn-id: https://develop.svn.wordpress.org/trunk@46754 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-customize-manager.php | 20 +++++++++++++++++++ tests/phpunit/tests/customize/manager.php | 8 ++++++++ 2 files changed, 28 insertions(+) diff --git a/src/wp-includes/class-wp-customize-manager.php b/src/wp-includes/class-wp-customize-manager.php index 17a6fa5767..decf0b87cf 100644 --- a/src/wp-includes/class-wp-customize-manager.php +++ b/src/wp-includes/class-wp-customize-manager.php @@ -4622,9 +4622,13 @@ final class WP_Customize_Manager { * * @since 4.4.0 * + * @global array $_registered_pages + * * @return string URL for link to close Customizer. */ public function get_return_url() { + global $_registered_pages; + $referer = wp_get_referer(); $excluded_referer_basenames = array( 'customize.php', 'wp-login.php' ); @@ -4637,6 +4641,22 @@ final class WP_Customize_Manager { } else { $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 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; } diff --git a/tests/phpunit/tests/customize/manager.php b/tests/phpunit/tests/customize/manager.php index bca6decdc4..0adaade1b3 100644 --- a/tests/phpunit/tests/customize/manager.php +++ b/tests/phpunit/tests/customize/manager.php @@ -2829,6 +2829,14 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase { $this->assertEquals( admin_url( 'edit.php' ), $this->manager->get_return_url() ); } + /** + * @ticket 46686 + */ + function test_return_url_with_deactivated_theme() { + $this->manager->set_return_url( admin_url( 'themes.php?page=mytheme_documentation' ) ); + $this->assertEquals( admin_url( 'themes.php' ), $this->manager->get_return_url() ); + } + /** * Test get_autofocus()/set_autofocus() methods. *