1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\BlackBar; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\BlackBar\Profiler; |
6
|
|
|
|
7
|
|
|
final class Application |
8
|
|
|
{ |
9
|
|
|
const DEBUG = 'debug'; |
10
|
|
|
const ID = 'blackbar'; |
11
|
|
|
const LANG = '/languages/'; |
12
|
|
|
|
13
|
|
|
protected $errors = array(); |
14
|
|
|
protected $file; |
15
|
|
|
protected $profiler; |
16
|
|
|
|
17
|
|
|
public function __construct() |
18
|
|
|
{ |
19
|
|
|
$this->file = realpath( dirname( __DIR__ ).'/'.static::ID.'.php' ); |
20
|
|
|
$this->profiler = new Profiler; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return void |
25
|
|
|
* @action admin_enqueue_scripts |
26
|
|
|
* @action wp_enqueue_scripts |
27
|
|
|
*/ |
28
|
|
|
public function enqueueAssets() |
29
|
|
|
{ |
30
|
|
|
wp_enqueue_script( static::ID, $this->url( 'assets/main.js' )); |
31
|
|
|
wp_enqueue_style( static::ID, $this->url( 'assets/main.css' ), array( 'dashicons' )); |
32
|
|
|
wp_enqueue_style( static::ID.'-syntax', $this->url( 'assets/syntax.css' )); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param int $errno |
37
|
|
|
* @param string $errstr |
38
|
|
|
* @param string $errfile |
39
|
|
|
* @param int $errline |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
|
public function errorHandler( $errno, $errstr, $errfile, $errline ) |
43
|
|
|
{ |
44
|
|
|
$errorCodes = array( |
45
|
|
|
2 => 'Warning', |
46
|
|
|
8 => 'Notice', |
47
|
|
|
2048 => 'Strict', |
48
|
|
|
8192 => 'Deprecated', |
49
|
|
|
); |
50
|
|
|
$errname = array_key_exists( $errno, $errorCodes ) |
51
|
|
|
? $errorCodes[$errno] |
52
|
|
|
: 'Unknown'; |
53
|
|
|
$hash = md5( $errno.$errstr.$errfile.$errline ); |
54
|
|
|
if( array_key_exists( $hash, $this->errors )) { |
55
|
|
|
$this->errors[$hash]['count']++; |
56
|
|
|
} |
57
|
|
|
else { |
58
|
|
|
$this->errors[$hash] = array( |
59
|
|
|
"errno" => $errno, |
60
|
|
|
"message" => $errstr, |
61
|
|
|
"file" => $errfile, |
62
|
|
|
"name" => $errname, |
63
|
|
|
"line" => $errline, |
64
|
|
|
"count" => 0, |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $classes |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function filterBodyClasses( $classes ) |
74
|
|
|
{ |
75
|
|
|
return trim( $classes.' '.static::ID ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
public function filterNoconflictScripts( $scripts ) |
82
|
|
|
{ |
83
|
|
|
$scripts[] = static::ID; |
84
|
|
|
return $scripts; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function filterNoconflictStyles( $styles ) |
91
|
|
|
{ |
92
|
|
|
$styles[] = static::ID; |
93
|
|
|
$styles[] = static::ID.'-syntax'; |
94
|
|
|
return $styles; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
public function init() |
101
|
|
|
{ |
102
|
|
|
add_action( 'admin_enqueue_scripts', array( $this, 'enqueueAssets' )); |
103
|
|
|
add_action( 'admin_footer', array( $this, 'renderBar' )); |
104
|
|
|
add_action( 'plugins_loaded', array( $this, 'registerLanguages' )); |
105
|
|
|
add_action( 'wp_enqueue_scripts', array( $this, 'enqueueAssets' )); |
106
|
|
|
add_action( 'wp_footer', array( $this, 'renderBar' )); |
107
|
|
|
add_filter( 'admin_body_class', array( $this, 'filterBodyClasses' )); |
108
|
|
|
add_filter( 'all', array( $this, 'initProfiler' )); |
109
|
|
|
add_filter( 'gform_noconflict_scripts', array( $this, 'filterNoconflictScripts' )); |
110
|
|
|
add_filter( 'gform_noconflict_styles', array( $this, 'filterNoconflictStyles' )); |
111
|
|
|
apply_filters( 'debug', 'Profiler Started' ); |
112
|
|
|
apply_filters( 'debug', 'blackbar/profiler/noise' ); |
113
|
|
|
set_error_handler( array( $this, 'errorHandler' ), E_ALL|E_STRICT ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return void |
118
|
|
|
* @action all |
119
|
|
|
*/ |
120
|
|
|
public function initProfiler() |
121
|
|
|
{ |
122
|
|
|
if( func_get_arg(0) != static::DEBUG )return; |
123
|
|
|
$this->profiler->trace( func_get_arg(1) ); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
public function registerLanguages() |
130
|
|
|
{ |
131
|
|
|
load_plugin_textdomain( static::ID, false, plugin_basename( $this->path() ).static::LANG ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Render a view and pass any provided data to the view |
136
|
|
|
* |
137
|
|
|
* @param string $view |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
public function render( $view, array $data = array() ) |
141
|
|
|
{ |
142
|
|
|
$file = $this->path( sprintf( 'views/%s.php', str_replace( '.php', '', $view ))); |
143
|
|
|
if( !file_exists( $file ))return; |
144
|
|
|
extract( $data ); |
145
|
|
|
include $file; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return void |
150
|
|
|
* @action admin_footer |
151
|
|
|
* @action wp_footer |
152
|
|
|
*/ |
153
|
|
|
public function renderBar() |
154
|
|
|
{ |
155
|
|
|
apply_filters( 'debug', 'Profiler Stopped' ); |
156
|
|
|
$this->render( 'debug-bar', array( |
157
|
|
|
'blackbar' => $this, |
158
|
|
|
'errors' => $this->getErrors(), |
159
|
|
|
'errorsLabel' => $this->getErrorsLabel(), |
160
|
|
|
'profiler' => $this->profiler, |
161
|
|
|
'profilerLabel' => $this->getProfilerLabel(), |
162
|
|
|
'queries' => $this->getQueries(), |
163
|
|
|
'queriesLabel' => $this->getQueriesLabel(), |
164
|
|
|
'templates' => $this->getTemplates(), |
165
|
|
|
)); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
protected function convertToMiliseconds( $time, $decimals = 2 ) |
172
|
|
|
{ |
173
|
|
|
return number_format( $time * 1000, $decimals ); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return array |
178
|
|
|
*/ |
179
|
|
|
protected function getErrors() |
180
|
|
|
{ |
181
|
|
|
$errors = array(); |
182
|
|
|
foreach( $this->errors as $error ) { |
183
|
|
|
if( $error['errno'] == E_WARNING ) { |
184
|
|
|
$error['name'] = '<span class="glbb-error">'.$error['name'].'</span>'; |
185
|
|
|
} |
186
|
|
|
if( $error['count'] > 1 ) { |
187
|
|
|
$error['name'] .= ' ('.$error['count'].')'; |
188
|
|
|
} |
189
|
|
|
$errors[] = array( |
190
|
|
|
'name' => $error['name'], |
191
|
|
|
'message' => sprintf( __( '%s on line %s in file %s', 'blackbar' ), |
192
|
|
|
$error['message'], |
193
|
|
|
$error['line'], |
194
|
|
|
$error['file'] |
195
|
|
|
), |
196
|
|
|
); |
197
|
|
|
} |
198
|
|
|
return $errors; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return string |
203
|
|
|
*/ |
204
|
|
|
protected function getErrorsLabel() |
205
|
|
|
{ |
206
|
|
|
$warningCount = 0; |
207
|
|
|
foreach( $this->errors as $error ) { |
208
|
|
|
if( $error['errno'] == E_WARNING ) { |
209
|
|
|
$warningCount++; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
$errorCount = count( $this->errors ); |
213
|
|
|
$warnings = $warningCount > 0 ? sprintf( ', %d!', $warningCount ) : ''; |
214
|
|
|
return sprintf( __( 'Errors (%s)', 'blackbar' ), $errorCount.$warnings ); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return array |
219
|
|
|
*/ |
220
|
|
|
protected function getIncludedFiles() |
221
|
|
|
{ |
222
|
|
|
$includes = array_values( array_filter( get_included_files(), function( $include ) { |
223
|
|
|
$bool = strpos( $include, '/themes/' ) !== false |
224
|
|
|
&& strpos( $include, '/functions.php' ) === false; |
225
|
|
|
return (bool)apply_filters( 'blackbar/templates/include', $bool, $include ); |
226
|
|
|
})); |
227
|
|
|
return array_map( function( $key, $value ) { |
228
|
|
|
$value = str_replace( trailingslashit( WP_CONTENT_DIR ), '', $value ); |
229
|
|
|
return sprintf( '[%s] => %s', $key, $value ); |
230
|
|
|
}, array_keys( $includes ), $includes ); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
|
|
protected function getProfilerLabel() |
237
|
|
|
{ |
238
|
|
|
$profilerTime = $this->convertToMiliseconds( $this->profiler->getTotalTime(), 0 ); |
239
|
|
|
return sprintf( __( 'Profiler (%s ms)', 'blackbar' ), $profilerTime ); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return array |
244
|
|
|
*/ |
245
|
|
|
protected function getQueries() |
246
|
|
|
{ |
247
|
|
|
global $wpdb; |
248
|
|
|
$queries = array(); |
249
|
|
|
$search = array( |
250
|
|
|
'AND', 'FROM', 'GROUP BY', 'INNER JOIN', 'LIMIT', 'ON DUPLICATE KEY UPDATE', |
251
|
|
|
'ORDER BY', 'SET', 'WHERE', |
252
|
|
|
); |
253
|
|
|
$replace = array_map( function( $value ) { |
254
|
|
|
return PHP_EOL.$value; |
255
|
|
|
}, $search ); |
256
|
|
|
foreach( $wpdb->queries as $query ) { |
257
|
|
|
$miliseconds = number_format( round( $query[1] * 1000, 4 ), 4 ); |
258
|
|
|
$sql = preg_replace( '/\s\s+/', ' ', trim( $query[0] )); |
259
|
|
|
$sql = str_replace( PHP_EOL, ' ', $sql ); |
260
|
|
|
$sql = str_replace( $search, $replace, $sql ); |
261
|
|
|
$queries[] = array( |
262
|
|
|
'ms' => $miliseconds, |
263
|
|
|
'sql' => $sql, |
264
|
|
|
); |
265
|
|
|
} |
266
|
|
|
return $queries; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @return string |
271
|
|
|
*/ |
272
|
|
|
protected function getQueriesLabel() |
273
|
|
|
{ |
274
|
|
|
if( !SAVEQUERIES ) { |
275
|
|
|
return __( 'SQL', 'blackbar' ); |
276
|
|
|
} |
277
|
|
|
global $wpdb; |
278
|
|
|
$queryTime = 0; |
279
|
|
|
foreach( $wpdb->queries as $query ) { |
280
|
|
|
$queryTime += $query[1]; |
281
|
|
|
} |
282
|
|
|
$queriesCount = '<span class="glbb-queries-count">'.count( $wpdb->queries ).'</span>'; |
283
|
|
|
$queriesTime = '<span class="glbb-queries-time">'.$this->convertToMiliseconds( $queryTime ).'</span>'; |
284
|
|
|
return sprintf( __( 'SQL (%s queries in %s ms)', 'blackbar' ), $queriesCount, $queriesTime ); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @return void|string |
289
|
|
|
*/ |
290
|
|
|
protected function getTemplates() |
291
|
|
|
{ |
292
|
|
|
if( is_admin() )return; |
293
|
|
|
if( class_exists( '\GeminiLabs\Castor\Facades\Development' )) { |
294
|
|
|
ob_start(); |
295
|
|
|
\GeminiLabs\Castor\Facades\Development::printTemplatePaths(); |
296
|
|
|
return ob_get_clean(); |
297
|
|
|
} |
298
|
|
|
return '<pre>'.implode( PHP_EOL, $this->getIncludedFiles() ).'</pre>'; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @param string $file |
303
|
|
|
* @return string |
304
|
|
|
*/ |
305
|
|
|
protected function path( $file = '' ) |
306
|
|
|
{ |
307
|
|
|
return plugin_dir_path( $this->file ).ltrim( trim( $file ), '/' ); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @param string $path |
312
|
|
|
* @return string |
313
|
|
|
*/ |
314
|
|
|
protected function url( $path = '' ) |
315
|
|
|
{ |
316
|
|
|
return esc_url( plugin_dir_url( $this->file ).ltrim( trim( $path ), '/' )); |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|