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