PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

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