From 2929dd17f684e23d9523e4700b9ccde0a354b007 Mon Sep 17 00:00:00 2001 From: shail-mehta Date: Sat, 4 May 2024 12:04:34 +0530 Subject: [PATCH 01/11] Validate URL in Locate Template --- src/wp-includes/template.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/template.php b/src/wp-includes/template.php index 401df4db19925..d6c4b220b313a 100644 --- a/src/wp-includes/template.php +++ b/src/wp-includes/template.php @@ -729,15 +729,19 @@ function locate_template( $template_names, $load = false, $load_once = true, $ar if ( ! $template_name ) { continue; } - if ( file_exists( $wp_stylesheet_path . '/' . $template_name ) ) { - $located = $wp_stylesheet_path . '/' . $template_name; + + $stylesheet = $wp_stylesheet_path . '/' . $template_name; + $template = $wp_template_path . '/' . $template_name; + $template_compat = ABSPATH . WPINC . '/theme-compat/' . $template_name; + + if ( file_exists( $stylesheet ) && 0 === validate_file( $stylesheet ) ) { + $located = $stylesheet; break; - } elseif ( $is_child_theme && file_exists( $wp_template_path . '/' . $template_name ) ) { + } elseif ( is_child_theme() && file_exists( $template ) && 0 === validate_file( $template ) ) { $located = $wp_template_path . '/' . $template_name; break; - } elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) { - $located = ABSPATH . WPINC . '/theme-compat/' . $template_name; - break; + } elseif ( file_exists( $template_compat ) && 0 === validate_file( $template_compat ) ) { + $located = $template_compat; } } From 3a3eae07d4a4e28e6d20b8bfcc5c081a5efd8b4c Mon Sep 17 00:00:00 2001 From: shail-mehta Date: Sat, 4 May 2024 13:27:49 +0530 Subject: [PATCH 02/11] Removed Empty Space --- src/wp-includes/template.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/wp-includes/template.php b/src/wp-includes/template.php index d6c4b220b313a..8086719245ad7 100644 --- a/src/wp-includes/template.php +++ b/src/wp-includes/template.php @@ -729,7 +729,6 @@ function locate_template( $template_names, $load = false, $load_once = true, $ar if ( ! $template_name ) { continue; } - $stylesheet = $wp_stylesheet_path . '/' . $template_name; $template = $wp_template_path . '/' . $template_name; $template_compat = ABSPATH . WPINC . '/theme-compat/' . $template_name; From 1935aa211b3f664b5c97546247416df7eab62700 Mon Sep 17 00:00:00 2001 From: shail-mehta Date: Sat, 4 May 2024 14:11:38 +0530 Subject: [PATCH 03/11] Validate URL in Locate Template --- src/wp-includes/template.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/template.php b/src/wp-includes/template.php index 8086719245ad7..59c8b920ef0d1 100644 --- a/src/wp-includes/template.php +++ b/src/wp-includes/template.php @@ -729,6 +729,7 @@ function locate_template( $template_names, $load = false, $load_once = true, $ar if ( ! $template_name ) { continue; } + $stylesheet = $wp_stylesheet_path . '/' . $template_name; $template = $wp_template_path . '/' . $template_name; $template_compat = ABSPATH . WPINC . '/theme-compat/' . $template_name; @@ -736,7 +737,7 @@ function locate_template( $template_names, $load = false, $load_once = true, $ar if ( file_exists( $stylesheet ) && 0 === validate_file( $stylesheet ) ) { $located = $stylesheet; break; - } elseif ( is_child_theme() && file_exists( $template ) && 0 === validate_file( $template ) ) { + } elseif ( $is_child_theme() && file_exists( $template ) && 0 === validate_file( $template ) ) { $located = $wp_template_path . '/' . $template_name; break; } elseif ( file_exists( $template_compat ) && 0 === validate_file( $template_compat ) ) { From 1ea77837a44e5033642703414238a48509120803 Mon Sep 17 00:00:00 2001 From: shail-mehta Date: Sat, 4 May 2024 14:14:23 +0530 Subject: [PATCH 04/11] Fix Spacing Issue --- src/wp-includes/template.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/wp-includes/template.php b/src/wp-includes/template.php index 59c8b920ef0d1..bb6e91d4dc92b 100644 --- a/src/wp-includes/template.php +++ b/src/wp-includes/template.php @@ -728,8 +728,7 @@ function locate_template( $template_names, $load = false, $load_once = true, $ar foreach ( (array) $template_names as $template_name ) { if ( ! $template_name ) { continue; - } - + } $stylesheet = $wp_stylesheet_path . '/' . $template_name; $template = $wp_template_path . '/' . $template_name; $template_compat = ABSPATH . WPINC . '/theme-compat/' . $template_name; From 6747fce313ba713bd68f46e8435f68f581cb52c4 Mon Sep 17 00:00:00 2001 From: shail-mehta Date: Sat, 4 May 2024 14:16:08 +0530 Subject: [PATCH 05/11] Fix Spacing Issue --- src/wp-includes/template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/template.php b/src/wp-includes/template.php index bb6e91d4dc92b..66aa4f38f5f12 100644 --- a/src/wp-includes/template.php +++ b/src/wp-includes/template.php @@ -728,7 +728,7 @@ function locate_template( $template_names, $load = false, $load_once = true, $ar foreach ( (array) $template_names as $template_name ) { if ( ! $template_name ) { continue; - } + } $stylesheet = $wp_stylesheet_path . '/' . $template_name; $template = $wp_template_path . '/' . $template_name; $template_compat = ABSPATH . WPINC . '/theme-compat/' . $template_name; From 116103c2dd5c00e35fb2912bee894bf7f4d1f530 Mon Sep 17 00:00:00 2001 From: shail-mehta Date: Sat, 4 May 2024 14:26:48 +0530 Subject: [PATCH 06/11] Validate URL in Locate Template --- src/wp-includes/template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/template.php b/src/wp-includes/template.php index 66aa4f38f5f12..cf2d796345ecc 100644 --- a/src/wp-includes/template.php +++ b/src/wp-includes/template.php @@ -736,7 +736,7 @@ function locate_template( $template_names, $load = false, $load_once = true, $ar if ( file_exists( $stylesheet ) && 0 === validate_file( $stylesheet ) ) { $located = $stylesheet; break; - } elseif ( $is_child_theme() && file_exists( $template ) && 0 === validate_file( $template ) ) { + } elseif ( $is_child_theme && file_exists( $template ) && 0 === validate_file( $template ) ) { $located = $wp_template_path . '/' . $template_name; break; } elseif ( file_exists( $template_compat ) && 0 === validate_file( $template_compat ) ) { From c276e0f13883a8e34039a996d9d08853db6e149f Mon Sep 17 00:00:00 2001 From: shail-mehta Date: Sun, 5 May 2024 10:04:23 +0530 Subject: [PATCH 07/11] Validate URL in Locate Template --- src/wp-includes/template.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/template.php b/src/wp-includes/template.php index cf2d796345ecc..469fa5069f3b3 100644 --- a/src/wp-includes/template.php +++ b/src/wp-includes/template.php @@ -729,6 +729,7 @@ function locate_template( $template_names, $load = false, $load_once = true, $ar if ( ! $template_name ) { continue; } + $stylesheet = $wp_stylesheet_path . '/' . $template_name; $template = $wp_template_path . '/' . $template_name; $template_compat = ABSPATH . WPINC . '/theme-compat/' . $template_name; From 8ae3447bbaa7223d82a2af3d259d3fb04a254077 Mon Sep 17 00:00:00 2001 From: shail-mehta Date: Sun, 5 May 2024 10:05:47 +0530 Subject: [PATCH 08/11] Fixed spacing issue --- src/wp-includes/template.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/wp-includes/template.php b/src/wp-includes/template.php index 469fa5069f3b3..cf2d796345ecc 100644 --- a/src/wp-includes/template.php +++ b/src/wp-includes/template.php @@ -729,7 +729,6 @@ function locate_template( $template_names, $load = false, $load_once = true, $ar if ( ! $template_name ) { continue; } - $stylesheet = $wp_stylesheet_path . '/' . $template_name; $template = $wp_template_path . '/' . $template_name; $template_compat = ABSPATH . WPINC . '/theme-compat/' . $template_name; From 2965b9e924bd56f999dd92d0cf4482bcc4634090 Mon Sep 17 00:00:00 2001 From: shail-mehta Date: Thu, 30 May 2024 22:42:44 +0530 Subject: [PATCH 09/11] added-suggested-change --- src/wp-includes/template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/template.php b/src/wp-includes/template.php index cf2d796345ecc..d07296045ca85 100644 --- a/src/wp-includes/template.php +++ b/src/wp-includes/template.php @@ -737,7 +737,7 @@ function locate_template( $template_names, $load = false, $load_once = true, $ar $located = $stylesheet; break; } elseif ( $is_child_theme && file_exists( $template ) && 0 === validate_file( $template ) ) { - $located = $wp_template_path . '/' . $template_name; + $located = $template; break; } elseif ( file_exists( $template_compat ) && 0 === validate_file( $template_compat ) ) { $located = $template_compat; From 10fab3f9af2926e0b67f74f4d402fa7f4046d4e0 Mon Sep 17 00:00:00 2001 From: Aaron Jorbin Date: Mon, 5 Aug 2024 16:41:56 -0400 Subject: [PATCH 10/11] Add some automated tests --- tests/phpunit/tests/template.php | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/phpunit/tests/template.php b/tests/phpunit/tests/template.php index 82505bba8e4f0..64173b2031f95 100644 --- a/tests/phpunit/tests/template.php +++ b/tests/phpunit/tests/template.php @@ -667,6 +667,45 @@ public function test_locate_template_uses_current_theme() { $this->assertSame( $new_theme->get_stylesheet_directory() . '/index.php', locate_template( $template_names ), 'Incorrect index template found in theme after switch.' ); } + /** + * Tests that `locate_template()` uses the current theme even after switching the theme. + * + * @dataProvider data_locate_template_only_loads_theme_files + * + * @ticket 58905 + * + * @covers ::locate_template + * + * @param string $template_name Template name to locate. + * @param bool $located_empty Whether the located template should be empty. + */ + public function test_locate_template_only_loads_theme_files( $template_name, $located_empty ) { + $located_return = locate_template( $template_name ); + $this->assertSame( empty( $located_return ), $located_empty ); + } + + /** + * Data provider for `test_locate_template_only_loads_theme_files()`. + * + * @return array + */ + function data_locate_template_only_loads_theme_files() { + return array( + 'file that is in theme compat so will always return' => array( + 'header.php', + false, + ), + 'something that does not exist' => array( + 'the_limit_according_to_cady_heron.php', + true, + ), + 'file that is not in a theme path' => array( + WP_PLUGIN_DIR . '/hello.php', + true, + ), + ); + } + public function assertTemplateHierarchy( $url, array $expected, $message = '' ) { $this->go_to( $url ); $hierarchy = $this->get_template_hierarchy(); From 031b6af70e67663506a3b3c3441558cbb3a27f3c Mon Sep 17 00:00:00 2001 From: Aaron Jorbin Date: Mon, 5 Aug 2024 16:45:38 -0400 Subject: [PATCH 11/11] add visibility to dataprovider --- tests/phpunit/tests/template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/template.php b/tests/phpunit/tests/template.php index 64173b2031f95..0b072065b86c0 100644 --- a/tests/phpunit/tests/template.php +++ b/tests/phpunit/tests/template.php @@ -689,7 +689,7 @@ public function test_locate_template_only_loads_theme_files( $template_name, $lo * * @return array */ - function data_locate_template_only_loads_theme_files() { + public function data_locate_template_only_loads_theme_files() { return array( 'file that is in theme compat so will always return' => array( 'header.php',