
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php

/* -----------------------------------------------------------------------------
 * Query Builder
 * -------------------------------------------------------------------------- */
if ( ! function_exists( 'presso_build_query' ) ) {
	function presso_build_query( $args=array() ) {
		$ignored_args = array(
			'before' => 0,
			'after' => 0,
			'layout' => 0,
			'has_post_thumbnail' => 0,
			'order' => 0,
			'lead_layout' => 0,
			'lead_layout_number' => 0,
		);

		$query_args = array(
			'post_type' => 'post',
			'ignore_sticky_posts' => true,
		);

		foreach ( $args as $option => $val ) {
			switch ( $option ) {
				case 'post_type' :
					$query_args[ 'post_type' ] = $val;
					break;

				case 'has_post_thumbnail' :
					if ( $val ) {
						$query_args[ 'meta_query' ][] = array(
							'key' => '_thumbnail_id',
							'compare' => 'EXISTS'
						);
					}
					break;

				case 'post_order' :
					switch ( $val ) {
						case 'latest' :
							$query_args[ 'orderby' ] = 'date';
							break;

						case 'random' :
							$query_args[ 'orderby' ] = 'rand';
							break;

						case 'featured' :
							$query_args[ 'meta_query' ][] = array(
								'key' => 'vw_post_featured',
								'value' => '1',
								'compare' => '=',
							);
							break;

						case 'latest_gallery' :
							$query_args['tax_query'][] = array(
								'taxonomy' => 'post_format',
								'field' => 'slug',
								'terms' => 'post-format-gallery',
							);
							break;

						case 'latest_video' :
							$query_args['tax_query'][] = array(
								'taxonomy' => 'post_format',
								'field' => 'slug',
								'terms' => 'post-format-video',
							);
							break;

						case 'latest_audio' :
							$query_args['tax_query'][] = array(
								'taxonomy' => 'post_format',
								'field' => 'slug',
								'terms' => 'post-format-audio',
							);
							break;

						case 'latest_reviews' :
							$query_args[ 'meta_query' ][] = array(
								'key' => 'vw_enable_review',
								'value' => '1',
								'compare' => '=',
							);
							break;

						case 'most_commented' :
							$query_args['orderby'] = 'comment_count';
							break;

						case 'most_review_score' :
							$query_args['orderby'] = 'meta_value_num';
							$query_args['meta_key'] = 'vw_review_average_score';
							break;

						case 'daily_trending_viewed' :
							$query_args['vw-trending-range'] = 'daily';
							presso_add_trending_query();
							break;

						case 'weekly_trending_viewed' :
							$query_args['vw-trending-range'] = 'weekly';
							presso_add_trending_query();
							break;

						case 'monthly_trending_viewed' :
							$query_args['vw-trending-range'] = 'monthly';
							presso_add_trending_query();
							break;

						case 'most_viewed' :
							$query_args['vw-trending-range'] = 'all';
							presso_add_trending_query();
							break;
						
						default :
							if ( ! empty( $val ) ) {
								$query_args['orderby'] = $val;
							}
							break;
					}
					break;

				case 'offset' :
				case 'cat' :
				case 'category__not_in' :
				case 'posts_per_page' :
				case 'tax_query' :
				case 'paged' :
					if ( ! empty( $val ) ) {
						$query_args[ $option ] = $val;
					}
					break;

				default :
					if ( ! isset( $ignored_args[ $option ] ) ) {
						$query_args[ $option ] = $val;
					}
					break;
			}
		}

		/**
		 * Manual calculate paged when using offset
		 */
		if ( isset( $query_args[ 'offset' ] ) && $query_args[ 'offset' ] > 0
			&& isset( $query_args[ 'paged' ] ) && $query_args[ 'paged' ] > 1
			&& isset( $query_args[ 'posts_per_page' ] ) && $query_args[ 'posts_per_page' ] > 0
			) {

				// Wordpress is not support Offset on Pagination. This is a hack.
				$query_args['offset'] += ( $query_args[ 'paged' ] - 1 ) * $query_args[ 'posts_per_page' ];
				unset( $query_args[ 'paged' ] );
		}

		return $query_args;
	}
}




