Skip to content

Commit 9bd1267

Browse files
committed
Add Vimeo support & simplify shortcode handling
1 parent a1c2110 commit 9bd1267

File tree

1 file changed

+78
-170
lines changed

1 file changed

+78
-170
lines changed

ableplayer.php

Lines changed: 78 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
Description: Accessible HTML5 media player
66
Contributors: terrillthompson
77
Tags: html5, media, audio, video, accessibility
8-
Version: 0.1.1
9-
Requires at least: 2.6
10-
Tested up to: 4.5
8+
Version: 0.1.2
9+
Requires at least: 4.9
10+
Tested up to: 4.9
1111
License: MIT
1212
License URI: https://github.com/ableplayer-wordpress/LICENSE
1313
*/
@@ -40,14 +40,15 @@ function ableplayer_enqueue_scripts(){
4040

4141
// Register/enqueue other dependencies
4242
wp_enqueue_script( 'js-cookie', plugins_url('thirdparty',__FILE__).'/js.cookie.js',array('jquery'));
43+
wp_enqueue_script( 'vimeo', 'https://player.vimeo.com/api/player.js' );
4344

4445
// Register/enqueue Able Player JavaScript (two options; uncomment the one you prefer)
4546

4647
// JS Option 1: minified, for production
47-
// wp_enqueue_script( 'ableplayer', plugins_url('build',__FILE__).'/ableplayer.min.js',array('jquery'));
48+
wp_enqueue_script( 'ableplayer', plugins_url('build',__FILE__).'/ableplayer.min.js',array('jquery'));
4849

4950
// JS Option 2: human-readable, for debugging
50-
wp_enqueue_script( 'ableplayer', plugins_url('build',__FILE__).'/ableplayer.js',array('jquery'));
51+
// wp_enqueue_script( 'ableplayer', plugins_url('build',__FILE__).'/ableplayer.js',array('jquery'));
5152

5253
// Register/enqueue Able Player CSS (two options; uncomment the one you prefer)
5354

@@ -79,8 +80,13 @@ function able_player_shortcode( $atts,$content=null ) {
7980
// build complete array of all attributes; defaults will be overridden with user values
8081
$all_atts = shortcode_atts([
8182
'id' => get_unique_id(),
82-
'type' => '',
83+
'youtube-id' => '',
84+
'youtube-desc-id' => '',
85+
'youtube-nocookie' => '',
86+
'vimeo-id' => '',
87+
'vimeo-desc-id' => '',
8388
'autoplay' => 'false',
89+
'preload' => 'auto',
8490
'loop' => 'false',
8591
'playsinline' => 'true',
8692
'hidecontrols' => 'false',
@@ -92,135 +98,67 @@ function able_player_shortcode( $atts,$content=null ) {
9298
'start' => '',
9399
'volume' => '',
94100
'seekinterval' => '',
95-
'nowplaying' => 'false'
101+
'nowplaying' => 'false',
102+
'skin' => '2020'
96103
], $atts );
97104

98105
// output
99-
if (!$all_atts['type']) {
106+
if (!($all_atts['youtube-id'] || $all_atts['vimeo-id'])) {
100107
// required fields are missing
101108
return false;
102109
}
103110
else {
104-
$type = $all_atts['type'];
105-
if (!($type == 'audio' || $type == 'video')) {
106-
// type is not a supported value
107-
return false;
111+
// build a video player!
112+
$o = '<video ';
113+
$o .= ' id="'.$all_atts['id'].'"';
114+
$o .= ' data-able-player';
115+
if (is_true($all_atts['autoplay'])) {
116+
$o .= ' autoplay';
108117
}
109-
else {
110-
// build a media player!
111-
$o = '<'.$type;
112-
$o .= ' id="'.$all_atts['id'].'"';
113-
$o .= ' data-able-player';
114-
$o .= ' preload="auto"';
115-
if (is_true($all_atts['autoplay'])) {
116-
$o .= ' autoplay';
117-
}
118-
if (is_true($all_atts['loop'])) {
119-
$o .= ' loop';
120-
}
121-
if (is_true($all_atts['playsinline'])) {
122-
$o .= ' playsinline';
123-
}
124-
if (is_true($all_atts['hidecontrols'])) {
125-
$o .= ' data-hide-controls';
126-
}
127-
if (!empty($all_atts['poster'])) {
128-
$o .= ' poster="'.$all_atts['poster'].'"';
129-
}
130-
if (!empty($all_atts['width'])) {
131-
$o .= ' width="'.$all_atts['width'].'"';
132-
}
133-
if (!empty($all_atts['height'])) {
134-
$o .= ' height="'.$all_atts['height'].'"';
135-
}
136-
if (!empty($all_atts['poster'])) {
137-
$o .= ' poster="'.$all_atts['poster'].'"';
138-
}
139-
if (!empty($all_atts['heading'])) {
140-
$o .= ' data-heading-level="'.$all_atts['heading'].'"';
141-
}
142-
if (!empty($all_atts['speed'])) {
143-
$o .= ' data-speed-icons="'.$all_atts['speed'].'"';
144-
}
145-
if (!empty($all_atts['start'])) {
146-
$o .= ' data-start-time="'.$all_atts['start'].'"';
147-
}
148-
if (!empty($all_atts['volume'])) {
149-
$o .= 'data-volume="'.$all_atts['volume'].'"';
150-
}
151-
if (!empty($all_atts['seekinterval'])) {
152-
$o .= ' data-seek-interval="'.$all_atts['seekinterval'].'"';
153-
}
154-
if (!empty($all_atts['shownowplaying'])) {
155-
$o .= ' data-show-now-playing="'.$all_atts['shownowplaying'].'"';
156-
}
157-
$o .= '>';
158-
159-
// enclosing tags
160-
if (!is_null($content)) {
161-
// run shortcode parser recursively
162-
$o .= do_shortcode($content);
163-
}
164-
165-
// end media tag
166-
$o .= '</'.$type.'>';
167-
168-
// return output
169-
// To display HTML <audio> or <video> code (for debugging), uncomment first return statement
170-
// For production, uncomment second return statement
171-
// return esc_html($o);
172-
return $o;
118+
if (is_true($all_atts['loop'])) {
119+
$o .= ' loop';
173120
}
174-
}
175-
}
176-
add_shortcode('able-player', 'able_player_shortcode');
177-
178-
/*
179-
*
180-
* Add support for [able-source] shortcode
181-
*
182-
*
183-
*/
184-
185-
function able_source_shortcode( $atts,$content=null ) {
186-
187-
// Each of the following attributes can be passed with the [able-source] shortcode
188-
// Either 'src', youtube-id, or vimeo-id is required
189-
190-
$all_atts = shortcode_atts([
191-
'src' => '',
192-
'type' => '',
193-
'desc-src' => '',
194-
'sign-src' => '',
195-
'youtube-id' => '',
196-
'youtube-desc-id' => '',
197-
'youtube-nocookie' => '',
198-
'vimeo-id' => '',
199-
'vimeo-desc-id' => ''
200-
], $atts );
201-
202-
// normalize attribute keys, lowercase
203-
$atts = array_change_key_case((array)$atts, CASE_LOWER);
204-
205-
// output
206-
if (!($all_atts['src'] || $all_atts['youtube-id'] || $all_atts['youtube-desc-id'])) {
207-
// there is no media source
208-
return false;
209-
}
210-
else {
211-
// build a source tag
212-
$o = '<source';
213-
if (!empty($all_atts['src'])) {
214-
$o .= ' src="'.$all_atts['src'].'"';
121+
if (is_true($all_atts['playsinline'])) {
122+
$o .= ' playsinline';
123+
}
124+
if (is_true($all_atts['hidecontrols'])) {
125+
$o .= ' data-hide-controls';
215126
}
216-
if (!empty($all_atts['type'])) {
217-
$o .= ' type="'.$all_atts['type'].'"';
127+
if (!empty($all_atts['preload'])) {
128+
$o .= ' preload="'.$all_atts['preload'].'"';
218129
}
219-
if (!empty($all_atts['desc-src'])) {
220-
$o .= ' data-desc-src="'.$all_atts['desc-src'].'"';
130+
if (!empty($all_atts['poster'])) {
131+
$o .= ' poster="'.$all_atts['poster'].'"';
221132
}
222-
if (!empty($all_atts['sign-src'])) {
223-
$o .= ' data-sign-src="'.$all_atts['sign-src'].'"';
133+
if (!empty($all_atts['width'])) {
134+
$o .= ' width="'.$all_atts['width'].'"';
135+
}
136+
if (!empty($all_atts['height'])) {
137+
$o .= ' height="'.$all_atts['height'].'"';
138+
}
139+
if (!empty($all_atts['poster'])) {
140+
$o .= ' poster="'.$all_atts['poster'].'"';
141+
}
142+
if (!empty($all_atts['heading'])) {
143+
$o .= ' data-heading-level="'.$all_atts['heading'].'"';
144+
}
145+
if (!empty($all_atts['speed'])) {
146+
$o .= ' data-speed-icons="'.$all_atts['speed'].'"';
147+
}
148+
if (!empty($all_atts['start'])) {
149+
$o .= ' data-start-time="'.$all_atts['start'].'"';
150+
}
151+
if (!empty($all_atts['volume'])) {
152+
$o .= 'data-volume="'.$all_atts['volume'].'"';
153+
}
154+
if (!empty($all_atts['seekinterval'])) {
155+
$o .= ' data-seek-interval="'.$all_atts['seekinterval'].'"';
156+
}
157+
if (!empty($all_atts['nowplaying'])) {
158+
$o .= ' data-show-now-playing="'.$all_atts['nowplaying'].'"';
159+
}
160+
if (!empty($all_atts['skin'])) {
161+
$o .= ' data-skin="'.$all_atts['skin'].'"';
224162
}
225163
if (!empty($all_atts['youtube-id'])) {
226164
$o .= ' data-youtube-id="'.$all_atts['youtube-id'].'"';
@@ -238,54 +176,24 @@ function able_source_shortcode( $atts,$content=null ) {
238176
$o .= ' data-vimeo-desc-id="'.$all_atts['vimeo-desc-id'].'"';
239177
}
240178
$o .= '>';
241-
}
242-
return $o;
243-
}
244-
add_shortcode('able-source', 'able_source_shortcode');
245-
246-
/*
247-
*
248-
* Add support for [able-track] shortcode
249-
*
250-
*
251-
*/
252179

253-
function able_track_shortcode( $atts,$content=null ) {
254-
255-
// Each of the following attributes can be passed with the [able-track] shortcode
256-
// 'kind' and 'src' are required
257-
258-
$all_atts = shortcode_atts([
259-
'kind' => '',
260-
'src' => '',
261-
'srclang' => '',
262-
'label' => ''
263-
], $atts );
180+
// enclosing tags
181+
if (!is_null($content)) {
182+
// run shortcode parser recursively
183+
$o .= do_shortcode($content);
184+
}
264185

265-
// normalize attribute keys, lowercase
266-
$atts = array_change_key_case((array)$atts, CASE_LOWER);
186+
// end media tag
187+
$o .= '</video>';
267188

268-
// output
269-
if (!($all_atts['kind'] && $all_atts['src'])) {
270-
// required fields are missing
271-
return false;
272-
}
273-
else {
274-
// build a track tag
275-
$o = '<track';
276-
$o .= ' kind="'.$all_atts['kind'].'"';
277-
$o .= ' src="'.$all_atts['src'].'"';
278-
if (!empty($all_atts['srclang'])) {
279-
$o .= ' srclang="'.$all_atts['srclang'].'"';
280-
}
281-
if (!empty($all_atts['label'])) {
282-
$o .= ' label="'.$all_atts['label'].'"';
283-
}
284-
$o .= '>';
189+
// return output
190+
// To display HTML <video> code (for debugging), uncomment first return statement
191+
// For production, uncomment second return statement
192+
// return esc_html($o);
285193
return $o;
286194
}
287195
}
288-
add_shortcode('able-track', 'able_track_shortcode');
196+
add_shortcode('able-player', 'able_player_shortcode');
289197

290198
/*
291199
*
@@ -300,16 +208,16 @@ function get_unique_id() {
300208
// return a unique id for each new player instance,
301209
// using the form 'able_player_1', 'able_player_2', etc.
302210
$this_player = 1;
303-
$num_players = get_option('able-player-count');
211+
$num_players = get_option('able_player_count');
304212
if (empty($num_players)) {
305-
add_option('able-player-count',$player_count);
306-
return 'able-player-'.$this_player;
213+
update_option('able_player_count',$this_player,false);
214+
return 'able_player_'.$this_player;
307215
}
308216
else {
309217
// there's already at least one player
310218
$this_player = $num_players + 1;
311-
update_option('able-player-count',$this_player);
312-
return 'able-player-'.$this_player;
219+
update_option('able_player_count',$this_player,false);
220+
return 'able_player_'.$this_player;
313221
}
314222
}
315223

0 commit comments

Comments
 (0)