<?php
/**
 * The file of query insides.
 *
 * @package Smart_Post_Show_Pro
 * @subpackage Smart_Post_Show_Pro/public/helper
 *
 * @since 2.0.0
 */

/**
 * The query inside class to process the query.
 *
 * @since 2.0.0
 */
class SP_PCP_QueryInside {

	/**
	 * Filtered content.
	 *
	 * @param array   $view_options options.
	 * @param integer $id id.
	 * @param string  $layout layout meta data.
	 * @param string  $on_screen on_screen.
	 * @return statement
	 */
	public static function get_filtered_content( $view_options, $id = '', $layout = '', $on_screen = null ) {
		$layout_preset                 = isset( $layout['pcp_layout_preset'] ) ? $layout['pcp_layout_preset'] : 'default';
		$timeline_style                = isset( $layout['timeline_other_style'] ) ? $layout['timeline_other_style'] : 'timeline_style_1';
		$pcp_post_type                 = isset( $view_options['pcp_select_post_type'] ) && ! empty( $view_options['pcp_select_post_type'] ) ? $view_options['pcp_select_post_type'] : 'any';
		$pcp_select_multiple_post_type = isset( $view_options['pcp_select_multiple_post_type'] ) && ! empty( $view_options['pcp_select_multiple_post_type'] ) ? $view_options['pcp_select_multiple_post_type'] : 'any';

		$pcp_post_type = ( 'multiple_post_type' === $pcp_post_type ) ? $pcp_select_multiple_post_type : $pcp_post_type;

		$post_limit      = isset( $view_options['pcp_post_limit'] ) && ! empty( $view_options['pcp_post_limit'] ) && 'glossary_layout' !== $layout_preset ? $view_options['pcp_post_limit'] : 5000;
		$post_per_page   = isset( $view_options['post_per_page'] ) ? $view_options['post_per_page'] : 12;
		$post_offset     = isset( $view_options['pcp_post_offset'] ) ? $view_options['pcp_post_offset'] : 0;
		$pcp_sticky_post = isset( $view_options['pcp_sticky_post'] ) ? $view_options['pcp_sticky_post'] : 0;
		$show_pagination = isset( $view_options['show_post_pagination'] ) ? $view_options['show_post_pagination'] : false;
		$post_per_page   = ( $post_per_page > $post_limit ) ? $post_limit : $post_per_page;

		$post_per_page = ( ! $show_pagination || ( 'carousel_layout' === $layout_preset || 'slider_layout' === $layout_preset || 'thumbnails_slider' === $layout_preset || 'glossary_layout' === $layout_preset || ( 'timeline_layout' === $layout_preset && 'timeline_style_2' === $timeline_style ) ) ) ? $post_limit : $post_per_page;
		$show_adv      = SP_PCP_Functions::pcp_metabox_value( 'show_adv', $view_options );
		if ( $show_adv ) {
			$post_per_page = SP_PCP_Functions::adv_adjust_with_post_per_page( $view_options, $post_per_page );
		}
		if ( 'filter_layout' === $layout_preset ) {
			$pagination_type = isset( $view_options['filter_pagination_type'] ) ? $view_options['filter_pagination_type'] : '';
		} else {
			$pagination_type = isset( $view_options['post_pagination_type'] ) ? $view_options['post_pagination_type'] : '';
			if ( 'on_mobile' === $on_screen ) {
				$pagination_type = isset( $view_options['post_pagination_type_mobile'] ) ? $view_options['post_pagination_type_mobile'] : '';
			}
		}

		if ( 'no_ajax' === $pagination_type ) {
			$paged_var = 'paged' . $id;
			$paged     = isset( $_GET[ "$paged_var" ] ) ? sanitize_text_field( wp_unslash( $_GET[ "$paged_var" ] ) ) : 1;
		} else {
			$paged            = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
			$filter_url_value = isset( $_SERVER['QUERY_STRING'] ) ? wp_unslash( $_SERVER['QUERY_STRING'] ) : '';
			if ( ! empty( $filter_url_value ) ) {
				$shortcode_id = isset( $_GET['sps'] ) ? wp_unslash( sanitize_text_field( $_GET['sps'] ) ) : '';
				if ( $shortcode_id == $id ) {
					$pcp_page = isset( $_GET['pcp_page'] ) ? wp_unslash( sanitize_text_field( $_GET['pcp_page'] ) ) : '1';
					if ( ! empty( $pcp_page ) ) {
						$paged = $pcp_page;
					}
				}
			}
		}

		$post_per_page = SP_PCP_Functions::pcp_post_per_page( $post_limit, $post_per_page, $paged );
		if ( $post_per_page < 1 ) {
			$post_per_page = isset( $view_options['post_per_page'] ) ? $view_options['post_per_page'] : 12;
		}
		$offset               = (int) $post_per_page * ( $paged - 1 );
		$sticky_post_position = 'top_list' === $pcp_sticky_post ? 0 : 1;
		$args                 = array(
			'post_type'           => $pcp_post_type,
			'suppress_filters'    => false,
			'ignore_sticky_posts' => $sticky_post_position,
			'posts_per_page'      => $post_per_page,
			'offset'              => (int) $post_offset,
			'post__not_in'        => array( get_the_ID() ),
		);
		if ( 'carousel_layout' !== $layout_preset && 'slider_layout' !== $layout_preset && 'thumbnails_slider' !== $layout_preset ) {
			$args['paged']  = $paged;
			$args['offset'] = (int) $offset + (int) $post_offset;
		}
		// Include specific posts.
		$include_posts = isset( $view_options['pcp_include_only_posts'] ) ? $view_options['pcp_include_only_posts'] : '';
		// Exclude posts.
		$exclude_post_set  = isset( $view_options['pcp_exclude_post_set'] ) ? $view_options['pcp_exclude_post_set'] : '';
		$exclude_too       = ! empty( $exclude_post_set['pcp_exclude_too'] ) ? $exclude_post_set['pcp_exclude_too'] : array();
		$current_post_id   = in_array( 'current', $exclude_too, true ) ? array( get_the_ID() ) : array();
		$sticky_post_ids   = 'hide' === $pcp_sticky_post && 'post' === $pcp_post_type ? get_option( 'sticky_posts' ) : array();
		$exclude_posts     = ! empty( $exclude_post_set['pcp_exclude_posts'] ) && isset( $exclude_post_set['pcp_exclude_posts'] ) ? $exclude_post_set['pcp_exclude_posts'] : '';
		$exclude_posts_int = array();
		if ( ! empty( $exclude_posts ) ) {
			foreach ( $exclude_posts as $exclude_post ) {
				$exclude_posts_int[] = intval( $exclude_post );
			}
		}
		$exclude_post_list = array_merge( $exclude_posts_int, $sticky_post_ids, $current_post_id );
		if ( ! empty( $exclude_post_list ) && ! empty( $include_posts ) ) {
			$include_posts = array_diff( $include_posts, $exclude_post_list );
		} elseif ( ! empty( $exclude_post_list ) ) {
			$args['post__not_in'] = ( $exclude_post_list );
		}
		// Include specific posts.
		if ( ! empty( $include_posts ) ) {
			$args['post__in'] = $include_posts;
			$args['orderby']  = 'post__in';
		}
		// Exclude password protected posts.
		$password_protected = in_array( 'password_protected', $exclude_too, true );
		if ( $password_protected ) {
			$args['has_password'] = false;
		}
		// Exclude children posts.
		$exclude_children = in_array( 'children', $exclude_too, true );
		if ( $exclude_children ) {
			$args['post_parent'] = 0;
		}
		$args['post_status'] = 'publish';

		$advanced_filters = isset( $view_options['pcp_advanced_filter'] ) && ! empty( $view_options['pcp_advanced_filter'] ) ? $view_options['pcp_advanced_filter'] : '';
		if ( $advanced_filters ) {
			foreach ( $advanced_filters as $advanced_filter ) {
				switch ( $advanced_filter ) {
					case 'taxonomy':
						$taxonomy_types = isset( $view_options['pcp_filter_by_taxonomy']['pcp_taxonomy_and_terms'] ) && ! empty( $view_options['pcp_filter_by_taxonomy']['pcp_taxonomy_and_terms'] ) ? $view_options['pcp_filter_by_taxonomy']['pcp_taxonomy_and_terms'] : '';
						if ( ! $taxonomy_types ) {
							break;
						}
						$tax_settings = array();
						foreach ( $taxonomy_types as $tax_type ) {
							$taxonomy         = isset( $tax_type['pcp_taxonomy_select_data']['pcp_select_taxonomy'] ) ? $tax_type['pcp_taxonomy_select_data']['pcp_select_taxonomy'] : '';
							$all_terms        = get_terms(
								$taxonomy,
								array(
									'get'    => 'all',
									'fields' => 'ids',
								)
							);
							$terms            = isset( $tax_type['pcp_select_terms'] ) && ! empty( $tax_type['pcp_select_terms'] ) ? $tax_type['pcp_select_terms'] : $all_terms;
							$all_button_label = isset( $tax_type['ajax_filter_options']['ajax_rename_all_text'] ) ? $tax_type['ajax_filter_options']['ajax_rename_all_text'] : '';

							if ( $taxonomy ) {
								if ( $terms ) {
									$operator = isset( $tax_type['pcp_taxonomy_term_operator'] ) ? $tax_type['pcp_taxonomy_term_operator'] : '';
									if ( 'AND' === $operator && 1 == count( $terms ) ) {
										$operator = 'IN';
									}
									$tax_settings[] = array(
										'taxonomy'         => $taxonomy,
										'field'            => 'term_id',
										'terms'            => $terms,
										'operator'         => $operator,
										'include_children' => ( 'AND' === $operator ? 'false' : 'true' ),
									);
								}
							}
						}
						if ( count( $tax_settings ) > 1 ) {
							$tax_settings['relation'] = isset( $view_options['pcp_filter_by_taxonomy']['pcp_taxonomies_relation'] ) ? $view_options['pcp_filter_by_taxonomy']['pcp_taxonomies_relation'] : 'AND';
						}
						// if sticky post position is the top of the list with taxonomy.hen run this block of code.
						if ( 'top_list' === $pcp_sticky_post ) {
							$sticky           = get_option( 'sticky_posts' );
							$new_args         = array(
								'post_type'           => $pcp_post_type,
								'suppress_filters'    => false,
								'ignore_sticky_posts' => $sticky_post_position,
								'posts_per_page'      => -1,
								'post__not_in'        => $sticky,
								'fields'              => 'ids',
							);
							$new_query_args   = get_posts( $new_args );
							$args['post__in'] = array_merge( $sticky, $new_query_args );
							$args['orderby']  = 'post__in';
						}
						$args = array_merge( $args, array( 'tax_query' => $tax_settings ) );
						break;
					case 'author':
						$author_include = isset( $view_options['pcp_filter_by_author']['pcp_select_author_by'] ) ? $view_options['pcp_filter_by_author']['pcp_select_author_by'] : '';
						$author_exclude = isset( $view_options['pcp_filter_by_author']['pcp_select_author_not_by'] ) ? $view_options['pcp_filter_by_author']['pcp_select_author_not_by'] : '';
						$wp37           = SP_PCP_Functions::wp_version_compare( '3.7' );
						if ( $author_include ) {
							$args = array_merge(
								$args,
								$wp37 ? array( 'author__in' => array_map( 'intval', $author_include ) ) : array( 'author' => intval( $author_include[0] ) )
							);
						}
						if ( $author_exclude && $wp37 ) {
							$args = array_merge(
								$args,
								array( 'author__not_in' => array_map( 'intval', $author_exclude ) )
							);
						}
						break;
					case 'sortby':
						$orderby = isset( $view_options['pcp_filter_by_order']['pcp_select_filter_orderby'] ) ? $view_options['pcp_filter_by_order']['pcp_select_filter_orderby'] : '';
						$order   = isset( $view_options['pcp_filter_by_order']['pcp_select_filter_order'] ) ? $view_options['pcp_filter_by_order']['pcp_select_filter_order'] : '';

						if ( 'custom_field' === $orderby ) {
							$order_custom_field_option = isset( $view_options['pcp_filter_by_order']['orderby_custom_field_options'] ) ? $view_options['pcp_filter_by_order']['orderby_custom_field_options'] : '';
							$order_field_key           = isset( $order_custom_field_option['pcp_select_custom_field_key'] ) ? $order_custom_field_option['pcp_select_custom_field_key'] : '';
							$field_value_type          = isset( $order_custom_field_option['pcp_select_custom_field_value_type'] ) ? $order_custom_field_option['pcp_select_custom_field_value_type'] : '';
							$order_settings            = array(
								'meta_key'  => $order_field_key,
								'orderby'   => 'meta_value',
								'meta_type' => $field_value_type,
								'order'     => $order_field_key ? $order : '',
							);
							$args                      = array_merge( $args, $order_settings );
						} elseif ( 'most_viewed' === $orderby ) {
							$order_settings = array(
								'meta_key' => '_post_views_count',
								'orderby'  => 'meta_value_num',
								'order'    => $orderby ? $order : '',
							);
							$args           = array_merge( $args, $order_settings );
						} else {
							if ( 'rand' === $orderby ) {
								if ( $paged && get_query_var( 'paged' ) === 0 && get_query_var( 'paged' ) !== null ) {
									set_transient( 'sps_rand', wp_rand() );
								}
							}
							$order_settings = array(
								'orderby' => ( 'rand' === $orderby ) ? 'rand(' . get_transient( 'sps_rand' ) . ')' : $orderby,
								'order'   => $orderby ? $order : '',
							);
							$args           = array_merge( $args, $order_settings );
						}
						break;
					case 'custom_field':
						self::pcp_filter_by_custom_field( $args, $view_options );
						break;
					case 'status':
						$pcp_post_status = isset( $view_options['pcp_filter_by_status']['pcp_select_post_status'] ) && ! empty( $view_options['pcp_filter_by_status']['pcp_select_post_status'] ) ? $view_options['pcp_filter_by_status']['pcp_select_post_status'] : array( 'publish' );
						$args            = array_merge( $args, array( 'post_status' => $pcp_post_status ) );
						// Exclude private post for disallowed or non logged user if private post selected in status option.
						$pcp_hide_private_posts_to_visitor = apply_filters( 'pcp_hide_private_posts_to_visitor', true );
						if ( in_array( 'private', $pcp_post_status ) && $pcp_hide_private_posts_to_visitor ) {
							$private_exclude = self::get_exclude_private_post_ids( $args );
							if ( array_key_exists( 'post__not_in', $args ) && ! empty( $exclude_post_list ) && ! empty( $private_exclude ) ) {
								$private_exclude = array_merge( $private_exclude, $exclude_post_list );
							}
							if ( ! empty( $private_exclude ) ) {
								$args['post__not_in'] = ( $private_exclude );
							}
						}
						break;
					case 'date':
						self::pcp_filter_by_date( $args, $view_options );
						break;
					case 'keyword':
						$keyword_value = isset( $view_options['pcp_filter_by_keyword']['pcp_set_post_keyword'] ) && ! empty( $view_options['pcp_filter_by_keyword']['pcp_set_post_keyword'] ) ? $view_options['pcp_filter_by_keyword']['pcp_set_post_keyword'] : '';
						if ( $keyword_value ) {
							$args = array_merge(
								$args,
								array(
									's' => $keyword_value,
								)
							);
						}
						break;
				}
			}
		}

		// Product filter.
		$pcp_select_filter_product_type = isset( $view_options['pcp_select_filter_product_type'] ) ? $view_options['pcp_select_filter_product_type'] : 'recent';
		if ( 'product' === $pcp_post_type && 'none' !== $pcp_select_filter_product_type ) {
			switch ( $pcp_select_filter_product_type ) {
				case 'recent':
						$recent_product = array(
							'orderby' => 'date',
							'order'   => 'DESC',
						);
						$args           = array_merge( $args, $recent_product );
					break;
				case 'featured':
					$args['tax_query'][] = array(
						'taxonomy' => 'product_visibility',
						'field'    => 'name',
						'terms'    => 'featured',
						'operator' => 'IN',
					);
					break;
				case 'on_sale':
					$args['meta_query'][] = array(
						'key'     => '_sale_price',
						'value'   => 0,
						'compare' => '>',
						'type'    => 'numeric',
					);
					break;
				case 'best_selling':
					$args['meta_query'][] = array(
						array(
							'key'     => 'total_sales',
							'value'   => 0,
							'compare' => '>',
						),
					);
					$args['orderby']      = 'meta_value_num';
					break;
				case 'top_rated':
					$args['meta_query'][] = array(
						array(
							'key'     => '_wc_average_rating',
							'value'   => 0,
							'compare' => '>',
						),
					);
					break;
				case 'out_of_stock':
					$args['meta_query'][] = array(
						array(
							'key'   => '_stock_status',
							'value' => 'outofstock',
						),
					);
					break;
			}
		}

		$filter_url_value = isset( $_SERVER['QUERY_STRING'] ) ? wp_unslash( $_SERVER['QUERY_STRING'] ) : '';
		if ( ! empty( $filter_url_value ) ) {
			$shortcode_id = isset( $_GET['sps'] ) ? wp_unslash( sanitize_text_field( $_GET['sps'] ) ) : '';
			if ( $shortcode_id == $id ) {
				$url_args           = $args;
				$url_args['fields'] = 'ids';
				$relation           = isset( $view_options['pcp_filter_by_taxonomy']['pcp_taxonomies_relation'] ) ? $view_options['pcp_filter_by_taxonomy']['pcp_taxonomies_relation'] : 'AND';

				$taxonomies          = get_object_taxonomies( $pcp_post_type );
				$tax_settings_by_url = array();
				foreach ( $taxonomies as $taxonomy ) {
					$filter_url_value = isset( $_GET[ "tx_$taxonomy" ] ) ? wp_unslash( sanitize_text_field( $_GET[ "tx_$taxonomy" ] ) ) : '';
					if ( ! empty( $filter_url_value ) ) {
						if ( strpos( $filter_url_value, ',' ) !== false ) {
							$filter_url_value = explode( ',', $filter_url_value );
						}
						$tax_settings_by_url[] = array(
							'taxonomy' => $taxonomy,
							'field'    => 'term_id',
							'terms'    => $filter_url_value,
							'operator' => 'IN',
						);
					}
				}
				if ( ! empty( $tax_settings_by_url ) ) {

					if ( count( $tax_settings_by_url ) > 1 ) {
						$tax_settings_by_url['relation'] = $relation;
					}
					if ( 'OR' === $relation ) {
						$url_args['posts_per_page'] = '10000';
						$url_post_ids               = get_posts( $url_args );
						$args['post__in']           = $url_post_ids;
					}
					$args = array_merge( $args, array( 'tax_query' => $tax_settings_by_url ) );
				}
				$final_author_url_value = isset( $_GET['pcp_author_id'] ) ? sanitize_text_field( wp_unslash( $_GET['pcp_author_id'] ) ) : '';
				if ( ! empty( $final_author_url_value ) ) {
					$args['author__in'] = $final_author_url_value;
				}
				$final_orderby_url_value = isset( $_GET['pcp_orderby'] ) ? sanitize_text_field( wp_unslash( $_GET['pcp_orderby'] ) ) : '';
				if ( ! empty( $final_orderby_url_value ) ) {
					$args['orderby'] = $final_orderby_url_value;
				}
				$final_order_url_value = isset( $_GET['pcp_order'] ) ? sanitize_text_field( wp_unslash( $_GET['pcp_order'] ) ) : '';
				if ( ! empty( $final_order_url_value ) ) {
					$args['order'] = $final_order_url_value;
				}
				$final_search_url_value = isset( $_GET['pcp_keyword'] ) ? sanitize_text_field( wp_unslash( $_GET['pcp_keyword'] ) ) : '';
				if ( ! empty( $final_search_url_value ) ) {
					$args['s'] = $final_search_url_value;
				}
				$selected_cf_keys = self::pcp_all_custom_field_key( $view_options );
				if ( ! empty( $selected_cf_keys ) ) {
					$pcp_meta_query = array();
					foreach ( $selected_cf_keys as $field_key ) {
						$filter_value = isset( $_GET[ "cf$field_key" ] ) ? sanitize_text_field( wp_unslash( $_GET[ "cf$field_key" ] ) ) : '';

						if ( strpos( $filter_value, ' ' ) !== false ) {
								$pcp_meta_query[] = array(
									'key'   => $field_key,
									'value' => $filter_value,
								);
						} else {
							if ( strpos( $filter_value, ',' ) !== false ) {
								$filter_value = explode( ',', $filter_value );
							}

							if ( ! empty( $filter_value ) ) {
								$filter_query = array(
									'key'   => $field_key,
									'value' => $filter_value,
								);

								if ( is_string( $filter_value ) && strpos( $filter_value, '|' ) !== false ) {
									$filter_value            = explode( '|', $filter_value );
									$filter_query['type']    = 'NUMERIC';
									$filter_query['compare'] = 'BETWEEN';
									$filter_query['value']   = $filter_value;
								}
								$pcp_meta_query[] = $filter_query;
							}
						}
					}
					if ( count( $pcp_meta_query ) > 1 ) {
						$pcp_meta_query['relation'] = 'AND';
					}
					$args = array_merge( $args, array( 'meta_query' => $pcp_meta_query ) );
				}
			}
		}
		if ( 'attachment' === $pcp_post_type ) {
			$args['post_status'] = 'inherit';
		}
		return apply_filters( 'sp_pcp_query_arguments', $args, $id );
	}

