Skip to content

Instantly share code, notes, and snippets.

@walidvb
Created February 11, 2014 00:21
Show Gist options
  • Save walidvb/8927000 to your computer and use it in GitHub Desktop.
Save walidvb/8927000 to your computer and use it in GitHub Desktop.
Adds visual and height manipulation to the html5 player
/*
* Modified on 11.02.2014 by walidvb
* to add a 'visual' checkbox, added by the soundcloud API
*
*
*
*/
<?php
/**
* @file
* SoundCloud filter is a module to embed a SoundCloud track in your site using a simple tag
*/
/**
* Implementation of hook_filter_info().
*/
function soundcloud_filter_filter_info() {
$filters = array();
$filters['soundcloud_filter'] = array(
'title' => t('SoundCloud filter'),
'description' => t('Replaces [soundcloud:URL] with embedded code'),
'settings callback' => '_soundcloud_filter_settings',
'default settings' => array(
'soundcloud_filter_width' => '100',
'soundcloud_filter_height' => '81',
'soundcloud_filter_setheight' => '305',
'soundcloud_filter_showcomments' => FALSE,
'soundcloud_filter_autoplay' => FALSE,
'soundcloud_filter_showplaycount' => FALSE,
'soundcloud_filter_showartwork' => FALSE,
'soundcloud_filter_color' => 'ff7700',
'soundcloud_filter_html5' => FALSE,
'soundcloud_filter_visual' => FALSE,
),
'process callback' => '_soundcloud_filter_process',
'cache' => 'FALSE',
'tips callback' => '_soundcloud_filter_tips',
'weight' => -1,
);
return $filters;
}
/**
* Implementation of hook_filter_tips().
*/
function _soundcloud_filter_filter_tips($filter, $format, $long = FALSE) {
if ($long) {
return t('
<p><strong>SoundCloud filter</strong></p>
<p>Insert SoundCloud track or set with <code>[soundcloud:URL]</code> tag.</p>
<p>Optional parameters can be set to override default player settings. Examples: </p>
<ul>
<li>override default width and height: <code>[soundcloud:http://soundcloud.com/blumarten/blu-mar-ten-she-moves-through-asc-remix width=100 height=81]</code></li>
<li>override default showcomments setting: <code>[soundcloud:http://soundcloud.com/blumarten/blu-mar-ten-she-moves-through-asc-remix showcomments=false]</code></li>
<li>override default autoplay setting: <code>[soundcloud:http://soundcloud.com/blumarten/blu-mar-ten-she-moves-through-asc-remix autoplay=true]</code></li>
<li>override default player color: <code>[soundcloud:http://soundcloud.com/blumarten/blu-mar-ten-she-moves-through-asc-remix color=ff7700]</code></li>
</ul>
');
} else {
return t('Insert SoundCloud track or set with the [soundcloud:URL] tag.');
}
}
/*
* Settings callback
*/
function _soundcloud_filter_settings($form, &$form_state, $filter, $format, $defaults, $filters) {
$settings['soundcloud_filter_html5'] = array(
'#type' => 'checkbox',
'#title' => t('Use HTML5 player'),
'#default_value' => isset($filter->settings['soundcloud_filter_html5']) ? $filter->settings['soundcloud_filter_html5'] : $defaults['soundcloud_filter_html5'],
'#description' => t('Use HTML5 player instead of flash player.'),
);
$settings['soundcloud_filter_width'] = array(
'#type' => 'textfield',
'#title' => t('Default width'),
'#default_value' => isset($filter->settings['soundcloud_filter_width']) ? $filter->settings['soundcloud_filter_width'] : $defaults['soundcloud_filter_width'],
'#maxlength' => 3,
'#description' => t('Player width. Default is @width.', array('@width' => $defaults['soundcloud_filter_width'])),
);
$settings['soundcloud_filter_height'] = array(
'#type' => 'textfield',
'#title' => t('Default height for tracks'),
'#default_value' => isset($filter->settings['soundcloud_filter_height']) ? $filter->settings['soundcloud_filter_height'] : $defaults['soundcloud_filter_height'],
'#maxlength' => 3,
'#description' => t('Player height for tracks. Default is @height.', array('@height' => $defaults['soundcloud_filter_height'])),
);
$settings['soundcloud_filter_setheight'] = array(
'#type' => 'textfield',
'#title' => t('Default height for sets'),
'#default_value' => isset($filter->settings['soundcloud_filter_setheight']) ? $filter->settings['soundcloud_filter_setheight'] : $defaults['soundcloud_filter_setheight'],
'#maxlength' => 3,
'#description' => t('Player height for sets. Default is @height.', array('@height' => $defaults['soundcloud_filter_setheight'])),
);
$settings['soundcloud_filter_showcomments'] = array(
'#type' => 'checkbox',
'#title' => t('Show comments'),
'#default_value' => isset($filter->settings['soundcloud_filter_showcomments']) ? $filter->settings['soundcloud_filter_showcomments'] : $defaults['soundcloud_filter_showcomments'],
'#description' => t('Default show comment setting.'),
);
$settings['soundcloud_filter_autoplay'] = array(
'#type' => 'checkbox',
'#title' => t('Autoplay'),
'#default_value' => isset($filter->settings['soundcloud_filter_autoplay']) ? $filter->settings['soundcloud_filter_autoplay'] : $defaults['soundcloud_filter_autoplay'],
'#description' => t('Default autoplay setting.'),
);
$settings['soundcloud_filter_showplaycount'] = array(
'#type' => 'checkbox',
'#title' => t('Show playcount'),
'#default_value' => isset($filter->settings['soundcloud_filter_showplaycount']) ? $filter->settings['soundcloud_filter_showplaycount'] : $defaults['soundcloud_filter_showplaycount'],
'#description' => t('Default show playcount setting.'),
);
$settings['soundcloud_filter_showartwork'] = array(
'#type' => 'checkbox',
'#title' => t('Show artwork'),
'#default_value' => isset($filter->settings['soundcloud_filter_showartwork']) ? $filter->settings['soundcloud_filter_showartwork'] : $defaults['soundcloud_filter_showartwork'],
'#description' => t('Default show artwork setting.'),
);
$settings['soundcloud_filter_visual'] = array(
'#type' => 'checkbox',
'#title' => t('Show visual'),
'#default_value' => isset($filter->settings['soundcloud_filter_visual']) ? $filter->settings['soundcloud_filter_visual'] : $defaults['soundcloud_filter_visual'],
'#description' => t('Default show visual setting.'),
);
$settings['soundcloud_filter_color'] = array(
'#type' => module_exists('jquery_colorpicker') ? 'jquery_colorpicker' : 'textfield',
'#title' => t('Default color'),
'#description' => t('Please enter a color value in the form RRGGBB. Turn on the jQuery Colorpicker module if available. '),
'#default_value' => isset($filter->settings['soundcloud_filter_color']) ? $filter->settings['soundcloud_filter_color'] : $defaults['soundcloud_filter_color'],
'#maxlength' => 6,
);
return $settings;
}
/*
* Process callback
*/
function _soundcloud_filter_process($text, $filter, $format, $langcode, $cache, $cache_id) {
if (preg_match_all('/\[soundcloud(\:(.+))?( .+)?\]/isU', $text, $matches)) {
$r = array();
foreach ($matches[0] as $c => $code) {
$sound = array(
'url' => $matches[2][$c],
'width' => $filter->settings['soundcloud_filter_width'],
'height' => $filter->settings['soundcloud_filter_height'],
'setheight' => $filter->settings['soundcloud_filter_setheight'],
'showcomments' => ($filter->settings['soundcloud_filter_showcomments']) ? 'true' : 'false',
'autoplay' => ($filter->settings['soundcloud_filter_autoplay']) ? 'true' : 'false',
'showplaycount' => ($filter->settings['soundcloud_filter_showplaycount']) ? 'true' : 'false',
'showartwork' => ($filter->settings['soundcloud_filter_showartwork']) ? 'true' : 'false',
'color' => $filter->settings['soundcloud_filter_color'],
'html5' => $filter->settings['soundcloud_filter_html5'],
'visual' => $filter->settings['soundcloud_filter_visual'] ? 'true' : 'false'
);
$encoded_url = urlencode($sound['url']);
$parsed_url = parse_url($sound['url']);
if ($matches[3][$c] && preg_match_all('/\s+([a-z]+)\=([^\s]+)/i', $matches[3][$c], $override_params)) {
foreach ($override_params[0] as $p => $par) {
$sound[$override_params[1][$p]] = $override_params[2][$p];
}
}
// Check that the user has permission to use autoplay
if (!user_access('use soundcloud filter autoplay')) {
$sound['autoplay'] = 'false';
}
switch ($sound['html5']) {
case 0:
$splitted_url = explode("/", $parsed_url['path']);
if ($splitted_url[2] != 'sets') {
$params = 'url=' . $encoded_url .
'&amp;show_comments=' . $sound['showcomments'] .
'&amp;auto_play=' . $sound['autoplay'] .
'&amp;color=' . $sound['color'];
$r = theme('soundcloud_filter_embed', array('sound' => $sound, 'params' => $params));
}
else {
$params = 'url=' . $encoded_url .
'&amp;show_comments=' . $sound['showcomments'] .
'&amp;auto_play=' . $sound['autoplay'] .
'&amp;show_playcount=' . $sound['showplaycount'] .
'&amp;show_artwork=' . $sound['showartwork'] .
'&amp;color=' . $sound['color'].
'&amp;visual=' . $sound['visual'];
$r = theme('soundcloud_filter_embed_set', array('sound' => $sound, 'params' => $params));
}
break;
case 1:
$r = theme('soundcloud_filter_embed_html5', array('sound' => $sound));
break;
}
$text = str_replace($code, $r, $text);
}
}
return $text;
}
/**
* Function that outputs the <object> element for tracks
*/
function theme_soundcloud_filter_embed($variables) {
$output = '';
$soundcloud = $variables['sound'];
$params = isset($variables['params']) ? $variables['params'] : array();
$parsed_url = parse_url($soundcloud['url']);
$output .= '<object height="' . $soundcloud['height'] . '" width="' . $soundcloud['width'] . '%"><param name="movie" value="http://player.' . $parsed_url['host'] . '/player.swf?' . $params . '"></param>';
$output .= '<param name="allowscriptaccess" value="always"></param><embed allowscriptaccess="always" height="' . $soundcloud['height'] . '" src="http://player.' . $parsed_url['host'] . '/player.swf?' . $params . '" type="application/x-shockwave-flash" width="' . $soundcloud['width'] . '%"></embed></object>';
return $output;
}
/**
* Function that outputs the <object> element for sets
*/
function theme_soundcloud_filter_embed_set($variables) {
$output = '';
$soundcloud = $variables['sound'];
$params = isset($variables['params']) ? $variables['params'] : array();
$parsed_url = parse_url($soundcloud['url']);
$output .= '<object height="' . $soundcloud['setheight'] . '" width="' . $soundcloud['width'] . '%"><param name="movie" value="https://player.' . $parsed_url['host'] . '/player.swf?' . $params . '"></param>';
$output .= '<param name="allowscriptaccess" value="always"></param><embed allowscriptaccess="always" height="' . $soundcloud['setheight'] . '" src="https://player.' . $parsed_url['host'] . '/player.swf?' . $params . '" type="application/x-shockwave-flash" width="' . $soundcloud['width'] . '%"></embed></object>';
return $output;
}
/**
* Function that outputs the HTML5 player
*/
function theme_soundcloud_filter_embed_html5($variables) {
$output = '';
$soundcloud = $variables['sound'];
$oembed_endpoint = 'http://soundcloud.com/oembed';
$encoded_url = urlencode($soundcloud['url']);
dpm($variables, 'soundcloud vars');
$oembed_url = $oembed_endpoint . '?iframe=true&url=' . ($encoded_url);
$oembed = simplexml_load_string(soundcloud_filter_curl_get($oembed_url));
// Replace player default settings with our settings
$final_iframe = preg_replace('/(width=)"([^"]+)"/', 'width="' . $soundcloud['width'] . '%"', $oembed->html);
// Replace player default settings with our settings
$final_iframe = preg_replace('/(height=)"([^"]+)"/', 'height="' . $soundcloud['height'] . '"', $final_iframe);
if (preg_match('/auto_play=(true|false)/', $final_iframe)) {
$final_iframe = preg_replace('/auto_play=(true|false)/', 'auto_play=' . $soundcloud['autoplay'], $final_iframe);
}
else {
$final_iframe = preg_replace('/">/', '&auto_play=' . $soundcloud['autoplay'] . '">', $final_iframe);
}
if (preg_match('/show_artwork=(true|false)/', $final_iframe)) {
$final_iframe = preg_replace('/show_artwork=(true|false)/', 'show_artwork=' . $soundcloud['showartwork'], $final_iframe);
}
else {
$final_iframe = preg_replace('/">/', '&show_artwork=' . $soundcloud['showartwork'] . '">', $final_iframe);
}
if (preg_match('/color=([a-zA-Z0-9]{6})/', $final_iframe)) {
$final_iframe = preg_replace('/color=([a-zA-Z0-9]{6})/', 'color=' . $soundcloud['color'], $final_iframe);
}
else {
$final_iframe = preg_replace('/">/', '&color=' . $soundcloud['color'] . '">', $final_iframe);
}
//Add visual
if (preg_match('/visual=(true|false)/', $final_iframe)) {
$final_iframe = preg_replace('/visual=(true|false)/', 'visual=' . $soundcloud['visual'], $final_iframe);
}
else {
$final_iframe = preg_replace('/">/', '&visual=' . $soundcloud['visual'] . '">', $final_iframe);
}
dpm($final_iframe, 'final_iframe');
$output = html_entity_decode($final_iframe); // '$oembed->html' for original embed
dpm($output, 'entity_decoded');
return $output;
}
/**
* Implementation of hook_theme().
*/
function soundcloud_filter_theme($existing, $type, $theme, $path) {
return array(
'soundcloud_filter_embed' => array(
'variables' => array(
'sound' => NULL,
'params' => NULL,
),
),
'soundcloud_filter_embed_set' => array(
'variables' => array(
'sound' => NULL,
'params' => NULL,
),
),
'soundcloud_filter_embed_html5' => array(
'variables' => array(
'sound' => NULL,
),
),
'soundcloud_filter_wysiwyg_popup' => array(
'variables' => array(
'path' => NULL,
'tinymce_path' => NULL,
'tinymce_js' => NULL,
),
'template' => 'soundcloud-filter-wysiwyg-popup',
'path' => drupal_get_path('module', 'soundcloud_filter') . '/theme',
),
);
}
/**
* Implementation of hook_menu().
*
* @return An array of menu items.
*/
function soundcloud_filter_menu() {
$items = array();
$items['soundcloud-filter/wysiwyg'] = array(
'title' => 'SoundCloud filter',
'description' => 'WYSIWYG Popup',
'page callback' => 'soundcloud_filter_wysiwyg_popup',
'access arguments' => array('access soundcloud filter wysiwyg popup'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implementation of hook_permission().
*
* @return An array of valid permissions for the soundcloud_filter module
*/
function soundcloud_filter_permission() {
return array(
'access soundcloud filter wysiwyg popup' => array(
'title' => t('Access SoundCloud filter WYSIWYG popup'),
'description' => t('Allow users to access SoundCloud filter WYSIWYG popup window'),
),
'use soundcloud filter autoplay' => array(
'title' => t('Use SoundCloud filter autoplay setting'),
'description' => t('Allow users to use the autoplay setting'),
),
);
}
/**
* Load the WYSIWYG popup window.
*/
function soundcloud_filter_wysiwyg_popup() {
$path = base_path() . drupal_get_path('module', 'soundcloud_filter');
$tinymce_path = base_path() .'sites/all/libraries';
$tinymce_js = $tinymce_path .'/tinymce/jscripts/tiny_mce/tiny_mce_popup.js';
print theme('soundcloud_filter_wysiwyg_popup', array(
'path' => $path,
'tinymce_path' => $tinymce_path,
'tinymce_js' => $tinymce_js,
'form' => render(drupal_get_form('_soundcloud_filter_form'))
));
exit();
}
function _soundcloud_filter_form() {
$form['#action'] = '#';
$form['#id'] = 'soundcloud_filter_popup_form';
$form['#prefix'] = '<div id="soundcloud_filter_popup">';
$form['#suffix'] = '</div>';
$form['#attributes'] = array(
'onsubmit' => array('insertSoundCloudFilterCode(); return false;'),
);
$form['soundcloud_filter'] = array(
'#type' => 'fieldset',
'#title' => t('Insert/Edit SoundCloud'),
'#weight' => 0,
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#attributes' => array('class' => array('clearfix')),
);
$form['soundcloud_filter']['soundcloud_filter_url'] = array(
'#type' => 'textfield',
'#title' => t('URL'),
'#maxlength' => 255,
'#size' => 48,
'#default_value' => '',
'#weight' => 1,
);
$form['soundcloud_filter']['soundcloud_filter_width'] = array(
'#type' => 'textfield',
'#title' => t('Width'),
'#maxlength' => 3,
'#size' => 8,
'#default_value' => '',
'#weight' => 2,
);
$form['soundcloud_filter']['soundcloud_filter_height'] = array(
'#type' => 'textfield',
'#title' => t('Height'),
'#maxlength' => 3,
'#size' => 8,
'#default_value' => '',
'#weight' => 3,
);
$form['soundcloud_filter']['soundcloud_filter_setheight'] = array(
'#type' => 'textfield',
'#title' => t('Height for sets'),
'#maxlength' => 3,
'#size' => 8,
'#default_value' => '',
'#weight' => 4,
);
$form['soundcloud_filter']['soundcloud_filter_showcomments'] = array(
'#type' => 'checkbox',
'#title' => t('Show comments'),
'#weight' => 5,
);
$form['soundcloud_filter']['soundcloud_filter_autoplay'] = array(
'#type' => 'checkbox',
'#title' => t('Autoplay'),
'#weight' => 6,
);
$form['soundcloud_filter']['soundcloud_filter_showplaycount'] = array(
'#type' => 'checkbox',
'#title' => t('Show play count'),
'#weight' => 7,
);
$form['soundcloud_filter']['soundcloud_filter_showartwork'] = array(
'#type' => 'checkbox',
'#title' => t('Show artwork'),
'#weight' => 8,
);
$form['soundcloud_filter']['soundcloud_filter_visual'] = array(
'#type' => 'checkbox',
'#title' => t('Visual'),
'#weight' => 9,
);
$form['soundcloud_filter']['soundcloud_filter_color'] = array(
'#type' => module_exists('jquery_colorpicker') ? 'jquery_colorpicker' : 'textfield',
'#title' => t('Color'),
'#weight' => 10,
);
$form['insert'] = array(
'#type' => 'submit',
'#id' => 'insert',
'#value' => t('Insert'),
'#weight' => 98,
);
$form['cancel'] = array(
'#type' => 'button',
'#id' => 'cancel',
'#value' => t('Cancel'),
'#attributes' => array(
'onclick' => array('tinyMCEPopup.close();'),
),
'#weight' => 99,
);
return $form;
}
/**
* Implementation of hook_wysiwyg_plugin().
*/
function soundcloud_filter_wysiwyg_plugin($editor, $version) {
switch ($editor) {
case 'tinymce':
if ($version > 3) {
return array(
'soundcloud_filter' => array(
'path' => drupal_get_path('module', 'soundcloud_filter') .'/wysiwyg/tinymce',
'filename' => 'editor_plugin.js',
'buttons' => array('soundcloud_filter' => t('SoundCloud filter')),
'url' => 'http://drupal.org/project/soundcloud_filter',
'load' => TRUE,
),
);
}
break;
}
}
/*
* Curl helper function
*/
function soundcloud_filter_curl_get($url) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$return = curl_exec($curl);
curl_close($curl);
return $return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment