@@ -4,86 +4,86 @@ |
||
4 | 4 | |
5 | 5 | class Profiler |
6 | 6 | { |
7 | - /** |
|
8 | - * This is the time that WordPress takes to execute the profiler hook. |
|
9 | - * @var float |
|
10 | - */ |
|
11 | - protected $noise; |
|
12 | - /** |
|
13 | - * @var float |
|
14 | - */ |
|
15 | - protected $start; |
|
16 | - /** |
|
17 | - * @var float |
|
18 | - */ |
|
19 | - protected $stop; |
|
20 | - /** |
|
21 | - * @var array |
|
22 | - */ |
|
23 | - protected $timers; |
|
7 | + /** |
|
8 | + * This is the time that WordPress takes to execute the profiler hook. |
|
9 | + * @var float |
|
10 | + */ |
|
11 | + protected $noise; |
|
12 | + /** |
|
13 | + * @var float |
|
14 | + */ |
|
15 | + protected $start; |
|
16 | + /** |
|
17 | + * @var float |
|
18 | + */ |
|
19 | + protected $stop; |
|
20 | + /** |
|
21 | + * @var array |
|
22 | + */ |
|
23 | + protected $timers; |
|
24 | 24 | |
25 | - public function __construct() |
|
26 | - { |
|
27 | - $this->noise = (float) 0; |
|
28 | - $this->start = (float) 0; |
|
29 | - $this->stop = (float) 0; |
|
30 | - $this->timers = []; |
|
31 | - } |
|
25 | + public function __construct() |
|
26 | + { |
|
27 | + $this->noise = (float) 0; |
|
28 | + $this->start = (float) 0; |
|
29 | + $this->stop = (float) 0; |
|
30 | + $this->timers = []; |
|
31 | + } |
|
32 | 32 | |
33 | - public function getMeasure(): array |
|
34 | - { |
|
35 | - return $this->timers; |
|
36 | - } |
|
33 | + public function getMeasure(): array |
|
34 | + { |
|
35 | + return $this->timers; |
|
36 | + } |
|
37 | 37 | |
38 | - public function getMemoryString(array $timer): string |
|
39 | - { |
|
40 | - return sprintf('%s kB', round($this->normalize($timer)['memory'] / 1000)); |
|
41 | - } |
|
38 | + public function getMemoryString(array $timer): string |
|
39 | + { |
|
40 | + return sprintf('%s kB', round($this->normalize($timer)['memory'] / 1000)); |
|
41 | + } |
|
42 | 42 | |
43 | - public function getNameString(array $timer): string |
|
44 | - { |
|
45 | - return $this->normalize($timer)['name']; |
|
46 | - } |
|
43 | + public function getNameString(array $timer): string |
|
44 | + { |
|
45 | + return $this->normalize($timer)['name']; |
|
46 | + } |
|
47 | 47 | |
48 | - public function getTimeString(array $timer): string |
|
49 | - { |
|
50 | - $timer = $this->normalize($timer); |
|
51 | - $index = array_search($timer['name'], array_column($this->timers, 'name')); |
|
52 | - $start = $this->start + ($index * $this->noise); |
|
53 | - $time = number_format(round(($timer['time'] - $start) * 1000, 4), 4); |
|
54 | - return sprintf('%s ms', $time); |
|
55 | - } |
|
48 | + public function getTimeString(array $timer): string |
|
49 | + { |
|
50 | + $timer = $this->normalize($timer); |
|
51 | + $index = array_search($timer['name'], array_column($this->timers, 'name')); |
|
52 | + $start = $this->start + ($index * $this->noise); |
|
53 | + $time = number_format(round(($timer['time'] - $start) * 1000, 4), 4); |
|
54 | + return sprintf('%s ms', $time); |
|
55 | + } |
|
56 | 56 | |
57 | - public function getTotalTime(): float |
|
58 | - { |
|
59 | - $totalNoise = (count($this->timers) - 1) * $this->noise; |
|
60 | - return $this->stop - $this->start - $totalNoise; |
|
61 | - } |
|
57 | + public function getTotalTime(): float |
|
58 | + { |
|
59 | + $totalNoise = (count($this->timers) - 1) * $this->noise; |
|
60 | + return $this->stop - $this->start - $totalNoise; |
|
61 | + } |
|
62 | 62 | |
63 | - public function trace(string $name): void |
|
64 | - { |
|
65 | - $microtime = microtime(true); // float |
|
66 | - if (!$this->start) { |
|
67 | - $this->start = $microtime; |
|
68 | - } |
|
69 | - if ('blackbar/profiler/noise' === $name) { |
|
70 | - $this->noise = $microtime - $this->start; |
|
71 | - return; |
|
72 | - } |
|
73 | - $this->timers[] = [ |
|
74 | - 'memory' => memory_get_peak_usage(), |
|
75 | - 'name' => $name, |
|
76 | - 'time' => $microtime, |
|
77 | - ]; |
|
78 | - $this->stop = $microtime; |
|
79 | - } |
|
63 | + public function trace(string $name): void |
|
64 | + { |
|
65 | + $microtime = microtime(true); // float |
|
66 | + if (!$this->start) { |
|
67 | + $this->start = $microtime; |
|
68 | + } |
|
69 | + if ('blackbar/profiler/noise' === $name) { |
|
70 | + $this->noise = $microtime - $this->start; |
|
71 | + return; |
|
72 | + } |
|
73 | + $this->timers[] = [ |
|
74 | + 'memory' => memory_get_peak_usage(), |
|
75 | + 'name' => $name, |
|
76 | + 'time' => $microtime, |
|
77 | + ]; |
|
78 | + $this->stop = $microtime; |
|
79 | + } |
|
80 | 80 | |
81 | - protected function normalize(array $timer): array |
|
82 | - { |
|
83 | - return wp_parse_args($timer, [ |
|
84 | - 'memory' => 0, |
|
85 | - 'name' => '', |
|
86 | - 'time' => (float) 0, |
|
87 | - ]); |
|
88 | - } |
|
81 | + protected function normalize(array $timer): array |
|
82 | + { |
|
83 | + return wp_parse_args($timer, [ |
|
84 | + 'memory' => 0, |
|
85 | + 'name' => '', |
|
86 | + 'time' => (float) 0, |
|
87 | + ]); |
|
88 | + } |
|
89 | 89 | } |
@@ -4,250 +4,250 @@ |
||
4 | 4 | |
5 | 5 | class Controller |
6 | 6 | { |
7 | - /** |
|
8 | - * @var Application |
|
9 | - */ |
|
10 | - protected $app; |
|
11 | - |
|
12 | - public function __construct() |
|
13 | - { |
|
14 | - $this->app = Application::load(); |
|
15 | - } |
|
16 | - |
|
17 | - /** |
|
18 | - * @action admin_enqueue_scripts |
|
19 | - * @action wp_enqueue_scripts |
|
20 | - */ |
|
21 | - public function enqueueAssets(): void |
|
22 | - { |
|
23 | - wp_enqueue_script(Application::ID, $this->app->url('assets/main.js')); |
|
24 | - wp_enqueue_style(Application::ID, $this->app->url('assets/main.css'), ['dashicons']); |
|
25 | - wp_enqueue_style(Application::ID.'-syntax', $this->app->url('assets/syntax.css')); |
|
26 | - } |
|
27 | - |
|
28 | - /** |
|
29 | - * @param string $classes |
|
30 | - * @action admin_body_class |
|
31 | - */ |
|
32 | - public function filterBodyClasses($classes): string |
|
33 | - { |
|
34 | - return trim((string) $classes.' '.Application::ID); |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * @filter all |
|
39 | - */ |
|
40 | - public function initConsole(): void |
|
41 | - { |
|
42 | - if (Application::CONSOLE_HOOK !== func_get_arg(0)) { |
|
43 | - return; |
|
44 | - } |
|
45 | - $args = array_pad(func_get_args(), 4, ''); |
|
46 | - $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 4); |
|
47 | - $entry = array_pop($backtrace); |
|
48 | - $message = $args[1]; |
|
49 | - $errno = $args[2]; |
|
50 | - $location = $args[3]; |
|
51 | - if (empty(trim($location)) && array_key_exists('file', $entry)) { |
|
52 | - $path = explode(ABSPATH, $entry['file']); |
|
53 | - $location = sprintf('%s:%s', array_pop($path), $entry['line']); |
|
54 | - } |
|
55 | - $this->app->console->store($message, $errno, '['.$location.'] '); |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * @filter all |
|
60 | - */ |
|
61 | - public function initProfiler(): void |
|
62 | - { |
|
63 | - if (Application::PROFILER_HOOK === func_get_arg(0)) { |
|
64 | - $this->app->profiler->trace(func_get_arg(1)); |
|
65 | - } |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * @filter all |
|
70 | - */ |
|
71 | - public function measureSlowActions(): void |
|
72 | - { |
|
73 | - $this->app->actions->startTimer(); |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * @action plugins_loaded |
|
78 | - */ |
|
79 | - public function registerLanguages(): void |
|
80 | - { |
|
81 | - load_plugin_textdomain(Application::ID, false, |
|
82 | - plugin_basename($this->app->path()).'/languages/' |
|
83 | - ); |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * @action admin_footer |
|
88 | - * @action wp_footer |
|
89 | - */ |
|
90 | - public function renderBar(): void |
|
91 | - { |
|
92 | - apply_filters('debug', 'Profiler Stopped'); |
|
93 | - $this->app->render('debug-bar', [ |
|
94 | - 'actions' => $this->app->actions, |
|
95 | - 'actionsLabel' => $this->getSlowActionsLabel(), |
|
96 | - 'blackbar' => $this->app, |
|
97 | - 'consoleEntries' => $this->getConsoleEntries(), |
|
98 | - 'consoleLabel' => $this->getConsoleLabel(), |
|
99 | - 'profiler' => $this->app->profiler, |
|
100 | - 'profilerLabel' => $this->getProfilerLabel(), |
|
101 | - 'queries' => $this->getQueries(), |
|
102 | - 'queriesLabel' => $this->getQueriesLabel(), |
|
103 | - 'templates' => $this->getTemplates(), |
|
104 | - ]); |
|
105 | - } |
|
106 | - |
|
107 | - protected function convertToMiliseconds(float $time, int $decimals = 2): string |
|
108 | - { |
|
109 | - return number_format($time * 1000, $decimals); |
|
110 | - } |
|
111 | - |
|
112 | - protected function getConsoleEntries(): array |
|
113 | - { |
|
114 | - return array_merge($this->getErrors(), $this->app->console->entries); |
|
115 | - } |
|
116 | - |
|
117 | - protected function getConsoleLabel(): string |
|
118 | - { |
|
119 | - $class = ''; |
|
120 | - $entries = $this->getConsoleEntries(); |
|
121 | - $entryCount = count($entries); |
|
122 | - $errorCount = 0; |
|
123 | - $label = __('Console', 'blackbar'); |
|
124 | - foreach ($entries as $entry) { |
|
125 | - if (in_array($entry['code'], [E_NOTICE, E_STRICT, E_DEPRECATED])) { |
|
126 | - $class = 'glbb-warning'; |
|
127 | - } |
|
128 | - if (in_array($entry['code'], [E_WARNING])) { |
|
129 | - ++$errorCount; |
|
130 | - } |
|
131 | - } |
|
132 | - if ($entryCount > 0) { |
|
133 | - $label .= sprintf(' (%d)', $entryCount); |
|
134 | - } |
|
135 | - if ($errorCount > 0) { |
|
136 | - $class = 'glbb-error'; |
|
137 | - $label .= sprintf(' (%d, %d!)', $entryCount, $errorCount); |
|
138 | - } |
|
139 | - return sprintf('<span class="%s">%s</span>', $class, $label); |
|
140 | - } |
|
141 | - |
|
142 | - protected function getErrors(): array |
|
143 | - { |
|
144 | - $errors = []; |
|
145 | - foreach ($this->app->errors as $error) { |
|
146 | - $class = 'glbb-info'; |
|
147 | - if (in_array($error['code'], [E_NOTICE, E_STRICT, E_DEPRECATED])) { |
|
148 | - $class = 'glbb-warning'; |
|
149 | - } |
|
150 | - if (E_WARNING == $error['code']) { |
|
151 | - $class = 'glbb-error'; |
|
152 | - } |
|
153 | - if ($error['count'] > 1) { |
|
154 | - $error['name'] .= ' ('.$error['count'].')'; |
|
155 | - } |
|
156 | - $errors[] = [ |
|
157 | - 'code' => $error['code'], |
|
158 | - 'name' => '<span class="'.$class.'">'.$error['name'].'</span>', |
|
159 | - 'message' => sprintf(__('%s on line %s in file %s', 'blackbar'), |
|
160 | - $error['message'], |
|
161 | - $error['line'], |
|
162 | - $error['file'] |
|
163 | - ), |
|
164 | - ]; |
|
165 | - } |
|
166 | - return $errors; |
|
167 | - } |
|
168 | - |
|
169 | - protected function getIncludedFiles(): array |
|
170 | - { |
|
171 | - $files = array_values(array_filter(get_included_files(), function ($file) { |
|
172 | - $bool = false !== strpos($file, '/themes/') && false === strpos($file, '/functions.php'); |
|
173 | - return (bool) apply_filters('blackbar/templates/file', $bool, $file); |
|
174 | - })); |
|
175 | - return array_map(function ($key, $value) { |
|
176 | - $value = str_replace(trailingslashit(WP_CONTENT_DIR), '', $value); |
|
177 | - return sprintf('[%s] => %s', $key, $value); |
|
178 | - }, array_keys($files), $files); |
|
179 | - } |
|
180 | - |
|
181 | - protected function getProfilerLabel(): string |
|
182 | - { |
|
183 | - $label = __('Profiler', 'blackbar'); |
|
184 | - $profilerTime = $this->convertToMiliseconds($this->app->profiler->getTotalTime(), 0); |
|
185 | - if ($profilerTime > 0) { |
|
186 | - $label .= sprintf(' (%s %s)', $profilerTime, __('ms', 'blackbar')); |
|
187 | - } |
|
188 | - return $label; |
|
189 | - } |
|
190 | - |
|
191 | - protected function getQueries(): array |
|
192 | - { |
|
193 | - global $wpdb; |
|
194 | - $queries = []; |
|
195 | - $search = [ |
|
196 | - 'AND', 'FROM', 'GROUP BY', 'INNER JOIN', 'LEFT JOIN', 'LIMIT', |
|
197 | - 'ON DUPLICATE KEY UPDATE', 'ORDER BY', 'OFFSET', ' SET', 'WHERE', |
|
198 | - ]; |
|
199 | - $replace = array_map(function ($value) { |
|
200 | - return PHP_EOL.$value; |
|
201 | - }, $search); |
|
202 | - foreach ($wpdb->queries as $query) { |
|
203 | - $miliseconds = number_format(round($query[1] * 1000, 4), 4); |
|
204 | - $sql = preg_replace('/\s\s+/', ' ', trim($query[0])); |
|
205 | - $sql = str_replace(PHP_EOL, ' ', $sql); |
|
206 | - $sql = str_replace($search, $replace, $sql); |
|
207 | - $queries[] = [ |
|
208 | - 'ms' => $miliseconds, |
|
209 | - 'sql' => $sql, |
|
210 | - ]; |
|
211 | - } |
|
212 | - return $queries; |
|
213 | - } |
|
214 | - |
|
215 | - protected function getQueriesLabel(): string |
|
216 | - { |
|
217 | - $label = __('SQL', 'blackbar'); |
|
218 | - if (!SAVEQUERIES) { |
|
219 | - return $label; |
|
220 | - } |
|
221 | - global $wpdb; |
|
222 | - $queryTime = 0; |
|
223 | - foreach ($wpdb->queries as $query) { |
|
224 | - $queryTime += $query[1]; |
|
225 | - } |
|
226 | - $queriesCount = sprintf('<span class="glbb-queries-count">%s</span>', count($wpdb->queries)); |
|
227 | - $queriesTime = sprintf('<span class="glbb-queries-time">%s</span>', $this->convertToMiliseconds((float) $queryTime)); |
|
228 | - return $label.sprintf(' (%s %s | %s %s)', $queriesCount, __('queries', 'blackbar'), $queriesTime, __('ms', 'blackbar')); |
|
229 | - } |
|
230 | - |
|
231 | - protected function getSlowActionsLabel(): string |
|
232 | - { |
|
233 | - $label = __('Hooks', 'blackbar'); |
|
234 | - $totalTime = $this->convertToMiliseconds($this->app->actions->getTotalTime(), 0); |
|
235 | - if ($totalTime > 0) { |
|
236 | - $label .= sprintf(' (%s %s)', $totalTime, __('ms', 'blackbar')); |
|
237 | - } |
|
238 | - return $label; |
|
239 | - } |
|
240 | - |
|
241 | - protected function getTemplates(): string |
|
242 | - { |
|
243 | - if (is_admin()) { |
|
244 | - return ''; |
|
245 | - } |
|
246 | - if (class_exists('\GeminiLabs\Castor\Facades\Development')) { |
|
247 | - ob_start(); |
|
248 | - \GeminiLabs\Castor\Facades\Development::printTemplatePaths(); |
|
249 | - return ob_get_clean(); |
|
250 | - } |
|
251 | - return sprintf('<pre>%s</pre>', implode(PHP_EOL, $this->getIncludedFiles())); |
|
252 | - } |
|
7 | + /** |
|
8 | + * @var Application |
|
9 | + */ |
|
10 | + protected $app; |
|
11 | + |
|
12 | + public function __construct() |
|
13 | + { |
|
14 | + $this->app = Application::load(); |
|
15 | + } |
|
16 | + |
|
17 | + /** |
|
18 | + * @action admin_enqueue_scripts |
|
19 | + * @action wp_enqueue_scripts |
|
20 | + */ |
|
21 | + public function enqueueAssets(): void |
|
22 | + { |
|
23 | + wp_enqueue_script(Application::ID, $this->app->url('assets/main.js')); |
|
24 | + wp_enqueue_style(Application::ID, $this->app->url('assets/main.css'), ['dashicons']); |
|
25 | + wp_enqueue_style(Application::ID.'-syntax', $this->app->url('assets/syntax.css')); |
|
26 | + } |
|
27 | + |
|
28 | + /** |
|
29 | + * @param string $classes |
|
30 | + * @action admin_body_class |
|
31 | + */ |
|
32 | + public function filterBodyClasses($classes): string |
|
33 | + { |
|
34 | + return trim((string) $classes.' '.Application::ID); |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * @filter all |
|
39 | + */ |
|
40 | + public function initConsole(): void |
|
41 | + { |
|
42 | + if (Application::CONSOLE_HOOK !== func_get_arg(0)) { |
|
43 | + return; |
|
44 | + } |
|
45 | + $args = array_pad(func_get_args(), 4, ''); |
|
46 | + $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 4); |
|
47 | + $entry = array_pop($backtrace); |
|
48 | + $message = $args[1]; |
|
49 | + $errno = $args[2]; |
|
50 | + $location = $args[3]; |
|
51 | + if (empty(trim($location)) && array_key_exists('file', $entry)) { |
|
52 | + $path = explode(ABSPATH, $entry['file']); |
|
53 | + $location = sprintf('%s:%s', array_pop($path), $entry['line']); |
|
54 | + } |
|
55 | + $this->app->console->store($message, $errno, '['.$location.'] '); |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * @filter all |
|
60 | + */ |
|
61 | + public function initProfiler(): void |
|
62 | + { |
|
63 | + if (Application::PROFILER_HOOK === func_get_arg(0)) { |
|
64 | + $this->app->profiler->trace(func_get_arg(1)); |
|
65 | + } |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * @filter all |
|
70 | + */ |
|
71 | + public function measureSlowActions(): void |
|
72 | + { |
|
73 | + $this->app->actions->startTimer(); |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * @action plugins_loaded |
|
78 | + */ |
|
79 | + public function registerLanguages(): void |
|
80 | + { |
|
81 | + load_plugin_textdomain(Application::ID, false, |
|
82 | + plugin_basename($this->app->path()).'/languages/' |
|
83 | + ); |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * @action admin_footer |
|
88 | + * @action wp_footer |
|
89 | + */ |
|
90 | + public function renderBar(): void |
|
91 | + { |
|
92 | + apply_filters('debug', 'Profiler Stopped'); |
|
93 | + $this->app->render('debug-bar', [ |
|
94 | + 'actions' => $this->app->actions, |
|
95 | + 'actionsLabel' => $this->getSlowActionsLabel(), |
|
96 | + 'blackbar' => $this->app, |
|
97 | + 'consoleEntries' => $this->getConsoleEntries(), |
|
98 | + 'consoleLabel' => $this->getConsoleLabel(), |
|
99 | + 'profiler' => $this->app->profiler, |
|
100 | + 'profilerLabel' => $this->getProfilerLabel(), |
|
101 | + 'queries' => $this->getQueries(), |
|
102 | + 'queriesLabel' => $this->getQueriesLabel(), |
|
103 | + 'templates' => $this->getTemplates(), |
|
104 | + ]); |
|
105 | + } |
|
106 | + |
|
107 | + protected function convertToMiliseconds(float $time, int $decimals = 2): string |
|
108 | + { |
|
109 | + return number_format($time * 1000, $decimals); |
|
110 | + } |
|
111 | + |
|
112 | + protected function getConsoleEntries(): array |
|
113 | + { |
|
114 | + return array_merge($this->getErrors(), $this->app->console->entries); |
|
115 | + } |
|
116 | + |
|
117 | + protected function getConsoleLabel(): string |
|
118 | + { |
|
119 | + $class = ''; |
|
120 | + $entries = $this->getConsoleEntries(); |
|
121 | + $entryCount = count($entries); |
|
122 | + $errorCount = 0; |
|
123 | + $label = __('Console', 'blackbar'); |
|
124 | + foreach ($entries as $entry) { |
|
125 | + if (in_array($entry['code'], [E_NOTICE, E_STRICT, E_DEPRECATED])) { |
|
126 | + $class = 'glbb-warning'; |
|
127 | + } |
|
128 | + if (in_array($entry['code'], [E_WARNING])) { |
|
129 | + ++$errorCount; |
|
130 | + } |
|
131 | + } |
|
132 | + if ($entryCount > 0) { |
|
133 | + $label .= sprintf(' (%d)', $entryCount); |
|
134 | + } |
|
135 | + if ($errorCount > 0) { |
|
136 | + $class = 'glbb-error'; |
|
137 | + $label .= sprintf(' (%d, %d!)', $entryCount, $errorCount); |
|
138 | + } |
|
139 | + return sprintf('<span class="%s">%s</span>', $class, $label); |
|
140 | + } |
|
141 | + |
|
142 | + protected function getErrors(): array |
|
143 | + { |
|
144 | + $errors = []; |
|
145 | + foreach ($this->app->errors as $error) { |
|
146 | + $class = 'glbb-info'; |
|
147 | + if (in_array($error['code'], [E_NOTICE, E_STRICT, E_DEPRECATED])) { |
|
148 | + $class = 'glbb-warning'; |
|
149 | + } |
|
150 | + if (E_WARNING == $error['code']) { |
|
151 | + $class = 'glbb-error'; |
|
152 | + } |
|
153 | + if ($error['count'] > 1) { |
|
154 | + $error['name'] .= ' ('.$error['count'].')'; |
|
155 | + } |
|
156 | + $errors[] = [ |
|
157 | + 'code' => $error['code'], |
|
158 | + 'name' => '<span class="'.$class.'">'.$error['name'].'</span>', |
|
159 | + 'message' => sprintf(__('%s on line %s in file %s', 'blackbar'), |
|
160 | + $error['message'], |
|
161 | + $error['line'], |
|
162 | + $error['file'] |
|
163 | + ), |
|
164 | + ]; |
|
165 | + } |
|
166 | + return $errors; |
|
167 | + } |
|
168 | + |
|
169 | + protected function getIncludedFiles(): array |
|
170 | + { |
|
171 | + $files = array_values(array_filter(get_included_files(), function ($file) { |
|
172 | + $bool = false !== strpos($file, '/themes/') && false === strpos($file, '/functions.php'); |
|
173 | + return (bool) apply_filters('blackbar/templates/file', $bool, $file); |
|
174 | + })); |
|
175 | + return array_map(function ($key, $value) { |
|
176 | + $value = str_replace(trailingslashit(WP_CONTENT_DIR), '', $value); |
|
177 | + return sprintf('[%s] => %s', $key, $value); |
|
178 | + }, array_keys($files), $files); |
|
179 | + } |
|
180 | + |
|
181 | + protected function getProfilerLabel(): string |
|
182 | + { |
|
183 | + $label = __('Profiler', 'blackbar'); |
|
184 | + $profilerTime = $this->convertToMiliseconds($this->app->profiler->getTotalTime(), 0); |
|
185 | + if ($profilerTime > 0) { |
|
186 | + $label .= sprintf(' (%s %s)', $profilerTime, __('ms', 'blackbar')); |
|
187 | + } |
|
188 | + return $label; |
|
189 | + } |
|
190 | + |
|
191 | + protected function getQueries(): array |
|
192 | + { |
|
193 | + global $wpdb; |
|
194 | + $queries = []; |
|
195 | + $search = [ |
|
196 | + 'AND', 'FROM', 'GROUP BY', 'INNER JOIN', 'LEFT JOIN', 'LIMIT', |
|
197 | + 'ON DUPLICATE KEY UPDATE', 'ORDER BY', 'OFFSET', ' SET', 'WHERE', |
|
198 | + ]; |
|
199 | + $replace = array_map(function ($value) { |
|
200 | + return PHP_EOL.$value; |
|
201 | + }, $search); |
|
202 | + foreach ($wpdb->queries as $query) { |
|
203 | + $miliseconds = number_format(round($query[1] * 1000, 4), 4); |
|
204 | + $sql = preg_replace('/\s\s+/', ' ', trim($query[0])); |
|
205 | + $sql = str_replace(PHP_EOL, ' ', $sql); |
|
206 | + $sql = str_replace($search, $replace, $sql); |
|
207 | + $queries[] = [ |
|
208 | + 'ms' => $miliseconds, |
|
209 | + 'sql' => $sql, |
|
210 | + ]; |
|
211 | + } |
|
212 | + return $queries; |
|
213 | + } |
|
214 | + |
|
215 | + protected function getQueriesLabel(): string |
|
216 | + { |
|
217 | + $label = __('SQL', 'blackbar'); |
|
218 | + if (!SAVEQUERIES) { |
|
219 | + return $label; |
|
220 | + } |
|
221 | + global $wpdb; |
|
222 | + $queryTime = 0; |
|
223 | + foreach ($wpdb->queries as $query) { |
|
224 | + $queryTime += $query[1]; |
|
225 | + } |
|
226 | + $queriesCount = sprintf('<span class="glbb-queries-count">%s</span>', count($wpdb->queries)); |
|
227 | + $queriesTime = sprintf('<span class="glbb-queries-time">%s</span>', $this->convertToMiliseconds((float) $queryTime)); |
|
228 | + return $label.sprintf(' (%s %s | %s %s)', $queriesCount, __('queries', 'blackbar'), $queriesTime, __('ms', 'blackbar')); |
|
229 | + } |
|
230 | + |
|
231 | + protected function getSlowActionsLabel(): string |
|
232 | + { |
|
233 | + $label = __('Hooks', 'blackbar'); |
|
234 | + $totalTime = $this->convertToMiliseconds($this->app->actions->getTotalTime(), 0); |
|
235 | + if ($totalTime > 0) { |
|
236 | + $label .= sprintf(' (%s %s)', $totalTime, __('ms', 'blackbar')); |
|
237 | + } |
|
238 | + return $label; |
|
239 | + } |
|
240 | + |
|
241 | + protected function getTemplates(): string |
|
242 | + { |
|
243 | + if (is_admin()) { |
|
244 | + return ''; |
|
245 | + } |
|
246 | + if (class_exists('\GeminiLabs\Castor\Facades\Development')) { |
|
247 | + ob_start(); |
|
248 | + \GeminiLabs\Castor\Facades\Development::printTemplatePaths(); |
|
249 | + return ob_get_clean(); |
|
250 | + } |
|
251 | + return sprintf('<pre>%s</pre>', implode(PHP_EOL, $this->getIncludedFiles())); |
|
252 | + } |
|
253 | 253 | } |
@@ -4,136 +4,136 @@ |
||
4 | 4 | |
5 | 5 | class SlowActions |
6 | 6 | { |
7 | - /** |
|
8 | - * @var array |
|
9 | - */ |
|
10 | - protected $flow; |
|
11 | - /** |
|
12 | - * This is the time that WordPress takes to execute the all hook. |
|
13 | - * @var float |
|
14 | - */ |
|
15 | - protected $noise; |
|
16 | - /** |
|
17 | - * @var float |
|
18 | - */ |
|
19 | - protected $start; |
|
20 | - /** |
|
21 | - * @var float |
|
22 | - */ |
|
23 | - protected $stop; |
|
24 | - /** |
|
25 | - * @var int |
|
26 | - */ |
|
27 | - protected $totalActions; |
|
28 | - /** |
|
29 | - * @var float |
|
30 | - */ |
|
31 | - protected $totalTime; |
|
7 | + /** |
|
8 | + * @var array |
|
9 | + */ |
|
10 | + protected $flow; |
|
11 | + /** |
|
12 | + * This is the time that WordPress takes to execute the all hook. |
|
13 | + * @var float |
|
14 | + */ |
|
15 | + protected $noise; |
|
16 | + /** |
|
17 | + * @var float |
|
18 | + */ |
|
19 | + protected $start; |
|
20 | + /** |
|
21 | + * @var float |
|
22 | + */ |
|
23 | + protected $stop; |
|
24 | + /** |
|
25 | + * @var int |
|
26 | + */ |
|
27 | + protected $totalActions; |
|
28 | + /** |
|
29 | + * @var float |
|
30 | + */ |
|
31 | + protected $totalTime; |
|
32 | 32 | |
33 | - public function __construct() |
|
34 | - { |
|
35 | - $this->flow = []; |
|
36 | - $this->noise = (float) 0; |
|
37 | - $this->start = microtime(true); |
|
38 | - $this->stop = (float) 0; |
|
39 | - $this->totalActions = 0; |
|
40 | - $this->totalTime = (float) 0; |
|
41 | - } |
|
33 | + public function __construct() |
|
34 | + { |
|
35 | + $this->flow = []; |
|
36 | + $this->noise = (float) 0; |
|
37 | + $this->start = microtime(true); |
|
38 | + $this->stop = (float) 0; |
|
39 | + $this->totalActions = 0; |
|
40 | + $this->totalTime = (float) 0; |
|
41 | + } |
|
42 | 42 | |
43 | - public function getTotalTimeForHook(array $data): float |
|
44 | - { |
|
45 | - $total = 0; |
|
46 | - foreach ($data['time'] as $time) { |
|
47 | - $total += ($time['stop'] - $time['start']) * 1000; |
|
48 | - } |
|
49 | - return (float) $total; |
|
50 | - } |
|
43 | + public function getTotalTimeForHook(array $data): float |
|
44 | + { |
|
45 | + $total = 0; |
|
46 | + foreach ($data['time'] as $time) { |
|
47 | + $total += ($time['stop'] - $time['start']) * 1000; |
|
48 | + } |
|
49 | + return (float) $total; |
|
50 | + } |
|
51 | 51 | |
52 | - public function addCallbacksForAction(string $action): void |
|
53 | - { |
|
54 | - global $wp_filter; |
|
55 | - if (!array_key_exists($action, $this->flow)) { |
|
56 | - return; |
|
57 | - } |
|
58 | - $this->flow[$action]['callbacks_count'] = 0; |
|
59 | - foreach ($wp_filter[$action] as $priority => $callbacks) { |
|
60 | - if (!array_key_exists($priority, $this->flow[$action]['callbacks'])) { |
|
61 | - $this->flow[$action]['callbacks'][$priority] = []; |
|
62 | - } |
|
63 | - foreach ($callbacks as $callback) { |
|
64 | - if (is_array($callback['function']) && 2 == count($callback['function'])) { |
|
65 | - list($object, $method) = $callback['function']; |
|
66 | - if (is_object($object)) { |
|
67 | - $object = get_class($object); |
|
68 | - } |
|
69 | - $this->flow[$action]['callbacks'][$priority][] = sprintf('%s::%s', $object, $method); |
|
70 | - } elseif (is_object($callback['function'])) { |
|
71 | - $this->flow[$action]['callbacks'][$priority][] = get_class($callback['function']); |
|
72 | - } else { |
|
73 | - $this->flow[$action]['callbacks'][$priority][] = $callback['function']; |
|
74 | - } |
|
75 | - ++$this->flow[$action]['callbacks_count']; |
|
76 | - } |
|
77 | - } |
|
78 | - } |
|
52 | + public function addCallbacksForAction(string $action): void |
|
53 | + { |
|
54 | + global $wp_filter; |
|
55 | + if (!array_key_exists($action, $this->flow)) { |
|
56 | + return; |
|
57 | + } |
|
58 | + $this->flow[$action]['callbacks_count'] = 0; |
|
59 | + foreach ($wp_filter[$action] as $priority => $callbacks) { |
|
60 | + if (!array_key_exists($priority, $this->flow[$action]['callbacks'])) { |
|
61 | + $this->flow[$action]['callbacks'][$priority] = []; |
|
62 | + } |
|
63 | + foreach ($callbacks as $callback) { |
|
64 | + if (is_array($callback['function']) && 2 == count($callback['function'])) { |
|
65 | + list($object, $method) = $callback['function']; |
|
66 | + if (is_object($object)) { |
|
67 | + $object = get_class($object); |
|
68 | + } |
|
69 | + $this->flow[$action]['callbacks'][$priority][] = sprintf('%s::%s', $object, $method); |
|
70 | + } elseif (is_object($callback['function'])) { |
|
71 | + $this->flow[$action]['callbacks'][$priority][] = get_class($callback['function']); |
|
72 | + } else { |
|
73 | + $this->flow[$action]['callbacks'][$priority][] = $callback['function']; |
|
74 | + } |
|
75 | + ++$this->flow[$action]['callbacks_count']; |
|
76 | + } |
|
77 | + } |
|
78 | + } |
|
79 | 79 | |
80 | - public function getMeasure(): array |
|
81 | - { |
|
82 | - foreach ($this->flow as $action => $data) { |
|
83 | - $total = $this->getTotalTimeForHook($data); |
|
84 | - $this->flow[$action]['total'] = $total; |
|
85 | - $this->totalTime += $total; |
|
86 | - $this->totalActions += $data['count']; |
|
87 | - $this->addCallbacksForAction($action); |
|
88 | - } |
|
89 | - uasort($this->flow, [$this, 'sortByTime']); |
|
90 | - return $this->flow; |
|
91 | - } |
|
80 | + public function getMeasure(): array |
|
81 | + { |
|
82 | + foreach ($this->flow as $action => $data) { |
|
83 | + $total = $this->getTotalTimeForHook($data); |
|
84 | + $this->flow[$action]['total'] = $total; |
|
85 | + $this->totalTime += $total; |
|
86 | + $this->totalActions += $data['count']; |
|
87 | + $this->addCallbacksForAction($action); |
|
88 | + } |
|
89 | + uasort($this->flow, [$this, 'sortByTime']); |
|
90 | + return $this->flow; |
|
91 | + } |
|
92 | 92 | |
93 | - public function getTotalActions(): int |
|
94 | - { |
|
95 | - return $this->totalActions; |
|
96 | - } |
|
93 | + public function getTotalActions(): int |
|
94 | + { |
|
95 | + return $this->totalActions; |
|
96 | + } |
|
97 | 97 | |
98 | - public function getTotalTime(): float |
|
99 | - { |
|
100 | - return $this->totalTime; |
|
101 | - // $totalNoise = (count($this->timers) - 1) * $this->noise; |
|
102 | - // return $this->stop - $this->start - $totalNoise; |
|
103 | - } |
|
98 | + public function getTotalTime(): float |
|
99 | + { |
|
100 | + return $this->totalTime; |
|
101 | + // $totalNoise = (count($this->timers) - 1) * $this->noise; |
|
102 | + // return $this->stop - $this->start - $totalNoise; |
|
103 | + } |
|
104 | 104 | |
105 | - public function startTimer(): void |
|
106 | - { |
|
107 | - if (!isset($this->flow[current_filter()])) { |
|
108 | - $this->flow[current_filter()] = [ |
|
109 | - 'callbacks' => [], |
|
110 | - 'count' => 0, |
|
111 | - 'stack' => [], |
|
112 | - 'time' => [], |
|
113 | - ]; |
|
114 | - add_action(current_filter(), [$this, 'stopTimer'], 9000); |
|
115 | - } |
|
116 | - $count = ++$this->flow[current_filter()]['count']; |
|
117 | - array_push($this->flow[current_filter()]['stack'], ['start' => microtime(true)]); |
|
118 | - } |
|
105 | + public function startTimer(): void |
|
106 | + { |
|
107 | + if (!isset($this->flow[current_filter()])) { |
|
108 | + $this->flow[current_filter()] = [ |
|
109 | + 'callbacks' => [], |
|
110 | + 'count' => 0, |
|
111 | + 'stack' => [], |
|
112 | + 'time' => [], |
|
113 | + ]; |
|
114 | + add_action(current_filter(), [$this, 'stopTimer'], 9000); |
|
115 | + } |
|
116 | + $count = ++$this->flow[current_filter()]['count']; |
|
117 | + array_push($this->flow[current_filter()]['stack'], ['start' => microtime(true)]); |
|
118 | + } |
|
119 | 119 | |
120 | - /** |
|
121 | - * @param mixed $possibleFilter |
|
122 | - * @return mixed |
|
123 | - */ |
|
124 | - public function stopTimer($possibleFilter = null) |
|
125 | - { |
|
126 | - $time = array_pop($this->flow[current_filter()]['stack']); |
|
127 | - $time['stop'] = microtime(true); |
|
128 | - array_push($this->flow[current_filter()]['time'], $time); |
|
129 | - return $possibleFilter; |
|
130 | - } |
|
120 | + /** |
|
121 | + * @param mixed $possibleFilter |
|
122 | + * @return mixed |
|
123 | + */ |
|
124 | + public function stopTimer($possibleFilter = null) |
|
125 | + { |
|
126 | + $time = array_pop($this->flow[current_filter()]['stack']); |
|
127 | + $time['stop'] = microtime(true); |
|
128 | + array_push($this->flow[current_filter()]['time'], $time); |
|
129 | + return $possibleFilter; |
|
130 | + } |
|
131 | 131 | |
132 | - protected function sortByTime(array $a, array $b): int |
|
133 | - { |
|
134 | - if ($a['total'] == $b['total']) { |
|
135 | - return 0; |
|
136 | - } |
|
137 | - return ($a['total'] > $b['total']) ? -1 : 1; |
|
138 | - } |
|
132 | + protected function sortByTime(array $a, array $b): int |
|
133 | + { |
|
134 | + if ($a['total'] == $b['total']) { |
|
135 | + return 0; |
|
136 | + } |
|
137 | + return ($a['total'] > $b['total']) ? -1 : 1; |
|
138 | + } |
|
139 | 139 | } |
@@ -6,58 +6,58 @@ |
||
6 | 6 | |
7 | 7 | class Console |
8 | 8 | { |
9 | - const ERROR_CODES = [ |
|
10 | - E_ERROR => 'Error', // 1 |
|
11 | - E_WARNING => 'Warning', // 2 |
|
12 | - E_NOTICE => 'Notice', // 8 |
|
13 | - E_STRICT => 'Strict', // 2048 |
|
14 | - E_DEPRECATED => 'Deprecated', // 8192 |
|
15 | - ]; |
|
9 | + const ERROR_CODES = [ |
|
10 | + E_ERROR => 'Error', // 1 |
|
11 | + E_WARNING => 'Warning', // 2 |
|
12 | + E_NOTICE => 'Notice', // 8 |
|
13 | + E_STRICT => 'Strict', // 2048 |
|
14 | + E_DEPRECATED => 'Deprecated', // 8192 |
|
15 | + ]; |
|
16 | 16 | |
17 | - const MAPPED_ERROR_CODES = [ |
|
18 | - 'debug' => 0, |
|
19 | - 'info' => 0, |
|
20 | - 'notice' => 0, |
|
21 | - 'warning' => E_NOTICE, // 8 |
|
22 | - 'error' => E_WARNING, // 2 |
|
23 | - 'critical' => E_WARNING, // 2 |
|
24 | - 'alert' => E_WARNING, // 2 |
|
25 | - 'emergency' => E_WARNING, // 2 |
|
26 | - ]; |
|
17 | + const MAPPED_ERROR_CODES = [ |
|
18 | + 'debug' => 0, |
|
19 | + 'info' => 0, |
|
20 | + 'notice' => 0, |
|
21 | + 'warning' => E_NOTICE, // 8 |
|
22 | + 'error' => E_WARNING, // 2 |
|
23 | + 'critical' => E_WARNING, // 2 |
|
24 | + 'alert' => E_WARNING, // 2 |
|
25 | + 'emergency' => E_WARNING, // 2 |
|
26 | + ]; |
|
27 | 27 | |
28 | - public $entries = []; |
|
28 | + public $entries = []; |
|
29 | 29 | |
30 | - /** |
|
31 | - * @param int|string $errno |
|
32 | - * @return static |
|
33 | - */ |
|
34 | - public function store(string $message, $errno = 0, string $location = '') |
|
35 | - { |
|
36 | - $errname = 'Debug'; |
|
37 | - if (array_key_exists($errno, static::MAPPED_ERROR_CODES)) { |
|
38 | - $errname = ucfirst($errno); |
|
39 | - $errno = static::MAPPED_ERROR_CODES[$errno]; |
|
40 | - } elseif (array_key_exists($errno, static::ERROR_CODES)) { |
|
41 | - $errname = static::ERROR_CODES[$errno]; |
|
42 | - } |
|
43 | - $this->entries[] = [ |
|
44 | - 'errno' => $errno, |
|
45 | - 'message' => $location.$this->normalizeValue($message), |
|
46 | - 'name' => sprintf('<span class="glbb-info glbb-%s">%s</span>', strtolower($errname), $errname), |
|
47 | - ]; |
|
48 | - return $this; |
|
49 | - } |
|
30 | + /** |
|
31 | + * @param int|string $errno |
|
32 | + * @return static |
|
33 | + */ |
|
34 | + public function store(string $message, $errno = 0, string $location = '') |
|
35 | + { |
|
36 | + $errname = 'Debug'; |
|
37 | + if (array_key_exists($errno, static::MAPPED_ERROR_CODES)) { |
|
38 | + $errname = ucfirst($errno); |
|
39 | + $errno = static::MAPPED_ERROR_CODES[$errno]; |
|
40 | + } elseif (array_key_exists($errno, static::ERROR_CODES)) { |
|
41 | + $errname = static::ERROR_CODES[$errno]; |
|
42 | + } |
|
43 | + $this->entries[] = [ |
|
44 | + 'errno' => $errno, |
|
45 | + 'message' => $location.$this->normalizeValue($message), |
|
46 | + 'name' => sprintf('<span class="glbb-info glbb-%s">%s</span>', strtolower($errname), $errname), |
|
47 | + ]; |
|
48 | + return $this; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * @param mixed $value |
|
53 | - */ |
|
54 | - protected function normalizeValue($value): string |
|
55 | - { |
|
56 | - if ($value instanceof DateTime) { |
|
57 | - $value = $value->format('Y-m-d H:i:s'); |
|
58 | - } elseif (is_object($value) || is_array($value)) { |
|
59 | - $value = print_r(json_decode(json_encode($value)), true); |
|
60 | - } |
|
61 | - return esc_html((string) $value); |
|
62 | - } |
|
51 | + /** |
|
52 | + * @param mixed $value |
|
53 | + */ |
|
54 | + protected function normalizeValue($value): string |
|
55 | + { |
|
56 | + if ($value instanceof DateTime) { |
|
57 | + $value = $value->format('Y-m-d H:i:s'); |
|
58 | + } elseif (is_object($value) || is_array($value)) { |
|
59 | + $value = print_r(json_decode(json_encode($value)), true); |
|
60 | + } |
|
61 | + return esc_html((string) $value); |
|
62 | + } |
|
63 | 63 | } |