-
Notifications
You must be signed in to change notification settings - Fork 0
/
theme-mentor.php
264 lines (218 loc) · 8.32 KB
/
theme-mentor.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
<?php
/**
* Plugin Name: Theme Mentor
* Description: Theme Mentor is a cousing of the Theme-Check plugin getting deeper into the code analysis.
* It's using different approaches to monitor for common problems regarding theme reviews from the
* WordPress Theme Reviewers Team. It is prone to fault analysis, so use only as a reference for improving
* your code base even further.
* Plugin URI: https://github.com/mpeshev/Theme-Mentor
* Version: 0.1
* Author: nofearinc
* Author URI: https://devrix.com/
* License: GPLv2 or later
*
*/
/*
* Copyright (C) 2013 Mario Peshev
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* */
define( 'TM_PLUGIN_PATH', trailingslashit( plugin_dir_path( __FILE__ ) ) );
define( 'TM_PLUGIN_URL', trailingslashit( plugin_dir_url( __FILE__ ) ) );
define( 'TM_INC_PATH', trailingslashit( TM_PLUGIN_PATH . 'inc' ) );
define( 'TM_INC_URL', trailingslashit( TM_PLUGIN_URL . 'inc' ) );
// if isset the option for complex checks, load them as well in the process of evaluation
/**
* The main class for the plugin, initializing everything needed and including all tests
*
* @author nofearinc
*
*/
class Theme_Mentor {
private $templates = array();
private $includes = array();
private $theme_path = '';
public static $validations = array();
public function __construct() {
add_action( 'admin_menu', array( $this, 'theme_mentor_page' ) );
add_action( 'dx_theme_mentor_before_tests_list', array( $this, 'display_theme_name_tested' ) );
// TODO: temporary
// $this->do_everything();
}
public function run_tests( ) {
// all the heavy lifting for picking up proper files from the theme folder
// for templates and includes, that is
$this->iterate_theme_folder( $this->theme_path, 0 );
// swap functions.php as it's include-alike
$functions_file = $this->theme_path . 'functions.php';
foreach( $this->templates as $index => $template ) {
if( $template === $functions_file )
unset( $this->templates[ $index ] );
}
$this->includes[] = $this->theme_path . 'functions.php';
// Include check files
include TM_INC_PATH . 'general-theme-validations.php';
$general_validations = new General_Theme_Validations();
// Include complex checks
include TM_PLUGIN_PATH . 'theme-mentor-executor.php';
$dir = 'inc/complex';
foreach (glob(dirname(__FILE__). "/{$dir}/*.php") as $file) {
include $file;
}
// iterate all templates
foreach( $this->templates as $index => $template ) {
// only unique theme stuff
$template_unique_only = str_replace( $this->theme_path , '', $template );
// read the files, keep the file number as it matters, you know
$file = file( $template , FILE_IGNORE_NEW_LINES );
if( false === $file ) { continue; }
// General
foreach( $general_validations->common_validations as $pattern => $message ) {
$this->iterate_data( $pattern, $message, $template_unique_only, $file );
}
foreach( $general_validations->template_validations as $pattern => $message ) {
$this->iterate_data( $pattern, $message, $template_unique_only, $file );
}
foreach( self::$validations as $validation ) {
$validation->crawl( $template, $file );
}
}
// iterate includes
foreach( $this->includes as $index => $functional ) {
// only unique theme stuff
$functional_unique_only = str_replace( $this->theme_path , '', $functional );
// read the files, keep the file number as it matters, you know
$file = file( $functional , FILE_IGNORE_NEW_LINES );
if( false === $file ) { continue; }
// General
foreach( $general_validations->common_validations as $pattern => $message ) {
$this->iterate_data( $pattern, $message, $functional_unique_only, $file );
}
foreach( $general_validations->include_validations as $pattern => $message ) {
$this->iterate_data( $pattern, $message, $functional_unique_only, $file );
}
}
// display complex validations errors
foreach( self::$validations as $validation ) {
$validation->execute();
$validation_description = $validation->get_description();
if( ! empty( $validation_description ) ) {
echo $validation_description;
}
}
}
/**
* Adapt the Theme Mentor page
*/
public function theme_mentor_page() {
$page = add_theme_page( 'Theme Mentor', 'Theme Mentor', 'manage_options', 'theme_mentor', array( $this, 'theme_mentor_page_cb' ) );
add_action( 'admin_print_styles-' . $page, array( $this, 'styles_theme_mentor' ) );
}
/**
* Admin page callback
*/
public function theme_mentor_page_cb() {
if( ! current_user_can( 'manage_options' ) ) {
wp_die( __( 'We all know you shouldn\'t be here', 'dx_theme_mentor' ) );
}
// get stylesheet to pick the selected theme
$stylesheet = get_stylesheet();
// default activated theme is selected atfirst
$selected = $stylesheet;
// list all themes
$themes = wp_get_themes();
echo '<div class="wrap">';
echo '<div id="icon-edit" class="icon32 icon32-base-template"><br></div>';
echo '<h2>' . __( 'Theme Mentor', 'dx_theme_mentor' ) . '</h2>';
do_action( 'dx_theme_mentor_before_tests_list' );
// is the form submitted
if( isset( $_POST['dx_theme'] ) ) {
$theme_name = $_POST['dx_theme'];
if( isset( $themes[$theme_name] ) ) {
$theme = $themes[$theme_name];
$this->theme_path = trailingslashit( $theme->get_template_directory() );
// selected is the last submitted to $_POST
$selected = $theme->get_stylesheet();
$this->run_tests();
}
}
do_action( 'dx_theme_mentor_before_admin_page' );
include_once 'inc/templates/admin-template.php';
do_action( 'dx_theme_mentor_after_admin_page' );
echo '</div>';
}
/**
* Iterate theme folder and assign templates and includes
* @param string $folder folder path
* @param int $level depth of the nesting
*/
public function iterate_theme_folder( $folder, $level = 0 ) {
// get all templates
$folder = trailingslashit( $folder );
$directory = dir( $folder );
while ( false !== ( $entry = $directory->read() ) ) {
// drop all empty folders, hidden folders/files and parents
if ( ( $entry[0] == "." ) ) continue;
// includes should be there
if( is_dir( $folder . $entry ) ) {
// iterate the next level
$this->iterate_theme_folder( $folder . $entry, $level + 1 );
} else {
// read only PHP files
if( substr( $entry , -4, 4 ) === '.php' ) {
if( $level === 0 ) {
// templates on level 0
$this->templates[] = $folder . $entry;
} else {
// includes
$this->includes[] = $folder . $entry;
}
}
}
}
}
/**
* Do the regex for the possibly dangerous snippets
*
* @param regex $pattern
* @param error message text $message
* @param path to file when something happened $file_path
* @param file to run after $file
*/
public function iterate_data( $pattern, $message, $file_path, $file ) {
$lines_found = preg_grep( $pattern, $file );
if( ! empty( $lines_found ) ) {
foreach( $lines_found as $line => $snippet ) {
printf( '<div class="tm_report_row"><span class="tm_message">%s</span> at file <span class="tm_file">%s</span>, line <span class="tm_line">%d</span>: <span class="tm_snippet">%s</span></div>',
$message, $file_path, $line + 1, esc_html( $snippet ) );
}
}
}
// list all errors
// lookout for the errors
// admin panel for theme list
/**
* Enqueue styles for admin
*/
public function styles_theme_mentor() {
wp_enqueue_style( 'theme-mentor', TM_PLUGIN_URL . 'css/theme-mentor.css' );
}
public function display_theme_name_tested( ) {
if( ! empty( $_POST['dx_theme'] ) ) {
$testing_word = __('Testing', 'dx_theme_mentor');
printf('<h2>%s %s...</h2>', $testing_word, $_POST['dx_theme']);
}
}
}
// Make things happen.
new Theme_Mentor();