Skip to content

Commit

Permalink
Reset memoized globals.
Browse files Browse the repository at this point in the history
  • Loading branch information
emilyatmobtown committed Nov 27, 2023
1 parent 7e4791d commit f5dee35
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/class-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ protected function maybe_migrate() {
return;
}

$this->maybe_reset_globals();

$this->switch_theme();

// Only migrate once in a request.
Expand All @@ -155,6 +157,37 @@ protected function switch_theme() {
add_filter( 'option_current_theme', [ $this, 'get_theme_name' ] );
}

/**
* In WP 6.4.1, the template and stylesheet paths were memoized in globals.
* This introduced a bug in which the theme path can not be reset if
* `get_template_directory()` or `get_stylesheet_directory()` are called
* before the theme has been fully initialized. This method resets the
* the globals if they are present.
*
* @see https://github.com/WordPress/wordpress-develop/commit/b6bf3553d975da2e017a00443b7639aacf0a8586
*/
protected function maybe_reset_globals(): void {
if ( ! array_key_exists( 'wp_template_path', $GLOBALS ) && ! array_key_exists( 'wp_stylesheet_path', $GLOBALS ) ) {
return;
}

global $wp_template_path, $wp_stylesheet_path;

$stylesheet = $this->get_theme_stylesheet();
$theme_root = get_theme_root( $stylesheet );

if ( "$theme_root/$stylesheet" !== $wp_stylesheet_path ) {
$wp_stylesheet_path = null; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
}

$template = $this->get_theme_stylesheet();
$theme_root = get_theme_root( $template );

if ( "$theme_root/$template" !== $wp_template_path ) {
$wp_template_path = null; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
}
}

/**
* Get the theme stylesheet.
*
Expand Down

0 comments on commit f5dee35

Please sign in to comment.