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

View File

@@ -153,11 +153,17 @@ function wp_clean_themes_cache( $clear_update_cache = true ) {
* Whether a child theme is in use.
*
* @since 3.0.0
* @since 6.5.0 Makes use of global template variables.
*
* @global string $wp_stylesheet_path Path to current theme's stylesheet directory.
* @global string $wp_template_path Path to current theme's template directory.
*
* @return bool True if a child theme is in use, false otherwise.
*/
function is_child_theme() {
return get_template_directory() !== get_stylesheet_directory();
global $wp_stylesheet_path, $wp_template_path;
return $wp_stylesheet_path !== $wp_template_path;
}
/**
@@ -836,6 +842,14 @@ function switch_theme( $stylesheet ) {
update_option( 'theme_switched', $old_theme->get_stylesheet() );
/*
* Reset template globals when switching themes outside of a switched blog
* context to ensure templates will be loaded from the new theme.
*/
if ( ! is_multisite() || ! ms_is_switched() ) {
wp_set_template_globals();
}
// Clear pattern caches.
if ( ! is_multisite() ) {
$new_theme->delete_pattern_cache();