PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 2.7.5
Jetpack – WP Security, Backup, Speed, & Growth v2.7.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 2.7.5, at modules/shortcodes/soundcloud.php

215 lines 7.5 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 /* Register oEmbed provider
27 -------------------------------------------------------------------------- */
28
29 wp_oembed_add_provider('#https?://(?:api\.)?soundcloud\.com/.*#i', 'http://soundcloud.com/oembed', true);
30
31
32 /* Register SoundCloud shortcode
33 -------------------------------------------------------------------------- */
34
35 add_shortcode("soundcloud", "soundcloud_shortcode");
36
37
38 /**
39 * SoundCloud shortcode handler
40 * @param {string|array} $atts The attributes passed to the shortcode like [soundcloud attr1="value" /].
41 * Is an empty string when no arguments are given.
42 * @param {string} $content The content between non-self closing [soundcloud]…[/soundcloud] tags.
43 * @return {string} Widget embed code HTML
44 */
45 function soundcloud_shortcode($atts, $content = null) {
46
47 // Custom shortcode options
48 $shortcode_options = array_merge(array('url' => trim($content)), is_array($atts) ? $atts : array());
49
50 // Turn shortcode option "param" (param=value&param2=value) into array
51 $shortcode_params = array();
52 if (isset($shortcode_options['params'])) {
53 parse_str(html_entity_decode($shortcode_options['params']), $shortcode_params);
54 }
55 $shortcode_options['params'] = $shortcode_params;
56
57 // User preference options
58 $plugin_options = array_filter(array(
59 'iframe' => soundcloud_get_option('player_iframe', true),
60 'width' => soundcloud_get_option('player_width'),
61 'height' => soundcloud_url_has_tracklist($shortcode_options['url']) ? soundcloud_get_option('player_height_multi') : soundcloud_get_option('player_height'),
62 'params' => array_filter(array(
63 'auto_play' => soundcloud_get_option('auto_play'),
64 'show_comments' => soundcloud_get_option('show_comments'),
65 'color' => soundcloud_get_option('color'),
66 'theme_color' => soundcloud_get_option('theme_color'),
67 )),
68 ));
69 // Needs to be an array
70 if (!isset($plugin_options['params'])) { $plugin_options['params'] = array(); }
71
72 // plugin options < shortcode options
73 $options = array_merge(
74 $plugin_options,
75 $shortcode_options
76 );
77
78 // plugin params < shortcode params
79 $options['params'] = array_merge(
80 $plugin_options['params'],
81 $shortcode_options['params']
82 );
83
84 // The "url" option is required
85 if (!isset($options['url'])) {
86 return '';
87 } else {
88 $options['url'] = trim($options['url']);
89 }
90
91 // Both "width" and "height" need to be integers
92 if (isset($options['width']) && !preg_match('/^\d+$/', $options['width'])) {
93 // set to 0 so oEmbed will use the default 100% and WordPress themes will leave it alone
94 $options['width'] = 0;
95 }
96 if (isset($options['height']) && !preg_match('/^\d+$/', $options['height'])) { unset($options['height']); }
97
98 // The "iframe" option must be true to load the iframe widget
99 $iframe = soundcloud_booleanize($options['iframe'])
100 // Default to flash widget for permalink urls (e.g. http://soundcloud.com/{username})
101 // because HTML5 widget doesn’t support those yet
102 ? preg_match('/api.soundcloud.com/i', $options['url'])
103 : false;
104
105 // Return html embed code
106 if ($iframe) {
107 return soundcloud_iframe_widget($options);
108 } else {
109 return soundcloud_flash_widget($options);
110 }
111
112 }
113
114 /**
115 * Plugin options getter
116 * @param {string|array} $option Option name
117 * @param {mixed} $default Default value
118 * @return {mixed} Option value
119 */
120 function soundcloud_get_option($option, $default = false) {
121 $value = get_option('soundcloud_' . $option);
122 return $value === '' ? $default : $value;
123 }
124
125 /**
126 * Booleanize a value
127 * @param {boolean|string} $value
128 * @return {boolean}
129 */
130 function soundcloud_booleanize($value) {
131 return is_bool($value) ? $value : $value === 'true' ? true : false;
132 }
133
134 /**
135 * Decide if a url has a tracklist
136 * @param {string} $url
137 * @return {boolean}
138 */
139 function soundcloud_url_has_tracklist($url) {
140 return preg_match('/^(.+?)\/(sets|groups|playlists)\/(.+?)$/', $url);
141 }
142
143 /**
144 * Parameterize url
145 * @param {array} $match Matched regex
146 * @return {string} Parameterized url
147 */
148 function soundcloud_oembed_params_callback($match) {
149 global $soundcloud_oembed_params;
150
151 // Convert URL to array
152 $url = parse_url(urldecode($match[1]));
153 // Convert URL query to array
154 parse_str($url['query'], $query_array);
155 // Build new query string
156 $query = http_build_query(array_merge($query_array, $soundcloud_oembed_params));
157
158 return 'src="' . $url['scheme'] . '://' . $url['host'] . $url['path'] . '?' . $query;
159 }
160
161 /**
162 * Iframe widget embed code
163 * @param {array} $options Parameters
164 * @return {string} Iframe embed code
165 */
166 function soundcloud_iframe_widget($options) {
167
168 // Merge in "url" value
169 $options['params'] = array_merge(array(
170 'url' => $options['url']
171 ), $options['params']);
172
173 // Build URL
174 $url = 'http://w.soundcloud.com/player?' . http_build_query($options['params']);
175 // Set default width if not defined
176 $width = isset($options['width']) && $options['width'] !== 0 ? $options['width'] : '100%';
177 // Set default height if not defined
178 $height = isset($options['height']) && $options['height'] !== 0 ? $options['height'] : (soundcloud_url_has_tracklist($options['url']) ? '450' : '166');
179
180 return sprintf('<iframe width="%s" height="%s" scrolling="no" frameborder="no" src="%s"></iframe>', $width, $height, $url);
181 }
182
183 /**
184 * Legacy Flash widget embed code
185 * @param {array} $options Parameters
186 * @return {string} Flash embed code
187 */
188 function soundcloud_flash_widget($options) {
189
190 // Merge in "url" value
191 $options['params'] = array_merge(array(
192 'url' => $options['url']
193 ), $options['params']);
194
195 // Build URL
196 $url = 'http://player.soundcloud.com/player.swf?' . http_build_query($options['params']);
197 // Set default width if not defined
198 $width = isset($options['width']) && $options['width'] !== 0 ? $options['width'] : '100%';
199 // Set default height if not defined
200 $height = isset($options['height']) && $options['height'] !== 0 ? $options['height'] : (soundcloud_url_has_tracklist($options['url']) ? '255' : '81');
201
202 return preg_replace('/\s\s+/', "", sprintf('<object width="%s" height="%s">
203 <param name="movie" value="%s"></param>
204 <param name="allowscriptaccess" value="always"></param>
205 <embed width="%s" height="%s" src="%s" allowscriptaccess="always" type="application/x-shockwave-flash"></embed>
206 </object>', $width, $height, $url, $width, $height, $url));
207 }
208
209
210
211 /* Settings
212 -------------------------------------------------------------------------- */
213
214 /* A8C: no user-defined options, KISS */
215