<?php
/**
 * The template for displaying the Grid Content
 *
 * @package Catch Themes
 * @subpackage Gridalicious
 * @since Gridalicious 1.0
 */


if ( !function_exists( 'gridalicious_featured_grid_content' ) ) :
/**
 * Add grid_content.
 *
 * @uses action hook gridalicious_before_content.
 *
 * @since Gridalicious 1.0
 */
function gridalicious_featured_grid_content() {
	global $wp_query;
	//gridalicious_flush_transients();

	// get data value from options
	$options        = gridalicious_get_theme_options();
	$enable         = $options['featured_grid_content_option'];
	$content_select = $options['featured_grid_content_type'];
	$layout         = $options['featured_grid_content_layout'];

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

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

	if ( 'entire-site' == $enable  || ( ( is_front_page() || ( is_home() && $page_for_posts != $page_id ) ) && 'homepage' == $enable  ) ) {
		if ( ( !$gridalicious_featured_grid_content = get_transient( 'gridalicious_featured_grid_content' ) ) ) {
			echo '<!-- refreshing cache -->';

			$gridalicious_featured_grid_content = '
				<section id="featured-grid-content" class="'. esc_attr( $content_select . ' ' . $layout ) .'">
					<div class="wrapper">';
							// Select Grid Content
							if ( 'demo-featured-grid-content' == $content_select  && function_exists( 'gridalicious_demo_grid_content' ) ) {
								$gridalicious_featured_grid_content .=  gridalicious_demo_grid_content();
							}
							else if ( 'featured-post-grid-content' == $content_select  && function_exists( 'gridalicious_post_grid_content' ) ) {
								$gridalicious_featured_grid_content .=  gridalicious_post_grid_content( $options );
							}
							elseif ( 'featured-page-grid-content' == $content_select  && function_exists( 'gridalicious_page_grid_content' ) ) {
								$gridalicious_featured_grid_content .=  gridalicious_page_grid_content( $options );
							}
							elseif ( 'featured-category-grid-content' == $content_select  && function_exists( 'gridalicious_category_grid_content' ) ) {
								$gridalicious_featured_grid_content .=  gridalicious_category_grid_content( $options );
							}
							elseif ( 'featured-image-grid-content' == $content_select  && function_exists( 'gridalicious_image_grid_content' ) ) {
								$gridalicious_featured_grid_content .=  gridalicious_image_grid_content( $options );
							}

			$gridalicious_featured_grid_content .= '
					</div><!-- .wrapper -->
				</section><!-- #feature-grid-content -->';

			set_transient( 'gridalicious_featured_grid_content', $gridalicious_featured_grid_content, 86940 );
		}
		echo $gridalicious_featured_grid_content;
	}
}
endif;
add_action( 'gridalicious_before_content', 'gridalicious_featured_grid_content', 10 );


if ( ! function_exists( 'gridalicious_demo_grid_content' ) ) :
/**
 * This function to display featured demo grid_content
 *
 * @since Gridalicious 1.0
 *
 */
function gridalicious_demo_grid_content() {
	return '
	<a class="grid-box first" title="Grid Content Image 1" href="'. esc_url( home_url( '/' ) ) .'">
		<img src="' . trailingslashit( esc_url ( get_template_directory_uri() ) ) . 'images/gallery/grid-800x450.jpg" class="wp-post-image" alt="Grid Content Image 1" title="Grid Content Image 1">
		<div class="caption">
			<span class="vcenter">
				<span class="entry-title">
					Grid Content Image 1
				</span>
				<span class="more">
					Read More ...
				</span>
			</span><!-- .vcenter -->
		</div><!-- .caption -->
	</a><!-- .grid-box -->

	<a class="grid-box" title="Grid Content Image 2" href="'. esc_url( home_url( '/' ) ) .'">
		<img src="' . trailingslashit( esc_url ( get_template_directory_uri() ) ) . 'images/gallery/grid-800x450-2.jpg" class="wp-post-image" alt="Grid Content Image 2" title="Grid Content Image 2">
		<div class="caption">
			<span class="vcenter">
				<span class="entry-title">
					Grid Content Image 2
				</span>
				<span class="more">
					Read More ...
				</span>
			</span><!-- .vcenter -->
		</div><!-- .caption -->
	</a><!-- .grid-box -->

	<a class="grid-box" title="Grid Content Image 3" href="'. esc_url( home_url( '/' ) ) .'">
		<img src="' . trailingslashit( esc_url ( get_template_directory_uri() ) ) . 'images/gallery/grid-800x450-3.jpg" class="wp-post-image" alt="Grid Content Image 3" title="Grid Content Image 3">
		<div class="caption">
			<span class="vcenter">
				<span class="entry-title">
					Grid Content Image 3
				</span>
				<span class="more">
					Read More ...
				</span>
			</span><!-- .vcenter -->
		</div><!-- .caption -->
	</a><!-- .grid-box -->';
}
endif; // gridalicious_demo_grid_content


