<?php

/**
 * file for holding dashboard welcome page for theme
 */
if (!function_exists('storemate_is_plugin_installed')) {
    function storemate_is_plugin_installed($plugin_slug)
    {
        $plugin_path = WP_PLUGIN_DIR . '/' . $plugin_slug;
        return file_exists($plugin_path);
    }
}
if (!function_exists('storemate_is_plugin_activated')) {
    function storemate_is_plugin_activated($plugin_slug)
    {
        return is_plugin_active($plugin_slug);
    }
}
if (!function_exists('storemate_welcome_notice')) :
    function storemate_welcome_notice()
    {
        if (get_option('storemate_dismissed_custom_notice')) {
            return;
        }
        global $pagenow;
        $current_screen  = get_current_screen();

        if (is_admin()) {
            if ($current_screen->id !== 'dashboard' && $current_screen->id !== 'themes') {
                return;
            }
            if (is_network_admin()) {
                return;
            }
            if (!current_user_can('manage_options')) {
                return;
            }
            $theme = wp_get_theme();

            if (is_child_theme()) {
                $theme = wp_get_theme()->parent();
            }
            $storemate_version = $theme->get('Version');


?>
            <div class="storemate-admin-notice notice notice-info is-dismissible content-install-plugin theme-info-notice" id="storemate-dismiss-notice">
                <div class="info-content">
                    <div class="notice-holder">
                        <h5><span class="theme-name"><span><?php echo __('Welcome to storemate', 'storemate'); ?></span></h5>
                        <h1><?php echo __('Optimize your workflow effortlessly! Cozy Blocks pairs perfectly with our 30+ Advanced Blocks—install for full benefits', 'storemate'); ?></h1>
                        </h3>
                        <div class="notice-text">
                            <p><?php echo __('Enhance your experience with our feature-rich 30+ Advanced Blocks! Unlock the full potential by integrating Cozy Addons today. Explore a multitude of powerful tools and functionalities designed to elevate your user experience.', 'storemate') ?></p>
                            <p><?php echo __('Please install and activate all recommended to use blcoks and demo importer features- Cozy Addons, Cozy Essential Addons, Advanced Import.', 'storemate') ?></p>
                        </div>
                        <a href="#" id="install-activate-button" class="button admin-button info-button"><?php echo __('Getting started with a single click', 'storemate'); ?></a>
                        <a href="<?php echo admin_url(); ?>themes.php?page=about-storemate" class="button admin-button info-button"><?php echo __('Explore Storemate', 'storemate'); ?></a>

                    </div>
                </div>
                <div class="theme-hero-screens">
                    <img src="<?php echo esc_url(get_template_directory_uri() . '/inc/admin/images/screen_plugin_ct.png'); ?>" />
                </div>

            </div>
    <?php
        }
    }
endif;
add_action('admin_notices', 'storemate_welcome_notice');
function storemate_dismissble_notice()
{
    if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'storemate_admin_nonce')) {
        wp_send_json_error(array('message' => 'Nonce verification failed.'));
        return;
    }

    $result = update_option('storemate_dismissed_custom_notice', 1);

    if ($result) {
        wp_send_json_success();
    } else {
        wp_send_json_error(array('message' => 'Failed to update option'));
    }
}
add_action('wp_ajax_storemate_dismissble_notice', 'storemate_dismissble_notice');
// Hook into a custom action when the button is clicked
add_action('wp_ajax_storemate_install_and_activate_plugins', 'storemate_install_and_activate_plugins');
add_action('wp_ajax_nopriv_storemate_install_and_activate_plugins', 'storemate_install_and_activate_plugins');
add_action('wp_ajax_storemate_rplugin_activation', 'storemate_rplugin_activation');
add_action('wp_ajax_nopriv_storemate_rplugin_activation', 'storemate_rplugin_activation');

// Function to install and activate the plugins



function check_plugin_installed_status($pugin_slug, $plugin_file)
{
    return file_exists(ABSPATH . 'wp-content/plugins/' . $pugin_slug . '/' . $plugin_file) ? true : false;
}

/* Check if plugin is activated */


function check_plugin_active_status($pugin_slug, $plugin_file)
{
    return is_plugin_active($pugin_slug . '/' . $plugin_file) ? true : false;
}

