SoundCloud Shortcode - Version 2.0

Version Description

  • HTML5 Player added as the default player, with Flash as an option and fallback for legacy URL formats.
Download this release

Release Info

Developer por_
Plugin Icon wp plugin SoundCloud Shortcode
Version 2.0
Comparing to
See all releases

Code changes from version 2.1 to 2.0

Files changed (4) hide show
  1. readme.txt +17 -15
  2. screenshot-1.png +0 -0
  3. screenshot-2.png +0 -0
  4. soundcloud-shortcode.php +185 -235
readme.txt CHANGED
@@ -1,15 +1,16 @@
1
  === SoundCloud Shortcode ===
2
- Contributors: jowagener, theophani, por_
 
3
  Tags: soundcloud, html5, flash, player, shortcode,
4
- Requires at least: 3.1.0
5
- Tested up to: 3.4.0
6
  Stable tag: trunk
7
 
8
  The SoundCloud Shortcode plugin allows you to integrate a player widget from SoundCloud into your Wordpress Blog by using a Wordpress shortcodes.
9
 
10
  == Description ==
11
 
12
- The SoundCloud Shortcode plugin allows you to easily integrate a player widget for a track, set or group from SoundCloud into your Wordpress Blog by using a Wordpress shortcode.
13
  Use it like that in your blog post: `[soundcloud]http://soundcloud.com/LINK_TO_TRACK_SET_OR_GROUP[/soundcloud]`
14
  It also supports these optional parameters: width, height and params.
15
  The "params" parameter will pass the given options on to the player widget.
@@ -23,19 +24,23 @@ Our player accepts the following parameter options:
23
  Examples:
24
 
25
  `[soundcloud params="auto_play=true&show_comments=false"]http://soundcloud.com/forss/flickermood[/soundcloud]`
26
- Embed a track player which starts playing automatically and won't show any comments.
27
 
28
- `[soundcloud params="color=33e040&theme_color=80e4a0"]https://soundcloud.com/forss/sets/soulhack[/soundcloud]`
29
  Embeds a set player with a green theme.
30
 
31
- `[soundcloud width="250"]http://soundcloud.com/groups/experimental[/soundcloud]`
32
- Embeds a group player with 250px width.
33
 
34
 
35
  When posting the standard SoundCloud embed code, the plugin tries to use the new HTML5 player, but falls back to the Flash Player for legacy URL formats.
36
-
37
  == Installation ==
 
 
 
38
  == Frequently Asked Questions ==
 
 
39
  == Screenshots ==
40
 
41
  1. This is how the Flash player looks, which is still available as an option. It is also the fallback for legacy URL formats.
@@ -43,19 +48,16 @@ When posting the standard SoundCloud embed code, the plugin tries to use the new
43
  2. This is how the default player looks, which uses HTML5.
44
 
45
  == Changelog ==
46
- = 2.1 =
47
- * Integrate oEmbed
48
-
49
  = 2.0 =
50
  * HTML5 Player added as the default player, with Flash as an option and fallback for legacy URL formats.
51
 
52
  = 1.2.1 =
53
  * Removed flash fallback HTML
54
 
55
- = 1.2 =
56
  * Added options page to allow blog-wide global default settings.
57
 
58
- = 1.1.9 =
59
  * Fix to support resources from api.soundcloud.com
60
  * Security enhancement. Only support players from player.soundcloud.com, player.sandbox-soundcloud.com and player.staging-soundcloud.com
61
 
@@ -63,4 +65,4 @@ When posting the standard SoundCloud embed code, the plugin tries to use the new
63
  Bugfix to use correct SoundCloud player host
64
 
65
  = 1.0 =
66
- * First version
1
  === SoundCloud Shortcode ===
2
+ Contributors: jowagener, theophani, por
3
+ Donate link: http://soundcloud.com
4
  Tags: soundcloud, html5, flash, player, shortcode,
5
+ Requires at least: 2.5.0
6
+ Tested up to: 3.3.1
7
  Stable tag: trunk
8
 
9
  The SoundCloud Shortcode plugin allows you to integrate a player widget from SoundCloud into your Wordpress Blog by using a Wordpress shortcodes.
10
 
11
  == Description ==
12
 
13
+ The SoundCloud Shortcode plugin allows you to easily integrate a player widget for a track, set or group from SoundCloud into your Wordpress Blog by using a Wordpress shortcode.
14
  Use it like that in your blog post: `[soundcloud]http://soundcloud.com/LINK_TO_TRACK_SET_OR_GROUP[/soundcloud]`
15
  It also supports these optional parameters: width, height and params.
