<?php
// Exit if accessed directly.
defined('ABSPATH') || exit;

class NewSpare_Notice
{
    public $name;
    public $type;
    public $dismiss_url;
    public $temporary_dismiss_url;
    public $pricing_url;
    public $current_user_id;

    /**
     * The constructor.
     *
     * @param string $name Notice Name.
     * @param string $type Notice type.
     * @param string $dismiss_url Notice permanent dismiss URL.
     * @param string $temporary_dismiss_url Notice temporary dismiss URL.
     *
     * @since 1.0.0
     *
     */
    public function __construct($name, $type, $dismiss_url, $temporary_dismiss_url)
    {
        $this->name = $name;
        $this->type = $type;
        $this->dismiss_url = $dismiss_url;
        $this->temporary_dismiss_url = $temporary_dismiss_url;
        $this->pricing_url = 'https://afthemes.com/products/newspare/';
        $this->current_user_id = get_current_user_id();

        // Notice markup.
        add_action('admin_notices', array($this, 'notice'));

        $this->dismiss_notice();
        $this->dismiss_notice_temporary();
    }

    public function notice()
    {
        if (!$this->is_dismiss_notice()) {
            $this->notice_markup();
        }
    }

    private function is_dismiss_notice()
    {
        return apply_filters('newspare_' . $this->name . '_notice_dismiss', true);
    }

    public function notice_markup()
    {
        echo '';
    }

    /**
     * Hide a notice if the GET variable is set.
     */
    public function dismiss_notice()
    {
        if (isset($_GET['newspare_upgrade_notice_dismiss']) && isset($_GET['_newspare_upgrade_notice_dismiss_nonce'])) { // WPCS: input var ok.
            if (!wp_verify_nonce(wp_unslash($_GET['_newspare_upgrade_notice_dismiss_nonce']), 'newspare_upgrade_notice_dismiss_nonce')) { // phpcs:ignore WordPress.VIP.ValidatedSanitizedInput.InputNotSanitized
                wp_die(__('Action failed. Please refresh the page and retry.', 'newspare')); // WPCS: xss ok.
            }

            if (!current_user_can('publish_posts')) {
                wp_die(__('Cheatin&#8217; huh?', 'newspare')); // WPCS: xss ok.
            }

            $dismiss_notice = sanitize_text_field(wp_unslash($_GET['newspare_upgrade_notice_dismiss']));

            // Hide.
            if ($dismiss_notice === $_GET['newspare_upgrade_notice_dismiss']) {
                add_user_meta(get_current_user_id(), 'newspare_' . $dismiss_notice . '_notice_dismiss', 'yes', true);
            }
        }
    }

    public function dismiss_notice_temporary()
    {
        if (isset($_GET['newspare_upgrade_notice_dismiss_temporary']) && isset($_GET['_newspare_upgrade_notice_dismiss_temporary_nonce'])) { // WPCS: input var ok.
            if (!wp_verify_nonce(wp_unslash($_GET['_newspare_upgrade_notice_dismiss_temporary_nonce']), 'newspare_upgrade_notice_dismiss_temporary_nonce')) { // phpcs:ignore WordPress.VIP.ValidatedSanitizedInput.InputNotSanitized
                wp_die(__('Action failed. Please refresh the page and retry.', 'newspare')); // WPCS: xss ok.
            }

            if (!current_user_can('publish_posts')) {
                wp_die(__('Cheatin&#8217; huh?', 'newspare')); // WPCS: xss ok.
            }

            $dismiss_notice = sanitize_text_field(wp_unslash($_GET['newspare_upgrade_notice_dismiss_temporary']));

            // Hide.
            if ($dismiss_notice === $_GET['newspare_upgrade_notice_dismiss_temporary']) {
                add_user_meta(get_current_user_id(), 'newspare_' . $dismiss_notice . '_notice_dismiss_temporary', 'yes', true);
            }
        }
    }
}


class NewSpare_Upgrade_Notice extends NewSpare_Notice
{

