<?php

/**
 * Episode Tags.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */

// Exit if accessed directly
if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * AIOPOD_Public_Tags class.
 *
 * @since 1.0.0
 */
class AIOPOD_Public_Tags {

	/**
	 * The only instance of the class.
	 *
	 * @since  1.0.0
	 * @access private
	 * @static
	 * @var    AIOPOD_Public_Tags|null
	 */
	private static $instance = null;

	/**
	 * The default shortcode attribute values.
	 *
	 * @since  1.0.0
	 * @access private
	 * @var    array
	 */
	private $defaults = array();

	/**
	 * Create a new instance of the main class.
	 *
	 * @since  1.0.0
	 * @static
	 * @return AIOPOD_Public_Tags
	 */
	public static function get_instance() {
		if ( self::$instance === null ) {
            self::$instance = new self();
        }

		return self::$instance;
	}

	/**
	 * Prevent cloning of the singleton instance.
	 *
	 * @since  1.0.0
	 * @access private
	 */
	private function __clone() {}

	/**
	 * Prevent unserializing of the singleton instance.
	 *
	 * @since 1.0.0
	 */
	public function __wakeup() {}

	/**
	 * Get things started.
	 *
	 * @since  1.0.0
	 * @access private
	 */
	private function __construct() {
		// Register shortcodes
		add_shortcode( 'aiopod_tag', array( $this, 'render_shortcode_tag' ) );
		add_shortcode( 'aiopod_tags', array( $this, 'render_shortcode_tags' ) );
	}	
	
	/**
	 * Render the shortcode [aiopod_tag].
	 *
	 * @since 1.0.0
	 * @param array $atts An associative array of attributes.
	 */
	public function render_shortcode_tag( $atts ) {
		$tags_settings  = aiopod_get_option( 'aiopod_tags_settings' );
		$tag_settings   = aiopod_get_option( 'aiopod_tag_settings' );
		$pages_settings = aiopod_get_option( 'aiopod_pages_settings' );

		$term_slug = get_query_var( 'aiopod_tag' );
		$content   = '';

		if ( ! empty( $term_slug ) ) {
			$term = get_term_by( 'slug', sanitize_title( $term_slug ), 'aiopod_tags' );
		} elseif ( ! empty( $atts['id'] ) ) {			
			$term = get_term_by( 'id', (int) $atts['id'], 'aiopod_tags' );
		}

		if ( isset( $term ) && ! empty( $term ) ) {
			$defaults = array(
				'show_breadcrumbs' => $tag_settings['show_breadcrumbs'],
				'current_page_id'  => ! empty( $pages_settings['tag'] ) ? (int) $pages_settings['tag'] : 0,
				'show_episodes'    => $tag_settings['show_episodes'],
				'episodes_title'   => $tag_settings['episodes_title']
			);

			$attributes = shortcode_atts( $defaults, $atts, 'aiopod_tag' );

			// Enqueue dependencies
			wp_enqueue_style( AIOPOD_PLUGIN_SLUG . '-public' );
			wp_enqueue_script( AIOPOD_PLUGIN_SLUG . '-public' );

			// Process output
			ob_start();
			include apply_filters( 'aiopod_load_template', AIOPOD_PLUGIN_DIR . 'public/templates/single-tag.php', $attributes );
			$content = ob_get_clean();
		} else {
			$content = do_shortcode( '[aiopod_tags]' );
		}			
		
		return $content;
	}
	
	/**
	 * Render the shortcode [aiopod_tags].
	 *
	 * @since 1.0.0
	 * @param array $atts An associative array of attributes.
	 */
	public function render_shortcode_tags( $atts ) {	
		// Vars
		$attributes = shortcode_atts( $this->get_defaults(), $atts, 'aiopod_tags' );		
		$attributes['shortcode'] = 'aiopod_tags';	
		
		if ( ! in_array( $attributes['layout'], array( 'cloud', 'list', 'dropdown' ) ) ) {
			$attributes['layout'] = 'cloud';
		}

		// Enqueue dependencies
		wp_enqueue_style( AIOPOD_PLUGIN_SLUG . '-public' );
		wp_enqueue_script( AIOPOD_PLUGIN_SLUG . '-public' );
		
		// Process output
		ob_start();
		include apply_filters( 'aiopod_load_template', AIOPOD_PLUGIN_DIR . 'public/templates/tags/tags-' . sanitize_file_name( $attributes['layout'] ) . '.php', $attributes );
		return ob_get_clean();	
	}	

	/**
	 * Get the default shortcode attribute values.
	 *
	 * @since  1.0.0
	 * @return array $atts An associative array of attributes.
	 */
	public function get_defaults() {	
		if ( empty( $this->defaults ) ) {
			$fields = aiopod_get_shortcode_fields();

			foreach ( $fields['tags']['sections'] as $section ) {
				foreach ( $section['fields'] as $field ) {
					$this->defaults[ $field['name'] ] = $field['value'];
				}
			}
			
			$this->defaults['source']        = 'tags';
			$this->defaults['title']         = '';
			$this->defaults['include']       = '';	
			$this->defaults['exclude']       = '';
			$this->defaults['hide_if_empty'] = 0;	
		}

		return $this->defaults;
	}
	
}
