From 69a56e30f96333dd6de25c141428c7e7ced480d4 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 19 Oct 2023 19:07:13 +0000 Subject: [PATCH] Multisite: Ensure that switching sites resets the current theme directory globals. The globals introduced in [56635] to cache the current theme directories in memory were not considering switching sites in a multisite network. This changeset addresses the bug including test coverage. Props codex-m, jeremyfelt. Fixes #59677. See #18298. git-svn-id: https://develop.svn.wordpress.org/trunk@56974 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/ms-blogs.php | 16 ++++-- tests/phpunit/tests/theme.php | 96 +++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/ms-blogs.php b/src/wp-includes/ms-blogs.php index eddb99c605..7e91e11b85 100644 --- a/src/wp-includes/ms-blogs.php +++ b/src/wp-includes/ms-blogs.php @@ -491,6 +491,8 @@ function update_blog_option( $id, $option, $value, $deprecated = null ) { * @global array $_wp_switched_stack * @global bool $switched * @global string $table_prefix + * @global string $wp_template_path + * @global string $wp_stylesheet_path * @global WP_Object_Cache $wp_object_cache * * @param int $new_blog_id The ID of the blog to switch to. Default: current blog. @@ -532,8 +534,10 @@ function switch_to_blog( $new_blog_id, $deprecated = null ) { } $wpdb->set_blog_id( $new_blog_id ); - $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix(); - $GLOBALS['blog_id'] = $new_blog_id; + $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix(); + $GLOBALS['blog_id'] = $new_blog_id; + $GLOBALS['wp_template_path'] = null; + $GLOBALS['wp_stylesheet_path'] = null; if ( function_exists( 'wp_cache_switch_to_blog' ) ) { wp_cache_switch_to_blog( $new_blog_id ); @@ -600,6 +604,8 @@ function switch_to_blog( $new_blog_id, $deprecated = null ) { * @global int $blog_id * @global bool $switched * @global string $table_prefix + * @global string $wp_template_path + * @global string $wp_stylesheet_path * @global WP_Object_Cache $wp_object_cache * * @return bool True on success, false if we're already on the current blog. @@ -625,8 +631,10 @@ function restore_current_blog() { } $wpdb->set_blog_id( $new_blog_id ); - $GLOBALS['blog_id'] = $new_blog_id; - $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix(); + $GLOBALS['blog_id'] = $new_blog_id; + $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix(); + $GLOBALS['wp_template_path'] = null; + $GLOBALS['wp_stylesheet_path'] = null; if ( function_exists( 'wp_cache_switch_to_blog' ) ) { wp_cache_switch_to_blog( $new_blog_id ); diff --git a/tests/phpunit/tests/theme.php b/tests/phpunit/tests/theme.php index 721fa81f22..77cad7156b 100644 --- a/tests/phpunit/tests/theme.php +++ b/tests/phpunit/tests/theme.php @@ -1058,6 +1058,102 @@ class Tests_Theme extends WP_UnitTestCase { ); } + /** + * Tests whether a switched site retrieves the correct stylesheet directory. + * + * @ticket 59677 + * @group ms-required + * + * @covers ::get_stylesheet_directory + */ + public function test_get_stylesheet_directory_with_switched_site() { + $blog_id = self::factory()->blog->create(); + + update_blog_option( $blog_id, 'stylesheet', 'switched_stylesheet' ); + + // Prime global storage with the current site's data. + get_stylesheet_directory(); + + switch_to_blog( $blog_id ); + $switched_stylesheet = get_stylesheet_directory(); + restore_current_blog(); + + $this->assertSame( WP_CONTENT_DIR . '/themes/switched_stylesheet', $switched_stylesheet ); + } + + /** + * Tests whether a switched site retrieves the correct template directory. + * + * @ticket 59677 + * @group ms-required + * + * @covers ::get_template_directory + */ + public function test_get_template_directory_with_switched_site() { + $blog_id = self::factory()->blog->create(); + + update_blog_option( $blog_id, 'template', 'switched_template' ); + + // Prime global storage with the current site's data. + get_template_directory(); + + switch_to_blog( $blog_id ); + $switched_template = get_template_directory(); + restore_current_blog(); + + $this->assertSame( WP_CONTENT_DIR . '/themes/switched_template', $switched_template ); + } + + /** + * Tests whether a restored site retrieves the correct stylesheet directory. + * + * @ticket 59677 + * @group ms-required + * + * @covers ::get_stylesheet_directory + */ + public function test_get_stylesheet_directory_with_restored_site() { + $blog_id = self::factory()->blog->create(); + + update_option( 'stylesheet', 'original_stylesheet' ); + update_blog_option( $blog_id, 'stylesheet', 'switched_stylesheet' ); + + $stylesheet = get_stylesheet_directory(); + + switch_to_blog( $blog_id ); + + // Prime global storage with the restored site's data. + get_stylesheet_directory(); + restore_current_blog(); + + $this->assertSame( WP_CONTENT_DIR . '/themes/original_stylesheet', $stylesheet ); + } + + /** + * Tests whether a restored site retrieves the correct template directory. + * + * @ticket 59677 + * @group ms-required + * + * @covers ::get_template_directory + */ + public function test_get_template_directory_with_restored_site() { + $blog_id = self::factory()->blog->create(); + + update_option( 'template', 'original_template' ); + update_blog_option( $blog_id, 'template', 'switched_template' ); + + $template = get_template_directory(); + + switch_to_blog( $blog_id ); + + // Prime global storage with the switched site's data. + get_template_directory(); + restore_current_blog(); + + $this->assertSame( WP_CONTENT_DIR . '/themes/original_template', $template ); + } + /** * Helper function to ensure that a block theme is available and active. */