<?php
/**
 * Popup compatibility package
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

if ( ! class_exists( 'Jet_Engine_Smart_Filters_Package' ) ) {

	/**
	 * Define Jet_Engine_Smart_Filters_Package class
	 */
	class Jet_Engine_Smart_Filters_Package {

		public function __construct() {

			add_filter(
				'jet-smart-filters/providers/jet-engine/stored-settings',
				array( $this, 'store_layout_settings' ),
				10, 2
			);

			add_filter(
				'jet-engine/ajax/get_listing/response',
				array( $this, 'add_to_response_filters_data' ),
				10, 3
			);

			add_filter(
				'jet-engine/listing/grid/is_lazy_load',
				array( $this, 'maybe_disable_lazy_load_listing' ),
				10, 2
			);

			add_filter(
				'jet-engine/listing/grid/posts-query-args',
				array( $this, 'maybe_enable_users_count' ),
				10, 2
			);

			add_filter(
				'jet-engine/listing/grid/users-query-results',
				array( $this, 'maybe_store_user_query_props' ),
				10, 3
			);

		}

		public function maybe_enable_users_count( $args, $widget ) {

			if ( isset( $args['count_total'] ) && ! empty( $args['jet_smart_filters'] ) ) {
				$args['count_total'] = true;
			}

			return $args;

		}

		public function maybe_store_user_query_props( $users, $users_query, $widget ) {

			if ( isset( $users_query->query_vars['jet_smart_filters'] ) ) {

				$provider_data = $users_query->query_vars['jet_smart_filters'];
				$provider_data = jet_smart_filters()->query->decode_provider_data( $provider_data );

				if ( isset( $_REQUEST['jet_paged'] ) ) {
					$page = absint( $_REQUEST['jet_paged'] );
				} elseif ( wp_doing_ajax() && isset( $_REQUEST['paged'] ) ) {
					$page = absint( $_REQUEST['paged'] );
				} else {
					$page = $widget->query_vars['page'];
				}

				jet_smart_filters()->query->set_props(
					$provider_data['provider'],
					array(
						'found_posts'   => $users_query->get_total(),
						'max_num_pages' => $widget->query_vars['pages'],
						'page'          => $page,
					),
					$provider_data['query_id']
				);

			}

			return $users;

		}

		/**
		 * Add the filters data to response data.
		 *
		 * @param array $response
		 * @param array $widget_settings
		 *
		 * @return array
		 */
		public function add_to_response_filters_data( $response, $widget_settings, $query = array() ) {

			if ( empty( $widget_settings['lazy_load'] ) ) {
				return $response;
			}

			if ( empty( $widget_settings['_element_id'] ) ) {
				$query_id = 'default';
			} else {
				$query_id = $widget_settings['_element_id'];
			}

			$filters_data = array();

			$filters_settings = array(
				'queries'   => jet_smart_filters()->query->get_default_queries(),
				'settings'  => jet_smart_filters()->providers->get_provider_settings(),
				'props'     => jet_smart_filters()->query->get_query_props(),
			);

			foreach ( $filters_settings as $param => $data ) {
				if ( ! empty( $data['jet-engine'][ $query_id ] ) ) {
					$filters_data[ $param ][ $query_id ] = $data['jet-engine'][ $query_id ];
				}
			}

			if ( ! empty( $filters_data ) ) {
				$response['filters_data'] = $filters_data;
			}

			if ( jet_smart_filters()->indexer->data ) {
				$response['indexer_data'] = array(
					'provider' => 'jet-engine/' . $query_id,
					'query'    => wp_parse_args(
						$query,
						isset( $filters_data['queries'][$query_id] ) ? $filters_data['queries'][$query_id] : array()
					)
				);
			}

			return $response;
		}

		/**
		 * Disable lazy loading if reload type filters are applied
		 *
		 * @param bool  $is_lazy_load
		 * @param array $settings
		 *
		 * @return bool
		 */
		public function maybe_disable_lazy_load_listing( $is_lazy_load, $settings ) {

			if ( ! $is_lazy_load ) {
				return $is_lazy_load;
			}

			if ( ! empty( $_REQUEST['jsf'] ) ) {
				$request_provider = $_REQUEST['jsf'];
				$current_provider = 'jet-engine' . ( $settings['_element_id'] ? ':' . $settings['_element_id'] : '' );
			} else if ( ! empty( $_REQUEST['jet-smart-filters'] ) ) {
				$request_provider = $_REQUEST['jet-smart-filters'];
				$current_provider = 'jet-engine' . ( $settings['_element_id'] ? '/' . $settings['_element_id'] : '/default' );
			} else {
				return $is_lazy_load;
			}

			if ( $request_provider !== $current_provider ) {
				return $is_lazy_load;
			}

			return false;
		}

		/**
		 * Store additional settings
		 *
		 * @param  [type] $stored_settings [description]
		 * @param  [type] $widget_settings [description]
		 * @return [type]                  [description]
		 */
		public function store_layout_settings( $stored_settings, $widget_settings ) {

			$settings_to_store = array(
				'inject_alternative_items',
				'injection_items',
				'use_load_more',
				'load_more_id',
			);

			foreach ( $settings_to_store as $setting ) {
				if ( isset( $widget_settings[ $setting ] ) )  {
					$stored_settings[ $setting ] = $widget_settings[ $setting ];
				}
			}

			return $stored_settings;
		}

	}

}

new Jet_Engine_Smart_Filters_Package();
