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

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

https://gitlab.com/hunt9310/ras
PHP | 310 lines | 145 code | 47 blank | 118 comment | 31 complexity | d2e99abddfdb04e69dffce7b05dac2a2 MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: SoundCloud Shortcode
  4. Plugin URI: https://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. *
  32. * @param string|array $atts The attributes passed to the shortcode like [soundcloud attr1="value" /].
  33. * Is an empty string when no arguments are given.
  34. * @param string $content The content between non-self closing [soundcloud]...[/soundcloud] tags.
  35. *
  36. * @return string Widget embed code HTML
  37. */
  38. function soundcloud_shortcode( $atts, $content = null ) {
  39. // Custom shortcode options
  40. $shortcode_options = array_merge( array( 'url' => trim( $content ) ), is_array( $atts ) ? $atts : array() );
  41. // Turn shortcode option "param" (param=value&param2=value) into array
  42. $shortcode_params = array();
  43. if ( isset( $shortcode_options['params'] ) ) {
  44. parse_str( html_entity_decode( $shortcode_options['params'] ), $shortcode_params );
  45. }
  46. $shortcode_options['params'] = $shortcode_params;
  47. $player_type = soundcloud_get_option( 'player_type', 'visual' );
  48. $isIframe = $player_type !== 'flash';
  49. $isVisual = ! $player_type || $player_type === 'visual' || $shortcode_options['visual'];
  50. // User preference options
  51. $plugin_options = array_filter(
  52. array(
  53. 'iframe' => $isIframe,
  54. 'width' => soundcloud_get_option( 'player_width' ),
  55. 'height' => soundcloud_url_has_tracklist( $shortcode_options['url'] ) ? soundcloud_get_option( 'player_height_multi' ) : soundcloud_get_option( 'player_height' ),
  56. 'params' => array_filter(
  57. array(
  58. 'auto_play' => soundcloud_get_option( 'auto_play' ),
  59. 'show_comments' => soundcloud_get_option( 'show_comments' ),
  60. 'color' => soundcloud_get_option( 'color' ),
  61. 'visual' => ( $isVisual ? 'true' : 'false' ),
  62. )
  63. ),
  64. )
  65. );
  66. // Needs to be an array
  67. if ( ! isset( $plugin_options['params'] ) ) {
  68. $plugin_options['params'] = array();
  69. }
  70. // plugin options < shortcode options
  71. $options = array_merge(
  72. $plugin_options,
  73. $shortcode_options
  74. );
  75. // plugin params < shortcode params
  76. $options['params'] = array_merge(
  77. $plugin_options['params'],
  78. $shortcode_options['params']
  79. );
  80. // The "url" option is required
  81. if ( ! isset( $options['url'] ) ) {
  82. return '';
  83. } else {
  84. $options['url'] = trim( $options['url'] );
  85. }
  86. // Both "width" and "height" need to be integers
  87. if ( isset( $options['width'] ) && ! preg_match( '/^\d+$/', $options['width'] ) ) {
  88. // set to 0 so oEmbed will use the default 100% and WordPress themes will leave it alone
  89. $options['width'] = 0;
  90. }
  91. if ( isset( $options['height'] ) && ! preg_match( '/^\d+$/', $options['height'] ) ) {
  92. unset( $options['height'] );
  93. }
  94. // The "iframe" option must be true to load the iframe widget
  95. $iframe = soundcloud_booleanize( $options['iframe'] );
  96. // Remove visual parameter from Flash widget, when it's false because that's the default, or when displaying the smallest player
  97. if ( $options['params']['visual'] && ( ! $iframe || ! soundcloud_booleanize( $options['params']['visual'] ) || ( isset( $options['height'] ) && '20' == $options['height'] ) ) ) {
  98. unset( $options['params']['visual'] );
  99. }
  100. // Merge in "url" value
  101. $options['params'] = array_merge(
  102. array(
  103. 'url' => $options['url'],
  104. ), $options['params']
  105. );
  106. // Return html embed code
  107. if ( $iframe ) {
  108. return soundcloud_iframe_widget( $options );
  109. } else {
  110. return soundcloud_flash_widget( $options );
  111. }
  112. }
  113. /**
  114. * Plugin options getter
  115. *
  116. * @param string|array $option Option name
  117. * @param mixed $default Default value
  118. *
  119. * @return mixed Option value
  120. */
  121. function soundcloud_get_option( $option, $default = false ) {
  122. $value = get_option( 'soundcloud_' . $option );
  123. return $value === '' ? $default : $value;
  124. }
  125. /**
  126. * Booleanize a value
  127. *
  128. * @param boolean|string $value
  129. *
  130. * @return boolean
  131. */
  132. function soundcloud_booleanize( $value ) {
  133. return is_bool( $value ) ? $value : $value === 'true' ? true : false;
  134. }
  135. /**
  136. * Decide if a url has a tracklist
  137. *
  138. * @param string $url
  139. *
  140. * @return boolean
  141. */
  142. function soundcloud_url_has_tracklist( $url ) {
  143. return preg_match( '/^(.+?)\/(sets|groups|playlists)\/(.+?)$/', $url );
  144. }
  145. /**
  146. * Parameterize url
  147. *
  148. * @param array $match Matched regex
  149. *
  150. * @return string Parameterized url
  151. */
  152. function soundcloud_oembed_params_callback( $match ) {
  153. global $soundcloud_oembed_params;
  154. // Convert URL to array
  155. $url = parse_url( urldecode( $match[1] ) );
  156. // Convert URL query to array
  157. parse_str( $url['query'], $query_array );
  158. // Build new query string
  159. $query = http_build_query( array_merge( $query_array, $soundcloud_oembed_params ) );
  160. return 'src="' . $url['scheme'] . '://' . $url['host'] . $url['path'] . '?' . $query;
  161. }
  162. /**
  163. * Iframe widget embed code
  164. *
  165. * @param array $options Parameters
  166. *
  167. * @return string Iframe embed code
  168. */
  169. function soundcloud_iframe_widget( $options ) {
  170. // Build URL
  171. $url = set_url_scheme( 'https://w.soundcloud.com/player/?' . http_build_query( $options['params'] ) );
  172. // Set default width if not defined
  173. $width = isset( $options['width'] ) && $options['width'] !== 0 ? $options['width'] : '100%';
  174. // Set default height if not defined
  175. $height = isset( $options['height'] ) && $options['height'] !== 0
  176. ? $options['height']
  177. : ( soundcloud_url_has_tracklist( $options['url'] ) || ( isset( $options['params']['visual'] ) && soundcloud_booleanize( $options['params']['visual'] ) ) ? '450' : '166' );
  178. return sprintf( '<iframe width="%s" height="%s" scrolling="no" frameborder="no" src="%s"></iframe>', $width, $height, $url );
  179. }
  180. /**
  181. * Legacy Flash widget embed code
  182. *
  183. * @param array $options Parameters
  184. *
  185. * @return string Flash embed code
  186. */
  187. function soundcloud_flash_widget( $options ) {
  188. // Build URL
  189. $url = set_url_scheme( 'https://player.soundcloud.com/player.swf?' . http_build_query( $options['params'] ) );
  190. // Set default width if not defined
  191. $width = isset( $options['width'] ) && $options['width'] !== 0 ? $options['width'] : '100%';
  192. // Set default height if not defined
  193. $height = isset( $options['height'] ) && $options['height'] !== 0 ? $options['height'] : ( soundcloud_url_has_tracklist( $options['url'] ) ? '255' : '81' );
  194. return preg_replace(
  195. '/\s\s+/', '', sprintf(
  196. '<object width="%s" height="%s">
  197. <param name="movie" value="%s" />
  198. <param name="allowscriptaccess" value="always" />
  199. <embed width="%s" height="%s" src="%s" allowscriptaccess="always" type="application/x-shockwave-flash"></embed>
  200. </object>', $width, $height, $url, $width, $height, $url
  201. )
  202. );
  203. }
  204. /**
  205. * SoundCloud Embed Reversal
  206. *
  207. * Converts a generic HTML embed code from SoundClound into a
  208. * WordPress.com-compatibly shortcode.
  209. *
  210. * @param string $content HTML content.
  211. *
  212. * @return string Parsed content.
  213. */
  214. function jetpack_soundcloud_embed_reversal( $content ) {
  215. if ( ! is_string( $content ) || false === stripos( $content, 'w.soundcloud.com/player' ) ) {
  216. return $content;
  217. }
  218. /* Sample embed code:
  219. <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>
  220. */
  221. $regexes = array();
  222. $regexes[] = '#<iframe[^>]+?src="((?:https?:)?//w\.soundcloud\.com/player/[^"\']++)"[^>]*+>\s*?</iframe>#i';
  223. $regexes[] = '#&lt;iframe(?:[^&]|&(?!gt;))+?src="((?:https?:)?//w\.soundcloud\.com/player/[^"\']++)"(?:[^&]|&(?!gt;))*+&gt;\s*?&lt;/iframe&gt;#i';
  224. foreach ( $regexes as $regex ) {
  225. if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) {
  226. continue;
  227. }
  228. foreach ( $matches as $match ) {
  229. // if pasted from the visual editor - prevent double encoding
  230. $match[1] = str_replace( '&amp;amp;', '&amp;', $match[1] );
  231. $args = parse_url( html_entity_decode( $match[1] ), PHP_URL_QUERY );
  232. $args = wp_parse_args( $args );
  233. if ( ! preg_match( '#^(?:https?:)?//api\.soundcloud\.com/.+$#i', $args['url'], $url_matches ) ) {
  234. continue;
  235. }
  236. if ( ! preg_match( '#height="(\d+)"#i', $match[0], $hmatch ) ) {
  237. $height = '';
  238. } else {
  239. $height = ' height="' . intval( $hmatch[1] ) . '"';
  240. }
  241. unset( $args['url'] );
  242. $params = 'params="';
  243. if ( count( $args ) > 0 ) {
  244. foreach ( $args as $key => $value ) {
  245. $params .= esc_html( $key ) . '=' . esc_html( $value ) . '&amp;';
  246. }
  247. $params = substr( $params, 0, -5 );
  248. }
  249. $params .= '"';
  250. $shortcode = '[soundcloud url="' . esc_url( $url_matches[0] ) . '" ' . $params . ' width="100%"' . $height . ' iframe="true" /]';
  251. $replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) );
  252. $content = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $shortcode ), $content );
  253. /** This action is documented in modules/shortcodes/youtube.php */
  254. do_action( 'jetpack_embed_to_shortcode', 'soundcloud', $url_matches[0] );
  255. }
  256. }
  257. return $content;
  258. }
  259. add_filter( 'pre_kses', 'jetpack_soundcloud_embed_reversal' );