|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\BlackBar; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\BlackBar\Application; |
|
6
|
|
|
|
|
7
|
|
|
class Controller |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var Application |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $app; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct( Application $app ) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->app = $app; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @return void |
|
21
|
|
|
* @action admin_enqueue_scripts |
|
22
|
|
|
* @action wp_enqueue_scripts |
|
23
|
|
|
*/ |
|
24
|
|
|
public function enqueueAssets() |
|
25
|
|
|
{ |
|
26
|
|
|
wp_enqueue_script( Application::ID, $this->app->url( 'assets/main.js' )); |
|
27
|
|
|
wp_enqueue_style( Application::ID, $this->app->url( 'assets/main.css' ), array( 'dashicons' )); |
|
28
|
|
|
wp_enqueue_style( Application::ID.'-syntax', $this->app->url( 'assets/syntax.css' )); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $classes |
|
33
|
|
|
* @return string |
|
34
|
|
|
* @action admin_body_class |
|
35
|
|
|
*/ |
|
36
|
|
|
public function filterBodyClasses( $classes ) |
|
37
|
|
|
{ |
|
38
|
|
|
return trim( $classes.' '.Application::ID ); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return array |
|
43
|
|
|
* @action gform_noconflict_scripts |
|
44
|
|
|
*/ |
|
45
|
|
|
public function filterNoconflictScripts( $scripts ) |
|
46
|
|
|
{ |
|
47
|
|
|
$scripts[] = Application::ID; |
|
48
|
|
|
return $scripts; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return array |
|
53
|
|
|
* @action gform_noconflict_styles |
|
54
|
|
|
*/ |
|
55
|
|
|
public function filterNoconflictStyles( $styles ) |
|
56
|
|
|
{ |
|
57
|
|
|
$styles[] = Application::ID; |
|
58
|
|
|
$styles[] = Application::ID.'-syntax'; |
|
59
|
|
|
return $styles; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return void |
|
64
|
|
|
* @filter all |
|
65
|
|
|
*/ |
|
66
|
|
|
public function initProfiler() |
|
67
|
|
|
{ |
|
68
|
|
|
if( func_get_arg(0) != Application::DEBUG )return; |
|
69
|
|
|
$this->app->profiler->trace( func_get_arg(1) ); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return void |
|
74
|
|
|
* @action plugins_loaded |
|
75
|
|
|
*/ |
|
76
|
|
|
public function registerLanguages() |
|
77
|
|
|
{ |
|
78
|
|
|
load_plugin_textdomain( Application::ID, false, |
|
|
|
|
|
|
79
|
|
|
plugin_basename( $this->app->path() ).Application::LANG |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return void |
|
85
|
|
|
* @action admin_footer |
|
86
|
|
|
* @action wp_footer |
|
87
|
|
|
*/ |
|
88
|
|
|
public function renderBar() |
|
89
|
|
|
{ |
|
90
|
|
|
apply_filters( 'debug', 'Profiler Stopped' ); |
|
91
|
|
|
$this->app->render( 'debug-bar', array( |
|
92
|
|
|
'blackbar' => $this->app, |
|
93
|
|
|
'errors' => $this->getErrors(), |
|
94
|
|
|
'errorsLabel' => $this->getErrorsLabel(), |
|
95
|
|
|
'profiler' => $this->app->profiler, |
|
96
|
|
|
'profilerLabel' => $this->getProfilerLabel(), |
|
97
|
|
|
'queries' => $this->getQueries(), |
|
98
|
|
|
'queriesLabel' => $this->getQueriesLabel(), |
|
99
|
|
|
'templates' => $this->getTemplates(), |
|
100
|
|
|
)); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function convertToMiliseconds( $time, $decimals = 2 ) |
|
107
|
|
|
{ |
|
108
|
|
|
return number_format( $time * 1000, $decimals ); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return array |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function getErrors() |
|
115
|
|
|
{ |
|
116
|
|
|
$errors = array(); |
|
117
|
|
|
foreach( $this->app->errors as $error ) { |
|
118
|
|
|
if( $error['errno'] == E_WARNING ) { |
|
119
|
|
|
$error['name'] = '<span class="glbb-error">'.$error['name'].'</span>'; |
|
120
|
|
|
} |
|
121
|
|
|
if( $error['count'] > 1 ) { |
|
122
|
|
|
$error['name'] .= ' ('.$error['count'].')'; |
|
123
|
|
|
} |
|
124
|
|
|
$errors[] = array( |
|
125
|
|
|
'name' => $error['name'], |
|
126
|
|
|
'message' => sprintf( __( '%s on line %s in file %s', 'blackbar' ), |
|
127
|
|
|
$error['message'], |
|
128
|
|
|
$error['line'], |
|
129
|
|
|
$error['file'] |
|
130
|
|
|
), |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
return $errors; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return string |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function getErrorsLabel() |
|
140
|
|
|
{ |
|
141
|
|
|
$warningCount = 0; |
|
142
|
|
|
foreach( $this->app->errors as $error ) { |
|
143
|
|
|
if( $error['errno'] == E_WARNING ) { |
|
144
|
|
|
$warningCount++; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
$errorCount = count( $this->app->errors ); |
|
148
|
|
|
$warnings = $warningCount > 0 ? sprintf( ', %d!', $warningCount ) : ''; |
|
149
|
|
|
return sprintf( __( 'Errors (%s)', 'blackbar' ), $errorCount.$warnings ); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @return array |
|
154
|
|
|
*/ |
|
155
|
|
|
protected function getIncludedFiles() |
|
156
|
|
|
{ |
|
157
|
|
|
$files = array_values( array_filter( get_included_files(), function( $file ) { |
|
158
|
|
|
$bool = strpos( $file, '/themes/' ) !== false |
|
159
|
|
|
&& strpos( $file, '/functions.php' ) === false; |
|
160
|
|
|
return (bool)apply_filters( 'blackbar/templates/file', $bool, $file ); |
|
161
|
|
|
})); |
|
162
|
|
|
return array_map( function( $key, $value ) { |
|
163
|
|
|
$value = str_replace( trailingslashit( WP_CONTENT_DIR ), '', $value ); |
|
164
|
|
|
return sprintf( '[%s] => %s', $key, $value ); |
|
165
|
|
|
}, array_keys( $files ), $files ); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @return string |
|
170
|
|
|
*/ |
|
171
|
|
|
protected function getProfilerLabel() |
|
172
|
|
|
{ |
|
173
|
|
|
$profilerTime = $this->convertToMiliseconds( $this->app->profiler->getTotalTime(), 0 ); |
|
174
|
|
|
return sprintf( __( 'Profiler (%s ms)', 'blackbar' ), $profilerTime ); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @return array |
|
179
|
|
|
*/ |
|
180
|
|
|
protected function getQueries() |
|
181
|
|
|
{ |
|
182
|
|
|
global $wpdb; |
|
183
|
|
|
$queries = array(); |
|
184
|
|
|
$search = array( |
|
185
|
|
|
'AND', 'FROM', 'GROUP BY', 'INNER JOIN', 'LIMIT', 'ON DUPLICATE KEY UPDATE', |
|
186
|
|
|
'ORDER BY', 'SET', 'WHERE', |
|
187
|
|
|
); |
|
188
|
|
|
$replace = array_map( function( $value ) { |
|
189
|
|
|
return PHP_EOL.$value; |
|
190
|
|
|
}, $search ); |
|
191
|
|
|
foreach( $wpdb->queries as $query ) { |
|
192
|
|
|
$miliseconds = number_format( round( $query[1] * 1000, 4 ), 4 ); |
|
193
|
|
|
$sql = preg_replace( '/\s\s+/', ' ', trim( $query[0] )); |
|
194
|
|
|
$sql = str_replace( PHP_EOL, ' ', $sql ); |
|
195
|
|
|
$sql = str_replace( $search, $replace, $sql ); |
|
196
|
|
|
$queries[] = array( |
|
197
|
|
|
'ms' => $miliseconds, |
|
198
|
|
|
'sql' => $sql, |
|
199
|
|
|
); |
|
200
|
|
|
} |
|
201
|
|
|
return $queries; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @return string |
|
206
|
|
|
*/ |
|
207
|
|
|
protected function getQueriesLabel() |
|
208
|
|
|
{ |
|
209
|
|
|
if( !SAVEQUERIES ) { |
|
210
|
|
|
return __( 'SQL', 'blackbar' ); |
|
211
|
|
|
} |
|
212
|
|
|
global $wpdb; |
|
213
|
|
|
$queryTime = 0; |
|
214
|
|
|
foreach( $wpdb->queries as $query ) { |
|
215
|
|
|
$queryTime += $query[1]; |
|
216
|
|
|
} |
|
217
|
|
|
$queriesCount = '<span class="glbb-queries-count">'.count( $wpdb->queries ).'</span>'; |
|
218
|
|
|
$queriesTime = '<span class="glbb-queries-time">'.$this->convertToMiliseconds( $queryTime ).'</span>'; |
|
219
|
|
|
return sprintf( __( 'SQL (%s queries in %s ms)', 'blackbar' ), $queriesCount, $queriesTime ); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* @return void|string |
|
224
|
|
|
*/ |
|
225
|
|
|
protected function getTemplates() |
|
226
|
|
|
{ |
|
227
|
|
|
if( is_admin() )return; |
|
228
|
|
|
if( class_exists( '\GeminiLabs\Castor\Facades\Development' )) { |
|
229
|
|
|
ob_start(); |
|
230
|
|
|
\GeminiLabs\Castor\Facades\Development::printTemplatePaths(); |
|
231
|
|
|
return ob_get_clean(); |
|
232
|
|
|
} |
|
233
|
|
|
return '<pre>'.implode( PHP_EOL, $this->getIncludedFiles() ).'</pre>'; |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|