<?php

/**
 * Single Podcast.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */

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

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

	/**
	 * Filter the post content.
	 *
	 * @since  1.0.0
	 * @param  string $content Content of the current post.
	 * @return string $content Modified Content.
	 */
	public function the_content( $content ) {
		$in_the_loop   = apply_filters( 'aiopod_the_content_in_the_loop', in_the_loop() );
		$is_main_query = apply_filters( 'aiopod_the_content_is_main_query', is_main_query() );

		if ( is_singular( 'aiopod_podcasts' ) && $in_the_loop && $is_main_query ) {		
			global $wp_the_query, $post;
			$post_id = $post->ID;

			if ( $wp_the_query->get_queried_object_id() !== $post_id ) {
				return $content;
			}
			
			if ( post_password_required( $post_id ) ) {
				return $content;
			}

			if ( ! aiopod_current_user_can( 'view_aiopod_podcast', $post_id ) ) {
				$access_denied_message = apply_filters( 'aiopod_podcast_access_denied_message', __( 'Access to this podcast is restricted.', 'aio-podcast-studio' ), $post_id );
				if ( ! empty( $access_denied_message ) ) {
					return '<p>' . wp_kses_post( $access_denied_message ) . '</p>';
				}

				return '';
			}
			
			// Vars
			$podcast_settings  = aiopod_get_option( 'aiopod_podcast_settings' );
			$episodes_settings = aiopod_get_option( 'aiopod_episodes_settings' );
			
			$attributes = array(
				'show_categories'        => isset( $podcast_settings['display_metadata']['categories'] ),
				'show_date'              => isset( $podcast_settings['display_metadata']['date'] ),
				'show_episodes_count'    => isset( $podcast_settings['display_metadata']['episodes_count'] ),
				'show_explicit_badge'    => isset( $podcast_settings['display_metadata']['explicit_badge'] ),
				'show_downloads_count'   => isset( $podcast_settings['display_metadata']['downloads_count'] ),
				'show_plays_count'       => isset( $podcast_settings['display_metadata']['plays_count'] ),
				'show_subscribe_buttons' => isset( $podcast_settings['social_buttons']['subscribe_buttons'] ),
				'show_follow_buttons'    => isset( $podcast_settings['social_buttons']['follow_buttons'] ),
				'show_share_buttons'     => isset( $podcast_settings['social_buttons']['share_buttons'] ),
				'show_episodes'          => $podcast_settings['show_episodes'],
				'episodes_title'         => $podcast_settings['episodes_title'],
				'show_trailer_episodes'  => $podcast_settings['show_trailer_episodes'],
				'trailer_episodes_title' => $podcast_settings['trailer_episodes_title'],
				'show_bonus_episodes'    => $podcast_settings['show_bonus_episodes'],
				'bonus_episodes_title'   => $podcast_settings['bonus_episodes_title'],
				'show_participants'      => $podcast_settings['show_participants'],
				'participants_title'     => $podcast_settings['participants_title'],
				'interactive_mode'       => 0,
				'slides_per_view'        => $podcast_settings['slides_per_view'],
				'space_between'          => $podcast_settings['space_between'],
				'show_dots'              => $podcast_settings['show_dots'],
				'show_arrows'            => $podcast_settings['show_arrows']		
			);

			if ( ! empty( $episodes_settings['enable_player'] ) || 'compact' === $episodes_settings['layout'] ) {
				$attributes['interactive_mode'] = 1;
			}

			// 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-podcast.php' );
			$content = ob_get_clean();			
		}
		
		return $content;	
	}
		
}