    public function __construct()
    {
        if (!current_user_can('publish_posts')) {
            return;
        }

        $dismiss_url = wp_nonce_url(
            add_query_arg('newspare_upgrade_notice_dismiss', 'upgrade', admin_url()),
            'newspare_upgrade_notice_dismiss_nonce',
            '_newspare_upgrade_notice_dismiss_nonce'
        );

        $temporary_dismiss_url = wp_nonce_url(
            add_query_arg('newspare_upgrade_notice_dismiss_temporary', 'upgrade', admin_url()),
            'newspare_upgrade_notice_dismiss_temporary_nonce',
            '_newspare_upgrade_notice_dismiss_temporary_nonce'
        );

        parent::__construct('upgrade', 'info', $dismiss_url, $temporary_dismiss_url);

        $this->set_notice_time();

        $this->set_temporary_dismiss_notice_time();

        $this->set_dismiss_notice();
    }

    private function set_notice_time()
    {
        if (!get_option('newspare_upgrade_notice_start_time')) {
            update_option('newspare_upgrade_notice_start_time', time());
        }
    }

    private function set_temporary_dismiss_notice_time()
    {
        if (isset($_GET['newspare_upgrade_notice_dismiss_temporary']) && 'upgrade' === $_GET['newspare_upgrade_notice_dismiss_temporary']) {
            update_user_meta($this->current_user_id, 'newspare_upgrade_notice_dismiss_temporary_start_time', time());
        }
    }

    public function set_dismiss_notice()
    {

        /**
         * Do not show notice if:
         *
         * 1. It has not been 5 days since the theme is activated.
         * 2. If the user has ignored the message partially for 2 days.
         * 3. Dismiss always if clicked on 'Dismiss' button.
         */
        if (
            get_option('newspare_upgrade_notice_start_time') > strtotime('-7 days')
            || get_user_meta(get_current_user_id(), 'newspare_upgrade_notice_dismiss', true)
            || get_user_meta(get_current_user_id(), 'newspare_upgrade_notice_dismiss_temporary_start_time', true) > strtotime('-2 day')
        ) {
            add_filter('newspare_upgrade_notice_dismiss', '__return_true');
        } else {
            add_filter('newspare_upgrade_notice_dismiss', '__return_false');
        }
    }

    public function notice_markup()
    {
?>
        <div class="notice updated theme-review-notice aft-notice-content-wrapper">
            <a class="newspare-notice-dismiss notice-dismiss" href="<?php echo esc_url($this->dismiss_url); ?>"></a>

            <p class="notice-text">
                <?php
                $current_user = wp_get_current_user();

                $theme_url = esc_url('https://afthemes.com/products/newspare/');

                printf(
                    /* translators: 1: current user display name, 2: theme name link, 3: discount coupon code, 4: discount percentage */
                    wp_kses_post(
                        
                        __(
                            '%1$s 🎁 You\'re currently using the free version of %2$s — thank you!! Upgrade to Pro to access advanced layouts, blocks, premium demos, and many powerful features designed to help you build a faster, more professional news or magazine site. As a thank-you for using our theme, you can unlock %4$s premium features using this exclusive coupon: %3$s',
                            'newspare'
                        )
                    ),
                    
                    '<h3> Greetings ' . esc_html($current_user->display_name) . '! Ready to unlock more powerful features in NewSpare?</h3>',
                    '<a href="' . esc_url($theme_url) . '">NewSpare</a>',
                    '<code class="coupon-code">ReadyMadeWebsites</code>',
                    '<strong>50% off</strong>'
                );
                ?>
            </p>

            <div class="links">
                <a href="<?php echo esc_url($this->pricing_url); ?>" class="button button-primary" target="_blank">

                    <span><?php esc_html_e('Unlock Pro Features', 'newspare'); ?></span>
                </a>

                
                <a href="https://afthemes.com/all-themes-plan/" class="button button-secondary" target="_blank">

                    <span><?php esc_html_e('View All Themes', 'newspare'); ?></span>
                </a>

                <a href="<?php echo esc_url($this->temporary_dismiss_url); ?>" class="button button-normal">

                    <span><?php esc_html_e('Maybe Later', 'newspare'); ?></span>
                </a>

                <a href="https://afthemes.com/supports/" class="button button-normal" target="_blank">

                    <span><?php esc_html_e('Need Help?', 'newspare'); ?></span>
                </a>
            </div>
        </div> <!-- /newspare-notice -->
<?php
    }
}

new NewSpare_Upgrade_Notice();
