Themes: Use original template paths when switching blogs.

This fixes a bug introduced by [57129] and [56635] in which deprecating the previous  `TEMPLATEPATH` and `STYLESHEETPATH` constants in favor of `get_template_directory()` and `get_stylesheet_directory()` functions caused the active theme template path to change when using `switch_to_blog()`.

This introduces a new function, `wp_set_template_globals()`, which is called during the bootstrap process to store the template paths to new globals values `$wp_template_path` and `$wp_stylesheet_path`. This restores behavior to how things worked prior to [56635] but retains the ability for template values to be reset for better testability.

Related #18298, #60025.

Props joemcgill, flixos90, mukesh27, swissspidy, manfcarlo, metropolis_john, jeremyfelt.
Fixes #60290.


git-svn-id: https://develop.svn.wordpress.org/trunk@57685 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Joe McGill
2024-02-21 19:24:12 +00:00
parent cd57def8b4
commit 618e42240f
9 changed files with 110 additions and 35 deletions
@@ -190,6 +190,10 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase {
*/
$GLOBALS['wp_sitemaps'] = null;
// Reset template globals.
$GLOBALS['wp_stylesheet_path'] = null;
$GLOBALS['wp_template_path'] = null;
$this->unregister_all_meta_keys();
remove_theme_support( 'html5' );
remove_filter( 'query', array( $this, '_create_temporary_tables' ) );
@@ -6,6 +6,14 @@ class Tests_Comment_MetaCache extends WP_UnitTestCase {
protected $i = 0;
protected $queries = 0;
/**
* Performs setup tasks for every test.
*/
public function set_up() {
parent::set_up();
switch_theme( 'default' );
}
/**
* @ticket 16894
*
+26
View File
@@ -1305,4 +1305,30 @@ class Tests_Theme extends WP_UnitTestCase {
$this->assertSame( $old_root . '/test-parent', $path1, 'The original template path is not correct' );
$this->assertSame( $new_root . '/test-parent', $path2, 'The new template path is not correct' );
}
/**
* Tests that switch_to_blog() uses the original template path.
*
* @ticket 60290
*
* @group ms-required
*
* @covers ::locate_template
*/
public function test_switch_to_blog_uses_original_template_path() {
$old_theme = wp_get_theme();
$template_path = locate_template( 'index.php' );
$blog_id = self::factory()->blog->create();
switch_to_blog( $blog_id );
switch_theme( 'block-theme' );
$new_template_path = locate_template( 'index.php' );
// Cleanup.
restore_current_blog();
switch_theme( $old_theme->get_stylesheet() );
$this->assertSame( $template_path, $new_template_path, 'Switching blogs switches the template path' );
}
}