SoundCloud Shortcode - Version 2.2.5

Version Description

Download this release

Release Info

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

Code changes from version 2.0 to 2.2.5

Files changed (4) hide show
  1. readme.txt +18 -17
  2. screenshot-1.png +0 -0
  3. screenshot-2.png +0 -0
  4. soundcloud-shortcode.php +239 -175
readme.txt CHANGED
@@ -1,16 +1,15 @@
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,23 +23,19 @@ Our player accepts the following parameter options:
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,16 +43,22 @@ When posting the standard SoundCloud embed code, the plugin tries to use the new
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,4 +66,4 @@ When posting the standard SoundCloud embed code, the plugin tries to use the new
65
  Bugfix to use correct SoundCloud player host
66
 
67
  = 1.0 =
68
- * First version
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
  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
  2. This is how the default player looks, which uses HTML5.
44
 
45
  == Changelog ==
46
+ = 2.2 =
47
+ * Improved default options support
48
+
49
+ = 2.1 =
50
+ * Integrate oEmbed
51
+
52
  = 2.0 =
53
  * HTML5 Player added as the default player, with Flash as an option and fallback for legacy URL formats.
54
 
55
  = 1.2.1 =
56
  * Removed flash fallback HTML
57
 
58
+ = 1.2 =
59
  * Added options page to allow blog-wide global default settings.
60
 
61
+ = 1.1.9 =
62
  * Fix to support resources from api.soundcloud.com
63
  * Security enhancement. Only support players from player.soundcloud.com, player.sandbox-soundcloud.com and player.staging-soundcloud.com
64
 
66
  Bugfix to use correct SoundCloud player host
67
 
68
  = 1.0 =
