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