Skip to content
Snippets Groups Projects
Commit a909a670 authored by lechuck's avatar lechuck Committed by lechuck
Browse files

Upgrade soundcloud shortcode to 2.0

parent 70f49b92
No related branches found
No related tags found
No related merge requests found
=== SoundCloud Shortcode === === SoundCloud Shortcode ===
Contributors: jowagener, theophani Contributors: jowagener, theophani, por
Donate link: http://soundcloud.com Donate link: http://soundcloud.com
Tags: soundcloud, flash, player, shortcode, Tags: soundcloud, html5, flash, player, shortcode,
Requires at least: 2.5.0 Requires at least: 2.5.0
Tested up to: 3.0.4 Tested up to: 3.3.1
Stable tag: trunk Stable tag: trunk
The SoundCloud Shortcode plugin allows you to integrate a player widget from SoundCloud into your Wordpress Blog by using a Wordpress shortcodes. The SoundCloud Shortcode plugin allows you to integrate a player widget from SoundCloud into your Wordpress Blog by using a Wordpress shortcodes.
...@@ -29,11 +29,11 @@ Embed a track player which starts playing automaticly and won't show any comment ...@@ -29,11 +29,11 @@ Embed a track player which starts playing automaticly and won't show any comment
`[soundcloud params="color=33e040&theme_color=80e4a0"]http://soundcloud.com/forss/sets/live-4[/soundcloud]` `[soundcloud params="color=33e040&theme_color=80e4a0"]http://soundcloud.com/forss/sets/live-4[/soundcloud]`
Embeds a set player with a green theme. Embeds a set player with a green theme.
`[soundcloud height="150" width="250"]http://soundcloud.com/groups/experimental[/soundcloud]` `[soundcloud width="250"]http://soundcloud.com/groups/experimental[/soundcloud]`
Embeds a group player with 150px height and 250px width. Embeds a group player with 250px width.
When posting the standard SoundCloud embed code, the plugin tries to replace it with a shortcode. 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.
== Installation == == Installation ==
...@@ -43,9 +43,14 @@ When posting the standard SoundCloud embed code, the plugin tries to replace it ...@@ -43,9 +43,14 @@ When posting the standard SoundCloud embed code, the plugin tries to replace it
== Screenshots == == Screenshots ==
1. This is how the player looks. 1. This is how the Flash player looks, which is still available as an option. It is also the fallback for legacy URL formats.
2. This is how the default player looks, which uses HTML5.
== Changelog == == Changelog ==
= 2.0 =
* HTML5 Player added as the default player, with Flash as an option and fallback for legacy URL formats.
= 1.2.1 = = 1.2.1 =
* Removed flash fallback HTML * Removed flash fallback HTML
......
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
/* /*
Plugin Name: SoundCloud Shortcode Plugin Name: SoundCloud Shortcode
Plugin URI: http://www.soundcloud.com Plugin URI: http://www.soundcloud.com
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="82" params="auto_play=true"]http.... 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....
Version: 1.2.2 Version: 2.0
Author: Johannes Wagener <johannes@soundcloud.com> Author: Johannes Wagener <johannes@soundcloud.com>
Author URI: http://johannes.wagener.cc Author URI: http://johannes.wagener.cc
*/ */
...@@ -27,90 +27,176 @@ You should have received a copy of the GNU General Public License ...@@ -27,90 +27,176 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
add_filter( "pre_kses", "soundcloud_reverse_shortcode" ); add_shortcode( "soundcloud", "soundcloud_shortcode" );
function soundcloud_reverse_shortcode_preg_replace_callback( $a ) {
$pattern = '/([a-zA-Z0-9\-_%=&]*)&?url=([^&]+)&?([a-zA-Z0-9\-_%&=]*)/'; function soundcloud_shortcode($attributes, $content=null) {
preg_match( $pattern, str_replace( "&amp;", "&", $a[3] ), $params ); return SoundcloudShortcode::parse($attributes, $content);
return '[soundcloud width="' . esc_attr( $a[1] ) . '" height="' .
esc_attr( $a[2] ) . '" params="' . esc_attr( $params[1] . $params[3] ) .
'" url="' . urldecode( $params[2] ) . '"]';
}
function soundcloud_reverse_shortcode( $content ) {
$pattern = '/<object.*width="([\d]+%?)".*height="([\d]+%?)".*src="http:\/\/.*soundcloud\.com\/player.swf\?(.*)".*<\/object>( <span[^>]*>.*<\/span>|)/U';
$pattern_ent = htmlspecialchars( $pattern, ENT_NOQUOTES );
if ( preg_match( $pattern_ent, $content ) ) {
return preg_replace_callback( $pattern_ent, 'soundcloud_reverse_shortcode_preg_replace_callback', $content );
} else {
return preg_replace_callback( $pattern, 'soundcloud_reverse_shortcode_preg_replace_callback', $content );
}
} }
add_shortcode( "soundcloud", "soundcloud_shortcode" ); class SoundcloudShortcode {
function soundcloud_shortcode( $atts, $url='' ) {
if ( empty( $url ) ) { const IFRAME_HEIGHT = '166';
extract(shortcode_atts( array( const IFRAME_TRACKLIST_HEIGHT = '450';
'url' => '', const IFRAME_WIDTH = '100%';
'params' => soundcloud_build_params_string(),
'height' => '', const FLASH_HEIGHT = '81';
'width' => '' const FLASH_TRACKLIST_HEIGHT = '255';
), $atts ) ); const FLASH_WIDTH = '100%';
} else {
// set to true when we deprecate the flash player
const DEFAULT_TO_IFRAME = false;
public function parse($attributes, $content=null) {
extract(shortcode_atts(array( extract(shortcode_atts(array(
'params' => soundcloud_build_params_string(), 'url' => $content,
'iframe' => self::getDefaultIframePreference(),
'params' => self::getDefaultQuery(),
'height' => '', 'height' => '',
'width' => '' 'width' => ''
), $atts ) ); ), $attributes));
$iframe = self::booleanize($iframe);
// The HTML5 widget doesn't support http://soundcloud.com/<username>
// style urls yet. So we force the old Flash widget for now.
if (self::isLegacyURL($url)) {
$iframe = false;
} }
$encoded_url = urlencode( $url ); $type = self::getType($url);
$width = self::getWidth($width, $iframe, $type);
$height = self::getHeight($height, $iframe, $type);
if ( $url = parse_url( $url ) ){ return self::getHTML($url, $iframe, $params, $width, $height);
$splitted_url = split( "/", $url['path'] ); }
$media_type = $splitted_url[count($splitted_url) - 2];
public function getDefaultQuery() {
$options = array(
'auto_play',
'show_comments',
'color',
'theme_color'
);
$params = array();
foreach ($options as &$option) {
$value = get_option('soundcloud_' . $option, '');
if (!empty($value)) {
$params[$option] = $value;
}
}
return http_build_query($params);
}
if ( $height == '' ) { public function getDefaultIframePreference() {
if ( $media_type == "groups" || $media_type == "sets" ){ $pref = get_option('soundcloud_player_iframe');
$height = get_option('soundcloud_player_height_multi'); return ($pref === '') ? DEFAULT_TO_IFRAME : self::booleanize($pref);
if (!$height || $height == '') $height = '255'; }
private function booleanize($value) {
if ($value && strtolower($value) !== "false") {
return true;
} else { } else {
$height = get_option('soundcloud_player_height', '81'); return false;
if (!$height || $height == '') $height = '81';
} }
} }
if ( $width == '' ) { private function isLegacyURL($url) {
return !preg_match("/api.soundcloud.com/i", $url);
}
private function getWidth($width, $iframe, $type) {
if (empty($width)) {
$default = ($iframe) ? self::IFRAME_WIDTH : self::FLASH_WIDTH;
$width = get_option('soundcloud_player_width'); $width = get_option('soundcloud_player_width');
if (!$width || $width == '') $width = '100%'; $width = $width === '' ? $default : $width;
}
return $width;
} }
$player_params = "url=$encoded_url&g=1&$params"; private function getHeight($height, $iframe, $type) {
switch ($type) {
case 'groups':
case 'sets':
case 'playlists':
$default = ($iframe) ? self::IFRAME_TRACKLIST_HEIGHT : self::FLASH_TRACKLIST_HEIGHT;
$height = (empty($height)) ? get_option('soundcloud_player_height_multi') : $height;
$height = (empty($height)) ? $default : $height;
if ($iframe) {
$height = self::fixHeight($height, $default);
}
break;
default:
$default = ($iframe) ? self::IFRAME_HEIGHT : self::FLASH_HEIGHT;
$height = (empty($height)) ? get_option('soundcloud_player_height') : $height;
$height = (empty($height)) ? $default : $height;
if ($iframe) {
$height = self::fixHeight($height, $default);
}
// sounds can only be default height
//$height = ($iframe) ? self::IFRAME_HEIGHT : self::FLASH_HEIGHT;
break;
}
return $height;
}
preg_match('/(.+\.)?(((staging|sandbox)-)?soundcloud\.com)/', $url['host'], $matches); private function fixHeight($height, $min_height) {
$player_host = "player." . $matches[2]; if (!preg_match("/[0-9]+%/", $height) && intval($height) < $min_height) {
$height = $min_height;
}
return $height;
}
return "<object height=\"" . esc_attr( $height ) . "\" width=\"" . private function getType($url) {
esc_attr( $width ) . "\"><param name=\"movie\" value=\"http://" . if (empty($url)) {
esc_attr( $player_host ) . "/player.swf?" . esc_attr( $player_params ) . return false;
"\"></param><param name=\"allowscriptaccess\" value=\"always\"></param>" . }
"<embed allowscriptaccess=\"always\" height=\"" . if ($url = parse_url($url)) {
esc_attr( $height ) . "\" src=\"http://" . esc_attr( $player_host ) . $splitted_url = split( "/", $url['path'] );
"/player.swf?" . esc_attr( $player_params ) . $media_type = $splitted_url[count($splitted_url) - 2];
"\" type=\"application/x-shockwave-flash\" width=\"" . return $media_type;
esc_attr( $width ) . "\"> </embed></object>";
} }
} }
private function getHTML($url, $iframe, $params, $width, $height) {
$encoded_url = urlencode($url);
$parsed_url = parse_url($url);
if ($iframe) {
$player_host = 'w.soundcloud.com';
$player_params = 'url=' . $encoded_url . '&' . $params;
$player_src = 'http://' . $player_host . '/player/?' . $player_params;
} else {
$player_host = preg_replace('/(.+\.)?(((staging|sandbox)-)?soundcloud\.com)/', 'player.$2', $parsed_url['host']);
$player_params = 'url=' . $encoded_url . '&g=1&' . $params;
$player_src = 'http://' . $player_host . '/player.swf?' . $player_params;
}
$width = esc_attr($width);
$height = esc_attr($height);
$player_src = esc_attr($player_src);
if ($iframe) {
$html = '<iframe width="' . $width . '" height="' . $height . '" scrolling="no" frameborder="no" src="' . $player_src . '"></iframe>';
} else {
$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>';
}
return $html;
}
}
// Add settings link on plugin page // Add settings link on plugin page
add_filter("plugin_action_links_".plugin_basename(__FILE__), 'soundcloud_settings_link' );
function soundcloud_settings_link($links) { function soundcloud_settings_link($links) {
$settings_link = '<a href="options-general.php?page=soundcloud-shortcode">Settings</a>'; $settings_link = '<a href="options-general.php?page=soundcloud-shortcode">Settings</a>';
array_unshift($links, $settings_link); array_unshift($links, $settings_link);
return $links; return $links;
} }
add_filter("plugin_action_links_".plugin_basename(__FILE__), 'soundcloud_settings_link' );
// Add admin menu
add_action('admin_menu', 'soundcloud_shortcode_options_menu'); add_action('admin_menu', 'soundcloud_shortcode_options_menu');
function soundcloud_shortcode_options_menu() { function soundcloud_shortcode_options_menu() {
add_options_page('SoundCloud Options', 'SoundCloud', 'manage_options', 'soundcloud-shortcode', 'soundcloud_shortcode_options'); add_options_page('SoundCloud Options', 'SoundCloud', 'manage_options', 'soundcloud-shortcode', 'soundcloud_shortcode_options');
add_action( 'admin_init', 'register_soundcloud_settings' ); add_action( 'admin_init', 'register_soundcloud_settings' );
...@@ -121,20 +207,13 @@ function register_soundcloud_settings() { ...@@ -121,20 +207,13 @@ function register_soundcloud_settings() {
register_setting( 'soundcloud-settings', 'soundcloud_player_height' ); register_setting( 'soundcloud-settings', 'soundcloud_player_height' );
register_setting( 'soundcloud-settings', 'soundcloud_player_height_multi' ); register_setting( 'soundcloud-settings', 'soundcloud_player_height_multi' );
register_setting( 'soundcloud-settings', 'soundcloud_player_width ' ); register_setting( 'soundcloud-settings', 'soundcloud_player_width ' );
register_setting( 'soundcloud-settings', 'soundcloud_player_iframe' );
register_setting( 'soundcloud-settings', 'soundcloud_auto_play' ); register_setting( 'soundcloud-settings', 'soundcloud_auto_play' );
register_setting( 'soundcloud-settings', 'soundcloud_show_comments ' ); register_setting( 'soundcloud-settings', 'soundcloud_show_comments ' );
register_setting( 'soundcloud-settings', 'soundcloud_color' ); register_setting( 'soundcloud-settings', 'soundcloud_color' );
register_setting( 'soundcloud-settings', 'soundcloud_theme_color' ); register_setting( 'soundcloud-settings', 'soundcloud_theme_color' );
} }
function soundcloud_build_params_string() {
$params = '';
$params .= 'auto_play=' . get_option( 'soundcloud_auto_play', '' );
$params .= '&show_comments=' . get_option( 'soundcloud_show_comments ', '' );
$params .= '&color=' . get_option( 'soundcloud_color', '' );
$params .= '&theme_color=' . get_option( 'soundcloud_theme_color', '' );
return $params;
}
function soundcloud_shortcode_options() { function soundcloud_shortcode_options() {
if (!current_user_can('manage_options')) { if (!current_user_can('manage_options')) {
...@@ -151,6 +230,14 @@ function soundcloud_shortcode_options() { ...@@ -151,6 +230,14 @@ function soundcloud_shortcode_options() {
<?php settings_fields( 'soundcloud-settings' ); ?> <?php settings_fields( 'soundcloud-settings' ); ?>
<table class="form-table"> <table class="form-table">
<tr valign="top">
<th scope="row">Widget Type</th>
<td>
<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>
<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>
</td>
</tr>
<tr valign="top"> <tr valign="top">
<th scope="row">Player Height for Tracks</th> <th scope="row">Player Height for Tracks</th>
<td> <td>
...@@ -175,11 +262,10 @@ function soundcloud_shortcode_options() { ...@@ -175,11 +262,10 @@ function soundcloud_shortcode_options() {
</td> </td>
</tr> </tr>
<tr valign="top"> <tr valign="top">
<th scope="row">Current Default 'params'</th> <th scope="row">Current Default 'params'</th>
<td> <td>
<?php echo soundcloud_build_params_string(); ?> <?php echo SoundcloudShortcode::getDefaultQuery(); ?>
</td> </td>
</tr> </tr>
...@@ -198,6 +284,7 @@ function soundcloud_shortcode_options() { ...@@ -198,6 +284,7 @@ function soundcloud_shortcode_options() {
<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> <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>
<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> <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>
<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> <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>
</td>
</tr> </tr>
<tr valign="top"> <tr valign="top">
...@@ -225,7 +312,5 @@ function soundcloud_shortcode_options() { ...@@ -225,7 +312,5 @@ function soundcloud_shortcode_options() {
</form> </form>
</div> </div>
<?php <?php
} }
?> ?>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment