<?php
/**
 * The admin preview.
 *
 * @link        https://wpsmartpost.com/
 * @since      2.1.4
 *
 * @package    Smart_Post_Show_Pro
 * @subpackage Smart_Post_Show_Pro/admin
 */

/**
 * The admin preview.
 */
class Class_SPSP_Preview {

	/**
	 * Script and style suffix
	 *
	 * @since 2.1.4
	 * @access protected
	 * @var string
	 */
	protected $suffix;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    2.1.4
	 */
	public function __construct() {
		$this->pcp_preview_action();
	}

	/**
	 * Public Action
	 *
	 * @return void
	 */
	private function pcp_preview_action() {
		// admin Preview.
		add_action( 'wp_ajax_spf_preview_meta_box', array( $this, 'pcp_backend_preview' ) );
		add_action( 'wp_ajax_pcp_admin_post_order', array( $this, 'pcp_admin_post_order' ) );
		add_action( 'wp_ajax_post_admin_grid_ajax', array( $this, 'post_admin_grid_ajax' ) );
		add_action( 'wp_ajax_post_admin_pagination_bar', array( $this, 'post_admin_pagination_bar' ) );
		add_action( 'wp_ajax_post_admin_pagination_bar_mobile', array( $this, 'post_admin_pagination_bar_mobile' ) );
		add_action( 'wp_ajax_pcp_admin_post_popup', array( $this, 'pcp_admin_post_popup' ) );
		add_action( 'wp_ajax_pcp_post_admin_popup_next_prev', array( $this, 'pcp_post_admin_popup_next_prev' ) );

	}

	/**
	 * Post admin modal.
	 */
	public static function pcp_admin_post_popup() {
		$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
		if ( ! wp_verify_nonce( $nonce, 'spf_pcp_metabox_nonce' ) ) {
			return false;
		}
		$post_id       = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : '';
		$smart_show_id = isset( $_POST['scode_id'] ) ? absint( $_POST['scode_id'] ) : '';
		$settings      = array();
		parse_str( $_POST['data'], $settings ); // phpcs:ignore
		$view_options         = $settings['sp_pcp_view_options'];
		$popup_content_sorter = isset( $view_options['popup_post_content_sorter'] ) ? $view_options['popup_post_content_sorter'] : '';
		$popup_type           = isset( $view_options['pcp_popup_type'] ) ? $view_options['pcp_popup_type'] : 'single_popup';
		$pcp_post             = get_post( $post_id );
		?>
		<div class="pcp-modal pcp-modal-<?php echo esc_attr( $smart_show_id ); ?> white-popup">
		<div class="popup_ajax_content">
		<?php SP_PCP_HTML::sp_pcp_single_post( $pcp_post, $popup_content_sorter, $view_options ); ?>
		</div>
		<?php
		if ( 'multi_popup' === $popup_type ) {
			?>
			<div class="pcp-popup-button-prev"><i class="fa fa-chevron-left"></i></div>
			<div class="pcp-popup-button-next"><i class="fa fa-chevron-right"></i></div>
			<?php
		}
		?>
		</div>
		<?php
		die();
	}