	/**
	 * Filter posts with date.
	 *
	 * @param array $args The arguments of the query.
	 * @param array $view_options The Array of the Metabox fields.
	 * @return void
	 */
	public static function pcp_filter_by_date( &$args, $view_options ) {
		$advanced_filters = isset( $view_options['pcp_advanced_filter'] ) && ! empty( $view_options['pcp_advanced_filter'] ) ? $view_options['pcp_advanced_filter'] : '';
		if ( in_array( 'date', $advanced_filters, true ) ) {
			$_date_type_to_filter = isset( $view_options['pcp_filter_by_date']['pcp_select_post_date_type'] ) && ! empty( $view_options['pcp_filter_by_date']['pcp_select_post_date_type'] ) ? $view_options['pcp_filter_by_date']['pcp_select_post_date_type'] : '';
			$now                  = new \DateTime( 'now' );
			$today                = $now->format( 'j' ); // A numeric representation of a month without 0.
			$this_month           = $now->format( 'n' ); // A numeric representation of a month without 0.
			$this_year            = $now->format( 'Y' );
			$date_query           = array();
			if ( $_date_type_to_filter ) {
				switch ( $_date_type_to_filter ) {
					case 'yesterday':
						$yesterday = gmdate( 'Y-m-d', strtotime( 'yesterday' ) );
						$date      = date_parse( $yesterday );

						$date_query = array(
							'year'  => $date['year'],
							'month' => $date['month'],
							'day'   => $date['day'],
						);
						break;

					case 'today_only':
						$date_query = array(
							'year'  => $this_year,
							'month' => $this_month,
							'day'   => $today,
						);
						break;

					case 'today_onwards':
						$yesterday  = gmdate( 'Y-m-d', strtotime( 'yesterday' ) );
						$date_query = array(
							'after' => $yesterday,
						);
						break;

					case 'this_week':
						$date_query = array(
							'year' => $this_year,
							'week' => $now->format( 'W' ),
						);
						break;

					case 'this_month':
						$date_query = array(
							'year'  => $this_year,
							'month' => $this_month,
						);
						break;

					case 'this_year':
						$date_query = array(
							'year' => $this_year,
						);
						break;

					case 'week_ago':
					case 'month_ago':
					case 'year_ago':
						$date_query = array(
							'column' => 'post_date',
							'after'  => sprintf( '1 %s ago', str_replace( '_ago', '', $_date_type_to_filter ) ),
						);
						break;

					case 'three_month_ago':
						$date_query = array(
							'column' => 'post_date',
							'after'  => '3 months ago',
						);
						break;

					case 'specific_date':
						$_specific_date = isset( $view_options['pcp_filter_by_date']['pcp_select_post_specific_date'] ) && ! empty( $view_options['pcp_filter_by_date']['pcp_select_post_specific_date'] ) ? $view_options['pcp_filter_by_date']['pcp_select_post_specific_date'] : '';
						$specific_date  = date_parse( $_specific_date );

						$date_query = array(
							'year'  => $specific_date['year'],
							'month' => $specific_date['month'],
							'day'   => $specific_date['day'],
						);
						break;

					case 'specific_month':
						$post_published_in_month = isset( $view_options['pcp_filter_by_date']['pcp_select_specific_month'] ) && ! empty( $view_options['pcp_filter_by_date']['pcp_select_specific_month'] ) ? $view_options['pcp_filter_by_date']['pcp_select_specific_month'] : '';
						$date_query              = array(
							'month' => $post_published_in_month,
						);
						break;

					case 'specific_year':
						$post_published_in_year = isset( $view_options['pcp_filter_by_date']['pcp_select_post_specific_year']['all'] ) && ! empty( $view_options['pcp_filter_by_date']['pcp_select_post_specific_year']['all'] ) ? $view_options['pcp_filter_by_date']['pcp_select_post_specific_year']['all'] : '';

						$date_query = array(
							'year' => $post_published_in_year,
						);
						break;

					case 'specific_period':
						$_date_from_to = isset( $view_options['pcp_filter_by_date']['pcp_select_post_date_from_to'] ) && ! empty( $view_options['pcp_filter_by_date']['pcp_select_post_date_from_to'] ) ? $view_options['pcp_filter_by_date']['pcp_select_post_date_from_to'] : '';
						$_date_from    = isset( $_date_from_to ) ? $_date_from_to['from'] : $today;
						$_date_to      = isset( $_date_from_to ) ? $_date_from_to['to'] : $today;
						$date_from     = date_parse( $_date_from );
						$date_to       = date_parse( $_date_to );
						if ( $date_from && $date_to ) {
								$date_query = array(
									'after'     => array(
										'year'  => $date_from['year'],
										'month' => $date_from['month'],
										'day'   => $date_from['day'],
									),
									'before'    => array(
										'year'  => $date_to['year'],
										'month' => $date_to['month'],
										'day'   => $date_to['day'],
									),
									'inclusive' => true,
								);
						}
						break;

				}
				if ( $date_query ) {
					$args['date_query'] = array( $date_query );
				}
			}
		}
	}

