Repository navigation
Expand file tree
/
Copy pathAdminBarPlugin.php
More file actions
181 lines (160 loc) · 5.28 KB
/
Copy pathAdminBarPlugin.php
File metadata and controls
181 lines (160 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
declare(strict_types=1);
namespace Plugin\AdminBar;
use App\Infrastructure\Services\Plugin;
use App\Shared\Services\Registry;
use App\Shared\Services\Utils;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\SimpleCache\InvalidArgumentException;
use Qubus\EventDispatcher\ActionFilter\Action;
use Qubus\Exception\Exception;
use ReflectionException;
use function App\Shared\Helpers\cms_enqueue_css;
use function App\Shared\Helpers\cms_enqueue_js;
use function App\Shared\Helpers\is_user_logged_in;
use function App\Shared\Helpers\plugin_basename;
use function App\Shared\Helpers\plugin_dir_path;
use function App\Shared\Helpers\plugin_url;
use function dirname;
use function function_exists;
use function get_class;
use function phpb_in_editmode;
use function Qubus\Security\Helpers\esc_html__;
use function Qubus\Security\Helpers\t__;
class AdminBarPlugin extends Plugin
{
private bool $rendered = false;
/**
* @inheritDoc
* @throws ReflectionException|Exception
*/
public function meta(): array
{
$plugin = [
'name' => esc_html__(string: 'AdminBar', domain: 'adminbar'),
'id' => 'adminbar',
'author' => 'Joshua Parker',
'version' => '4.0.0',
'description' => t__(msgid: 'Adds an admin bar to Devflow site.', domain: 'adminbar'),
'basename' => plugin_basename(dirname(__FILE__)),
'path' => plugin_dir_path(dirname(__FILE__)),
'url' => plugin_url('', __CLASS__),
'pluginUri' => 'https://github.com/getdevflow/adminbar',
'authorUri' => 'https://nomadicjosh.com/',
'className' => get_class($this),
];
Registry::getInstance()->set('adminbar', $plugin);
return $plugin;
}
/**
* @inheritDoc
* @throws ReflectionException
*/
public function handle(): void
{
$action = Action::getInstance();
// The backend has no body-open hook, so render the fixed toolbar in its footer.
$action->addAction('cms_admin_head', [$this, 'enqueueCss'], 99);
$action->addAction('cms_admin_body_open', [$this, 'render'], 1);
$action->addAction('cms_admin_footer', [$this, 'enqueueJs'], 99);
// Every conforming frontend theme exposes these three CMS lifecycle hooks.
$action->addAction('cms_head', [$this, 'enqueueCss'], 99);
$action->addAction('cms_body_open', [$this, 'render'], 1);
$action->addAction('cms_footer', [$this, 'renderFallback'], 1);
$action->addAction('cms_footer', [$this, 'enqueueJs'], 99);
}
/**
* @throws ContainerExceptionInterface
* @throws Exception
* @throws InvalidArgumentException
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
public function enqueueCss(): void
{
if (!$this->shouldDisplay()) {
return;
}
// Admin screens already load Font Awesome; frontend themes may use a different icon library.
if (!Utils::isAdmin()) {
cms_enqueue_css(
config: 'plugin',
asset: 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css',
slug: $this->id()
);
}
cms_enqueue_css(
config: 'plugin',
asset: $this->url() . '/css/style.css',
slug: $this->id()
);
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws Exception
* @throws InvalidArgumentException
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
public function enqueueJs(): void
{
if (!$this->shouldDisplay()) {
return;
}
cms_enqueue_js(
config: 'plugin',
asset: $this->url() . '/js/adminbar.js',
slug: $this->id()
);
}
/**
* @throws ContainerExceptionInterface
* @throws Exception
* @throws InvalidArgumentException
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
public function render(bool $fallback = false): void
{
if ($this->rendered || !$this->shouldDisplay()) {
return;
}
$this->rendered = true;
echo $this->view->render(
'plugin::AdminBar/view/index',
['plugin' => $this->meta(), 'fallback' => $fallback]
);
}
/**
* Supports older themes that have a footer hook but no body-open hook.
*
* @throws ContainerExceptionInterface
* @throws Exception
* @throws InvalidArgumentException
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
public function renderFallback(): void
{
$this->render(fallback: true);
}
/**
* Keep the toolbar and its assets out of Vihzhuo's editable canvas.
*
* @return bool
* @throws ContainerExceptionInterface
* @throws Exception
* @throws InvalidArgumentException
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
private function shouldDisplay(): bool
{
if (function_exists('phpb_in_editmode') && phpb_in_editmode()) {
return false;
}
return is_user_logged_in();
}
}