-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathTheme_Cache_Command.php
More file actions
117 lines (108 loc) · 3.08 KB
/
Theme_Cache_Command.php
File metadata and controls
117 lines (108 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* Manages theme cache.
*
* ## EXAMPLES
*
* # Clear cache for a specific theme
* $ wp theme cache clear twentytwentyfour
* Success: Cleared cache for 'twentytwentyfour' theme.
*
* # Flush the entire theme cache group
* $ wp theme cache flush
* Success: The theme cache was flushed.
*/
class Theme_Cache_Command extends WP_CLI_Command {
/**
* Clears the cache for one or more themes.
*
* ## OPTIONS
*
* [<theme>...]
* : 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 ) {
$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 ) {
$theme->cache_delete();
++$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 ) {
// 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 ) {
$theme->cache_delete();
}
WP_CLI::success( 'The theme cache was flushed.' );
} else {
WP_CLI::warning( 'Your WordPress version does not support flushing the theme cache group.' );
}
}
}