	/**
	 * Exclude private post ids for disallowed or non logged user.
	 *
	 * @param  array $args query arg.
	 * @return array
	 */
	public static function get_exclude_private_post_ids( $args ) {
		$args['fields']              = 'ids';
		$post_ids                    = get_posts( $args );
		$exclude_private             = array();
		$which_user_see_private_post = apply_filters( 'pcp_which_user_see_private_post', 'edit_others_posts' ); // Allow editor or upper role  to see any private post.
		foreach ( $post_ids  as $key => $id ) {
			$post_author_can_see = apply_filters( 'pcp_post_author_can_see_private_post', true ); // Allow author to see his private post.
			$post_author_can_see = ( get_current_user_id() == get_post_field( 'post_author', $id ) && $post_author_can_see ) ? true : false;
			// Exclude private post if current user has not editor or upper role.
			if ( ! current_user_can( $which_user_see_private_post, $id ) && 'private' === get_post_status( $id ) ) {
				if ( ! $post_author_can_see ) { // Exclude private post if post author can not see private post.
					array_push( $exclude_private, $id );
				}
			}
		}
		return $exclude_private;
	}



	/**
	 * Filter posts with Custom Fields.
	 *
	 * @param array $args Arguments of WP Query.
	 * @param array $view_options The array of Metabox fields.
	 * @return void
	 */
	public static function pcp_filter_by_custom_field( &$args, $view_options ) {
		$advanced_filters = isset( $view_options['pcp_advanced_filter'] ) && ! empty( $view_options['pcp_advanced_filter'] ) ? $view_options['pcp_advanced_filter'] : '';
		if ( in_array( 'custom_field', $advanced_filters, true ) ) {
			$_custom_fields = isset( $view_options['pcp_filter_custom_field']['pcp_filter_by_custom_field_group'] ) && ! empty( $view_options['pcp_filter_custom_field']['pcp_filter_by_custom_field_group'] ) ? $view_options['pcp_filter_custom_field']['pcp_filter_by_custom_field_group'] : '';

			$pcp_meta_query = array();
			if ( is_array( $_custom_fields ) && ! empty( $_custom_fields ) ) {
				foreach ( $_custom_fields as $_custom_field ) {
					$c_field_key        = $_custom_field['pcp_select_custom_field_key'];
					$c_field_value_type = $_custom_field['pcp_select_custom_field_value_type'];
					$add_filter         = isset( $_custom_field['add_filter_post'] ) ? $_custom_field['add_filter_post'] : '';
					if ( ! $add_filter ) {
						switch ( $c_field_value_type ) {
							case 'CHAR':
								$compare_operator = $_custom_field['pcp_select_custom_compare_operator_text'];
								$c_field_value    = $_custom_field['pcp_select_custom_field_text_value'];
								break;
							case 'NUMERIC':
								$compare_operator = $_custom_field['pcp_select_custom_compare_operator_number'];
								$c_field_value    = $_custom_field['pcp_select_custom_field_text_value'];
								break;
							case 'DATE':
								$compare_operator = $_custom_field['pcp_select_custom_compare_operator_date'];
								if ( in_array( $compare_operator, array( 'BETWEEN', 'NOT BETWEEN', 'EXISTS', 'NOT EXISTS' ), true ) ) {
									$c_field_date_from_to = $_custom_field['pcp_select_custom_field_date_value_from_to'];
									$c_field_value        = array( $c_field_date_from_to['from'], $c_field_date_from_to['to'] );
								} elseif ( in_array( $compare_operator, array( 'NOW_FUTURE', 'IN_PAST', 'TODAY', 'NOW_PAST' ), true ) ) {
									if ( 'NOW_FUTURE' === $compare_operator ) {
										$compare_operator = '>=';
									} elseif ( 'IN_PAST' === $compare_operator ) {
										$compare_operator = '<';
									} elseif ( 'TODAY' === $compare_operator ) {
										$compare_operator = '=';
									} elseif ( 'NOW_PAST' === $compare_operator ) {
										$compare_operator = '<=';
									}
									$c_field_value = current_time( 'Y-m-d' );
								} else {
									$c_field_value = $_custom_field['pcp_select_custom_field_date_value'];
								}
								break;
							case 'BOOLEAN':
								$compare_operator = $_custom_field['pcp_select_custom_compare_operator_boolean'];
								$c_field_value    = $_custom_field['pcp_select_custom_field_boolean_value'];
								break;
						}

						$pcp_meta_query[] = array(
							'key'     => $c_field_key,
							'type'    => $c_field_value_type,
							'compare' => $compare_operator,
							'value'   => $c_field_value,
						);
					}
				}
			}

			if ( count( $pcp_meta_query ) > 1 ) {
				$pcp_meta_query['relation'] = isset( $view_options['pcp_filter_custom_field']['pcp_filter_custom_field_relation'] ) ? $view_options['pcp_filter_custom_field']['pcp_filter_custom_field_relation'] : 'OR';
			}
			$args = array_merge( $args, array( 'meta_query' => $pcp_meta_query ) );
		}
	}
	/**
	 * All Custom Fields key.
	 *
	 * @param array $view_options The array of Metabox fields.
	 * @return array
	 */
	public static function pcp_all_custom_field_key( $view_options ) {

		$advanced_filters = isset( $view_options['pcp_advanced_filter'] ) && ! empty( $view_options['pcp_advanced_filter'] ) ? $view_options['pcp_advanced_filter'] : array();
		$all_keys         = array();
		if ( in_array( 'custom_field', $advanced_filters, true ) ) {
			$_custom_fields = isset( $view_options['pcp_filter_custom_field']['pcp_filter_by_custom_field_group'] ) && ! empty( $view_options['pcp_filter_custom_field']['pcp_filter_by_custom_field_group'] ) ? $view_options['pcp_filter_custom_field']['pcp_filter_by_custom_field_group'] : '';

			if ( is_array( $_custom_fields ) && ! empty( $_custom_fields ) ) {
				foreach ( $_custom_fields as $_custom_field ) {
					$c_field_key = $_custom_field['pcp_select_custom_field_key'];
					$add_filter  = isset( $_custom_field['add_filter_post'] ) ? $_custom_field['add_filter_post'] : '';
					if ( $add_filter && ! empty( $c_field_key ) ) {
						array_push( $all_keys, $c_field_key );
					}
				}
			}
		}
		return $all_keys;
	}
}
