From 6189d64f313dd43a953b8eb4e49dabfbc08421fb Mon Sep 17 00:00:00 2001 From: Jb Audras Date: Mon, 25 Apr 2022 09:45:00 +0000 Subject: [PATCH] Themes: Ensure `WP_Theme::get_files()` doesn't return unexpected values. This change filters out empty entries from `WP_Theme::get_files()` before returning the array of files. This avoid returning an entry with `false` value when the directory of the theme does not exist. Props dd32, opurockey, Rahmohn, audrasjb. Fixes #53599. git-svn-id: https://develop.svn.wordpress.org/trunk@53253 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-theme.php | 2 +- tests/phpunit/tests/theme/wpTheme.php | 29 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-theme.php b/src/wp-includes/class-wp-theme.php index 2d6ee7eca1..04cca33add 100644 --- a/src/wp-includes/class-wp-theme.php +++ b/src/wp-includes/class-wp-theme.php @@ -1210,7 +1210,7 @@ final class WP_Theme implements ArrayAccess { $files += (array) self::scandir( $this->get_template_directory(), $type, $depth ); } - return $files; + return array_filter( $files ); } /** diff --git a/tests/phpunit/tests/theme/wpTheme.php b/tests/phpunit/tests/theme/wpTheme.php index 3fb1e6e4b7..1096ca29c6 100644 --- a/tests/phpunit/tests/theme/wpTheme.php +++ b/tests/phpunit/tests/theme/wpTheme.php @@ -261,6 +261,35 @@ class Tests_Theme_wpTheme extends WP_UnitTestCase { $this->assertSame( $expected, $theme->is_block_theme() ); } + /** + * Test get_files for an existing theme. + * + * @ticket 53599 + */ + public function test_get_files_theme() { + $theme = new WP_Theme( 'theme1', $this->theme_root ); + $files = $theme->get_files(); + + $this->assertIsArray( $files ); + $this->assertCount( 3, $files ); + $this->assertArrayHasKey( 'functions.php', $files ); + $this->assertArrayHasKey( 'index.php', $files ); + $this->assertArrayHasKey( 'style.css', $files ); + } + + /** + * Test get_files for a non-existing theme. + * + * @ticket 53599 + */ + public function test_get_files_nonexistent_theme() { + $theme = new WP_Theme( 'nonexistent', $this->theme_root ); + $files = $theme->get_files(); + + $this->assertIsArray( $files ); + $this->assertEmpty( $files ); + } + /** * Data provider. *