From f2c05952c07bf8860a49211854479a5e478069d2 Mon Sep 17 00:00:00 2001 From: Aaron Jorbin Date: Mon, 4 Dec 2023 19:57:30 +0000 Subject: [PATCH] Themes: Remove memoization from stylesheet and theme directories. This fixes bugs introduced in [56635] whereby the template or stylesheet path could be memoized incorrectly if get_template_directory() or get_stylesheet_directory() were called before the theme has been fully initialized. Reviewed by Jorbin. Merges [57129] to 6.4 branch. Props partyfrikadelle, coreyw, kdowns, rebasaurus, meta4, flixos90, mukesh27, joemcgill, icaleb. Fixes #59847. git-svn-id: https://develop.svn.wordpress.org/branches/6.4@57156 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/ms-blogs.php | 16 +-- src/wp-includes/theme.php | 95 ++++--------- .../data/themedir2/test-parent/functions.php | 7 + .../data/themedir2/test-parent/index.php | 7 + .../data/themedir2/test-parent/style.css | 12 ++ .../phpunit/data/themedir2/test/functions.php | 7 + tests/phpunit/data/themedir2/test/index.php | 7 + tests/phpunit/data/themedir2/test/style.css | 12 ++ tests/phpunit/tests/theme.php | 126 ++++++++++++++++++ 9 files changed, 211 insertions(+), 78 deletions(-) create mode 100644 tests/phpunit/data/themedir2/test-parent/functions.php create mode 100644 tests/phpunit/data/themedir2/test-parent/index.php create mode 100644 tests/phpunit/data/themedir2/test-parent/style.css create mode 100644 tests/phpunit/data/themedir2/test/functions.php create mode 100644 tests/phpunit/data/themedir2/test/index.php create mode 100644 tests/phpunit/data/themedir2/test/style.css diff --git a/src/wp-includes/ms-blogs.php b/src/wp-includes/ms-blogs.php index 7e91e11b8532f..eddb99c605cff 100644 --- a/src/wp-includes/ms-blogs.php +++ b/src/wp-includes/ms-blogs.php @@ -491,8 +491,6 @@ 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. @@ -534,10 +532,8 @@ 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['wp_template_path'] = null; - $GLOBALS['wp_stylesheet_path'] = null; + $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix(); + $GLOBALS['blog_id'] = $new_blog_id; if ( function_exists( 'wp_cache_switch_to_blog' ) ) { wp_cache_switch_to_blog( $new_blog_id ); @@ -604,8 +600,6 @@ 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. @@ -631,10 +625,8 @@ 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['wp_template_path'] = null; - $GLOBALS['wp_stylesheet_path'] = null; + $GLOBALS['blog_id'] = $new_blog_id; + $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix(); if ( function_exists( 'wp_cache_switch_to_blog' ) ) { wp_cache_switch_to_blog( $new_blog_id ); diff --git a/src/wp-includes/theme.php b/src/wp-includes/theme.php index 6315cc2ab5ce2..89fccddd37393 100644 --- a/src/wp-includes/theme.php +++ b/src/wp-includes/theme.php @@ -188,39 +188,25 @@ function get_stylesheet() { * * @since 1.5.0 * @since 6.4.0 Memoizes filter execution so that it only runs once for the current theme. - * - * @global string $wp_stylesheet_path Current theme stylesheet directory path. + * @since 6.4.2 Memoization removed. * * @return string Path to active theme's stylesheet directory. */ function get_stylesheet_directory() { - global $wp_stylesheet_path; - - if ( null === $wp_stylesheet_path ) { - $stylesheet = get_stylesheet(); - $theme_root = get_theme_root( $stylesheet ); - $stylesheet_dir = "$theme_root/$stylesheet"; - - /** - * Filters the stylesheet directory path for the active theme. - * - * @since 1.5.0 - * - * @param string $stylesheet_dir Absolute path to the active theme. - * @param string $stylesheet Directory name of the active theme. - * @param string $theme_root Absolute path to themes directory. - */ - $stylesheet_dir = apply_filters( 'stylesheet_directory', $stylesheet_dir, $stylesheet, $theme_root ); - - // If there are filter callbacks, force the logic to execute on every call. - if ( has_filter( 'stylesheet' ) || has_filter( 'theme_root' ) || has_filter( 'stylesheet_directory' ) ) { - return $stylesheet_dir; - } + $stylesheet = get_stylesheet(); + $theme_root = get_theme_root( $stylesheet ); + $stylesheet_dir = "$theme_root/$stylesheet"; - $wp_stylesheet_path = $stylesheet_dir; - } - - return $wp_stylesheet_path; + /** + * Filters the stylesheet directory path for the active theme. + * + * @since 1.5.0 + * + * @param string $stylesheet_dir Absolute path to the active theme. + * @param string $stylesheet Directory name of the active theme. + * @param string $theme_root Absolute path to themes directory. + */ + return apply_filters( 'stylesheet_directory', $stylesheet_dir, $stylesheet, $theme_root ); } /** @@ -338,39 +324,25 @@ function get_template() { * * @since 1.5.0 * @since 6.4.0 Memoizes filter execution so that it only runs once for the current theme. - * - * @global string $wp_template_path Current theme template directory path. + * @since 6.4.1 Memoization removed. * * @return string Path to active theme's template directory. */ function get_template_directory() { - global $wp_template_path; - - if ( null === $wp_template_path ) { - $template = get_template(); - $theme_root = get_theme_root( $template ); - $template_dir = "$theme_root/$template"; + $template = get_template(); + $theme_root = get_theme_root( $template ); + $template_dir = "$theme_root/$template"; - /** - * Filters the active theme directory path. - * - * @since 1.5.0 - * - * @param string $template_dir The path of the active theme directory. - * @param string $template Directory name of the active theme. - * @param string $theme_root Absolute path to the themes directory. - */ - $template_dir = apply_filters( 'template_directory', $template_dir, $template, $theme_root ); - - // If there are filter callbacks, force the logic to execute on every call. - if ( has_filter( 'template' ) || has_filter( 'theme_root' ) || has_filter( 'template_directory' ) ) { - return $template_dir; - } - - $wp_template_path = $template_dir; - } - - return $wp_template_path; + /** + * Filters the active theme directory path. + * + * @since 1.5.0 + * + * @param string $template_dir The path of the active theme directory. + * @param string $template Directory name of the active theme. + * @param string $theme_root Absolute path to the themes directory. + */ + return apply_filters( 'template_directory', $template_dir, $template, $theme_root ); } /** @@ -776,13 +748,11 @@ function locale_stylesheet() { * @global WP_Customize_Manager $wp_customize * @global array $sidebars_widgets * @global array $wp_registered_sidebars - * @global string $wp_stylesheet_path - * @global string $wp_template_path * * @param string $stylesheet Stylesheet name. */ function switch_theme( $stylesheet ) { - global $wp_theme_directories, $wp_customize, $sidebars_widgets, $wp_registered_sidebars, $wp_stylesheet_path, $wp_template_path; + global $wp_theme_directories, $wp_customize, $sidebars_widgets, $wp_registered_sidebars; $requirements = validate_theme_requirements( $stylesheet ); if ( is_wp_error( $requirements ) ) { @@ -866,13 +836,6 @@ function switch_theme( $stylesheet ) { update_option( 'theme_switched', $old_theme->get_stylesheet() ); - /* - * Reset globals to force refresh the next time these directories are - * accessed via `get_stylesheet_directory()` / `get_template_directory()`. - */ - $wp_stylesheet_path = null; - $wp_template_path = null; - // Clear pattern caches. $new_theme->delete_pattern_cache(); $old_theme->delete_pattern_cache(); diff --git a/tests/phpunit/data/themedir2/test-parent/functions.php b/tests/phpunit/data/themedir2/test-parent/functions.php new file mode 100644 index 0000000000000..f538fe42304c2 --- /dev/null +++ b/tests/phpunit/data/themedir2/test-parent/functions.php @@ -0,0 +1,7 @@ + diff --git a/tests/phpunit/data/themedir2/test-parent/index.php b/tests/phpunit/data/themedir2/test-parent/index.php new file mode 100644 index 0000000000000..f538fe42304c2 --- /dev/null +++ b/tests/phpunit/data/themedir2/test-parent/index.php @@ -0,0 +1,7 @@ + diff --git a/tests/phpunit/data/themedir2/test-parent/style.css b/tests/phpunit/data/themedir2/test-parent/style.css new file mode 100644 index 0000000000000..aaa61dfea4bda --- /dev/null +++ b/tests/phpunit/data/themedir2/test-parent/style.css @@ -0,0 +1,12 @@ +/* +Theme Name: Test Parent Theme +Theme URI: http://example.org/ +Description: An example parent theme +Version: 1.3 +Author: Minnie Bannister +Author URI: http://example.com/ +Template: test-parent +*/ + + + diff --git a/tests/phpunit/data/themedir2/test/functions.php b/tests/phpunit/data/themedir2/test/functions.php new file mode 100644 index 0000000000000..f538fe42304c2 --- /dev/null +++ b/tests/phpunit/data/themedir2/test/functions.php @@ -0,0 +1,7 @@ + diff --git a/tests/phpunit/data/themedir2/test/index.php b/tests/phpunit/data/themedir2/test/index.php new file mode 100644 index 0000000000000..f538fe42304c2 --- /dev/null +++ b/tests/phpunit/data/themedir2/test/index.php @@ -0,0 +1,7 @@ + diff --git a/tests/phpunit/data/themedir2/test/style.css b/tests/phpunit/data/themedir2/test/style.css new file mode 100644 index 0000000000000..b3d9e77cd2323 --- /dev/null +++ b/tests/phpunit/data/themedir2/test/style.css @@ -0,0 +1,12 @@ +/* +Theme Name: Test Theme +Theme URI: http://example.org/ +Description: An example theme +Version: 1.3 +Author: Minnie Bannister +Author URI: http://example.com/ +Template: test-parent +*/ + + + diff --git a/tests/phpunit/tests/theme.php b/tests/phpunit/tests/theme.php index 7260d6af57c46..75bfbdd612de1 100644 --- a/tests/phpunit/tests/theme.php +++ b/tests/phpunit/tests/theme.php @@ -1179,4 +1179,130 @@ private function helper_requires_block_theme() { $this->markTestSkipped( "Could not switch to $block_theme." ); } } + + /** + * Make sure filters added after the initial call are fired. + * + * @ticket 59847 + * + * @covers ::get_stylesheet_directory + */ + public function test_get_stylesheet_directory_filters_apply() { + // Call the function prior to the filter being added. + get_stylesheet_directory(); + + $expected = 'test_root/dir'; + + // Add the filer. + add_filter( + 'stylesheet_directory', + function () use ( $expected ) { + return $expected; + } + ); + + $this->assertSame( $expected, get_stylesheet_directory() ); + } + + /** + * Make sure filters added after the initial call are fired. + * + * @ticket 59847 + * + * @covers ::get_template_directory + */ + public function test_get_template_directory_filters_apply() { + // Call the function prior to the filter being added. + get_template_directory(); + + $expected = 'test_root/dir'; + + // Add the filer. + add_filter( + 'template_directory', + function () use ( $expected ) { + return $expected; + } + ); + + $this->assertSame( $expected, get_template_directory() ); + } + + /** + * Make sure get_stylesheet_directory uses the correct path when the root theme dir changes. + * + * @ticket 59847 + * + * @covers ::get_stylesheet_directory + */ + public function test_get_stylesheet_directory_uses_registered_theme_dir() { + $old_theme = wp_get_theme(); + + switch_theme( 'test' ); + + $old_root = get_theme_root( 'test' ); + $path1 = get_stylesheet_directory(); + + $new_root = DIR_TESTDATA . '/themedir2'; + register_theme_directory( $new_root ); + + // Mock the stylesheet root option to mimic that the active root has changed. + add_filter( + 'pre_option_stylesheet_root', + function () use ( $new_root ) { + return $new_root; + } + ); + + $path2 = get_stylesheet_directory(); + + // Cleanup. + switch_theme( $old_theme->get_stylesheet() ); + + $this->assertEquals( $old_root . '/test', $path1, 'The original stylesheet path is not correct' ); + $this->assertEquals( $new_root . '/test', $path2, 'The new stylesheet path is not correct' ); + } + + /** + * Make sure get_template_directory uses the correct path when the root theme dir changes. + * + * @ticket 59847 + * + * @covers ::get_template_directory + */ + public function test_get_template_directory_uses_registered_theme_dir() { + $old_theme = wp_get_theme(); + + switch_theme( 'test' ); + + // Mock parent theme to be returned as the template. + add_filter( + 'pre_option_template', + function () { + return 'test-parent'; + } + ); + + $old_root = get_theme_root( 'test' ); + $path1 = get_template_directory(); + + $new_root = DIR_TESTDATA . '/themedir2'; + register_theme_directory( $new_root ); + + // Mock the template root option to mimic that the active root has changed. + add_filter( + 'pre_option_template_root', + function () use ( $new_root ) { + return $new_root; + } + ); + + $path2 = get_template_directory(); + + // Cleanup. + switch_theme( $old_theme->get_stylesheet() ); + + $this->assertEquals( $old_root . '/test-parent', $path1, 'The original template path is not correct' ); + $this->assertEquals( $new_root . '/test-parent', $path2, 'The new template path is not correct' ); + } }