Skip to content

Commit 004bc5c

Browse files
committed
Add support for shortcodes
1 parent 0b684a6 commit 004bc5c

File tree

2 files changed

+360
-20
lines changed

2 files changed

+360
-20
lines changed

ableplayer.php

Lines changed: 275 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
Description: Accessible HTML5 media player
66
Contributors: terrillthompson
77
Tags: html5, media, audio, video, accessibility
8-
Version: 0.1
9-
Stable tag: 0.1
8+
Version: 0.1.1
109
Requires at least: 2.6
1110
Tested up to: 4.5
1211
License: MIT
@@ -40,29 +39,291 @@ function ableplayer_enqueue_scripts(){
4039
wp_enqueue_script( 'jquery' );
4140

4241
// Register/enqueue other dependencies
43-
wp_enqueue_script( 'modernizer', plugins_url('thirdparty',__FILE__).'/modernizr.custom.js');
4442
wp_enqueue_script( 'js-cookie', plugins_url('thirdparty',__FILE__).'/js.cookie.js',array('jquery'));
4543

46-
// Register/enqueue Able Player script
44+
// Register/enqueue Able Player JavaScript (two options; uncomment the one you prefer)
45+
46+
// JS Option 1: minified, for production
47+
// wp_enqueue_script( 'ableplayer', plugins_url('build',__FILE__).'/ableplayer.min.js',array('jquery'));
48+
49+
// JS Option 2: human-readable, for debugging
4750
wp_enqueue_script( 'ableplayer', plugins_url('build',__FILE__).'/ableplayer.js',array('jquery'));
4851

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

51-
// Use minimized CSS that's included in Able Player build
52-
// wp_enqueue_style( 'ableplayer', plugins_url('build',__FILE__).'/ableplayer.min.css');
54+
// CSS Option 1: minified, for production
55+
wp_enqueue_style( 'ableplayer', plugins_url('build',__FILE__).'/ableplayer.min.css');
5356

54-
// Use human-readable CSS if you intend to change the styles and customize the player
55-
wp_enqueue_style( 'ableplayer', plugins_url('styles',__FILE__).'/ableplayer.css');
57+
// CSS Option 2: human-readable; use this if you intend to change the styles and customize the player
58+
// wp_enqueue_style( 'ableplayer', plugins_url('styles',__FILE__).'/ableplayer.css');
5659

5760
}
5861
add_action( 'wp_enqueue_scripts', 'ableplayer_enqueue_scripts');
5962

60-
function ableplayer_head() {
6163

62-
// add a variable that points to the current plugin directory
63-
echo "<script>\n";
64-
echo 'var pluginUrl = "'.plugins_url('',__FILE__).'";'."\n";
65-
echo "</script>\n";
64+
/*
65+
*
66+
* Add support for [able-player] shortcode
67+
*
68+
*
69+
*/
70+
71+
function able_player_shortcode( $atts,$content=null ) {
72+
73+
// Each of the following attributes can be passed with the [able-player] shortcode
74+
// 'id' and 'type' (video or audio) is required
75+
76+
// normalize attribute keys, lowercase
77+
$atts = array_change_key_case((array)$atts, CASE_LOWER);
78+
79+
// build complete array of all attributes; defaults will be overridden with user values
80+
$all_atts = shortcode_atts([
81+
'id' => get_unique_id(),
82+
'type' => '',
83+
'autoplay' => 'false',
84+
'loop' => 'false',
85+
'playsinline' => 'true',
86+
'hidecontrols' => 'false',
87+
'poster' => '',
88+
'width' => '',
89+
'height' => '',
90+
'heading' => '',
91+
'speed' => 'animals',
92+
'start' => '',
93+
'volume' => '',
94+
'seekinterval' => '',
95+
'nowplaying' => 'false'
96+
], $atts );
97+
98+
// output
99+
if (!$all_atts['type']) {
100+
// required fields are missing
101+
return false;
102+
}
103+
else {
104+
$type = $all_atts['type'];
105+
if (!($type == 'audio' || $type == 'video')) {
106+
// type is not a supported value
107+
return false;
108+
}
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;
173+
}
174+
}
66175
}
67-
add_action('wp_head','ableplayer_head',3);
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'].'"';
215+
}
216+
if (!empty($all_atts['type'])) {
217+
$o .= ' type="'.$all_atts['type'].'"';
218+
}
219+
if (!empty($all_atts['desc-src'])) {
220+
$o .= ' data-desc-src="'.$all_atts['desc-src'].'"';
221+
}
222+
if (!empty($all_atts['sign-src'])) {
223+
$o .= ' data-sign-src="'.$all_atts['sign-src'].'"';
224+
}
225+
if (!empty($all_atts['youtube-id'])) {
226+
$o .= ' data-youtube-id="'.$all_atts['youtube-id'].'"';
227+
}
228+
if (!empty($all_atts['youtube-desc-id'])) {
229+
$o .= ' data-youtube-desc-id="'.$all_atts['youtube-desc-id'].'"';
230+
}
231+
if (!empty($all_atts['youtube-nocookie'])) {
232+
$o .= ' data-youtube-nocookie="'.$all_atts['youtube-nocookie'].'"';
233+
}
234+
if (!empty($all_atts['vimeo-id'])) {
235+
$o .= ' data-vimeo-id="'.$all_atts['vimeo-id'].'"';
236+
}
237+
if (!empty($all_atts['vimeo-desc-id'])) {
238+
$o .= ' data-vimeo-desc-id="'.$all_atts['vimeo-desc-id'].'"';
239+
}
240+
$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+
*/
252+
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 );
264+
265+
// normalize attribute keys, lowercase
266+
$atts = array_change_key_case((array)$atts, CASE_LOWER);
267+
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 .= '>';
285+
return $o;
286+
}
287+
}
288+
add_shortcode('able-track', 'able_track_shortcode');
289+
290+
/*
291+
*
292+
* Supporting functions
293+
*
294+
*/
295+
296+
function get_unique_id() {
297+
298+
// use add_option(), get_option(), update_option(), & delete_option()
299+
// to track the number of Able Player instances on the current page
300+
// return a unique id for each new player instance,
301+
// using the form 'able_player_1', 'able_player_2', etc.
302+
$this_player = 1;
303+
$num_players = get_option('able-player-count');
304+
if (empty($num_players)) {
305+
add_option('able-player-count',$player_count);
306+
return 'able-player-'.$this_player;
307+
}
308+
else {
309+
// there's already at least one player
310+
$this_player = $num_players + 1;
311+
update_option('able-player-count',$this_player);
312+
return 'able-player-'.$this_player;
313+
}
314+
}
315+
316+
function is_true($var) {
317+
318+
// $var is a Boolean parameter
319+
// check for all variations that might be considered 'true'
320+
// return true if... well, true
321+
if ($var == '1' || $var == 'yes' || $var == 'true') {
322+
return true;
323+
}
324+
else {
325+
return false;
326+
}
327+
}
328+
68329
?>

0 commit comments

Comments
 (0)