-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathshortcode-core.php
244 lines (207 loc) · 7.48 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
<?php
namespace Grav\Plugin;
use Grav\Common\Assets;
use Grav\Common\Page\Page;
use Grav\Common\Plugin;
use Grav\Common\Utils;
use RocketTheme\Toolbox\Event\Event;
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
class ShortcodeCorePlugin extends Plugin
{
/** @var ShortcodeManager $shortcodes */
protected $shortcodes;
/**
* @return array
*/
public static function getSubscribedEvents()
{
require_once(__DIR__.'/vendor/autoload.php');
require_once(__DIR__.'/classes/Shortcode.php');
require_once(__DIR__.'/classes/ShortcodeObject.php');
require_once(__DIR__.'/classes/ShortcodeManager.php');
return [
'onPluginsInitialized' => ['onPluginsInitialized', 10],
];
}
/**
* 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]
]);
$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');
}
protected function processShortcodes($page, $type = 'processContent') {
$meta = [];
$config = $this->mergeConfig($page);
// 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://'))) {
return;
} else {
$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);
}
}
protected function getConfig($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);
}
}
}
}
if (isset($page_meta['shortcode'])) {
$objects = $page_meta['shortcode'];
$twig = $this->grav['twig'];
if (!empty($objects)) {
foreach ($objects as $key => $object) {
$twig->twig_vars['shortcode'][$key] = $object;
}
}
}
}
}
/**
* Event that handles registering handler for shortcodes
*/
public function onShortcodeHandlers()
{
$this->shortcodes->registerAllShortcodes(__DIR__.'/shortcodes');
// 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 \Twig_SimpleFilter(
'shortcodes',
[$this->shortcodes, 'processShortcodes']
)
);
}
/**
* Helper method that merges the content meta shortcode data with twig variables
*
* @param $meta
*/
private function mergeTwigVars($meta)
{
// check content meta for objects, and if found as them as twig variables
if (isset($meta['shortcode'])) {
$objects = $meta['shortcode'];
$twig = $this->grav['twig'];
if (!empty($objects)) {
foreach ($objects as $key => $object) {
$twig->twig_vars['shortcode'][$key] = $object;
}
}
}
}
}