1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\BlackBar\Modules; |
4
|
|
|
|
5
|
|
|
class Hooks extends Module |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var array |
9
|
|
|
*/ |
10
|
|
|
protected $hooks = []; |
11
|
|
|
/** |
12
|
|
|
* @var int |
13
|
|
|
*/ |
14
|
|
|
protected $totalHooks = 0; |
15
|
|
|
/** |
16
|
|
|
* Total elapsed time in nanoseconds |
17
|
|
|
* @var int |
18
|
|
|
*/ |
19
|
|
|
protected $totalTime = 0; |
20
|
|
|
|
21
|
|
|
public function entries(): array |
22
|
|
|
{ |
23
|
|
|
if (!$this->hasEntries()) { |
24
|
|
|
return []; |
25
|
|
|
} |
26
|
|
|
if (!empty($this->hooks)) { |
27
|
|
|
return $this->hooks; |
28
|
|
|
} |
29
|
|
|
array_walk($this->entries, function (&$data) { |
30
|
|
|
$total = $this->totalTimeForHook($data); |
31
|
|
|
$perCall = (int) round($total / $data['count']); |
32
|
|
|
$data['per_call'] = $this->formatTime($perCall); |
33
|
|
|
$data['total'] = $total; |
34
|
|
|
$data['total_formatted'] = $this->formatTime($total); |
35
|
|
|
}); |
36
|
|
|
$entries = $this->entries; |
37
|
|
|
$executionOrder = array_keys($entries); |
38
|
|
|
uasort($entries, [$this, 'sortByTime']); |
39
|
|
|
$this->hooks = array_slice($entries, 0, 50); // Keep the 50 slowest hooks |
40
|
|
|
$this->totalHooks = array_sum(wp_list_pluck($this->entries, 'count')); |
41
|
|
|
$this->totalTime = array_sum(wp_list_pluck($this->entries, 'total')); |
42
|
|
|
$order = array_intersect($executionOrder, array_keys($this->hooks)); |
43
|
|
|
foreach ($order as $index => $hook) { |
44
|
|
|
$this->hooks[$hook]['index'] = $index; |
45
|
|
|
} |
46
|
|
|
return $this->hooks; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function info(): string |
50
|
|
|
{ |
51
|
|
|
$this->entries(); // calculate the totalTime |
52
|
|
|
return $this->formatTime($this->totalTime); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function label(): string |
56
|
|
|
{ |
57
|
|
|
return __('Hooks', 'blackbar'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function startTimer(): void |
61
|
|
|
{ |
62
|
|
|
if (class_exists('Debug_Bar_Slow_Actions')) { |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
$hook = current_filter(); |
66
|
|
|
if (!isset($this->entries[$hook])) { |
67
|
|
|
$callbacks = $this->callbacksForHook($hook); |
68
|
|
|
if (empty($callbacks)) { |
69
|
|
|
return; // We skipped Blackbar callbacks |
70
|
|
|
} |
71
|
|
|
$this->entries[$hook] = [ |
72
|
|
|
'callbacks' => $callbacks, |
73
|
|
|
'callbacks_count' => count(array_merge(...$callbacks)), |
74
|
|
|
'count' => 0, |
75
|
|
|
'stack' => [], |
76
|
|
|
'time' => [], |
77
|
|
|
]; |
78
|
|
|
add_action($hook, [$this, 'stopTimer'], 9999); // @phpstan-ignore-line |
79
|
|
|
} |
80
|
|
|
++$this->entries[$hook]['count']; |
81
|
|
|
array_push($this->entries[$hook]['stack'], ['start' => (int) hrtime(true)]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param mixed $filteredValue |
86
|
|
|
* @return mixed |
87
|
|
|
*/ |
88
|
|
|
public function stopTimer($filteredValue = null) |
89
|
|
|
{ |
90
|
|
|
$time = array_pop($this->entries[current_filter()]['stack']); |
91
|
|
|
$time['stop'] = (int) hrtime(true); |
92
|
|
|
array_push($this->entries[current_filter()]['time'], $time); |
93
|
|
|
return $filteredValue; // In case this was a filter. |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected function callbacksForHook(string $hook): array |
97
|
|
|
{ |
98
|
|
|
global $wp_filter; |
99
|
|
|
$results = []; |
100
|
|
|
if (!isset($wp_filter[$hook])) { |
101
|
|
|
return $results; |
102
|
|
|
} |
103
|
|
|
foreach ($wp_filter[$hook] as $priority => $callbacks) { |
104
|
|
|
$results[$priority] = $results[$priority] ?? []; |
105
|
|
|
foreach ($callbacks as $callback) { |
106
|
|
|
if (is_array($callback['function']) && 2 === count($callback['function'])) { |
107
|
|
|
list($object, $method) = $callback['function']; |
108
|
|
|
if (is_object($object)) { |
109
|
|
|
$object = get_class($object); |
110
|
|
|
$reflection = new \ReflectionClass($object); |
111
|
|
|
if (str_starts_with($reflection->getNamespaceName(), 'GeminiLabs\BlackBar')) { |
112
|
|
|
continue; // skip Blackbar callbacks |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
$results[$priority][] = sprintf('%s::%s', $object, $method); |
116
|
|
|
} elseif (is_object($callback['function'])) { |
117
|
|
|
$results[$priority][] = get_class($callback['function']); |
118
|
|
|
} else { |
119
|
|
|
$results[$priority][] = $callback['function']; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
return $results; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected function sortByTime(array $a, array $b): int |
127
|
|
|
{ |
128
|
|
|
if ($a['total'] !== $b['total']) { |
129
|
|
|
return ($a['total'] > $b['total']) ? -1 : 1; |
130
|
|
|
} |
131
|
|
|
return 0; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Total elapsed time in nanoseconds |
136
|
|
|
*/ |
137
|
|
|
protected function totalTimeForHook(array $data): int |
138
|
|
|
{ |
139
|
|
|
$total = 0; |
140
|
|
|
foreach ($data['time'] as $time) { |
141
|
|
|
$total += ($time['stop'] - $time['start']); |
142
|
|
|
} |
143
|
|
|
return $total; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|