Skip to content

Commit 3d762f7

Browse files
Copilotswissspidy
andcommitted
Add Theme_Cache_Command with clear and flush methods
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 054741f commit 3d762f7

5 files changed

Lines changed: 197 additions & 1 deletion

File tree

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
"plugin update",
6262
"theme",
6363
"theme activate",
64+
"theme cache",
65+
"theme cache clear",
66+
"theme cache flush",
6467
"theme delete",
6568
"theme disable",
6669
"theme enable",

extension-command.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@
2222
WP_CLI::add_command( 'theme', 'Theme_Command' );
2323
WP_CLI::add_command( 'theme auto-updates', 'Theme_AutoUpdates_Command', $wpcli_extension_requires_wp_5_5 );
2424
WP_CLI::add_command( 'theme mod', 'Theme_Mod_Command' );
25+
WP_CLI::add_command( 'theme cache', 'Theme_Cache_Command' );

features/theme-cache.feature

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
Feature: Manage theme cache
2+
3+
Background:
4+
Given a WP installation
5+
6+
Scenario: Clear cache for a single theme
7+
When I run `wp theme install twentytwentyfour --activate`
8+
Then STDOUT should contain:
9+
"""
10+
Success:
11+
"""
12+
13+
When I run `wp theme cache clear twentytwentyfour`
14+
Then STDOUT should be:
15+
"""
16+
Success: Cleared cache for 'twentytwentyfour' theme.
17+
"""
18+
19+
Scenario: Clear cache for multiple themes
20+
When I run `wp theme install twentytwentythree`
21+
Then STDOUT should contain:
22+
"""
23+
Success:
24+
"""
25+
26+
When I run `wp theme install twentytwentyfour`
27+
Then STDOUT should contain:
28+
"""
29+
Success:
30+
"""
31+
32+
When I run `wp theme cache clear twentytwentythree twentytwentyfour`
33+
Then STDOUT should be:
34+
"""
35+
Success: Cleared cache for 2 themes.
36+
"""
37+
38+
Scenario: Clear cache for all themes
39+
When I run `wp theme install twentytwentythree`
40+
Then STDOUT should contain:
41+
"""
42+
Success:
43+
"""
44+
45+
When I run `wp theme cache clear --all`
46+
Then STDOUT should contain:
47+
"""
48+
Success: Cleared cache for
49+
"""
50+
And STDOUT should contain:
51+
"""
52+
themes.
53+
"""
54+
55+
Scenario: Clear cache for non-existent theme
56+
When I try `wp theme cache clear nonexistent`
57+
Then STDERR should contain:
58+
"""
59+
Warning: Theme 'nonexistent' not found.
60+
"""
61+
And the return code should be 1
62+
63+
Scenario: Clear cache with no arguments
64+
When I try `wp theme cache clear`
65+
Then STDERR should be:
66+
"""
67+
Error: Please specify one or more themes, or use --all.
68+
"""
69+
And the return code should be 1
70+
71+
Scenario: Flush the entire theme cache group
72+
When I run `wp theme cache flush`
73+
Then STDOUT should be:
74+
"""
75+
Success: The theme cache was flushed.
76+
"""

phpcs.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454
<!-- Exclude existing classes from the prefix rule as it would break BC to prefix them now. -->
5555
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound">
56-
<exclude-pattern>*/src/(Plugin_(AutoUpdates_)?|Theme_(Mod_|AutoUpdates_)?)Command\.php$</exclude-pattern>
56+
<exclude-pattern>*/src/(Plugin_(AutoUpdates_)?|Theme_(Mod_|AutoUpdates_|Cache_)?)Command\.php$</exclude-pattern>
5757
</rule>
5858

5959
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedNamespaceFound">