/* -----------------------------------------------------------------------------
 * Trending Query
 * -------------------------------------------------------------------------- */

if ( ! function_exists( 'presso_add_trending_query' ) ) {
	function presso_add_trending_query() {
		add_filter('posts_join', 'presso_trending_query_join', 10, 2 );
	}
}

if ( ! function_exists( 'presso_trending_query_join' ) ) {
	function presso_trending_query_join( $statement, &$query ) {
		global $wpdb;

		$trending_range = $query->get( 'vw-trending-range' );

		if ( ! empty( $trending_range ) ) {
			$join_query = presso_get_post_views_raw_query( '', $trending_range );

			if ( ! empty( $join_query) ) {
				$statement = 'INNER JOIN (' . $join_query . ') pvw ON '.$wpdb->posts.'.id = pvw.postid ' . $statement;
				// var_dump($statement);die();

				add_filter( 'posts_orderby', 'presso_trending_query_orderby', 10, 2 );
			}
		}

		// Remove itself
		remove_filter( 'posts_join', 'presso_trending_query_join', 10, 2 );

		return $statement;
	}
}

if ( ! function_exists( 'presso_trending_query_orderby' ) ) {
	function presso_trending_query_orderby( $statement, &$query ) {
		$ordersql = 'pvw.pageviews DESC';

		if ( ! empty( $statement ) ) {
			$statement = $ordersql . ', ' . $statement;

		} else {
			$statement = $ordersql;

		}

		// Remove itself
		remove_filter( 'posts_orderby', 'presso_trending_query_orderby', 10, 2 );

		return $statement;
	}
}



/* -----------------------------------------------------------------------------
 * Get Main Query Args
 * -------------------------------------------------------------------------- */
if ( ! function_exists( 'presso_get_main_query_args' ) ) {
	function presso_get_main_query_args() {
		global $wp_query;
		$args = $wp_query->query;

		// Remove unnecessary args
		unset( $args['paged'] );

		return $args;
	}
}




/* -----------------------------------------------------------------------------
 * Secondary Query Functions
 * -------------------------------------------------------------------------- */
if ( ! function_exists( 'presso_setup_secondary_query' ) ) {
	function presso_setup_secondary_query( $query=null ) {
		global $presso_secondary_query;

		if ( ! empty( $query ) ) {
			$presso_secondary_query = $query;
			
		} else {
			// Duplicate the main query to secondary for using with loop templates
			global $wp_query;
			$presso_secondary_query = $wp_query;
		}
	}
}

if ( ! function_exists( 'presso_reset_secondary_query' ) ) {
	function presso_reset_secondary_query() {
		global $presso_secondary_query;
		$presso_secondary_query = null;
	}
}

/**
 * Check more post (No side-effect like have_posts())
 */
if ( ! function_exists( 'presso_secondary_query_have_more_post' ) ) {
	function presso_secondary_query_have_more_post() {
		global $presso_secondary_query;
		return ! empty( $presso_secondary_query ) && ( ( $presso_secondary_query->current_post + 1 ) < $presso_secondary_query->post_count );
	}
}

/**
 * Same functional as have_posts() but has ability to control looping for 2 loops
 */
if ( ! function_exists( 'presso_secondary_query_have_posts' ) ) {
	function presso_secondary_query_have_posts() {
		global $presso_secondary_query;
		global $presso_lead_layout_limit;

		// Ignore calling have_posts() when reached 0
		return ( is_null( $presso_lead_layout_limit ) || $presso_lead_layout_limit-- != 0 )
				&& $presso_secondary_query->have_posts();
	}
}

if ( ! function_exists( 'presso_set_lead_layout_limit' ) ) {
	function presso_set_lead_layout_limit( $count ) {
		global $presso_lead_layout_limit;
 		$presso_lead_layout_limit = $count;
	}
}

 add_action( 'loop_end', 'presso_reset_lead_layout_limit' );
 if ( ! function_exists( 'presso_reset_lead_layout_limit' ) ) {
 	function presso_reset_lead_layout_limit() {
		global $presso_lead_layout_limit;
		$presso_lead_layout_limit = null;
 	}
 }