if ( ! function_exists( 'gridalicious_post_grid_content' ) ) :
/**
 * This function to display featured posts grid_content
 *
 * @param $options: gridalicious_theme_options from customizer
 *
 * @since Gridalicious 1.0
 */
function gridalicious_post_grid_content( $options ) {
	$quantity	= $options['featured_grid_content_number'];

	global $post;

	$output = '';

	$number_of_post = 0; 		// for number of posts

	$post_list		= array();	// list of valid post ids

	//Get valid post ids
	for( $i = 1; $i <= $quantity; $i++ ){
		if ( isset ( $options['featured_grid_content_post_' . $i] ) && $options['featured_grid_content_post_' . $i] > 0 ){
			$number_of_post++;

			$post_list	=	array_merge( $post_list, array( $options['featured_grid_content_post_' . $i] ) );
		}

	}

	if ( !empty( $post_list ) && $number_of_post > 0 ) {
		$output = '';

		$loop = new WP_Query( array(
			'posts_per_page' => $number_of_post,
			'post__in'       => $post_list,
			'orderby'        => 'post__in',
			'ignore_sticky_posts' => 1 // ignore sticky posts
		));

		$i=1;

		while ( $loop->have_posts() ) {

			$loop->the_post();

			$title_attribute = the_title_attribute( 'echo=0' );

			$classes = 'post postid-' . $post->ID;

			if ( 1 == $i ) {
				$classes .= ' first';
			}
			else if ( 1 == $i%3 ) {
				$classes .= ' first-cols';
			}

			$output .=
			'<a class="grid-box '. $classes .'" title="' . the_title_attribute( array( 'before' => __( 'Permalink to:', 'gridalicious-pro' ), 'echo' => false ) ) . '" href="' . esc_url( get_permalink() ) . '">';

			$layout = $options['featured_grid_content_layout'];

			$thumbnail = 'gridalicious-featured-content';

			if ( 1 === $i ) {
				$thumbnail = 'gridalicious-featured-grid';
			}

			if ( 'layout-one' === $layout ) {
				$thumbnail = 'gridalicious-featured-header';
			} elseif ( 'layout-two' === $layout ) {
				$thumbnail = 'gridalicious-featured-grid';
			}


			if ( has_post_thumbnail() ) {
				$output .= get_the_post_thumbnail( $post->ID, $thumbnail, array( 'title' => $title_attribute, 'alt' => $title_attribute ) );
			}
			else {
				//Default value if there is no first image
				$gridalicious_image =
					'<img class="no-image" src="'.esc_url( get_template_directory_uri() ).'/images/gallery/no-featured-image-800x450.jpg" />';

				//Get the first image in page, returns false if there is no image
				$gridalicious_first_image = gridalicious_get_first_image( $post->ID, $thumbnail, array( 'title' => $title_attribute, 'alt' => $title_attribute ) );

				//Set value of image as first image if there is an image present in the post
				if ( '' != $gridalicious_first_image ) {
					$gridalicious_image =	$gridalicious_first_image;
				}

				$output .= $gridalicious_image;
			}

			$output .= '
				<div class="caption">
					<span class="vcenter">
						<span class="entry-title">
							' . the_title('', '', false) . '
						</span>
						<span class="more">';

						$output .=  $options['excerpt_more_text'];

				$output .= '
						</span><!-- .more -->
					</span><!-- .vcenter -->
				</div><!-- .caption -->
			</a><!-- .grid-box -->';

			$i++;
		}

		wp_reset_postdata();
	}

	return $output;
}
endif; // gridalicious_post_grid_content


if ( ! function_exists( 'gridalicious_page_grid_content' ) ) :
/**
 * This function to display featured page grid_content
 *
 * @param $options: gridalicious_theme_options from customizer
 *
 * @since Gridalicious 1.0
 */
