<?php
/**
 * Admin pages class
 *
 * @since             1.0.0
 * @package           TInvWishlist\Admin
 */

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

/**
 * Admin pages class
 */
class TInvWL_Admin_TInvWL extends TInvWL_Admin_Base
{

	/**
	 * Constructor
	 *
	 * @param string $plugin_name Plugin name.
	 * @param string $version Plugin version.
	 */
	function __construct($plugin_name, $version)
	{
		$this->_name = $plugin_name;
		$this->_version = $version;
	}

	/**
	 * Load functions.
	 * Create Wishlist and Product class.
	 * Load settings classes.
	 */
	function load_function()
	{
		$this->wishlist = new TInvWL_Admin_Wishlist($this->_name, $this->_version);
		$this->product = new TInvWL_Admin_Product($this->_name, $this->_version);
		$this->load_settings();

		$this->define_hooks();
	}

	/**
	 * Load settings classes.
	 *
	 * @return boolean
	 */
	function load_settings()
	{
		$dir = TINVWL_PATH . 'admin/settings/';
		if (!file_exists($dir) || !is_dir($dir)) {
			return false;
		}
		$files = scandir($dir);
		foreach ($files as $value) {
			if (preg_match('/\.class\.php$/i', $value)) {
				$file = preg_replace('/\.class\.php$/i', '', $value);
				$class = 'TInvWL_Admin_Settings_' . ucfirst($file);
				$class::instance($this->_name, $this->_version);
			}
		}

		return true;
	}

	/**
	 * Sync emails settings between WooCommerce emails settings and Wishlists Emails settings page
	 */
	function sync_email_settings()
	{
		if (tinv_get_option('notification_stock_email', 'enabled')) {
			add_action('woocommerce_product_set_stock_status', array($this, 'product_set_stock_status'), 100, 2);
			add_action('woocommerce_variation_set_stock_status', array(
				$this,
				'variation_set_stock_status',
			), 100, 2);
		}
		if (tinv_get_option('notification_price_email', 'enabled')) {
			add_action('updated_postmeta', array($this, 'updated_postmeta_price'), 100, 4);
		}
	}

	/**
	 * Define hooks
	 */
	function define_hooks()
	{
		add_action('init', array($this, 'preview_email'), 1000);
		add_action('admin_menu', array($this, 'action_menu'));
		add_action('init', array($this, 'sync_email_settings'));
		add_action('tinvwl_send_promotional_error', array($this, 'email_promotional_error'), 10, 4);
		add_action('tinvwl_send_promotional_successfully', array(
			$this,
			'email_promotional_successfully',
		), 10, 3);

		add_action('wp_ajax_prepare_promotion', array($this->product, 'preview_email_presave'));
		add_action('init', array($this->product, 'preview_email'), 1000);
		if ('skip' === filter_input(INPUT_GET, $this->_name . '-wizard')) {
			update_option($this->_name . '_wizard', true);
		}
		if (!get_option($this->_name . '_wizard')) {
			add_action('admin_notices', array($this, 'wizard_run_admin_notice'));
		} elseif (!tinv_get_option('page', 'wishlist')) {
			add_action('admin_notices', array($this, 'empty_page_admin_notice'));
		}
		add_action('woocommerce_system_status_report', array($this, 'system_report_templates'));
		add_action('switch_theme', array($this, 'admin_notice_outdated_templates'));
		add_action('tinvwl_updated', array($this, 'admin_notice_outdated_templates'));


		// Add a post display state for special WC pages.
		add_filter('display_post_states', array($this, 'add_display_post_states'), 10, 2);

		add_action('tinvwl_admin_promo_footer', array($this, 'promo_footer'));
		add_action('tinvwl_remove_without_author_wishlist', array($this, 'remove_old_wishlists'));
		$this->scheduled_remove_wishlist();
	}

