forked from anhskohbo/wp-cli-themecheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.php
293 lines (240 loc) · 8.99 KB
/
command.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
/**
* WP CLI Themecheck Command.
*
* @author anhskohbo <[email protected]>
* @license MIT
* @link https://github.com/anhskohbo/wp-cli-themecheck
*/
if ( ! class_exists( 'WP_CLI' ) ) {
return;
}
use WP_CLI\Utils as Utils;
use Symfony\Component\Finder\Finder;
if ( ! class_exists( 'WP_CLI_Themecheck_Command' ) ) :
/**
* Themecheck_Command class.
*/
class WP_CLI_Themecheck_Command extends WP_CLI_Command {
/**
* Run themecheck in CLI.
*
* ## OPTIONS
*
* [--theme=<theme-name>]
* : Theme name to check.
*
* [--skip-info]
* : Suppress INFO.
*
* [--skip-recommended]
* : Suppress RECOMMENDED.
*
* [--interactive]
* : Prompt user for input (default).
*
* ## EXAMPLES
*
* $ wp themecheck --theme="twentysixteen"
*
* @when after_wp_load
*/
public function __invoke( $args, $assoc_args ) {
$interactive = (bool) Utils\get_flag_value( $assoc_args, 'interactive', true );
$this->before_themecheck( $interactive );
require_once WP_PLUGIN_DIR . '/theme-check/checkbase.php';
require_once WP_PLUGIN_DIR . '/theme-check/main.php';
if ( Utils\get_flag_value( $assoc_args, 'theme' ) ) {
$themename = Utils\get_flag_value( $assoc_args, 'theme' );
} elseif ( ! $interactive ) {
$themename = get_stylesheet();
} else {
$themename = $this->choices_theme();
}
// Find theme to check.
$theme = wp_get_theme( $themename );
if ( $theme->exists() ) {
$themepath = trailingslashit( $theme->get_template_directory() );
} elseif ( is_dir( $guest_path ) ) {
$themepath = trailingslashit( $guest_path );
} else {
WP_CLI::error( 'Unable find theme named "' . $themename . '"' );
}
// Run themecheck.
WP_CLI::line( "\n" );
WP_CLI::line( sprintf( '| Checking %s...', $theme->get( 'Name' ) ) );
WP_CLI::line( "\n" );
$is_success = $this->themecheck( $themename, $themepath, $theme );
// Display themecheck logs.
foreach ( $this->stack_errors as $log_level => $errors ) {
if ( Utils\get_flag_value( $assoc_args, 'skip-info' ) && 'INFO' === $log_level ) {
continue;
}
if ( Utils\get_flag_value( $assoc_args, 'skip-recommended' ) && 'RECOMMENDED' === $log_level ) {
continue;
}
foreach ( $errors as $error ) {
WP_CLI::line( $error . "\n" );
usleep( 50000 );
}
}
$required_count = ! empty( $this->stack_errors['REQUIRED'] ) ? count( $this->stack_errors['REQUIRED'] ) : 0;
$warning_count = ! empty( $this->stack_errors['WARNING'] ) ? count( $this->stack_errors['WARNING'] ) : 0;
$total_errors = $required_count + $warning_count;
if ( $is_success ) {
WP_CLI::success( sprintf( 'Congratulations! %s passed the tests!', $theme->get( 'Name' ) ) );
} else {
WP_CLI::error( sprintf( '%d error(s) found for %s!', $total_errors, $theme->get( 'Name' ) ), $total_errors );
}
}
/**
* Run themecheck.
*
* @param string $theme Theme name.
* @param string $path Them absolute path.
* @param WP_Theme $theme_object Theme object.
*/
private function themecheck( $theme, $path, $theme_object ) {
global $themechecks, $data, $themename;
$themename = $theme;
$datafiles = array( 'php' => array(), 'css' => array(), 'other' => array() );
// Find all files.
$finder = new Finder();
$finder->ignoreDotFiles( false )->ignoreVCS( false )->exclude( array( 'node_modules', 'tests' ) )->in( $path );
foreach ( $finder as $node ) {
$filename = $node->getRealPath();
switch ( $node->getExtension() ) {
case 'php':
$datafiles['php'][ $filename ] = tc_strip_comments( file_get_contents( $filename ) );
break;
case 'css':
$datafiles['css'][ $filename ] = file_get_contents( $filename );
break;
default:
$datafiles['other'][ $filename ] = $node->isDir() ? '' : file_get_contents( $filename );
break;
}
}
// Run Themecheck.
$data = array(
'Name' => $theme_object->get( 'Name' ),
'URI' => $theme_object->display( 'ThemeURI', true, false ),
'Description' => $theme_object->display( 'Description', true, false ),
'Author' => $theme_object->display( 'Author', true, false ),
'AuthorURI' => $theme_object->display( 'AuthorURI', true, false ),
'Version' => $theme_object->get( 'Version' ),
'Template' => $theme_object->get( 'Template' ),
'Status' => $theme_object->get( 'Status' ),
'Tags' => $theme_object->get( 'Tags' ),
'Title' => $theme_object->get( 'Name' ),
'AuthorName' => $theme_object->display( 'Author', false, false ),
'License' => $theme_object->display( 'License', false, false ),
'License URI' => $theme_object->display( 'License URI', false, false ),
'Template Version' => $theme_object->display( 'Template Version', false, false ),
);
$success = run_themechecks( $datafiles['php'], $datafiles['css'], $datafiles['other'], array(
'theme' => $theme_object,
'slug' => $themename,
) );
// Build logs report.
$log_pattern = '/(<span\sclass=.*>(REQUIRED|WARNING|RECOMMENDED|INFO)<\/span>\s?:)/i';
$stack_errors = array( 'REQUIRED' => array(), 'WARNING' => array(), 'RECOMMENDED' => array(), 'INFO' => array() );
foreach ( $themechecks as $check ) {
if ( ! $check instanceof Themecheck ) {
continue;
}
$errors = (array) $check->getError();
if ( empty( $errors ) ) {
continue;
}
foreach ( $errors as $error ) {
$log_level = '';
if ( preg_match( $log_pattern, $error, $matches ) ) {
$error = preg_replace( $log_pattern, '', $error );
$log_level = strtoupper( $matches[2] );
}
$error = $this->format_themecheck_result( $error, $log_level );
if ( isset( $stack_errors[ $log_level ] ) && ! in_array( $error, $stack_errors[ $log_level ], true ) ) {
$stack_errors[ $log_level ][] = $error;
}
}
}
$this->stack_errors = $stack_errors;
return $success;
}
/**
* Show a list to choices theme.
*
* @return string
*/
private function choices_theme() {
$themes = array();
foreach ( wp_get_themes() as $id => $theme ) {
$themes[ $id ] = $theme->get( 'Name' );
}
return cli\menu( $themes, get_stylesheet(), 'Choose a theme' );
}
/**
* Format themecheck result.
*
* @param string $string HTML report result.
* @param string $log_level Log level.
* @return string
*/
private function format_themecheck_result( $string, $log_level ) {
switch ( $log_level ) {
case 'REQUIRED':
$string = WP_CLI::colorize( '%R× ' . $log_level . ':%n' ) . $string;
break;
case 'WARNING':
$string = WP_CLI::colorize( '%R* ' . $log_level . ':%n' ) . $string;
break;
case 'RECOMMENDED':
$string = WP_CLI::colorize( '%G* ' . $log_level . ':%n' ) . $string;
break;
case 'INFO':
$string = WP_CLI::colorize( '%C* ' . $log_level . ':%n' ) . $string;
break;
default:
$string = $log_level ? ( '* ' . $log_level . $string ) : '* ' . $string;
break;
}
$string = str_replace( array( '<span>', '<span class="tc-grep">', "<span class='tc-grep'>", '</span>' ), '', $string );
$string = str_replace( array( '<br>', '<br />', '<br/>' ), "\n ", $string );
$string = str_replace( array( '<strong>', '<em>' ), WP_CLI::colorize( '%9"' ), $string );
$string = str_replace( array( '</strong>', '</em>' ), WP_CLI::colorize( '"%n' ), $string );
$string = str_replace( array( "<pre class='tc-grep'>", '<pre class="tc-grep">' ), WP_CLI::colorize( "\n %5" ), $string );
$string = str_replace( '</pre>', WP_CLI::colorize( '%n' ), $string );
$string = preg_replace( '/(<a\s?href\s?=\s?[\'|"]([^"|\']*)[\'|"]>([^<]*)<\/a>)/i', "\033[1m\$3\033[0m (\$2)", $string );
$string = str_replace( 'See See:', 'See:', $string ); // Correcly wrong spell.
return htmlspecialchars_decode( $string );
}
/**
* Make sure the Theme Check plugin is installed.
*
* @param bool $interactive Prompt user before exiting.
*/
private function before_themecheck( $interactive ) {
if ( class_exists( 'ThemeCheckMain' ) ) {
return true;
}
require_once ABSPATH . 'wp-admin/includes/plugin.php';
if ( array_key_exists( 'theme-check/theme-check.php', get_plugins() ) ) {
$command = array( array( 'plugin', 'activate', 'theme-check' ), array() );
$error_msg = "The Theme Check plugin must be activated.\n\n\tRun: wp plugin activate theme-check\n";
} else {
$command = array( array( 'plugin', 'install', 'theme-check' ), array( 'activate' => true ) );
$error_msg = "The Theme Check plugin must be installed and activated.\n\n\tRun: wp plugin install theme-check --activate\n";
}
WP_CLI::error( $error_msg, ! $interactive );
$choose = cli\choose( 'Do you want run above command right now' );
if ( 'y' !== $choose ) {
exit;
}
WP_CLI::line();
WP_CLI::run_command( $command[0], $command[1] );
WP_CLI::line();
}
}
WP_CLI::add_command( 'themecheck', 'WP_CLI_Themecheck_Command' );
endif;