-
Notifications
You must be signed in to change notification settings - Fork 4
/
shortcodes.php
188 lines (164 loc) · 4.86 KB
/
shortcodes.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
<?php
/**
* Shortcodes v1.2.0
*
* This plugin enables to use shortcodes (simple snippets) inside a
* document to be rendered by Grav.
*
* Dual licensed under the MIT or GPL Version 3 licenses, see LICENSE.
* http://benjamin-regler.de/license/
*
* @package Shortcodes
* @version 1.2.0
* @link <https://github.com/sommerregen/grav-plugin-shortcodes>
* @author Benjamin Regler <sommerregen@benjamin-regler.de>
* @copyright 2015, Benjamin Regler
* @license <http://opensource.org/licenses/MIT> MIT
* @license <http://opensource.org/licenses/GPL-3.0> GPLv3
*/
namespace Grav\Plugin;
use Grav\Common\Grav;
use Grav\Common\Plugin;
use Grav\Plugin\Shortcodes;
use RocketTheme\Toolbox\Event\Event;
/**
* ShortcodesPlugin
*
* This plugin enables to use shortcodes (simple snippets) inside a
* document to be rendered by Grav.
*/
class ShortcodesPlugin extends Plugin
{
/**
* Instance of Shortcodes class
*
* @var \Grav\Plugin\Shortcodes\Shortcodes
*/
protected $shortcodes;
/**
* Return a list of subscribed events.
*
* @return array A list of events, i.e. 'name' => ['method', priority].
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
/**
* Initialize configuration
*/
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
$this->active = false;
return;
}
if ($this->config->get('plugins.shortcodes.enabled')) {
$this->enable([
'onPageInitialized' => ['onPageInitialized', 0],
'onPageContentRaw' => ['onPageContentRaw', 0],
'onPageContentProcessed' => ['onPageContentProcessed', 0],
'onTwigInitialized' => ['onTwigInitialized', 0]
]);
}
}
/**
* Initialize page.
*/
public function onPageInitialized()
{
/** @var \Grav\Common\Page\Page $page */
$page = $this->grav['page'];
/** @var Cache $cache */
$cache = $this->grav['cache'];
/** @var Debugger $debugger */
$debugger = $this->grav['debugger'];
$cache_id = md5('shortcodes' . $page->id() . $cache->getKey());
if ($data = $cache->fetch($cache_id)) {
$debugger->addMessage("Shortcodes Plugin cache hit.");
foreach ($data as $key => $extra) {
$object = ($key != 'page') ? $this->grav[$key] : $page;
foreach ($extra as $value) {
list($method, $arguments) = $value;
call_user_func_array([$object, $method], $arguments);
}
}
}
}
/**
* Add content after page content was read into the system.
*
* @param Event $event An event object, when `onPageContentRaw` is
* fired.
*/
public function onPageContentRaw(Event $event)
{
/** @var Page $page */
$page = $event['page'];
/** @var Cache $cache */
$cache = $this->grav['cache'];
$config = $this->mergeConfig($page);
if ($config->get('enabled')) {
$raw = $page->getRawContent();
// Set parsed content back into as raw content
$page->setRawContent($this->init()->render($raw, $config->get('shortcodes', []), $page));
}
}
/**
* Add content after page was processed.
*
* @param Event $event An event object, when `onPageContentProcessed`
* is fired.
*/
public function onPageContentProcessed(Event $event)
{
// Get the page header
$page = $event['page'];
// Get modified content, replace all tokens with their
// respective shortcodes and write content back to page
$content = $page->getRawContent();
$page->setRawContent($this->init()->normalize($content));
}
/**
* Initialize Twig configuration and filters.
*/
public function onTwigInitialized()
{
// Expose function
$this->grav['twig']->twig()->addFunction(
new \Twig_SimpleFunction('shortcodes', [$this, 'shortcodeFunction'], ['is_safe' => ['html']])
);
}
/**
* Filter to parse Shortcodes.
*
* @param string $content The content to be filtered.
* @param array $options Array of options for the Shortcode filter.
*
* @return string The filtered content.
*/
public function shortcodeFunction($content, $params = [])
{
$config = $this->mergeConfig($this->grav['page'], $params);
return $this->init()->render($content, $config->get('shortcodes', []), $this->grav['page']);
}
/**
* Initialize plugin and all dependencies.
*
* @return Shortcodes Returns an instance of \Grav\Plugin\Shortcodes\Shortcodes.
*/
protected function init()
{
if (!$this->shortcodes) {
// Initialize Autoloader
require_once(__DIR__.'/classes/Autoloader.php');
$autoloader = new Shortcodes\Autoloader();
$autoloader->register();
// Initialize back-end
$this->shortcodes = new Shortcodes\Shortcodes($this->config);
}
return $this->shortcodes;
}
}