require_once(ABSPATH . 'wp-admin/includes/plugin-install.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/misc.php');
require_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php');
function storemate_install_and_activate_plugins()
{
    if (!current_user_can('manage_options')) {
        return;
    }
    check_ajax_referer('storemate_welcome_nonce', 'nonce');
    // Define the plugins to be installed and activated
    $recommended_plugins = array(
        array(
            'slug' => 'cozy-addons',
            'file' => 'cozy-addons.php',
            'name' => 'Cozy Addons'
        ),
        array(
            'slug' => 'advanced-import',
            'file' => 'advanced-import.php',
            'name' => 'Advanced Imporrt'
        ),
        array(
            'slug' => 'cozy-essential-addons',
            'file' => 'cozy-essential-addons.php',
            'name' => 'Cozy Essential Addons'
        ),
        // Add more plugins here as needed
    );

    // Include the necessary WordPress functions


    // Set up a transient to store the installation progress
    set_transient('install_and_activate_progress', array(), MINUTE_IN_SECONDS * 10);

    // Loop through each plugin
    foreach ($recommended_plugins as $plugin) {
        $plugin_slug = $plugin['slug'];
        $plugin_file = $plugin['file'];
        $plugin_name = $plugin['name'];

        // Check if the plugin is active
        if (is_plugin_active($plugin_slug . '/' . $plugin_file)) {
            update_install_and_activate_progress($plugin_name, 'Already Active');
            continue; // Skip to the next plugin
        }

        // Check if the plugin is installed but not active
        if (is_storemate_plugin_installed($plugin_slug . '/' . $plugin_file)) {
            $activate = activate_plugin($plugin_slug . '/' . $plugin_file);
            if (is_wp_error($activate)) {
                update_install_and_activate_progress($plugin_name, 'Error');
                continue; // Skip to the next plugin
            }
            update_install_and_activate_progress($plugin_name, 'Activated');
            continue; // Skip to the next plugin
        }

        // Plugin is not installed or activated, proceed with installation
        update_install_and_activate_progress($plugin_name, 'Installing');

        // Fetch plugin information
        $api = plugins_api('plugin_information', array(
            'slug' => $plugin_slug,
            'fields' => array('sections' => false),
        ));

        // Check if plugin information is fetched successfully
        if (is_wp_error($api)) {
            update_install_and_activate_progress($plugin_name, 'Error');
            continue; // Skip to the next plugin
        }

        // Set up the plugin upgrader
        $upgrader = new Plugin_Upgrader();
        $install = $upgrader->install($api->download_link);

        // Check if installation is successful
        if ($install) {
            // Activate the plugin
            $activate = activate_plugin($plugin_slug . '/' . $plugin_file);

            // Check if activation is successful
            if (is_wp_error($activate)) {
                update_install_and_activate_progress($plugin_name, 'Error');
                continue; // Skip to the next plugin
            }
            update_install_and_activate_progress($plugin_name, 'Activated');
        } else {
            update_install_and_activate_progress($plugin_name, 'Error');
        }
    }

    // Delete the progress transient
    $redirect_url = admin_url('themes.php?page=advanced-import');

    // Delete the progress transient
    delete_transient('install_and_activate_progress');
    // Return JSON response
    wp_send_json_success(array('redirect_url' => $redirect_url));
}

// Function to check if a plugin is installed but not active
function is_storemate_plugin_installed($plugin_slug)
{
    $plugins = get_plugins();
    return isset($plugins[$plugin_slug]);
}

// Function to update the installation and activation progress
function update_install_and_activate_progress($plugin_name, $status)
{
    $progress = get_transient('install_and_activate_progress');
    $progress[] = array(
        'plugin' => $plugin_name,
        'status' => $status,
    );
    set_transient('install_and_activate_progress', $progress, MINUTE_IN_SECONDS * 10);
}


