-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheckbase.php
357 lines (308 loc) · 11.5 KB
/
checkbase.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
// main global to hold our checks
global $themechecks;
$themechecks = array();
// counter for the checks
global $checkcount;
$checkcount = 0;
// interface that all checks should implement
interface CampusPress_themecheck
{
// should return true for good/okay/acceptable, false for bad/not-okay/unacceptable
public function check( $php_files, $css_files, $other_files );
// should return an array of strings explaining any problems found
public function getError();
}
// load all the checks in the checks directory
$dir = 'checks';
$files = array();
foreach (glob(dirname(__FILE__). "/{$dir}/*.php") as $file) {
$files[] = $file;
}
$files = apply_filters( 'campuspress_files_checks', $files );
foreach ( $files as $file ) {
include $file;
}
do_action('themecheck_checks_loaded');
function campuspress_run_themechecks($php, $css, $other) {
global $themechecks;
$pass = true;
foreach($themechecks as $check) {
if ($check instanceof CampusPress_themecheck) {
$pass = $pass & $check->check($php, $css, $other);
}
}
return $pass;
}
function campuspress_display_themechecks() {
$results = '';
global $themechecks;
$errors = array();
foreach ($themechecks as $check) {
if ($check instanceof CampusPress_themecheck) {
$error = $check->getError();
$error = (array) $error;
if (!empty($error)) {
$errors = array_unique( array_merge( $error, $errors ) );
}
}
}
if (!empty($errors)) {
rsort($errors);
foreach ($errors as $e) {
if ( defined( 'TC_TRAC' ) ) {
$results .= ( isset( $_POST['s_info'] ) && preg_match( '/INFO/', $e ) ) ? '' : '* ' . campuspress_tc_trac( $e ) . "\r\n";
} else {
$results .= ( isset( $_POST['s_info'] ) && preg_match( '/INFO/', $e ) ) ? '' : '<li>' . campuspress_tc_trac( $e ) . '</li>';
}
}
}
if ( defined( 'TC_TRAC' ) ) {
if ( defined( 'TC_PRE' ) ) $results = TC_PRE . $results;
$results = '<textarea cols=140 rows=20>' . strip_tags( $results );
if ( defined( 'TC_POST' ) ) $results = $results . TC_POST;
$results .= '</textarea>';
}
return $results;
}
function campuspress_checkcount() {
global $checkcount;
$checkcount++;
}
// some functions theme checks use
function campuspress_tc_grep( $error, $file ) {
if ( ! file_exists( $file ) ) {
return '';
}
$lines = file( $file, FILE_IGNORE_NEW_LINES ); // Read the theme file into an array
$line_index = 0;
$bad_lines = '';
foreach( $lines as $this_line ) {
if ( stristr ( $this_line, $error ) ) {
$error = str_replace( '"', "'", $error );
$this_line = str_replace( '"', "'", $this_line );
$error = ltrim( $error );
$pre = ( FALSE !== ( $pos = strpos( $this_line, $error ) ) ? substr( $this_line, 0, $pos ) : FALSE );
$pre = ltrim( htmlspecialchars( $pre ) );
$bad_lines .= "<pre class='tc-grep'>". __("Line ", "theme-check") . ( $line_index+1 ) . ": " . $pre . htmlspecialchars( substr( stristr( $this_line, $error ), 0, 75 ) ) . "</pre>";
}
$line_index++;
}
return str_replace( $error, '<span class="tc-grep">' . $error . '</span>', $bad_lines );
}
function campuspress_tc_grep_preg( $preg, $file ) {
if ( ! file_exists( $file ) ) {
return '';
}
$lines = file( $file, FILE_IGNORE_NEW_LINES ); // Read the theme file into an array
$line_index = 0;
$bad_lines = '';
foreach( $lines as $this_line ) {
if ( preg_match( $preg, $this_line, $matches, PREG_OFFSET_CAPTURE ) ) {
$this_line = str_replace( '"', "'", $this_line );
foreach ( $matches as $match ) {
$pre = substr( $this_line, 0, $match[1] );
$pre = ltrim( htmlspecialchars( $pre ) );
$characters_count = strlen( $match[0] );
$bad_lines .= "<pre class='tc-grep'>". __("Line ", "theme-check") . ( $line_index+1 ) . ": " . $pre;
$bad_lines .= '<span class="tc-grep">' . htmlspecialchars( substr( $this_line, $match[1], $characters_count ) ) . '</span>';
$bad_lines .= htmlspecialchars( substr( $this_line, $match[1] + $characters_count, 75 ) );
$bad_lines .= "</pre>";
}
}
$line_index++;
}
return $bad_lines;
}
function campuspress_tc_preg( $preg, $file ) {
if ( ! file_exists( $file ) ) {
return '';
}
$lines = file( $file, FILE_IGNORE_NEW_LINES ); // Read the theme file into an array
$line_index = 0;
$bad_lines = '';
$error = '';
foreach( $lines as $this_line ) {
if ( preg_match( $preg, $this_line, $matches ) ) {
$error = $matches[0];
$this_line = str_replace( '"', "'", $this_line );
$error = ltrim( $error );
$pre = '';
if ( !empty( $error ) ) {
$pre = ( FALSE !== ( $pos = strpos( $this_line, $error ) ) ? substr( $this_line, 0, $pos ) : FALSE );
}
$pre = ltrim( htmlspecialchars( $pre ) );
$bad_lines .= "<pre class='tc-grep'>" . __("Line ", "theme-check") . ( $line_index+1 ) . ": " . $pre . htmlspecialchars( substr( stristr( $this_line, $error ), 0, 75 ) ) . "</pre>";
}
$line_index++;
}
return str_replace( $error, '<span class="tc-grep">' . $error . '</span>', $bad_lines );
}
function campuspress_tc_strxchr($haystack, $needle, $l_inclusive = 0, $r_inclusive = 0){
if(strrpos($haystack, $needle)){
//Everything before last $needle in $haystack.
$left = substr($haystack, 0, strrpos($haystack, $needle) + $l_inclusive);
//Switch value of $r_inclusive from 0 to 1 and viceversa.
$r_inclusive = ($r_inclusive == 0) ? 1 : 0;
//Everything after last $needle in $haystack.
$right = substr(strrchr($haystack, $needle), $r_inclusive);
//Return $left and $right into an array.
return array($left, $right);
} else {
if(strrchr($haystack, $needle)) return array('', substr(strrchr($haystack, $needle), $r_inclusive));
else return false;
}
}
function campuspress_tc_filename( $file ) {
$filename = ( preg_match( '/themes\/[a-z0-9]*\/(.*)/', $file, $out ) ) ? $out[1] : basename( $file );
return $filename;
}
function campuspress_tc_trac( $e ) {
$trac_left = array( '<strong>', '</strong>' );
$trac_right= array( "'''", "'''" );
$html_link = '/<a\s?href\s?=\s?[\'|"]([^"|\']*)[\'|"]>([^<]*)<\/a>/i';
$html_new = '[$1 $2]';
if ( defined( 'TC_TRAC' ) ) {
$e = preg_replace( $html_link, $html_new, $e );
$e = str_replace( $trac_left, $trac_right, $e );
$e = preg_replace( '/<pre.*?>/', "\r\n{{{\r\n", $e );
$e = str_replace( '</pre>', "\r\n}}}\r\n", $e );
}
return $e;
}
function campuspress_listdir( $dir ) {
$files = array();
$dir_iterator = new RecursiveDirectoryIterator( $dir );
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $file) {
array_push( $files, $file->getPathname() );
}
return $files;
}
function campuspress_old_listdir( $start_dir='.' ) {
$files = array();
if ( is_dir( $start_dir ) ) {
$fh = opendir( $start_dir );
while ( ( $file = readdir( $fh ) ) !== false ) {
# loop through the files, skipping . and .., and recursing if necessary
if ( strcmp( $file, '.' )==0 || strcmp( $file, '..' )==0 ) continue;
$filepath = $start_dir . '/' . $file;
if ( is_dir( $filepath ) )
$files = array_merge( $files, campuspres_listdir( $filepath ) );
else
array_push( $files, $filepath );
}
closedir( $fh );
} else {
# false if the function was called with an invalid non-directory argument
$files = false;
}
return $files;
}
function campuspress_tc_print_r( $data ) {
$out = "\n<pre class='html-print-r'";
$out .= " style='border: 1px solid #ccc; padding: 7px;'>\n";
$out .= esc_html( print_r( $data, TRUE ) );
$out .= "\n</pre>\n";
echo $out;
}
function campuspress_get_theme_data_from_contents( $theme_data ) {
$themes_allowed_tags = array(
'a' => array(
'href' => array(),'title' => array()
),
'abbr' => array(
'title' => array()
),
'acronym' => array(
'title' => array()
),
'code' => array(),
'em' => array(),
'strong' => array()
);
$theme_data = str_replace ( '\r', '\n', $theme_data );
preg_match( '|^[ \t\/*#@]*Theme Name:(.*)$|mi', $theme_data, $theme_name );
preg_match( '|^[ \t\/*#@]*Theme URI:(.*)$|mi', $theme_data, $theme_uri );
preg_match( '|^[ \t\/*#@]*Description:(.*)$|mi', $theme_data, $description );
if ( preg_match( '|^[ \t\/*#@]*Author URI:(.*)$|mi', $theme_data, $author_uri ) )
$author_uri = esc_url( trim( $author_uri[1]) );
else
$author_uri = '';
if ( preg_match( '|^[ \t\/*#@]*Template:(.*)$|mi', $theme_data, $template ) )
$template = wp_kses( trim( $template[1] ), $themes_allowed_tags );
else
$template = '';
if ( preg_match( '|^[ \t\/*#@]*Version:(.*)|mi', $theme_data, $version ) )
$version = wp_kses( trim( $version[1] ), $themes_allowed_tags );
else
$version = '';
if ( preg_match('|^[ \t\/*#@]*Status:(.*)|mi', $theme_data, $status) )
$status = wp_kses( trim( $status[1] ), $themes_allowed_tags );
else
$status = 'publish';
if ( preg_match('|^[ \t\/*#@]*Tags:(.*)|mi', $theme_data, $tags) )
$tags = array_map( 'trim', explode( ',', wp_kses( trim( $tags[1] ), array() ) ) );
else
$tags = array();
$theme = ( isset( $theme_name[1] ) ) ? wp_kses( trim( $theme_name[1] ), $themes_allowed_tags ) : '';
$theme_uri = ( isset( $theme_uri[1] ) ) ? esc_url( trim( $theme_uri[1] ) ) : '';
$description = ( isset( $description[1] ) ) ? wp_kses( trim( $description[1] ), $themes_allowed_tags ) : '';
if ( preg_match( '|^[ \t\/*#@]*Author:(.*)$|mi', $theme_data, $author_name ) ) {
if ( empty( $author_uri ) ) {
$author = wp_kses( trim( $author_name[1] ), $themes_allowed_tags );
} else {
$author = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>', $author_uri, __( 'Visit author homepage' ), wp_kses( trim( $author_name[1] ), $themes_allowed_tags ) );
}
} else {
$author = __('Anonymous');
}
return array( 'Name' => $theme, 'Title' => $theme, 'URI' => $theme_uri, 'Description' => $description, 'Author' => $author, 'Author_URI' => $author_uri, 'Version' => $version, 'Template' => $template, 'Status' => $status, 'Tags' => $tags );
}
/*
* 3.3/3.4 compat
*
*/
function campuspress_tc_get_themes() {
if ( ! class_exists( 'WP_Theme' ) )
return get_themes();
global $wp_themes;
if ( isset( $wp_themes ) )
return $wp_themes;
$themes = wp_get_themes();
$wp_themes = array();
foreach ( $themes as $theme ) {
$name = $theme->get('Name');
if ( isset( $wp_themes[ $name ] ) )
$wp_themes[ $name . '/' . $theme->get_stylesheet() ] = $theme;
else
$wp_themes[ $name ] = $theme;
}
return $wp_themes;
}
function campuspress_tc_get_plugins() {
$plugins = get_plugins();
return $plugins;
}
function campuspress_tc_get_theme_data( $theme_file ) {
if ( ! class_exists( 'WP_Theme' ) )
return get_theme_data( $theme_file );
$theme = new WP_Theme( basename( dirname( $theme_file ) ), dirname( dirname( $theme_file ) ) );
$theme_data = array(
'Name' => $theme->get('Name'),
'URI' => $theme->display('ThemeURI', true, false),
'Description' => $theme->display('Description', true, false),
'Author' => $theme->display('Author', true, false),
'AuthorURI' => $theme->display('AuthorURI', true, false),
'Version' => $theme->get('Version'),
'Template' => $theme->get('Template'),
'Status' => $theme->get('Status'),
'Tags' => $theme->get('Tags'),
'Title' => $theme->get('Name'),
'AuthorName' => $theme->display('Author', false, false),
'License' => $theme->display( 'License', false, false),
'License URI' => $theme->display( 'License URI', false, false),
'Template Version' => $theme->display( 'Template Version', false, false)
);
return $theme_data;
}