<?php 

if ( ! function_exists( 'gp_data_properties' ) ) {
	function gp_data_properties( $type ) {

		// Check to see if options exists	
		$GLOBALS['gp_ajax_post_parent_id'] = ! empty( $GLOBALS['gp_post_parent_id'] ) ? $GLOBALS['gp_post_parent_id'] : '';
		$GLOBALS['gp_ajax_cats'] = ! empty( $GLOBALS['gp_cats'] ) ? $GLOBALS['gp_cats'] : '';		
		$GLOBALS['gp_ajax_post_types'] = ! empty( $GLOBALS['gp_post_types'] ) ? $GLOBALS['gp_post_types'] : '';		
		$GLOBALS['gp_ajax_format'] = ! empty( $GLOBALS['gp_format'] ) ? $GLOBALS['gp_format'] : '';	
		$GLOBALS['gp_ajax_size'] = ! empty( $GLOBALS['gp_size'] ) ? $GLOBALS['gp_size'] : '';		
		$GLOBALS['gp_ajax_orderby'] = ! empty( $GLOBALS['gp_orderby'] ) ? $GLOBALS['gp_orderby'] : '';
		$GLOBALS['gp_ajax_date_posted'] = ! empty( $GLOBALS['gp_date_posted'] ) ? $GLOBALS['gp_date_posted'] : '';
		$GLOBALS['gp_ajax_date_modified'] = ! empty( $GLOBALS['gp_date_modified'] ) ? $GLOBALS['gp_date_modified'] : '';
		$GLOBALS['gp_ajax_per_page'] = ! empty( $GLOBALS['gp_per_page'] ) ? $GLOBALS['gp_per_page'] : '';
		$GLOBALS['gp_ajax_offset'] = ! empty( $GLOBALS['gp_offset'] ) ? $GLOBALS['gp_offset'] : 0;
		$GLOBALS['gp_ajax_featured_image'] = ! empty( $GLOBALS['gp_featured_image'] ) ? $GLOBALS['gp_featured_image'] : '';
		$GLOBALS['gp_ajax_image_width'] = ! empty( $GLOBALS['gp_image_width'] ) ? $GLOBALS['gp_image_width'] : '';
		$GLOBALS['gp_ajax_image_height'] = ! empty( $GLOBALS['gp_image_height'] ) ? $GLOBALS['gp_image_height'] : '';
		$GLOBALS['gp_ajax_hard_crop'] = ! empty( $GLOBALS['gp_hard_crop'] ) ? $GLOBALS['gp_hard_crop'] : '';
		$GLOBALS['gp_ajax_image_alignment'] = ! empty( $GLOBALS['gp_image_alignment'] ) ? $GLOBALS['gp_image_alignment'] : '';
		$GLOBALS['gp_ajax_title_position'] = ! empty( $GLOBALS['gp_title_position'] ) ? $GLOBALS['gp_title_position'] : '';
		$GLOBALS['gp_ajax_content_display'] = ! empty( $GLOBALS['gp_content_display'] ) ? $GLOBALS['gp_content_display'] : '';
		$GLOBALS['gp_ajax_excerpt_length'] = ! empty( $GLOBALS['gp_excerpt_length'] ) ? $GLOBALS['gp_excerpt_length'] : 0;
		$GLOBALS['gp_ajax_meta_author'] = ! empty( $GLOBALS['gp_meta_author'] ) ? $GLOBALS['gp_meta_author'] : '';
		$GLOBALS['gp_ajax_meta_date'] = ! empty( $GLOBALS['gp_meta_date'] ) ? $GLOBALS['gp_meta_date'] : '';
		$GLOBALS['gp_ajax_meta_comment_count'] = ! empty( $GLOBALS['gp_meta_comment_count'] ) ? $GLOBALS['gp_meta_comment_count'] : '';
		$GLOBALS['gp_ajax_meta_views'] = ! empty( $GLOBALS['gp_meta_views'] ) ? $GLOBALS['gp_meta_views'] : '';
		$GLOBALS['gp_ajax_meta_followers'] = ! empty( $GLOBALS['gp_meta_followers'] ) ? $GLOBALS['gp_meta_followers'] : '';
		$GLOBALS['gp_ajax_meta_cats'] = ! empty( $GLOBALS['gp_meta_cats'] ) ? $GLOBALS['gp_meta_cats'] : '';
		$GLOBALS['gp_ajax_meta_tags'] = ! empty( $GLOBALS['gp_meta_tags'] ) ? $GLOBALS['gp_meta_tags'] : '';
		$GLOBALS['gp_ajax_meta_hub_cats'] = ! empty( $GLOBALS['gp_meta_hub_cats'] ) ? $GLOBALS['gp_meta_hub_cats'] : '';
		$GLOBALS['gp_ajax_meta_hub_fields'] = ! empty( $GLOBALS['gp_meta_hub_fields'] ) ? $GLOBALS['gp_meta_hub_fields'] : '';
		$GLOBALS['gp_ajax_meta_hub_award'] = ! empty( $GLOBALS['gp_meta_hub_award'] ) ? $GLOBALS['gp_meta_hub_award'] : '';
		$GLOBALS['gp_ajax_hub_cats_selected'] = ! empty( $GLOBALS['gp_hub_cats_selected'] ) ? implode( ',', $GLOBALS['gp_hub_cats_selected'] ) : '';
		$GLOBALS['gp_ajax_hub_fields'] = ! empty( $GLOBALS['gp_hub_fields'] ) ? implode( ',', $GLOBALS['gp_hub_fields'] ) : '';
		$GLOBALS['gp_ajax_display_site_rating'] = ! empty( $GLOBALS['gp_display_site_rating'] ) ? $GLOBALS['gp_display_site_rating'] : '';
		$GLOBALS['gp_ajax_display_user_rating'] = ! empty( $GLOBALS['gp_display_user_rating'] ) ? $GLOBALS['gp_display_user_rating'] : '';
		$GLOBALS['gp_ajax_read_more_link'] = ! empty( $GLOBALS['gp_read_more_link'] ) ? $GLOBALS['gp_read_more_link'] : '';
		$GLOBALS['gp_ajax_page_arrows'] = ! empty( $GLOBALS['gp_page_arrows'] ) ? $GLOBALS['gp_page_arrows'] : '';
		$GLOBALS['gp_ajax_page_numbers'] = ! empty( $GLOBALS['gp_page_numbers'] ) ? $GLOBALS['gp_page_numbers'] : '';
		$GLOBALS['gp_ajax_author_id'] = ! empty( $GLOBALS['gp_author_id'] ) ? $GLOBALS['gp_author_id'] : '';	
	 
		// Add to blog wrappers to pull query data 
		return ' data-type="' . $type . '" data-postparentid="' . $GLOBALS['gp_ajax_post_parent_id'] . '" data-cats="' . $GLOBALS['gp_ajax_cats'] . '" data-posttypes="' . $GLOBALS['gp_ajax_post_types'] . '" data-format="' . $GLOBALS['gp_ajax_format'] . '" data-size="' . $GLOBALS['gp_ajax_size'] . '" data-orderby="' . $GLOBALS['gp_ajax_orderby'] . '" data-dateposted="' . $GLOBALS['gp_ajax_date_posted'] . '" data-datemodified="' . $GLOBALS['gp_ajax_date_modified'] . '" data-perpage="' . $GLOBALS['gp_ajax_per_page'] . '" data-offset="' . $GLOBALS['gp_ajax_offset'] . '"  data-featuredimage="' . $GLOBALS['gp_ajax_featured_image'] . '" data-imagewidth="' . $GLOBALS['gp_ajax_image_width'] . '" data-imageheight="' . $GLOBALS['gp_ajax_image_height'] . '" data-hardcrop="' . $GLOBALS['gp_ajax_hard_crop'] . '" data-imagealignment="' . $GLOBALS['gp_ajax_image_alignment'] . '" data-titleposition="' . $GLOBALS['gp_ajax_title_position'] . '" data-contentdisplay="' . $GLOBALS['gp_ajax_content_display'] . '" data-excerptlength="' . $GLOBALS['gp_ajax_excerpt_length'] . '" data-metaauthor="' . $GLOBALS['gp_ajax_meta_author'] . '" data-metadate="' . $GLOBALS['gp_ajax_meta_date'] . '" data-metacommentcount="' . $GLOBALS['gp_ajax_meta_comment_count'] . '" data-metaviews="' . $GLOBALS['gp_ajax_meta_views'] . '" data-metafollowers="' . $GLOBALS['gp_ajax_meta_followers'] . '" data-metacats="' . $GLOBALS['gp_ajax_meta_cats'] . '" data-metatags="' . $GLOBALS['gp_ajax_meta_tags'] . '" data-metahubcats="' . $GLOBALS['gp_ajax_meta_hub_cats'] . '" data-metahubfields="' . $GLOBALS['gp_ajax_meta_hub_fields'] . '" data-metahubaward="' . $GLOBALS['gp_ajax_meta_hub_award'] . '" data-hubcats="' . $GLOBALS['gp_ajax_hub_cats_selected'] . '" data-hubfields="' . $GLOBALS['gp_ajax_hub_fields'] . '" data-displaysiterating="' . $GLOBALS['gp_ajax_display_site_rating'] . '" data-displayuserrating="' . $GLOBALS['gp_ajax_display_user_rating'] . '" data-readmorelink="' . $GLOBALS['gp_ajax_read_more_link'] . '"  data-pagenumbers="' . $GLOBALS['gp_ajax_page_numbers'] . '" data-authorid="' . $GLOBALS['gp_ajax_author_id'] . '"';

	}
}
 
if ( ! function_exists( 'gp_register_ajax' ) ) {
	function gp_register_ajax() {
	
		global $query_string, $post;

		wp_enqueue_style( 'wp-mediaelement' );
		wp_enqueue_script( 'wp-mediaelement' );
		wp_enqueue_script( 'ajax-loop', gp_scripts_uri . 'ajax-loop.js', array( 'jquery' ) );
		wp_localize_script( 'ajax-loop', 'gpAjax', array(
			'ajaxurl' => admin_url( 'admin-ajax.php' ),
			'ajaxnonce' => wp_create_nonce( 'gp-ajax-nonce' ),
			'querystring' => $query_string,
		) ); 
		
	}
}
add_action( 'wp_enqueue_scripts', 'gp_register_ajax' );

if ( ! function_exists( 'gp_ajax' ) ) {
	function gp_ajax() {
	
		global $gp;
	
		if ( ! wp_verify_nonce( $_GET['ajaxnonce'], 'gp-ajax-nonce' ) )
			die();
	
		// Pagination
		$pagination = ( isset( $_GET['pagenumber'] )  ) ? $_GET['pagenumber'] : 0;
				
		// Get theme options from ajax values
		$GLOBALS['gp_cats'] = isset( $_GET['cats'] ) ? $_GET['cats'] : '';		
		$GLOBALS['gp_post_types'] = isset( $_GET['posttypes'] ) ? $_GET['posttypes'] : '';		
		$GLOBALS['gp_format'] = isset( $_GET['format'] ) ? $_GET['format'] : '';	
		$GLOBALS['gp_size'] = isset( $_GET['size'] ) ? $_GET['size'] : '';		
		$GLOBALS['gp_orderby'] = isset( $_GET['orderby'] ) ? $_GET['orderby'] : '';
		$GLOBALS['gp_date_posted'] = isset( $_GET['dateposted'] ) ? $_GET['dateposted'] : '';
		$GLOBALS['gp_date_modified'] = isset( $_GET['datemodified'] ) ? $_GET['datemodified'] : '';
		$GLOBALS['gp_per_page'] = isset( $_GET['perpage'] ) ? $_GET['perpage'] : '';
		$GLOBALS['gp_offset'] = isset( $_GET['offset'] ) ? $_GET['offset'] : '';
		$GLOBALS['gp_featured_image'] = isset( $_GET['featuredimage'] ) ? $_GET['featuredimage'] : '';
		$GLOBALS['gp_image_width'] = isset( $_GET['imagewidth'] ) ? $_GET['imagewidth'] : '';
		$GLOBALS['gp_image_height'] = isset( $_GET['imageheight'] ) ? $_GET['imageheight'] : '';
		$GLOBALS['gp_hard_crop'] = isset( $_GET['hardcrop'] ) ? $_GET['hardcrop'] : '';
		$GLOBALS['gp_image_alignment'] = isset( $_GET['imagealignment'] ) ? $_GET['imagealignment'] : '';
		$GLOBALS['gp_title_position'] = isset( $_GET['titleposition'] ) ? $_GET['titleposition'] : '';
		$GLOBALS['gp_content_display'] = isset( $_GET['contentdisplay'] ) ? $_GET['contentdisplay'] : '';
		$GLOBALS['gp_excerpt_length'] = isset( $_GET['excerptlength'] ) ? $_GET['excerptlength'] : '0';
		$GLOBALS['gp_meta_author'] = isset( $_GET['metaauthor'] ) ? $_GET['metaauthor'] : '';
		$GLOBALS['gp_meta_date'] = isset( $_GET['metadate'] ) ? $_GET['metadate'] : '';
		$GLOBALS['gp_meta_comment_count'] = isset( $_GET['metacommentcount'] ) ? $_GET['metacommentcount'] : '';
		$GLOBALS['gp_meta_views'] = isset( $_GET['metaviews'] ) ? $_GET['metaviews'] : '';
		$GLOBALS['gp_meta_followers'] = isset( $_GET['metafollowers'] ) ? $_GET['metafollowers'] : '';
		$GLOBALS['gp_meta_cats'] = isset( $_GET['metacats'] ) ? $_GET['metacats'] : '';
		$GLOBALS['gp_meta_tags'] = isset( $_GET['metatags'] ) ? $_GET['metatags'] : '';
		$GLOBALS['gp_meta_hub_cats'] = isset( $_GET['metahubcats'] ) ? $_GET['metahubcats'] : '';
		$GLOBALS['gp_meta_hub_fields'] = isset( $_GET['metahubfields'] ) ? $_GET['metahubfields'] : '';
		$GLOBALS['gp_meta_hub_award'] = isset( $_GET['metahubaward'] ) ? $_GET['metahubaward'] : '';
		$GLOBALS['gp_hub_cats_selected'] = isset( $_GET['hubcats'] ) ? explode( ',', $_GET['hubcats'] ) : '';
		$GLOBALS['gp_hub_fields'] = isset( $_GET['hubfields'] ) ? explode( ',', $_GET['hubfields'] ) : '';	
		$GLOBALS['gp_display_site_rating'] = isset( $_GET['displaysiterating'] ) ? $_GET['displaysiterating'] : '';
		$GLOBALS['gp_display_user_rating'] = isset( $_GET['displayuserrating'] ) ? $_GET['displayuserrating'] : '';
		$GLOBALS['gp_read_more_link'] = isset( $_GET['readmorelink'] ) ? $_GET['readmorelink'] : '';
		$GLOBALS['gp_page_numbers'] = isset( $_GET['pagenumbers'] ) ? $_GET['pagenumbers'] : '';
		$GLOBALS['gp_author_id'] = isset( $_GET['authorid'] ) ? $_GET['authorid'] : '';

		// Post Type
		if ( $_GET['type'] == 'news' OR $_GET['type'] == 'news-template' OR $_GET['type'] == 'videos' OR $_GET['type'] == 'videos-template' ) {
			$post_type = 'post';
		} elseif ( $_GET['type'] == 'user-reviews' ) {
			$post_type = 'gp_user_review';			
		} else {
			$post_type = explode( ',', $GLOBALS['gp_post_types'] );
		}

		// Use filtered category is selected
		if ( isset( $_GET['cats_new'] ) && $_GET['cats_new'] != '0' ) {
			$GLOBALS['gp_cats'] = $_GET['cats_new'];
		}
	
		// Use filtered menu category is selected
		if ( isset( $_GET['menu_cats_new'] ) && $_GET['menu_cats_new'] != '0' ) {
			$GLOBALS['gp_cats'] = $_GET['menu_cats_new'];
		}
					
		// Use filtered orderby if selected
		if ( isset( $_GET['orderby_new'] ) && $_GET['orderby_new'] != '0' ) {
			$GLOBALS['gp_orderby'] = $_GET['orderby_new'];
		}	

		// Use filtered date posted if selected
		if ( isset( $_GET['date_posted_new'] ) && $_GET['date_posted_new'] != '0' ) {
			$GLOBALS['gp_date_posted'] = $_GET['date_posted_new'];
		}	

		// Use filtered date modified if selected
		if ( isset( $_GET['date_modified_new'] ) && $_GET['date_modified_new'] != '0' ) {
			$GLOBALS['gp_date_modified'] = $_GET['date_modified_new'];
		}	
							
		gp_query_variables();				
		
		$GLOBALS['gp_meta_query'] = '';

		// Get news posts associated with hub
		if ( ( $_GET['type'] == 'news' && get_post_meta( $_GET['postparentid'], '_wp_page_template', true ) == 'hub-template.php' ) OR $_GET['type'] == 'news-template' ) {
			$GLOBALS['gp_meta_query'] = array( 'relation' => 'AND', array( 'key' => 'post_association', 'value' => $_GET['postparentid'], 'compare' => 'LIKE' ) );
			$GLOBALS['gp_meta_key'] = 'post_association';
		}
				
		// Get video posts associated with hub
		if ( ( $_GET['type'] == 'videos' && get_post_meta( $_GET['postparentid'], '_wp_page_template', true ) == 'hub-template.php' ) OR $_GET['type'] == 'videos-template' ) {
			$GLOBALS['gp_meta_query'] = array( array( 'key' => 'post_association', 'value' => $_GET['postparentid'], 'compare' => 'LIKE' ) );	
			$GLOBALS['gp_meta_key'] = 'post_association';
		}

		// Tax query
		if ( $_GET['type'] == 'news' OR $_GET['type'] == 'news-template' ) {
			$tax_query = array( 'relation' => 'AND', $GLOBALS['gp_post_cats'], array( 'taxonomy' => 'post_format', 'field' => 'slug', 'terms' => array( 'post-format-quote', 'post-format-audio', 'post-format-gallery', 'post-format-image', 'post-format-link', 'post-format-video' ), 'operator' => 'NOT IN' )	 );	
		} elseif ( $_GET['type'] == 'videos' OR $_GET['type'] == 'videos-template' ) {
			$tax_query = array( 'relation' => 'AND', $GLOBALS['gp_video_cats'], array( 'taxonomy' => 'post_format', 'field' => 'slug', 'terms' => array( 'post-format-video' ) ) );			
		} elseif ( $_GET['type'] == 'blog' OR $_GET['type'] == 'blog-template' OR $_GET['type'] == 'menu' ) {
			$tax_query = array( 'relation' => 'OR', $GLOBALS['gp_hub_cats'], $GLOBALS['gp_video_cats'], $GLOBALS['gp_post_cats'], $GLOBALS['gp_slide_cats'], $GLOBALS['gp_portfolio_cats'] );
		} else {
			$tax_query = '';
		}

		// Query														
		if ( $_GET['type'] == 'taxonomy' ) {
			$defaults = array(
				'date_query' => array( $GLOBALS['gp_date_posted_value'], $GLOBALS['gp_date_modified_value'] ),
			);
			$args = $_GET['querystring'] . "&post_status=publish&orderby=" . $GLOBALS['gp_orderby_value'] . "&order=" . $GLOBALS['gp_order'] . "&meta_key=" . $GLOBALS['gp_meta_key'] . "&posts_per_page=" . $GLOBALS['gp_per_page'] . "&paged=$pagination";		
			$args = wp_parse_args( $args, $defaults );
		} elseif ( $_GET['type'] == 'author' ) {		
			$args = array(
				'post_status' => 'publish',
				'post_type' => array( 'post', 'page', 'gp_user_review' ),
				'author' => $GLOBALS['gp_author_id'],
				'meta_query' => array(
					'relation' => 'OR',		
					array(
						'key' => '_wp_page_template',
						'value' => array( 'hub-template.php', 'review-template.php' ),
						'compare' => 'IN',
					),
					array(
						'key' => '_wp_page_template',
						'compare' => 'NOT EXISTS',
					),						
				), 
				'orderby' => $GLOBALS['gp_orderby_value'],
				'order' => $GLOBALS['gp_order'],	
				'posts_per_page' => $GLOBALS['gp_per_page'],
				'paged' => $pagination,
				'date_query' => array( $GLOBALS['gp_date_posted_value'], $GLOBALS['gp_date_modified_value'] ),
			);
		} elseif ( $_GET['type'] == 'menu' ) {	
			$args = array(
				'post_status' 	  => 'publish',
				'post_type'       => array( 'post', 'page' ),
				'tax_query'       => $tax_query,
				'orderby'         => 'date',
				'order'           => 'desc',
				'posts_per_page'  => $GLOBALS['gp_per_page'],
				'paged'           => $pagination,		
			);				
		} else {
			$args = array(
				'post_status' 	 => 'publish',
				'post_type' 	 => $post_type,
				'tax_query' 	 => $tax_query,
				'orderby' 		 => $GLOBALS['gp_orderby_value'],
				'order' 		 => $GLOBALS['gp_order'],
				'meta_query' 	 => $GLOBALS['gp_meta_query'],
				'meta_key' 		 => $GLOBALS['gp_meta_key'],
				'posts_per_page' => $GLOBALS['gp_per_page'],
				'offset' 		 => $GLOBALS['gp_offset'],
				'paged'          => $pagination,
				'date_query' => array( $GLOBALS['gp_date_posted_value'], $GLOBALS['gp_date_modified_value'] ),
			);
		}
		
		//print_r($args);
		
		$gp_query = new wp_query( $args );
						
		if ( $gp_query->have_posts() ) :
		
			$total_pages = $gp_query->max_num_pages;

			// Pagination (Arrows)
			if ( $total_pages > 1 && $_GET['type'] == 'menu' ) {
				echo '<div class="ajax-pagination pagination-arrows">';
					if ( $pagination > 1 ) {
						echo '<a href="' . ( $pagination - 1 ) . '" class="prev"></a>';
					}
					if ( $pagination < $total_pages ) {
						echo '<a href="' . ( $pagination + 1 ) . '" class="next"></a>';
					}	
				echo '</div>'; 
			}
					
			while ( $gp_query->have_posts() ) : $gp_query->the_post(); ?>

				<?php if ( $_GET['type'] == 'menu' ) {
					$image = aq_resize( wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) ), 198, 125, true, false, true );
					if ( $gp['retina'] == 'gp-retina' ) {
						$retina = aq_resize( wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) ), 198 * 2, 125 * 2, true, true, true );
					} else {
						$retina = '';
					}
					echo '<article class="post-item">
						<div class="post-thumbnail featured-content image-above-content">
							<a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '">
								<img src="' . $image[0] . '" data-rel="' . $retina . '" width="' . $image[1] . '" height="' . $image[2] . '" alt="" class="wp-post-image" />
							</a>
						</div>
						<div class="loop-header">
							<h2 class="entry-title loop-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '">' . get_the_title() . '</a></h2>	
						</div>
					</article>';
				} elseif ( get_post_meta( get_the_ID(), '_wp_page_template', true ) == 'review-template.php' OR get_post_meta( get_the_ID(), '_wp_page_template', true ) == 'hub-template.php' OR get_post_meta( get_the_ID(), '_user_review_rating', true ) ) { 
					get_template_part( 'review', 'loop' );
				} else {
					get_template_part( 'post', 'loop' );
				} ?>

			<?php endwhile; ?>

			<?php 

			// Pagination (Numbers)
			if ( $total_pages > 1 && $_GET['type'] != 'menu' && $GLOBALS['gp_page_numbers'] == 'enabled' ) {
				  echo '<div class="pagination pagination-numbers ajax-pagination">';
				  echo paginate_links(array(  
					'base'     => '%_%',  
					'format'   => '/page/%#%',
					'current'  => $pagination,  
					'total'    => $total_pages,  
					'type'      => 'list',
					'prev_text' => '',
					'next_text' => '',        
				  ));
				  echo '</div>'; 
			}
			?>
		
		<?php else : ?>

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

		die();
	}	
}
add_action( 'wp_ajax_gp_ajax', 'gp_ajax' );
add_action( 'wp_ajax_nopriv_gp_ajax', 'gp_ajax' );

?>