-
Notifications
You must be signed in to change notification settings - Fork 5
/
videoembed.php
295 lines (254 loc) · 7.98 KB
/
videoembed.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
294
295
<?php
namespace Grav\Plugin;
use Grav\Common\Assets;
use Grav\Common\Page\Page;
use Grav\Common\Plugin;
use RocketTheme\Toolbox\Event\Event;
use Grav\Common\Data\Data;
/**
* Class VideoEmbedPlugin
* This plugin replace links to youtube and replace it with embed element
* @package Grav\Plugin
* @author Maxim Hodyrev <maximkou@gmail.com>
* @license MIT
*
* @property $grav \Grav\Common\Grav
* @property $config \Grav\Common\Config
*/
class VideoEmbedPlugin extends Plugin
{
/**
* @var \Grav\Common\Data\Data
*/
private $cfg;
/**
* @return array
* @codeCoverageIgnore
*/
public static function getSubscribedEvents()
{
return [
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
'onPageProcessed' => ['onPageProcessed', 0],
];
}
/**
* Add styles for video responsiveness
* @see onPageInitialized
* @return void
* @codeCoverageIgnore
*/
public function onTwigSiteVariables()
{
$this->addAssets($this->grav['page'], $this->grav['assets']);
}
/**
* Process links in page
* @param Event $event
* @throws \Exception
* @codeCoverageIgnore
*/
public function onPageProcessed(Event $event)
{
require_once __DIR__ . "/src/ServiceInterface.php";
require_once __DIR__ . "/src/ServiceAbstract.php";
/** @var \Grav\Common\Page\Page $page */
$page = $event['page'];
$config = $this->getConfig($page);
$content = $this->processPage($page, $config);
$isProcessMarkdown = $page->shouldProcess('markdown');
$page->process(['markdown' => false]);
$page->content($content);
$page->process(['markdown' => $isProcessMarkdown]);
}
/**
* @param $serviceName
* @param array $serviceConfig
* @return \Grav\Plugin\VideoEmbed\ServiceInterface
* @throws \Exception
*/
protected function getServiceByName($serviceName, array $serviceConfig = [])
{
$serviceName = ucfirst($serviceName);
$servicePath = __DIR__."/src/Service/$serviceName.php";
$serviceClass = "\\Grav\\Plugin\\VideoEmbed\\Service\\$serviceName";
if (!file_exists($servicePath)) {
throw new \Exception("Service $serviceName is not supported.");
}
require_once $servicePath;
return new $serviceClass($serviceConfig);
}
/**
* @param Page $page
* @return \Grav\Common\Data\Data
* @codeCoverageIgnore
*/
protected function getConfig(Page $page = null)
{
if ($this->cfg && empty($page)) {
return $this->cfg;
}
if (!$this->cfg) {
$this->cfg = new Data(
(array)$this->config->get('plugins.videoembed', [])
);
}
if (!empty($page)) {
$headers = $page->header();
if (isset($headers->videoembed)) {
$this->cfg = new Data(
$this->mergeOptions(
$this->cfg->toArray(),
(array)$headers->videoembed
)
);
}
}
return $this->cfg;
}
/**
* @param Page $page
* @param Data $config
* @return string
* @throws \ErrorException
* @throws \Exception
*/
protected function processPage(Page $page, Data $config)
{
if ($config->get('responsive')) {
$this->enableResponsiveness($config);
}
$container = $this->getEmbedContainer($config);
$services = array_filter(
(array)$config->get('services', []),
function ($service) {
return !empty($service['enabled']);
}
);
$usedServices = [];
$content = $page->content();
foreach ($services as $serviceName => $serviceConfig) {
$service = $this->getServiceByName($serviceName, $serviceConfig);
$content = $service->processHtml($content, $container, $processedCnt);
if ($processedCnt > 0) {
$usedServices[] = $serviceName;
}
}
if (!empty($usedServices)) {
$this->defineAssets($page, $config, $usedServices);
}
return $content;
}
/**
* @throws \ErrorException
*/
protected function enableResponsiveness(Data $config)
{
/**
* if you enable responsiveness, you need use some container for video
* @see http://css-tricks.com/NetMag/FluidWidthVideo/Article-FluidWidthVideo.php
*/
if (!$config->get('container.element')) {
throw new \ErrorException(
'"Responsive" option requires using some container for video.
Please, set "container.element" option or disable responsiveness.'
);
}
$containerClasses = explode(' ', $config->get('container.html_attr.class', ''));
$containerClasses = array_map('trim', $containerClasses);
$containerClasses[] = 'plugin-videoembed-container-fluid';
$containerClasses = array_unique($containerClasses);
$config->set(
'container.html_attr.class',
implode(' ', $containerClasses)
);
$assets = (array)$config->get('assets', []);
$assets[] = 'plugin://videoembed/css/videombed-responsive.css';
$config->set('assets', array_unique($assets));
}
/**
* @param Page $page
* @param Data $config
* @param array $services
*/
protected function defineAssets(Page $page, Data $config, array $services)
{
if (empty($services)) {
return;
}
$assets = (array)$config->get('assets', []);
foreach ($services as $name) {
$assets = array_merge(
$assets,
$config->get("services.$name.assets", [])
);
}
$assets = array_unique($assets);
if (!empty($assets)) {
$existHeaders = [];
$headers = $page->header();
if (isset($headers->videoembed)) {
$existHeaders = $headers->videoembed;
}
$page->header()->videoembed = array_merge(
$existHeaders,
['assets' => $assets]
);
}
}
/**
* Add plugin specific assets
* @param Page $page
* @param Assets $assets
*/
protected function addAssets(Page $page, Assets $assets)
{
$pluginAssets = [];
if (!empty($page->header()->videoembed['assets'])) {
$pluginAssets = (array)$page->header()->videoembed['assets'];
}
foreach ($pluginAssets as $asset) {
$assets->add($asset);
}
}
/**
* @param Data $config
* @return \DOMElement|null
*/
protected function getEmbedContainer(Data $config)
{
$container = null;
if ($cElem = $config->get('container.element')) {
$document = new \DOMDocument();
$container = $document->createElement($cElem);
$containerAttr = (array)$config->get('container.html_attr', []);
foreach ($containerAttr as $htmlAttr => $attrValue) {
$container->setAttribute($htmlAttr, $attrValue);
}
}
return $container;
}
/**
* Merge options recursively
*
* @param array $array1
* @param mixed $array2
* @return array
* @codeCoverageIgnore
*/
protected function mergeOptions(array $array1, $array2 = null)
{
if (is_array($array2)) {
foreach ($array2 as $key => $val) {
if (is_array($array2[$key])) {
$array1[$key] = (array_key_exists($key, $array1) && is_array($array1[$key]))
? $this->mergeOptions($array1[$key], $array2[$key])
: $array2[$key];
} else {
$array1[$key] = $val;
}
}
}
return $array1;
}
}