Example #1
0
/**
 * SoundCloud shortcode handler
 *
 * @param  string|array $atts        The attributes passed to the shortcode like [soundcloud attr1="value" /].
 *                                   Is an empty string when no arguments are given.
 * @param  string       $content     The content between non-self closing [soundcloud]...[/soundcloud] tags.
 *
 * @return string                  Widget embed code HTML
 */
function soundcloud_shortcode($atts, $content = null)
{
    // Custom shortcode options
    $shortcode_options = array_merge(array('url' => trim($content)), is_array($atts) ? $atts : array());
    // Turn shortcode option "param" (param=value&param2=value) into array
    $shortcode_params = array();
    if (isset($shortcode_options['params'])) {
        parse_str(html_entity_decode($shortcode_options['params']), $shortcode_params);
    }
    $shortcode_options['params'] = $shortcode_params;
    $player_type = soundcloud_get_option('player_type', 'visual');
    $isIframe = $player_type !== 'flash';
    $isVisual = !$player_type || $player_type === 'visual' || $shortcode_options['visual'];
    // User preference options
    $plugin_options = array_filter(array('iframe' => $isIframe, 'width' => soundcloud_get_option('player_width'), 'height' => soundcloud_url_has_tracklist($shortcode_options['url']) ? soundcloud_get_option('player_height_multi') : soundcloud_get_option('player_height'), 'params' => array_filter(array('auto_play' => soundcloud_get_option('auto_play'), 'show_comments' => soundcloud_get_option('show_comments'), 'color' => soundcloud_get_option('color'), 'visual' => $isVisual ? 'true' : 'false'))));
    // Needs to be an array
    if (!isset($plugin_options['params'])) {
        $plugin_options['params'] = array();
    }
    // plugin options < shortcode options
    $options = array_merge($plugin_options, $shortcode_options);
    // plugin params < shortcode params
    $options['params'] = array_merge($plugin_options['params'], $shortcode_options['params']);
    // The "url" option is required
    if (!isset($options['url'])) {
        return '';
    } else {
        $options['url'] = trim($options['url']);
    }
    // Both "width" and "height" need to be integers
    if (isset($options['width']) && !preg_match('/^\\d+$/', $options['width'])) {
        // set to 0 so oEmbed will use the default 100% and WordPress themes will leave it alone
        $options['width'] = 0;
    }
    if (isset($options['height']) && !preg_match('/^\\d+$/', $options['height'])) {
        unset($options['height']);
    }
    // The "iframe" option must be true to load the iframe widget
    $iframe = soundcloud_booleanize($options['iframe']);
    // Remove visual parameter from Flash widget, when it's false because that's the default, or when displaying the smallest player
    if ($options['params']['visual'] && (!$iframe || !soundcloud_booleanize($options['params']['visual']) || isset($options['height']) && '20' == $options['height'])) {
        unset($options['params']['visual']);
    }
    // Merge in "url" value
    $options['params'] = array_merge(array('url' => $options['url']), $options['params']);
    // Return html embed code
    if ($iframe) {
        return soundcloud_iframe_widget($options);
    } else {
        return soundcloud_flash_widget($options);
    }
}
/**
 * SoundCloud shortcode handler
 * @param  {string|array}  $atts     The attributes passed to the shortcode like [soundcloud attr1="value" /].
 *                                   Is an empty string when no arguments are given.
 * @param  {string}        $content  The content between non-self closing [soundcloud]…[/soundcloud] tags.
 * @return {string}                  Widget embed code HTML
 */
function soundcloud_shortcode($atts, $content = null)
{
    // Custom shortcode options
    $shortcode_options = array_merge(array('url' => trim($content)), is_array($atts) ? $atts : array());
    // Turn shortcode option "param" (param=value&param2=value) into array
    $shortcode_params = array();
    if (isset($shortcode_options['params'])) {
        parse_str(html_entity_decode($shortcode_options['params']), $shortcode_params);
    }
    $shortcode_options['params'] = $shortcode_params;
    // User preference options
    $plugin_options = array_filter(array('iframe' => soundcloud_get_option('player_iframe', true), 'width' => soundcloud_get_option('player_width'), 'height' => soundcloud_url_has_tracklist($shortcode_options['url']) ? soundcloud_get_option('player_height_multi') : soundcloud_get_option('player_height'), 'params' => array_filter(array('auto_play' => soundcloud_get_option('auto_play'), 'show_comments' => soundcloud_get_option('show_comments'), 'color' => soundcloud_get_option('color'), 'theme_color' => soundcloud_get_option('theme_color')))));
    // Needs to be an array
    if (!isset($plugin_options['params'])) {
        $plugin_options['params'] = array();
    }
    // plugin options < shortcode options
    $options = array_merge($plugin_options, $shortcode_options);
    // plugin params < shortcode params
    $options['params'] = array_merge($plugin_options['params'], $shortcode_options['params']);
    // The "url" option is required
    if (!isset($options['url'])) {
        return '';
    } else {
        $options['url'] = trim($options['url']);
    }
    // Both "width" and "height" need to be integers
    if (isset($options['width']) && !preg_match('/^\\d+$/', $options['width'])) {
        // set to 0 so oEmbed will use the default 100% and WordPress themes will leave it alone
        $options['width'] = 0;
    }
    if (isset($options['height']) && !preg_match('/^\\d+$/', $options['height'])) {
        unset($options['height']);
    }
    // The "iframe" option must be true to load the iframe widget
    $options['iframe'] = isset($options['iframe']) ? $options['iframe'] : '';
    $iframe = soundcloud_booleanize($options['iframe']) ? preg_match('/api.soundcloud.com/i', $options['url']) : false;
    // Return html embed code
    if ($iframe) {
        return soundcloud_iframe_widget($options);
    } else {
        return soundcloud_flash_widget($options);
    }
}