function storemate_dashboard_menu()
{
    add_theme_page(esc_html__('About Storemate', 'storemate'), esc_html__('About Storemate', 'storemate'), 'edit_theme_options', 'about-storemate', 'storemate_theme_info_display');
}
add_action('admin_menu', 'storemate_dashboard_menu');
function storemate_theme_info_display()
{ ?>
    <div class="dashboard-about-storemate">
        <div class="storemate-dashboard">
            <ul id="storemate-dashboard-tabs-nav">
                <li><a href="#storemate-welcome"><?php echo __('Get Started', 'storemate') ?></a></li>
                <li><a href="#storemate-setup"><?php echo __('Setup Instruction', 'storemate') ?></a></li>
                <li><a href="#storemate-comparision"><?php echo __('FREE VS PRO', 'storemate') ?></a></li>
            </ul> <!-- END tabs-nav -->
            <div id="tabs-content">
                <div id="storemate-welcome" class="tab-content">
                    <h1> <?php echo __('Welcome to the Storemate - Ultimate WooCommerce Theme for Shopping', 'storemate') ?></h1>
                    <p><?php echo __('Introducing Storemate: the ultimate ecommerce solution for WordPress users. Seamlessly compatible with WordPress\'s native editor, including Full Site Editing, and the Gutenberg Editor, Storemate empowers you to effortlessly create stunning online shops. With its minimal and Full Site Editing features, you can tailor your website to any requirement with ease. Explore an extensive selection of ready-to-use patterns, offering more than 35 options, to craft a visually captivating store that sets you apart from the competition. Plus, enjoy the convenience of 7 global styles that facilitate switching your favorite color scheme with just one click. Storemate is fully customizable, allowing you to achieve any type of design from scratch. Jumpstart your online venture with a complimentary ready-to-import demo site, and for those craving further customization, unlock 3 additional premium demos. Harness the power of Storemate to dominate the ecommerce landscape and transform your vision into reality. Explore more about Storemate at https://cozythemes.com/storemate-free-wordpress-theme-for-ecommerce/
', 'storemate') ?></p>
                    <h3><?php echo __('Required Plugins', 'storemate'); ?></h3>
                    <p class="notice-text"><?php echo __('Please install and activate all recommended pluign to import the demo with "one click demo import" feature and unlock premium features!(for pro version)', 'storemate') ?></p>
                    <ul class="storemate-required-plugin">
                        <li>

                            <h4><?php echo __('1. Cozy Addons', 'storemate'); ?>
                                <?php
                                if (storemate_is_plugin_activated('cozy-addons/cozy-addons.php')) {
                                    echo __(': Plugin has been already activated!', 'storemate');
                                } elseif (storemate_is_plugin_installed('cozy-addons/cozy-addons.php')) {
                                    echo __(': Plugin does not activated, Activate the plugin to use one click demo import and unlock premium features.', 'storemate');
                                } else {
                                    echo ': <a href="' . get_admin_url() . 'plugin-install.php?tab=plugin-information&plugin=cozy-addons&TB_iframe=true&width=600&height=550">' . esc_html__('Install and Activate', 'storemate') . '</a>';
                                }
                                ?>
                            </h4>
                        </li>
                        <li>

                            <h4><?php echo __('2. Cozy Essential Addons', 'storemate'); ?>
                                <?php
                                if (storemate_is_plugin_activated('cozy-essential-addons/cozy-essential-addons.php')) {
                                    echo __(': Plugin has been already activated!', 'storemate');
                                } elseif (storemate_is_plugin_installed('cozy-essential-addons/cozy-essential-addons.php')) {
                                    echo __(': Plugin does not activated, Activate the plugin to use one click demo import and unlock premium features.', 'storemate');
                                } else {
                                    echo ': <a href="' . get_admin_url() . 'plugin-install.php?tab=plugin-information&plugin=cozy-essential-addons&TB_iframe=true&width=600&height=550">' . esc_html__('Install and Activate', 'storemate') . '</a>';
                                }
                                ?>
                            </h4>
                        </li>
                        <li>
                            <h4><?php echo __('3. Advanced Import - Need only to use "one click demo import" features', 'storemate'); ?>
                                <?php
                                if (storemate_is_plugin_activated('advanced-import/advanced-import.php')) {
                                    echo __(': Plugin has been already activated!', 'storemate');
                                } elseif (storemate_is_plugin_installed('advanced-import/advanced-import.php')) {
                                    echo __(': Plugin does not activated, Activate the plugin to use one click demo import feature.', 'storemate');
                                } else {
                                    echo ': <a href="' . get_admin_url() . 'plugin-install.php?tab=plugin-information&plugin=advanced-import&TB_iframe=true&width=600&height=550">' . esc_html__('Install and Activate', 'storemate') . '</a>';
                                }
                                ?>
                            </h4>
                        </li>
                    </ul>
                    <a href="#" id="install-activate-button" class="button admin-button info-button"><?php echo __('Getting started with a single click', 'storemate'); ?></a>
                </div>
                <div id="storemate-setup" class="tab-content">
                    <h3 class="storemate-baisc-guideline-header"><?php echo __('Basic Theme Setup', 'storemate') ?></h3>
                    <div class="storemate-baisc-guideline">
                        <div class="featured-box">
                            <ul>
                                <li><strong><?php echo __('Setup Header Layout:', 'storemate') ?></strong>
                                    <ul>
                                        <li> - <?php echo __('Go to Appearance -> Editor -> Patterns -> Template Parts -> Header:', 'storemate') ?></li>
                                        <li> - <?php echo __('click on Header > Click on Edit (Icon) -> Add or Remove Requirend block/content as your requirement.:', 'storemate') ?></li>
                                        <li> - <?php echo __('Click on save to update your layout', 'storemate') ?></li>
                                    </ul>
                                </li>
                            </ul>
                        </div>
                        <div class="featured-box">
                            <ul>
                                <li><strong><?php echo __('Setup Footer Layout:', 'storemate') ?></strong>
                                    <ul>
                                        <li> - <?php echo __('Go to Appearance -> Editor -> Patterns -> Template Parts -> Footer:', 'storemate') ?></li>
                                        <li> - <?php echo __('click on Footer > Click on Edit (Icon) > Add or Remove Requirend block/content as your requirement.:', 'storemate') ?></li>
                                        <li> - <?php echo __('Click on save to update your layout', 'storemate') ?></li>
                                    </ul>
                                </li>
                            </ul>
                        </div>
                        <div class="featured-box">
                            <ul>
                                <li><strong><?php echo __('Setup Templates like Homepage/404/Search/Page/Single and more templates Layout:', 'storemate') ?></strong>
                                    <ul>
                                        <li> - <?php echo __('Go to Appearance -> Editor -> Templates:', 'storemate') ?></li>
                                        <li> - <?php echo __('click on Template(You need to edit/update) > Click on Edit (Icon) > Add or Remove Requirend block/content as your requirement.:', 'storemate') ?></li>
                                        <li> - <?php echo __('Click on save to update your layout', 'storemate') ?></li>
                                    </ul>
                                </li>
                            </ul>
                        </div>
                        <div class="featured-box">
                            <ul>
                                <li><strong><?php echo __('Restore/Reset Default Content layout of Template(Like: Frontpage/Blog/Archive etc.)', 'storemate') ?></strong>
                                    <ul>
                                        <li> - <?php echo __('Go to Appearance -> Editor -> Templates:', 'storemate') ?></li>
                                        <li> - <?php echo __('Click on Manage all Templates', 'storemate') ?></li>
                                        <li> - <?php echo __('Click on 3 Dots icon at right side of respective Template', 'storemate') ?></li>
                                        <li> - <?php echo __('Click on Clear Customization', 'storemate') ?></li>
                                    </ul>
                                </li>
                            </ul>
                        </div>
                        <div class="featured-box">
                            <ul>
                                <li><strong><?php echo __('Restore/Reset Default Content layout of Template Parts(Header/Footer/Sidebar)', 'storemate') ?></strong>
                                    <ul>
                                        <li> - <?php echo __('Go to Appearance -> Editor -> Patterns:', 'storemate') ?></li>
                                        <li> - <?php echo __('Click on Manage All Template Parts', 'storemate') ?></li>
                                        <li> - <?php echo __('Click on 3 Dots icon at right side of respective Template parts', 'storemate') ?></li>
                                        <li> - <?php echo __('Click on Clear Customization', 'storemate') ?></li>
                                    </ul>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
                <div id="storemate-comparision" class="tab-content">
                    <div class="featured-list">
                        <div class="half-col free-features">
                            <h3><?php echo __('Storemate Features (Free)', 'storemate') ?></h3>
                            <ul>
                                <li><strong> - <?php echo __('Storemate offer wide range of  ready to use Home Sections Patterns', 'storemate') ?></strong>
                                    <ul>
                                        <li><?php echo __('About Us Section - 2', 'storemate') ?></li>
                                        <li><?php echo __('Download our App Section', 'storemate') ?></li>
                                        <li><?php echo __('Featured Banner Section', 'storemate') ?></li>
                                        <li><?php echo __('Featured Box Section to display ads or featured category', 'storemate') ?></li>
                                        <li><?php echo __('Featured Categories Section - 2', 'storemate') ?></li>
                                        <li><?php echo __('Product Display Section - 5', 'storemate') ?></li>
                                        <li><?php echo __('Offer and Advertisement Display Section - 5+', 'storemate') ?></li>
                                        <li><?php echo __('Newsletter Section - 2', 'storemate') ?></li>
                                        <li><?php echo __('Brands Showcase Display Section', 'storemate') ?></li>
                                        <li><?php echo __('Latest Post Display Section', 'storemate') ?></li>
                                        <li><?php echo __('Testimonials Display Section', 'storemate') ?></li>
                                        <li><?php echo __('Store Features  Section - 2', 'storemate') ?></li>
                                    </ul>
                                </li>

                                <li> <strong>- <?php echo __('FSE Templates Ready', 'storemate') ?></strong>
                                    <ul>
                                        <li> <?php echo __('404 Template', 'storemate') ?></li>
                                        <li> <?php echo __('Archive Template', 'storemate') ?></li>
                                        <li> <?php echo __('Product Catalog Template', 'storemate') ?></li>
                                        <li> <?php echo __('Blank Template', 'storemate') ?></li>
                                        <li> <?php echo __('Front Page Template', 'storemate') ?></li>
                                        <li> <?php echo __('Blog Home Template', 'storemate') ?></li>
                                        <li> <?php echo __('Index Page Template', 'storemate') ?></li>
                                        <li> <?php echo __('Search Template', 'storemate') ?></li>
                                        <li> <?php echo __('Sitemap Template', 'storemate') ?></li>
                                        <li> <?php echo __('Page Template', 'storemate') ?></li>
                                        <li> <?php echo __('Left Sidebar Page Template', 'storemate') ?></li>
                                        <li> <?php echo __('Right sidebar page  Template', 'storemate') ?></li>
                                        <li> <?php echo __('Single Template', 'storemate') ?></li>
                                        <li> <?php echo __('Single Product Template', 'storemate') ?></li>
                                        <li> <?php echo __('Cart Page Template', 'storemate') ?></li>
                                        <li> <?php echo __('Checkout Page Template', 'storemate') ?></li>
                                        <li> <?php echo __('Left Sidebar Single Template', 'storemate') ?></li>
                                        <li> <?php echo __('Full Width Single Template', 'storemate') ?></li>

                                    </ul>
                                <li>
                                <li><strong> - <?php echo __('Fully Customizable Header Layout - 5', 'storemate') ?></strong></li>
                                <li> <strong>- <?php echo __('Fully Customizable Footer Layout - 4 ', 'storemate') ?></strong></li>
                                <li><strong> - <?php echo __('12 Beautiful Fonts Option', 'storemate') ?></strong></li>
                                <li> <strong>- <?php echo __('7 Global Styles Variations', 'storemate') ?></strong></li>
                                <li> <strong>- <?php echo __('On Scroll Animation option', 'storemate') ?></strong></li>
                                <li> <strong>- <?php echo __('Access Cozy Blocks with upto 22 Advanced Blocks for FSE and Gutenberg Editor', 'storemate') ?></strong></li>
                            </ul>
                        </div>
                        <div class="half-col pro-features">
                            <h3><?php echo __('Premium Features', 'storemate') ?></h3>
                            <p><?php echo __('Including all free features and comes with 30+ advanced blocks that enhance and power up the ecommerce website, here are some blocks that add the powerful features for your ecommerce/shopping website. By seamlessly integrating these blocks with our ready-to-use patterns, you have the flexibility to craft a wide selection of captivating online store ease', 'storemate') ?></p>
                            <ul>
                                <li><?php echo __('Product slider', 'storemate') ?></li>
                                <li><?php echo __('Product Grid/Carousel', 'storemate') ?></li>
                                <li><?php echo __('Product Category', 'storemate') ?></li>
                                <li><?php echo __('Products Showcase Tab', 'storemate') ?></li>
                                <li><?php echo __('All Product Reviews', 'storemate') ?></li>
                                <li><?php echo __('Post Blocks', 'storemate') ?></li>
                                <li><?php echo __('Slider Block', 'storemate') ?></li>
                                <li><?php echo __('Counter Block', 'storemate') ?></li>
                                <li><?php echo __('Social Shares Blocks', 'storemate') ?></li>
                                <li><?php echo __('Social Icons Blocks', 'storemate') ?></li>
                                <li><?php echo __('Breadcrumbs Block', 'storemate') ?></li>
                                <li><?php echo __('Popup buidler Block to display offer and flash sale', 'storemate') ?>
                                    <?php echo __('and access', 'storemate') ?> <a href="https://cozythemes.com/cozy-addons/" target="_blank"><?php echo __('Cozy Blocks with more than 30+ advanced block.', 'storemate') ?></a>
                                </li>

                            </ul>
                            <br />
                            <h3><?php echo __('Extra', 'storemate') ?></h3>
                            <ul>
                                <li><?php echo __('Additional 3 Pre-build ready to one click import site demo', 'storemate') ?></li>


                            </ul>
                            <a href="https://cozythemes.com/pricing-and-plans/" class="upgrade-btn button" target="_blank"><?php echo __('Upgrade to Pro', 'storemate') ?></a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php
}
