<?php
/**
 * Function to pass the slider effect parameters from php file to js file.
 */
function catchflames_pass_slider_value() {
	global $catchflames_options_settings;
	$options = $catchflames_options_settings;

	$transition_effect = $options[ 'transition_effect' ];
	$transition_delay = $options[ 'transition_delay' ] * 1000;
	$transition_duration = $options[ 'transition_duration' ] * 1000;

	wp_localize_script(
		'catchflames-slider',
		'js_value',
		array(
			'transition_effect' => $transition_effect,
			'transition_delay' => $transition_delay,
			'transition_duration' => $transition_duration
		)

	);

}// catchflames_pass_slider_value


/**
 * Shows Default Slider Demo if there is not iteam in Featured Post Slider
 */
function catchflames_default_sliders() {
	delete_transient( 'catchflames_default_sliders' );

	// get data value from theme options
	global $catchflames_options_settings;
    $options = $catchflames_options_settings;
	$sliderlayout = $options[ 'select_slider_layout' ];

	if ( !$catchflames_default_sliders = get_transient( 'catchflames_default_sliders' ) ) {
		echo '<!-- refreshing cache -->';

		$sliderlayout = $options[ 'select_slider_layout' ];
		if ( $sliderlayout == 'fullwidth' ) :
			$layoutclass = 'full-width';
		else :
			$layoutclass = 'normal-width';
		endif;

		$catchflames_default_sliders = '
		<div id="main-slider" class="featured-demo-slider ' . $layoutclass . '">
			<section class="featured-slider">

				<article class="post hentry slides demo-image displayblock">
					<figure class="slider-image">
						<a title="Seto Ghumba" href="#">
							<img src="'. get_template_directory_uri() . '/images/demo/slider-1-1600x650.jpg" class="wp-post-image" alt="Seto Ghumba" title="Seto Ghumba">
						</a>
					</figure>
				</article><!-- .slides -->


				<article class="post hentry slides demo-image displaynone">
					<figure class="slider-image">
						<a title="Kathmandu Durbar Square" href="#">
							<img src="'. get_template_directory_uri() . '/images/demo/slider-2-1600x650.jpg" class="wp-post-image" alt="Kathmandu Durbar Square" title="Kathmandu Durbar Square">
						</a>
					</figure>
				</article><!-- .slides -->
			</section>
        	<div id="slider-nav">
        		<a class="slide-previous"><span>Previous</span></a>
        		<a class="slide-next"><span>Next</span></a>
        	</div>
			<div id="controllers"></div>
		</div><!-- #main-slider -->';

	set_transient( 'catchflames_default_sliders', $catchflames_default_sliders, 86940 );
	}
	echo $catchflames_default_sliders;
} // catchflames_default_sliders


if ( ! function_exists( 'catchflames_post_sliders' ) ) :
/**
 * Template for Featued Post Slider
 *
 * To override this in a child theme
 * simply create your own catchflames_post_sliders(), and that function will be used instead.
 *
 * @uses catchflames_header action to add it in the header
 * @since Catch Flames Pro 1.0
 */
