-
Notifications
You must be signed in to change notification settings - Fork 57
/
theme-check-cli.php
261 lines (238 loc) · 7.94 KB
/
theme-check-cli.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
260
261
<?php
/**
* Run Envato Theme Check from the command line
*/
include 'checkbase.php';
include 'main.php';
class ThemeCheckCLI extends WP_CLI_Command {
function __construct()
{
parent::__construct();
$this->fetcher = new \WP_CLI\Fetchers\Theme;
}
/**
* Show a list of the current themes
*
* ## OPTIONS
*
* [--errors=<errors>]
* : set true to return only themes with errors, false for only without errors. Default: false
*
* [--allowed=<allowed>]
* : (Multisite) set true to return only themes allowed on this site, false for only those not allowed,
* 'site' for only site-allowed, 'network' for only network-allowed.
*
* [--blog_id=<id>]
* : (Multisite) Blog ID, if different than current
*
* @subcommand list
*/
public function list_themes( $args = array(), $assoc_args = array() )
{
$defaults = array( 'errors' => false, 'allowed' => null, 'blog_id' => 0 );
$args = wp_parse_args( $assoc_args, $defaults );
$args['errors'] = 'true' === $args['errors'];
if ( ( 'true' == $args['allowed'] ) || ( 'false' == $args['allowed'] ) )
$args['allowed'] = 'true' === $args['allowed'];
$themes = wp_get_themes( $args );
foreach ( $themes as $slug => $theme )
{
WP_CLI::line( $slug . ': ' . $theme->get('Name') );
}
}
/**
* Check a theme
*
* <theme>
* : The theme slug to check
*
* [--format=<format>]
* : set to true to format as json. Default: false
*
*/
public function check( $args = array(), $assoc_args = array() )
{
global $themechecks;
checkcount();
// prevent undefined index errors
if (!array_key_exists('format', $assoc_args)) {
$assoc_args['format'] = '';
}
// empty array for the json format
$required_json = array();
$warnings_json = array();
$recommended_json = array();
$errors_json = array();
$result_json = array();
$theme = $this->fetcher->get_check( $args[0] );
$files = $theme->get_files( null, -1 );
$css = $php = $other = array();
foreach( $files as $key => $filename )
{
if ( strpos( $filename, 'tgm-plugin-activation' ) === false && strpos( $filename, 'merlin' ) === false ) {
if ( substr( $filename, -4 ) == '.php' )
{
$php[ $filename ] = php_strip_whitespace( $filename );
}
else if ( substr( $filename, -4 ) == '.css' )
{
$css[ $filename ] = file_get_contents( $filename );
}
else
{
$other[ $filename ] = ( ! is_dir( $filename ) ) ? file_get_contents( $filename ) : '';
}
}
}
$success = run_themechecks($php, $css, $other);
$errors = array();
foreach ( $themechecks as $check )
{
if ( $check instanceof themecheck )
{
$error = $check->getError();
if ( ! empty( $error ) )
{
$errors = array_merge( $error, $errors );
}
}
}
$errors = array_unique( $errors );
$errors = array_map( 'strip_tags', $errors );
rsort( $errors );
// assume to pass unless we see a required or warning message.
$pass = true;
foreach ( $errors as $error )
{
list( $type, $message ) = explode( ':', $error, 2 );
if ( 'REQUIRED' == trim( $type ) )
{
if ( 'true' == $assoc_args['format'] )
{
array_push( $required_json, "REQUIRED: " . trim( $message ) );
}
else
{
WP_CLI::warning( '%rREQUIRED:%n ' . trim( $message ) );
}
$pass = false;
}
elseif ( 'WARNING' == trim( $type ) )
{
if ( 'true' == $assoc_args['format'] )
{
array_push( $warnings_json, "WARNING: " . trim( $message ) );
}
else
{
WP_CLI::warning( '%yWARNING:%n ' . trim( $message ) );
}
$pass = false;
}
elseif ( 'RECOMMENDED' == trim( $type ) )
{
if ( 'true' == $assoc_args['format'] )
{
array_push( $recommended_json, "RECOMMENDED: " . trim( $message ) );
}
else
{
WP_CLI::warning( '%cRECOMMENDED:%n ' . trim( $message ) );
}
}
else
{
if ( 'true' == $assoc_args['format'] )
{
array_push( $errors_json, "ERROR: " . trim( $error ) );
}
else
{
WP_CLI::warning( $error );
}
}
}
WP_CLI::line();
if ( empty( $errors ) )
{
if ( 'true' == $assoc_args['format'] )
{
array_push( $result_json, "SUCCESS" );
array_push( $result_json, "THEME PASSED REVIEW" );
}
else
{
WP_CLI::success( "THEME PASSED REVIEW" );
}
}
elseif ( true === $pass )
{
if ( 'true' == $assoc_args['format'] )
{
array_push( $result_json, "SUCCESS" );
array_push( $result_json, "THEME PASSED REVIEW WITH RECOMMENDED CHANGES" );
}
else
{
WP_CLI::success( "THEME PASSED REVIEW WITH RECOMMENDED CHANGES" );
}
}
else
{
if ( 'true' == $assoc_args['format'] )
{
array_push( $result_json, "FAIL" );
array_push( $result_json, "THEME DID NOT PASS REVIEW" );
}
else
{
WP_CLI::line( WP_CLI::colorize( "%RFAIL:%n THEME DID NOT PASS REVIEW" ) );
}
}
if ( 'true' == $assoc_args['format'] )
{
$output = array (
'result' => $result_json,
'required' => $required_json,
'recommended' => $recommended_json,
'warnings' => $warnings_json,
'errors' => $errors_json
);
echo htmlspecialchars_decode( json_encode( $output, JSON_UNESCAPED_SLASHES ) );
}
}
/**
* Check for the active theme
*
* [--format=<format>]
* : set to true to format as json. Default: false
*
*/
public function active( $args = array(), $assoc_args = array() )
{
$active_theme = wp_get_theme();
$theme_folder_name = $active_theme->template;
// Next four lines set up $themename and $data for wp cli version, as check_main is never run
global $themename, $data;
$themename = $theme_folder_name;
$theme = get_theme_root( $theme_folder_name ) . "/$theme_folder_name";
$data = tc_get_theme_data( $theme . '/style.css' );
$this->check( array($theme_folder_name), $assoc_args);
}
}
class ThemeCheckCLILogger extends WP_CLI\Loggers\Regular {
public function _line( $message, $label, $color, $handle = STDOUT )
{
if ( ! empty( $label ) )
{
$label = \cli\Colors::colorize( "$color$label:%n ", $this->in_color );
}
$this->write( $handle, "{$label}{$message}\n" );
}
function warning( $message )
{
$this->_line( WP_CLI::colorize( $message ), '', '', STDERR );
}
}
WP_CLI::set_logger( new ThemeCheckCLILogger( true ) );
WP_CLI::add_command( 'theme review', 'ThemeCheckCLI' );