	/**
	 * Error notice if wizard didn't run.
	 */
	function wizard_run_admin_notice()
	{
		printf('<div class="notice notice-error"><p>%1$s</p><p><a href="%2$s" class="button-primary">%3$s</a> <a href="%4$s" class="button-secondary">%5$s</a></p></div>',
			__('<strong>Welcome to WooCommerce Wishlist</strong> – You‘re almost ready to start :)', 'ti-woocommerce-wishlist-premium'), // @codingStandardsIgnoreLine WordPress.XSS.EscapeOutput.OutputNotEscaped
			esc_url(admin_url('index.php?page=tinvwl-wizard')),
			esc_html__('Run the Setup Wizard', 'ti-woocommerce-wishlist-premium'),
			esc_url(admin_url('index.php?page=' . $this->_name . '&' . $this->_name . '-wizard=skip')),
			esc_html__('Skip Setup', 'ti-woocommerce-wishlist-premium')
		);
	}

	/**
	 * Error notice if wishlist page not set.
	 */
	function empty_page_admin_notice()
	{
		printf('<div class="notice notice-error is-dismissible" style="position: relative;"><p>%1$s <a href="%2$s">%3$s</a>%4$s<a href="%5$s">%6$s</a></p><button type="button" class="notice-dismiss"><span class="screen-reader-text">' . __('Dismiss', 'ti-woocommerce-wishlist-premium') . '</span></button></div>', // @codingStandardsIgnoreLine WordPress.XSS.EscapeOutput.OutputNotEscaped
			esc_html__('Link to Wishlists does not work!', 'ti-woocommerce-wishlist-premium'),
			esc_url($this->admin_url('') . '#general'),
			esc_html__('Please apply the Wishlist page', 'ti-woocommerce-wishlist-premium'),
			esc_html__(' or ', 'ti-woocommerce-wishlist-premium'),
			esc_url(admin_url('index.php?page=tinvwl-wizard')),
			esc_html__('Run the Setup Wizard', 'ti-woocommerce-wishlist-premium')
		);
	}

	/**
	 * Check change product price
	 *
	 * @param integer $meta_id Not Used.
	 * @param integer $object_id Product or Variation ID.
	 * @param strung $meta_key Name meta key.
	 * @param mixed $meta_value Price value.
	 *
	 * @return boolean
	 */
	function updated_postmeta_price($meta_id, $object_id, $meta_key, $meta_value)
	{
		if ('_price' === $meta_key) {
			$object_id = $product_id = absint($object_id);
			$product = wc_get_product($object_id);
			if ($product) {
				if (in_array($product->get_type(), array(
					'variable',
					'variation',
					'variable-subscription'
				))) {
					$product_id = $product->get_parent_id();
				} else {
					$product_id = $product->get_id();
				}
			}
			$meta_value = floatval($meta_value);
			$wlp = new TInvWL_Product(array(), $this->_name);
			$products = $wlp->get(array(
				'external' => false,
				'sql' => "SELECT * FROM `{table}` WHERE ( ( `product_id` = $product_id  AND `variation_id` = 0 ) OR `variation_id` = $object_id ) AND `price` <> $meta_value",
			));
			if (empty($products)) {
				return false;
			}
			$wishlists = array();
			$product = null;
			foreach ($products as $_product) {
				$wlp->update($_product);
				$_product['price'] = floatval($_product['price']);
				if ($meta_value < $_product['price']) {
					$product = $_product;
					$wishlists[] = $_product['wishlist_id'];
				}
			}
			$wishlists = array_unique($wishlists, SORT_NUMERIC);
			if (!empty($product) && !empty($wishlists)) {
				$_product = $wlp->product_data($product['product_id'], $product['variation_id']);
				if (!empty($_product)) {
					$wl = new TInvWL_Wishlist($this->_name);
					$wishlists = $wl->get(array(
						'ID' => $wishlists,
					));
					$users = array();
					foreach ($wishlists as $wishlist) {
						$users[$wishlist['author']][] = $wishlist['ID'];
					}
					if (function_exists('WC')) {
						WC()->mailer();
					}
					foreach ($users as $user_id => $wishlists) {
						do_action('tinvwl_send_notification_price', $_product, $user_id, $wishlists);
					}
				}
			}
		} // End if().
	}

