From 054741f8119b8f8e9707a4fb0f618d6fad10d485 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 2 Feb 2026 13:58:38 +0000 Subject: [PATCH 1/7] Initial plan From 3d762f7dd2e32a74ff54fed8bcc24319f98451c0 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 2 Feb 2026 14:03:18 +0000 Subject: [PATCH 2/7] Add Theme_Cache_Command with clear and flush methods Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --- composer.json | 3 + extension-command.php | 1 + features/theme-cache.feature | 76 +++++++++++++++++++++++ phpcs.xml.dist | 2 +- src/Theme_Cache_Command.php | 116 +++++++++++++++++++++++++++++++++++ 5 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 features/theme-cache.feature create mode 100644 src/Theme_Cache_Command.php diff --git a/composer.json b/composer.json index 62a605a16..898b5c035 100644 --- a/composer.json +++ b/composer.json @@ -61,6 +61,9 @@ "plugin update", "theme", "theme activate", + "theme cache", + "theme cache clear", + "theme cache flush", "theme delete", "theme disable", "theme enable", diff --git a/extension-command.php b/extension-command.php index 18b87efd0..a80b2167b 100644 --- a/extension-command.php +++ b/extension-command.php @@ -22,3 +22,4 @@ WP_CLI::add_command( 'theme', 'Theme_Command' ); WP_CLI::add_command( 'theme auto-updates', 'Theme_AutoUpdates_Command', $wpcli_extension_requires_wp_5_5 ); WP_CLI::add_command( 'theme mod', 'Theme_Mod_Command' ); +WP_CLI::add_command( 'theme cache', 'Theme_Cache_Command' ); diff --git a/features/theme-cache.feature b/features/theme-cache.feature new file mode 100644 index 000000000..ea62a3db6 --- /dev/null +++ b/features/theme-cache.feature @@ -0,0 +1,76 @@ +Feature: Manage theme cache + + Background: + Given a WP installation + + Scenario: Clear cache for a single theme + When I run `wp theme install twentytwentyfour --activate` + Then STDOUT should contain: + """ + Success: + """ + + When I run `wp theme cache clear twentytwentyfour` + Then STDOUT should be: + """ + Success: Cleared cache for 'twentytwentyfour' theme. + """ + + Scenario: Clear cache for multiple themes + When I run `wp theme install twentytwentythree` + Then STDOUT should contain: + """ + Success: + """ + + When I run `wp theme install twentytwentyfour` + Then STDOUT should contain: + """ + Success: + """ + + When I run `wp theme cache clear twentytwentythree twentytwentyfour` + Then STDOUT should be: + """ + Success: Cleared cache for 2 themes. + """ + + Scenario: Clear cache for all themes + When I run `wp theme install twentytwentythree` + Then STDOUT should contain: + """ + Success: + """ + + When I run `wp theme cache clear --all` + Then STDOUT should contain: + """ + Success: Cleared cache for + """ + And STDOUT should contain: + """ + themes. + """ + + Scenario: Clear cache for non-existent theme + When I try `wp theme cache clear nonexistent` + Then STDERR should contain: + """ + Warning: Theme 'nonexistent' not found. + """ + And the return code should be 1 + + Scenario: Clear cache with no arguments + When I try `wp theme cache clear` + Then STDERR should be: + """ + Error: Please specify one or more themes, or use --all. + """ + And the return code should be 1 + + Scenario: Flush the entire theme cache group + When I run `wp theme cache flush` + Then STDOUT should be: + """ + Success: The theme cache was flushed. + """ diff --git a/phpcs.xml.dist b/phpcs.xml.dist index d0cb7801d..1d8835833 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -53,7 +53,7 @@ - */src/(Plugin_(AutoUpdates_)?|Theme_(Mod_|AutoUpdates_)?)Command\.php$ + */src/(Plugin_(AutoUpdates_)?|Theme_(Mod_|AutoUpdates_|Cache_)?)Command\.php$ diff --git a/src/Theme_Cache_Command.php b/src/Theme_Cache_Command.php new file mode 100644 index 000000000..5840f2707 --- /dev/null +++ b/src/Theme_Cache_Command.php @@ -0,0 +1,116 @@ +...] + * : One or more themes to clear the cache for. + * + * [--all] + * : If set, clear cache for all installed themes. + * + * ## EXAMPLES + * + * # Clear cache for a single theme + * $ wp theme cache clear twentytwentyfour + * Success: Cleared cache for 'twentytwentyfour' theme. + * + * # Clear cache for multiple themes + * $ wp theme cache clear twentytwentythree twentytwentyfour + * Success: Cleared cache for 2 themes. + * + * # Clear cache for all themes + * $ wp theme cache clear --all + * Success: Cleared cache for all themes. + * + * @param string[] $args Positional arguments. + * @param array{all?: bool} $assoc_args Associative arguments. + */ + public function clear( $args, $assoc_args ) { + if ( ! \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) && empty( $args ) ) { + WP_CLI::error( 'Please specify one or more themes, or use --all.' ); + } + + $themes = []; + + if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) ) { + $all_themes = wp_get_themes(); + foreach ( $all_themes as $theme_slug => $theme ) { + $themes[] = $theme; + } + } else { + foreach ( $args as $theme_slug ) { + $theme = wp_get_theme( $theme_slug ); + if ( ! $theme->exists() ) { + WP_CLI::warning( "Theme '{$theme_slug}' not found." ); + continue; + } + $themes[] = $theme; + } + } + + if ( empty( $themes ) ) { + WP_CLI::error( 'No valid themes to clear cache for.' ); + } + + $cleared = 0; + foreach ( $themes as $theme ) { + $this->clear_theme_cache( $theme ); + ++$cleared; + } + + if ( 1 === $cleared ) { + WP_CLI::success( "Cleared cache for '{$themes[0]->get_stylesheet()}' theme." ); + } else { + WP_CLI::success( "Cleared cache for {$cleared} themes." ); + } + } + + /** + * Flushes the entire theme cache group. + * + * ## EXAMPLES + * + * # Flush the entire theme cache group + * $ wp theme cache flush + * Success: The theme cache was flushed. + * + * @param string[] $args Positional arguments. Unused. + * @param array $assoc_args Associative arguments. Unused. + */ + public function flush( $args, $assoc_args ) { + wp_cache_flush_group( 'themes' ); + WP_CLI::success( 'The theme cache was flushed.' ); + } + + /** + * Clear cache for a specific theme. + * + * @param \WP_Theme $theme Theme object. + */ + private function clear_theme_cache( $theme ) { + $cache_hash = md5( $theme->get_theme_root() . '/' . $theme->get_stylesheet() ); + $cache_keys = [ 'theme', 'screenshot', 'headers', 'page_templates' ]; + + foreach ( $cache_keys as $key ) { + wp_cache_delete( $key . '-' . $cache_hash, 'themes' ); + } + } +} From 27a6daeb5a917b49d7a10ba4bb85c8625d6b9487 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 2 Feb 2026 20:31:40 -0500 Subject: [PATCH 3/7] Apply suggestions from code review --- features/theme-cache.feature | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/features/theme-cache.feature b/features/theme-cache.feature index ea62a3db6..ad712ced0 100644 --- a/features/theme-cache.feature +++ b/features/theme-cache.feature @@ -4,7 +4,7 @@ Feature: Manage theme cache Given a WP installation Scenario: Clear cache for a single theme - When I run `wp theme install twentytwentyfour --activate` + When I run `wp theme install twentytwentyfour --force --activate` Then STDOUT should contain: """ Success: @@ -17,13 +17,13 @@ Feature: Manage theme cache """ Scenario: Clear cache for multiple themes - When I run `wp theme install twentytwentythree` + When I run `wp theme install twentytwentythree --force ` Then STDOUT should contain: """ Success: """ - When I run `wp theme install twentytwentyfour` + When I run `wp theme install twentytwentyfour --force ` Then STDOUT should contain: """ Success: @@ -36,7 +36,7 @@ Feature: Manage theme cache """ Scenario: Clear cache for all themes - When I run `wp theme install twentytwentythree` + When I run `wp theme install twentytwentythree --force ` Then STDOUT should contain: """ Success: From 63c621166a84bfbc03f37d425898df87c58f1b44 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 3 Feb 2026 20:56:29 -0500 Subject: [PATCH 4/7] fixes --- features/theme-cache.feature | 10 +++++----- src/Theme_Cache_Command.php | 5 ++++- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/features/theme-cache.feature b/features/theme-cache.feature index ad712ced0..5101614c3 100644 --- a/features/theme-cache.feature +++ b/features/theme-cache.feature @@ -4,16 +4,16 @@ Feature: Manage theme cache Given a WP installation Scenario: Clear cache for a single theme - When I run `wp theme install twentytwentyfour --force --activate` + When I run `wp theme install twentytwenty --force --activate` Then STDOUT should contain: """ Success: """ - When I run `wp theme cache clear twentytwentyfour` + When I run `wp theme cache clear twentytwenty` Then STDOUT should be: """ - Success: Cleared cache for 'twentytwentyfour' theme. + Success: Cleared cache for 'twentytwenty' theme. """ Scenario: Clear cache for multiple themes @@ -23,13 +23,13 @@ Feature: Manage theme cache Success: """ - When I run `wp theme install twentytwentyfour --force ` + When I run `wp theme install twentytwenty --force ` Then STDOUT should contain: """ Success: """ - When I run `wp theme cache clear twentytwentythree twentytwentyfour` + When I run `wp theme cache clear twentytwentythree twentytwenty` Then STDOUT should be: """ Success: Cleared cache for 2 themes. diff --git a/src/Theme_Cache_Command.php b/src/Theme_Cache_Command.php index 5840f2707..51685e7f6 100644 --- a/src/Theme_Cache_Command.php +++ b/src/Theme_Cache_Command.php @@ -96,7 +96,10 @@ public function clear( $args, $assoc_args ) { * @param array $assoc_args Associative arguments. Unused. */ public function flush( $args, $assoc_args ) { - wp_cache_flush_group( 'themes' ); + // Only added in WordPress 6.1. + if ( function_exists( 'wp_cache_flush_group' ) ) { + wp_cache_flush_group( 'themes' ); + } WP_CLI::success( 'The theme cache was flushed.' ); } From 02bd9101341ab1784bd08ae5d898d55cca90529e Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 3 Feb 2026 21:09:20 -0500 Subject: [PATCH 5/7] Update src/Theme_Cache_Command.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/Theme_Cache_Command.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Theme_Cache_Command.php b/src/Theme_Cache_Command.php index 51685e7f6..e7b29be59 100644 --- a/src/Theme_Cache_Command.php +++ b/src/Theme_Cache_Command.php @@ -99,8 +99,20 @@ public function flush( $args, $assoc_args ) { // Only added in WordPress 6.1. if ( function_exists( 'wp_cache_flush_group' ) ) { wp_cache_flush_group( 'themes' ); + WP_CLI::success( 'The theme cache was flushed.' ); + return; + } + + // Fallback for WordPress versions prior to 6.1: clear cache for all themes. + if ( function_exists( 'wp_get_themes' ) ) { + $all_themes = wp_get_themes(); + foreach ( $all_themes as $theme ) { + $this->clear_theme_cache( $theme ); + } + WP_CLI::success( 'The theme cache was flushed.' ); + } else { + WP_CLI::warning( 'Your WordPress version does not support flushing the theme cache group.' ); } - WP_CLI::success( 'The theme cache was flushed.' ); } /** From 63b777275db766f24ebc341203c4f9dc51cceb5f Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 3 Feb 2026 21:09:30 -0500 Subject: [PATCH 6/7] Update src/Theme_Cache_Command.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/Theme_Cache_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Theme_Cache_Command.php b/src/Theme_Cache_Command.php index e7b29be59..db6b733b8 100644 --- a/src/Theme_Cache_Command.php +++ b/src/Theme_Cache_Command.php @@ -52,7 +52,7 @@ public function clear( $args, $assoc_args ) { if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) ) { $all_themes = wp_get_themes(); - foreach ( $all_themes as $theme_slug => $theme ) { + foreach ( $all_themes as $theme ) { $themes[] = $theme; } } else { From 37ac8dfb07db41b38276fc131becb489695c1b13 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 3 Feb 2026 21:10:10 -0500 Subject: [PATCH 7/7] Apply suggestions from code review --- features/theme-cache.feature | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/features/theme-cache.feature b/features/theme-cache.feature index 5101614c3..3f4fb4a2a 100644 --- a/features/theme-cache.feature +++ b/features/theme-cache.feature @@ -17,7 +17,7 @@ Feature: Manage theme cache """ Scenario: Clear cache for multiple themes - When I run `wp theme install twentytwentythree --force ` + When I run `wp theme install twentytwentythree --force` Then STDOUT should contain: """ Success: @@ -36,7 +36,7 @@ Feature: Manage theme cache """ Scenario: Clear cache for all themes - When I run `wp theme install twentytwentythree --force ` + When I run `wp theme install twentytwentythree --force` Then STDOUT should contain: """ Success: