<?php
/*
Template Name: Following
*/
get_header();

if ( function_exists( 'gp_list_follow_items' ) ) {
	gp_list_follow_items();
}

?>

<?php gp_page_header( get_the_ID() ); ?>

<div id="content-wrapper"<?php if ( $GLOBALS['gp_layout'] != 'gp-fullwidth' ) { ?> class="gp-container"<?php } ?>>

	<div id="content">
	
		<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>	

			<?php if ( isset( $GLOBALS['gp_hub_header'] ) && $GLOBALS['gp_hub_header'] == true ) { ?>
				<header class="entry-header small-header">
					<h2 class="entry-title"><?php echo gp_prefix_hub_title( get_the_ID() ); ?></h2>
				</header>
			<?php } ?>		
		
			<?php the_content(); ?>
		
		<?php endwhile; endif; rewind_posts(); ?>	

		<?php if ( isset( $following_page_ids ) && $following_page_ids ) {

			gp_query_variables();

			// Option names
			$following_page_ids = array_reverse( $following_page_ids );
	
			$post_id = '';
			
			$args = array(
				'post_status' => 'publish',
				'post_type'   => 'page', 
				'post__in'    => $following_page_ids,
				'orderby'     => 'post__in',
				'order'       => 'desc',
				'per_page'    => $GLOBALS['gp_per_page'],
				'paged'       => $GLOBALS['gp_paged'],
			);

			$gp_query = new wp_query( $args ); ?>	

			<div class="blog-wrapper following-wrapper <?php echo sanitize_html_class( $GLOBALS['gp_format'] ); ?> <?php echo sanitize_html_class( $GLOBALS['gp_size'] ); ?>">

				<?php if ( $gp_query->have_posts() ) : ?>
				
					<?php gp_cookie_warning(); ?>
					
					<div class="inner-loop wpfp-span">

						<?php while ( $gp_query->have_posts() ) : $gp_query->the_post(); 
						
						// Hub ID
						$follow_page_id = get_the_ID();
						
						// Display modified items
						if ( $gp['following_hub_items_modified'] == 'enabled' ) {
							$modified = array( 'column' => 'post_modified_gmt', 'after' =>  $gp['following_hub_items_days_ago'] . ' days ago' );
						} else {
							$modified = '';
						}
						
						?>

							<?php get_template_part( 'review', 'loop' ); ?>
							
							<?php
								
							// Hub posts and child pages that have been created in the last X days
							$args = array(
								'post_status' => 'publish',
								'post_type'   => $gp['following_hub_items_post_types'],
								//'post__not_in' => $follow_page_id,
								'posts_per_page' => $gp['following_hub_items_per_page'],
								'paged' => 1,
								'orderby'     => 'meta_value',
								'meta_query'  => array( 
									'relation' => 'OR',
									array( 'key' => 'post_association', 'value' => $follow_page_id, 'compare' => 'LIKE' ),
									array( 'key' => '_hub_page_id', 'value' => $follow_page_id, 'compare' => 'LIKE' ),
									array( 'key' => '_wp_page_template', 'value' => 'review-template.php', 'compare' => '=' ),
								),
								//'meta_key '   => 'post_association',	
								'date_query' => array(
									'relation' => 'OR',
									array(
										'column' => 'post_date_gmt', 'after' =>  $gp['following_hub_items_days_ago'] . ' days ago'
									), 
										$modified
									),	
							);

							$gp_i_query = new wp_query( $args ); ?>	
		
							<?php if ( $gp_i_query->have_posts() ) : ?>
							
								<div class="followed-content">
								
									<div class="last-updated-title"><?php _e( 'Last ' . $gp['following_hub_items_per_page'] . ' updates from the last ' . $gp['following_hub_items_days_ago'] . ' days', 'gp_lang' ); ?></div>
								
									<?php while ( $gp_i_query->have_posts() ) : $gp_i_query->the_post(); 
								
										// Display if a post or child of hub page
										if ( $post->post_type == 'post' OR $post->post_type == 'gp_user_review' OR ( $follow_page_id == $post->post_parent ) ) { ?>
								
											<article <?php post_class( 'post-item' ); ?>>

												<div class="loop-header">
													<h3 class="loop-title"><a href="<?php the_permalink(); ?>" title="<?php echo gp_prefix_hub_title( get_the_ID() ); ?>"><?php echo gp_prefix_hub_title( get_the_ID() ); ?></a></h3>					
												</div>
																										
												<div class="entry-meta">	
													<span class="meta-date"><?php the_time( get_option( 'date_format' ) ); ?><?php if ( $gp['following_hub_items_modified'] == 'enabled' && ( get_the_modified_date( get_option( 'date_format' ) ) != get_the_time( get_option( 'date_format' ) ) ) ) { ?> (<?php _e( 'Updated on' , 'gp_lang' ); ?> <?php the_modified_date( get_option( 'date_format' ) ); ?>)<?php } ?></span>
												</div>
											
											</article>	
									
										<?php } ?>
								
									<?php endwhile; ?>
								
								</div>	
								
							<?php endif; wp_reset_postdata(); ?>

						<?php endwhile; ?>

					</div>
				
					<?php echo gp_clear_list_link(); ?>

					<?php echo gp_pagination( $gp_query->max_num_pages ); ?>

				<?php else : ?>

					<strong class="no-items-found"><?php _e( 'No items found.', 'gp_lang' ); ?></strong>

				<?php endif; wp_reset_postdata(); ?>
			
			</div><!-- END BLOG WRAPPER -->

		<?php } else { ?>
		
			<?php if ( $gp['hub_following_items'] == 'both' OR ( is_user_logged_in() && $gp['hub_following_items'] == 'members' ) ) { ?>
			
				<strong class="no-items-found"><?php _e( 'You are not currently following any items.', 'gp_lang' ); ?></strong>
			
			<?php } else { ?>
			
				<strong class="no-items-found"><?php _e( 'Please login to follow items.', 'gp_lang' ); ?></strong>
			
			<?php } ?>
			
		<?php } ?>
	
	</div><!-- END CONTENT -->

	<?php get_sidebar(); ?>
	
</div><!-- END CONTENT WRAPPER -->

<?php get_footer(); ?>