	/**
	 * Check product stock status when is update
	 *
	 * @param integer $product_id Product.
	 * @param string $status Stock Staus.
	 *
	 * @return boolean
	 */
	function product_set_stock_status($product_id, $status = 'instock')
	{
		if (empty($product_id)) {
			return false;
		}
		$status = in_array($status, array('onbackorder', 'instock'));
		$wlp = new TInvWL_Product(array(), $this->_name);
		$products = $wlp->get(array(
			'product_id' => $product_id,
			'variation_id' => 0,
			'external' => false,
			'in_stock' => $status ? 0 : 1,
		));

		return $this->set_stock_status($products, $status);
	}

	/**
	 * Check variation product stock status when is updated
	 *
	 * @param integer $variation_id Variation product.
	 * @param string $status Stock Staus.
	 *
	 * @return boolean
	 */
	function variation_set_stock_status($variation_id, $status = 'instock')
	{
		if (empty($variation_id)) {
			return false;
		}
		$status = in_array($status, array('onbackorder', 'instock'));
		$wlp = new TInvWL_Product(array(), $this->_name);
		$products = $wlp->get(array(
			'variation_id' => $variation_id,
			'external' => false,
			'in_stock' => $status ? 0 : 1,
		));

		return $this->set_stock_status($products, $status);
	}

	/**
	 * Update stock status in wishlist table
	 *
	 * @param array $products Array of the products.
	 * @param string $status Stock Status.
	 *
	 * @return boolean
	 */
	function set_stock_status($products, $status = true)
	{
		if (empty($products)) {
			return false;
		}
		$wlp = new TInvWL_Product(array(), $this->_name);
		$wishlists = array();
		$product = null;
		foreach ($products as $_product) {
			if ($status !== $_product['in_stock']) {
				$wlp->update($_product);
				$product = $_product;
				$wishlists[] = $_product['wishlist_id'];
			}
		}
		$wishlists = array_unique($wishlists, SORT_NUMERIC);
		if (!empty($product) && !empty($wishlists) && $status) {
			$_product = $wlp->product_data($product['product_id'], $product['variation_id']);
			if (!empty($_product)) {
				$wl = new TInvWL_Wishlist($this->_name);
				$wishlists = $wl->get(array(
					'ID' => $wishlists,
				));
				$users = array();
				foreach ($wishlists as $wishlist) {
					do_action('tinvwl_changed_wishlist', ($status ? 32 : 16), $wishlist, $product['product_id'], $product['variation_id']);
					$users[$wishlist['author']][] = $wishlist['ID'];
				}
				if ($status) {
					if (function_exists('WC')) {
						WC()->mailer();
					}
					foreach ($users as $user_id => $wishlists) {
						do_action('tinvwl_send_notification_stock', $_product, $user_id, $wishlists);
					}
				}
			}
		}
	}

	/**
	 * Creation mune and sub-menu
	 */
	function action_menu()
	{
		global $wp_roles;
		$page = add_menu_page(__('TI Wishlist', 'ti-woocommerce-wishlist-premium'), __('TI Wishlist', 'ti-woocommerce-wishlist-premium'), 'tinvwl_wishlists', $this->_name, array(
			$this->wishlist,
			'_print_',
		), TINVWL_URL . 'assets/img/icon_menu.png', '56.888');
		add_action("load-$page", array($this, 'onload'));
		add_action('admin_enqueue_scripts', array($this, 'add_inline_scripts'));
		$menu = apply_filters('tinvwl_admin_menu', array());

		foreach ($menu as $item) {
			if (!array_key_exists('page_title', $item)) {
				$item['page_title'] = $item['title'];
			}
			if (!array_key_exists('parent', $item)) {
				$item['parent'] = $this->_name;
			}
			if (!array_key_exists('capability', $item)) {
				$item['capability'] = 'manage_woocommerce';
			}

			if (!array_key_exists('roles', $item)) {
				$item['roles'] = array('administrator');
			}

			foreach ($item['roles'] as $role) {
				$wp_roles->add_cap($role, $item['capability']);
			}

			$item['slug'] = implode('-', array_filter(array($this->_name, $item['slug'])));

			$page = add_submenu_page($item['parent'], $item['page_title'], $item['title'], $item['capability'], $item['slug'], $item['method']);
			add_action("load-$page", array($this, 'onload'));
		}
	}

