PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 3.2.5
Jetpack – WP Security, Backup, Speed, & Growth v3.2.5
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / shortcodes / soundcloud.php

soundcloud.php in Jetpack – WP Security, Backup, Speed, & Growth 3.2.5, at modules/shortcodes/soundcloud.php

258 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
11 Original version: Johannes Wagener <johannes@soundcloud.com>
12 Options support: Tiffany Conroy <tiffany@soundcloud.com>
13 HTML5 & oEmbed support: Tim Bormans <tim@soundcloud.com>
14 */
15
16 /*
17 A8C: Taken from http://plugins.svn.wordpress.org/soundcloud-shortcode/trunk/
18 at revision 664386.
19
20 Commenting out (instead of removing) and replacing code with custom modifs
21 so it's eqsy to see what differs from the standard DOTORG version.
22
23 All custom modifs are annoted with "A8C" keyword in comment.
24 */
25
26 /**
27 * Register oEmbed provider
28 */
29
30 wp_oembed_add_provider('#https?://(?:api\.)?soundcloud\.com/.*#i', 'http://soundcloud.com/oembed', true);
31
32
33 /**
34 * Register SoundCloud shortcode
35 */
36
37 add_shortcode("soundcloud", "soundcloud_shortcode");
38
39
40 /**
41 * SoundCloud shortcode handler
42 * @param {string|array} $atts The attributes passed to the shortcode like [soundcloud attr1="value" /].
43 * Is an empty string when no arguments are given.
44 * @param {string} $content The content between non-self closing [soundcloud]…[/soundcloud] tags.
45 * @return {string} Widget embed code HTML
46 */
47 function soundcloud_shortcode($atts, $content = null) {
48
49 // Custom shortcode options
50 $shortcode_options = array_merge(array('url' => trim($content)), is_array($atts) ? $atts : array());
51
52 // Turn shortcode option "param" (param=value&param2=value) into array
53 $shortcode_params = array();
54 if (isset($shortcode_options['params'])) {
55 parse_str(html_entity_decode($shortcode_options['params']), $shortcode_params);
56 }
57 $shortcode_options['params'] = $shortcode_params;
58
59 // User preference options
60 $plugin_options = array_filter(array(
61 'iframe' => soundcloud_get_option('player_iframe', true),
62 'width' => soundcloud_get_option('player_width'),
63 'height' => soundcloud_url_has_tracklist($shortcode_options['url']) ? soundcloud_get_option('player_height_multi') : soundcloud_get_option('player_height'),
64 'params' => array_filter(array(
65 'auto_play' => soundcloud_get_option('auto_play'),
66 'show_comments' => soundcloud_get_option('show_comments'),
67 'color' => soundcloud_get_option('color'),
68 'theme_color' => soundcloud_get_option('theme_color'),
69 )),
70 ));
71
72 // Needs to be an array
73 if ( ! isset( $plugin_options['params'] ) ) {
74 $plugin_options['params'] = array();
75 }
76
77 // plugin options < shortcode options
78 $options = array_merge(
79 $plugin_options,
80 $shortcode_options
81 );
82
83 // plugin params < shortcode params
84 $options['params'] = array_merge(
85 $plugin_options['params'],
86 $shortcode_options['params']
87 );
88
89 // The "url" option is required
90 if ( ! isset( $options['url'] ) ) {
91 return '';
92 } else {
93 $options['url'] = trim($options['url']);
94 }
95
96 // Both "width" and "height" need to be integers
97 if (isset($options['width']) && !preg_match('/^\d+$/', $options['width'])) {
98 // set to 0 so oEmbed will use the default 100% and WordPress themes will leave it alone
99 $options['width'] = 0;
100 }
101 if (isset($options['height']) && !preg_match('/^\d+$/', $options['height'])) { unset($options['height']); }
102
103 // The "iframe" option must be true to load the iframe widget
104 if ( isset( $options[ 'iframe' ] ) ) {
105 $iframe = soundcloud_booleanize($options['iframe'])
106 // Default to flash widget for permalink urls (e.g. http://soundcloud.com/{username})
107 // because HTML5 widget doesn’t support those yet
108 ? preg_match('/api.soundcloud.com/i', $options['url'])
109 : false;
110
111 if ( $iframe ) {
112 return soundcloud_iframe_widget( $options );
113 }
114 }
115
116 return soundcloud_flash_widget( $options );
117 }
118
119 /**
120 * Plugin options getter
121 * @param {string|array} $option Option name
122 * @param {mixed} $default Default value
123 * @return {mixed} Option value
124 */
125 function soundcloud_get_option($option, $default = false) {
126 $value = get_option('soundcloud_' . $option);
127 return $value === '' ? $default : $value;
128 }
129
130 /**
131 * Booleanize a value
132 * @param {boolean|string} $value
133 * @return {boolean}
134 */
135 function soundcloud_booleanize($value) {
136 return is_bool($value) ? $value : $value === 'true' ? true : false;
137 }
138
139 /**
140 * Decide if a url has a tracklist
141 * @param {string} $url
142 * @return {boolean}
143 */
144 function soundcloud_url_has_tracklist($url) {
145 return preg_match( '/^(.+?)\/(sets|groups|playlists)\/(.+?)$/', $url );
146 }
147
148 /**
149 * Parameterize url
150 * @param {array} $match Matched regex
151 * @return {string} Parameterized url
152 */
153 function soundcloud_oembed_params_callback($match) {
154 global $soundcloud_oembed_params;
155
156 // Convert URL to array
157 $url = parse_url(urldecode($match[1]));
158 // Convert URL query to array
159 parse_str($url['query'], $query_array);
160 // Build new query string
161 $query = http_build_query(array_merge($query_array, $soundcloud_oembed_params));
162
163 return 'src="' . $url['scheme'] . '://' . $url['host'] . $url['path'] . '?' . $query;
164 }
165
166 /**
167 * Iframe widget embed code
168 * @param {array} $options Parameters
169 * @return {string} Iframe embed code
170 */
171 function soundcloud_iframe_widget($options) {
172
173 // Merge in "url" value
174 $options['params'] = array_merge(array(
175 'url' => $options['url']
176 ), $options['params']);
177
178 // Build URL
179 $url = set_url_scheme( 'http://w.soundcloud.com/player/?' . http_build_query($options['params']) );
180 // Set default width if not defined
181 $width = isset($options['width']) && $options['width'] !== 0 ? $options['width'] : '100%';
182 // Set default height if not defined
183 $height = isset($options['height']) && $options['height'] !== 0 ? $options['height'] : (soundcloud_url_has_tracklist($options['url']) ? '450' : '166');
184
185 return sprintf('<iframe width="%s" height="%s" scrolling="no" frameborder="no" src="%s"></iframe>', $width, $height, $url);
186 }
187
188 /**
189 * Legacy Flash widget embed code
190 * @param {array} $options Parameters
191 * @return {string} Flash embed code
192 */
193 function soundcloud_flash_widget($options) {
194
195 // Merge in "url" value
196 $options['params'] = array_merge(array(
197 'url' => $options['url']
198 ), $options['params']);
199
200 // Build URL
201 $url = set_url_scheme( 'http://player.soundcloud.com/player.swf?' . http_build_query($options['params']) );
202 // Set default width if not defined
203 $width = isset($options['width']) && $options['width'] !== 0 ? $options['width'] : '100%';
204 // Set default height if not defined
205 $height = isset($options['height']) && $options['height'] !== 0 ? $options['height'] : (soundcloud_url_has_tracklist($options['url']) ? '255' : '81');
206
207 return preg_replace('/\s\s+/', "", sprintf('<object width="%s" height="%s">
208 <param name="movie" value="%s"></param>
209 <param name="allowscriptaccess" value="always"></param>
210 <embed width="%s" height="%s" src="%s" allowscriptaccess="always" type="application/x-shockwave-flash"></embed>
211 </object>', $width, $height, $url, $width, $height, $url));
212 }
213
214
215 /**
216 * SoundCloud Embed Reversal
217 *
218 * Converts a generic HTML embed code from SoundClound into a
219 * WordPress.com-compatibly shortcode.
220 */
221 function jetpack_soundcloud_embed_reversal( $content ) {
222 if ( false === stripos( $content, 'w.soundcloud.com/player' ) )
223 return $content;
224
225 /* Sample embed code:
226
227 <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>
228 */
229
230 $regexes = array();
231
232 $regexes[] = '#<iframe[^>]+?src="((?:https?:)?//w\.soundcloud\.com/player/[^"\']++)"[^>]*+>\s*?</iframe>#i';
233 $regexes[] = '#&lt;iframe(?:[^&]|&(?!gt;))+?src="((?:https?:)?//w\.soundcloud\.com/player/[^"\']++)"(?:[^&]|&(?!gt;))*+&gt;\s*?&lt;/iframe&gt;#i';
234
235 foreach ( $regexes as $regex ) {
236 if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) )
237 continue;
238
239 foreach ( $matches as $match ) {
240 $args = parse_url( html_entity_decode( $match[1] ), PHP_URL_QUERY );
241 $args = wp_parse_args( $args );
242
243 if ( ! preg_match( '#^(?:https?:)?//api\.soundcloud\.com/.+$#i', $args['url'], $url_matches ) )
244 continue;
245
246 $shortcode = sprintf( '[soundcloud url="%s"]', esc_url( $url_matches[0] ) );
247 $replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) );
248 $content = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $shortcode ), $content );
249 do_action( 'jetpack_embed_to_shortcode', 'soundcloud', $url_matches[0] );
250 }
251 }
252
253 return $content;
254 }
255
256
257 add_filter( 'pre_kses', 'jetpack_soundcloud_embed_reversal' );
258