| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class wrapper for audio shortcode |
| 5 |
*/ |
| 6 |
class AudioShortcode { |
| 7 |
|
| 8 |
static $add_script = false; |
| 9 |
|
| 10 |
/** |
| 11 |
* Add all the actions & resgister the shortcode |
| 12 |
*/ |
| 13 |
function __construct() { |
| 14 |
add_shortcode( 'audio', array( $this, 'audio_shortcode' ) ); |
| 15 |
add_action( 'wp_enqueue_scripts', array( $this, 'check_infinite' ) ); |
| 16 |
add_action( 'infinite_scroll_render', array( $this, 'audio_shortcode_infinite' ), 11 ); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Return the $url of the audio |
| 21 |
*/ |
| 22 |
static function get_audio_id( $atts ) { |
| 23 |
if ( isset( $atts[0] ) ) |
| 24 |
return $atts[0]; |
| 25 |
else |
| 26 |
return 0; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Shortcode for audio |
| 31 |
* [audio http://wpcom.files.wordpress.com/2007/01/mattmullenweg-interview.mp3|width=180|titles=1|artists=2] |
| 32 |
* |
| 33 |
* The important question here is whether the shortcode applies to widget_text: |
| 34 |
* add_filter('widget_text', 'do_shortcode'); |
| 35 |
* */ |
| 36 |
function audio_shortcode( $atts ) { |
| 37 |
global $ap_playerID; |
| 38 |
global $post; |
| 39 |
if ( ! is_array( $atts ) ) { |
| 40 |
return '<!-- Audio shortcode passed invalid attributes -->'; |
| 41 |
} |
| 42 |
|
| 43 |
if ( ! isset( $atts[0] ) ) { |
| 44 |
if ( isset( $atts['src'] ) ) { |
| 45 |
$atts[0] = $atts['src']; |
| 46 |
unset( $atts['src'] ); |
| 47 |
} else { |
| 48 |
return '<!-- Audio shortcode source not set -->'; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
// add the special .js |
| 53 |
wp_enqueue_script( |
| 54 |
'audio-shortcode', |
| 55 |
plugins_url( 'js/audio-shortcode.js', __FILE__ ), |
| 56 |
array( 'jquery' ), |
| 57 |
'1.1', |
| 58 |
true); |
| 59 |
|
| 60 |
// alert the infinite scroll renderer that it should try to load the script |
| 61 |
self::$add_script = true; |
| 62 |
$atts[0] = strip_tags( join( ' ', $atts ) ); |
| 63 |
$src = ltrim( $atts[0], '=' ); |
| 64 |
$ap_options = apply_filters( |
| 65 |
'audio_player_default_colors', |
| 66 |
array( |
| 67 |
"bg" => "0xF8F8F8", |
| 68 |
"leftbg" => "0xEEEEEE", |
| 69 |
"lefticon" => "0x666666", |
| 70 |
"rightbg" => "0xCCCCCC", |
| 71 |
"rightbghover" => "0x999999", |
| 72 |
"righticon" => "0x666666", |
| 73 |
"righticonhover" => "0xFFFFFF", |
| 74 |
"text" => "0x666666", |
| 75 |
"slider" => "0x666666", |
| 76 |
"track" => "0xFFFFFF", |
| 77 |
"border" => "0x666666", |
| 78 |
"loader" => "0x9FFFB8" |
| 79 |
) ); |
| 80 |
|
| 81 |
if ( ! isset( $ap_playerID ) ) { |
| 82 |
$ap_playerID = 1; |
| 83 |
} else { |
| 84 |
$ap_playerID++; |
| 85 |
} |
| 86 |
|
| 87 |
if ( ! isset( $load_audio_script ) ) { |
| 88 |
$load_audio_script = true; |
| 89 |
} |
| 90 |
|
| 91 |
// prep the audio files |
| 92 |
$src = trim( $src, ' "' ); |
| 93 |
$options = array(); |
| 94 |
$data = preg_split( "/\|/", $src ); |
| 95 |
$sound_file = $data[0]; |
| 96 |
$sound_files = explode( ',', $sound_file ); |
| 97 |
|
| 98 |
if ( is_ssl() ) { |
| 99 |
for ( $i = 0; $i < count( $sound_files ); $i++ ) { |
| 100 |
$sound_files[ $i ] = preg_replace( '#^http://([^.]+).files.wordpress.com/#', 'https://$1.files.wordpress.com/', $sound_files[ $i ] ); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
$sound_files = array_map( 'trim', $sound_files ); |
| 105 |
$sound_files = array_map( array( $this, 'rawurlencode_spaces' ), $sound_files ); |
| 106 |
$sound_files = array_map( 'esc_url_raw', $sound_files ); // Ensure each is a valid URL |
| 107 |
$num_files = count( $sound_files ); |
| 108 |
$sound_types = array( |
| 109 |
'mp3' => 'mpeg', |
| 110 |
'wav' => 'wav', |
| 111 |
'ogg' => 'ogg', |
| 112 |
'oga' => 'ogg', |
| 113 |
'm4a' => 'mp4', |
| 114 |
'aac' => 'mp4', |
| 115 |
'webm' => 'webm' |
| 116 |
); |
| 117 |
|
| 118 |
for ( $i = 1; $i < count( $data ); $i++ ) { |
| 119 |
$pair = explode( "=", $data[$i] ); |
| 120 |
if ( strtolower( $pair[0] ) != 'autostart' ) { |
| 121 |
$options[$pair[0]] = $pair[1]; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
// Merge runtime options to default colour options |
| 126 |
// (runtime options overwrite default options) |
| 127 |
foreach ( $ap_options as $key => $default ) { |
| 128 |
if ( isset( $options[$key] ) ) { |
| 129 |
if ( preg_match( '/^(0x)?[a-f0-9]{6}$/i', $default ) && !preg_match( '/^(0x)?[a-f0-9]{6}$/i', $options[$key] ) ) { |
| 130 |
// Default is a hex color, but input is not |
| 131 |
$options[$key] = $default; |
| 132 |
} |
| 133 |
} else { |
| 134 |
$options[$key] = $default; |
| 135 |
} |
| 136 |
} |
| 137 |
$options['soundFile'] = join( ',', $sound_files ); // Rebuild the option with our now sanitized data |
| 138 |
$flash_vars = array(); |
| 139 |
foreach ( $options as $key => $value ) { |
| 140 |
$flash_vars[] = rawurlencode( $key ) . '=' . rawurlencode( $value ); |
| 141 |
} |
| 142 |
$flash_vars = implode( '&', $flash_vars ); |
| 143 |
$flash_vars = esc_attr( $flash_vars ); |
| 144 |
|
| 145 |
// extract some of the options to insert into the markup |
| 146 |
if ( isset( $options['bgcolor'] ) && preg_match( '/^(0x)?[a-f0-9]{6}$/i', $options['bgcolor'] ) ) { |
| 147 |
$bgcolor = preg_replace( '/^(0x)?/', '#', $options['bgcolor'] ); |
| 148 |
$bgcolor = esc_attr( $bgcolor ); |
| 149 |
} else { |
| 150 |
$bgcolor = '#FFFFFF'; |
| 151 |
} |
| 152 |
|
| 153 |
if ( isset( $options['width'] ) ) { |
| 154 |
$width = intval( $options['width'] ); |
| 155 |
} else { |
| 156 |
$width = 290; |
| 157 |
} |
| 158 |
|
| 159 |
$loop = ''; |
| 160 |
$script_loop = 'false'; |
| 161 |
if ( isset( $options['loop'] ) && 'yes' == $options['loop'] ) { |
| 162 |
$script_loop = 'true'; |
| 163 |
if ( 1 == $num_files ) { |
| 164 |
$loop = 'loop'; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
$volume = 0.6; |
| 169 |
if ( isset( $options['initialvolume'] ) && |
| 170 |
0.0 < floatval( $options['initialvolume'] ) && |
| 171 |
100.0 >= floatval( $options['initialvolume'] ) ) { |
| 172 |
|
| 173 |
$volume = floatval( $options['initialvolume'] )/100.0; |
| 174 |
} |
| 175 |
|
| 176 |
$file_artists = array_pad( array(), $num_files, '' ); |
| 177 |
if ( isset( $options['artists'] ) ) { |
| 178 |
$artists = preg_split( '/,/', $options['artists'] ); |
| 179 |
foreach ( $artists as $i => $artist ) { |
| 180 |
$file_artists[$i] = esc_html( $artist ) . ' - '; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
// generate default titles |
| 185 |
$file_titles = array(); |
| 186 |
for ( $i = 0; $i < $num_files; $i++ ) { |
| 187 |
$file_titles[] = 'Track #' . ($i+1); |
| 188 |
} |
| 189 |
|
| 190 |
// replace with real titles if they exist |
| 191 |
if ( isset( $options['titles'] ) ) { |
| 192 |
$titles = preg_split( '/,/', $options['titles'] ); |
| 193 |
foreach ( $titles as $i => $title ) { |
| 194 |
$file_titles[$i] = esc_html( $title ); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
// fallback for the fallback, just a download link |
| 199 |
$not_supported = ''; |
| 200 |
foreach ( $sound_files as $sfile ) { |
| 201 |
$not_supported .= sprintf( |
| 202 |
__( 'Download: <a href="%s">%s</a><br />', 'jetpack' ), |
| 203 |
esc_url( $sfile ), |
| 204 |
esc_html( basename( $sfile ) ) ); |
| 205 |
} |
| 206 |
|
| 207 |
// HTML5 audio tag |
| 208 |
$html5_audio = ''; |
| 209 |
$all_mp3 = true; |
| 210 |
$add_audio = true; |
| 211 |
$num_good = 0; |
| 212 |
$to_remove = array(); |
| 213 |
foreach ( $sound_files as $i => $sfile ) { |
| 214 |
$file_extension = pathinfo( $sfile, PATHINFO_EXTENSION ); |
| 215 |
if ( ! preg_match( '/^(mp3|wav|ogg|oga|m4a|aac|webm)$/i', $file_extension ) ) { |
| 216 |
$html5_audio .= '<!-- Audio shortcode unsupported audio format -->'; |
| 217 |
if ( 1 == $num_files ) { |
| 218 |
$html5_audio .= $not_supported; |
| 219 |
} |
| 220 |
|
| 221 |
$to_remove[] = $i; // make a note of the bad files |
| 222 |
$all_mp3 = false; |
| 223 |
continue; |
| 224 |
} elseif ( ! preg_match( '/^mp3$/i', $file_extension ) ) { |
| 225 |
$all_mp3 = false; |
| 226 |
} |
| 227 |
|
| 228 |
if ( 0 == $i ) { // only need one player |
| 229 |
$html5_audio .= <<<AUDIO |
| 230 |
<span id="wp-as-{$post->ID}_{$ap_playerID}-container"> |
| 231 |
<audio id='wp-as-{$post->ID}_{$ap_playerID}' controls preload='none' $loop style='background-color:$bgcolor;width:{$width}px;'> |
| 232 |
<span id="wp-as-{$post->ID}_{$ap_playerID}-nope">$not_supported</span> |
| 233 |
</audio> |
| 234 |
</span> |
| 235 |
<br /> |
| 236 |
AUDIO; |
| 237 |
} |
| 238 |
$num_good++; |
| 239 |
} |
| 240 |
|
| 241 |
// player controls, if needed |
| 242 |
if ( 1 < $num_files ) { |
| 243 |
$html5_audio .= <<<CONTROLS |
| 244 |
<span id='wp-as-{$post->ID}_{$ap_playerID}-controls' style='display:none;'> |
| 245 |
<a id='wp-as-{$post->ID}_{$ap_playerID}-prev' |
| 246 |
href='javascript:audioshortcode.prev_track( "{$post->ID}_{$ap_playerID}" );' |
| 247 |
style='font-size:1.5em;'>«</a> |
| 248 |
| |
| 249 |
<a id='wp-as-{$post->ID}_{$ap_playerID}-next' |
| 250 |
href='javascript:audioshortcode.next_track( "{$post->ID}_{$ap_playerID}", true, $script_loop );' |
| 251 |
style='font-size:1.5em;'>»</a> |
| 252 |
</span> |
| 253 |
CONTROLS; |
| 254 |
} |
| 255 |
$html5_audio .= "<span id='wp-as-{$post->ID}_{$ap_playerID}-playing'></span>"; |
| 256 |
|
| 257 |
$swfurl = apply_filters( |
| 258 |
'jetpack_static_url', |
| 259 |
set_url_scheme( "http://en.wordpress.com/wp-content/plugins/audio-player/player.swf" ) |
| 260 |
); |
| 261 |
|
| 262 |
// all the fancy javascript is causing Google Reader to break, just include flash in GReader |
| 263 |
// override html5 audio code w/ just not supported code |
| 264 |
if ( is_feed() ) { |
| 265 |
$html5_audio = $not_supported; |
| 266 |
} |
| 267 |
|
| 268 |
if ( $all_mp3 ) { |
| 269 |
// process regular flash player, inserting HTML5 tags into object as fallback |
| 270 |
$audio_tags = <<<FLASH |
| 271 |
<object id='wp-as-{$post->ID}_{$ap_playerID}-flash' type='application/x-shockwave-flash' data='$swfurl' width='$width' height='24'> |
| 272 |
<param name='movie' value='$swfurl' /> |
| 273 |
<param name='FlashVars' value='{$flash_vars}' /> |
| 274 |
<param name='quality' value='high' /> |
| 275 |
<param name='menu' value='false' /> |
| 276 |
<param name='bgcolor' value='$bgcolor' /> |
| 277 |
<param name='wmode' value='opaque' /> |
| 278 |
$html5_audio |
| 279 |
</object> |
| 280 |
FLASH; |
| 281 |
} else { // just HTML5 for non-mp3 versions |
| 282 |
$audio_tags = $html5_audio; |
| 283 |
} |
| 284 |
|
| 285 |
// strip out all the bad files before it reaches .js |
| 286 |
foreach ( $to_remove as $i ) { |
| 287 |
array_splice( $sound_files, $i, 1 ); |
| 288 |
array_splice( $file_artists, $i, 1 ); |
| 289 |
array_splice( $file_titles, $i, 1 ); |
| 290 |
} |
| 291 |
|
| 292 |
// mashup the artist/titles for the script |
| 293 |
$script_titles = array(); |
| 294 |
for ( $i = 0; $i < $num_files; $i++ ) { |
| 295 |
$script_titles[] = $file_artists[$i] . $file_titles[$i]; |
| 296 |
|
| 297 |
} |
| 298 |
|
| 299 |
// javacript to control audio |
| 300 |
$script_files = json_encode( $sound_files ); |
| 301 |
$script_titles = json_encode( $script_titles ); |
| 302 |
$script = <<<SCRIPT |
| 303 |
<script type='text/javascript'> |
| 304 |
//<![CDATA[ |
| 305 |
(function() { |
| 306 |
var prep = function() { |
| 307 |
if ( 'undefined' === typeof window.audioshortcode ) { return; } |
| 308 |
audioshortcode.prep( |
| 309 |
'{$post->ID}_{$ap_playerID}', |
| 310 |
$script_files, |
| 311 |
$script_titles, |
| 312 |
$volume, |
| 313 |
$script_loop |
| 314 |
); |
| 315 |
}; |
| 316 |
if ( 'undefined' === typeof jQuery ) { |
| 317 |
if ( document.addEventListener ) { |
| 318 |
window.addEventListener( 'load', prep, false ); |
| 319 |
} else if ( document.attachEvent ) { |
| 320 |
window.attachEvent( 'onload', prep ); |
| 321 |
} |
| 322 |
} else { |
| 323 |
jQuery(document).on( 'ready as-script-load', prep ); |
| 324 |
} |
| 325 |
})(); |
| 326 |
//]]> |
| 327 |
</script> |
| 328 |
SCRIPT; |
| 329 |
|
| 330 |
// add the special javascript, if needed |
| 331 |
if ( 0 < $num_good && ! is_feed() ) { |
| 332 |
$audio_tags .= $script; |
| 333 |
} |
| 334 |
|
| 335 |
return "<span style='text-align:left;display:block;'><p>$audio_tags</p></span>"; |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* If the theme uses infinite scroll, include jquery at the start |
| 340 |
*/ |
| 341 |
function check_infinite() { |
| 342 |
if ( current_theme_supports( 'infinite-scroll' ) && class_exists( 'The_Neverending_Home_Page' ) && The_Neverending_Home_Page::archive_supports_infinity() ) |
| 343 |
wp_enqueue_script( 'jquery' ); |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
/** |
| 348 |
* Dynamically load the .js, if needed |
| 349 |
* |
| 350 |
* This hooks in late (priority 11) to infinite_scroll_render to determine |
| 351 |
* a posteriori if a shortcode has been called. |
| 352 |
*/ |
| 353 |
function audio_shortcode_infinite() { |
| 354 |
// only try to load if a shortcode has been called |
| 355 |
if( self::$add_script ) { |
| 356 |
$script_url = json_encode( esc_url_raw( plugins_url( 'js/audio-shortcode.js', __FILE__ ) ) ); |
| 357 |
|
| 358 |
// if the script hasn't been loaded, load it |
| 359 |
// if the script loads successfully, fire an 'as-script-load' event |
| 360 |
echo <<<SCRIPT |
| 361 |
<script type='text/javascript'> |
| 362 |
//<![CDATA[ |
| 363 |
if ( typeof window.audioshortcode === 'undefined' ) { |
| 364 |
var wp_as_js = document.createElement( 'script' ); |
| 365 |
wp_as_js.type = 'text/javascript'; |
| 366 |
wp_as_js.src = $script_url; |
| 367 |
wp_as_js.async = true; |
| 368 |
wp_as_js.onload = function() { |
| 369 |
jQuery( document.body ).trigger( 'as-script-load' ); |
| 370 |
}; |
| 371 |
document.getElementsByTagName( 'head' )[0].appendChild( wp_as_js ); |
| 372 |
} else { |
| 373 |
jQuery( document.body ).trigger( 'as-script-load' ); |
| 374 |
} |
| 375 |
//]]> |
| 376 |
</script> |
| 377 |
SCRIPT; |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* Fixes URLs that have been pasted with spaces: |
| 383 |
* [audio http://example.com/Some Cool Music.mp3] |
| 384 |
* |
| 385 |
* @param string $url |
| 386 |
* @return string |
| 387 |
*/ |
| 388 |
function rawurlencode_spaces( $url ) { |
| 389 |
return str_replace( ' ', rawurlencode( ' ' ), $url ); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
// kick it all off |
| 394 |
new AudioShortcode(); |
| 395 |
|