/* -----------------------------------------------------------------------------
 * Default query for gallery custom post type
 * -------------------------------------------------------------------------- */

if ( ! function_exists( 'presso_get_gallery_categories_query' ) ) {
	function presso_get_gallery_categories_query() {
		$query = array(
			'post_type' => 'presso_gallery',
			'posts_per_page' => -1,
		);

		$cats = get_post_meta( get_the_id(), 'vw_gallery_cats', true );

		if ( ! empty( $cats ) ) {
			$query[ 'tax_query' ] = array(
				array(
					'taxonomy' => 'presso_gallery_cat',
					'terms'    => $cats,
				),
			);
		}

		return presso_build_query( $query );
	}
}




/* -----------------------------------------------------------------------------
 * Default query for portfolio custom post type
 * -------------------------------------------------------------------------- */

if ( ! function_exists( 'presso_get_portfolio_categories_query' ) ) {
	function presso_get_portfolio_categories_query() {

		$query = array(
			'post_type' => 'presso_portfolio',
			'posts_per_page' => -1,
		);

		$cats = get_post_meta( get_the_id(), 'presso_portfolio_cat', true );

		if ( ! empty( $cats ) ) {
			$query[ 'tax_query' ] = array(
				array(
					'taxonomy' => '',
					'terms'    => $cats,
				),
			);
		}
		
		return presso_build_query( $query );
	}
}





/* -----------------------------------------------------------------------------
 * Avoid duplicate post
 * -------------------------------------------------------------------------- */

global $presso_duplicate_posts;
$presso_duplicate_posts = false; // Must be initialed as an array before using

add_action( 'after_setup_theme', 'presso_init_avoid_duplicate_post' );
if ( ! function_exists( 'presso_init_avoid_duplicate_post' ) ) {
	function presso_init_avoid_duplicate_post() {
		if ( presso_get_theme_option( 'avoid_duplicate_post', 0 ) ) {
			global $presso_duplicate_posts;
			$presso_duplicate_posts = array();
		}
	}
}

// add_action( 'wp', 'presso_force_turn_off_avoid_duplicate_post' );
if ( ! function_exists( 'presso_force_turn_off_avoid_duplicate_post' ) ) {
	function presso_force_turn_off_avoid_duplicate_post() {
		global $post;

		if ( is_page() && '1' == get_post_meta( $post->ID, 'vw_turn_off_avoid_duplicate_post', true ) ) {
			global $presso_duplicate_posts;
			$presso_duplicate_posts = false;
		}
	}
}


if ( ! function_exists( 'presso_enable_avoid_duplicate_post' ) ) {
	function presso_enable_avoid_duplicate_post() {
		global $presso_duplicate_posts;
		if ( $presso_duplicate_posts !== false ) {
			add_action( 'pre_get_posts', 'presso_avoid_duplicate_post' );
			add_action( 'the_post', 'presso_do_not_duplicate_this_post' );
		}
	}
}

if ( ! function_exists( 'presso_disable_avoid_duplicate_post' ) ) {
	function presso_disable_avoid_duplicate_post() {
		global $presso_duplicate_posts;
		if ( $presso_duplicate_posts !== false ) {
			remove_action( 'pre_get_posts', 'presso_avoid_duplicate_post' );
			remove_action( 'the_post', 'presso_do_not_duplicate_this_post' );
		}
	}
}

if ( ! function_exists( 'presso_avoid_duplicate_post' ) ) {
	function presso_avoid_duplicate_post( $query ) {
		global $presso_duplicate_posts;
		$query->set( 'post__not_in', $presso_duplicate_posts );
	}
}

if ( ! function_exists( 'presso_do_not_duplicate_this_post' ) ) {
	function presso_do_not_duplicate_this_post( $post_object ) {
		global $presso_duplicate_posts;
		$presso_duplicate_posts[] = $post_object->ID;
	}
}