WordPress.org

Plugin Directory

Changeset 664493


Ignore:
Timestamp:
02/06/13 19:32:32 (15 months ago)
Author:
stephdau
Message:

Jetpack: SoundCloud handling: updating to latest version (2.3), simplified.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • jetpack/trunk/modules/shortcodes/soundcloud.php

    r357693 r664493  
    11<?php 
    2  
    32/* 
    43Plugin Name: SoundCloud Shortcode 
    5 Plugin URI: http://www.soundcloud.com 
    6 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 like that [soundcloud height="82" params="auto_play=true"]http.... 
    7 Version: 1.1.5 
    8 Author: Johannes Wagener <johannes@soundcloud.com> added to wpcom by tott 
    9 Author URI: http://johannes.wagener.cc 
    10  
    11 [soundcloud url="http://api.soundcloud.com/tracks/9408008"] 
    12 <object height="81" width="100%"> <param name="movie" value="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F8781356"></param> <param name="allowscriptaccess" value="always"></param> <embed allowscriptaccess="always" height="81" src="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F8781356" type="application/x-shockwave-flash" width="100%"></embed> </object>  <span><a href="http://soundcloud.com/robokopbeats/robokop-we-move-at-midnight-preview-forthcoming-on-mwm-recordings">Robokop - We move at midnight preview ( FORTHCOMING ON MWM recordings)</a> by <a href="http://soundcloud.com/robokopbeats">Robokop</a></span>  
     4Plugin URI: http://wordpress.org/extend/plugins/soundcloud-shortcode/ 
     5Description: Converts SoundCloud WordPress shortcodes to a SoundCloud widget. Example: [soundcloud]http://soundcloud.com/forss/flickermood[/soundcloud] 
     6Version: 2.3 
     7Author: SoundCloud Inc., simplified for Jetpack by Automattic, Inc. 
     8Author URI: http://soundcloud.com 
     9License: GPLv2 
     10 
     11Original version: Johannes Wagener <johannes@soundcloud.com> 
     12Options support: Tiffany Conroy <tiffany@soundcloud.com> 
     13HTML5 & oEmbed support: Tim Bormans <tim@soundcloud.com> 
    1314*/ 
    1415 
    15 add_filter( "pre_kses", "soundcloud_reverse_shortcode" ); 
    16  
    17 function soundcloud_reverse_shortcode_preg_replace_callback( $a ) { 
    18     $pattern = '/([a-zA-Z0-9\-_%=&]*)&?url=([^&]+)&?([a-zA-Z0-9\-_%&=]*)/'; 
    19     preg_match( $pattern, str_replace( "&amp;", "&", $a[3] ), $params ); 
    20  
    21     return '[soundcloud width="' . esc_attr( $a[2] ) . '" height="' . esc_attr( $a[1] ) . '" params="' . esc_attr( $params[1] . $params[3] ) . '" url="' . urldecode( $params[2] ) . '"]'; 
    22 } 
    23  
    24 function soundcloud_reverse_shortcode( $content ){ 
    25     if ( false === stripos( $content, 'http://player.soundcloud.com/player.swf' ) ) 
    26         return $content; 
    27  
    28     $pattern = '!<object\s*height="(\d+%?)"\s*width="(\d+%?)".*?src="http://.*?soundcloud\.com/player.swf\?([^"]+)".*?</object>.*?</span>!'; 
    29     $pattern_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $pattern, ENT_NOQUOTES ) );  
    30  
    31     if ( preg_match( $pattern_ent, $content ) ) 
    32         return( preg_replace_callback( $pattern_ent, 'soundcloud_reverse_shortcode_preg_replace_callback', $content ) ); 
    33     else 
    34         return( preg_replace_callback( $pattern, 'soundcloud_reverse_shortcode_preg_replace_callback', $content ) ); 
    35 } 
    36  
    37 add_shortcode( "soundcloud", "soundcloud_shortcode" ); 
    38  
    39 function soundcloud_shortcode( $atts, $url = '' ) { 
    40     if ( empty( $url ) ) 
    41         extract( shortcode_atts( array( 'url' => '', 'params' => '', 'height' => '', 'width' => '100%' ), $atts ) ); 
    42     else 
    43         extract( shortcode_atts( array( 'params' => '', 'height' => '', 'width' => '100%' ), $atts ) ); 
    44  
    45     $encoded_url = urlencode( $url ); 
    46     if ( $url = parse_url( $url ) ) { 
    47         $splitted_url = split( "/", $url['path'] ); 
    48         $media_type = $splitted_url[ count( $splitted_url ) - 2 ]; 
    49  
    50         if ( '' == $height ){ 
    51             if ( in_array( $media_type, array( 'groups', 'sets' ) ) ) 
    52                 $height = 225; 
    53             else 
    54                 $height = 81; 
    55         } 
    56         $player_params = "url=$encoded_url&g=1&$params"; 
    57  
    58         return '<object height="' . esc_attr( $height ) . '" width="' . esc_attr( $width ) . '"><param name="movie" value="' . esc_url( "http://player.soundcloud.com/player.swf?$player_params" ) . '"></param><embed height="' . esc_attr( $height ) . '" src="' . esc_url( "http://player.soundcloud.com/player.swf?$player_params" ) . '" type="application/x-shockwave-flash" width="' . esc_attr( $width ) . '"> </embed> </object>'; 
    59     } 
    60 } 
     16/* 
     17A8C: Taken from http://plugins.svn.wordpress.org/soundcloud-shortcode/trunk/ 
     18at revision 664386. 
     19 
     20Commenting out (instead of removing) and replacing code with custom modifs 
     21so it's eqsy to see what differs from the standard DOTORG version. 
     22 
     23All custom modifs are annoted with "A8C" keyword in comment. 
     24*/ 
     25 
     26/* Register oEmbed provider 
     27   -------------------------------------------------------------------------- */ 
     28 
     29wp_oembed_add_provider('#https?://(?:api\.)?soundcloud\.com/.*#i', 'http://soundcloud.com/oembed', true); 
     30 
     31 
     32/* Register SoundCloud shortcode 
     33   -------------------------------------------------------------------------- */ 
     34 
     35add_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 */ 
     45function 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  // A8C: default to iframe 
     99  $iframe = true; 
     100/* 
     101  // The "iframe" option must be true to load the iframe widget 
     102  $iframe = soundcloud_booleanize($options['iframe']) 
     103    // Default to flash widget for permalink urls (e.g. http://soundcloud.com/{username}) 
     104    // because HTML5 widget doesn’t support those yet 
     105    ? preg_match('/api.soundcloud.com/i', $options['url']) 
     106    : false; 
     107 */ 
     108 
     109  // Return html embed code 
     110  if ($iframe) { 
     111    return soundcloud_iframe_widget($options); 
     112  } else { 
     113    return soundcloud_flash_widget($options); 
     114  } 
     115 
     116} 
     117 
     118/** 
     119 * Plugin options getter 
     120 * @param  {string|array}  $option   Option name 
     121 * @param  {mixed}         $default  Default value 
     122 * @return {mixed}                   Option value 
     123 */ 
     124function soundcloud_get_option($option, $default = false) { 
     125  $value = get_option('soundcloud_' . $option); 
     126  return $value === '' ? $default : $value; 
     127} 
     128 
     129/** 
     130 * Booleanize a value 
     131 * @param  {boolean|string}  $value 
     132 * @return {boolean} 
     133 */ 
     134function soundcloud_booleanize($value) { 
     135  return is_bool($value) ? $value : $value === 'true' ? true : false; 
     136} 
     137 
     138/** 
     139 * Decide if a url has a tracklist 
     140 * @param  {string}   $url 
     141 * @return {boolean} 
     142 */ 
     143function soundcloud_url_has_tracklist($url) { 
     144  return preg_match('/^(.+?)\/(sets|groups|playlists)\/(.+?)$/', $url); 
     145} 
     146 
     147/** 
     148 * Parameterize url 
     149 * @param  {array}  $match  Matched regex 
     150 * @return {string}          Parameterized url 
     151 */ 
     152function soundcloud_oembed_params_callback($match) { 
     153  global $soundcloud_oembed_params; 
     154 
     155  // Convert URL to array 
     156  $url = parse_url(urldecode($match[1])); 
     157  // Convert URL query to array 
     158  parse_str($url['query'], $query_array); 
     159  // Build new query string 
     160  $query = http_build_query(array_merge($query_array, $soundcloud_oembed_params)); 
     161 
     162  return 'src="' . $url['scheme'] . '://' . $url['host'] . $url['path'] . '?' . $query; 
     163} 
     164 
     165/** 
     166 * Iframe widget embed code 
     167 * @param  {array}   $options  Parameters 
     168 * @return {string}            Iframe embed code 
     169 */ 
     170function soundcloud_iframe_widget($options) { 
     171 
     172  // Merge in "url" value 
     173  $options['params'] = array_merge(array( 
     174    'url' => $options['url'] 
     175  ), $options['params']); 
     176 
     177  // Build URL 
     178  $url = 'http://w.soundcloud.com/player?' . http_build_query($options['params']); 
     179  // Set default width if not defined 
     180  $width = isset($options['width']) && $options['width'] !== 0 ? $options['width'] : '100%'; 
     181  // Set default height if not defined 
     182  $height = isset($options['height']) && $options['height'] !== 0 ? $options['height'] : (soundcloud_url_has_tracklist($options['url']) ? '450' : '166'); 
     183 
     184  return sprintf('<iframe width="%s" height="%s" scrolling="no" frameborder="no" src="%s"></iframe>', $width, $height, $url); 
     185} 
     186 
     187/** 
     188 * Legacy Flash widget embed code 
     189 * @param  {array}   $options  Parameters 
     190 * @return {string}            Flash embed code 
     191 */ 
     192function soundcloud_flash_widget($options) { 
     193 
     194  // Merge in "url" value 
     195  $options['params'] = array_merge(array( 
     196    'url' => $options['url'] 
     197  ), $options['params']); 
     198 
     199  // Build URL 
     200  $url = 'http://player.soundcloud.com/player.swf?' . http_build_query($options['params']); 
     201  // Set default width if not defined 
     202  $width = isset($options['width']) && $options['width'] !== 0 ? $options['width'] : '100%'; 
     203  // Set default height if not defined 
     204  $height = isset($options['height']) && $options['height'] !== 0 ? $options['height'] : (soundcloud_url_has_tracklist($options['url']) ? '255' : '81'); 
     205 
     206  return preg_replace('/\s\s+/', "", sprintf('<object width="%s" height="%s"> 
     207                                <param name="movie" value="%s"></param> 
     208                                <param name="allowscriptaccess" value="always"></param> 
     209                                <embed width="%s" height="%s" src="%s" allowscriptaccess="always" type="application/x-shockwave-flash"></embed> 
     210                              </object>', $width, $height, $url, $width, $height, $url)); 
     211} 
     212 
     213 
     214 
     215/* Settings 
     216   -------------------------------------------------------------------------- */ 
     217 
     218/* A8C: no user-defined options, KISS */ 
Note: See TracChangeset for help on using the changeset viewer.