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