function gridalicious_page_grid_content( $options ) {
	$quantity		= $options['featured_grid_content_number'];

	global $post;

	$output = '';
	$number_of_page 		= 0; 		// for number of pages
	$page_list				= array();	// list of valid page ids

	//Get number of valid pages
	for( $i = 1; $i <= $quantity; $i++ ){
		if ( isset ( $options['featured_grid_content_page_' . $i] ) && $options['featured_grid_content_page_' . $i] > 0 ){
			$number_of_page++;

			$page_list	=	array_merge( $page_list, array( $options['featured_grid_content_page_' . $i] ) );
		}

	}

	if ( !empty( $page_list ) && $number_of_page > 0 ) {
		$loop = new WP_Query( array(
			'posts_per_page'	=> $quantity,
			'post_type'			=> 'page',
			'post__in'			=> $page_list,
			'orderby' 			=> 'post__in'
		));

		$i=1;

		while ( $loop->have_posts() ) {

			$loop->the_post();

			$title_attribute = the_title_attribute( 'echo=0' );

			$classes = 'page pageid-' . $post->ID;

			if ( 1 == $i ) {
				$classes .= ' first';
			}
			else if ( 1 == $i%3 ) {
				$classes .= ' first-cols';
			}

			$output .=
			'<a class="grid-box '. $classes .'" title="' . the_title_attribute( array( 'before' => __( 'Permalink to:', 'gridalicious-pro' ), 'echo' => false ) ) . '" href="' . esc_url( get_permalink() ) . '">';

			$layout = $options['featured_grid_content_layout'];

			$thumbnail = 'gridalicious-featured-content';

			if ( 1 === $i ) {
				$thumbnail = 'gridalicious-featured-grid';
			}

			if ( 'layout-one' === $layout ) {
				$thumbnail = 'gridalicious-featured-header';
			} elseif ( 'layout-two' === $layout ) {
				$thumbnail = 'gridalicious-featured-grid';
			}


			if ( has_post_thumbnail() ) {
				$output .= get_the_post_thumbnail( $post->ID, $thumbnail, array( 'title' => $title_attribute, 'alt' => $title_attribute ) );
			}
			else {
				//Default value if there is no first image
				$gridalicious_image =
					'<img class="no-image" src="'.esc_url( get_template_directory_uri() ).'/images/gallery/no-featured-image-800x450.jpg" />';

				//Get the first image in page, returns false if there is no image
				$gridalicious_first_image = gridalicious_get_first_image( $post->ID, $thumbnail, array( 'title' => $title_attribute, 'alt' => $title_attribute ) );

				//Set value of image as first image if there is an image present in the post
				if ( '' != $gridalicious_first_image ) {
					$gridalicious_image =	$gridalicious_first_image;
				}

				$output .= $gridalicious_image;
			}

			$output .= '
				<div class="caption">
					<span class="vcenter">
						<span class="entry-title">
							' . the_title('', '', false) . '
						</span>
						<span class="more">';

						$output .=  $options['excerpt_more_text'];

				$output .= '
						</span><!-- .more -->
					</span><!-- .vcenter -->
				</div><!-- .caption -->
			</a><!-- .grid-box -->';

			$i++;
		}

		wp_reset_postdata();
	}

	return $output;
}
endif; // gridalicious_page_grid_content


if ( ! function_exists( 'gridalicious_category_grid_content' ) ) :
/**
 * This function to display featured category grid_content
 *
 * @param $options: gridalicious_theme_options from customizer
 *
 * @since Gridalicious 1.0
 */
function gridalicious_category_grid_content( $options ) {
	$quantity	= $options['featured_grid_content_number'];

	global $post;

	$output = '';

	$category_list	= $options['featured_grid_content_select_category'];

	$loop = new WP_Query( array(
		'posts_per_page'		=> $quantity,
		'category__in'			=> $category_list,
		'ignore_sticky_posts' 	=> 1 // ignore sticky posts
	));

	$i=1;

	while ( $loop->have_posts() ) {

		$loop->the_post();

		$title_attribute = the_title_attribute( 'echo=0' );

		$classes = 'page pageid-' . $post->ID;

		if ( 1 == $i ) {
			$classes .= ' first';
		}
		else if ( 1 == $i%3 ) {
			$classes .= ' first-cols';
		}

		$output .=
			'<a class="grid-box '. $classes .'" title="' . the_title_attribute( array( 'before' => __( 'Permalink to:', 'gridalicious-pro' ), 'echo' => false ) ) . '" href="' . esc_url( get_permalink() ) . '">';

		$layout = $options['featured_grid_content_layout'];

		$thumbnail = 'gridalicious-featured-content';

		if ( 1 === $i ) {
			$thumbnail = 'gridalicious-featured-grid';
		}

		if ( 'layout-one' === $layout ) {
			$thumbnail = 'gridalicious-featured-header';
		} elseif ( 'layout-two' === $layout ) {
			$thumbnail = 'gridalicious-featured-grid';
		}


		if ( has_post_thumbnail() ) {
			$output .= get_the_post_thumbnail( $post->ID, $thumbnail, array( 'title' => $title_attribute, 'alt' => $title_attribute ) );
		}
		else {
			//Default value if there is no first image
			$gridalicious_image =
				'<img class="no-image" src="'.esc_url( get_template_directory_uri() ).'/images/gallery/no-featured-image-800x450.jpg" />';

			//Get the first image in page, returns false if there is no image
			$gridalicious_first_image = gridalicious_get_first_image( $post->ID, $thumbnail, array( 'title' => $title_attribute, 'alt' => $title_attribute ) );

			//Set value of image as first image if there is an image present in the post
			if ( '' != $gridalicious_first_image ) {
				$gridalicious_image =	$gridalicious_first_image;
			}

			$output .= $gridalicious_image;
		}

		$output .= '
			<div class="caption">
				<span class="vcenter">
					<span class="entry-title">
						' . the_title('', '', false) . '
					</span>
					<span class="more">';
						$output .=  $options['excerpt_more_text'];

			$output .= '
					</span><!-- .more -->
				</span><!-- .vcenter -->
			</div><!-- .caption -->
		</a><!-- .grid-box -->';

		$i++;
	}
	wp_reset_postdata();

	return $output;
}
endif; // gridalicious_category_grid_content


