<?php
/*
Plugin Name: Audio Album
Plugin URI: http://cubecolour.co.uk/audio-album
Description: Provides shortcodes to format native WordPress audio players as an album of tracks with additional info
Author: cubecolour
Version: 1.2.0
Author URI: http://cubecolour.co.uk/
License: GPLv2

  Copyright 2013-2016 Michael Atkins

  michael@cubecolour.co.uk

  Licenced under the GNU GPL:

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/**
* Set Constant: Plugin Version
*
*/
define( 'CC_AUDIO_ALBUM_VERSION', '1.2.0' );


/**
* Add Links in Plugins Table
*
*/
add_filter( 'plugin_row_meta', 'cc_audioalbum_meta_links', 10, 2 );
function cc_audioalbum_meta_links( $links, $file ) {

	$plugin = plugin_basename(__FILE__);

//* create the links
	if ( $file == $plugin ) {

		$supportlink = 'https://wordpress.org/support/plugin/audio-album';
		$donatelink = 'http://cubecolour.co.uk/wp';
		$reviewlink = 'https://wordpress.org/support/view/plugin-reviews/audio-album#postform';
		$twitterlink = 'http://twitter.com/cubecolour';
		$iconstyle = 'style="-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;"';

		return array_merge( $links, array(
			'<a href="' . $supportlink . '"> <span class="dashicons dashicons-lightbulb" ' . $iconstyle . 'title="Audio Album Support"></span></a>',
			'<a href="' . $twitterlink . '"><span class="dashicons dashicons-twitter" ' . $iconstyle . 'title="Cubecolour on Twitter"></span></a>',
			'<a href="' . $reviewlink . '"><span class="dashicons dashicons-star-filled"' . $iconstyle . 'title="Review Audio Album"></span></a>',
			'<a href="' . $donatelink . '"><span class="dashicons dashicons-heart"' . $iconstyle . 'title="Donate"></span></a>'
		) );
	}

	return $links;
}


/**
* Register the script for the popup
*
*/
function cc_audiotrackpopup_script() {
	wp_register_script( 'audiotrackpopup', plugins_url() . "/" . basename(dirname(__FILE__)) . '/js/audiotrackpopup.js', array('jquery'), CC_AUDIO_ALBUM_VERSION, false );
}

add_action('wp_enqueue_scripts', 'cc_audiotrackpopup_script');


/**
* Prevent unstyled players being shown until the page has fully loaded
*
*/
function cc_hide_audio_until_load() {
	echo "\n" . '<script type="text/javascript">jQuery(window).load(function() { jQuery("div.track").css("visibility", "visible"); });</script>' . "\n";
}

add_action('wp_head', 'cc_hide_audio_until_load');


/**
* Add stylesheet
* To use custom styles, unregister the default stylesheet by adding the following lines to the theme's functions.php,
* Then either register a custom version with the name 'audioalbum', or add your customised styles to the theme or child theme

//* Remove Default styles from the cubecolour audio album plugin
add_action('wp_enqueue_scripts', 'cc_remove_default_audioalbum_css', 100);

function cc_remove_default_audioalbum_css() {
	wp_deregister_style( 'audioalbum' );
}

*/
add_action('wp_enqueue_scripts', 'cc_audioalbum_css', 30);

function cc_audioalbum_css() {
	wp_register_style('audioalbum', plugin_dir_url(__FILE__).'styles/' . 'audioalbum.css' , false, CC_AUDIO_ALBUM_VERSION );
}


/**
* Shortcode to add Album Title
* Not Mandatory
*/
function cc_audioheading_shortcode( $atts, $content = null ) {

	wp_register_style( 'audioalbum' );

	$args =  shortcode_atts( array(

		'title'		=> '',
		'label'		=> '',
		'catalog'	=> '',

	), $atts, 'audioheading' );

	return '<h1 class="audioheading">' . $args['title'] . '</h1><p class="audioheading">' . $args['label'] . ' <span>' . $args['catalog'] . '</span></p>';
}

add_shortcode( 'audioheading', 'cc_audioheading_shortcode' );


/**
* shortcode to add Album Header info and enclose the audio players
* do_shortcode($content) allows the shortcode for each track to be nested inside the album's Shortcode
*/
function cc_audioalbum_shortcode( $atts, $content = null ) {

	$args =  shortcode_atts( array(

		'title'		=> '',
		'detail'	=> '',
		'date'		=> '',

	), $atts, 'audioalbum' );

	return '<h2 class="audioalbum">' . $args['title'] . '</h2>'
	. '<p class="audioalbum">' . $args['detail'] . '<span>' . $args['date'] .'</span>' . do_shortcode($content) . '</p>';
}

add_shortcode( 'audioalbum', 'cc_audioalbum_shortcode' );


/**
* Shortcode to add each audio track inside the album
*
*/
function cc_audiotrack_shortcode( $atts, $content = null ) {

	wp_enqueue_script( 'audiotrackpopup' );
	wp_enqueue_style( 'audioalbum' );

	$lyricslink= '';
	$popupbutton = '';
	$cc_siteurl = get_bloginfo('url');

	$args =  shortcode_atts( array(

		'title'			=> '',
		'width'			=> '520',
		'height'		=> '400',
		'songwriter'	=> '',
		'buttontext'	=> 'lyrics',
		'buttonlink'	=> '#',
		'preload'		=> 'metadata',
		'src'			=> '',
		'mp3'			=> '',
		'ogg'			=> '',
		'wma'			=> '',
		'm4a'			=> '',
		'wav'			=> '',
		'loop'			=> '',
		'autoplay'		=> '',

	), $atts, 'audiotrack' );

	$wpaudioshortcode = 'audio';

	$args['title'] = esc_attr( $args['title'] );
	$args['buttontext'] = esc_attr( $args['buttontext'] );

	if ($args['songwriter']  !== '') {
		$args['songwriter'] = '<span class="songwriter">(' . $args['songwriter'] . ')</span>';
	}

	if ($args['src'] !== ''){
	$wpaudioshortcode .= ' src="' . esc_url( $args['src'] ) . '"';
	}

	if ($args['mp3'] !== ''){
	$wpaudioshortcode .= ' mp3="' . esc_url( $args['mp3'] ) . '"';
	}

	if ($args['ogg'] !== ''){
	$wpaudioshortcode .= ' ogg="' . esc_url( $args['ogg'] ) . '"';
	}

	if ($args['wma'] !== ''){
	$wpaudioshortcode .= ' wma="' . esc_url( $args['wma'] ) . '"';
	}

	if ($args['m4a'] !== ''){
	$wpaudioshortcode .= ' m4a="' . esc_url( $args['m4a'] ) . '"';
	}

	if ($args['wav'] !== ''){
	$wpaudioshortcode .= ' wav="' . esc_url( $args['wav'] ) . '"';
	}

	if ($args['loop'] !== ''){
	$wpaudioshortcode .= ' loop="' . esc_attr($args['loop']) . '"';
	}

	if ($args['autoplay'] !== ''){
	$wpaudioshortcode .= ' autoplay="' . esc_attr( $args['autoplay'] ) . '"';
	}

	if ($preload !== 'none'){
	$wpaudioshortcode .= ' preload="' . esc_attr( $preload ) . '"';
	}

	if ($args['buttonlink']  !== '#') {
		$popupbutton = '<a href="'. $cc_siteurl .'/?p=' . esc_attr( $args['buttonlink'] ) . '&pop=yes" class="info-popup" data-width="' . esc_attr( $args['width'] ) . '" data-height="' . esc_attr($args['height']) . '">' . esc_attr($args['buttontext']) . '</a>';
	}

	$audiotrack = '<span class="songtitle">' . $args['title'] . '</span>' . $args['songwriter'] . '<span class="audiobutton">' . $popupbutton . '</span>';

	//* Shortcode Inception - call the native WP audio shortcode with all the attributes
	return '<div class="track" style="visibility:hidden;">' . $audiotrack . do_shortcode('[' . $wpaudioshortcode . ']') . '</div>';
}

add_shortcode( 'audiotrack', 'cc_audiotrack_shortcode' );


/**
* Bonus for Genesis theme users!
* If a Genesis child theme is being used, add a template for the popup
*
*/
if ( basename( get_template_directory() ) == 'genesis' ) {
	add_filter( 'template_include', 'cc_popup_audioalbum_template' );
}

function cc_popup_audioalbum_template( $template ) {
	if( isset( $_GET['pop']) && 'yes' == $_GET['pop'] )
		$template = plugin_dir_path( __FILE__ ) . 'popup-genesis-template.php';

	return $template;
}