69
+ * First version
screenshot-1.png CHANGED
Binary file
screenshot-2.png ADDED
Binary file
soundcloud-shortcode.php CHANGED
@@ -1,225 +1,282 @@
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,8 +290,10 @@ function soundcloud_shortcode_options() {
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,7 +301,7 @@ function soundcloud_shortcode_options() {
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,7 +309,7 @@ function soundcloud_shortcode_options() {
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,14 +317,19 @@ function soundcloud_shortcode_options() {
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
 
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.2.5
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
+ // Turn shortcode option "param" (param=value&param2=value) into array
44
+ $shortcode_params = array();
45
+ if (isset($shortcode_options['params'])) {
46
+ parse_str(html_entity_decode($shortcode_options['params']), $shortcode_params);
47
+ }
48
+ $shortcode_options['params'] = $shortcode_params;
49
+
50
+ // User preference options
51
+ $plugin_options = array_filter(array(
52
+ 'iframe' => soundcloud_get_option('player_iframe', true),
53
+ 'width' => soundcloud_get_option('player_width'),
54
+ 'height' => soundcloud_url_has_tracklist($shortcode_options['url']) ? soundcloud_get_option('player_height_multi') : soundcloud_get_option('player_height'),
55
+ 'params' => array_filter(array(
56
+ 'auto_play' => soundcloud_get_option('auto_play'),
57
+ 'show_comments' => soundcloud_get_option('show_comments'),
58
+ 'color' => soundcloud_get_option('color'),
59
+ 'theme_color' => soundcloud_get_option('theme_color'),
60
+ )),
61
+ ));
62
+ // Needs to be an array
63
+ if (!isset($plugin_options['params'])) { $plugin_options['params'] = array(); }
64
+
65
+ // plugin options < shortcode options
66
+ $options = array_merge(
67
+ $plugin_options,
68
+ $shortcode_options
69
+ );
70
+
71
+ // plugin params < shortcode params
72
+ $options['params'] = array_merge(
73
+ $plugin_options['params'],
74
+ $shortcode_options['params']
75
+ );
76
+
77
+ // The "url" option is required
78
+ if (!isset($options['url'])) { return ''; }
79
+
80
+ // Both "width" and "height" need to be integers
81
+ if (isset($options['width']) && !preg_match('/^\d+$/', $options['width'])) {
82
+ // set to 0 so oEmbed will use the default 100% and WordPress themes will leave it alone
83
+ $options['width'] = 0;
84
+ }
85
+ if (isset($options['height']) && !preg_match('/^\d+$/', $options['height'])) { unset($options['height']); }
86
 
87
+ // The "iframe" option must be true to load widget via oEmbed
88
+ $oEmbed = soundcloud_booleanize($options['iframe']);
89
 
90
+ if ($oEmbed) {
91
+ // This handler handles calling the oEmbed class
92
+ // and more importantly will also do the caching!
93
+ $embed = $wp_embed->shortcode($options, $options['url']);
94
+
95
+ // Unfortunately WordPress only passes on "width" and "height" options
96
+ // so we have to add custom params ourselves.
97
+ if (count($options['params'])) {
98
+ $embed = soundcloud_oembed_params($embed, $options['params']);
99
  }
100
 
101
+ return $embed;
 
 
102
 
103
+ } else {
104
+ // We can’t use default WordPress oEmbed implementation since
105
+ // it doesn’t support sending the iframe=false parameter.
106
+ return soundcloud_flash_widget($options);
107
  }
108
 
109
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
+ /**
112
+ * Plugin options getter
113
+ * @param {string|array} $option Option name
114
+ * @param {mixed} $default Default value
115
+ * @return {mixed} Option value
116
+ */
117
+ function soundcloud_get_option($option, $default = false) {
118
+ $value = get_option('soundcloud_' . $option);
119
+ return $value === '' ? $default : $value;
120
+ }
121
 
122
+ /**
123
+ * Booleanize a value
124
+ * @param {boolean|string} $value
125
+ * @return {boolean}
126
+ */
127
+ function soundcloud_booleanize($value) {
128
+ return is_bool($value) ? $value : $value === 'true' ? true : false;
129
+ }
130
 
131
+ /**
132
+ * Decide if a url has a tracklist
133
+ * @param {string} $url
134
+ * @return {boolean}
135
+ */
136
+ function soundcloud_url_has_tracklist($url) {
137
+ return preg_match('/^(.+?)\/(sets|groups|playlists)\/(.+?)$/', $url);
138
+ }
139
 
140
+ /**
141
+ * Add custom parameters to iframe embed code
142
+ * @param {string} $embed Embed code (html)
143
+ * @param {array} $params Parameters array
144
+ * @return {string} Embed code with added parameters
145
+ */
146
+ function soundcloud_oembed_params($embed, $params) {
147
+ // Needs to be global because we can’t pass parameters to regex callback function < PHP 5.3
148
+ global $soundcloud_oembed_params;
149
+ $soundcloud_oembed_params = $params;
150
+ return preg_replace_callback('/src="(https?:\/\/(?:w|wt)\.soundcloud\.(?:com|dev)\/[^"]*)/i', 'soundcloud_oembed_params_callback', $embed);
151
+ }
152
 
153
+ /**
154
+ * Parameterize url
155
+ * @param {array} $match Matched regex
156
+ * @return {string} Parameterized url
157
+ */
158
+ function soundcloud_oembed_params_callback($match) {
159
+ global $soundcloud_oembed_params;
160
+
161
+ // Convert URL to array
162
+ $url = parse_url(urldecode($match[1]));
163
+ // Convert URL query to array
164
+ parse_str($url['query'], $query_array);
165
+ // Build new query string
166
+ $query = http_build_query(array_merge($query_array, $soundcloud_oembed_params));
167
+
168
+ return 'src="' . $url['scheme'] . '://' . $url['host'] . $url['path'] . '?' . $query;
169
+ }
 
 
 
 
 
 
 
 
170
 
171
+ /**
172
+ * Legacy Flash widget embed code
173
+ * @param {options} $options Querystring
174
+ * @return {string} Flash embed code
175
+ */
176
+ function soundcloud_flash_widget($options) {
177
+
178
+ // Merge in "url" value
179
+ $options['params'] = array_merge(array(
180
+ 'url' => $options['url']
181
+ ), $options['params']);
182
+
183
+ // Build URL
184
+ $url = 'http://player.soundcloud.com/player.swf?' . http_build_query($options['params']);
185
+ // Set default width if not defined
186
+ $width = isset($options['width']) && $options['width'] !== 0 ? $options['width'] : '100%';
187
+ // Set default height if not defined
188
+ $height = isset($options['height']) && $options['height'] !== 0 ? $options['height'] : (soundcloud_url_has_tracklist($options['url']) ? '255' : '81');
189
+
190
+ return preg_replace('/\s\s+/', "", sprintf('<object width="%s" height="%s">
191
+ <param name="movie" value="%s"></param>
192
+ <param name="allowscriptaccess" value="always"></param>
193
+ <embed width="%s" height="%s" src="%s" allowscriptaccess="always" type="application/x-shockwave-flash"></embed>
194
+ </object>', $width, $height, $url, $width, $height, $url));
195
+ }
196
 
 
 
 
 
 
 
 
 
 
 
197
 
198
+ /* Register reverse shortcode filter
199
+ ========================================================================== */
200
 
201
+ // Disabling this for now because it seems to mess up content it shouldn’t
202
+ // http://wordpress.org/support/topic/plugin-soundcloud-shortcode-disastrous-update-223-breaks-encoding-of-existing-and-new-posts-when-saving
203
+ // http://wordpress.org/support/topic/plugin-soundcloud-shortcode-postpage-content-not-saved-beyond-nbsp-etc-when-plugin-is-activated
204
+ // add_filter("content_save_pre", "soundcloud_reverse_shortcode");
205
 
206
+ /**
207
+ * Replace SoundCloud <iframe> widgets with [soundcloud] shortcodes
208
+ * @param {string} $content
209
+ * @return {string}
210
+ */
211
+ function soundcloud_reverse_shortcode($content) {
212
+ return preg_replace_callback('/<iframe(?:(?!>).)*>.*?<\/iframe>/i', 'soundcloud_reverse_shortcode_callback', stripslashes(html_entity_decode($content)));
213
+ }
214
+ /**
215
+ * Matched iframe regex result to shortcode
216
+ * @param {array} $match Matches
217
+ * @return {string} Shortcode
218
+ */
219
+ function soundcloud_reverse_shortcode_callback($match) {
220
 
221
+ $tag = $match[0];
222
+ $dom = new DOMDocument();
223
+ @$dom->loadHTML($tag);
224
 
225
+ $iframeElement = $dom->getElementsByTagName('iframe')->item(0);
226
+
227
+ $src = $iframeElement->getAttribute('src');
228
+ if (!preg_match('/w.soundcloud.com/i', $src)) { return $tag; }
229
+
230
+ $srcObj = parse_url($src);
231
+ $srcQuery = isset($srcObj['query']) ? $srcObj['query'] : '';
232
+
233
+ // Create an array of querystring
234
+ parse_str(html_entity_decode($srcQuery), $params);
235
+
236
+ $url = isset($params['url']) ? $params['url'] : '';
237
+ unset($params['url']);
238
+ $params = http_build_query($params);
239
+ $width = $iframeElement->getAttribute('width');
240
+ $height = $iframeElement->getAttribute('height');
241
 
242
+ return sprintf('[soundcloud url="%s" width="%s" height="%s" params="%s" iframe="true" /]', $url, $width, $height, $params);
243
  }
244
 
245
+
246
+ /* Settings
247
+ ========================================================================== */
248
+
249
+ /* Add settings link on plugin page */
250
+ add_filter("plugin_action_links_" . plugin_basename(__FILE__), 'soundcloud_settings_link');
251
+
252
  function soundcloud_settings_link($links) {
253
  $settings_link = '<a href="options-general.php?page=soundcloud-shortcode">Settings</a>';
254
  array_unshift($links, $settings_link);
255
  return $links;
256
  }
257
 
258
+ /* Add admin menu */
259
  add_action('admin_menu', 'soundcloud_shortcode_options_menu');
260
  function soundcloud_shortcode_options_menu() {
261
  add_options_page('SoundCloud Options', 'SoundCloud', 'manage_options', 'soundcloud-shortcode', 'soundcloud_shortcode_options');
262
+ add_action('admin_init', 'register_soundcloud_settings');
263
  }
264
 
265
  function register_soundcloud_settings() {
266
+ register_setting('soundcloud-settings', 'soundcloud_player_height');
267
+ register_setting('soundcloud-settings', 'soundcloud_player_height_multi');
268
+ register_setting('soundcloud-settings', 'soundcloud_player_width ');
269
+ register_setting('soundcloud-settings', 'soundcloud_player_iframe');
270
+ register_setting('soundcloud-settings', 'soundcloud_auto_play');
271
+ register_setting('soundcloud-settings', 'soundcloud_show_comments');
272
+ register_setting('soundcloud-settings', 'soundcloud_color');
273
+ register_setting('soundcloud-settings', 'soundcloud_theme_color');
 
274
  }
275
 
 
276
  function soundcloud_shortcode_options() {
277
  if (!current_user_can('manage_options')) {
278
  wp_die( __('You do not have sufficient permissions to access this page.') );
279
  }
 
280
  ?>
281
  <div class="wrap">
282
  <h2>SoundCloud Shortcode Default Settings</h2>
290
  <tr valign="top">
291
  <th scope="row">Widget Type</th>
292
  <td>
293
+ <input type="radio" id="player_iframe_true" name="soundcloud_player_iframe" value="true" <?php if (strtolower(get_option('soundcloud_player_iframe')) === 'true') echo 'checked'; ?> />
294
+ <label for="player_iframe_true" style="margin-right: 1em;">HTML5</label>
295
+ <input type="radio" id="player_iframe_false" name="soundcloud_player_iframe" value="false" <?php if (strtolower(get_option('soundcloud_player_iframe')) === 'false') echo 'checked'; ?> />
296
+ <label for="player_iframe_false" style="margin-right: 1em;">Flash</label>
297
  </td>
298
  </tr>
299
 
301
  <th scope="row">Player Height for Tracks</th>
302
  <td>
303
  <input type="text" name="soundcloud_player_height" value="<?php echo get_option('soundcloud_player_height'); ?>" /> (no unit, or %)<br />
304
+ Leave blank to use the default.
305
  </td>
306
  </tr>
307
 
309
  <th scope="row">Player Height for Groups/Sets</th>
310
  <td>
311
  <input type="text" name="soundcloud_player_height_multi" value="<?php echo get_option('soundcloud_player_height_multi'); ?>" /> (no unit, or %)<br />
312
+ Leave blank to use the default.
313
  </td>
314
  </tr>
315
 
317
  <th scope="row">Player Width</th>
318
  <td>
319
  <input type="text" name="soundcloud_player_width" value="<?php echo get_option('soundcloud_player_width'); ?>" /> (no unit, or %)<br />
320
+ Leave blank to use the default.
321
  </td>
322
  </tr>
323
 
324
  <tr valign="top">
325
  <th scope="row">Current Default 'params'</th>
326
  <td>
327
+ <?php echo http_build_query(array_filter(array(
328
+ 'auto_play' => get_option('soundcloud_auto_play'),
329
+ 'show_comments' => get_option('soundcloud_show_comments'),
330
+ 'color' => get_option('soundcloud_color'),
331
+ 'theme_color' => get_option('soundcloud_theme_color'),
332
+ ))) ?>
333
  </td>
334
  </tr>
335