forked from h6ah4i/grav-plugin-mediaembed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediaembed.php
212 lines (184 loc) · 5.36 KB
/
mediaembed.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
<?php
/**
* MediaEmbed v1.2.0
*
* This plugin embeds several media sites (e.g. YouTube, Vimeo,
* Soundcloud) by only providing the URL to the medium.
*
* Dual licensed under the MIT or GPL Version 3 licenses, see LICENSE.
* http://benjamin-regler.de/license/
*
* @package MediaEmbed
* @version 1.2.0
* @link <https://github.com/sommerregen/grav-plugin-archive-plus>
* @author Benjamin Regler <[email protected]>
* @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\Common\Page\Page;
use RocketTheme\Toolbox\Event\Event;
use Grav\Plugin\MediaEmbed\Autoloader;
use Grav\Plugin\MediaEmbed\MediaEmbed;
/**
* MediaEmbed
*
* This plugin ...
*/
class MediaEmbedPlugin extends Plugin
{
/**
* @var MediaEmbedPlugin
*/
/** ---------------------------
* Private/protected properties
* ----------------------------
*/
/**
* Instance of MediaEmbed class
*
* @var object
*/
protected $mediaembed;
/** -------------
* 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()
{
if ($this->isAdmin()) {
$this->active = false;
return;
}
if ($this->config->get('plugins.mediaembed.enabled')) {
// Initialize Autoloader
require_once(__DIR__ . '/classes/Autoloader.php');
require_once(__DIR__ . '/vendor/Requests/library/Requests.php');
$autoloader = new Autoloader();
$autoloader->route([
'Requests_' => __DIR__ . '/vendor/Requests/library/Requests',
], false);
$autoloader->register();
// Initialize MediaEmbed class
$this->mediaembed = new MediaEmbed($this->config);
$this->enable([
'onPageContentRaw' => ['onPageContentRaw', 0],
'onPageContentProcessed' => ['onPageContentProcessed', 0],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
]);
}
}
/**
* 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'];
$config = $this->mergeConfig($page);
if ($config->get('enabled')) {
// Get raw content and substitute all formulas by a unique token
$raw_content = $page->getRawContent();
// Save modified page content with tokens as placeholders
$page->setRawContent(
$this->mediaembed->prepare($raw_content, $page->id())
);
}
}
/**
* Apply mediaembed 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);
if ($config->get('enabled') && $this->compileOnce($page)) {
// Get content
$content = $page->getRawContent();
// Apply MediaEmbed filter and save modified page content
$page->setRawContent(
$this->mediaembed->process($content, $config)
);
}
}
/**
* Add current directory to twig lookup paths.
*/
public function onTwigTemplatePaths()
{
// Register MediaEmbed Twig templates
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
// Fire event for MediaEmbed plugins
$this->mediaembed->fireEvent('onTwigTemplatePaths');
}
/**
* Set needed variables to display videos.
*/
public function onTwigSiteVariables()
{
// Register built-in CSS assets
if ($this->config->get('plugins.mediaembed.built_in_css')) {
$this->grav['assets']
->add('plugin://mediaembed/assets/css/mediaembed.css');
}
if ($this->config->get('plugins.mediaembed.built_in_js')) {
$this->grav['assets']
->add('plugin://mediaembed/assets/js/mediaembed.js');
}
// Register assets from MediaEmbed Services
$assets = $this->mediaembed->getAssets();
foreach ($assets as $asset) {
$this->grav['assets']->add($asset);
}
}
/** -------------------------------
* 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;
}
}