Bez popisu

soundcloud.php 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * SoundCloud Shortcode
  4. * Based on this plugin: https://wordpress.org/plugins/soundcloud-shortcode/
  5. *
  6. * Credits:
  7. * Original version: Johannes Wagener <johannes@soundcloud.com>
  8. * Options support: Tiffany Conroy <tiffany@soundcloud.com>
  9. * HTML5 & oEmbed support: Tim Bormans <tim@soundcloud.com>
  10. *
  11. * Examples:
  12. * [soundcloud]http://soundcloud.com/forss/flickermood[/soundcloud]
  13. * [soundcloud url="https://api.soundcloud.com/tracks/156661852" params="auto_play=false&amp;hide_related=false&amp;visual=false" width="100%" height="450" iframe="true" /]
  14. * [soundcloud url="https://api.soundcloud.com/tracks/156661852" params="auto_play=false&amp;hide_related=false&amp;visual=true" width="100%" height="450" iframe="true" /]
  15. * [soundcloud url="https://soundcloud.com/closetorgan/paul-is-dead" width=400 height=400]
  16. * [soundcloud url="https://soundcloud.com/closetorgan/sets/smells-like-lynx-africa-private"]
  17. * [soundcloud url="https://soundcloud.com/closetorgan/sets/smells-like-lynx-africa-private" color="00cc11"]
  18. * <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>
  19. *
  20. * @package automattic/jetpack
  21. */
  22. /**
  23. * SoundCloud shortcode handler
  24. *
  25. * @param string|array $atts The attributes passed to the shortcode like [soundcloud attr1="value" /].
  26. * Is an empty string when no arguments are given.
  27. * @param string $content The content between non-self closing [soundcloud]...[/soundcloud] tags.
  28. *
  29. * @return string Widget embed code HTML
  30. */
  31. function soundcloud_shortcode( $atts, $content = null ) {
  32. global $wp_embed;
  33. // Custom shortcode options.
  34. $shortcode_options = array_merge(
  35. array( 'url' => trim( $content ) ),
  36. is_array( $atts ) ? $atts : array()
  37. );
  38. // The "url" option is required.
  39. if ( empty( $shortcode_options['url'] ) ) {
  40. if ( current_user_can( 'edit_posts' ) ) {
  41. return esc_html__( 'Please specify a Soundcloud URL.', 'jetpack' );
  42. } else {
  43. return '<!-- Missing Soundcloud URL -->';
  44. }
  45. }
  46. // If the shortcode is displayed in a WPCOM notification, display a simple link only.
  47. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  48. require_once WP_CONTENT_DIR . '/lib/display-context.php';
  49. $context = A8C\Display_Context\get_current_context();
  50. if ( A8C\Display_Context\NOTIFICATIONS === $context ) {
  51. return sprintf(
  52. '<a href="%1$s" target="_blank" rel="noopener noreferrer">%1$s</a>',
  53. esc_url( $shortcode_options['url'] )
  54. );
  55. }
  56. }
  57. // Turn shortcode option "param" (param=value&param2=value) into array of params.
  58. $shortcode_params = array();
  59. if ( isset( $shortcode_options['params'] ) ) {
  60. parse_str( html_entity_decode( $shortcode_options['params'] ), $shortcode_params );
  61. $shortcode_options = array_merge(
  62. $shortcode_options,
  63. $shortcode_params
  64. );
  65. unset( $shortcode_options['params'] );
  66. }
  67. $options = shortcode_atts(
  68. // This list used to include an 'iframe' option. We don't include it anymore as we don't support the Flash player anymore.
  69. array(
  70. 'url' => '',
  71. 'width' => soundcloud_get_option( 'player_width' ),
  72. 'height' => soundcloud_url_has_tracklist( $shortcode_options['url'] ) ? soundcloud_get_option( 'player_height_multi' ) : soundcloud_get_option( 'player_height' ),
  73. 'auto_play' => soundcloud_get_option( 'auto_play' ),
  74. 'hide_related' => false,
  75. 'visual' => false,
  76. 'show_comments' => soundcloud_get_option( 'show_comments' ),
  77. 'color' => soundcloud_get_option( 'color' ),
  78. 'show_user' => false,
  79. 'show_reposts' => false,
  80. ),
  81. $shortcode_options,
  82. 'soundcloud'
  83. );
  84. // "width" needs to be an integer.
  85. if ( ! empty( $options['width'] ) && ! preg_match( '/^\d+$/', $options['width'] ) ) {
  86. // set to 0 so oEmbed will use the default 100% and WordPress themes will leave it alone.
  87. $options['width'] = 0;
  88. }
  89. // Set default width if not defined.
  90. $width = ! empty( $options['width'] ) ? absint( $options['width'] ) : '100%';
  91. // Set default height if not defined.
  92. if (
  93. empty( $options['height'] )
  94. || (
  95. // "height" needs to be an integer.
  96. ! empty( $options['height'] )
  97. && ! preg_match( '/^\d+$/', $options['height'] )
  98. )
  99. ) {
  100. if (
  101. soundcloud_url_has_tracklist( $options['url'] )
  102. || 'true' === $options['visual']
  103. ) {
  104. $height = 450;
  105. } else {
  106. $height = 166;
  107. }
  108. } else {
  109. $height = absint( $options['height'] );
  110. }
  111. // Set visual to false when displaying the smallest player.
  112. if ( '20' === $options['height'] ) {
  113. $options['visual'] = false;
  114. }
  115. if (
  116. class_exists( 'Jetpack_AMP_Support' )
  117. && Jetpack_AMP_Support::is_amp_request()
  118. && ! empty( $options['url'] )
  119. && 'api.soundcloud.com' !== wp_parse_url( $options['url'], PHP_URL_HOST )
  120. ) {
  121. // Defer to oEmbed if an oEmbeddable URL is provided.
  122. return $wp_embed->shortcode( $options, $options['url'] );
  123. }
  124. // Build our list of Soundcloud parameters.
  125. $query_args = array(
  126. 'url' => rawurlencode( $options['url'] ),
  127. );
  128. // Add our options, if they are set to true or false.
  129. foreach ( $options as $name => $value ) {
  130. if ( 'true' === $value ) {
  131. $query_args[ $name ] = 'true';
  132. }
  133. if ( 'false' === $value || false === $value ) {
  134. $query_args[ $name ] = 'false';
  135. }
  136. }
  137. // Add the color parameter if it was specified and is a valid color.
  138. if ( ! empty( $options['color'] ) ) {
  139. $color = sanitize_hex_color_no_hash( $options['color'] );
  140. if ( ! empty( $color ) ) {
  141. $query_args['color'] = $color;
  142. }
  143. }
  144. // Build final embed URL.
  145. $url = add_query_arg(
  146. $query_args,
  147. 'https://w.soundcloud.com/player/'
  148. );
  149. return sprintf(
  150. '<iframe width="%1$s" height="%2$d" scrolling="no" frameborder="no" src="%3$s"></iframe>',
  151. esc_attr( $width ),
  152. esc_attr( $height ),
  153. $url
  154. );
  155. }
  156. add_shortcode( 'soundcloud', 'soundcloud_shortcode' );
  157. /**
  158. * Plugin options getter
  159. *
  160. * @param string|array $option Option name.
  161. * @param mixed $default Default value.
  162. *
  163. * @return mixed Option value
  164. */
  165. function soundcloud_get_option( $option, $default = false ) {
  166. $value = get_option( 'soundcloud_' . $option );
  167. return '' === $value ? $default : $value;
  168. }
  169. /**
  170. * Decide if a url has a tracklist
  171. *
  172. * @param string $url Soundcloud URL.
  173. *
  174. * @return boolean
  175. */
  176. function soundcloud_url_has_tracklist( $url ) {
  177. return preg_match( '/^(.+?)\/(sets|groups|playlists)\/(.+?)$/', $url );
  178. }
  179. /**
  180. * SoundCloud Embed Reversal
  181. *
  182. * Converts a generic HTML embed code from SoundClound into a
  183. * WordPress.com-compatibly shortcode.
  184. *
  185. * @param string $content HTML content.
  186. *
  187. * @return string Parsed content.
  188. */
  189. function jetpack_soundcloud_embed_reversal( $content ) {
  190. if ( ! is_string( $content ) || false === stripos( $content, 'w.soundcloud.com/player' ) ) {
  191. return $content;
  192. }
  193. $regexes = array();
  194. $regexes[] = '#<iframe[^>]+?src="((?:https?:)?//w\.soundcloud\.com/player/[^"\']++)"[^>]*+>\s*?</iframe>#i';
  195. $regexes[] = '#&lt;iframe(?:[^&]|&(?!gt;))+?src="((?:https?:)?//w\.soundcloud\.com/player/[^"\']++)"(?:[^&]|&(?!gt;))*+&gt;\s*?&lt;/iframe&gt;#i';
  196. foreach ( $regexes as $regex ) {
  197. if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) {
  198. continue;
  199. }
  200. foreach ( $matches as $match ) {
  201. // if pasted from the visual editor - prevent double encoding.
  202. $match[1] = str_replace( '&amp;amp;', '&amp;', $match[1] );
  203. $args = wp_parse_url( html_entity_decode( $match[1] ), PHP_URL_QUERY );
  204. $args = wp_parse_args( $args );
  205. if ( ! preg_match( '#^(?:https?:)?//api\.soundcloud\.com/.+$#i', $args['url'], $url_matches ) ) {
  206. continue;
  207. }
  208. if ( ! preg_match( '#height="(\d+)"#i', $match[0], $hmatch ) ) {
  209. $height = '';
  210. } else {
  211. $height = ' height="' . (int) $hmatch[1] . '"';
  212. }
  213. unset( $args['url'] );
  214. $params = 'params="';
  215. if ( count( $args ) > 0 ) {
  216. foreach ( $args as $key => $value ) {
  217. $params .= esc_html( $key ) . '=' . esc_html( $value ) . '&amp;';
  218. }
  219. $params = substr( $params, 0, -5 );
  220. }
  221. $params .= '"';
  222. $shortcode = '[soundcloud url="' . esc_url( $url_matches[0] ) . '" ' . $params . ' width="100%"' . $height . ' iframe="true" /]';
  223. $replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) );
  224. $content = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $shortcode ), $content );
  225. /** This action is documented in modules/shortcodes/youtube.php */
  226. do_action( 'jetpack_embed_to_shortcode', 'soundcloud', $url_matches[0] );
  227. }
  228. }
  229. return $content;
  230. }
  231. add_filter( 'pre_kses', 'jetpack_soundcloud_embed_reversal' );