1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\BlackBar; |
4
|
|
|
|
5
|
|
|
class Controller |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var Application |
9
|
|
|
*/ |
10
|
|
|
protected $app; |
11
|
|
|
|
12
|
|
|
public function __construct(Application $app) |
13
|
|
|
{ |
14
|
|
|
$this->app = $app; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @return void |
19
|
|
|
* @action admin_enqueue_scripts |
20
|
|
|
* @action wp_enqueue_scripts |
21
|
|
|
*/ |
22
|
|
|
public function enqueueAssets() |
23
|
|
|
{ |
24
|
|
|
wp_enqueue_script(Application::ID, $this->app->url('assets/main.js')); |
25
|
|
|
wp_enqueue_style(Application::ID, $this->app->url('assets/main.css'), array('dashicons')); |
26
|
|
|
wp_enqueue_style(Application::ID.'-syntax', $this->app->url('assets/syntax.css')); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $classes |
31
|
|
|
* @return string |
32
|
|
|
* @action admin_body_class |
33
|
|
|
*/ |
34
|
|
|
public function filterBodyClasses($classes) |
35
|
|
|
{ |
36
|
|
|
return trim($classes.' '.Application::ID); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return void |
41
|
|
|
* @filter all |
42
|
|
|
*/ |
43
|
|
|
public function initConsole() |
44
|
|
|
{ |
45
|
|
|
if (Application::CONSOLE_HOOK != func_get_arg(0)) { |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
$args = array_pad(func_get_args(), 4, ''); |
49
|
|
|
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 4); |
50
|
|
|
$entry = array_pop($backtrace); |
51
|
|
|
$location = $args[3]; |
52
|
|
|
if (empty(trim($location)) && array_key_exists('file', $entry)) { |
53
|
|
|
$path = explode(ABSPATH, $entry['file']); |
54
|
|
|
$location = sprintf('%s:%s', array_pop($path), $entry['line']); |
55
|
|
|
} |
56
|
|
|
$this->app->console->store($args[1], $args[2], '['.$location.'] '); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return void |
61
|
|
|
* @filter all |
62
|
|
|
*/ |
63
|
|
|
public function initProfiler() |
64
|
|
|
{ |
65
|
|
|
if (Application::PROFILER_HOOK != func_get_arg(0)) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
$this->app->profiler->trace(func_get_arg(1)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return void |
73
|
|
|
* @filter all |
74
|
|
|
*/ |
75
|
|
|
public function measureSlowActions() |
76
|
|
|
{ |
77
|
|
|
$this->app->actions->startTimer(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return void |
82
|
|
|
* @action plugins_loaded |
83
|
|
|
*/ |
84
|
|
|
public function registerLanguages() |
85
|
|
|
{ |
86
|
|
|
load_plugin_textdomain(Application::ID, false, |
87
|
|
|
plugin_basename($this->app->path()).Application::LANG |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return void |
93
|
|
|
* @action admin_footer |
94
|
|
|
* @action wp_footer |
95
|
|
|
*/ |
96
|
|
|
public function renderBar() |
97
|
|
|
{ |
98
|
|
|
apply_filters('debug', 'Profiler Stopped'); |
99
|
|
|
$this->app->render('debug-bar', array( |
100
|
|
|
'blackbar' => $this->app, |
101
|
|
|
'actions' => $this->app->actions, |
102
|
|
|
'actionsLabel' => $this->getSlowActionsLabel(), |
103
|
|
|
'consoleEntries' => $this->getConsoleEntries(), |
104
|
|
|
'consoleLabel' => $this->getConsoleLabel(), |
105
|
|
|
'profiler' => $this->app->profiler, |
106
|
|
|
'profilerLabel' => $this->getProfilerLabel(), |
107
|
|
|
'queries' => $this->getQueries(), |
108
|
|
|
'queriesLabel' => $this->getQueriesLabel(), |
109
|
|
|
'templates' => $this->getTemplates(), |
110
|
|
|
)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param int $time |
115
|
|
|
* @param int $decimals |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
protected function convertToMiliseconds($time, $decimals = 2) |
119
|
|
|
{ |
120
|
|
|
return number_format($time * 1000, $decimals); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
|
protected function getConsoleEntries() |
127
|
|
|
{ |
128
|
|
|
return array_merge($this->getErrors(), $this->app->console->entries); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
protected function getConsoleLabel() |
135
|
|
|
{ |
136
|
|
|
$class = ''; |
137
|
|
|
$entries = $this->getConsoleEntries(); |
138
|
|
|
$entryCount = count($entries); |
139
|
|
|
$errorCount = 0; |
140
|
|
|
$label = __('Console', 'blackbar'); |
141
|
|
|
foreach ($entries as $entry) { |
142
|
|
|
if (in_array($entry['errno'], [E_NOTICE, E_STRICT, E_DEPRECATED])) { |
143
|
|
|
$class = 'glbb-warning'; |
144
|
|
|
} |
145
|
|
|
if (in_array($entry['errno'], [E_WARNING])) { |
146
|
|
|
++$errorCount; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
if ($entryCount > 0) { |
150
|
|
|
$label .= sprintf(' (%d)', $entryCount); |
151
|
|
|
} |
152
|
|
|
if ($errorCount > 0) { |
153
|
|
|
$class = 'glbb-error'; |
154
|
|
|
$label .= sprintf(' (%d, %d!)', $entryCount, $errorCount); |
155
|
|
|
} |
156
|
|
|
return '<span class="'.$class.'">'.$label.'</span>'; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
|
|
protected function getErrors() |
163
|
|
|
{ |
164
|
|
|
$errors = array(); |
165
|
|
|
foreach ($this->app->errors as $error) { |
166
|
|
|
$class = 'glbb-info'; |
167
|
|
|
if (in_array($error['errno'], [E_NOTICE, E_STRICT, E_DEPRECATED])) { |
168
|
|
|
$class = 'glbb-warning'; |
169
|
|
|
} |
170
|
|
|
if (E_WARNING == $error['errno']) { |
171
|
|
|
$class = 'glbb-error'; |
172
|
|
|
} |
173
|
|
|
if ($error['count'] > 1) { |
174
|
|
|
$error['name'] .= ' ('.$error['count'].')'; |
175
|
|
|
} |
176
|
|
|
$errors[] = array( |
177
|
|
|
'errno' => $error['errno'], |
178
|
|
|
'name' => '<span class="'.$class.'">'.$error['name'].'</span>', |
179
|
|
|
'message' => sprintf(__('%s on line %s in file %s', 'blackbar'), |
180
|
|
|
$error['message'], |
181
|
|
|
$error['line'], |
182
|
|
|
$error['file'] |
183
|
|
|
), |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
return $errors; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
|
|
protected function getIncludedFiles() |
193
|
|
|
{ |
194
|
|
|
$files = array_values(array_filter(get_included_files(), function ($file) { |
195
|
|
|
$bool = false !== strpos($file, '/themes/') |
196
|
|
|
&& false === strpos($file, '/functions.php'); |
197
|
|
|
return (bool) apply_filters('blackbar/templates/file', $bool, $file); |
198
|
|
|
})); |
199
|
|
|
return array_map(function ($key, $value) { |
200
|
|
|
$value = str_replace(trailingslashit(WP_CONTENT_DIR), '', $value); |
201
|
|
|
return sprintf('[%s] => %s', $key, $value); |
202
|
|
|
}, array_keys($files), $files); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
|
|
protected function getProfilerLabel() |
209
|
|
|
{ |
210
|
|
|
$label = __('Profiler', 'blackbar'); |
211
|
|
|
$profilerTime = $this->convertToMiliseconds($this->app->profiler->getTotalTime(), 0); |
212
|
|
|
if ($profilerTime > 0) { |
213
|
|
|
$label .= sprintf(' (%s %s)', $profilerTime, __('ms', 'blackbar')); |
214
|
|
|
} |
215
|
|
|
return $label; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return array |
220
|
|
|
*/ |
221
|
|
|
protected function getQueries() |
222
|
|
|
{ |
223
|
|
|
global $wpdb; |
224
|
|
|
$queries = array(); |
225
|
|
|
$search = array( |
226
|
|
|
'AND', 'FROM', 'GROUP BY', 'INNER JOIN', 'LIMIT', 'ON DUPLICATE KEY UPDATE', |
227
|
|
|
'ORDER BY', 'SET', 'WHERE', |
228
|
|
|
); |
229
|
|
|
$replace = array_map(function ($value) { |
230
|
|
|
return PHP_EOL.$value; |
231
|
|
|
}, $search); |
232
|
|
|
foreach ($wpdb->queries as $query) { |
233
|
|
|
$miliseconds = number_format(round($query[1] * 1000, 4), 4); |
234
|
|
|
$sql = preg_replace('/\s\s+/', ' ', trim($query[0])); |
235
|
|
|
$sql = str_replace(PHP_EOL, ' ', $sql); |
236
|
|
|
$sql = str_replace($search, $replace, $sql); |
237
|
|
|
$queries[] = array( |
238
|
|
|
'ms' => $miliseconds, |
239
|
|
|
'sql' => $sql, |
240
|
|
|
); |
241
|
|
|
} |
242
|
|
|
return $queries; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @return string |
247
|
|
|
*/ |
248
|
|
|
protected function getQueriesLabel() |
249
|
|
|
{ |
250
|
|
|
$label = __('SQL', 'blackbar'); |
251
|
|
|
if (!SAVEQUERIES) { |
252
|
|
|
return $label; |
253
|
|
|
} |
254
|
|
|
global $wpdb; |
255
|
|
|
$queryTime = 0; |
256
|
|
|
foreach ($wpdb->queries as $query) { |
257
|
|
|
$queryTime += $query[1]; |
258
|
|
|
} |
259
|
|
|
$queriesCount = '<span class="glbb-queries-count">'.count($wpdb->queries).'</span>'; |
260
|
|
|
$queriesTime = '<span class="glbb-queries-time">'.$this->convertToMiliseconds($queryTime).'</span>'; |
261
|
|
|
return $label.sprintf(' (%s %s | %s %s)', $queriesCount, __('queries', 'blackbar'), $queriesTime, __('ms', 'blackbar')); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @return string |
266
|
|
|
*/ |
267
|
|
|
protected function getSlowActionsLabel() |
268
|
|
|
{ |
269
|
|
|
$label = __('Hooks', 'blackbar'); |
270
|
|
|
$totalTime = $this->convertToMiliseconds($this->app->actions->getTotalTime(), 0); |
271
|
|
|
if ($totalTime > 0) { |
272
|
|
|
$label .= sprintf(' (%s %s)', $totalTime, __('ms', 'blackbar')); |
273
|
|
|
} |
274
|
|
|
return $label; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @return void|string |
279
|
|
|
*/ |
280
|
|
|
protected function getTemplates() |
281
|
|
|
{ |
282
|
|
|
if (is_admin()) { |
283
|
|
|
return; |
284
|
|
|
} |
285
|
|
|
if (class_exists('\GeminiLabs\Castor\Facades\Development')) { |
286
|
|
|
ob_start(); |
287
|
|
|
\GeminiLabs\Castor\Facades\Development::printTemplatePaths(); |
288
|
|
|
return ob_get_clean(); |
289
|
|
|
} |
290
|
|
|
return '<pre>'.implode(PHP_EOL, $this->getIncludedFiles()).'</pre>'; |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|