	/**
	 * Load style and javascript
	 */
	function onload()
	{
		add_action('admin_enqueue_scripts', array($this, 'enqueue_styles'));
		add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
		add_filter('admin_footer_text', array($this, 'footer_admin'));
		add_filter('screen_options_show_screen', array($this, 'screen_options_hide_screen'), 10, 2);
	}

	/**
	 * Load style
	 */
	function enqueue_styles()
	{
		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';

		if (apply_filters('tinvwl_load_webfont_admin', true)) {
			wp_enqueue_style($this->_name . '-gfonts', (is_ssl() ? 'https' : 'http') . '://fonts.googleapis.com/css?family=Open+Sans:400,600,700,800', '', null, 'all');
			wp_enqueue_style($this->_name . '-webfont', TINVWL_URL . 'assets/css/webfont' . $suffix . '.css', array(), $this->_version, 'all');
			wp_style_add_data($this->_name . '-webfont', 'rtl', 'replace');
			wp_style_add_data($this->_name . '-webfont', 'suffix', $suffix);
		}

		wp_enqueue_style($this->_name, TINVWL_URL . 'assets/css/admin' . $suffix . '.css', array(), $this->_version, 'all');
		wp_style_add_data($this->_name, 'rtl', 'replace');
		wp_style_add_data($this->_name, 'suffix', $suffix);
		wp_enqueue_style($this->_name . '-form', TINVWL_URL . 'assets/css/admin-form' . $suffix . '.css', array('wp-color-picker'), $this->_version, 'all');
		wp_style_add_data($this->_name . '-form', 'rtl', 'replace');
		wp_style_add_data($this->_name . '-form', 'suffix', $suffix);
	}

	/**
	 * Load javascript
	 */
	function add_inline_scripts()
	{
		wp_add_inline_script('jquery-blockui', 'jQuery(function(c){c("body").on("click.woo",\'a[href*="//woocommerce.com"]\',function(o){var e=(((o||{}).originalEvent||{}).target||{}).href||!1,r=((o||{}).currentTarget||{}).href||!1,t="&";e&&r&&(o.currentTarget.href=e.split("?")[0]+"?aff=3955",setTimeout(function(){o.originalEvent.target.href=e},1)),c("body").off("click.woo",\'a[href*="woocommerce.com"]\')})});');
	}

