<?php
/**
 * Handling all the Notification calls in WPSyncSheets.
 *
 * @package wpsyncsheets-wpforms
 * @since 1.6.9.3
 */

if (!defined('ABSPATH')) {
	// Exit if accessed directly.
	exit;
}

if (!class_exists('WPSSLWP_Notification')):

	/**
	 * Handle Notification for Inline.
	 */
	class WPSSLWP_Notification
	{

		/**
		 * Class constructor
		 */
		public function __construct()
		{

			$this->notification_hooks();
		}

		/**
		 * Hook into actions and filters
		 *
		 * @since  1.6.9.3
		 */
		private function notification_hooks()
		{
			add_action('admin_init', array($this, 'WPSSLWP_review_notice'));
		}

		/**
		 * Ask users to review our plugin on wordpress.org
		 *
		 * @since 1.6.9.3
		 * @version 1.6.9.3
		 */
		public function wpsslwp_review_notice()
		{

			$this->wpsslwp_review_dismissal();
			$this->wpsslwp_review_pending();

			$activation_time = get_site_option('wpsslwp_active_time');
			$review_dismissal = get_site_option('wpsslwp_review_dismiss');

			// Update the $review_dismissal value in 1.6.9.3
			if ('yes_v2_1_0' === $review_dismissal):
				return;
			endif;

			if (!$activation_time):

				$activation_time = time();
				add_site_option('wpsslwp_active_time', $activation_time);
			endif;

			// 1296000 = 15 Days in seconds.
			if ((time() - $activation_time > 1296000) && current_user_can('manage_options')):

				wp_enqueue_style('wpsslwp_review_style', plugins_url('../assets/css/wpsslwp-admin-review.css', __FILE__), array(), WPSSLWP_VERSION);
				add_action('admin_notices', array($this, 'wpsslwp_review_notice_message'));
			endif;

		}

		/**
		 * Check and Dismiss review message.
		 *
		 * @since 1.6.9.3
		 * @version 1.6.9.3
		 */
		private function wpsslwp_review_dismissal()
		{

			if (!is_admin() ||
			!current_user_can('manage_options') ||
			!isset($_GET['_wpnonce']) ||
			!wp_verify_nonce(sanitize_key(wp_unslash($_GET['_wpnonce'])), 'wpsslwp-review-nonce') ||
			!isset($_GET['wpsslwp_review_dismiss'])):

				return;
			endif;

			// Update the $review_dismissal value in 1.6.9.3
			update_site_option('wpsslwp_review_dismiss', 'yes_v2_1_0');
		}

		/**
		 * Set time to current so review notice will popup after 14 days
		 *
		 * @since 1.6.9.3
		 */
		private function wpsslwp_review_pending()
		{

			if (!is_admin() ||
			!current_user_can('manage_options') ||
			!isset($_GET['_wpnonce']) ||
			!wp_verify_nonce(sanitize_key(wp_unslash($_GET['_wpnonce'])), 'wpsslwp-review-nonce') ||
			!isset($_GET['wpsslwp_review_later'])):

				return;
			endif;

			// Reset Time to current time.
			update_site_option('wpsslwp_active_time', time());
		}

		/**
		 * Review notice message
		 *
		 * @since 1.6.9.3
		 * @version 1.6.9.3
		 */
		public function wpsslwp_review_notice_message()
		{

			$request_uri = isset($_SERVER['REQUEST_URI']) ? esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'])) : '';
			$scheme = (wp_parse_url($request_uri, PHP_URL_QUERY)) ? '&' : '?';
			// Update the wpsslwp_review_dismiss value in 1.6.9.3
			$url = esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'])) . $scheme . 'wpsslwp_review_dismiss=yes_v2_1_0';
			$dismiss_url = wp_nonce_url($url, 'wpsslwp-review-nonce');

			$_later_link = esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'])) . $scheme . 'wpsslwp_review_later=yes';
			$later_url = wp_nonce_url($_later_link, 'wpsslwp-review-nonce'); ?>

			<div class="wpsslwp-review-notice notice">
				<div class="wpsslwp-review-thumbnail">
					<img src="<?php echo esc_url(plugins_url('../assets/images/logo.svg', __FILE__)); ?>" alt="Inline Logo">
				</div>
				<div class="wpsslwp-review-text">
					<h3><?php esc_html_e('Leave A Review?', 'wpsyncsheets-wpforms'); ?></h3>
					<p><?php esc_html_e('We hope you\'ve enjoyed using Inline WPSyncSheets Lite For WPForms! Would you consider leaving us a review on WordPress.org?', 'wpsyncsheets-wpforms'); ?></p>
					<ul class="wpsslwp-review-ul">
						<li><a href="https://wordpress.org/support/plugin/wpsyncsheets-wpforms/reviews/?rate=5#rate-response" target="_blank"><span class="dashicons dashicons-external"></span><?php esc_html_e('Sure! I\'d love to!', 'wpsyncsheets-wpforms'); ?></a></li>
						<li><a href="<?php echo esc_url($dismiss_url); ?>"><span class="dashicons dashicons-smiley"></span><?php esc_html_e('I\'ve already left a review', 'wpsyncsheets-wpforms'); ?></a></li>
						<li><a href="<?php echo esc_url($later_url); ?>"><span class="dashicons dashicons-calendar-alt"></span><?php esc_html_e('Maybe Later', 'wpsyncsheets-wpforms'); ?></a></li>
						<li><a href="<?php echo esc_url($dismiss_url); ?>"><span class="dashicons dashicons-dismiss"></span><?php esc_html_e('Never show again', 'wpsyncsheets-wpforms'); ?></a></li>
					</ul>
				</div>
			</div>
			<?php
		}
	}
endif;
new WPSSLWP_Notification();