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
|
|
|
* @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
|
|
|
* @action 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
|
|
|
$args = array_combine(['hook', 'message', 'errno', 'location'], $args); |
47
|
|
|
$args = array_map('sanitize_textarea_field', $args); |
48
|
|
|
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 4); |
49
|
|
|
$entry = array_pop($backtrace); // get the fourth backtrace entry |
50
|
|
|
if (empty(trim($args['location'])) && array_key_exists('file', $entry)) { |
51
|
|
|
$path = explode(ABSPATH, $entry['file']); |
52
|
|
|
$args['location'] = sprintf('%s:%s', array_pop($path), $entry['line']); |
53
|
|
|
} |
54
|
|
|
$this->app->console->store($args['message'], $args['errno'], $args['location']); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @action all |
59
|
|
|
*/ |
60
|
|
|
public function initHooks(): void |
61
|
|
|
{ |
62
|
|
|
$this->app->hooks->startTimer(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @action all |
67
|
|
|
*/ |
68
|
|
|
public function initProfiler(): void |
69
|
|
|
{ |
70
|
|
|
$hook = func_get_arg(0); |
71
|
|
|
if ('blackbar/profiler/start' === $hook) { // time when the profiler started |
72
|
|
|
$this->app->profiler->setStart((int) hrtime(true)); |
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
if ('blackbar/profiler/noise' === $hook) { // measure the profiler noise |
76
|
|
|
$this->app->profiler->setNoise((int) hrtime(true)); |
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
if ('blackbar/profiler/stop' === $hook) { // time when the profiler stopped |
80
|
|
|
$this->app->profiler->setStop((int) hrtime(true)); |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
if ('timer:start' === $hook) { |
84
|
|
|
$name = func_num_args() > 1 ? func_get_arg(1) : 'Timer'; |
85
|
|
|
$this->app->profiler->start($name); |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
if ('timer:stop' === $hook) { |
89
|
|
|
$this->app->profiler->stop(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @action plugins_loaded |
95
|
|
|
*/ |
96
|
|
|
public function registerLanguages(): void |
97
|
|
|
{ |
98
|
|
|
load_plugin_textdomain(Application::ID, false, |
99
|
|
|
plugin_basename($this->app->path()).'/languages/' |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @action admin_footer |
105
|
|
|
* @action wp_footer |
106
|
|
|
*/ |
107
|
|
|
public function renderBar(): void |
108
|
|
|
{ |
109
|
|
|
do_action('blackbar/profiler/stop'); // stop profiler |
110
|
|
|
$this->app->render('debug-bar', [ |
111
|
|
|
'modules' => [ // order is intentional |
112
|
|
|
$this->app->console, |
113
|
|
|
$this->app->profiler, |
114
|
|
|
$this->app->queries, |
115
|
|
|
$this->app->hooks, |
116
|
|
|
$this->app->templates, |
117
|
|
|
$this->app->globals, |
118
|
|
|
], |
119
|
|
|
]); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|