AudioShortcode

Class wrapper for audio shortcode.

Defined (1)

The class is defined in the following location(s).

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