function catchflames_post_sliders() {
	//delete_transient( 'catchflames_post_sliders' );

	// get data value from theme options
	global $post, $catchflames_options_settings;
   	$options = $catchflames_options_settings;

	if( ( !$catchflames_post_sliders = get_transient( 'catchflames_post_sliders' ) ) && !empty( $options[ 'featured_slider' ] ) ) {
		echo '<!-- refreshing cache -->';

		$sliderlayout = $options[ 'select_slider_layout' ];
		if ( $sliderlayout == 'fullwidth' ) :
			$layoutclass = 'full-width';
			$imagesize = 'featured-slider-full';
		else :
			$layoutclass = 'normal-width';
			$imagesize = 'featured-slider-normal';
		endif;

		$catchflames_post_sliders = '
		<div id="main-slider" class="featured-post-slider ' . $layoutclass . '">
        	<section class="featured-slider">';
				$get_featured_posts = new WP_Query( array(
					'posts_per_page' => $options[ 'slider_qty' ],
					'post__in'		 => $options[ 'featured_slider' ],
					'orderby' 		 => 'post__in',
					'ignore_sticky_posts' => 1 // ignore sticky posts
				));
				$i=0; while ( $get_featured_posts->have_posts()) : $get_featured_posts->the_post(); $i++;
					$title_attribute = apply_filters( 'the_title', get_the_title( $post->ID ) );
					$excerpt = get_the_excerpt();
					if ( $i == 1 ) { $classes = 'post postid-'.$post->ID.' hentry slides displayblock'; } else { $classes = 'post postid-'.$post->ID.' hentry slides displaynone'; }
					$catchflames_post_sliders .= '
					<article class="'.$classes.'">
						<figure class="slider-image">
							<a title="Permalink to '.the_title('','',false).'" href="' . get_permalink() . '">
								'. get_the_post_thumbnail( $post->ID, $imagesize, array( 'title' => esc_attr( $title_attribute ), 'alt' => esc_attr( $title_attribute ), 'class'	=> 'pngfix' ) ).'
							</a>
						</figure>';
						if ( empty ( $options[ 'disable_slider_text' ] ) ) {
							$catchflames_post_sliders .= '
							<div class="entry-container">
								<header class="entry-header">
									<h1 class="entry-title">
										<a title="Permalink to '.the_title('','',false).'" href="' . get_permalink() . '">'.the_title( '<span>','</span>', false ).'</a>
									</h1>
								</header>';
								if( $excerpt !='') {
									$catchflames_post_sliders .= '<div class="entry-content">'. $excerpt.'</div>';
								}
								$catchflames_post_sliders .= '
							</div>';
						}

					$catchflames_post_sliders .= '</article><!-- .slides -->';

				endwhile; wp_reset_query();
				$catchflames_post_sliders .= '
			</section>
        	<div id="slider-nav">
        		<a class="slide-previous"><span>Previous</span></a>
        		<a class="slide-next"><span>Next</span></a>
        	</div>
        	<div id="controllers"></div>
  		</div><!-- #main-slider -->';

	set_transient( 'catchflames_post_sliders', $catchflames_post_sliders, 86940 );
	}
	echo $catchflames_post_sliders;
} // catchflames_post_sliders
endif;


if ( ! function_exists( 'catchflames_page_sliders' ) ) :
/**
 * Template for Featued Page Slider
 *
 * To override this in a child theme
 * simply create your own catchflames_page_sliders(), and that function will be used instead.
 *
 * @uses catchflames_header action to add it in the header
 * @since Catch Flames Pro 1.0
 */
function catchflames_page_sliders() {
	//delete_transient( 'catchflames_page_sliders' );

	global $post, $catchflames_options_settings;
   	$options = $catchflames_options_settings;


	if( ( !$catchflames_page_sliders = get_transient( 'catchflames_page_sliders' ) ) && !empty( $options[ 'featured_slider_page' ] ) ) {
		echo '<!-- refreshing cache -->';

		$sliderlayout = $options[ 'select_slider_layout' ];
		if ( $sliderlayout == 'fullwidth' ) :
			$layoutclass = 'full-width';
			$imagesize = 'featured-slider-full';
		else :
			$layoutclass = 'normal-width';
			$imagesize = 'featured-slider-normal';
		endif;

		$catchflames_page_sliders = '
		<div id="main-slider" class="featured-page-slider ' . $layoutclass . '">
        	<section class="featured-slider">';
				$get_featured_posts = new WP_Query( array(
					'posts_per_page'	=> $options[ 'slider_qty' ],
					'post_type'			=> 'page',
					'post__in'			=> $options[ 'featured_slider_page' ],
					'orderby' 			=> 'post__in'
				));
				$i=0; while ( $get_featured_posts->have_posts()) : $get_featured_posts->the_post(); $i++;
					$title_attribute = apply_filters( 'the_title', get_the_title( $post->ID ) );
					$excerpt = get_the_excerpt();
					if ( $i == 1 ) { $classes = 'page pageid-'.$post->ID.' hentry slides displayblock'; } else { $classes = 'page pageid-'.$post->ID.' hentry slides displaynone'; }
					$catchflames_page_sliders .= '
					<article class="'.$classes.'">
						<figure class="slider-image">
							<a title="Permalink to '.the_title('','',false).'" href="' . get_permalink() . '">
								'. get_the_post_thumbnail( $post->ID, $imagesize, array( 'title' => esc_attr( $title_attribute ), 'alt' => esc_attr( $title_attribute ), 'class'	=> 'pngfix' ) ).'
							</a>
						</figure>';
						if ( empty ( $options[ 'disable_slider_text' ] ) ) {
							$catchflames_page_sliders .= '
							<div class="entry-container">
								<header class="entry-header">
									<h1 class="entry-title">
										<a title="Permalink to '.the_title('','',false).'" href="' . get_permalink() . '">'.the_title( '<span>','</span>', false ).'</a>
									</h1>
								</header>';
								if( $excerpt !='') {
									$catchflames_page_sliders .= '<div class="entry-content">'. $excerpt.'</div>';
								}
								$catchflames_page_sliders .= '
							</div>';
						}
					$catchflames_page_sliders .= '</article><!-- .slides -->';

				endwhile; wp_reset_query();
				$catchflames_page_sliders .= '
			</section>
        	<div id="slider-nav">
        		<a class="slide-previous"><span>Previous</span></a>
        		<a class="slide-next"><span>Next</span></a>
        	</div>
        	<div id="controllers"></div>
  		</div><!-- #main-slider -->';

	set_transient( 'catchflames_page_sliders', $catchflames_page_sliders, 86940 );
	}
	echo $catchflames_page_sliders;
} // catchflames_page_sliders
endif;


