-
Notifications
You must be signed in to change notification settings - Fork 1
/
class-sublimevideo-shortcodes.php
197 lines (160 loc) · 6.36 KB
/
class-sublimevideo-shortcodes.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
<?php
/**
* This class defines the shortcode available with the SublimeVideo plugin
*/
class SublimeVideoShortcodes {
static function create_shortcode_from_params($params) {
$shortcode_base = 'sublimevideo';
$attributes = array();
if ($params[$params['poster_source'].'_poster_url']) {
$attributes[] = 'poster="'.$params[$params['poster_source'].'_poster_url'].'"';
}
$i = 1;
foreach (SublimeVideo::$formats as $format => $infos) {
foreach ($infos[SublimeVideo::FORMAT_QUALITIES] as $quality) {
$origin = $params[strtolower($format.'_'.$quality).'_source'];
$source = $params[$origin.'_'.strtolower($format.'_'.$quality)];
if ($source != '') {
if (!isset($uid)) $uid = self::generateUID($source);
$quality_prefix = !in_array(strtolower($quality), array('normal', 'mobile')) ? '('.strtolower($quality).')' : '';
$attributes[] = 'src'.$i.'="'.$quality_prefix.$source.'"';
}
$i++;
}
}
if (isset($uid)) $attributes[] = 'uid="'.$uid.'"';
if (isset($uid)) $attributes[] = 'id="'.$uid.'"';
if ($params['source_origin'] == 'youtube') {
$attributes[] = 'settings="youtube-id:'.$params['source_youtube_src'].'"';
}
$dimensions = array('width' => 640, 'height' => 360);
foreach ($dimensions as $dimension => $default_size) {
$size = $params['final_'.$dimension] ? $params['final_'.$dimension] : $default_size;
$attributes[] = $dimension.'="'.$size.'"';
}
var_dump($attributes);
return '['.$shortcode_base.' '.join(" ", $attributes).']';
}
static function generateUID($string) {
return dechex(crc32($string));
}
static function default_video_attributes() {
$array = array(
'id' => '',
'class' => 'sublime',
'style' => '',
'width' => esc_attr(get_option('sv_player_width')),
'height' => '',
'poster' => '',
'preload' => 'none'
);
return $array;
}
static function default_video_settings() {
$array = array();
foreach (SublimeVideo::$allowed_data_attributes as $shortcode_attribute => $data_attribute) {
$array[$shortcode_attribute] = '';
$array['data_'.$shortcode_attribute] = '';
}
return $array;
}
static function behaviors($attributes) {
$behaviors = array();
$data_attributes = array();
foreach (SublimeVideo::$allowed_behaviors as $behavior) {
if (in_array($behavior, $attributes)) {
switch ($behavior) {
case 'autoplay':
$data_attributes[] = "data-autoplay='true'";
break;
case 'loop':
$data_attributes[] = "data-on-end='replay'";
break;
}
}
}
return empty($behaviors) ? join(' ', $data_attributes) : "data-sublime-wp='".join(' ', $behaviors)."'";
}
static function sources($hash) {
ksort($hash);
$sources = array();
foreach ($hash as $key => $value) {
if (preg_match('/^src/i', $key) && $value != '') {
preg_match('/(\((\w+)\))?(.+)/', $value, $matches);
$sources[] = array('quality' => $matches[2], 'src' => do_shortcode($matches[3]));
}
}
return $sources;
}
// function to process the shortcode
static function video($attributes) {
$shortcode = new SublimeVideoShortcodes($attributes);
return $shortcode->generate_video_code();
}
// Process the shortcode for the floating lightbox feature
static function lightbox($atts, $content='') {
$attributes = array_merge(array(
'style' => 'display:none;',
'class' => '',
'lightbox_settings' => ''
), $atts);
$video = new SublimeVideoShortcodes($attributes);
return "<a class='sublime' href='#".$video->video_attributes['id']."' data-settings='".$attributes['lightbox_settings']."'>".do_shortcode($content)."</a>".$video->generate_video_code();
}
public function __construct($attributes=array()) {
$this->video_attributes = shortcode_atts(self::default_video_attributes(), $attributes);
$this->video_settings = shortcode_atts(self::default_video_settings(), $attributes);
$this->video_behaviors = self::behaviors($attributes);
$this->sources = self::sources($attributes);
$this->generate_id();
$this->generate_uid();
}
public function generate_id() {
if ($this->video_attributes['id'] == '') {
if (preg_match('/youtube-id:([^;\s]+)/i', $this->video_settings['settings'], $matches)) {
$this->video_attributes['id'] = $matches[1];
} else if ($this->sources[0]) {
$this->video_attributes['id'] = self::generateUID($this->sources[0]['src']);
}
}
}
public function generate_uid() {
if ($this->video_settings['uid'] == '' && $this->video_attributes['id'] != '') {
$this->video_settings['uid'] = $this->video_attributes['id'];
}
}
public function generate_video_code() {
$html = "<video ".$this->write_video_attributes()." ".$this->write_data_settings()." ".$this->video_behaviors.">\n";
foreach ($this->sources as $source) {
$data_quality = $source['quality'] != '' ? " data-quality='".$source['quality']."'" : '';
$html .= "\t<source src='".$source['src']."'".$data_quality." />\n";
}
$html .= "</video>\n";
return $html;
}
function write_video_attributes() {
$attrs = array();
foreach (array('id', 'class', 'style', 'width', 'height', 'poster', 'preload') as $key) {
if ($this->video_attributes[$key] != '') $attrs[] = $key."='".$this->video_attributes[$key]."'";
}
return join(' ', $attrs);
}
function write_data_settings() {
$data_attributes = array();
foreach (SublimeVideo::$allowed_data_attributes as $shortcode_attribute => $data_attribute) {
$data = null;
if ($this->video_settings[$shortcode_attribute] != '') {
$data = $this->video_settings[$shortcode_attribute];
} else if ($this->video_settings['data_'.$shortcode_attribute] != '') {
$data = $this->video_settings['data_'.$shortcode_attribute];
}
if ($data) $data_attributes[] = $data_attribute."='".$data."'";
}
return join(' ', $data_attributes);
}
}
// tell wordpress to register the sublimevideo shortcode
add_shortcode('sublimevideo-lightbox', array('SublimeVideoShortcodes', 'lightbox'));
// tell wordpress to register the sublimevideo shortcode
add_shortcode('sublimevideo', array('SublimeVideoShortcodes', 'video'));
?>