	/**
	 * Load javascript
	 */
	function enqueue_scripts()
	{
		$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
		wp_enqueue_script($this->_name . '-bootstrap', TINVWL_URL . 'assets/js/bootstrap' . $suffix . '.js', array('jquery'), $this->_version, 'all');
		wp_enqueue_script('wp-color-picker-alpha', TINVWL_URL . 'assets/js/wp-color-picker-alpha.min.js', array('wp-color-picker'), $this->_version, 'all');
		wp_register_script($this->_name, TINVWL_URL . 'assets/js/admin' . $suffix . '.js', array(
			'jquery',
			'wp-color-picker-alpha',
		), $this->_version, 'all');
		wp_localize_script($this->_name, 'tinvwl_comfirm', array(
			'text_comfirm_reset' => __('Are you sure you want to reset the settings?', 'ti-woocommerce-wishlist-premium'),
		));
		wp_enqueue_script($this->_name);

		if (!tinv_get_option('chat', 'disabled')) {

			$user_id = get_current_user_id();
			$user_info = get_userdata($user_id);
			$current_theme = wp_get_theme();

			$parent_theme = $current_theme->parent();

			$license_email = $this->get_option('license', 'email');
			$email_setings = ($license_email) ? 'email: "' . $license_email . '",' : '';
			wp_add_inline_script($this->_name, 'window.intercomSettings = {
				app_id: "wj7rirzi",
				' . $email_setings . '
				"Website": "' . get_site_url() . '",
			"Plugin name": "WooCommerce Wishlist Premium Plugin",
			"Plugin version":"' . TINVWL_VERSION . '",
			"Theme name":"' . $current_theme->get('Name') . '",
			"Theme version":"' . $current_theme->get('Version') . '",
			"Theme URI":"' . $current_theme->get('ThemeURI') . '",
			"Theme author":"' . $current_theme->get('Author') . '",
			"Theme author URI":"' . $current_theme->get('AuthorURI') . '",
			"Parent theme name":"' . (($parent_theme) ? $parent_theme->get('Name') : '') . '",
			"Parent theme version":"' . (($parent_theme) ? $parent_theme->get('Version') : '') . '",
			"Parent theme URI":"' . (($parent_theme) ? $parent_theme->get('ThemeURI') : '') . '",
			"Parent theme author":"' . (($parent_theme) ? $parent_theme->get('Author') : '') . '",
			"Parent theme author URI":"' . (($parent_theme) ? $parent_theme->get('AuthorURI') : '') . '",
			};
			(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic("reattach_activator");ic("update",intercomSettings);}else{var d=document;var i=function(){i.c(arguments)};i.q=[];i.c=function(args){i.q.push(args)};w.Intercom=i;function l(){var s=d.createElement("script");s.type="text/javascript";s.async=true;s.src="https://widget.intercom.io/widget/zyh6v0pc";var x=d.getElementsByTagName("script")[0];x.parentNode.insertBefore(s,x);}if(w.attachEvent){w.attachEvent("onload",l);}else{w.addEventListener("load",l,false);}}})();
			Intercom("trackEvent", "wishlist-free-install", {
				theme_name:"' . (($parent_theme) ? $parent_theme->get('Name') : $current_theme->get('Name')) . '",
				theme_uri:"' . (($parent_theme) ? $parent_theme->get('ThemeURI') : $current_theme->get('ThemeURI')) . '",
				theme_author:"' . (($parent_theme) ? $parent_theme->get('Author') : $current_theme->get('Author')) . '",
				theme_author_uri:"' . (($parent_theme) ? $parent_theme->get('AuthorURI') : $current_theme->get('AuthorURI')) . '",
				theme_version:"' . (($parent_theme) ? $parent_theme->get('Version') : $current_theme->get('Version')) . '",
				website:"' . get_site_url() . '",
				user:"' . $user_info->user_email . '",
				user_name:"' . $user_info->user_nicename . '",
				plugin_name:"WooCommerce Wishlist Premium Plugin",
				plugin_version:"' . TINVWL_VERSION . '",
				source:"' . TINVWL_SOURCE . '",
			});
			');
		}
	}

	/**
	 * Get options
	 *
	 * @param string $name Name setting.
	 * @param string $key Key setting.
	 * @param mixed $default Default value.
	 *
	 * @return mixed
	 */
	protected function get_option($name, $key = '', $default = false)
	{
		$value = get_option(sprintf('%s-%s', $this->_name, $name), null);
		if (empty($key)) {
			return is_null($value) ? $default : $value;
		} else {
			if (is_null($value)) {
				return $default;
			} else {
				return array_key_exists($key, $value) ? $value[$key] : $default;
			}
		}

		return false;
	}

	/**
	 * Add plugin footer copywriting
	 */
	function footer_admin()
	{
		do_action('tinvwl_admin_promo_footer');
	}

	/**
	 * Promo in footer for wishlist
	 */
	function promo_footer()
	{
		echo 'Made with <i class="ftinvwl ftinvwl-heart2"></i> by <a href="https://templateinvaders.com/?utm_source=wishlist_plugin_premium&utm_campaign=made_by&utm_medium=footer">TemplateInvaders</a>';
	}

	/**
	 * Show preview email
	 */
	function preview_email()
	{
		$data = filter_input_array(INPUT_GET, array(
			'page' => FILTER_SANITIZE_STRING,
			'class' => FILTER_SANITIZE_STRING,
			'email' => FILTER_SANITIZE_STRING,
			'default' => FILTER_VALIDATE_BOOLEAN,
			'_tinvwl_nonce' => FILTER_DEFAULT,
		));
		if ((isset($data['page']) && $this->_name . '-previewemail' !== $data['page']) || (isset($data['_tinvwl_nonce']) && !wp_verify_nonce($data['_tinvwl_nonce'], sprintf('%s-%s', $this->_name, 'previewemail'))) || !isset($data['class'])) {
			return false;
		}
		WC_Emails::instance();
		$email_class = 'TInvWL_Public_Email_' . $data['class'];
		if (class_exists($email_class)) {
			$email = new $email_class($this->_name, $this->_version);
			$email->set_template($data['email']);
			tinv_wishlist_template(str_replace(DIRECTORY_SEPARATOR . 'ti-', DIRECTORY_SEPARATOR . 'ti-preview', $email->template_html));
		}
		die();
	}

	/**
	 * Error message for sent promotional email
	 *
	 * @param string $recipient Recipient.
	 * @param object $user User.
	 * @param object $product Product.
	 * @param string $error Error message.
	 */
	function email_promotional_error($recipient, $user, $product, $error)
	{
		TInvWL_View::set_error(sprintf(__('Promotional email for Product "%1$s" is not sent! %2$s', 'ti-woocommerce-wishlist-premium'), esc_html(is_callable(array(
			$this->product,
			'get_name'
		)) ? $this->product->get_name() : $this->product->get_title()), $error), 138); // WPCS: xss ok.
	}

	/**
	 * Successfully message for sent promotional email
	 *
	 * @param string $recipient Recipient.
	 * @param object $user User.
	 * @param object $product Product.
	 */
	function email_promotional_successfully($recipient, $user, $product)
	{
		TInvWL_View::set_tips(sprintf(__('Promotional email for Product "%s" has been sent successfully!', 'ti-woocommerce-wishlist-premium'), esc_html(is_callable(array(
			$product,
			'get_name'
		)) ? $product->get_name() : $product->get_title())));
	}

	/**
	 * Templates overriding status check.
	 *
	 * @param boolean $outdated Out date status.
	 *
	 * @return string
	 */
	function templates_status_check($outdated = false)
	{

		$found_files = array();

		$scanned_files = WC_Admin_Status::scan_template_files(TINVWL_PATH . '/templates/');

		foreach ($scanned_files as $file) {
			if (file_exists(get_stylesheet_directory() . '/' . $file)) {
				$theme_file = get_stylesheet_directory() . '/' . $file;
			} elseif (file_exists(get_stylesheet_directory() . '/woocommerce/' . $file)) {
				$theme_file = get_stylesheet_directory() . '/woocommerce/' . $file;
			} elseif (file_exists(get_template_directory() . '/' . $file)) {
				$theme_file = get_template_directory() . '/' . $file;
			} elseif (file_exists(get_template_directory() . '/woocommerce/' . $file)) {
				$theme_file = get_template_directory() . '/woocommerce/' . $file;
			} else {
				$theme_file = false;
			}

			if (!empty($theme_file)) {
				$core_version = WC_Admin_Status::get_file_version(TINVWL_PATH . '/templates/' . $file);
				$theme_version = WC_Admin_Status::get_file_version($theme_file);

				if ($core_version && (empty($theme_version) || version_compare($theme_version, $core_version, '<'))) {
					if ($outdated) {
						return 'outdated';
					}
					$found_files[] = sprintf(__('<code>%1$s</code> version <strong style="color:red">%2$s</strong> is out of date. The core version is <strong style="color:red">%3$s</strong>', 'ti-woocommerce-wishlist-premium'), str_replace(WP_CONTENT_DIR . '/themes/', '', $theme_file), $theme_version ? $theme_version : '-', $core_version);
				} else {
					$found_files[] = str_replace(WP_CONTENT_DIR . '/themes/', '', $theme_file);
				}
			}
		}

		return $found_files;
	}

	/**
	 * Templates overriding status for WooCommerce Status report page.
	 */
	function system_report_templates()
	{

		TInvWL_View::view('templates-status', array('found_files' => $this->templates_status_check()));
	}

	/**
	 * Outdated templates notice.
	 */
	function admin_notice_outdated_templates()
	{
		if ('outdated' === $this->templates_status_check(true)) {

			$theme = wp_get_theme();

			$html = sprintf(__('<strong>Your theme (%1$s) contains outdated copies of some WooCommerce Wishlist Premium template files.</strong><br> These files may need updating to ensure they are compatible with the current version of TI WooCommerce Wishlist.<br> You can see which files are affected from the <a href="%2$s">system status page</a>.<br> If in doubt, check with the author of the theme.', 'ti-woocommerce-wishlist-premium'), esc_html($theme['Name']), esc_url(admin_url('admin.php?page=wc-status')));

			WC_Admin_Notices::add_custom_notice('outdated_templates', $html);
		} else {
			WC_Admin_Notices::remove_notice('outdated_templates');
		}
	}

	/**
	 * Disable screen option on plugin pages
	 *
	 * @param boolean $show_screen Show screen.
	 * @param \WP_Screen $_this Screen option page.
	 *
	 * @return boolean
	 */
	function screen_options_hide_screen($show_screen, $_this)
	{
		if ($this->_name === $_this->parent_base || $this->_name === $_this->parent_file) {
			return false;
		}

		return $show_screen;
	}

	/**
	 * Check if there is a hook in the cron
	 */
	function scheduled_remove_wishlist()
	{
		$timestamp = wp_next_scheduled('tinvwl_remove_without_author_wishlist');
		if (!$timestamp) {
			$time = strtotime('00:00 today +1 HOURS');
			wp_schedule_event($time, 'daily', 'tinvwl_remove_without_author_wishlist');
		}
	}

	/**
	 * Removing old wishlist without a user older than 34 days
	 */
	public function remove_old_wishlists()
	{
		$wl = new TInvWL_Wishlist();
		$wishlists = $wl->get(array(
			'author' => 0,
			'type' => 'default',
			'sql' => 'SELECT * FROM {table} {where} AND `date` < DATE_SUB( CURDATE(), INTERVAL 34 DAY)',
		));
		foreach ($wishlists as $wishlist) {
			$wl->remove($wishlist['ID']);
		}
	}

	/**
	 * Add a post display state for special WC pages in the page list table.
	 *
	 * @param array $post_states An array of post display states.
	 * @param WP_Post $post The current post object.
	 *
	 * @return array
	 */
	public function add_display_post_states($post_states, $post)
	{
		if (tinv_get_option('page', 'wishlist') === $post->ID) {
			$post_states['tinvwl_page_for_wishlist'] = __('Wishlist Page', 'ti-woocommerce-wishlist-premium');
		}
		if (tinv_get_option('page', 'manage') === $post->ID) {
			$post_states['tinvwl_page_for_manage_wishlists'] = __('Manage Wishlists Page', 'ti-woocommerce-wishlist-premium');
		}
		if (tinv_get_option('page', 'search') === $post->ID) {
			$post_states['tinvwl_page_for_wishlist_search_results'] = __('Wishlist Search Results Page', 'ti-woocommerce-wishlist-premium');
		}
		if (tinv_get_option('page', 'public') === $post->ID) {
			$post_states['tinvwl_page_for_all_public_wishlists'] = __('All Public Wishlists Page', 'ti-woocommerce-wishlist-premium');
		}
		if (tinv_get_option('page', 'searchp') === $post->ID) {
			$post_states['tinvwl_page_for_wishlist_search'] = __('Search for Wishlist', 'ti-woocommerce-wishlist-premium');
		}
		if (tinv_get_option('page', 'create') === $post->ID) {
			$post_states['tinvwl_page_for_create_wishlist'] = __('Create Wishlist Page', 'ti-woocommerce-wishlist-premium');
		}

		return $post_states;
	}
}