	/**
	 * Post admin next prev modal.
	 */
	public static function pcp_post_admin_popup_next_prev() {
		$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
		if ( ! wp_verify_nonce( $nonce, 'spf_pcp_metabox_nonce' ) ) {
			return false;
		}
		$post_id       = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : '';
		$smart_show_id = isset( $_POST['scode_id'] ) ? absint( $_POST['scode_id'] ) : '';
		$settings      = array();
		parse_str( $_POST['data'], $settings ); // phpcs:ignore
		$view_options         = $settings['sp_pcp_view_options'];
		$popup_content_sorter = isset( $view_options['popup_post_content_sorter'] ) ? $view_options['popup_post_content_sorter'] : '';
		$post                 = get_post( $post_id );
		SP_PCP_HTML::sp_pcp_single_post( $post, $popup_content_sorter, $view_options );
		die();
	}
	/**
	 * Post Ajax Pagination.
	 */
	public static function post_admin_grid_ajax() {
		$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
		if ( ! wp_verify_nonce( $nonce, 'spf_pcp_metabox_nonce' ) ) {
			return false;
		}
		$smart_show_id       = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : '';
		$keyword             = isset( $_POST['keyword'] ) ? sanitize_text_field( wp_unslash( $_POST['keyword'] ) ) : '';
		$orderby             = isset( $_POST['orderby'] ) ? sanitize_text_field( wp_unslash( $_POST['orderby'] ) ) : '';
		$order               = isset( $_POST['order'] ) ? sanitize_text_field( wp_unslash( $_POST['order'] ) ) : '';
		$taxonomy            = isset( $_POST['taxonomy'] ) ? sanitize_text_field( wp_unslash( $_POST['taxonomy'] ) ) : '';
		$term_id             = isset( $_POST['term_id'] ) ? sanitize_text_field( wp_unslash( $_POST['term_id'] ) ) : '';
		$author_id           = isset( $_POST['author_id'] ) ? sanitize_text_field( wp_unslash( $_POST['author_id'] ) ) : '';
		$paged               = isset( $_POST['page'] ) ? sanitize_text_field( wp_unslash( $_POST['page'] ) ) : '';
		$selected_term_list  = isset( $_POST['term_list'] ) ? wp_unslash( $_POST['term_list'] ) : ''; // phpcs:ignore
		$custom_fields_array = isset( $_POST['custom_fields_array'] ) ? wp_unslash( $_POST['custom_fields_array'] ) : ''; // phpcs:ignore
		$pcp_lang            = isset( $_POST['lang'] ) ? sanitize_text_field( wp_unslash( $_POST['lang'] ) ) : ''; // phpcs:ignore

		$settings = array();
		parse_str( $_POST['data'], $settings ); // phpcs:ignore

		$layout        = $settings['sp_pcp_layouts'];
		$layout_preset = isset( $layout['pcp_layout_preset'] ) ? $layout['pcp_layout_preset'] : '';
		$view_options  = $settings['sp_pcp_view_options'];
		// Post display settings.
		if ( 'filter_layout' === $layout_preset ) {
			$pagination_type        = isset( $view_options['filter_pagination_type'] ) ? $view_options['filter_pagination_type'] : '';
			$pagination_type_mobile = 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'] : '';
			$pagination_type_mobile = isset( $view_options['post_pagination_type_mobile'] ) ? $view_options['post_pagination_type_mobile'] : '';
		}

		if ( 'ajax_number' === $pagination_type ) {
			if ( empty( $paged ) ) {
				$paged = isset( $_POST['page'] ) ? sanitize_text_field( wp_unslash( $_POST['page'] ) ) : '';
			}
		}
		$post_content_sorter              = isset( $view_options['post_content_sorter'] ) ? $view_options['post_content_sorter'] : '';
		$query_args                       = SP_PCP_QueryInside::get_filtered_content( $view_options, $smart_show_id, $layout );
		$post_limit                       = isset( $view_options['pcp_post_limit'] ) && ! empty( $view_options['pcp_post_limit'] ) ? $view_options['pcp_post_limit'] : 10000;
		$post_offset                      = isset( $view_options['pcp_post_offset'] ) ? $view_options['pcp_post_offset'] : 0;
		$post_limit                       = isset( $view_options['pcp_post_limit'] ) && ! empty( $view_options['pcp_post_limit'] ) ? $view_options['pcp_post_limit'] : 10000;
		$post_offset                      = isset( $view_options['pcp_post_offset'] ) ? $view_options['pcp_post_offset'] : 0;
		$new_query_args                   = $query_args;
		$new_query_args['fields']         = 'ids';
		$new_query_args['posts_per_page'] = $post_limit;
		$query_post_ids                   = get_posts( $new_query_args );
		$relation                         = isset( $view_options['pcp_filter_by_taxonomy']['pcp_taxonomies_relation'] ) ? $view_options['pcp_filter_by_taxonomy']['pcp_taxonomies_relation'] : 'AND';
		$query_args                       = SP_PCP_Functions::modify_query_params( $query_args, $keyword, $author_id, $custom_fields_array, $orderby, $order, $selected_term_list, $post_offset, $relation, $query_post_ids, $pcp_lang );
		$new_query_args                   = $query_args;
		$new_query_args['fields']         = 'ids';
		$new_query_args['posts_per_page'] = $post_limit;
		$total_posts                      = count( get_posts( $new_query_args ) );
		$post_limit                       = $total_posts;
		if ( $post_limit > 0 ) {
			$post_per_page              = isset( $view_options['post_per_page'] ) ? $view_options['post_per_page'] : '';
						$post_per_click = isset( $view_options['post_load_per_click'] ) ? $view_options['post_load_per_click'] : 12;

			$post_per_page = ( $post_per_page > $post_limit ) ? $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 ( $post_limit < 1 ) {
				$total_page = 0;
			} else {
				if ( ( 'ajax_load_more' === $pagination_type || 'infinite_scroll' === $pagination_type ) && $post_per_click ) {
					$total_page = SP_PCP_Functions::pcp_per_click_max_pages( $post_limit, $post_per_page, $post_per_click );
				} else {
					$total_page = SP_PCP_Functions::pcp_max_pages( $post_limit, $post_per_page );
				}
			}

			$pcp_last_page_post = SP_PCP_Functions::pcp_last_page_post( $post_limit, $post_per_page, $total_page );
			if ( ( 'ajax_load_more' === $pagination_type || 'infinite_scroll' === $pagination_type ) && $post_per_click ) {
				$first_offset = (int) $post_per_page;
				if ( $paged == 2 ) {
					$offset                       = $first_offset;
					$query_args['posts_per_page'] = $post_per_click;
				}
				if ( $paged > 2 ) {
					$offset                       = (int) $post_per_click * ( $paged - 2 );
					$offset                       = $first_offset + $offset;
					$query_args['posts_per_page'] = $post_per_click;
				}
			} else {
				$offset = (int) $post_per_page * ( $paged - 1 );
			}
			$query_args['offset'] = (int) $offset + (int) $post_offset;
			if ( $total_page == $paged ) {
				$query_args['posts_per_page'] = $pcp_last_page_post;
			}
			if ( $paged > $total_page ) {
				return false;
			}
		}
		$query_args['paged'] = $paged;
		$pcp_query           = new WP_Query( $query_args );
		SP_PCP_HTML::pcp_get_posts( $view_options, $layout, $post_content_sorter, $pcp_query, $smart_show_id );
		die();
	}
	/**
	 * Post Admin Ajax Pagination.
	 */
	public static function post_admin_pagination_bar() {
		$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
		if ( ! wp_verify_nonce( $nonce, 'spf_pcp_metabox_nonce' ) ) {
			return false;
		}
		$smart_show_id       = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : '';
		$keyword             = isset( $_POST['keyword'] ) ? sanitize_text_field( wp_unslash( $_POST['keyword'] ) ) : '';
		$orderby             = isset( $_POST['orderby'] ) ? sanitize_text_field( wp_unslash( $_POST['orderby'] ) ) : '';
		$order               = isset( $_POST['order'] ) ? sanitize_text_field( wp_unslash( $_POST['order'] ) ) : '';
		$taxonomy            = isset( $_POST['taxonomy'] ) ? sanitize_text_field( wp_unslash( $_POST['taxonomy'] ) ) : '';
		$term_id             = isset( $_POST['term_id'] ) ? sanitize_text_field( wp_unslash( $_POST['term_id'] ) ) : '';
		$pcp_lang            = isset( $_POST['lang'] ) ? sanitize_text_field( wp_unslash( $_POST['lang'] ) ) : '';
		$author_id           = isset( $_POST['author_id'] ) ? sanitize_text_field( wp_unslash( $_POST['author_id'] ) ) : '';
		$paged               = isset( $_POST['page'] ) ? sanitize_text_field( wp_unslash( $_POST['page'] ) ) : '';
		$custom_fields_array = isset( $_POST['custom_fields_array'] ) ? wp_unslash( $_POST['custom_fields_array'] ) : '';//phpcs:ignore
		$selected_term_list  = isset( $_POST['term_list'] ) ? wp_unslash( $_POST['term_list'] ) : '';//phpcs:ignore

		$settings = array();
		parse_str( $_POST['data'], $settings );// phpcs:ignore

		$view_options    = $settings['sp_pcp_view_options'];
		$pagination_type = isset( $view_options['post_pagination_type'] ) ? $view_options['post_pagination_type'] : '';
		$pagination_type = isset( $view_options['post_pagination_type_mobile'] ) ? $view_options['post_pagination_type_mobile'] : '';
		$layout          = $settings['sp_pcp_layouts'];
		$layout_preset   = isset( $layout['pcp_layout_preset'] ) ? $layout['pcp_layout_preset'] : '';
		$query_args      = SP_PCP_QueryInside::get_filtered_content( $view_options, $smart_show_id, $layout );

		$post_offset                      = isset( $view_options['pcp_post_offset'] ) ? $view_options['pcp_post_offset'] : 0;
		$new_query_args                   = $query_args;
		$new_query_args['fields']         = 'ids';
		$post_limit                       = isset( $view_options['pcp_post_limit'] ) && ! empty( $view_options['pcp_post_limit'] ) ? $view_options['pcp_post_limit'] : 10000;
		$new_query_args['posts_per_page'] = $post_limit;
		$query_post_ids                   = get_posts( $new_query_args );

		$relation           = isset( $view_options['pcp_filter_by_taxonomy']['pcp_taxonomies_relation'] ) ? $view_options['pcp_filter_by_taxonomy']['pcp_taxonomies_relation'] : 'AND';
		$query_args         = SP_PCP_Functions::modify_query_params( $query_args, $keyword, $author_id, $custom_fields_array, $orderby, $order, $selected_term_list, $post_offset, $relation, $query_post_ids, $pcp_lang );
		$query_args['lang'] = '';
		$pcp_query          = new WP_Query( $query_args );
		SP_PCP_HTML::pcp_pagination_bar( $pcp_query, $view_options, $layout, $smart_show_id, $paged );
		die();
	}
	/**
	 * Post Ajax Pagination.
	 */
	public static function post_admin_pagination_bar_mobile() {
		$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
		if ( ! wp_verify_nonce( $nonce, 'spf_pcp_metabox_nonce' ) ) {
			return false;
		}
		$smart_show_id       = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : '';
		$keyword             = isset( $_POST['keyword'] ) ? sanitize_text_field( wp_unslash( $_POST['keyword'] ) ) : '';
		$orderby             = isset( $_POST['orderby'] ) ? sanitize_text_field( wp_unslash( $_POST['orderby'] ) ) : '';
		$order               = isset( $_POST['order'] ) ? sanitize_text_field( wp_unslash( $_POST['order'] ) ) : '';
		$taxonomy            = isset( $_POST['taxonomy'] ) ? sanitize_text_field( wp_unslash( $_POST['taxonomy'] ) ) : '';
		$term_id             = isset( $_POST['term_id'] ) ? sanitize_text_field( wp_unslash( $_POST['term_id'] ) ) : '';
		$pcp_lang            = isset( $_POST['lang'] ) ? sanitize_text_field( wp_unslash( $_POST['lang'] ) ) : '';
		$author_id           = isset( $_POST['author_id'] ) ? sanitize_text_field( wp_unslash( $_POST['author_id'] ) ) : '';
		$paged               = isset( $_POST['page'] ) ? sanitize_text_field( wp_unslash( $_POST['page'] ) ) : '';
		$selected_term_list  = isset( $_POST['term_list'] ) ? wp_unslash( $_POST['term_list'] ) : ''; //phpcs:ignore
		$custom_fields_array = isset( $_POST['custom_fields_array'] ) ? wp_unslash( $_POST['custom_fields_array'] ) : ''; //phpcs:ignore

		$settings = array();
		parse_str( $_POST['data'], $settings ); // phpcs:ignore
		$layout        = $settings['sp_pcp_layouts'];
		$layout_preset = isset( $layout['pcp_layout_preset'] ) ? $layout['pcp_layout_preset'] : '';

		$view_options    = $settings['sp_pcp_view_options'];
		$pagination_type = isset( $view_options['post_pagination_type'] ) ? $view_options['post_pagination_type'] : '';
		$pagination_type = isset( $view_options['post_pagination_type_mobile'] ) ? $view_options['post_pagination_type_mobile'] : '';
		$query_args      = SP_PCP_QueryInside::get_filtered_content( $view_options, $smart_show_id, $layout, 'on_mobile' );
		$tax_settings    = array();
		$post_offset     = isset( $view_options['pcp_post_offset'] ) ? $view_options['pcp_post_offset'] : 0;

		$new_query_args                   = $query_args;
		$new_query_args['fields']         = 'ids';
		$post_limit                       = isset( $view_options['pcp_post_limit'] ) && ! empty( $view_options['pcp_post_limit'] ) ? $view_options['pcp_post_limit'] : 10000;
		$new_query_args['posts_per_page'] = $post_limit;
		$query_post_ids                   = get_posts( $new_query_args );
		$query_post_ids                   = array( '' );

		$relation   = isset( $view_options['pcp_filter_by_taxonomy']['pcp_taxonomies_relation'] ) ? $view_options['pcp_filter_by_taxonomy']['pcp_taxonomies_relation'] : 'AND';
		$query_args = SP_PCP_Functions::modify_query_params( $query_args, $keyword, $author_id, $custom_fields_array, $orderby, $order, $selected_term_list, $post_offset, $relation, $query_post_ids, $pcp_lang );
		$pcp_query  = new WP_Query( $query_args );
		SP_PCP_HTML::pcp_pagination_bar( $pcp_query, $view_options, $layout, $smart_show_id, $paged, 'on_mobile' );
		die();
	}

