forked from WordPress/theme-check
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp-cli.php
129 lines (117 loc) · 3.67 KB
/
wp-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
<?php
if ( !function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
require_once dirname(__FILE__).'/checkbase.php';
class Theme_Check_Commands extends WP_CLI_Command {
/**
* Theme check.
*
* ## OPTIONS
*
* [<theme>]
* : Theme name. Default: current theme
*
* [--format=<format>]
* : Accepted values: text, json. Default: text
*
* ## EXAMPLES
*
* wp theme-check twentyfourteen
*/
function __invoke($args, $assoc_args) {
// theme exists ?
$theme = isset($args[0]) ? $args[0] : get_stylesheet();
if ( empty($theme) || !wp_get_theme($theme)->exists() ) {
WP_CLI::error(sprintf('Theme not found "%s".', $theme));
exit;
}
global $themechecks, $checkcount, $data, $themename;
$themename = $theme;
$theme = get_theme_root( $theme ) . "/$theme";
$files = listdir( $theme );
$data = tc_get_theme_data( $theme . '/style.css' );
if ( $data[ 'Template' ] ) {
// This is a child theme, so we need to pull files from the parent, which HAS to be installed.
$parent = get_theme_root( $data[ 'Template' ] ) . '/' . $data['Template'];
if ( ! tc_get_theme_data( $parent . '/style.css' ) ) { // This should never happen but we will check while were here!
WP_CLI::error( sprintf(
__('Parent theme <strong>%1$s</strong> not found! You have to have parent AND child-theme installed!', 'theme-check'),
$data[ 'Template' ] ) );
exit;
}
$parent_data = tc_get_theme_data( $parent . '/style.css' );
$themename = basename( $parent );
$files = array_merge( listdir( $parent ), $files );
}
if ( !$files ) {
WP_CLI::error(sprintf('Not found : theme [%s]', $theme));
exit;
}
$php = $css = $other = array();
foreach( $files as $key => $filename ) {
switch ( substr( $filename, -4 ) ) {
case '.php':
$php[$filename] = php_strip_whitespace( $filename );
break;
case '.css':
$css[$filename] = file_get_contents( $filename );
break;
default:
$other[$filename] = ( ! is_dir($filename) ) ? file_get_contents( $filename ) : '';
}
}
// run the checks
$success = run_themechecks($php, $css, $other);
$errors = array();
foreach ($themechecks as $check) {
if ($check instanceof themecheck) {
$error = $check->getError();
$error = (array) $error;
if (!empty($error)) {
$errors = array_unique( array_merge( $error, $errors ) );
}
}
}
$error_counts = array();
if ( !empty($errors) ) {
foreach( $errors as $value ) {
$value = explode(':', $value, 2);
if ( !isset($value[1]) ) {
continue;
}
$error_kind = trim(strip_tags($value[0]));
if ( !isset($error_counts[$error_kind]) ) {
$error_counts[$error_kind] = array();
}
$error_counts[$error_kind][] = trim($value[1]);
}
}
$format = strtolower(isset($assoc_args['format']) ? $assoc_args['format'] : 'text');
switch ($format) {
case 'json':
$errors = array('Meta' => (array)$data, 'Errors' => $error_counts);
echo json_encode($errors)."\n";
break;
default:
echo "*** Theme Information ***\n";
foreach ( (array)$data as $key => $value ) {
printf( '[%s] : %s'."\n", $key, !is_array($value) ? $value : implode(',', $value) );
}
echo "\n*** Theme Errors ***\n";
foreach ( $error_counts as $kind => $value ) {
foreach ( $value as $msg ) {
printf('[%s] %s'."\n", $kind, trim(strip_tags($msg,'<a>')) );
}
}
echo "\n*** Error Count ***\n";
foreach ( $error_counts as $key => $count ) {
printf('[%s]'."\t".'%d'."\n", $key, count($count));
}
printf('[Summary]'."\t".'%d'."\n", count($errors));
break;
}
exit( count($errors) > 0 ? 1 : 0 );
}
}
WP_CLI::add_command('theme-check', 'Theme_Check_Commands');