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

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

if ( ! class_exists( 'WPSSLC_Notification' ) ) :

	/**
	 * Handle Notification for Inline.
	 */
	class WPSSLC_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, 'maybe_run_review_notice' ) );
		}
		/**
		 * Run review notice logic on plugin page load.
		 *
		 * @since 3.7.8
		 */
		public function maybe_run_review_notice() {

			global $pagenow;
			// Skip everything unless on the Plugins page.
			if ( 'plugins.php' !== $pagenow ) {
				return;
			}
			$this->wpsslc_review_notice();
			$this->wpsslc_coming_soon_notice();
		}
		/**
		 * Ask users to review our plugin on wordpress.org
		 *
		 * @since 1.6.9.3
		 * @version 2.1.0
		 */
		public function wpsslc_review_notice() {

			$this->wpsslc_review_dismissal();
			$this->wpsslc_review_pending();

			$activation_time  = get_site_option( 'wpsslc_active_time' );
			$review_dismissal = get_site_option( 'wpsslc_review_dismiss' );

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

			if ( ! $activation_time ) :

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

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

				wp_enqueue_style( 'wpsslc_review_style', plugins_url( '../assets/css/wpsslc-admin-review.css', __FILE__ ), array(), WPSSLC_VERSION );
				add_action( 'admin_notices', array( $this, 'wpsslc_review_notice_message' ) );
			endif;

		}

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

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

				return;
			endif;

			// Update the $review_dismissal value in 2.1.0
			update_site_option( 'wpsslc_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 wpsslc_review_pending() {

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

				return;
			endif;

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

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

			$request_uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
			$dismiss_url = wp_nonce_url(
				add_query_arg( 'wpsslc_review_dismiss', 'yes_v2_1_0', $request_uri ),
				'wpsslc-review-nonce'
			);
			$later_url   = wp_nonce_url(
				add_query_arg( 'wpsslc_review_later', 'yes', $request_uri ),
				'wpsslc-review-nonce'
			);


			?>

			<div class="wpsslc-review-notice notice">
				<div class="wpsslc-review-thumbnail">
					<img src="<?php echo esc_url( plugins_url( '../assets/images/logo.svg', __FILE__ ) ); ?>" alt="Inline Logo">
				</div>
				<div class="wpsslc-review-text">
					<h3><?php esc_html_e( 'Leave A Review?', 'contactsheets-lite' ); ?></h3>
					<p><?php esc_html_e( 'We hope you\'ve enjoyed using Inline WPSyncSheets Lite For Contact Form 7! Would you consider leaving us a review on WordPress.org?', 'contactsheets-lite' ); ?></p>
					<ul class="wpsslc-review-ul">
						<li><a href="https://wordpress.org/support/plugin/contactsheets-lite/reviews/?rate=5#rate-response" target="_blank"><span class="dashicons dashicons-external"></span><?php esc_html_e( 'Sure! I\'d love to!', 'contactsheets-lite' ); ?></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', 'contactsheets-lite' ); ?></a></li>
						<li><a href="<?php echo esc_url( $later_url ); ?>"><span class="dashicons dashicons-calendar-alt"></span><?php esc_html_e( 'Maybe Later', 'contactsheets-lite' ); ?></a></li>
						<li><a href="<?php echo esc_url( $dismiss_url ); ?>"><span class="dashicons dashicons-dismiss"></span><?php esc_html_e( 'Never show again', 'contactsheets-lite' ); ?></a></li>
					</ul>
				</div>
			</div>
			<?php
		}
		/**
		 * Ask users to coming_soon our plugin on wordpress.org
		 *
		 * @since 3.7.8
		 * @version 3.7.8
		 */
		public function wpsslc_coming_soon_notice() {

			$this->wpsslc_coming_soon_dismissal();
			$this->wpsslc_coming_soon_pending();

			$activation_time  = get_site_option( 'wpsslc_active_coming_soon_time' );
			$coming_soon_dismissal = get_site_option( 'wpsslc_coming_soon_dismiss' );

			// Update the $coming_soon_dismissal value in 3.7.8 .
			if ( 'yes_v2_1_0' === $coming_soon_dismissal ) :
				return;
			endif;

			if ( ! $activation_time ) :
				$activation_time = time() - 172800; // 172800 = 2 Days in seconds.
				update_site_option( 'wpsslc_active_coming_soon_time', $activation_time );
			endif;

			// 86400 = 1 Days in seconds.
			if ( ( time() - $activation_time > 86400  ) && current_user_can( 'manage_options' ) ) :
				wp_enqueue_style( 'wpsslc_coming_soon_style', plugins_url( '../assets/css/wpsslc-admin-review.css', __FILE__ ), array(), WPSSLC_VERSION );
				add_action( 'admin_notices', array( $this, 'wpsslc_coming_soon_notice_message' ) );
			endif;
		}

		/**
		 * Check and Dismiss coming_soon message.
		 *
		 * @since 3.7.8
		 * @version 3.7.8
		 */
		private function wpsslc_coming_soon_dismissal() {

			if ( ! is_admin() ||
				! current_user_can( 'manage_options' ) ||
				! isset( $_GET['_wpnonce'] ) ||
				! wp_verify_nonce( sanitize_key( wp_unslash( $_GET['_wpnonce'] ) ), 'wpsslc-coming-soon-nonce' ) ||
				! isset( $_GET['wpsslc_coming_soon_dismiss'] ) ) :

				return;
			endif;

			// Update the $coming_soon_dismissal value in 3.7.8 .
			update_site_option( 'wpsslc_coming_soon_dismiss', 'yes_v2_1_0' );

			if( isset( $_GET['wpsslc_connect_now'] ) ){
				wp_safe_redirect( admin_url( 'admin.php?page=wpsyncsheets-for-contact-form-7&view=forms' ) );
				exit;
			}
		}

		/**
		 * Set time to current so coming_soon notice will popup after 14 days
		 *
		 * @since 3.7.8
		 */
		private function wpsslc_coming_soon_pending() {

			if ( ! is_admin() ||
				! current_user_can( 'manage_options' ) ||
				! isset( $_GET['_wpnonce'] ) ||
				! wp_verify_nonce( sanitize_key( wp_unslash( $_GET['_wpnonce'] ) ), 'wpsslc-coming-soon-nonce' ) ||
				! isset( $_GET['wpsslc_coming_soon_later'] ) ) :

				return;
			endif;

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

		/**
		 * Review notice message wpsslc_coming_soon_notice_message() {
		 *
		 * @since 3.7.8
		 * @version 3.7.8
		 */
		public function wpsslc_coming_soon_notice_message() {

			$request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
			$dismiss_url = wp_nonce_url(
				add_query_arg( 'wpsslc_coming_soon_dismiss', 'yes_v2_1_0', $request_uri ),
				'wpsslc-coming-soon-nonce'
			);
			$connect_now_url = wp_nonce_url(
				add_query_arg(
					array(
						'wpsslc_coming_soon_dismiss' => 'yes_v2_1_0',
						'wpsslc_connect_now'         => 'yes'
					),
					$request_uri
				),
				'wpsslc-coming-soon-nonce'
			);
			$later_url   = wp_nonce_url(
				add_query_arg( 'wpsslc_coming_soon_later', 'yes', $request_uri ),
				'wpsslc-coming-soon-nonce'
			);
			?>

			<div class="wpsslc-review-notice notice ">
				<div class="wpsslc-review-notice-div">
					<div class="wpsslc-review-thumbnail">
						<img src="<?php echo esc_url( plugins_url( '../assets/images/logo.svg', __FILE__ ) ); ?>" alt="Inline Logo">
					</div>
					<div class="wpsslc-review-text">
						<h3><?php esc_html_e( 'New Feature: Save Contact Form 7 submissions directly to your WordPress database.', 'contactsheets-lite' ); ?></h3>
						<p><?php esc_html_e( 'Enable it from the Save to DB tab and start managing entries from your dashboard.', 'contactsheets-lite' ); ?></p>
					</div>
					<div class="wpsslc-review-btn">
						<div class="connect-review-btn">
	                        <a class="pramary-btn" href="<?php echo esc_url( $connect_now_url ); ?>">Connect Now</a>
	                    </div>
	                    <a class="dismiss-link" href="<?php echo esc_url( $dismiss_url ); ?>"><?php esc_html_e( 'Dismiss', 'contactsheets-lite' ); ?></a>
					</div>
				</div>
			</div>
			<?php
		}
	}
endif;
new WPSSLC_Notification();