Controller::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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/blackbar.js'), [], $this->app->version);
24
        wp_enqueue_style(Application::ID, $this->app->url('assets/blackbar.css'), ['dashicons'], $this->app->version);
25
    }
26
27
    /**
28
     * @param string $classes
29
     * @action admin_body_class
30
     */
31
    public function filterBodyClasses($classes): string
32
    {
33
        return trim((string) $classes.' '.Application::ID);
34
    }
35
36
    /**
37
     * @action all
38
     */
39
    public function initConsole(): void
40
    {
41
        if (Application::CONSOLE_HOOK !== func_get_arg(0)) {
42
            return;
43
        }
44
        $args = array_pad(func_get_args(), 4, '');
45
        $args = array_combine(['hook', 'message', 'errno', 'location'], $args);
46
        if (!is_numeric($args['errno'])) {
47
            $args['errno'] = '';
48
        }
49
        if (!is_string($args['location'])) {
50
            $args['location'] = '';
51
        }
52
        $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 4);
53
        $entry = array_pop($backtrace); // get the fourth backtrace entry
54
        if (empty(trim($args['location'])) && array_key_exists('file', $entry)) {
55
            $path = explode(ABSPATH, $entry['file']);
56
            $args['location'] = sprintf('%s:%s', array_pop($path), $entry['line']);
57
        }
58
        $this->app->console->store($args['message'], $args['errno'], $args['location']);
59
    }
60
61
    /**
62
     * @action all
63
     */
64
    public function initHooks(): void
65
    {
66
        $this->app->hooks->startTimer();
67
    }
68
69
    /**
70
     * @action all
71
     */
72
    public function initProfiler(): void
73
    {
74
        $hook = func_get_arg(0);
75
        if (str_starts_with($hook, 'blackbar/profiler/')) {
76
            $property = str_replace('blackbar/profiler/', '', $hook);
77
            $this->app->profiler->set($property);
78
        } elseif ('timer:start' === $hook) {
79
            $name = func_num_args() > 1 ? func_get_arg(1) : 'Timer';
80
            $this->app->profiler->start($name);
81
        } elseif ('timer:stop' === $hook) {
82
            $this->app->profiler->stop();
83
        }
84
    }
85
86
    /**
87
     * @action plugins_loaded
88
     */
89
    public function registerLanguages(): void
90
    {
91
        load_plugin_textdomain(Application::ID, false,
92
            plugin_basename($this->app->path()).'/languages/'
93
        );
94
    }
95
96
    /**
97
     * @action admin_footer
98
     * @action wp_footer
99
     */
100
    public function renderBar(): void
101
    {
102
        do_action('blackbar/profiler/stop'); // stop profiler
103
        $this->app->render('debug-bar', [
104
            'modules' => [ // order is intentional
105
                $this->app->console,
106
                $this->app->profiler,
107
                $this->app->queries,
108
                $this->app->hooks,
109
                $this->app->templates,
110
                $this->app->globals,
111
            ],
112
        ]);
113
    }
114
}
115