16
  The "params" parameter will pass the given options on to the player widget.
24
  Examples:
25
 
26
  `[soundcloud params="auto_play=true&show_comments=false"]http://soundcloud.com/forss/flickermood[/soundcloud]`
27
+ Embed a track player which starts playing automaticly and won't show any comments.
28
 
29
+ `[soundcloud params="color=33e040&theme_color=80e4a0"]http://soundcloud.com/forss/sets/live-4[/soundcloud]`
30
  Embeds a set player with a green theme.
31
 
32
+ `[soundcloud width="250"]http://soundcloud.com/groups/experimental[/soundcloud]`
33
+ Embeds a group player with 250px width.
34
 
35
 
36
  When posting the standard SoundCloud embed code, the plugin tries to use the new HTML5 player, but falls back to the Flash Player for legacy URL formats.
 
37
  == Installation ==
38
+
39
+
40
+
41
  == Frequently Asked Questions ==
42
+
43
+
44
  == Screenshots ==
45
 
46
  1. This is how the Flash player looks, which is still available as an option. It is also the fallback for legacy URL formats.
48
  2. This is how the default player looks, which uses HTML5.
49
 
50
  == Changelog ==
 
 
 
51
  = 2.0 =
52
  * HTML5 Player added as the default player, with Flash as an option and fallback for legacy URL formats.
53
 
54
  = 1.2.1 =
55
  * Removed flash fallback HTML
56
 
57
+ = 1.2 =
58
  * Added options page to allow blog-wide global default settings.
59
 
60
+ = 1.1.9 =
61
  * Fix to support resources from api.soundcloud.com
62
  * Security enhancement. Only support players from player.soundcloud.com, player.sandbox-soundcloud.com and player.staging-soundcloud.com
63
 
65
  Bugfix to use correct SoundCloud player host
66
 
67
  = 1.0 =
68
+ * First version
screenshot-1.png CHANGED
Binary file
screenshot-2.png DELETED
Binary file
soundcloud-shortcode.php CHANGED
@@ -1,274 +1,225 @@
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.1
7
- Author: SoundCloud 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
- /* Register oEmbed provider
18
- ========================================================================== */
19
 
20
- wp_oembed_add_provider('#https?://(?:api\.)?soundcloud\.com/.*#i', 'http://soundcloud.com/oembed', true);
 
 
21
 
 
22
 
23
- /* Register SoundCloud shortcode
24
- ========================================================================== */
 
25
 
26
- add_shortcode("soundcloud", "soundcloud_shortcode");
 
 
27
 
