<?php
require_once get_template_directory() . '/includes/welcome/welcome-page.php';

function vividone_show_welcome_notice() {

    if (!is_admin()) {
        return;
    }
    /**
     * Dismiss notice
     */
    if (get_user_meta(get_current_user_id(), 'vividone_notice_dismissed', true)) {
        return;
    }

    global $pagenow;
    if ($pagenow === 'themes.php' && isset($_GET['activated'])) {

        $theme = wp_get_theme();
        $theme_name = esc_html($theme->get('Name'));
        $welcome_url = esc_url(admin_url('themes.php?page=vividone-welcome'));
        $theme_url = esc_url('https://themesmachine.com/index.php/product/vividone/');

        echo '<div class="notice notice-success is-dismissible vividone-notice">
            <p><strong>' . sprintf(__('Thanks for activating %s!', 'vividone'), $theme_name) . '</strong></p>
            <p>' . esc_html__('Check the Welcome Page to learn how to get started with the theme.', 'vividone') . '</p>
            <p>
                <a href="' . $welcome_url . '" class="button button-primary">' . esc_html__('Welcome Page', 'vividone') . '</a>
                <a href="' . $theme_url . '" class="button" target="_blank" rel="noopener">' . esc_html__('Theme Page', 'vividone') . '</a>
            </p>
        </div>';
    }
}
add_action('admin_notices', 'vividone_show_welcome_notice');


/**
 * Save dismiss
 */
function vividone_dismiss_notice()
{
    update_user_meta(get_current_user_id(), 'vividone_notice_dismissed', true);
    wp_send_json_success();
}
add_action('wp_ajax_vividone_dismiss_notice', 'vividone_dismiss_notice');


/**
 * Dismiss js
 */
function vividone_show_welcome_notice_script($hook)
{
    if ($hook !== 'themes.php') {
        return;
    }
    ?>
    <script>
    jQuery(document).on('click', '.vividone-notice .notice-dismiss', function () {
        jQuery.post(ajaxurl, { action: 'vividone_dismiss_notice' });
    });
    </script>
    <?php
}
add_action('admin_footer', 'vividone_show_welcome_notice_script');

