PageRenderTime 28ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/shortcodes/audio.php

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