28
- /**
29
- * SoundCloud shortcode handler
30
- * @param {string|array} $atts The attributes passed to the shortcode like [soundcloud attr1="value" /].
31
- * Is an empty string when no arguments are given.
32
- * @param {string} $content The content between non-self closing [soundcloud]…[/soundcloud] tags.
33
- * @return {string} Widget embed code HTML
34
- */
35
- function soundcloud_shortcode($atts, $content = null) {
36
 
37
- // We need to use the WP_Embed class instance
38
- global $wp_embed;
39
 
40
- // Custom shortcode options
41
- $shortcode_options = array_merge(array('url' => trim($content)), is_array($atts) ? $atts : array());
 
 
 
 
 
42
 
43
- // User preference options
44
- $plugin_options = array_filter(array(
45
- 'iframe' => soundcloud_get_option('player_iframe', true),
46
- 'width' => soundcloud_get_option('player_width'),
47
- 'height' => soundcloud_url_has_tracklist($shortcode_options['url']) ? soundcloud_get_option('player_height_multi') : soundcloud_get_option('player_height'),
48
- 'params' => http_build_query(array_filter(array(
49
- 'auto_play' => soundcloud_get_option('auto_play'),
50
- 'show_comments' => soundcloud_get_option('show_comments'),
51
- 'color' => soundcloud_get_option('color'),
52
- 'theme_color' => soundcloud_get_option('theme_color'),
53
- ))),
54
- ));
55
 
56
- // plugin options < shortcode options
57
- $options = array_merge(
58
- $plugin_options,
59
- $shortcode_options
60
- );
61
 
62
- // The "url" option is required
63
- if (!isset($options['url'])) { return ''; }
 
64
 
65
- // Both "width" and "height" need to be integers
66
- if (isset($options['width']) && !preg_match('/^\d+$/', $options['width'])) {
67
- // set to 0 so oEmbed will use the default 100% and WordPress themes will leave it alone
68
- $options['width'] = 0;
69
  }
70
- if (isset($options['height']) && !preg_match('/^\d+$/', $options['height'])) { unset($options['height']); }
71
-
72
- // The "iframe" option must be true to load widget via oEmbed
73
- $oEmbed = soundcloud_booleanize($options['iframe']);
74
-
75
- if ($oEmbed) {
76
- // This handler handles calling the oEmbed class
77
- // and more importantly will also do the caching!
78
- $embed = $wp_embed->shortcode($options, $options['url']);
79
 
80
- // Unfortunately WordPress only passes on "width" and "height" options
81
- // so we have to add custom params ourselves.
82
- if (isset($options['params'])) {
83
- $embed = soundcloud_oembed_params($embed, $options['params']);
 
 
 
 
 
 
 
 
 
84
  }
85
-
86
- return $embed;
87
-
88
- } else {
89
- // We can’t use default WordPress oEmbed implementation since
90
- // it doesn’t support sending the iframe=false parameter.
91
- return soundcloud_flash_widget($options);
92
  }
93
 
94
- }
95
-
96
- /**
97
- * Plugin options getter
98
- * @param {string|array} $option Option name
99
- * @param {mixed} $default Default value
100
- * @return {mixed} Option value
101
- */
102
- function soundcloud_get_option($option, $default = false) {
103
- $value = get_option('soundcloud_' . $option);
104
- return $value === '' ? $default : $value;
105
- }
106
-
107
- /**
108
- * Booleanize a value
109
- * @param {boolean|string} $value
110
- * @return {boolean}
111
- */
112
- function soundcloud_booleanize($value) {
113
- return is_bool($value) ? $value : $value === 'true' ? true : false;
114
- }
115
-
116
- /**
117
- * Decide if a url has a tracklist
118
- * @param {string} $url
119
- * @return {boolean}
120
- */
121
- function soundcloud_url_has_tracklist($url) {
122
- return preg_match('/^(.+?)\/(sets|groups|playlists)\/(.+?)$/', $url);
123
- }
124
-
125
- /**
126
- * Add custom parameters to iframe embed code
127
- * @param {string} $embed Embed code (html)
128
- * @param {string} $query Parameters querystring
129
- * @return {string} Embed code with added parameters
130
- */
131
- function soundcloud_oembed_params($embed, $query) {
132
- // Needs to be global because we can’t pass parameters to regex callback function < PHP 5.3
133
- global $soundcloud_oembed_query;
134
- $soundcloud_oembed_query = $query;
135
- return preg_replace_callback('/src="(https?:\/\/(?:w|wt)\.soundcloud\.(?:com|dev)\/[^"]*)/i', 'soundcloud_oembed_params_callback', $embed);
136
- }
137
-
138
- /**
139
- * Parameterize url
140
- * @param {array} $match Matched regex
141
- * @return {string} Parameterized url
142
- */
143
- function soundcloud_oembed_params_callback($match) {
144
- global $soundcloud_oembed_query;
145
-
146
- // Convert oEmbed query to array
147
- parse_str(html_entity_decode($soundcloud_oembed_query), $oembed_query_array);
148
- // Convert URL to array
149
- $url = parse_url(urldecode($match[1]));
150
- // Convert URL query to array
151
- parse_str($url['query'], $query_array);
152
- // Build new query string
153
- $query = http_build_query(array_merge($query_array, $oembed_query_array));
154
-
155
- return 'src="' . $url['scheme'] . '://' . $url['host'] . $url['path'] . '?' . $query;
156
- }
157
 