if ( ! function_exists( 'catchflames_category_sliders' ) ) :
/**
 * Template for Featued Category Slider
 *
 * To override this in a child theme
 * simply create your own catchflames_category_sliders(), and that function will be used instead.
 *
 * @uses catchflames_header action to add it in the header
 * @since Catch Flames Pro 1.0
 */
function catchflames_category_sliders() {
	//delete_transient( 'catchflames_category_sliders' );

	global $post, $catchflames_options_settings;
   	$options = $catchflames_options_settings;


	if( ( !$catchflames_category_sliders = get_transient( 'catchflames_category_sliders' ) ) && !empty( $options[ 'slider_category' ] ) ) {
		echo '<!-- refreshing cache -->';

		$sliderlayout = $options[ 'select_slider_layout' ];
		if ( $sliderlayout == 'fullwidth' ) :
			$layoutclass = 'full-width';
			$imagesize = 'featured-slider-full';
		else :
			$layoutclass = 'normal-width';
			$imagesize = 'featured-slider-normal';
		endif;

		$catchflames_category_sliders = '
		<div id="main-slider" class="featured-category-slider ' . $layoutclass . '">
        	<section class="featured-slider">';
				$get_featured_posts = new WP_Query( array(
					'posts_per_page'		=> $options[ 'slider_qty' ],
					'category__in'			=> $options[ 'slider_category' ],
					'ignore_sticky_posts' 	=> 1 // ignore sticky posts
				));
				$i=0; while ( $get_featured_posts->have_posts()) : $get_featured_posts->the_post(); $i++;
					$title_attribute = apply_filters( 'the_title', get_the_title( $post->ID ) );
					$excerpt = get_the_excerpt();
					if ( $i == 1 ) { $classes = 'post pageid-'.$post->ID.' hentry slides displayblock'; } else { $classes = 'post pageid-'.$post->ID.' hentry slides displaynone'; }
					$catchflames_category_sliders .= '
					<article class="'.$classes.'">
						<figure class="slider-image">
							<a title="Permalink to '.the_title('','',false).'" href="' . get_permalink() . '">
								'. get_the_post_thumbnail( $post->ID, $imagesize, array( 'title' => esc_attr( $title_attribute ), 'alt' => esc_attr( $title_attribute ), 'class'	=> 'pngfix' ) ).'
							</a>
						</figure>';
						if ( empty ( $options[ 'disable_slider_text' ] ) ) {
							$catchflames_category_sliders .= '
							<div class="entry-container">
								<header class="entry-header">
									<h1 class="entry-title">
										<a title="Permalink to '.the_title('','',false).'" href="' . get_permalink() . '">'.the_title( '<span>','</span>', false ).'</a>
									</h1>
								</header>';
								if( $excerpt !='') {
									$catchflames_category_sliders .= '<div class="entry-content">'. $excerpt.'</div>';
								}
								$catchflames_category_sliders .= '
							</div>';
						}
					$catchflames_category_sliders .= '</article><!-- .slides -->';

				endwhile; wp_reset_query();
				$catchflames_category_sliders .= '
			</section>
        	<div id="slider-nav">
        		<a class="slide-previous"><span>Previous</span></a>
        		<a class="slide-next"><span>Next</span></a>
        	</div>
        	<div id="controllers"></div>
  		</div><!-- #main-slider -->';

	set_transient( 'catchflames_category_sliders', $catchflames_category_sliders, 86940 );
	}
	echo $catchflames_category_sliders;
} // catchflames_category_sliders
endif;


