forked from zencoder/video-js-for-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
video-js.php
executable file
·292 lines (238 loc) · 8.16 KB
/
video-js.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
<?php
/**
* @package Video.js
* @version 4.5.0
*/
/*
Plugin Name: Video.js - HTML5 Video Player for WordPress
Plugin URI: http://videojs.com/
Description: Self-hosted responsive HTML5 video for WordPress, built on the widely used Video.js HTML5 video player library. Allows you to embed video in your post or page using HTML5 with Flash fallback support for non-HTML5 browsers.
Author: <a href="http://www.nosecreekweb.ca">Dustin Lammiman</a>, <a href="http://steveheffernan.com">Steve Heffernan</a>
Version: 4.5.0
*/
define('NAME', 'Video.js');
/*************** DEFINE ***************/
$plugin_dir = plugin_dir_path( __FILE__ );
/* The options page */
include_once($plugin_dir . 'admin.php');
/* Useful Functions */
include_once($plugin_dir . 'lib.php');
/* Register the scripts and enqueue css files */
function register_videojs(){
$options = get_option('videojs_options');
//wp_register_style( 'videojs-plugin', plugins_url( 'plugin-styles.css' , __FILE__ ) );
//wp_enqueue_style( 'videojs-plugin' );
if($options['videojs_cdn'] == 'on') { //use the cdn hosted version
wp_register_script( 'videojs', '//vjs.zencdn.net/5.4.6/video.min.js' );
wp_register_style( 'videojs', '//vjs.zencdn.net/5.4.6/video-js.min.css' );
wp_enqueue_style( 'videojs' );
} else { //use the self hosted version
wp_register_script( 'videojs', plugins_url( 'videojs/video.min.js' , __FILE__ ) );
wp_register_style( 'videojs', plugins_url( 'videojs/video-js.min.css' , __FILE__ ) );
wp_enqueue_style( 'videojs' );
}
wp_register_script( 'videojs-youtube', plugins_url( 'videojs/vjs.youtube.min.js' , __FILE__ ) );
}
add_action( 'wp_enqueue_scripts', 'register_videojs' );
/* Include the scripts before </body> */
function add_videojs_header(){
wp_enqueue_script( 'videojs' );
wp_enqueue_script( 'videojs-youtube' );
}
/* Include custom color styles in the site header */
function videojs_custom_colors() {
$options = get_option('videojs_options');
if($options['videojs_color_one'] != "#ccc" || $options['videojs_color_two'] != "#66A8CC" || $options['videojs_color_three'] != "#000") { //If custom colors are used
$color3 = vjs_hex2RGB($options['videojs_color_three'], true); //Background color is rgba
echo "
<style type='text/css'>
.video-js { color: " . $options['videojs_color_one'] . " }
.video-js .vjs-play-progress, .video-js .vjs-volume-level { background-color: " . $options['videojs_color_two'] . " }
.video-js .vjs-control-bar, .video-js .vjs-big-play-button { background: rgba(" . $color3 . ",0.7) }
.video-js .vjs-slider { background: rgba(" . $color3 . ",0.2333333333333333) }
</style>
";
}
}
add_action( 'wp_head', 'videojs_custom_colors' );
/* Prevent mixed content warnings for the self-hosted version */
function add_videojs_swf(){
$options = get_option('videojs_options');
if($options['videojs_cdn'] != 'on') {
echo '
<script type="text/javascript">
if(typeof videojs != "undefined") {
videojs.options.flash.swf = "'. plugins_url( 'videojs/video-js.swf' , __FILE__ ) .'";
}
document.createElement("video");document.createElement("audio");document.createElement("track");
</script>
';
} else {
echo '
<script type="text/javascript"> document.createElement("video");document.createElement("audio");document.createElement("track"); </script>
';
}
}
add_action('wp_head','add_videojs_swf');
/* The [video] or [videojs] shortcode */
function video_shortcode($atts, $content=null){
add_videojs_header();
$options = get_option('videojs_options'); //load the defaults
extract(shortcode_atts(array(
'mp4' => '',
'webm' => '',
'ogg' => '',
'youtube' => '',
'poster' => '',
'width' => $options['videojs_width'],
'height' => $options['videojs_height'],
'preload' => $options['videojs_preload'],
'autoplay' => $options['videojs_autoplay'],
'loop' => '',
'controls' => '',
'id' => '',
'class' => '',
'muted' => ''
), $atts));
$dataSetup = array();
// ID is required for multiple videos to work
if ($id == '')
$id = 'videojs_id_'.rand();
// MP4 Source Supplied
if ($mp4)
$mp4_source = '<source src="'.$mp4.'" type=\'video/mp4\' />';
else
$mp4_source = '';
// WebM Source Supplied
if ($webm)
$webm_source = '<source src="'.$webm.'" type=\'video/webm; codecs="vp8, vorbis"\' />';
else
$webm_source = '';
// Ogg source supplied
if ($ogg)
$ogg_source = '<source src="'.$ogg.'" type=\'video/ogg; codecs="theora, vorbis"\' />';
else
$ogg_source = '';
if ($youtube) {
//$dataSetup['forceSSL'] = 'true';
$dataSetup['techOrder'] = array("youtube");
$dataSetup['sources'] = array(array(
"type" => "video/youtube",
"src" => $youtube
));
$dataSetup['youtube'] = array(
"iv_load_policy" => 3
);
}
// Poster image supplied
if ($poster)
$poster_attribute = ' poster="'.$poster.'"';
else
$poster_attribute = '';
// Preload the video?
if ($preload == "auto" || $preload == "true" || $preload == "on")
$preload_attribute = ' preload="auto"';
elseif ($preload == "metadata")
$preload_attribute = ' preload="metadata"';
else
$preload_attribute = ' preload="none"';
// Autoplay the video?
if ($autoplay == "true" || $autoplay == "on")
$autoplay_attribute = " autoplay";
else
$autoplay_attribute = "";
// Loop the video?
if ($loop == "true")
$loop_attribute = " loop";
else
$loop_attribute = "";
// Controls?
if ($controls == "false")
$controls_attribute = "";
else
$controls_attribute = " controls";
// Is there a custom class?
if ($class)
$class = ' ' . $class;
// Muted?
if ($muted == "true")
$muted_attribute = " muted";
else
$muted_attribute = "";
// Tracks
if(!is_null( $content ))
$track = do_shortcode($content);
else
$track = "";
// Responsive Fluid
if($options['videojs_responsive'] == 'on') //add the responsive css
$fluid = " vjs-fluid";
else
$fluid = "";
$jsonDataSetup = str_replace('\\/', '/', json_encode($dataSetup));
//Output the <video> tag
$videojs = <<<_end_
<!-- Begin Video.js -->
<video id="{$id}" class="video-js{$class}{$fluid}" width="{$width}" height="{$height}"{$poster_attribute}{$controls_attribute}{$preload_attribute}{$autoplay_attribute}{$loop_attribute}{$muted_attribute} data-setup='{$jsonDataSetup}'>
{$mp4_source}
{$webm_source}
{$ogg_source}{$track}
</video>
<!-- End Video.js -->
_end_;
return $videojs;
}
add_shortcode('videojs', 'video_shortcode');
//Only use the [video] shortcode if the correct option is set
$options = get_option('videojs_options');
if( !array_key_exists('videojs_video_shortcode', $options) || $options['videojs_video_shortcode'] ){
add_shortcode('video', 'video_shortcode');
}
/* The [track] shortcode */
function track_shortcode($atts, $content=null){
extract(shortcode_atts(array(
'kind' => '',
'src' => '',
'srclang' => '',
'label' => '',
'default' => ''
), $atts));
if($kind)
$kind = " kind='" . $kind . "'";
if($src)
$src = " src='" . $src . "'";
if($srclang)
$srclang = " srclang='" . $srclang . "'";
if($label)
$label = " label='" . $label . "'";
if($default == "true" || $default == "default")
$default = " default";
else
$default = "";
$track = "
<track" . $kind . $src . $srclang . $label . $default . " />
";
return $track;
}
add_shortcode('track', 'track_shortcode');
/* TinyMCE Shortcode Generator */
function video_js_button() {
if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') )
return;
if ( get_user_option('rich_editing') == 'true' ) {
add_filter('mce_external_plugins', 'video_js_mce_plugin');
add_filter('mce_buttons', 'register_video_js_button');
}
}
add_action('init', 'video_js_button');
function register_video_js_button($buttons) {
array_push($buttons, "|", "videojs");
$options = get_option('videojs_options');
echo('<div style="display:none"><input type="hidden" id="videojs-autoplay-default" value="' . $options['videojs_autoplay'] . '"><input type="hidden" id="videojs-preload-default" value="' . $options['videojs_preload'] . '"></div>'); //the default values from the admin screen, to be used by our javascript
return $buttons;
}
function video_js_mce_plugin($plugin_array) {
$plugin_array['videojs'] = plugins_url( 'mce-button.js' , __FILE__ );
return $plugin_array;
}
?>