<?php
/**
 * Gridpress admin notices
 *
 * @package Gridpress
 */
defined('ABSPATH') || exit;
if ( ! class_exists( 'Gridpress_Admin_Notices' ) ) {

    class Gridpress_Admin_Notices
    {
        private static $gridpress_notices = [];

        private static $gridpress_core_notices = [
            'install_setup' => '_gridpress_admin_notice_install_setup',
        ];

        public function __construct()
        {
            self::$gridpress_notices = get_option('gridpress_plugin_admin_notices');

            add_action('admin_init', array($this, 'gridpress_add_notices'));
            add_action('wp_loaded', array(__CLASS__, 'gridpress_hide_notices'));
        }

        /**
         * Initiate admin notices
         */
        public function gridpress_add_notices()
        {

            $gridpress_all_notices = self::$gridpress_notices;

            if (empty($gridpress_all_notices)) {
                return;
            }

            foreach ($gridpress_all_notices as $gridpress_notice) {

                if (
                    isset(self::$gridpress_core_notices[$gridpress_notice]) &&
                    ! get_transient(self::$gridpress_core_notices[$gridpress_notice]) &&
                    apply_filters('gridpress_show_admin_notice', true, $gridpress_notice)
                ) {
                    // add_action('admin_notices', array(__CLASS__, $gridpress_notice), 20);
                }
            }
        }

        /**
         * Add notice
         */
        public static function gridpress_add_notice($gridpress_notice)
        {

            $gridpress_all_notices = self::$gridpress_notices;
            $gridpress_all_notices = empty($gridpress_all_notices) ? [] : $gridpress_all_notices;

            if (! in_array($gridpress_notice, $gridpress_all_notices, true)) {

                $gridpress_all_notices[] = $gridpress_notice;
                update_option('gridpress_plugin_admin_notices', $gridpress_all_notices);

                if (isset(self::$gridpress_core_notices[$gridpress_notice])) {
                    $gridpress_transient = self::$gridpress_core_notices[$gridpress_notice];

                    if (get_transient($gridpress_transient)) {
                        delete_transient($gridpress_transient);
                    }
                }
            }
        }

        /**
         * Remove notice
         */
        public static function gridpress_remove_notice($gridpress_notice)
        {

            $gridpress_all_notices = self::$gridpress_notices;

            if (! empty($gridpress_all_notices) && in_array($gridpress_notice, $gridpress_all_notices, true)) {

                $gridpress_notice_key = array_search($gridpress_notice, $gridpress_all_notices, true);

                unset($gridpress_all_notices[$gridpress_notice_key]);

                $gridpress_all_notices = array_values($gridpress_all_notices);

                update_option('gridpress_plugin_admin_notices', $gridpress_all_notices);
            }
        }

        /**
         * Install setup notice
         */
        public static function gridpress_install_setup()
        {

            global $pagenow, $post;

            $gridpress_notice = 'install_setup';

            if (
                !isset($_POST['gridpress_nonce']) ||
                !wp_verify_nonce(sanitize_text_field( wp_unslash($_POST['gridpress_nonce'])), 'gridpress_admin_nonce')
            ) {
                wp_send_json([
                    'error' => 'security_failed'
                ]);
            }

            $gridpress_request_post_type = isset($_GET['post_type'])
                ? sanitize_key(wp_unslash($_GET['post_type']))
                : '';
            if (
                $pagenow === 'index.php' ||
                $pagenow === 'plugins.php' ||
                ($pagenow === 'edit.php' && $gridpress_request_post_type === 'gridpress_grid') ||
                ($pagenow === 'post-new.php' && $gridpress_request_post_type === 'gridpress_grid') ||
                ($pagenow === 'post.php' && isset($post->post_type) && $post->post_type === 'gridpress_grid')
            ) {
                ?>
                <div class="wpgps-admin-notice wpgps-installsetup-notice notice notice-info">
                    <p>
                        <strong><?php esc_html_e('Welcome to Gridpress', 'gridpress-grid-layout-design'); ?></strong>
                        &#8211;
                        <?php esc_html_e('You are almost ready. Please run the setup wizard.', 'gridpress-grid-layout-design'); ?>
                    </p>
                    <p class="submit">
                        <a href="<?php echo esc_url(admin_url('admin.php?page=gridpress-setup-wizard')); ?>" class="button-primary">
                            <?php esc_html_e('Run Setup Wizard', 'gridpress-grid-layout-design'); ?>
                        </a>

                        <a class="button-secondary skip"
                            href="<?php echo esc_url(
                                        wp_nonce_url(
                                            add_query_arg('gridpress-hide-notice', $gridpress_notice),
                                            'gridpress_hide_notices_nonce',
                                            '_gridpress_notice_nonce'
                                        )
                                    ); ?>">
                            <?php esc_html_e('Skip setup', 'gridpress-grid-layout-design'); ?>
                        </a>
                    </p>
                </div>
                <?php
            }
        }

        /**
         * Hide notices
         */
        public static function gridpress_hide_notices()
        {

            if (
                isset($_GET['gridpress-hide-notice'], $_GET['_gridpress_notice_nonce'])
            ) {
                $gridpress_notice = sanitize_text_field(wp_unslash($_GET['gridpress-hide-notice']));

                if (! wp_verify_nonce(
                    sanitize_key(wp_unslash($_GET['_gridpress_notice_nonce'])),
                    'gridpress_hide_notices_nonce'
                )) {
                    wp_die(esc_html__('Action failed. Please refresh the page and retry.', 'gridpress-grid-layout-design'));
                }

                if (isset(self::$gridpress_core_notices[$gridpress_notice])) {

                    $gridpress_transient = self::$gridpress_core_notices[$gridpress_notice];

                    set_transient($gridpress_transient, true, 30 * DAY_IN_SECONDS);
                }
            }
        }
    }
    new Gridpress_Admin_Notices();
}