if ( ! function_exists( 'catchflames_image_sliders' ) ) :
/**
 * Template for Featued Image Slider
 *
 * To override this in a child theme
 * simply create your own catchflames_image_sliders(), and that function will be used instead.
 *
 * @uses catchflames_header action to add it in the header
 * @since Catch Flames Pro 1.0
 */
function catchflames_image_sliders() {
	//delete_transient( 'catchflames_image_sliders' );

	global $catchflames_options_settings;
   	$options = $catchflames_options_settings;

	$more_tag_text = $options[ 'more_tag_text' ];

	if ( ( !$catchflames_image_sliders = get_transient( 'catchflames_image_sliders' ) ) && !empty( $options[ 'featured_image_slider_image' ] ) ) {
		echo '<!-- refreshing cache -->';

		$sliderlayout = $options[ 'select_slider_layout' ];
		if ( $sliderlayout == 'fullwidth' ) :
			$layoutclass = 'full-width';
		else :
			$layoutclass = 'normal-width';
		endif;

		$catchflames_image_sliders = '
		<div id="main-slider" class="featured-image-slider ' . $layoutclass . '">
        	<section class="featured-slider">';

				for ( $i=0; $i<=$options[ 'slider_qty' ]; $i++ ) {

					//Adding in Classes for Display blok and none
					if ( $i == 1 ) { $classes = 'image imageid-'.$i.' hentry slides displayblock'; } else { $classes = 'image imageid-'.$i.' hentry slides displaynone'; }

					//Checking Link
					if ( !empty ( $options[ 'featured_image_slider_link' ][ $i ] ) ) {
						//support for qtranslate custom link
						if ( function_exists( 'qtrans_convertURL' ) ) {
							$link = qtrans_convertURL($options[ 'featured_image_slider_link' ][ $i ]);
						}
						else {
							$link = $options[ 'featured_image_slider_link' ][ $i ];
						}
						//Checking Link Target
						if ( !empty ( $options[ 'featured_image_slider_base' ][ $i ] ) ) {
							$target = '_blank';
						} else {
							$target = '_self';
						}
					}
					else {
						$link = '';
						$target = '';
					}

					//Check Image Not Empty to add in the slides
					if ( !empty ( $options[ 'featured_image_slider_image' ][ $i ] ) ) {

						//Checking Title
						if ( !empty ( $options[ 'featured_image_slider_title' ][ $i ] ) ) {
							$imagetitle = $options[ 'featured_image_slider_title' ][ $i ];

							if ( !empty ( $options[ 'featured_image_slider_link' ][ $i ] ) ) {
								$title = '<header class="entry-header"><h1 class="entry-title"><a title="' . $imagetitle . '" href="'.$options[ 'featured_image_slider_link' ][ $i ].'" target="'.$target.'"><span>' . $imagetitle . '</span></a></h1></header>';
							}
							else {
								$title = '<header class="entry-header"><h1 class="entry-title"><span>' . $imagetitle . '</span></h1></header>';
							}

						}
						else {
							$title = '';
							$imagetitle = '';
						}

						//Checking Content
						if ( !empty ( $options[ 'featured_image_slider_content' ][ $i ] ) ) {
							$imagecontent = $options[ 'featured_image_slider_content' ][ $i ];

							if ( !empty ( $options[ 'featured_image_slider_link' ][ $i ] ) ) {
								$content = '<div class="entry-content">' . $imagecontent . ' <a href="' . $link . '" class="more-link" target="' . $target . '">' . $more_tag_text . '</a></div>';
							}
							else {
								$content = '<div class="entry-content">' . $imagecontent . '</div>';
							}
						}
						else {
							$content = '';
							$imagecontent = '';
						}

						//Checking Image
						if ( !empty ( $options[ 'featured_image_slider_link' ][ $i ] ) ) {
							$image = '<figure class="slider-image"><a title="' . $imagetitle . '" href="' . $link . '" target="' . $target . '"><img title="' . $imagetitle . '" alt="' . $imagetitle . '" class="wp-post-image" src="'.esc_url( $options[ 'featured_image_slider_image' ][ $i ] ).'" /></a></figure>';
						}
						else {
							$image = '<figure class="slider-image"><span class="no-img-link"><img title="' . $imagetitle . '" alt="' . $imagetitle . '" class="wp-post-image" src="'.esc_url( $options[ 'featured_image_slider_image' ][ $i ] ).'" /></figure>';
						}

						//Content Opening and Closing
						if ( !empty ( $options[ 'featured_image_slider_title' ][ $i ] ) || !empty ( $options[ 'featured_image_slider_content' ][ $i ] ) ) {
							$contentopening = '<div class="entry-container">';
							$contentclosing = '</div>';
						}
						else {
							$contentopening = '';
							$contentclosing = '';
						}

						$catchflames_image_sliders .= '
						<article class="'.$classes.'">'
							.$image;
							if ( empty ( $options[ 'disable_slider_text' ] ) ) {
								$catchflames_image_sliders .=
								$contentopening
									.$title
									.$content
								.$contentclosing;
							}
						$catchflames_image_sliders .= '</article><!-- .slides -->';

					}
				}
			$catchflames_image_sliders .= '
			</section>
        	<div id="slider-nav">
        		<a class="slide-previous"><span>Previous</span></a>
        		<a class="slide-next"><span>Next</span></a>
        	</div>
        	<div id="controllers"></div>
  		</div><!-- #main-slider -->';

	set_transient( 'catchflames_image_sliders', $catchflames_image_sliders, 86940 );
	}
	echo $catchflames_image_sliders;
} // catchflames_image_sliders
endif;


