-
Notifications
You must be signed in to change notification settings - Fork 3
/
TextformatterVideoEmbedOptions.module
209 lines (169 loc) · 5.21 KB
/
TextformatterVideoEmbedOptions.module
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
<?php
/**
* TextformatterVideoEmbedOptions
*
* An addition to TextformatterVideoEmbed for Youtube videos
*
* @todo yt_playerapiid & vimeo id: function to generate different ids for multiple videos on page
* ... ?
* @todo what about playlists?
*
*/
class TextformatterVideoEmbedOptions extends Textformatter implements ConfigurableModule {
/**
* getModuleInfo is a module required by all modules to tell ProcessWire about them
*
* @return array
*
*/
public static function getModuleInfo() {
return array(
'title' => 'Apply options for embedded YouTube and Vimeo videos',
'version' => '0.4.0',
'summary' => 'Set options for embedded YouTube and Vimeo videos. Must run after e.g. TextformatterVideoEmbed! For detailed documentation the options, see: https://developers.google.com/youtube/player_parameters or https://developer.vimeo.com/player/embedding',
'href' => 'https://processwire.com/talk/topic/11160-textformattervideoembedoptions/',
'author' => 'Steffen Henschel'
);
}
/**
* Data as used by the get/set functions
*
*/
protected $data = array();
/**
* default config
*
*/
protected static $configDefaults;
/**
* Set our configuration defaults
*
*/
public function __construct() {
// why?
self::loadDefaults();
}
/**
* load defaults
*
*/
private static function loadDefaults() {
// $defaultConfigFile = __DIR__.DIRECTORY_SEPARATOR.self::class.".config.json";
$defaultConfigFile = __DIR__.DIRECTORY_SEPARATOR."TextformatterVideoEmbedOptions.config.json";
self::$configDefaults = wireDecodeJSON(file_get_contents($defaultConfigFile));
}
/**
* Text formatting function as used by the Textformatter interface
*
*/
public function format(&$str) {
if (isset($this->data)) {
// perform a strpos fast check before performing regex check
// checks for already embedded videos
if (strpos($str, "youtube.com/embed") !== false) $this->applyYoutube($str);
if (strpos($str, "youtube-nocookie.com/embed") !== false) $this->applyYoutube($str);
if (strpos($str, "player.vimeo.com") !== false) $this->applyVimeo($str);
}
}
/**
* Check for Youtube embeds and add parameters
*
*/
protected function applyYoutube(&$str) {
$regex = "/https?:\/\/(?:www\.)?youtu(?:.be|be.com|be-nocookie.com)+\/embed\/[^\s\'\"]+/";
if(!preg_match_all($regex, $str, $matches)) return;
foreach($matches[0] as $key => $line) {
$extended_line = $line;
// append ? if not present
if (!strpos($extended_line, "?")) $extended_line .= "?";
foreach ($this->data as $key => $value) {
if($key==='yt_nocookies') continue;
if (strpos($key, "yt_") === 0 && $value != self::$configDefaults[$key]["value"]) {
$extended_line .= "&".substr($key, 3)."=$value";
}
}
if($this->yt_nocookies) $extended_line = str_replace('youtube.com', 'youtube-nocookie.com', $extended_line);
$str = str_replace($line, $extended_line, $str);
}
}
/**
* Check for Vimeo embeds and add parameters
*
*/
protected function applyVimeo(&$str) {
$regex = "/https?:\/\/player\.vimeo\.com\/video\/[^\s\'\"]+/";
if(!preg_match_all($regex, $str, $matches)) return;
foreach($matches[0] as $key => $line) {
$extended_line = $line;
// append ? if not present
if (!strpos($extended_line, "?")) $extended_line .= "?";
foreach ($this->data as $key => $value) {
if (strpos($key, "vm_") === 0 && $value != self::$configDefaults[$key]["value"]) {
$extended_line .= "&".substr($key, 3)."=$value";
}
}
$str = str_replace($line, $extended_line, $str);
}
}
/**
* Module configuration screen
*
*/
public static function getModuleConfigInputfields(array $data) {
self::loadDefaults();
$inputfields = new InputfieldWrapper();
foreach (self::$configDefaults as $name => $conf) {
// set data
if(!isset($data[$name])) $data[$name] = $conf["value"];
// set type of Inputfield
if (isset($conf["type"])) {
$f = wire('modules')->get($conf["type"]);
} else {
switch (gettype($conf["value"])) {
case 'boolean':
$f = wire('modules')->get('InputfieldCheckbox');
break;
case 'integer':
$f = wire('modules')->get('InputfieldInteger');
break;
default:
$f = wire('modules')->get('InputfieldText');
break;
}
}
// set other attributes
$f->attr('name', $name);
$f->attr('value', "$data[$name]");
$f->label = __($conf["label"]);
$f->description = __($conf["description"]);
$f->notes = __($conf["notes"]);
// misc
if ($conf["type"] === "InputfieldCheckbox" || gettype($conf["value"]) === "boolean") {
if($data[$name]) $f->attr('checked', 'checked');
}
// add to set
$inputfields->add($f);
}
return $inputfields;
}
/**
* The following functions are to support the ConfigurableModule interface
* since Textformatter does not originate from WireData
*
*/
public function set($key, $value) {
$this->data[$key] = $value;
return $this;
}
public function get($key) {
$value = Wire::getFuel($key);
if($value) return $value;
return isset($this->data[$key]) ? $this->data[$key] : null;
}
public function __set($key, $value) {
$this->set($key, $value);
}
public function __get($key) {
return $this->get($key);
}
}