158
- /**
159
- * Legacy Flash widget embed code
160
- * @param {options} $options Querystring
161
- * @return {string} Flash embed code
162
- */
163
- function soundcloud_flash_widget($options) {
164
-
165
- $params = array();
166
- // Create an array of 'param=value&param2=value' string
167
- if (isset($options['params'])) {
168
- parse_str(html_entity_decode($options['params']), $params);
169
  }
170
- // Merge in "url" value
171
- $params = array_merge(array(
172
- 'url' => $options['url']
173
- ), $params);
174
-
175
- // Build URL
176
- $url = 'http://player.soundcloud.com/player.swf?' . http_build_query($params);
177
- // Set default width if not defined
178
- $width = isset($options['width']) && $options['width'] !== 0 ? $options['width'] : '100%';
179
- // Set default height if not defined
180
- $height = isset($options['height']) && $options['height'] !== 0 ? $options['height'] : (soundcloud_url_has_tracklist($options['url']) ? '255' : '81');
181
-
182
- return preg_replace('/\s\s+/', "", sprintf('<object width="%s" height="%s">
183
- <param name="movie" value="%s"></param>
184
- <param name="allowscriptaccess" value="always"></param>
185
- <embed width="%s" height="%s" src="%s" allowscriptaccess="always" type="application/x-shockwave-flash"></embed>
186
- </object>', $width, $height, $url, $width, $height, $url));
187
- }
188
 
 
 
 
189
 
190
- /* Register reverse shortcode filter
191
- ========================================================================== */
 
 
 
 
 
 
192
 
193
- // Disabling this for now because it seems to mess up content it shouldn’t
194
- // http://wordpress.org/support/topic/plugin-soundcloud-shortcode-disastrous-update-223-breaks-encoding-of-existing-and-new-posts-when-saving
195
- // http://wordpress.org/support/topic/plugin-soundcloud-shortcode-postpage-content-not-saved-beyond-nbsp-etc-when-plugin-is-activated
196
- // add_filter("content_save_pre", "soundcloud_reverse_shortcode");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
 
198
- /**
199
- * Replace SoundCloud <iframe> widgets with [soundcloud] shortcodes
200
- * @param {string} $content
201
- * @return {string}
202
- */
203
- function soundcloud_reverse_shortcode($content) {
204
- return preg_replace_callback('/<iframe(?:(?!>).)*>.*?<\/iframe>/i', 'soundcloud_reverse_shortcode_callback', stripslashes(html_entity_decode($content)));
205
- }
206
- /**
207
- * Matched iframe regex result to shortcode
208
- * @param {array} $match Matches
209
- * @return {string} Shortcode
210
- */
211
- function soundcloud_reverse_shortcode_callback($match) {
212
 
213
- $tag = $match[0];
214
- $dom = new DOMDocument();
215
- @$dom->loadHTML($tag);
 
 
 
 
 
 
 
216
 
217
- $iframeElement = $dom->getElementsByTagName('iframe')->item(0);
218
 
219
- $src = $iframeElement->getAttribute('src');
220
- if (!preg_match('/w.soundcloud.com/i', $src)) { return $tag; }
221
 
222
- $srcObj = parse_url($src);
223
- $srcQuery = isset($srcObj['query']) ? $srcObj['query'] : '';
 
 
 
 
 
 
 
224
 
225
- // Create an array of querystring
226
- parse_str(html_entity_decode($srcQuery), $params);
 
227
 
228
- $url = isset($params['url']) ? $params['url'] : '';
229
- unset($params['url']);
230
- $params = http_build_query($params);
231
- $width = $iframeElement->getAttribute('width');
232
- $height = $iframeElement->getAttribute('height');
 
 
233
 
234
- return sprintf('[soundcloud url="%s" width="%s" height="%s" params="%s" iframe="true" /]', $url, $width, $height, $params);
235
  }
236
 
237
-
238
- /* Settings
239
- ========================================================================== */
240
-
241
- /* Add settings link on plugin page */
242
- add_filter("plugin_action_links_" . plugin_basename(__FILE__), 'soundcloud_settings_link');
243
-
244
  function soundcloud_settings_link($links) {
245
  $settings_link = '<a href="options-general.php?page=soundcloud-shortcode">Settings</a>';
246
  array_unshift($links, $settings_link);
247
  return $links;
248
  }
249
 
250
- /* Add admin menu */
251
  add_action('admin_menu', 'soundcloud_shortcode_options_menu');
252
  function soundcloud_shortcode_options_menu() {
253
  add_options_page('SoundCloud Options', 'SoundCloud', 'manage_options', 'soundcloud-shortcode', 'soundcloud_shortcode_options');
254
- add_action('admin_init', 'register_soundcloud_settings');
255
  }
256
 
257
  function register_soundcloud_settings() {
258
- register_setting('soundcloud-settings', 'soundcloud_player_height');
259
- register_setting('soundcloud-settings', 'soundcloud_player_height_multi');
260
- register_setting('soundcloud-settings', 'soundcloud_player_width ');
261
- register_setting('soundcloud-settings', 'soundcloud_player_iframe');
262
- register_setting('soundcloud-settings', 'soundcloud_auto_play');
263
- register_setting('soundcloud-settings', 'soundcloud_show_comments');
264
- register_setting('soundcloud-settings', 'soundcloud_color');
265
- register_setting('soundcloud-settings', 'soundcloud_theme_color');
 
266
  }
267
 
 
268
  function soundcloud_shortcode_options() {
269
  if (!current_user_can('manage_options')) {
270
  wp_die( __('You do not have sufficient permissions to access this page.') );
271
  }
 
272
  ?>
273
  <div class="wrap">
274
  <h2>SoundCloud Shortcode Default Settings</h2>
@@ -282,10 +233,8 @@ function soundcloud_shortcode_options() {
282
  <tr valign="top">
283
  <th scope="row">Widget Type</th>
284
  <td>
285
- <input type="radio" id="player_iframe_true" name="soundcloud_player_iframe" value="true" <?php if (strtolower(get_option('soundcloud_player_iframe')) === 'true') echo 'checked'; ?> />
286
- <label for="player_iframe_true" style="margin-right: 1em;">HTML5</label>
287
- <input type="radio" id="player_iframe_false" name="soundcloud_player_iframe" value="false" <?php if (strtolower(get_option('soundcloud_player_iframe')) === 'false') echo 'checked'; ?> />
288
- <label for="player_iframe_false" style="margin-right: 1em;">Flash</label>
289
  </td>
290
  </tr>
291
 
@@ -293,7 +242,7 @@ function soundcloud_shortcode_options() {
293
  <th scope="row">Player Height for Tracks</th>
294
  <td>
295
  <input type="text" name="soundcloud_player_height" value="<?php echo get_option('soundcloud_player_height'); ?>" /> (no unit, or %)<br />
296
- Leave blank to use the default.
297
  </td>
298
  </tr>
299
 
@@ -301,7 +250,7 @@ function soundcloud_shortcode_options() {
301
  <th scope="row">Player Height for Groups/Sets</th>
302
  <td>
303
  <input type="text" name="soundcloud_player_height_multi" value="<?php echo get_option('soundcloud_player_height_multi'); ?>" /> (no unit, or %)<br />
304
- Leave blank to use the default.
305
  </td>
306
  </tr>
307
 
@@ -309,37 +258,38 @@ function soundcloud_shortcode_options() {
309
  <th scope="row">Player Width</th>
310
  <td>
311
  <input type="text" name="soundcloud_player_width" value="<?php echo get_option('soundcloud_player_width'); ?>" /> (no unit, or %)<br />
312
- Leave blank to use the default.
313
  </td>
314
  </tr>
315
 
316
  <tr valign="top">
317
  <th scope="row">Current Default 'params'</th>
318
  <td>
319
- <?php echo http_build_query(array_filter(array(
320
- 'auto_play' => get_option('soundcloud_auto_play'),
321
- 'show_comments' => get_option('soundcloud_show_comments'),
322
- 'color' => get_option('soundcloud_color'),
323
- 'theme_color' => get_option('soundcloud_theme_color'),
324
- ))) ?>
325
  </td>
326
  </tr>
327
 
328
  <tr valign="top">
329
  <th scope="row">Auto Play</th>
330
  <td>
331
- <label for="auto_play_none" style="margin-right: 1em;"><input type="radio" id="auto_play_none" name="soundcloud_auto_play" value="" <?php if (get_option('soundcloud_auto_play') == '') echo 'checked'; ?> />Default</label>
332
- <label for="auto_play_true" style="margin-right: 1em;"><input type="radio" id="auto_play_true" name="soundcloud_auto_play" value="true" <?php if (get_option('soundcloud_auto_play') == 'true') echo 'checked'; ?> />True</label>
333
- <label for="auto_play_false" style="margin-right: 1em;"><input type="radio" id="auto_play_false" name="soundcloud_auto_play" value="false" <?php if (get_option('soundcloud_auto_play') == 'false') echo 'checked'; ?> />False</label>
 
 
 
334
  </td>
335
  </tr>
336
 
337
  <tr valign="top">
338
  <th scope="row">Show Comments</th>
339
  <td>
340
- <label for="show_comments_none" style="margin-right: 1em;"><input type="radio" id="show_comments_none" name="soundcloud_show_comments" value="" <?php if (get_option('soundcloud_show_comments') == '') echo 'checked'; ?> />Default</label>
341
- <label for="show_comments_true" style="margin-right: 1em;"><input type="radio" id="show_comments_true" name="soundcloud_show_comments" value="true" <?php if (get_option('soundcloud_show_comments') == 'true') echo 'checked'; ?> />True</label>
342
- <label for="show_comments_false" style="margin-right: 1em;"><input type="radio" id="show_comments_false" name="soundcloud_show_comments" value="false" <?php if (get_option('soundcloud_show_comments') == 'false') echo 'checked'; ?> />False</label>
 
 
 
343
  </td>
344
  </tr>
345
 
1
  <?php
2
  /*
3
  Plugin Name: SoundCloud Shortcode
4
+ Plugin URI: http://www.soundcloud.com
5
+ Description: SoundCloud Shortcode. Usage in your posts: [soundcloud]http://soundcloud.com/TRACK_PERMALINK[/soundcloud] . Works also with set or group instead of track. You can provide optional parameters height/width/params as follows [soundcloud height="166" params="auto_play=true"]http....
6
+ Version: 2.0
7
+ Author: Johannes Wagener <johannes@soundcloud.com>
8
+ Author URI: http://johannes.wagener.cc
 
 
 
 
 
9
  */
10
 
11
+ /*
12
+ SoundCloud Shortcode (Wordpress Plugin)
13
+ Copyright (C) 2009 Johannes Wagener
14
+ Options support added by Tiffany Conroy <tif@tif.ca>
15
+
16
+ This program is free software: you can redistribute it and/or modify
17
+ it under the terms of the GNU General Public License as published by
18
+ the Free Software Foundation, either version 3 of the License, or
19
+ (at your option) any later version.
20
+
21
+ This program is distributed in the hope that it will be useful,
22
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
23
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
+ GNU General Public License for more details.
25
+
26
+ You should have received a copy of the GNU General Public License
27
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
28
+ */
29
 
30
+ add_shortcode( "soundcloud", "soundcloud_shortcode" );
 
31
 
32
+ function soundcloud_shortcode($attributes, $content=null) {
33
+ return SoundcloudShortcode::parse($attributes, $content);
34
+ }
35
 
36
+ class SoundcloudShortcode {
37
 
38
+ const IFRAME_HEIGHT = '166';
39
+ const IFRAME_TRACKLIST_HEIGHT = '450';
40
+ const IFRAME_WIDTH = '100%';
41
 
42
+ const FLASH_HEIGHT = '81';
43
+ const FLASH_TRACKLIST_HEIGHT = '255';
44
+ const FLASH_WIDTH = '100%';
45
 
46
+ // set to true when we deprecate the flash player
47
+ const DEFAULT_TO_IFRAME = false;
 
 
 
 
 
 
48
 
49
+ public function parse($attributes, $content=null) {
 
50
 
51
+ extract(shortcode_atts(array(
52
+ 'url' => $content,
53
+ 'iframe' => self::getDefaultIframePreference(),
54
+ 'params' => self::getDefaultQuery(),
55
+ 'height' => '',
56
+ 'width' => ''
57
+ ), $attributes));
58
 
59
+ $iframe = self::booleanize($iframe);
 
 
 
 
 
 
 
 
 
 
 
60
 
61
+ // The HTML5 widget doesn't support http://soundcloud.com/<username>
62
+ // style urls yet. So we force the old Flash widget for now.
63
+ if (self::isLegacyURL($url)) {
64
+ $iframe = false;
65
+ }
66
 
67
+ $type = self::getType($url);
68
+ $width = self::getWidth($width, $iframe, $type);
69
+ $height = self::getHeight($height, $iframe, $type);
70
 
71
+ return self::getHTML($url, $iframe, $params, $width, $height);
 
 
 
72
  }
 
 
 
 
 
 
 
 
 
73
 
74
+ public function getDefaultQuery() {
75
+ $options = array(
76
+ 'auto_play',
77
+ 'show_comments',
78
+ 'color',
79
+ 'theme_color'
80
+ );
81
+ $params = array();
82
+ foreach ($options as &$option) {
83
+ $value = get_option('soundcloud_' . $option, '');
84
+ if (!empty($value)) {
85
+ $params[$option] = $value;
86
+ }
87
  }
88
+ return http_build_query($params);
 
 
 
 
 
 
89
  }
90
 
91
+ public function getDefaultIframePreference() {
92
+ $pref = get_option('soundcloud_player_iframe');
93
+ return ($pref === '') ? DEFAULT_TO_IFRAME : self::booleanize($pref);
94
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
+ private function booleanize($value) {
97
+ if ($value && strtolower($value) !== "false") {
98
+ return true;
99
+ } else {
100
+ return false;
101
+ }
 
 
 
 
 
102
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
+ private function isLegacyURL($url) {
105
+ return !preg_match("/api.soundcloud.com/i", $url);
106
+ }
107
 
108
+ private function getWidth($width, $iframe, $type) {
109
+ if (empty($width)) {
110
+ $default = ($iframe) ? self::IFRAME_WIDTH : self::FLASH_WIDTH;
111
+ $width = get_option('soundcloud_player_width');
112
+ $width = $width === '' ? $default : $width;
113
+ }
114
+ return $width;
115
+ }
116
 
117
+ private function getHeight($height, $iframe, $type) {
118
+ switch ($type) {
119
+ case 'groups':
120
+ case 'sets':
121
+ case 'playlists':
122
+ $default = ($iframe) ? self::IFRAME_TRACKLIST_HEIGHT : self::FLASH_TRACKLIST_HEIGHT;
123
+ $height = (empty($height)) ? get_option('soundcloud_player_height_multi') : $height;
124
+ $height = (empty($height)) ? $default : $height;
125
+ if ($iframe) {
126
+ $height = self::fixHeight($height, $default);
127
+ }
128
+ break;
129
+ default:
130
+ $default = ($iframe) ? self::IFRAME_HEIGHT : self::FLASH_HEIGHT;
131
+ $height = (empty($height)) ? get_option('soundcloud_player_height') : $height;
132
+ $height = (empty($height)) ? $default : $height;
133
+ if ($iframe) {
134
+ $height = self::fixHeight($height, $default);
135
+ }
136
+ // sounds can only be default height
137
+ //$height = ($iframe) ? self::IFRAME_HEIGHT : self::FLASH_HEIGHT;
138
+ break;
139
+ }
140
+ return $height;
141
+ }
142
 
143
+ private function fixHeight($height, $min_height) {
144
+ if (!preg_match("/[0-9]+%/", $height) && intval($height) < $min_height) {
145
+ $height = $min_height;
146
+ }
147
+ return $height;
148
+ }
 
 
 
 
 
 
 
 
149
 
150
+ private function getType($url) {
151
+ if (empty($url)) {
152
+ return false;
153
+ }
154
+ if ($url = parse_url($url)) {
155
+ $splitted_url = split( "/", $url['path'] );
156
+ $media_type = $splitted_url[count($splitted_url) - 2];
157
+ return $media_type;
158
+ }
159
+ }
160
 
161
+ private function getHTML($url, $iframe, $params, $width, $height) {
162
 
163
+ $encoded_url = urlencode($url);
164
+ $parsed_url = parse_url($url);
165
 
166
+ if ($iframe) {
167
+ $player_host = 'w.soundcloud.com';
168
+ $player_params = 'url=' . $encoded_url . '&' . $params;
169
+ $player_src = 'http://' . $player_host . '/player/?' . $player_params;
170
+ } else {
171
+ $player_host = preg_replace('/(.+\.)?(((staging|sandbox)-)?soundcloud\.com)/', 'player.$2', $parsed_url['host']);
172
+ $player_params = 'url=' . $encoded_url . '&g=1&' . $params;
173
+ $player_src = 'http://' . $player_host . '/player.swf?' . $player_params;
174
+ }
175
 
176
+ $width = esc_attr($width);
177
+ $height = esc_attr($height);
178
+ $player_src = esc_attr($player_src);
179
 
180
+ if ($iframe) {
181
+ $html = '<iframe width="' . $width . '" height="' . $height . '" scrolling="no" frameborder="no" src="' . $player_src . '"></iframe>';
182
+ } else {
183
+ $html = '<object height="' . $height . '" width="' . $width . '"><param name="movie" value="' . $player_src . '"></param><param name="allowscriptaccess" value="always"></param><embed allowscriptaccess="always" height="' . $height . '" src="' . $player_src . '" type="application/x-shockwave-flash" width="' . $width . '"></embed></object>';
184
+ }
185
+ return $html;
186
+ }
187
 
 
188
  }
189
 
190
+ // Add settings link on plugin page
191
+ add_filter("plugin_action_links_".plugin_basename(__FILE__), 'soundcloud_settings_link' );
 
 
 
 
 
192
  function soundcloud_settings_link($links) {
193
  $settings_link = '<a href="options-general.php?page=soundcloud-shortcode">Settings</a>';
194
  array_unshift($links, $settings_link);
195
  return $links;
196
  }
197
 
198
+ // Add admin menu
199
  add_action('admin_menu', 'soundcloud_shortcode_options_menu');
200
  function soundcloud_shortcode_options_menu() {
201
  add_options_page('SoundCloud Options', 'SoundCloud', 'manage_options', 'soundcloud-shortcode', 'soundcloud_shortcode_options');
202
+ add_action( 'admin_init', 'register_soundcloud_settings' );
203
  }
204
 
205
  function register_soundcloud_settings() {
206
+ // register our settings
207
+ register_setting( 'soundcloud-settings', 'soundcloud_player_height' );
208
+ register_setting( 'soundcloud-settings', 'soundcloud_player_height_multi' );
209
+ register_setting( 'soundcloud-settings', 'soundcloud_player_width ' );
210
+ register_setting( 'soundcloud-settings', 'soundcloud_player_iframe' );
211
+ register_setting( 'soundcloud-settings', 'soundcloud_auto_play' );
212
+ register_setting( 'soundcloud-settings', 'soundcloud_show_comments ' );
213
+ register_setting( 'soundcloud-settings', 'soundcloud_color' );
214
+ register_setting( 'soundcloud-settings', 'soundcloud_theme_color' );
215
  }
216
 
217
+
218
  function soundcloud_shortcode_options() {
219
  if (!current_user_can('manage_options')) {
220
  wp_die( __('You do not have sufficient permissions to access this page.') );
221
  }
222
+
223
  ?>
224
  <div class="wrap">
225
  <h2>SoundCloud Shortcode Default Settings</h2>
233
  <tr valign="top">
234
  <th scope="row">Widget Type</th>
235
  <td>
236
+ <label for="player_iframe_true" style="margin-right: 1em;"><input type="radio" id="player_iframe_true" name="soundcloud_player_iframe" value="true" <?php if (SoundcloudShortcode::getDefaultIframePreference() == true) echo 'checked'; ?> />HTML5</label>
237
+ <label for="player_iframe_false" style="margin-right: 1em;"><input type="radio" id="player_iframe_false" name="soundcloud_player_iframe" value="false" <?php if (SoundcloudShortcode::getDefaultIframePreference() == false) echo 'checked'; ?> />Flash</label>
 
 
238
  </td>
239
  </tr>
240
 
242
  <th scope="row">Player Height for Tracks</th>
243
  <td>
244
  <input type="text" name="soundcloud_player_height" value="<?php echo get_option('soundcloud_player_height'); ?>" /> (no unit, or %)<br />
245
+ Leave blank to use the default, 81 (pixels).
246
  </td>
247
  </tr>
248
 
250
  <th scope="row">Player Height for Groups/Sets</th>
251
  <td>
252
  <input type="text" name="soundcloud_player_height_multi" value="<?php echo get_option('soundcloud_player_height_multi'); ?>" /> (no unit, or %)<br />
253
+ Leave blank to use the default, 225 (pixels).
254
  </td>
255
  </tr>
256
 
258
  <th scope="row">Player Width</th>
259
  <td>
260
  <input type="text" name="soundcloud_player_width" value="<?php echo get_option('soundcloud_player_width'); ?>" /> (no unit, or %)<br />
261
+ Leave blank to use the default, 100%.
262
  </td>
263
  </tr>
264
 
265
  <tr valign="top">
266
  <th scope="row">Current Default 'params'</th>
267
  <td>
268
+ <?php echo SoundcloudShortcode::getDefaultQuery(); ?>
 
 
 
 
 
269
  </td>
270
  </tr>
271
 
272
  <tr valign="top">
273
  <th scope="row">Auto Play</th>
274
  <td>
275
+ <input type="radio" id="auto_play_none" name="soundcloud_auto_play" value=""<?php if (get_option('soundcloud_auto_play') == '') echo 'checked'; ?> />
276
+ <label for="auto_play_none" style="margin-right: 1em;">Default</label>
277
+ <input type="radio" id="auto_play_true" name="soundcloud_auto_play" value="true"<?php if (get_option('soundcloud_auto_play') == 'true') echo 'checked'; ?> />
278
+ <label for="auto_play_true" style="margin-right: 1em;">True</label>
279
+ <input type="radio" id="auto_play_false" name="soundcloud_auto_play" value="false" <?php if (get_option('soundcloud_auto_play') == 'false') echo 'checked'; ?> />
280
+ <label for="auto_play_false" style="margin-right: 1em;">False</label>
281
  </td>
282
  </tr>
283
 
284
  <tr valign="top">
285
  <th scope="row">Show Comments</th>
286
  <td>
287
+ <input type="radio" id="show_comments_none" name="soundcloud_show_comments" value=""<?php if (get_option('soundcloud_show_comments') == '') echo 'checked'; ?> />
288
+ <label for="show_comments_none" style="margin-right: 1em;">Default</label>
289
+ <input type="radio" id="show_comments_true" name="soundcloud_show_comments" value="true"<?php if (get_option('soundcloud_show_comments') == 'true') echo 'checked'; ?> />
290
+ <label for="show_comments_true" style="margin-right: 1em;">True</label>
291
+ <input type="radio" id="show_comments_false" name="soundcloud_show_comments" value="false" <?php if (get_option('soundcloud_show_comments') == 'false') echo 'checked'; ?> />
292
+ <label for="show_comments_false" style="margin-right: 1em;">False</label>
293
  </td>
294
  </tr>
295