)?\[audio:(([^]]+))\](<\/p>)?/i";
$content = preg_replace_callback( $pattern, array(&$this, "insertPlayer"), $content );
}
// Enclosure integration (don't do this for feeds, excerpts and comments)
if( !is_feed() && !$this->inExcerpt && !$comment && in_array( "enclosure", $this->options["behaviour"] ) ) {
$enclosure = get_enclosed($post_id);
// Insert intro and outro clips if set
$introClip = $this->options["introClip"];
if( $introClip != "" ) $introClip .= ",";
$outroClip = $this->options["outroClip"];
if( $outroClip != "" ) $outroClip = "," . $outroClip;
if( count($enclosure) > 0 ) {
for($i = 0;$i < count($enclosure);$i++) {
// Make sure the enclosure is an mp3 file and it hasn't been inserted into the post yet
if( preg_match( "/.*\.mp3$/", $enclosure[$i] ) == 1 && !in_array( $enclosure[$i], $this->instances ) ) {
if ($this->options["enclosuresAtTop"]) {
$content = $this->getPlayer( $introClip . $enclosure[$i] . $outroClip, null, $enclosure[$i] ) . "\n\n" . $content;
} else {
$content .= "\n\n" . $this->getPlayer( $introClip . $enclosure[$i] . $outroClip, null, $enclosure[$i] );
}
}
}
}
}
return $content;
}
/**
* Callback function for preg_replace_callback
* @return string to replace matches with
* @param $matches Array
*/
function insertPlayer($matches) {
// Split options
$data = preg_split("/[\|]/", $matches[3]);
$files = array();
// Alternate content for excerpts (don't do this for feeds)
if($this->inExcerpt && !is_feed()) {
return $this->options["excerptAlternate"];
}
if (!is_feed()) {
// Insert intro clip if set
if ( $this->options["introClip"] != "" ) {
$afile = $this->options["introClip"];
if (!$this->isAbsoluteURL($afile)) {
$afile = $this->audioRoot . "/" . $afile;
}
array_push( $files, $afile );
}
}
$actualFiles = array();
$actualFile = "";
// Create an array of files to load in player
foreach ( explode( ",", trim($data[0]) ) as $afile ) {
$afile = trim($afile);
// Get absolute URLs for relative ones
if (!$this->isAbsoluteURL($afile)) {
$afile = $this->audioRoot . "/" . $afile;
}
array_push( $actualFiles, $afile );
array_push( $files, $afile );
// Add source file to instances already added to the post
array_push( $this->instances, $afile );
}
if (count($actualFiles) == 1) {
$actualFile = $actualFiles[0];
}
if (!is_feed()) {
// Insert outro clip if set
if ( $this->options["outroClip"] != "" ) {
$afile = $this->options["outroClip"];
if (!$this->isAbsoluteURL($afile)) {
$afile = $this->audioRoot . "/" . $afile;
}
array_push( $files, $afile );
}
}
// Build runtime options array
$playerOptions = array();
for ($i = 1; $i < count($data); $i++) {
$pair = explode("=", $data[$i]);
$playerOptions[trim($pair[0])] = trim($pair[1]);
}
// Return player instance code
return $this->getPlayer( implode( ",", $files ), $playerOptions, $actualFile );
}
/**
* Generic player instance function (returns player widget code to insert)
* @return String the html code to insert
* @param $source String list of mp3 file urls to load in player
* @param $playerOptions Object[optional] options to load in player
* @param $actualFile String[optional] url of main single file (empty if multiple files)
*/
function getPlayer($source, $playerOptions = array(), $actualFile = "") {
// Decode HTML entities in file names
if (function_exists("html_entity_decode")) {
$source = html_entity_decode($source);
}
// Add source to options and encode if necessary
if ($this->options["encodeSource"]) {
$playerOptions["soundFile"] = $this->encodeSource($source);
} else {
$playerOptions["soundFile"] = $source;
}
if (is_feed()) {
// We are in a feed so use RSS alternate content option
switch ( $this->options["rssAlternate"] ) {
case "download":
// Get filenames from path and output a link for each file in the sequence
$files = explode(",", $source);
$links = "";
for ($i = 0; $i < count($files); $i++) {
$fileparts = explode("/", $files[$i]);
$fileName = $fileparts[count($fileparts)-1];
$links .= '' . __('Download audio file', $this->textDomain) . ' (' . $fileName . ')
';
}
return $links;
break;
case "nothing":
return "";
break;
case "custom":
return $this->options["rssCustomAlternate"];
break;
}
} else {
// Not in a feed so return player widget
$playerElementID = "audioplayer_" . ++$this->playerID;
if (strlen($this->options["flashAlternate"]) > 0) {
$playerCode = str_replace(array("%playerID%", "%downloadURL%"), array($playerElementID, $actualFile), $this->options["flashAlternate"]);
} else {
$playerCode = '
' . sprintf(__('Audio clip: Adobe Flash Player (version 9 or above) is required to play this audio clip. Download the latest version here. You also need to have JavaScript enabled in your browser.', $this->textDomain), 'http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash&promoid=BIOW') . '
'; } $this->footerCode .= 'AudioPlayer.embed("' . $playerElementID . '", ' . $this->php2js($playerOptions) . ');'; $this->footerCode .= "\n"; return $playerCode; } } /** * Outputs the options sub panel */ function outputOptionsSubpanel() { $this->loadLanguageFile(); add_action("in_admin_footer", array(&$this, "addFooterCredits")); // Include options panel include($this->pluginPath . "/php/options-panel.php"); } /** * Handles submitted options (validates and saves modified options) */ function optionsPanelAction() { if( isset($_POST['AudioPlayerReset']) && $_POST['AudioPlayerReset'] == "1" ) { if( function_exists('current_user_can') && !current_user_can('manage_options') ) { wp_die(__('You do not have sufficient permissions to access this page.')); } // Reset colour scheme back to default values $this->options["colorScheme"] = $this->defaultColorScheme; $this->saveOptions(); $goback = add_query_arg("updated", "true", "options-general.php?page=" . $this->optionsPageName); wp_redirect($goback); exit(); } else if( isset($_POST['AudioPlayerSubmit']) ) { if( function_exists('current_user_can') && !current_user_can('manage_options') ) { wp_die(__('You do not have sufficient permissions to access this page.')); } if ( function_exists('check_admin_referer') ) { check_admin_referer('audio-player-action'); } // Set audio web path $_POST['ap_audiowebpath'] = trim($_POST['ap_audiowebpath']); if ($_POST["ap_audiowebpath_iscustom"] != "true") { if ( substr( $_POST['ap_audiowebpath'], -1, 1 ) == "/" ) { $_POST['ap_audiowebpath'] = substr( $_POST['ap_audiowebpath'], 0, strlen( $_POST['ap_audiowebpath'] ) - 1 ); } if ( substr( $_POST['ap_audiowebpath'], 0, 1 ) != "/" ) { $_POST['ap_audiowebpath'] = "/" . $_POST['ap_audiowebpath']; } $this->options["audioFolder"] = $_POST['ap_audiowebpath']; } else if ($this->isAbsoluteURL($_POST['ap_audiowebpath'])) { $this->options["audioFolder"] = $_POST['ap_audiowebpath']; } // Update behaviour and rss alternate content options $this->options["encodeSource"] = isset( $_POST["ap_encodeSource"] ); $this->options["enableAnimation"] = !isset( $_POST["ap_disableAnimation"] ); $this->options["showRemaining"] = isset( $_POST["ap_showRemaining"] ); $this->options["noInfo"] = isset( $_POST["ap_disableTrackInformation"] ); $this->options["checkPolicy"] = isset( $_POST["ap_checkPolicy"] ); $this->options["rtl"] = isset( $_POST["ap_rtlMode"] ); $this->options["enclosuresAtTop"] = isset( $_POST["ap_enclosuresAtTop"] ); $this->options["disableEnclosures"] = isset( $_POST["ap_disableEnclosures"] ); if (isset($_POST['ap_behaviour'])) { $this->options["behaviour"] = $_POST['ap_behaviour']; } else { $this->options["behaviour"] = array(); } //$this->options["flashAlternate"] = trim(stripslashes($_POST['ap_flashalternate'])); $this->options["excerptAlternate"] = trim(stripslashes($_POST['ap_excerptalternate'])); $this->options["rssAlternate"] = $_POST['ap_rssalternate']; $this->options["rssCustomAlternate"] = trim(stripslashes($_POST['ap_rsscustomalternate'])); $this->options["introClip"] = trim($_POST['ap_audioprefixwebpath']); $this->options["outroClip"] = trim($_POST['ap_audiopostfixwebpath']); $_POST['ap_player_width'] = trim($_POST['ap_player_width']); if ( preg_match("/^[0-9]+%?$/", $_POST['ap_player_width']) == 1 ) { $this->options["playerWidth"] = $_POST['ap_player_width']; } $_POST['ap_initial_volume'] = trim($_POST['ap_initial_volume']); if ( preg_match("/^[0-9]+$/", $_POST['ap_initial_volume']) == 1 ) { $_POST['ap_initial_volume'] = intval($_POST['ap_initial_volume']); if ($_POST['ap_initial_volume'] <= 100) { $this->options["initialVolume"] = $_POST['ap_initial_volume']; } } $_POST['ap_buffertime'] = trim($_POST['ap_buffertime']); if ( preg_match("/^[0-9]+$/", $_POST['ap_buffertime']) == 1 ) { $_POST['ap_buffertime'] = intval($_POST['ap_buffertime']); if ($_POST['ap_buffertime'] > 0) { $this->options["bufferTime"] = $_POST['ap_buffertime']; } } // Update colour options foreach ( $this->colorKeys as $colorKey ) { // Ignore missing or invalid color values if ( isset( $_POST["ap_" . $colorKey . "color"] ) && preg_match( "/^#[0-9A-Fa-f]{6}$/", $_POST["ap_" . $colorKey . "color"] ) == 1 ) { $this->options["colorScheme"][$colorKey] = str_replace( "#", "", $_POST["ap_" . $colorKey . "color"] ); } } if ( isset( $_POST["ap_pagebgcolor"] ) && preg_match( "/^#[0-9A-Fa-f]{6}$/", $_POST["ap_pagebgcolor"] ) == 1 ) { $this->options["colorScheme"]["pagebg"] = str_replace( "#", "", $_POST['ap_pagebgcolor']); } $this->options["colorScheme"]["transparentpagebg"] = isset( $_POST["ap_transparentpagebg"] ); $this->saveOptions(); $goback = add_query_arg("updated", "true", "options-general.php?page=" . $this->optionsPageName); wp_redirect($goback); exit(); } } /** * Inserts Audio Player button into media library popup * @return the amended form_fields structure * @param $form_fields Object * @param $post Object */ function insertAudioPlayerButton($form_fields, $post) { global $wp_version; $file = wp_get_attachment_url($post->ID); // Only add the extra button if the attachment is an mp3 file if ($post->post_mime_type == 'audio/mpeg') { $form_fields["url"]["html"] .= ""; if (version_compare($wp_version, "2.7", "<")) { $form_fields["url"]["html"] .= "\n"; } } return $form_fields; } /** * Format the html inserted when the Audio Player button is used * @param $html String * @return String */ function sendToEditor($html) { if (preg_match("/([^<]*)<\/a>/i", $html, $matches)) { $html = $matches[2]; if (strlen($matches[5]) > 0) { $html = preg_replace("/]$/i", "|titles=" . $matches[5] . "]", $html); } } return $html; } /** * Output necessary stuff to WP head section */ function addHeaderCode() { echo ''; echo "\n"; echo ''; echo "\n"; } /** * Output necessary stuff to WP footer section (JS calls to embed players) */ function addFooterCode() { if (strlen($this->footerCode) > 0) { echo ''; echo "\n"; // Reset it now $this->footerCode = ""; } } /** * Override media-upload script to handle Audio Player inserts from media library */ function overrideMediaUpload() { echo ''; echo "\n"; } /** * Output necessary stuff to WP admin head section */ function addAdminHeaderCode() { global $wp_version; echo ''; echo "\n"; echo ''; echo "\n"; // Include jquery library if we are not running WP 2.5 or above if (version_compare($wp_version, "2.5", "<")) { echo ''; echo "\n"; } echo ''; echo "\n"; echo ''; echo "\n"; echo ''; echo "\n"; echo ''; echo "\n"; } /** * Verifies that the given audio folder exists on the server (Ajax call) */ function checkAudioFolder() { $audioRoot = $_POST["audioFolder"]; $sysDelimiter = '/'; if (strpos(ABSPATH, '\\') !== false) $sysDelimiter = '\\'; $audioAbsPath = preg_replace('/[\\\\\/]+/', $sysDelimiter, ABSPATH . $audioRoot); if (!file_exists($audioAbsPath)) { echo $audioAbsPath; } else { echo "ok"; } } /** * Parses theme style sheet * @return array of colors from current theme */ function getThemeColors() { $current_theme_data = get_theme(get_current_theme()); $theme_css = implode('', file( get_theme_root() . "/" . $current_theme_data["Stylesheet"] . "/style.css")); preg_match_all('/:[^:,;\{\}].*?#([abcdef1234567890]{3,6})/i', $theme_css, $matches); return array_unique($matches[1]); } /** * Formats a php associative array into a javascript object * @return formatted string * @param $object Object containing the options to format */ function php2js($object) { $js_options = '{'; $separator = ""; $real_separator = ","; foreach($object as $key=>$value) { // Format booleans if (is_bool($value)) $value = $value?"yes":"no"; else if (in_array($key, array("soundFile", "titles", "artists"))) { if (in_array($key, array("titles", "artists"))) { // Decode HTML entities in titles and artists if (function_exists("html_entity_decode")) { $value = html_entity_decode($value); } } $value = rawurlencode($value); } $js_options .= $separator . $key . ':"' . $value .'"'; $separator = $real_separator; } $js_options .= "}"; return $js_options; } /** * @return true if $path is absolute * @param $path Object */ function isAbsoluteURL($path) { if (strpos($path, "http://") === 0) { return true; } if (strpos($path, "https://") === 0) { return true; } if (strpos($path, "ftp://") === 0) { return true; } return false; } /** * Encodes the given string * @return the encoded string * @param $string String the string to encode */ function encodeSource($string) { $source = utf8_decode($string); $ntexto = ""; $codekey = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-"; for ($i = 0; $i < strlen($string); $i++) { $ntexto .= substr("0000".base_convert(ord($string{$i}), 10, 2), -8); } $ntexto .= substr("00000", 0, 6-strlen($ntexto)%6); $string = ""; for ($i = 0; $i < strlen($ntexto)-1; $i = $i + 6) { $string .= $codekey{intval(substr($ntexto, $i, 6), 2)}; } return $string; } } } // Instantiate the class if (class_exists('AudioPlayer')) { global $AudioPlayer; if (!isset($AudioPlayer)) { if (version_compare(PHP_VERSION, '5.0.0', '<')) { $AudioPlayer = &new AudioPlayer(); } else { $AudioPlayer = new AudioPlayer(); } } } /** * Experimental "tag" function for inserting players anywhere (yuk) * @return * @param $source Object */ function insert_audio_player($source) { global $AudioPlayer; echo $AudioPlayer->processContent($source); } ?>