-
Notifications
You must be signed in to change notification settings - Fork 14
/
external_links.php
221 lines (194 loc) · 6.38 KB
/
external_links.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
<?php
/**
* External Links v1.6.1
*
* This plugin adds small icons to external and mailto links, informing
* users the link will take them to a new site or open their email client.
*
* Dual licensed under the MIT or GPL Version 3 licenses, see LICENSE.
* http://benjamin-regler.de/license/
*
* @package External Links
* @version 1.6.1
* @link <https://github.com/sommerregen/grav-plugin-external-links>
* @author Benjamin Regler <sommerregen@benjamin-regler.de>
* @copyright 2017+, Benjamin Regler
* @license <http://opensource.org/licenses/MIT> MIT
* @license <http://opensource.org/licenses/GPL-3.0> GPLv3
*/
namespace Grav\Plugin;
use Grav\Common\Plugin;
use Grav\Common\Page\Page;
use Grav\Common\Data\Blueprints;
use RocketTheme\Toolbox\Event\Event;
/**
* External Links Plugin
*
* This plugin adds small icons to external and mailto links, informing
* users the link will take them to a new site or open their email client.
*/
class ExternalLinksPlugin extends Plugin
{
/**
* @var ExternaLinksPlugin
*/
/**
* Instance of ExternalLinks class
*
* @var \Grav\Plugin\ExternalLinks
*/
protected $external_links;
/** -------------
* Public methods
* --------------
*/
/**
* Return a list of subscribed events.
*
* @return array The list of events of the plugin of the form
* 'name' => ['method_name', priority].
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
/**
* Initialize configuration
*/
public function onPluginsInitialized()
{
// Process contents order according to weight option
$weight = $this->config->get('plugins.external_links.weight', 0);
// Set default events
$events = [
'onTwigInitialized' => ['onTwigInitialized', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
'onPageContentProcessed' => ['onPageContentProcessed', $weight]
];
// Set admin specific events
if ($this->isAdmin()) {
$this->active = false;
$events = [
'onBlueprintCreated' => ['onBlueprintCreated', 0]
];
}
// Register events
$this->enable($events);
}
/**
* Extend page blueprints with ExternalLinks configuration options.
*
* @param Event $event
*/
public function onBlueprintCreated(Event $event)
{
/** @var Blueprints $blueprint */
$blueprint = $event['blueprint'];
if ($blueprint->get('form/fields/tabs', null, '/')) {
$blueprints = new Blueprints(__DIR__ . '/blueprints/');
$extends = $blueprints->get($this->name);
$blueprint->extend($extends, true);
}
}
/**
* Apply external links filter to content, when each page has not been
* cached yet.
*
* @param Event $event The event when 'onPageContentProcessed' was
* fired.
*/
public function onPageContentProcessed(Event $event)
{
/** @var Page $page */
$page = $event['page'];
$config = $this->mergeConfig($page);
$enabled = ($config->get('process') && $config->get('enabled')) ? true : false;
if ($enabled && $this->compileOnce($page)) {
// Do nothing, if a route for a given page does not exist and check if
// mode option is valid
$mode = strtolower($config->get('mode', 'passive'));
if (!$page->route() || !in_array($mode, array('active', 'passive'))) {
return;
}
// Get content and list of exclude tags
$content = $page->getRawContent();
// Apply external links filter and save modified page content
$page->setRawContent(
$this->externalLinksFilter($content, $config->toArray(), $page)
);
}
}
/**
* Initialize Twig configuration and filters.
*/
public function onTwigInitialized()
{
// Expose function
$this->grav['twig']->twig()->addFilter(
new \Twig_SimpleFilter('external_links', [$this, 'externalLinksFilter'], ['is_safe' => ['html']])
);
}
/**
* Set needed variables to display external links.
*/
public function onTwigSiteVariables()
{
if ($this->config->get('plugins.external_links.built_in_css')) {
$this->grav['assets']->add('plugin://external_links/assets/css/external_links.css');
}
}
/**
* Filter to parse external links.
*
* @param string $content The content to be filtered.
* @param array $options Array of options for the External links filter.
*
* @return string The filtered content.
*/
public function externalLinksFilter($content, $params = [])
{
// Get custom user configuration
$page = func_num_args() > 2 ? func_get_arg(2) : $this->grav['page'];
$config = $this->mergeConfig($page, true, $params);
// Render
return $this->init()->render($content, $config, $page);
}
/** -------------------------------
* Private/protected helper methods
* --------------------------------
*/
/**
* Checks if a page has already been compiled yet.
*
* @param Page $page The page to check
* @return boolean Returns true if page has already been
* compiled yet, false otherwise
*/
protected function compileOnce(Page $page)
{
static $processed = [];
$id = md5($page->path());
// Make sure that contents is only processed once
if (!isset($processed[$id]) || ($processed[$id] < $page->modified())) {
$processed[$id] = $page->modified();
return true;
}
return false;
}
/**
* Initialize plugin and all dependencies.
*
* @return \Grav\Plugin\ExternalLinks Returns ExternalLinks instance.
*/
protected function init()
{
if (!$this->external_links) {
// Initialize ExternalLinks instance
require_once(__DIR__ . '/classes/ExternalLinks.php');
$this->external_links = new ExternalLinks();
}
return $this->external_links;
}
}