src/Theme_Cache_Command.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
/**
4+
* Manages theme cache.
5+
*
6+
* ## EXAMPLES
7+
*
8+
* # Clear cache for a specific theme
9+
* $ wp theme cache clear twentytwentyfour
10+
* Success: Cleared cache for 'twentytwentyfour' theme.
11+
*
12+
* # Flush the entire theme cache group
13+
* $ wp theme cache flush
14+
* Success: The theme cache was flushed.
15+
*/
16+
class Theme_Cache_Command extends WP_CLI_Command {
17+
18+
/**
19+
* Clears the cache for one or more themes.
20+
*
21+
* ## OPTIONS
22+
*
23+
* [<theme>...]
24+
* : One or more themes to clear the cache for.
25+
*
26+
* [--all]
27+
* : If set, clear cache for all installed themes.
28+
*
29+
* ## EXAMPLES
30+
*
31+
* # Clear cache for a single theme
32+
* $ wp theme cache clear twentytwentyfour
33+
* Success: Cleared cache for 'twentytwentyfour' theme.
34+
*
35+
* # Clear cache for multiple themes
36+
* $ wp theme cache clear twentytwentythree twentytwentyfour
37+
* Success: Cleared cache for 2 themes.
38+
*
39+
* # Clear cache for all themes
40+
* $ wp theme cache clear --all
41+
* Success: Cleared cache for all themes.
42+
*
43+
* @param string[] $args Positional arguments.
44+
* @param array{all?: bool} $assoc_args Associative arguments.
45+
*/
46+
public function clear( $args, $assoc_args ) {
47+
if ( ! \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) && empty( $args ) ) {
48+
WP_CLI::error( 'Please specify one or more themes, or use --all.' );
49+
}
50+
51+
$themes = [];
52+
53+
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) ) {
54+
$all_themes = wp_get_themes();
55+
foreach ( $all_themes as $theme_slug => $theme ) {
56+
$themes[] = $theme;
57+
}
58+
} else {
59+
foreach ( $args as $theme_slug ) {
60+
$theme = wp_get_theme( $theme_slug );
61+
if ( ! $theme->exists() ) {
62+
WP_CLI::warning( "Theme '{$theme_slug}' not found." );
63+
continue;
64+
}
65+
$themes[] = $theme;
66+
}
67+
}
68+
69+
if ( empty( $themes ) ) {
70+
WP_CLI::error( 'No valid themes to clear cache for.' );
71+
}
72+
73+
$cleared = 0;
74+
foreach ( $themes as $theme ) {
75+
$this->clear_theme_cache( $theme );
76+
++$cleared;
77+
}
78+
79+
if ( 1 === $cleared ) {
80+
WP_CLI::success( "Cleared cache for '{$themes[0]->get_stylesheet()}' theme." );
81+
} else {
82+
WP_CLI::success( "Cleared cache for {$cleared} themes." );
83+
}
84+
}
85+
86+
/**
87+
* Flushes the entire theme cache group.
88+
*
89+
* ## EXAMPLES
90+
*
91+
* # Flush the entire theme cache group
92+
* $ wp theme cache flush
93+
* Success: The theme cache was flushed.
94+
*
95+
* @param string[] $args Positional arguments. Unused.
96+
* @param array $assoc_args Associative arguments. Unused.
97+
*/
98+
public function flush( $args, $assoc_args ) {
99+
wp_cache_flush_group( 'themes' );
100+
WP_CLI::success( 'The theme cache was flushed.' );
101+
}
102+
103+
/**
104+
* Clear cache for a specific theme.
105+
*
106+
* @param \WP_Theme $theme Theme object.
107+
*/
108+
private function clear_theme_cache( $theme ) {
109+
$cache_hash = md5( $theme->get_theme_root() . '/' . $theme->get_stylesheet() );
110+
$cache_keys = [ 'theme', 'screenshot', 'headers', 'page_templates' ];
111+
112+
foreach ( $cache_keys as $key ) {
113+
wp_cache_delete( $key . '-' . $cache_hash, 'themes' );
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)