-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathableplayer.php
More file actions
237 lines (208 loc) · 6.75 KB
/
ableplayer.php
File metadata and controls
237 lines (208 loc) · 6.75 KB
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
<?php
/*
Plugin Name: Able Player
Plugin URI: https://github.com/ableplayer-wordpress
Description: Accessible HTML5 media player
Contributors: terrillthompson
Tags: html5, media, audio, video, accessibility
Version: 0.1.2
Requires at least: 4.9
Tested up to: 4.9
License: MIT
License URI: https://github.com/ableplayer-wordpress/LICENSE
*/
/*
*
* Disable the feature in WordPress that wraps everything (including Able Player code)
* in <p> tags
*
* NOTE: This will affect ALL content on site, and may have undesirable consequences
* Therefore it's commented out by default. Test before using.
*
*
*/
// remove_filter ('the_content', 'wpautop');
/*
*
* load styles and scripts to head
*
*/
function ableplayer_enqueue_scripts(){
// Documentation:
// http://codex.wordpress.org/Function_Reference/wp_enqueue_script
// Register/enqueue common scripts that can be called automatically with just their handles (as of WP 3.5)
wp_enqueue_script( 'jquery' );
// Register/enqueue other dependencies
wp_enqueue_script( 'js-cookie', plugins_url('thirdparty',__FILE__).'/js.cookie.js',array('jquery'));
wp_enqueue_script( 'vimeo', 'https://player.vimeo.com/api/player.js' );
// Register/enqueue Able Player JavaScript (two options; uncomment the one you prefer)
// JS Option 1: minified, for production
wp_enqueue_script( 'ableplayer', plugins_url('build',__FILE__).'/ableplayer.min.js',array('jquery'));
// JS Option 2: human-readable, for debugging
// wp_enqueue_script( 'ableplayer', plugins_url('build',__FILE__).'/ableplayer.js',array('jquery'));
// Register/enqueue Able Player CSS (two options; uncomment the one you prefer)
// CSS Option 1: minified, for production
wp_enqueue_style( 'ableplayer', plugins_url('build',__FILE__).'/ableplayer.min.css');
// CSS Option 2: human-readable; use this if you intend to change the styles and customize the player
// wp_enqueue_style( 'ableplayer', plugins_url('styles',__FILE__).'/ableplayer.css');
}
add_action( 'wp_enqueue_scripts', 'ableplayer_enqueue_scripts');
/*
*
* Add support for [ableplayer] shortcode
*
*
*/
function ableplayer_shortcode( $atts,$content=null ) {
// Each of the following attributes can be passed with the [ableplayer] shortcode
// 'id' and 'type' (video or audio) is required
// normalize attribute keys, lowercase
$atts = array_change_key_case((array)$atts, CASE_LOWER);
// build complete array of all attributes; defaults will be overridden with user values
$all_atts = shortcode_atts([
'id' => ableplayer_get_unique_id(),
'youtube-id' => '',
'youtube-desc-id' => '',
'youtube-nocookie' => '',
'vimeo-id' => '',
'vimeo-desc-id' => '',
'autoplay' => 'false',
'preload' => 'auto',
'loop' => 'false',
'playsinline' => 'true',
'hidecontrols' => 'false',
'poster' => '',
'width' => '',
'height' => '',
'heading' => '',
'speed' => 'animals',
'start' => '',
'volume' => '',
'seekinterval' => '',
'nowplaying' => 'false',
'skin' => '2020'
], $atts );
// output
if (!($all_atts['youtube-id'] || $all_atts['vimeo-id'])) {
// required fields are missing
return false;
}
else {
// build a video player!
$o = '<video ';
$o .= ' id="'.$all_atts['id'].'"';
$o .= ' data-able-player';
if (ableplayer_is_true($all_atts['autoplay'])) {
$o .= ' autoplay';
}
if (ableplayer_is_true($all_atts['loop'])) {
$o .= ' loop';
}
if (ableplayer_is_true($all_atts['playsinline'])) {
$o .= ' playsinline';
}
if (ableplayer_is_true($all_atts['hidecontrols'])) {
$o .= ' data-hide-controls';
}
if (!empty($all_atts['preload'])) {
$o .= ' preload="'.$all_atts['preload'].'"';
}
if (!empty($all_atts['poster'])) {
$o .= ' poster="'.$all_atts['poster'].'"';
}
if (!empty($all_atts['width'])) {
$o .= ' width="'.$all_atts['width'].'"';
}
if (!empty($all_atts['height'])) {
$o .= ' height="'.$all_atts['height'].'"';
}
if (!empty($all_atts['poster'])) {
$o .= ' poster="'.$all_atts['poster'].'"';
}
if (!empty($all_atts['heading'])) {
$o .= ' data-heading-level="'.$all_atts['heading'].'"';
}
if (!empty($all_atts['speed'])) {
$o .= ' data-speed-icons="'.$all_atts['speed'].'"';
}
if (!empty($all_atts['start'])) {
$o .= ' data-start-time="'.$all_atts['start'].'"';
}
if (!empty($all_atts['volume'])) {
$o .= 'data-volume="'.$all_atts['volume'].'"';
}
if (!empty($all_atts['seekinterval'])) {
$o .= ' data-seek-interval="'.$all_atts['seekinterval'].'"';
}
if (!empty($all_atts['nowplaying'])) {
$o .= ' data-show-now-playing="'.$all_atts['nowplaying'].'"';
}
if (!empty($all_atts['skin'])) {
$o .= ' data-skin="'.$all_atts['skin'].'"';
}
if (!empty($all_atts['youtube-id'])) {
$o .= ' data-youtube-id="'.$all_atts['youtube-id'].'"';
}
if (!empty($all_atts['youtube-desc-id'])) {
$o .= ' data-youtube-desc-id="'.$all_atts['youtube-desc-id'].'"';
}
if (!empty($all_atts['youtube-nocookie'])) {
$o .= ' data-youtube-nocookie="'.$all_atts['youtube-nocookie'].'"';
}
if (!empty($all_atts['vimeo-id'])) {
$o .= ' data-vimeo-id="'.$all_atts['vimeo-id'].'"';
}
if (!empty($all_atts['vimeo-desc-id'])) {
$o .= ' data-vimeo-desc-id="'.$all_atts['vimeo-desc-id'].'"';
}
$o .= '>';
// enclosing tags
if (!is_null($content)) {
// run shortcode parser recursively
$o .= do_shortcode($content);
}
// end media tag
$o .= '</video>';
// return output
// To display HTML <video> code (for debugging), uncomment first return statement
// For production, uncomment second return statement
// return esc_html($o);
return $o;
}
}
add_shortcode('ableplayer', 'ableplayer_shortcode');
/*
*
* Supporting functions
*
*/
function ableplayer_get_unique_id() {
// use add_option(), get_option(), update_option(), & delete_option()
// to track the number of Able Player instances on the current page
// return a unique id for each new player instance,
// using the form 'able_player_1', 'able_player_2', etc.
$this_player = 1;
$num_players = get_option('able_player_count');
if (empty($num_players)) {
update_option('able_player_count',$this_player,false);
return 'able_player_'.$this_player;
}
else {
// there's already at least one player
$this_player = $num_players + 1;
update_option('able_player_count',$this_player,false);
return 'able_player_'.$this_player;
}
}
function ableplayer_is_true($var) {
// $var is a Boolean parameter
// check for all variations that might be considered 'true'
// return true if... well, true
if ($var == '1' || $var == 'yes' || $var == 'true') {
return true;
}
else {
return false;
}
}
?>