<?php
/**
 * Class to display the `Upgrade To Pro` admin notice.
 *
 * @package Lively
 */

defined( 'ABSPATH' ) || exit;

/**
 * Class to display the `Upgrade to Pro` admin notice.
 *
 * Class Lively_Theme_Notice
 */
class Lively_Theme_Notice {

	/**
	 * Currently active theme in the site.
	 *
	 * @var \WP_Theme
	 */
	protected $active_theme;

	/**
	 * Current user id.
	 *
	 * @var int Current user id.
	 */
	protected $current_user_data;

	/**
	 * Constructor function for `Upgrade To Pro` admin notice.
	 *
	 * Lively_Theme_Notice constructor.
	 */
	public function __construct() {

		add_action( 'after_setup_theme', array( $this, 'pro_theme_notice' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );

	}

	/**
	 * Function to hold the available themes, which have pro version available.
	 *
	 * @return array Theme lists.
	 */
	public static function get_theme_lists() {

		$theme_lists = array(
			'lively'      => 'https://www.templatesell.com/item/lively-pro',
		);

		return $theme_lists;

	}

	/**
	 * Set upgrade time and display the admin notice as required.
	 */
	public function pro_theme_notice() {

		global $current_user;
		$this->current_user_data = $current_user;
		$this->active_theme      = wp_get_theme();

		// In case user is using child theme, we need to show `Upgrade To Pro` notice too.
		if ( is_child_theme() ) {
			$this->active_theme = wp_get_theme()->parent()->get( 'Name' );
		}

		$option = get_option( 'lively_theme_notice_start_time' );

		if ( ! $option ) {
			update_option( 'lively_theme_notice_start_time', time() );
		}

		add_action( 'admin_notices', array( $this, 'pro_theme_notice_markup' ), 0 );
		add_action( 'admin_init', array( $this, 'pro_theme_notice_partial_ignore' ), 0 );
		add_action( 'admin_init', array( $this, 'pro_theme_notice_ignore' ), 0 );

	}

	/**
	 * Enqueue the required scripts.
	 */
	public function enqueue_scripts() {

		wp_enqueue_style( 'lively-notice', get_template_directory_uri() . '/templatesell/pro-notice/notice.css', array(), LIVELY_S_VERSION );
	}

	/**
	 * Display the `Upgrade To Pro` admin notice.
	 */
	public function pro_theme_notice_markup() {

		$theme_lists             = self::get_theme_lists();
		$current_theme           = strtolower( $this->active_theme );
		$theme_notice_start_time = get_option( 'lively_theme_notice_start_time' );
		$pre_sales_query_link    = 'https://www.templatesell.com/support';
		$ignore_notice_permanent = get_user_meta( $this->current_user_data->ID, 'lively_nag_pro_theme_notice_ignore', true );
		$ignore_notice_partially = get_user_meta( $this->current_user_data->ID, 'lively_nag_pro_theme_notice_partial_ignore', true );

		// Return if the theme is not available in theme lists.
		if ( ! array_key_exists( $current_theme, $theme_lists ) ) {
			return;
		}
		/**
		 * Return from notice display if:
		 *
		 * 1. The theme installed is less than 10 days ago.
		 * 2. If the user has ignored the message partially for 2 days.
		 * 3. Dismiss always if clicked on 'Dismiss' button.
		 */
		if ( ( $theme_notice_start_time > strtotime( '-10 days' ) ) || ( $ignore_notice_partially > strtotime( '-5 days' ) ) || ( $ignore_notice_permanent ) ) {
			return;
		}
		?>

		<div class="notice updated pro-theme-notice">
			<p>
				<?php
				$pro_link = '<a target="_blank" href=" ' . esc_url( $theme_lists[ $current_theme ] ) . ' ">' . esc_html__( 'upgrade to pro', 'lively' ) . '</a>';

				printf(
					/* translators: 1: current user display name, 2: active theme name, 3: link to the pro theme, 4: coupon code. */
					esc_html__( 'Howdy, %1$s! You\'ve been using %2$s theme for a while now, and we hope you\'re happy with it. If you need more options and access to the premium features, you can %3$s. Also, you can use the coupon code %4$s to get 20 percent discount while making the purchase. Enjoy!', 'lively' ),
					'<strong>' . esc_html( $this->current_user_data->display_name ) . '</strong>',
					esc_html( (string) $this->active_theme ),
					$pro_link,
					'<code>TSCare20</code>'
				);
				?>
			</p>

			<div class="links">
				<a href="<?php echo esc_url( $theme_lists[ $current_theme ] ); ?>" class="btn button-primary"
				   target="_blank">
					<span class="dashicons dashicons-cart"></span>
					<span><?php esc_html_e( 'Upgrade To Pro', 'lively' ); ?></span>
				</a>

				<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'lively_nag_pro_theme_notice_partial_ignore', '1' ), 'lively_nag_pro_theme_notice_partial_ignore' ) ); ?>" class="btn button-secondary">
					<span class="dashicons dashicons-calendar-alt"></span>
					<span><?php esc_html_e( 'Maybe later', 'lively' ); ?></span>
				</a>

				<a href="<?php echo esc_url( $pre_sales_query_link ); ?>"
				   class="btn button-secondary" target="_blank">
					<span class="dashicons dashicons-email-alt"></span>
					<span><?php esc_html_e( 'Contact Us', 'lively' ); ?></span>
				</a>
			</div>

			<a class="notice-dismiss" href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'lively_nag_pro_theme_notice_ignore', '1' ), 'lively_nag_pro_theme_notice_ignore' ) ); ?>">
				<span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice', 'lively' ); ?></span>
			</a>
		</div>

		<?php
	}

	/**
	 * Set the nag for partially ignored users.
	 */
	public function pro_theme_notice_partial_ignore() {

		$this->dismiss( 'lively_nag_pro_theme_notice_partial_ignore' );
	}

	/**
	 * Record a dismissal against the current user.
	 *
	 * @param string $key Query argument and user meta key.
	 */
	protected function dismiss( $key ) {

		if ( ! isset( $_GET[ $key ] ) || '1' !== $_GET[ $key ] ) {
			return;
		}

		if ( ! current_user_can( 'edit_theme_options' ) ) {
			return;
		}

		$nonce = isset( $_GET['_wpnonce'] ) ? sanitize_key( wp_unslash( $_GET['_wpnonce'] ) ) : '';

		if ( ! wp_verify_nonce( $nonce, $key ) ) {
			return;
		}

		update_user_meta( get_current_user_id(), $key, time() );
	}

	/**
	 * Set the nag for permanently ignored users.
	 */
	public function pro_theme_notice_ignore() {

		$this->dismiss( 'lively_nag_pro_theme_notice_ignore' );
	}
}

new Lively_Theme_Notice();
