PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/jetpack/modules/shortcodes/audio.php

https://gitlab.com/mattswann/launch-housing
PHP | 435 lines | 298 code | 45 blank | 92 comment | 58 complexity | 8f79247fda22c161a185c7e01ed4bde4 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. $post_id = 0;
  46. if ( isset( $post ) ) {
  47. $post_id = $post->ID;
  48. }
  49. // add the special .js
  50. wp_enqueue_script(
  51. 'audio-shortcode',
  52. plugins_url( 'js/audio-shortcode.js', __FILE__ ),
  53. array( 'jquery' ),
  54. '1.1',
  55. true);
  56. // alert the infinite scroll renderer that it should try to load the script
  57. self::$add_script = true;
  58. $atts[0] = strip_tags( join( ' ', $atts ) );
  59. $src = ltrim( $atts[0], '=' );
  60. /**
  61. * Set the audio player default colors.
  62. *
  63. * @module shortcodes
  64. *
  65. * @since 1.4.0
  66. *
  67. * @param array $ap_options {
  68. * The default colors for the audio player in hexidecimal format (e.g. 0x#F8F8F8).
  69. *
  70. * @type string $bg Background color.
  71. * @type string $leftbg Left background color.
  72. * @type string $lefticon Left icon color.
  73. * @type string $rightbg Right background color.
  74. * @type string $rightbghover Right background hover color.
  75. * @type string $righticon Right icon color.
  76. * @type string $righticonhover Right icon hover color.
  77. * @type string $text Text color.
  78. * @type string $slider Slider color.
  79. * @type string $track Track color.
  80. * @type string $border Border color.
  81. * @type string $loader Loader color.
  82. */
  83. $ap_options = apply_filters(
  84. 'audio_player_default_colors',
  85. array(
  86. "bg" => "0xF8F8F8",
  87. "leftbg" => "0xEEEEEE",
  88. "lefticon" => "0x666666",
  89. "rightbg" => "0xCCCCCC",
  90. "rightbghover" => "0x999999",
  91. "righticon" => "0x666666",
  92. "righticonhover" => "0xFFFFFF",
  93. "text" => "0x666666",
  94. "slider" => "0x666666",
  95. "track" => "0xFFFFFF",
  96. "border" => "0x666666",
  97. "loader" => "0x9FFFB8"
  98. ) );
  99. if ( ! isset( $ap_playerID ) ) {
  100. $ap_playerID = 1;
  101. } else {
  102. $ap_playerID++;
  103. }
  104. if ( ! isset( $load_audio_script ) ) {
  105. $load_audio_script = true;
  106. }
  107. // prep the audio files
  108. $src = trim( $src, ' "' );
  109. $options = array();
  110. $data = preg_split( "/\|/", $src );
  111. $sound_file = $data[0];
  112. $sound_files = explode( ',', $sound_file );
  113. if ( is_ssl() ) {
  114. for ( $i = 0; $i < count( $sound_files ); $i++ ) {
  115. $sound_files[ $i ] = preg_replace( '#^http://([^.]+).files.wordpress.com/#', 'https://$1.files.wordpress.com/', $sound_files[ $i ] );
  116. }
  117. }
  118. $sound_files = array_map( 'trim', $sound_files );
  119. $sound_files = array_map( array( $this, 'rawurlencode_spaces' ), $sound_files );
  120. $sound_files = array_map( 'esc_url_raw', $sound_files ); // Ensure each is a valid URL
  121. $num_files = count( $sound_files );
  122. $sound_types = array(
  123. 'mp3' => 'mpeg',
  124. 'wav' => 'wav',
  125. 'ogg' => 'ogg',
  126. 'oga' => 'ogg',
  127. 'm4a' => 'mp4',
  128. 'aac' => 'mp4',
  129. 'webm' => 'webm'
  130. );
  131. for ( $i = 1; $i < count( $data ); $i++ ) {
  132. $pair = explode( "=", $data[$i] );
  133. if ( strtolower( $pair[0] ) != 'autostart' ) {
  134. $options[$pair[0]] = $pair[1];
  135. }
  136. }
  137. // Merge runtime options to default colour options
  138. // (runtime options overwrite default options)
  139. foreach ( $ap_options as $key => $default ) {
  140. if ( isset( $options[$key] ) ) {
  141. if ( preg_match( '/^(0x)?[a-f0-9]{6}$/i', $default ) && !preg_match( '/^(0x)?[a-f0-9]{6}$/i', $options[$key] ) ) {
  142. // Default is a hex color, but input is not
  143. $options[$key] = $default;
  144. }
  145. } else {
  146. $options[$key] = $default;
  147. }
  148. }
  149. $options['soundFile'] = join( ',', $sound_files ); // Rebuild the option with our now sanitized data
  150. $flash_vars = array();
  151. foreach ( $options as $key => $value ) {
  152. $flash_vars[] = rawurlencode( $key ) . '=' . rawurlencode( $value );
  153. }
  154. $flash_vars = implode( '&amp;', $flash_vars );
  155. $flash_vars = esc_attr( $flash_vars );
  156. // extract some of the options to insert into the markup
  157. if ( isset( $options['bgcolor'] ) && preg_match( '/^(0x)?[a-f0-9]{6}$/i', $options['bgcolor'] ) ) {
  158. $bgcolor = preg_replace( '/^(0x)?/', '#', $options['bgcolor'] );
  159. $bgcolor = esc_attr( $bgcolor );
  160. } else {
  161. $bgcolor = '#FFFFFF';
  162. }
  163. if ( isset( $options['width'] ) ) {
  164. $width = intval( $options['width'] );
  165. } else {
  166. $width = 290;
  167. }
  168. $loop = '';
  169. $script_loop = 'false';
  170. if ( isset( $options['loop'] ) && 'yes' == $options['loop'] ) {
  171. $script_loop = 'true';
  172. if ( 1 == $num_files ) {
  173. $loop = 'loop';
  174. }
  175. }
  176. $volume = 0.6;
  177. if ( isset( $options['initialvolume'] ) &&
  178. 0.0 < floatval( $options['initialvolume'] ) &&
  179. 100.0 >= floatval( $options['initialvolume'] ) ) {
  180. $volume = floatval( $options['initialvolume'] )/100.0;
  181. }
  182. $file_artists = array_pad( array(), $num_files, '' );
  183. if ( isset( $options['artists'] ) ) {
  184. $artists = preg_split( '/,/', $options['artists'] );
  185. foreach ( $artists as $i => $artist ) {
  186. $file_artists[$i] = esc_html( $artist ) . ' - ';
  187. }
  188. }
  189. // generate default titles
  190. $file_titles = array();
  191. for ( $i = 0; $i < $num_files; $i++ ) {
  192. $file_titles[] = 'Track #' . ($i+1);
  193. }
  194. // replace with real titles if they exist
  195. if ( isset( $options['titles'] ) ) {
  196. $titles = preg_split( '/,/', $options['titles'] );
  197. foreach ( $titles as $i => $title ) {
  198. $file_titles[$i] = esc_html( $title );
  199. }
  200. }
  201. // fallback for the fallback, just a download link
  202. $not_supported = '';
  203. foreach ( $sound_files as $sfile ) {
  204. $not_supported .= sprintf(
  205. __( 'Download: <a href="%s">%s</a><br />', 'jetpack' ),
  206. esc_url( $sfile ),
  207. esc_html( basename( $sfile ) ) );
  208. }
  209. // HTML5 audio tag
  210. $html5_audio = '';
  211. $all_mp3 = true;
  212. $add_audio = true;
  213. $num_good = 0;
  214. $to_remove = array();
  215. foreach ( $sound_files as $i => $sfile ) {
  216. $file_extension = pathinfo( $sfile, PATHINFO_EXTENSION );
  217. if ( ! preg_match( '/^(mp3|wav|ogg|oga|m4a|aac|webm)$/i', $file_extension ) ) {
  218. $html5_audio .= '<!-- Audio shortcode unsupported audio format -->';
  219. if ( 1 == $num_files ) {
  220. $html5_audio .= $not_supported;
  221. }
  222. $to_remove[] = $i; // make a note of the bad files
  223. $all_mp3 = false;
  224. continue;
  225. } elseif ( ! preg_match( '/^mp3$/i', $file_extension ) ) {
  226. $all_mp3 = false;
  227. }
  228. if ( 0 == $i ) { // only need one player
  229. $html5_audio .= <<<AUDIO
  230. <span id="wp-as-{$post_id}_{$ap_playerID}-container">
  231. <audio id='wp-as-{$post_id}_{$ap_playerID}' controls preload='none' $loop style='background-color:$bgcolor;width:{$width}px;'>
  232. <span id="wp-as-{$post_id}_{$ap_playerID}-nope">$not_supported</span>
  233. </audio>
  234. </span>
  235. <br />
  236. AUDIO;
  237. }
  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;'>&laquo;</a>
  247. |
  248. <a id='wp-as-{$post_id}_{$ap_playerID}-next'
  249. href='javascript:audioshortcode.next_track( "{$post_id}_{$ap_playerID}", true, $script_loop );'
  250. style='font-size:1.5em;'>&raquo;</a>
  251. </span>
  252. CONTROLS;
  253. }
  254. $html5_audio .= "<span id='wp-as-{$post_id}_{$ap_playerID}-playing'></span>";
  255. /**
  256. * Sets external resource URL.
  257. *
  258. * @module shortcodes
  259. *
  260. * @since 1.4.0
  261. *
  262. * @param string $args URL of external resource.
  263. *
  264. */
  265. $swfurl = apply_filters(
  266. 'jetpack_static_url',
  267. set_url_scheme( "http://en.wordpress.com/wp-content/plugins/audio-player/player.swf" )
  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. }
  303. // javacript to control audio
  304. $script_files = json_encode( $sound_files );
  305. $script_titles = json_encode( $script_titles );
  306. $script = <<<SCRIPT
  307. <script type='text/javascript'>
  308. //<![CDATA[
  309. (function() {
  310. var prep = function() {
  311. if ( 'undefined' === typeof window.audioshortcode ) { return; }
  312. audioshortcode.prep(
  313. '{$post_id}_{$ap_playerID}',
  314. $script_files,
  315. $script_titles,
  316. $volume,
  317. $script_loop
  318. );
  319. };
  320. if ( 'undefined' === typeof jQuery ) {
  321. if ( document.addEventListener ) {
  322. window.addEventListener( 'load', prep, false );
  323. } else if ( document.attachEvent ) {
  324. window.attachEvent( 'onload', prep );
  325. }
  326. } else {
  327. jQuery(document).on( 'ready as-script-load', prep );
  328. }
  329. })();
  330. //]]>
  331. </script>
  332. SCRIPT;
  333. // add the special javascript, if needed
  334. if ( 0 < $num_good && ! is_feed() ) {
  335. $audio_tags .= $script;
  336. }
  337. return "<span style='text-align:left;display:block;'><p>$audio_tags</p></span>";
  338. }
  339. /**
  340. * If the theme uses infinite scroll, include jquery at the start
  341. */
  342. function check_infinite() {
  343. if ( current_theme_supports( 'infinite-scroll' ) && class_exists( 'The_Neverending_Home_Page' ) && The_Neverending_Home_Page::archive_supports_infinity() )
  344. wp_enqueue_script( 'jquery' );
  345. }
  346. /**
  347. * Dynamically load the .js, if needed
  348. *
  349. * This hooks in late (priority 11) to infinite_scroll_render to determine
  350. * a posteriori if a shortcode has been called.
  351. */
  352. function audio_shortcode_infinite() {
  353. // only try to load if a shortcode has been called
  354. if( self::$add_script ) {
  355. $script_url = json_encode( esc_url_raw( plugins_url( 'js/audio-shortcode.js', __FILE__ ) ) );
  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. //]]>
  374. </script>
  375. SCRIPT;
  376. }
  377. }
  378. /**
  379. * Fixes URLs that have been pasted with spaces:
  380. * [audio http://example.com/Some Cool Music.mp3]
  381. *
  382. * @param string $url
  383. * @return string
  384. */
  385. function rawurlencode_spaces( $url ) {
  386. return str_replace( ' ', rawurlencode( ' ' ), $url );
  387. }
  388. }
  389. // kick it all off
  390. new AudioShortcode();