-
Notifications
You must be signed in to change notification settings - Fork 12
/
youtube.php
164 lines (139 loc) · 5.13 KB
/
youtube.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
<?php
/**
* YouTube
*
* This plugin embeds YouTube video from markdown URLs
*
* Licensed under MIT, see LICENSE.
*/
namespace Grav\Plugin;
use Grav\Common\Data\Data;
use Grav\Common\Plugin;
use Grav\Common\Page\Page;
use Grav\Common\Twig\Twig;
use Grav\Plugin\Youtube\Twig\YoutubeTwigExtension;
use RocketTheme\Toolbox\Event\Event;
class YoutubePlugin extends Plugin
{
const YOUTUBE_REGEX = '(?:https?:\/{2}(?:(?:www.youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=))|(?:youtu\.be\/)))([a-zA-Z0-9_-]{11})(?:\?size=(\d+),(\d+))?';
/**
* 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],
'registerNextGenEditorPlugin' => ['registerNextGenEditorPluginShortcodes', 0],
];
}
/**
* Initialize configuration.
*/
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
$this->enable([
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
]);
return;
}
$this->enable([
'onPageContentRaw' => ['onPageContentRaw', 0],
'onTwigExtensions' => ['onTwigExtensions', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onShortcodeHandlers' => ['onShortcodeHandlers', 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'];
/** @var Twig $twig */
$twig = $this->grav['twig'];
/** @var Data $config */
$config = $this->mergeConfig($page, TRUE);
if ($config->get('enabled')) {
// Get raw content and substitute all formulas by a unique token
$raw = $page->getRawContent();
// build an anonymous function to pass to `parseLinks()`
$function = function ($matches) use ($twig, $config) {
$search = $matches[0];
// double check to make sure we found a valid YouTube video ID
if (!isset($matches[1])) {
return $search;
}
$options = array(
'player_parameters' => $config->get('player_parameters'),
'privacy_enhanced_mode' => $config->get('privacy_enhanced_mode'),
'lazy_load' => $config->get('lazy_load'),
'video_id' => $matches[1]
);
// check if size was given
if (isset($matches[2]) && isset($matches[3])) {
$options['video_size'] = true;
$options['video_height'] = $matches[2];
$options['video_width'] = $matches[3];
}
// build the replacement embed HTML string
$replace = $twig->processTemplate('partials/youtube.html.twig', $options);
// do the replacement
return str_replace($search, $replace, $search);
};
// set the parsed content back into as raw content
$page->setRawContent($this->parseLinks($raw, $function, $this::YOUTUBE_REGEX));
}
}
/**
* Add Twig Extensions.
*/
public function onTwigExtensions()
{
require_once __DIR__ . '/classes/Twig/YoutubeTwigExtension.php';
$this->grav['twig']->twig->addExtension(new YoutubeTwigExtension());
}
/**
* Set needed variables to display breadcrumbs.
*/
public function onTwigSiteVariables()
{
if (!$this->isAdmin() && $this->config->get('plugins.youtube.built_in_css')) {
$this->grav['assets']->add('plugin://youtube/css/youtube.css');
}
if (!$this->isAdmin() && $this->config->get('plugins.youtube.built_in_js')) {
$this->grav['assets']->add('plugin://youtube/js/youtube.js');
}
if ($this->isAdmin() && $this->config->get('plugins.youtube.add_editor_button')) {
$this->grav['assets']->add('plugin://youtube/admin/editor-button/js/button.js');
}
}
/**
* Add current directory to twig lookup paths.
*/
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
/**
* Initialize shortcodes
*/
public function onShortcodeHandlers()
{
$this->grav['shortcode']->registerAllShortcodes(__DIR__.'/shortcodes');
}
public function registerNextGenEditorPluginShortcodes($event) {
$plugins = $event['plugins'];
// youtube
$plugins['js'][] = 'plugin://youtube/nextgen-editor/shortcodes/youtube/youtube.js';
$event['plugins'] = $plugins;
return $event;
}
}