if ( ! function_exists( 'gridalicious_image_grid_content' ) ) :
/**
 * This function to display featured posts grid_content
 *
 * @get the data value from theme options
 * @displays on the index
 *
 * @useage Featured Image, Title and Excerpt of Post
 *
 */
function gridalicious_image_grid_content( $options ) {
	$quantity	= $options['featured_grid_content_number'];

	$gridalicious_image_grid_content = '';

	for ( $i = 1 ; $i <= $quantity; $i++ ) {

		$classes = '';

		if ( 1 == $i ) {
			$classes .= ' first';
		}
		else if ( 1 == $i%3 ) {
			$classes .= ' first-cols';
		}

		//Check Image Not Empty to add in the grid-content
		if ( !empty ( $options[ 'featured_grid_content_image_'. $i ] ) ) {

			//Checking Link Target
			if ( !empty ( $options[ 'featured_target_'. $i ] ) ) {
				$target = '_blank';
			} else {
				$target = '_self';
			}

			//Checking Title
			if ( !empty ( $options[ 'featured_title_'. $i ] ) ) {
				$imagetitle = $options[ 'featured_title_'. $i ];
			}
			else {
				$imagetitle = 'Featured Grid Content-'.$i;
			}

			//Checking Link
			if ( !empty ( $options[ 'featured_link_'. $i ] ) ) {
				$link = $options[ 'featured_link_'. $i ];
			}
			else {
				$link = '';
			}

			$image_id = gridalicious_get_image_id( $options['featured_grid_content_image_'. $i] );

			if ( 1 == $i ) {
				$image_attributes = wp_get_attachment_image_src( $image_id, 'gridalicious-featured-grid');
			}
			else{
				$image_attributes = wp_get_attachment_image_src( $image_id, 'gridalicious-featured-content');
			}

			// retrieve the thumbnail size of our image
			$image = '<img title="'. esc_attr( $imagetitle ).'" alt="'. esc_attr( $imagetitle ) .'" class="pngfix wp-post-image" src="'. esc_url( $image_attributes[0] ).'" />';

			//Checking Content
			if ( !empty ( $options['featured_title_'. $i] ) ) {
				$content_title = $options['featured_title_'. $i];
			}
			else {
				$content_title = '';
			}

			if ( '' == $link ) {
				$gridalicious_image_grid_content .= '
									<div class="grid-box '. $classes .'">'
										. $image . '
										<div class="caption">
											<span class="vcenter">';

				if ( '' != $content_title ) {
					$gridalicious_image_grid_content .= '<span class="entry-title">'. esc_attr( $content_title ) .'</span>';
				}

				$gridalicious_image_grid_content .= '
												</span><!-- .vcenter -->
										</div><!-- .caption -->
									</div><!-- .grid-box -->';
			}
			else {
				$gridalicious_image_grid_content .= '
									<a class="grid-box '. $classes .'" title="'. esc_attr( $content_title ) .'" href="'. esc_url( $link ) .'" target="'. $target .'">'
										. $image . '
										<div class="caption">
											<span class="vcenter">';

				if ( '' != $content_title ) {
					$gridalicious_image_grid_content .= '<span class="entry-title">'. esc_attr( $content_title ) .'</span>';
				}

				$gridalicious_image_grid_content .= '
												<span class="more">
													'. esc_attr( $options['excerpt_more_text'] ) .'
												</span>
												</span><!-- .vcenter -->
										</div><!-- .caption -->
									</a><!-- .grid-box -->';
			}
		}

	}
	return $gridalicious_image_grid_content;
}
endif; //gridalicious_image_grid_content
