PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/Gashler/sg
PHP | 261 lines | 117 code | 45 blank | 99 comment | 26 complexity | fe1e252b37e0e279345b075afa337f7f MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: SoundCloud Shortcode
  4. Plugin URI: http://wordpress.org/extend/plugins/soundcloud-shortcode/
  5. Description: Converts SoundCloud WordPress shortcodes to a SoundCloud widget. Example: [soundcloud]http://soundcloud.com/forss/flickermood[/soundcloud]
  6. Version: 2.3
  7. Author: SoundCloud Inc., simplified for Jetpack by Automattic, Inc.
  8. Author URI: http://soundcloud.com
  9. License: GPLv2
  10. Original version: Johannes Wagener <johannes@soundcloud.com>
  11. Options support: Tiffany Conroy <tiffany@soundcloud.com>
  12. HTML5 & oEmbed support: Tim Bormans <tim@soundcloud.com>
  13. */
  14. /*
  15. A8C: Taken from http://plugins.svn.wordpress.org/soundcloud-shortcode/trunk/
  16. at revision 664386.
  17. Commenting out (instead of removing) and replacing code with custom modifs
  18. so it's eqsy to see what differs from the standard DOTORG version.
  19. All custom modifs are annoted with "A8C" keyword in comment.
  20. */
  21. /**
  22. * Register oEmbed provider
  23. */
  24. wp_oembed_add_provider('#https?://(?:api\.)?soundcloud\.com/.*#i', 'http://soundcloud.com/oembed', true);
  25. /**
  26. * Register SoundCloud shortcode
  27. */
  28. add_shortcode("soundcloud", "soundcloud_shortcode");
  29. /**
  30. * SoundCloud shortcode handler
  31. * @param {string|array} $atts The attributes passed to the shortcode like [soundcloud attr1="value" /].
  32. * Is an empty string when no arguments are given.
  33. * @param {string} $content The content between non-self closing [soundcloud]…[/soundcloud] tags.
  34. * @return {string} Widget embed code HTML
  35. */
  36. function soundcloud_shortcode( $atts, $content = null ) {
  37. // Custom shortcode options
  38. $shortcode_options = array_merge( array('url' => trim( $content ) ), is_array( $atts ) ? $atts : array() );
  39. // Turn shortcode option "param" (param=value&param2=value) into array
  40. $shortcode_params = array();
  41. if ( isset( $shortcode_options['params'] ) ) {
  42. parse_str( html_entity_decode( $shortcode_options['params'] ), $shortcode_params );
  43. }
  44. $shortcode_options['params'] = $shortcode_params;
  45. $player_type = soundcloud_get_option( 'player_type', 'visual' );
  46. $isIframe = $player_type !== 'flash';
  47. $isVisual = !$player_type || $player_type === 'visual';
  48. // User preference options
  49. $plugin_options = array_filter(array(
  50. 'iframe' => $isIframe,
  51. 'width' => soundcloud_get_option( 'player_width' ),
  52. 'height' => soundcloud_url_has_tracklist( $shortcode_options['url'] ) ? soundcloud_get_option( 'player_height_multi' ) : soundcloud_get_option( 'player_height' ),
  53. 'params' => array_filter( array(
  54. 'auto_play' => soundcloud_get_option( 'auto_play' ),
  55. 'show_comments' => soundcloud_get_option( 'show_comments' ),
  56. 'color' => soundcloud_get_option( 'color' ),
  57. 'visual' => ( $isVisual ? 'true' : 'false' )
  58. )),
  59. ));
  60. // Needs to be an array
  61. if ( ! isset( $plugin_options['params'] ) ) {
  62. $plugin_options['params'] = array();
  63. }
  64. // plugin options < shortcode options
  65. $options = array_merge(
  66. $plugin_options,
  67. $shortcode_options
  68. );
  69. // plugin params < shortcode params
  70. $options['params'] = array_merge(
  71. $plugin_options['params'],
  72. $shortcode_options['params']
  73. );
  74. // The "url" option is required
  75. if ( ! isset( $options['url'] ) ) {
  76. return '';
  77. } else {
  78. $options['url'] = trim( $options['url'] );
  79. }
  80. // Both "width" and "height" need to be integers
  81. if (isset( $options['width'] ) && ! preg_match( '/^\d+$/', $options['width'] ) ) {
  82. // set to 0 so oEmbed will use the default 100% and WordPress themes will leave it alone
  83. $options['width'] = 0;
  84. }
  85. if ( isset( $options['height'] ) && ! preg_match( '/^\d+$/', $options['height'] ) ) {
  86. unset( $options['height'] );
  87. }
  88. // The "iframe" option must be true to load the iframe widget
  89. $iframe = soundcloud_booleanize( $options['iframe'] );
  90. // Remove visual parameter from Flash widget, when it's false because that's the default, or when displaying the smallest player
  91. if ( $options['params']['visual'] && ( ! $iframe || ! soundcloud_booleanize( $options['params']['visual'] ) || ( isset( $options['height'] ) && '20' == $options['height'] ) ) ) {
  92. unset( $options['params']['visual'] );
  93. }
  94. // Merge in "url" value
  95. $options['params'] = array_merge( array(
  96. 'url' => $options['url']
  97. ), $options['params'] );
  98. // Return html embed code
  99. if ( $iframe ) {
  100. return soundcloud_iframe_widget( $options );
  101. } else {
  102. return soundcloud_flash_widget( $options );
  103. }
  104. }
  105. /**
  106. * Plugin options getter
  107. * @param {string|array} $option Option name
  108. * @param {mixed} $default Default value
  109. * @return {mixed} Option value
  110. */
  111. function soundcloud_get_option( $option, $default = false ) {
  112. $value = get_option( 'soundcloud_' . $option );
  113. return $value === '' ? $default : $value;
  114. }
  115. /**
  116. * Booleanize a value
  117. * @param {boolean|string} $value
  118. * @return {boolean}
  119. */
  120. function soundcloud_booleanize( $value ) {
  121. return is_bool( $value ) ? $value : $value === 'true' ? true : false;
  122. }
  123. /**
  124. * Decide if a url has a tracklist
  125. * @param {string} $url
  126. * @return {boolean}
  127. */
  128. function soundcloud_url_has_tracklist( $url ) {
  129. return preg_match( '/^(.+?)\/(sets|groups|playlists)\/(.+?)$/', $url );
  130. }
  131. /**
  132. * Parameterize url
  133. * @param {array} $match Matched regex
  134. * @return {string} Parameterized url
  135. */
  136. function soundcloud_oembed_params_callback( $match ) {
  137. global $soundcloud_oembed_params;
  138. // Convert URL to array
  139. $url = parse_url( urldecode( $match[1] ) );
  140. // Convert URL query to array
  141. parse_str( $url['query'], $query_array );
  142. // Build new query string
  143. $query = http_build_query( array_merge( $query_array, $soundcloud_oembed_params ) );
  144. return 'src="' . $url['scheme'] . '://' . $url['host'] . $url['path'] . '?' . $query;
  145. }
  146. /**
  147. * Iframe widget embed code
  148. * @param {array} $options Parameters
  149. * @return {string} Iframe embed code
  150. */
  151. function soundcloud_iframe_widget( $options ) {
  152. // Build URL
  153. $url = set_url_scheme( 'https://w.soundcloud.com/player/?' . http_build_query( $options['params'] ) );
  154. // Set default width if not defined
  155. $width = isset($options['width']) && $options['width'] !== 0 ? $options['width'] : '100%';
  156. // Set default height if not defined
  157. $height = isset($options['height']) && $options['height'] !== 0
  158. ? $options['height']
  159. : ( soundcloud_url_has_tracklist( $options['url']) || ( isset( $options['params']['visual'] ) && soundcloud_booleanize( $options['params']['visual'] ) ) ? '450' : '166');
  160. return sprintf( '<iframe width="%s" height="%s" scrolling="no" frameborder="no" src="%s"></iframe>', $width, $height, $url );
  161. }
  162. /**
  163. * Legacy Flash widget embed code
  164. * @param {array} $options Parameters
  165. * @return {string} Flash embed code
  166. */
  167. function soundcloud_flash_widget( $options ) {
  168. // Build URL
  169. $url = set_url_scheme( 'https://player.soundcloud.com/player.swf?' . http_build_query($options['params']) );
  170. // Set default width if not defined
  171. $width = isset( $options['width'] ) && $options['width'] !== 0 ? $options['width'] : '100%';
  172. // Set default height if not defined
  173. $height = isset( $options['height'] ) && $options['height'] !== 0 ? $options['height'] : ( soundcloud_url_has_tracklist( $options['url'] ) ? '255' : '81');
  174. return preg_replace( '/\s\s+/', "", sprintf( '<object width="%s" height="%s">
  175. <param name="movie" value="%s"></param>
  176. <param name="allowscriptaccess" value="always"></param>
  177. <embed width="%s" height="%s" src="%s" allowscriptaccess="always" type="application/x-shockwave-flash"></embed>
  178. </object>', $width, $height, $url, $width, $height, $url ) );
  179. }
  180. /**
  181. * SoundCloud Embed Reversal
  182. *
  183. * Converts a generic HTML embed code from SoundClound into a
  184. * WordPress.com-compatibly shortcode.
  185. */
  186. function jetpack_soundcloud_embed_reversal( $content ) {
  187. if ( false === stripos( $content, 'w.soundcloud.com/player' ) )
  188. return $content;
  189. /* Sample embed code:
  190. <iframe width="100%" height="450" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/150745932&amp;auto_play=false&amp;hide_related=false&amp;show_comments=true&amp;show_user=true&amp;show_reposts=false&amp;visual=true"></iframe>
  191. */
  192. $regexes = array();
  193. $regexes[] = '#<iframe[^>]+?src="((?:https?:)?//w\.soundcloud\.com/player/[^"\']++)"[^>]*+>\s*?</iframe>#i';
  194. $regexes[] = '#&lt;iframe(?:[^&]|&(?!gt;))+?src="((?:https?:)?//w\.soundcloud\.com/player/[^"\']++)"(?:[^&]|&(?!gt;))*+&gt;\s*?&lt;/iframe&gt;#i';
  195. foreach ( $regexes as $regex ) {
  196. if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) )
  197. continue;
  198. foreach ( $matches as $match ) {
  199. $args = parse_url( html_entity_decode( $match[1] ), PHP_URL_QUERY );
  200. $args = wp_parse_args( $args );
  201. if ( ! preg_match( '#^(?:https?:)?//api\.soundcloud\.com/.+$#i', $args['url'], $url_matches ) )
  202. continue;
  203. $shortcode = sprintf( '[soundcloud url="%s"]', esc_url( $url_matches[0] ) );
  204. $replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) );
  205. $content = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $shortcode ), $content );
  206. /** This action is documented in modules/shortcodes/youtube.php */
  207. do_action( 'jetpack_embed_to_shortcode', 'soundcloud', $url_matches[0] );
  208. }
  209. }
  210. return $content;
  211. }
  212. add_filter( 'pre_kses', 'jetpack_soundcloud_embed_reversal' );