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
|
<?php
/* Widget Name: Video Player Description: Play all your self or externally hosted videos in a customizable video player. Author: SiteOrigin Author URI: https://siteorigin.com Documentation: https://siteorigin.com/widgets-bundle/video-player-widget/ */
class SiteOrigin_Widget_Video_Widget extends SiteOrigin_Widget {
function __construct() {
parent::__construct( 'sow-video', __( 'SiteOrigin Video Player', 'so-widgets-bundle' ), array( 'description' => __( 'A video player widget.', 'so-widgets-bundle' ), 'help' => 'http://siteorigin.com/widgets-bundle/video-widget-documentation/' ), array(), false, plugin_dir_path( __FILE__ ) ); }
function get_widget_form() { return array( 'title' => array( 'type' => 'text', 'label' => __( 'Title', 'so-widgets-bundle' ) ), 'host_type' => array( 'type' => 'radio', 'label' => __( 'Video location', 'so-widgets-bundle' ), 'default' => 'self', 'options' => array( 'self' => __( 'Self hosted', 'so-widgets-bundle' ), 'external' => __( 'Externally hosted', 'so-widgets-bundle' ), ),
// This field should be a video type state emitter 'state_emitter' => array( 'callback' => 'select', 'args' => array( 'video_type' ) ) ),
'video' => array( 'type' => 'section', 'label' => __( 'Video File', 'so-widgets-bundle' ), 'fields' => array( 'self_sources' => array( 'type' => 'repeater', 'label' => __( 'Sources', 'so-widgets-bundle' ), 'fields' => array( 'self_video' => array( 'type' => 'media', 'fallback' => true, 'label' => __( 'Select video', 'so-widgets-bundle' ), 'default' => '', 'library' => 'video', ), ), 'state_handler' => array( 'video_type[self]' => array( 'show' ), 'video_type[external]' => array( 'hide' ), ), ), 'self_poster' => array( 'type' => 'media', 'label' => __( 'Select cover image', 'so-widgets-bundle' ), 'default' => '', 'library' => 'image', 'state_handler' => array( 'video_type[self]' => array( 'show' ), 'video_type[external]' => array( 'hide' ), ), ), 'external_video' => array( 'type' => 'text', 'sanitize' => 'url', 'label' => __( 'Video URL', 'so-widgets-bundle' ), 'state_handler' => array( 'video_type[external]' => array( 'show' ), 'video_type[self]' => array( 'hide' ), ), ), ), ),
'playback' => array( 'type' => 'section', 'label' => __( 'Video Playback', 'so-widgets-bundle' ), 'fields' => array( 'autoplay' => array( 'type' => 'checkbox', 'default' => false, 'label' => __( 'Autoplay', 'so-widgets-bundle' ) ), 'oembed' => array( 'type' => 'checkbox', 'default' => true, 'label' => __( 'Use oEmbed', 'so-widgets-bundle' ), 'description' => __( 'Always use the embedded video rather than the MediaElement player.', 'so-widgets-bundle' ), 'state_handler' => array( 'video_type[external]' => array( 'show' ), 'video_type[self]' => array( 'hide' ), ) ), 'related_videos' => array( 'type' => 'checkbox', 'default' => true, 'label' => __( 'Show related videos.', 'so-widgets-bundle' ), 'description' => __( 'If the external host supports it.', 'so-widgets-bundle' ), 'state_handler' => array( 'video_type[external]' => array( 'show' ), 'video_type[self]' => array( 'hide' ), ) ), ), ), ); }
function enqueue_frontend_scripts( $instance ) { $video_host = empty( $instance['host_type'] ) ? '' : $instance['host_type']; if ( $video_host == 'external' ) { $video_host = ! empty( $instance['video']['external_video'] ) ? $this->get_host_from_url( $instance['video']['external_video'] ) : ''; } if ( $this->is_skinnable_video_host( $video_host ) ) { if ( $video_host == 'vimeo' && ! wp_script_is( 'froogaloop' ) ) { wp_enqueue_script( 'froogaloop' ); } if ( ! wp_style_is( 'sow-html-player-responsive' ) ) { wp_enqueue_style( 'html-player-responsive', plugin_dir_url( __FILE__ ) . 'css/html-player-responsive.css', array(), SOW_BUNDLE_VERSION ); } if ( ! wp_style_is( 'wp-mediaelement' ) ) { wp_enqueue_style( 'wp-mediaelement' ); } if ( ! wp_script_is( 'so-video-widget' ) ) { wp_enqueue_script( 'so-video-widget', plugin_dir_url( __FILE__ ) . 'js/so-video-widget' . SOW_BUNDLE_JS_SUFFIX . '.js', array( 'jquery', 'mediaelement' ), SOW_BUNDLE_VERSION ); } } parent::enqueue_frontend_scripts( $instance ); }
function get_template_name( $instance ) { return 'default'; }
function get_template_variables( $instance, $args ) { static $player_id = 1;
$self_sources = array(); $external_src = ''; $external_video_type = ''; $poster = ''; $video_host = $instance['host_type']; if ( $video_host == 'self' ) {
if ( isset( $instance['video']['self_sources'] ) ) { foreach ( $instance['video']['self_sources'] as $source ) { $src = ''; $video_type = ''; if ( ! empty( $source['self_video'] ) ) { // Handle an attachment video $src = wp_get_attachment_url( $source['self_video'] ); $video_type = get_post_mime_type( $source['self_video'] ); } else if ( ! empty( $source['self_video_fallback'] ) ) { // Handle an external URL video $src = $source['self_video_fallback']; $vid_info = wp_check_filetype( basename( $source['self_video_fallback'] ) ); $video_type = $vid_info['type']; } if ( ! empty( $src ) ) { $self_sources[] = array( 'src' => $src, 'video_type' => $video_type ); } } } $poster = ! empty( $instance['video']['self_poster'] ) ? wp_get_attachment_url( $instance['video']['self_poster'] ) : ''; } else { $video_host = $this->get_host_from_url( $instance['video']['external_video'] ); $external_video_type = 'video/' . $video_host; $external_src = ! empty( $instance['video']['external_video'] ) ? $instance['video']['external_video'] : ''; }
$return = array( 'player_id' => 'sow-player-' . ( $player_id ++ ), 'host_type' => $instance['host_type'], 'src' => $external_src, 'sources' => $self_sources, 'video_type' => $external_video_type, 'is_skinnable_video_host' => $this->is_skinnable_video_host( $video_host ), 'poster' => $poster, 'autoplay' => ! empty( $instance['playback']['autoplay'] ), 'related_videos' => ! empty( $instance['playback']['related_videos'] ), 'skin_class' => 'default' );
// Force oEmbed for this video if ( $instance['host_type'] == 'external' && $instance['playback']['oembed'] ) { $return['is_skinnable_video_host'] = false; }
return $return; }
function get_style_name( $instance ) { // For now, we'll only use the default style return ''; }
/** * Get the video host from the URL * * @param $video_url * * @return string */ private function get_host_from_url( $video_url ) { preg_match( '/https?:\/\/(www.)?([A-Za-z0-9\-]+)\./', $video_url, $matches );
return ( ! empty( $matches ) && count( $matches ) > 2 ) ? $matches[2] : ''; }
/** * Check if the current host is skinnable * * @param $video_host * * @return bool */ private function is_skinnable_video_host( $video_host ) { global $wp_version;
return $video_host == 'self' || ( ( $video_host == 'youtube' || $video_host == 'vimeo' ) && $wp_version >= 4.2 ); }
/** * * Update older versions of widget to use multiple sources. * * @param $instance * * @return mixed */ function modify_instance( $instance ) { $video_src = array(); if ( isset( $instance['video']['self_video'] ) && ! empty( $instance['video']['self_video'] ) ) { $video_src['self_video'] = $instance['video']['self_video']; unset( $instance['video']['self_video'] ); } if ( isset( $instance['video']['self_video_fallback'] ) && ! empty( $instance['video']['self_video_fallback'] ) ) { $video_src['self_video_fallback'] = $instance['video']['self_video_fallback']; unset( $instance['video']['self_video_fallback'] ); } if ( ! empty( $video_src ) ) { if ( ! isset( $instance['video']['self_sources'] ) ) { $instance['video']['self_sources'] = array(); } $instance['video']['self_sources'][] = $video_src; }
return $instance; } }
siteorigin_widget_register( 'video', __FILE__, 'SiteOrigin_Widget_Video_Widget' );
|