]*\s*href\s*=\s*["\']([^"\']*)\.(ogg|oga|flac|mp3|wav)["\']\s*>([^<>]*)<\s*/\s*a\s*>#');
/**
* Replaces links to audio files with audiobar controls
* @param string $content The post content
* @return string Modified post content
*/
function audiobar_replace_audio_links($content) {
return preg_replace_callback(AUDIOBAR_REGEX_AUDIO_FILE_LINKS, 'audiobar_callback', $content);
}
add_action('the_content', 'audiobar_replace_audio_links');
/**
* Used to make a note of the first replaced link
*/
$audiobar_first_page = null;
$audiobar_first_title = null;
$audiobar_first_extensions = array();
/**
* Callback function for audiobar_replace_audio_links, replaces a single link
* @param array $matches Matches array as passed by preg_replace_callback
* @return string Replacement
*/
function audiobar_callback( $matches ) {
global $audiobar_first_base, $audiobar_first_title, $audiobar_first_extensions;
// Extract information
$base = substr( $matches[1], strlen(get_bloginfo('wpurl')));
$linked = $matches[3];
$extensions = array();
$possibleExtensions = array( 'ogg','oga','flac','wav','mp3' );
foreach ( $possibleExtensions as $extension ) {
if ( file_exists( ABSPATH . $base . '.' . $extension ) ) {
$extensions[] = $extension;
}
}
$title = audiobar_get_title( $base, $extensions, $linked );
if ($audiobar_first_base === null) {
$audiobar_first_base = $base;
$audiobar_first_title = $title;
$audiobar_first_extensions = $extensions;
}
return audiobar_get_buttons( $base, $title, $extensions );
}
/**
* Retrieves the buttons template and returns its markup
*
* @param string $base Relative path of the audio file, excluding extension
* @param string $title Track title
* @param array $extensions Extensions of available file formats for this track
* @return string Markup of audio bar buttons for one track
*/
function audiobar_get_buttons( $base, $title, $extensions ) {
// MP3 is required to play files in the browser.
$show_play_button = in_array('mp3', $extensions);
ob_start();
include(audiobar_get_template('audiobar-buttons.php'));
$buttons = ob_get_contents();
ob_end_clean();
return $buttons;
}
/**
* Retrieves the title meta tag for an audio file designated by a relative path
*
* @param string $relative Relative path of audio file
* @return string Track title from metadata
*/
function audiobar_get_title( $base, $extensions, $fallback ) {
// Retrieve track title from audio file meta tags
$title = false;
foreach ($extensions as $extension) {
$abspath = ABSPATH.$base.'.'.$extension;
if (!file_exists($abspath)) {
continue;
}
if ( 'ogg' == $extension || 'oga' == $extension ) {
include_once(ABSPATH.'/wp-content/plugins/audiobar/lib/classAudioFile.php');
$af = new AudioFile();
$af->wave_filename = $abspath;
$af->ogginfo();
$title = $af->vorbis_comment->TITLE;
if (is_array($title)) {
$title = $title[0];
}
} elseif ( 'flac' == $extension ) {
include_once(ABSPATH.'/wp-content/plugins/audiobar/lib/flacTags.php');
$ft = new flacTags($abspath);
$ft->readTags();
$title = $ft->getComment('TITLE');
if (is_array($title)) {
$title = $title[0];
}
} elseif ( 'mp3' == $extension ) {
include_once(ABSPATH.'/wp-content/plugins/audiobar/lib/classAudioFile.php');
$af = new AudioFile();
$af->wave_filename = $abspath;
$af->mp3info();
$title = $af->id3_title;
}
if ($title !== false && $title != '') {
return $title;
}
}
return $fallback;
}
/**
* Redirects the user to the audiobar wrapper
* @param string $url Original home URL
* @return string Modified home URL
*/
function audiobar_page_js() {
$url = get_bloginfo('url');
$slug = str_replace($url, '', get_permalink());
if ( false === strpos( $slug, AUDIOBAR_FRAMEPARAMETER ) ) {
$url .= '/#' . $slug;
}
include(audiobar_get_template('audiobar-page-js.php'));
}
add_action('wp_footer', 'audiobar_page_js');
/**
* Finds an audiobar template file either in the current theme directory
* or the audiobar template directory.
* @param string $filename File name of the template
* @return string $template Full path of the template
*/
function audiobar_get_template($filename) {
$template = locate_template( array( $filename ));
if ( '' == $template ) {
$template = ABSPATH . '/wp-content/plugins/audiobar/templates/'.$filename;
}
return $template;
}
define('AUDIOBAR_HTACCESS_RULES', '
Header set Content-Disposition attachment
');
/**
* Automatically configures OGG, OGA, MP3, FLAC, WAV files as downloads
* on Apache servers on plugin activation, or displays a notice.
*
* This function based on save_mod_rewrite_rules() in wp-admin/includes/misc.php
* of the core Wordpress installation.
*/
function add_htaccess_rules($reverse = false) {
if ( is_multisite() )
return;
global $is_apache;
if (!$is_apache) {
return;
}
$home_path = get_home_path();
$htaccess_file = $home_path.'.htaccess';
if ((!file_exists($htaccess_file) && is_writable($home_path)) || is_writable($htaccess_file)) {
$rules = $reverse ? array() : explode("\n", AUDIOBAR_HTACCESS_RULES);
insert_with_markers( $htaccess_file, 'Audiobar', $rules);
}
}
/**
* Initialize the plugin on activation
*/
function audiobar_activation() {
include(ABSPATH.'/wp-content/plugins/audiobar/audiobar-default-settings.php');
foreach($audiobar_default_colors as $key => $value) {
add_option($key, $value, '', 'yes');
}
add_option('audiobar_disable_backlink', 0, '', 'yes');
add_option('audiobar_position', 'top', '', 'yes');
add_htaccess_rules();
}
register_activation_hook(__FILE__, 'audiobar_activation');
/**
* Remove plugin data on deactivation
*/
function audiobar_deactivation() {
add_htaccess_rules(true);
}
register_deactivation_hook(__FILE__, 'audiobar_deactivation');
/**
* Admin Menu
*/
function audiobar_admin_menu() {
add_options_page('Audiobar', 'Audiobar', 'administrator', 'audiobar', 'audiobar_admin');
}
if ( is_admin() ){
add_action('admin_menu', 'audiobar_admin_menu');
}
/**
* Admin Menu Markup
*/
function audiobar_admin() {
include(ABSPATH.'/wp-content/plugins/audiobar/audiobar-default-settings.php');
$labels = array(
'audiobar_title_color' => __('Color of song title'),
'audiobar_hover_color' => __('Color for hovering over the flash player'),
'audiobar_bar_gradient_1' => __('Top color for bar gradient'),
'audiobar_bar_gradient_2' => __('Bottom color for bar gradient'),
'audiobar_button_gradient_1' => __('Top color for button gradient'),
'audiobar_button_gradient_2' => __('Bottom color for button gradient'),
'audiobar_button_hilite_gradient_1' => __('Top color for highlighted button gradient'),
'audiobar_button_hilite_gradient_2' => __('Bottom color for highlighted button gradient'),
'audiobar_disable_backlink' => __('Check here to disable the audiobar link'),
'audiobar_position' => __('The audiobar is displayed:'),
);
$page_options = array_keys($labels);
$page_options = implode(',', $page_options);
include(audiobar_get_template('audiobar-admin.php'));
}
/**
* Display note if automatic configuration for audio file downloads has been flagged
* to need need some manual attention.
*/
function audiobar_notices() {
global $is_apache;
if (!$is_apache) {
$audiobar_notice = 'no_apache';
} elseif ( ! apache_mod_loaded( 'mod_headers', true ) ) {
$audiobar_notice = 'no_mod_headers';
} elseif (!file_exists($htaccess_file) && !is_writable($home_path)) {
$audiobar_notice = 'homedir_not_writable';
} elseif (file_exists($htaccess_file) && !is_writable($htaccess_file)) {
$audiobar_notice = 'htaccess_not_writable';
}
if (get_option('audiobar_notice') != '') {
$messages = array(
'no_apache' => __('If your download links start playing in the browser instead of downloading,please configure the file types ogg,oga,mp3,flac and wav to have the header Content-disposition=attachment.'),
'no_mod_headers' => __('If your download links start playing in the browser instead of downloading, please make sure your Apache has the module mod_headers enabled.'),
'htaccess_not_writable' => __('If your download links start playing in the browser instead of downloading, please manually add the following code to your .htaccess file, as it is not writable to Wordpress.') . ''.AUDIOBAR_HTACCESS_RULES.'
',
'htaccess_not_writable' => __('If your download links start playing in the browser instead of downloading, please manually create and add the following code to your .htaccess file, as your home directory is not writable to Wordpress.').''.AUDIOBAR_HTACCESS_RULES.'
',
);
$message = __('Thank you for installing Audiobar!') . $messages[$audiobar_notice] . __('Your hosting provider may be able to help you with this.');
echo sprintf('', $message);
}
}
/**
* Returns wpurl as a relative url
* @return string WPURL as relative URL
*/
function audiobar_relative_wpurl() {
$url = get_bloginfo('wpurl');
return substr($url, strpos($url, '/', 9));
}
/**
* Allows for the uploading of flac files
* @param array $mimes Original array of mime types
* @return array Augmented array of mime types
*/
function allow_flac($mimes) {
$mimes = array_merge($mimes, array(
'flac' => 'audio/flac'
));
return $mimes;
}
add_filter('upload_mimes', 'allow_flac');