audio.php
| 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 | if ( is_ssl() ) |
| 258 | $protocol = 'https'; |
| 259 | else |
| 260 | $protocol = 'http'; |
| 261 | |
| 262 | $swfurl = apply_filters( |
| 263 | 'jetpack_static_url', |
| 264 | "$protocol://en.wordpress.com/wp-content/plugins/audio-player/player.swf" ); |
| 265 | |
| 266 | // all the fancy javascript is causing Google Reader to break, just include flash in GReader |
| 267 | // override html5 audio code w/ just not supported code |
| 268 | if ( is_feed() ) { |
| 269 | $html5_audio = $not_supported; |
| 270 | } |
| 271 | |
| 272 | if ( $all_mp3 ) { |
| 273 | // process regular flash player, inserting HTML5 tags into object as fallback |
| 274 | $audio_tags = <<<FLASH |
| 275 | <object id='wp-as-{$post->ID}_{$ap_playerID}-flash' type='application/x-shockwave-flash' data='$swfurl' width='$width' height='24'> |
| 276 | <param name='movie' value='$swfurl' /> |
| 277 | <param name='FlashVars' value='{$flash_vars}' /> |
| 278 | <param name='quality' value='high' /> |
| 279 | <param name='menu' value='false' /> |
| 280 | <param name='bgcolor' value='$bgcolor' /> |
| 281 | <param name='wmode' value='opaque' /> |
| 282 | $html5_audio |
| 283 | </object> |
| 284 | FLASH; |
| 285 | } else { // just HTML5 for non-mp3 versions |
| 286 | $audio_tags = $html5_audio; |
| 287 | } |
| 288 | |
| 289 | // strip out all the bad files before it reaches .js |
| 290 | foreach ( $to_remove as $i ) { |
| 291 | array_splice( $sound_files, $i, 1 ); |
| 292 | array_splice( $file_artists, $i, 1 ); |
| 293 | array_splice( $file_titles, $i, 1 ); |
| 294 | } |
| 295 | |
| 296 | // mashup the artist/titles for the script |
| 297 | $script_titles = array(); |
| 298 | for ( $i = 0; $i < $num_files; $i++ ) { |
| 299 | $script_titles[] = $file_artists[$i] . $file_titles[$i]; |
| 300 | |
| 301 | } |
| 302 | |
| 303 | // javacript to control audio |
| 304 | $script_files = json_encode( $sound_files ); |
| 305 | $script_titles = json_encode( $script_titles ); |
| 306 | $script = <<<SCRIPT |
| 307 | <script type='text/javascript'> |
| 308 | //<![CDATA[ |
| 309 | (function() { |
| 310 | var prep = function() { |
| 311 | if ( 'undefined' === typeof window.audioshortcode ) { return; } |
| 312 | audioshortcode.prep( |
| 313 | '{$post->ID}_{$ap_playerID}', |
| 314 | $script_files, |
| 315 | $script_titles, |
| 316 | $volume, |
| 317 | $script_loop |
| 318 | ); |
| 319 | }; |
| 320 | if ( 'undefined' === typeof jQuery ) { |
| 321 | if ( document.addEventListener ) { |
| 322 | window.addEventListener( 'load', prep, false ); |
| 323 | } else if ( document.attachEvent ) { |
| 324 | window.attachEvent( 'onload', prep ); |
| 325 | } |
| 326 | } else { |
| 327 | jQuery(document).on( 'ready as-script-load', prep ); |
| 328 | } |
| 329 | })(); |
| 330 | //]]> |
| 331 | </script> |
| 332 | SCRIPT; |
| 333 | |
| 334 | // add the special javascript, if needed |
| 335 | if ( 0 < $num_good && ! is_feed() ) { |
| 336 | $audio_tags .= $script; |
| 337 | } |
| 338 | |
| 339 | return "<span style='text-align:left;display:block;'><p>$audio_tags</p></span>"; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * If the theme uses infinite scroll, include jquery at the start |
| 344 | */ |
| 345 | function check_infinite() { |
| 346 | if ( current_theme_supports( 'infinite-scroll' ) && class_exists( 'The_Neverending_Home_Page' ) && The_Neverending_Home_Page::archive_supports_infinity() ) |
| 347 | wp_enqueue_script( 'jquery' ); |
| 348 | } |
| 349 | |
| 350 | |
| 351 | /** |
| 352 | * Dynamically load the .js, if needed |
| 353 | * |
| 354 | * This hooks in late (priority 11) to infinite_scroll_render to determine |
| 355 | * a posteriori if a shortcode has been called. |
| 356 | */ |
| 357 | function audio_shortcode_infinite() { |
| 358 | // only try to load if a shortcode has been called |
| 359 | if( self::$add_script ) { |
| 360 | $script_url = json_encode( esc_url_raw( plugins_url( 'js/audio-shortcode.js', __FILE__ ) ) ); |
| 361 | |
| 362 | // if the script hasn't been loaded, load it |
| 363 | // if the script loads successfully, fire an 'as-script-load' event |
| 364 | echo <<<SCRIPT |
| 365 | <script type='text/javascript'> |
| 366 | //<![CDATA[ |
| 367 | if ( typeof window.audioshortcode === 'undefined' ) { |
| 368 | var wp_as_js = document.createElement( 'script' ); |
| 369 | wp_as_js.type = 'text/javascript'; |
| 370 | wp_as_js.src = $script_url; |
| 371 | wp_as_js.async = true; |
| 372 | wp_as_js.onload = function() { |
| 373 | jQuery( document.body ).trigger( 'as-script-load' ); |
| 374 | }; |
| 375 | document.getElementsByTagName( 'head' )[0].appendChild( wp_as_js ); |
| 376 | } else { |
| 377 | jQuery( document.body ).trigger( 'as-script-load' ); |
| 378 | } |
| 379 | //]]> |
| 380 | </script> |
| 381 | SCRIPT; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Fixes URLs that have been pasted with spaces: |
| 387 | * [audio http://example.com/Some Cool Music.mp3] |
| 388 | * |
| 389 | * @param string $url |
| 390 | * @return string |
| 391 | */ |
| 392 | function rawurlencode_spaces( $url ) { |
| 393 | return str_replace( ' ', rawurlencode( ' ' ), $url ); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | // kick it all off |
| 398 | new AudioShortcode(); |
| 399 |