forked from getgrav/grav-plugin-shortcode-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortcode-core.php
293 lines (251 loc) · 10.8 KB
/
shortcode-core.php
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
namespace Grav\Plugin;
use Composer\Autoload\ClassLoader;
use Grav\Common\Assets;
use Grav\Common\Page\Interfaces\PageInterface;
use Grav\Common\Plugin;
use Grav\Common\Utils;
use Grav\Plugin\ShortcodeCore\ShortcodeManager;
use Grav\Plugin\ShortcodeCore\ShortcodeTwigVar;
use RocketTheme\Toolbox\Event\Event;
use Twig\TwigFilter;
class ShortcodeCorePlugin extends Plugin
{
/** @var ShortcodeManager $shortcodes */
protected $shortcodes;
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => [
['autoload', 100001],
['onPluginsInitialized', 10]
],
'registerNextGenEditorPlugin' => [
['registerNextGenEditorPlugin', 0],
['registerNextGenEditorPluginShortcodes', 0],
]
];
}
/**
* [onPluginsInitialized:100000] Composer autoload.
*
* @return ClassLoader
*/
public function autoload()
{
return require __DIR__ . '/vendor/autoload.php';
}
/**
* Initialize configuration
*/
public function onPluginsInitialized()
{
$this->config = $this->grav['config'];
// don't continue if this is admin and plugin is disabled for admin
if (!$this->config->get('plugins.shortcode-core.active_admin') && $this->isAdmin()) {
return;
}
$this->enable([
'onThemeInitialized' => ['onThemeInitialized', 0],
'onMarkdownInitialized' => ['onMarkdownInitialized', 0],
'onShortcodeHandlers' => ['onShortcodeHandlers', 0],
'onPageContentRaw' => ['onPageContentRaw', 0],
'onPageContentProcessed' => ['onPageContentProcessed', -10],
'onPageContent' => ['onPageContent', 0],
'onTwigInitialized' => ['onTwigInitialized', 0],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
]);
$this->grav['shortcode'] = $this->shortcodes = new ShortcodeManager();
}
/**
* Theme initialization is best place to fire onShortcodeHandler event
* in order to support both plugins and themes
*/
public function onThemeInitialized()
{
$this->grav->fireEvent('onShortcodeHandlers');
}
/**
* Handle the markdown Initialized event by setting up shortcode block tags
*
* @param Event $event the event containing the markdown parser
*/
public function onMarkdownInitialized(Event $event)
{
$this->shortcodes->setupMarkdown($event['markdown']);
}
/**
* Process shortcodes before Grav's processing
*
* @param Event $e
*/
public function onPageContentRaw(Event $e)
{
$this->processShortcodes($e['page'], 'processRawContent');
}
/**
* Process shortcodes after Grav's processing, but before caching
*
* @param Event $e
*/
public function onPageContentProcessed(Event $e)
{
$this->processShortcodes($e['page'], 'processContent');
}
/**
* @param PageInterface $page
* @param string $type
*/
protected function processShortcodes(PageInterface $page, $type = 'processContent') {
$meta = [];
$this->shortcodes->resetObjects(); // clear shortcodes that may have been processed in this execution thread before
$config = $this->mergeConfig($page);
// Don't run in admin pages other than content
$admin_pages_only = $config['admin_pages_only'] ?? true;
if ($admin_pages_only && $this->isAdmin() && !Utils::startsWith($page->filePath(), $this->grav['locator']->findResource('page://'))) {
return;
}
$this->active = $config->get('active', true);
// if the plugin is not active (either global or on page) exit
if (!$this->active) {
return;
}
// process the content for shortcodes
$page->setRawContent($this->shortcodes->$type($page, $config));
// if objects found set them as page content meta
$shortcode_objects = $this->shortcodes->getObjects();
if (!empty($shortcode_objects)) {
$meta['shortcode'] = $shortcode_objects;
}
// if assets founds set them as page content meta
$shortcode_assets = $this->shortcodes->getAssets();
if (!empty($shortcode_assets)) {
$meta['shortcodeAssets'] = $shortcode_assets;
}
// if we have meta set, let's add it to the content meta
if (!empty($meta)) {
$page->addContentMeta('shortcodeMeta', $meta);
}
}
/**
* @param PageInterface $page
* @return \Grav\Common\Data\Data
*/
protected function getConfig(PageInterface $page)
{
$config = $this->mergeConfig($page);
$this->active = false;
// Don't run in admin pages other than content
$admin_pages_only = isset($config['admin_pages_only']) ? $config['admin_pages_only'] : true;
if ($admin_pages_only &&
$this->isAdmin() &&
!Utils::startsWith($page->filePath(), $this->grav['locator']->findResource('page://'))) {
} else {
$this->active = $config->get('active', true);
}
return $config;
}
/**
* Handle the assets that might be associated with this page
*/
public function onPageContent(Event $event)
{
if (!$this->active) {
return;
}
$page = $event['page'];
// get the meta and check for assets
$page_meta = $page->getContentMeta('shortcodeMeta');
if (is_array($page_meta)) {
if (isset($page_meta['shortcodeAssets'])) {
$page_assets = (array) $page_meta['shortcodeAssets'];
/** @var Assets $assets */
$assets = $this->grav['assets'];
// if we actually have data now, add it to asset manager
foreach ($page_assets as $type => $asset) {
foreach ($asset as $item) {
$method = 'add'.ucfirst($type);
if (is_array($item)) {
$assets->$method($item[0], $item[1]);
} else {
$assets->$method($item);
}
}
}
}
}
}
/**
* Event that handles registering handler for shortcodes
*/
public function onShortcodeHandlers()
{
$include_default_shortcodes = $this->config->get('plugins.shortcode-core.include_default_shortcodes', true);
if ($include_default_shortcodes) {
$this->shortcodes->registerAllShortcodes(__DIR__ . '/classes/shortcodes', ['ignore' => ['Shortcode', 'ShortcodeObject']]);
}
// Add custom shortcodes directory if provided
$custom_shortcodes = $this->config->get('plugins.shortcode-core.custom_shortcodes');
if (isset($custom_shortcodes)) {
$this->shortcodes->registerAllShortcodes(GRAV_ROOT . $custom_shortcodes);
}
}
/**
* Add a twig filter for processing shortcodes in templates
*/
public function onTwigInitialized()
{
$this->grav['twig']->twig()->addFilter(new TwigFilter('shortcodes', [$this->shortcodes, 'processShortcodes']));
$this->grav['twig']->twig_vars['shortcode'] = new ShortcodeTwigVar();
}
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
public function registerNextGenEditorPlugin($event) {
$config = $this->config->get('plugins.shortcode-core.nextgen-editor');
$plugins = $event['plugins'];
if ($config['env'] !== 'development') {
$plugins['css'][] = 'plugin://shortcode-core/nextgen-editor/dist/css/app.css';
$plugins['js'][] = 'plugin://shortcode-core/nextgen-editor/dist/js/app.js';
} else {
$plugins['js'][] = 'http://' . $config['dev_host'] . ':' . $config['dev_port'] . '/js/app.js';
}
$event['plugins'] = $plugins;
return $event;
}
public function registerNextGenEditorPluginShortcodes($event) {
$include_default_shortcodes = $this->config->get('plugins.shortcode-core.include_default_shortcodes', true);
if ($include_default_shortcodes) {
$plugins = $event['plugins'];
$plugins['js'][] = 'plugin://shortcode-core/nextgen-editor/shortcodes/shortcode-core.js';
$plugins['js'][] = 'plugin://shortcode-core/nextgen-editor/shortcodes/align/align.js';
$plugins['js'][] = 'plugin://shortcode-core/nextgen-editor/shortcodes/color/color.js';
$plugins['js'][] = 'plugin://shortcode-core/nextgen-editor/shortcodes/columns/columns.js';
$plugins['css'][] = 'plugin://shortcode-core/nextgen-editor/shortcodes/details/details.css';
$plugins['js'][] = 'plugin://shortcode-core/nextgen-editor/shortcodes/details/details.js';
$plugins['js'][] = 'plugin://shortcode-core/nextgen-editor/shortcodes/div/div.js';
$plugins['js'][] = 'plugin://shortcode-core/nextgen-editor/shortcodes/figure/figure.js';
$plugins['js'][] = 'plugin://shortcode-core/nextgen-editor/shortcodes/fontawesome/fontawesome.js';
$plugins['css'][] = 'plugin://shortcode-core/nextgen-editor/shortcodes/headers/headers.css';
$plugins['js'][] = 'plugin://shortcode-core/nextgen-editor/shortcodes/headers/headers.js';
$plugins['js'][] = 'plugin://shortcode-core/nextgen-editor/shortcodes/language/language.js';
$plugins['js'][] = 'plugin://shortcode-core/nextgen-editor/shortcodes/lorem/lorem.js';
$plugins['css'][] = 'plugin://shortcode-core/nextgen-editor/shortcodes/mark/mark.css';
$plugins['js'][] = 'plugin://shortcode-core/nextgen-editor/shortcodes/mark/mark.js';
$plugins['css'][] = 'plugin://shortcode-core/nextgen-editor/shortcodes/notice/notice.css';
$plugins['js'][] = 'plugin://shortcode-core/nextgen-editor/shortcodes/notice/notice.js';
$plugins['js'][] = 'plugin://shortcode-core/nextgen-editor/shortcodes/raw/raw.js';
$plugins['js'][] = 'plugin://shortcode-core/nextgen-editor/shortcodes/safe-email/safe-email.js';
$plugins['js'][] = 'plugin://shortcode-core/nextgen-editor/shortcodes/section/section.js';
$plugins['js'][] = 'plugin://shortcode-core/nextgen-editor/shortcodes/size/size.js';
$plugins['js'][] = 'plugin://shortcode-core/nextgen-editor/shortcodes/span/span.js';
$plugins['js'][] = 'plugin://shortcode-core/nextgen-editor/shortcodes/u/u.js';
$event['plugins'] = $plugins;
}
return $event;
}
}