forked from wp-cli/wp-cli
/
theme.php
259 lines (204 loc) · 5.85 KB
/
theme.php
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
WP_CLI::addCommand('theme', 'ThemeCommand');
/**
* Implement theme command
*
* @package wp-cli
* @subpackage commands/internals
*/
class ThemeCommand extends WP_CLI_Command {
protected $default_subcommand = 'status';
/**
* Get the status of one or all themes
*
* @param array $args
**/
public function status( $args = array() ) {
if ( empty( $args ) ) {
$this->list_themes();
return;
}
$name = $args[0];
$details = get_theme_data( $this->get_stylesheet_path( $name ) );
$status = $this->get_status( $details['Name'], true );
$version = $details[ 'Version' ];
if ( WP_CLI::get_update_status( $name, 'update_themes' ) )
$version .= ' (%gUpdate available%n)';
WP_CLI::line( 'Theme %9' . $name . '%n details:' );
WP_CLI::line( ' Name: ' . $details[ 'Name' ] );
WP_CLI::line( ' Status: ' . $status .'%n' );
WP_CLI::line( ' Version: ' . $version );
WP_CLI::line( ' Author: ' . strip_tags( $details[ 'Author' ] ) );
}
private function list_themes() {
// Print the header
WP_CLI::line( 'Installed themes:' );
foreach ( get_themes() as $theme ) {
if ( WP_CLI::get_update_status( $theme['Stylesheet'], 'update_themes' ) ) {
$line = ' %yU%n';
} else {
$line = ' ';
}
$line .= $this->get_status( $theme['Name'] ) . ' ' . $theme['Stylesheet'] . '%n';
WP_CLI::line( $line );
}
// Print the footer
WP_CLI::line();
$legend = array(
'I' => 'Inactive',
'%gA' => 'Active',
);
WP_CLI::legend( $legend );
}
private function get_status( $theme_name, $long = false ) {
if ( get_current_theme() == $theme_name ) {
$line = '%g';
$line .= $long ? 'Active' : 'A';
} else {
$line = $long ? 'Inactive' : 'I';
}
return $line;
}
/**
* Activate a theme
*
* @param array $args
**/
public function activate( $args = array() ) {
list( $stylesheet, $child ) = $this->parse_name( $args, __FUNCTION__ );
$details = get_theme_data( $stylesheet );
$parent = $details['Template'];
if ( empty( $parent ) ) {
$parent = $child;
} elseif ( !is_readable( $this->get_stylesheet_path( $parent ) ) ) {
WP_CLI::warning( 'parent theme not found' );
exit;
}
switch_theme( $parent, $child );
}
/**
* Get a theme path
*
* @param array $args
* @param array $assoc_args
*/
function path( $args, $assoc_args ) {
if ( empty( $args ) ) {
$path = WP_CONTENT_DIR . '/themes';
} else {
list( $stylesheet, $name ) = $this->parse_name( $args, __FUNCTION__ );
$path = $stylesheet;
if ( isset( $assoc_args['directory'] ) )
$path = dirname( $path );
}
WP_CLI::line( $path );
}
/**
* Update a theme
*
* @param array $args
* @param array $assoc_args
*/
function update( $args, $assoc_args ) {
// Force WordPress to update the theme list
wp_update_themes();
if ( !empty( $args ) && !isset( $assoc_args['all'] ) ) {
$this->update_single( $args, $assoc_args );
} else {
$this->update_multiple( $args, $assoc_args );
}
}
private function update_single( $args, $assoc_args ) {
list( $file, $name ) = $this->parse_name( $args, __FUNCTION__ );
WP_CLI::get_upgrader( 'Theme_Upgrader' )->upgrade( $name );
}
private function update_multiple( $args, $assoc_args ) {
$themes = get_themes();
// Grab all Themes that need Updates
// If we have no sub-arguments, add them to the output list.
$theme_list = "Available theme updates:";
$themes_to_update = array();
foreach ( $themes as $theme ) {
$file = $theme['Stylesheet'];
if ( WP_CLI::get_update_status( $file, 'update_themes' ) ) {
$themes_to_update[] = $file;
if ( empty( $assoc_args ) ) {
if ( false === strpos( $file, '/' ) )
$name = str_replace('.php', '', basename($file));
else
$name = dirname($file);
$theme_list .= "\n\t%y$name%n";
}
}
}
if ( empty( $themes_to_update ) ) {
WP_CLI::line( 'No theme updates available.' );
return;
}
// If --all, UPDATE ALL THE THINGS
if ( isset( $assoc_args['all'] ) ) {
$upgrader = WP_CLI::get_upgrader( 'Theme_Upgrader' );
$result = $upgrader->bulk_upgrade( $themes_to_update );
// Let the user know the results.
$num_to_update = count( $themes_to_update );
$num_updated = count( array_filter( $result ) );
$line = "Updated $num_updated/$num_to_update Themes.";
if ( $num_to_update == $num_updated ) {
WP_CLI::success( $line );
} else if ( $num_updated > 0 ) {
WP_CLI::warning( $line );
} else {
WP_CLI::error( $line );
}
// Else list themes that require updates
} else {
WP_CLI::line( $theme_list );
}
}
/**
* Delete a theme
*
* @param array $args
*/
function delete( $args ) {
list( $file, $name ) = $this->parse_name( $args, __FUNCTION__ );
$r = delete_theme( $name );
if ( is_wp_error( $r ) ) {
WP_CLI::error( $r );
}
}
protected function parse_name( $args, $subcommand ) {
if ( empty( $args ) ) {
WP_CLI::line( "usage: wp theme $subcommand <theme-name>" );
exit;
}
$name = $args[0];
$stylesheet = $this->get_stylesheet_path( $name );
if ( !is_readable( $stylesheet ) ) {
WP_CLI::error( "The theme '$name' could not be found." );
exit;
}
return array( $stylesheet, $name );
}
protected function get_stylesheet_path( $theme ) {
return WP_CONTENT_DIR . '/themes/' . $theme . '/style.css';
}
/**
* Help function for this command
*/
public static function help() {
WP_CLI::line( <<<EOB
usage: wp theme <sub-command> [<theme-name>]
or: wp theme path [<theme-name>] [--directory]
Available sub-commands:
status display status of all installed themes or of a particular theme
activate activate a particular theme
path print path to the theme's stylesheet
--directory get the path to the closest parent directory
update update a theme from wordpress.org
--all update all themes from wordpress.org
delete delete a theme
EOB
);
}
}