if ( ! function_exists( 'catchflames_slider_display' ) ) :
/**
 * Shows Slider
 */
function catchflames_slider_display() {
	global $post, $wp_query, $catchflames_options_settings;
   	$options = $catchflames_options_settings;

	// get data value from theme options
	$enableslider = $options[ 'enable_slider' ];
	$slidertype = $options[ 'select_slider_type' ];

	// Front page displays in Reading Settings
	$page_on_front = get_option('page_on_front') ;
	$page_for_posts = get_option('page_for_posts');

	// Get Page ID outside Loop
	$page_id = $wp_query->get_queried_object_id();

	if ( ( $enableslider == 'enable-slider-allpage' ) || ( ( is_front_page() || ( is_home() && $page_for_posts != $page_id ) ) && $enableslider == 'enable-slider-homepage' ) ) :
		// This function passes the value of slider effect to js file
		if ( function_exists( 'catchflames_pass_slider_value' ) ) : catchflames_pass_slider_value(); endif;

		if (  $slidertype == 'post-slider' ) {
			if ( !empty( $options[ 'featured_slider' ] ) && function_exists( 'catchflames_post_sliders' ) ) {
				catchflames_post_sliders();
			}
			else {
				echo '<p style="text-align: center">' . esc_attr__( 'You have selected Post Slider but you haven\'t added the Post ID in "Appearance => Theme Options => Featured Slider => Featured Post Slider Options"', 'catch-flames' ) . '</p>';
			}
		}
		elseif (  $slidertype == 'page-slider' ) {
			if ( !empty( $options[ 'featured_slider_page' ] ) && function_exists( 'catchflames_page_sliders' ) ) {
				catchflames_page_sliders();
			}
			else {
				echo '<p style="text-align: center">' . esc_attr__( 'You have selected Page Slider but you haven\'t added the Page ID in "Appearance => Theme Options => Featured Slider => Featured Page Slider Options"', 'catch-flames' ) . '</p>';
			}
		}
		elseif (  $slidertype == 'category-slider' ) {
			if ( !empty( $options[ 'slider_category' ] ) && function_exists( 'catchflames_category_sliders' ) ) {
				catchflames_category_sliders();
			}
			else {
				echo '<p style="text-align: center">' . esc_attr__( 'You have selected Category Slider but you haven\'t selected any categories in "Appearance => Theme Options => Featured Slider => Featured Category Slider Options"', 'catch-flames' ) . '</p>';
			}
		}
		elseif (  $slidertype == 'image-slider' ) {
			if ( !empty( $options[ 'featured_image_slider_image' ] ) && function_exists( 'catchflames_image_sliders' ) ) {
				catchflames_image_sliders();
			}
			else {
				echo '<p style="text-align: center">' . esc_attr__( 'You have selected Image Slider but you haven\'t uploaded any image in "Appearance => Theme Options => Featured Slider => Featured Image Slider Options"', 'catch-flames' ) . '</p>';
			}
		}
		else {
			catchflames_default_sliders();
		}
	endif;
}
endif; // catchflames_slider_display

add_action( 'catchflames_before_main', 'catchflames_slider_display', 40 );