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