	/**
	 * Admin Post Ajax filter.
	 */
	public static function pcp_admin_post_order() {
		$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
		if ( ! wp_verify_nonce( $nonce, 'spf_pcp_metabox_nonce' ) ) {
			return false;
		}
		$smart_show_id       = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : '';
		$keyword             = isset( $_POST['keyword'] ) ? sanitize_text_field( wp_unslash( $_POST['keyword'] ) ) : '';
		$orderby             = isset( $_POST['orderby'] ) ? sanitize_text_field( wp_unslash( $_POST['orderby'] ) ) : '';
		$order               = isset( $_POST['order'] ) ? sanitize_text_field( wp_unslash( $_POST['order'] ) ) : '';
		$taxonomy            = isset( $_POST['taxonomy'] ) ? sanitize_text_field( wp_unslash( $_POST['taxonomy'] ) ) : '';
		$term_id             = isset( $_POST['term_id'] ) ? sanitize_text_field( wp_unslash( $_POST['term_id'] ) ) : '';
		$pcp_lang            = isset( $_POST['lang'] ) ? sanitize_text_field( wp_unslash( $_POST['lang'] ) ) : '';
		$author_id           = isset( $_POST['author_id'] ) ? sanitize_text_field( wp_unslash( $_POST['author_id'] ) ) : '';
		$selected_term_list  = isset( $_POST['term_list'] ) ? wp_unslash( $_POST['term_list'] ) : ''; //phpcs:ignore
		$custom_fields_array = isset( $_POST['custom_fields_array'] ) ? wp_unslash( $_POST['custom_fields_array'] ) : ''; //phpcs:ignore
		$settings            = array();
		parse_str( $_POST['fdata'], $settings );// phpcs:ignore

		$layout                 = $settings['sp_pcp_layouts'];
		$layout_preset          = isset( $layout['pcp_layout_preset'] ) ? $layout['pcp_layout_preset'] : '';
		$view_options           = $settings['sp_pcp_view_options'];
		$pagination_type        = isset( $view_options['post_pagination_type'] ) ? $view_options['post_pagination_type'] : '';
		$pagination_type_mobile = isset( $view_options['post_pagination_type_mobile'] ) ? $view_options['post_pagination_type_mobile'] : '';
		$post_content_sorter    = isset( $view_options['post_content_sorter'] ) ? $view_options['post_content_sorter'] : '';
		$query_args             = SP_PCP_QueryInside::get_filtered_content( $view_options, $smart_show_id, $layout );

		$post_offset = isset( $view_options['pcp_post_offset'] ) ? $view_options['pcp_post_offset'] : 0;

		$new_query_args                   = $query_args;
		$new_query_args['fields']         = 'ids';
		$post_limit                       = isset( $view_options['pcp_post_limit'] ) && ! empty( $view_options['pcp_post_limit'] ) ? $view_options['pcp_post_limit'] : 10000;
		$new_query_args['posts_per_page'] = $post_limit;
		$query_post_ids                   = get_posts( $new_query_args );
		$relation                         = isset( $view_options['pcp_filter_by_taxonomy']['pcp_taxonomies_relation'] ) ? $view_options['pcp_filter_by_taxonomy']['pcp_taxonomies_relation'] : 'AND';
		$query_args                       = SP_PCP_Functions::modify_query_params( $query_args, $keyword, $author_id, $custom_fields_array, $orderby, $order, $selected_term_list, $post_offset, $relation, $query_post_ids, $pcp_lang );
		$pcp_query                        = new WP_Query( $query_args );
		SP_PCP_HTML::pcp_get_posts( $view_options, $layout, $post_content_sorter, $pcp_query, $smart_show_id );
		die();
	}

	/**
	 * Function Backed preview.
	 *
	 * @since 2.2.5
	 */
	public function pcp_backend_preview() {
		$nonce = isset( $_POST['ajax_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['ajax_nonce'] ) ) : '';
		if ( ! wp_verify_nonce( $nonce, 'spf_pcp_metabox_nonce' ) ) {
			return false;
		}

		$settings = array();
		parse_str( $_POST['data'], $settings );// phpcs:ignore
		$pcp_settings = get_option( 'sp_post_carousel_settings' );
		// Shortcode id.
		$smart_show_id = (int) $settings['post_ID'];
		// Preset Layouts.
		$layout  = $settings['sp_pcp_layouts'];
		$layouts = $layout;
		// All the visible options for the Shortcode like – Global, Filter, Display, Popup, Typography etc.
		$view_options  = $settings['sp_pcp_view_options'];
		$section_title = $settings['post_title'];

		ob_start();
		// Load dynamic style.
		$dynamic_style = Smart_Post_Show_Pro_Public::load_dynamic_style( $smart_show_id, $view_options, $layouts );
		$enqueue_fonts = Smart_Post_Show_Pro_Public::load_google_fonts( $dynamic_style['typography'] );

		// Enqueue Google fonts.
		if ( ! empty( $enqueue_fonts ) ) {
			echo '<link rel="stylesheet" href="' . esc_url( add_query_arg( 'family', rawurlencode( implode( '|', $enqueue_fonts ) ), '//fonts.googleapis.com/css' ) ) . '" media="all">'; //phpcs:ignore
		}
		echo '<style id="smart_post_show_dynamic_css' . $smart_show_id . '">' . $dynamic_style['dynamic_css'] . '</style>';//phpcs:ignore
		SP_PCP_HTML::pcp_html_show( $view_options, $layout, $smart_show_id, $section_title );
		die();
	}
}
new Class_SPSP_Preview();
