<?php

/**
* Get started notice
*/

//  Plugin Activation Start
if (!function_exists('misbah_architecture_blocks_is_plugin_installed')) {
    function misbah_architecture_blocks_is_plugin_installed($plugin_slug)
    {
        $plugin_path = WP_PLUGIN_DIR . '/' . $plugin_slug;
        return file_exists($plugin_path);
    }
}
if (!function_exists('misbah_architecture_blocks_is_plugin_activated')) {
    function misbah_architecture_blocks_is_plugin_activated($plugin_slug)
    {
        return is_plugin_active($plugin_slug);
    }
}

// Hook into a custom action when the button is clicked
add_action('wp_ajax_misbah_architecture_blocks_install_and_activate_plugins', 'misbah_architecture_blocks_install_and_activate_plugins');
add_action('wp_ajax_nopriv_misbah_architecture_blocks_install_and_activate_plugins', 'misbah_architecture_blocks_install_and_activate_plugins');
add_action('wp_ajax_misbah_architecture_blocks_rplugin_activation', 'misbah_architecture_blocks_rplugin_activation');
add_action('wp_ajax_nopriv_misbah_architecture_blocks_rplugin_activation', 'misbah_architecture_blocks_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');
// Helper function to check if all recommended plugins are installed and activated
function misbah_architecture_blocks_all_plugins_active() {
    $recommended_plugins = array(
        array(
            'name' => __( 'CBX Bookmark & Favorite', 'misbah-architecture-blocks' ),
            'slug' => 'cbxwpbookmark',
            'file' => 'cbxwpbookmark.php'
        ),
    );

    foreach ($recommended_plugins as $plugin) {
        $plugin_slug = $plugin['slug'];
        $plugin_file = $plugin['file'];

        // Check if the plugin is active
        if (!is_plugin_active($plugin_slug . '/' . $plugin_file)) {
            return false; // If any plugin is not active, return false
        }
    }

    return true; // All plugins are active
}

class Silent_Skin extends WP_Upgrader_Skin {
    public function header() {}
    public function footer() {}
    public function feedback($string, ...$args) {}
    public function error($errors) {}
    public function before() {}
    public function after() {}
}

// Function to install and activate plugins
function misbah_architecture_blocks_install_and_activate_plugins() {
    if (!current_user_can('manage_options')) {
        return;
    }
    check_ajax_referer('misbah_architecture_blocks_welcome_nonce', 'nonce');

    // Define the recommended plugins
    $recommended_plugins = array(
        array(
            'name' => __( 'Mosaic Gallery Advanced Gallery', 'misbah-architecture-blocks' ),
            'slug' => 'mosaic-gallery-advanced-gallery',
            'file' => 'mosaic-image-gallery.php'
        ),
    );

    set_transient('install_and_activate_progress', array(), MINUTE_IN_SECONDS * 10);

    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');

    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;
        }

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

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

        $api = plugins_api('plugin_information', array('slug' => $plugin_slug, 'fields' => array('sections' => false)));
        if (is_wp_error($api)) {
            update_install_and_activate_progress($plugin_name, 'Error');
            continue;
        }

        $upgrader = new Plugin_Upgrader(new Silent_Skin());
        $install = $upgrader->install($api->download_link);

        if ($install) {
            $activate = activate_plugin($plugin_slug . '/' . $plugin_file);
            if (is_wp_error($activate)) {
                update_install_and_activate_progress($plugin_name, 'Error');
                continue;
            }
            update_install_and_activate_progress($plugin_name, 'Activated');
            continue;
        } else {
            update_install_and_activate_progress($plugin_name, 'Error');
        }
    }
    delete_transient('install_and_activate_progress');
    if (ob_get_length()) ob_clean();

    header('Content-Type: application/json; charset=utf-8');
    $redirect_url = admin_url('themes.php?page=misbah-architecture-blocks-guide-page');
    echo json_encode([
        'success' => true,
        'data' => [
            'redirect_url' => $redirect_url,
        ],
    ]);

    wp_die();
}

// Function to check if a plugin is installed
function is_misbah_architecture_blocks_plugin_installed($plugin_slug) {
    $installed_plugins = get_plugins();
    foreach ($installed_plugins as $path => $details) {
        if (strpos($path, $plugin_slug) === 0) {
            return true;
        }
    }
    return false;
}

// Function to update the installation 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);
}

// Dismiss function for AJAX request
add_action('wp_ajax_misbah_architecture_blocks_dismissed_notice_handler', 'misbah_architecture_blocks_ajax_notice_dismiss_function');

function misbah_architecture_blocks_ajax_notice_dismiss_function() {
    if (!wp_verify_nonce($_POST['wpnonce'], 'misbah_architecture_blocks_welcome_nonce')) {
        wp_send_json_error('Invalid nonce');
        exit;
    }
    
    if (isset($_POST['type'])) {
        $misbah_architecture_blocks_type = sanitize_text_field(wp_unslash($_POST['type']));
        update_option('dismissed-' . $misbah_architecture_blocks_type, true);
        wp_send_json_success('Notice dismissed');
    } else {
        wp_send_json_error('Type not set');
    }
}
//  Plugin Activation End

function misbah_architecture_blocks_deprecated_hook_admin_notice() {
        // Check if it's been dismissed...
        if ( ! get_option('dismissed-get_started_notice', FALSE ) ) {
        $misbah_architecture_blocks_current_screen = get_current_screen();
			if ($misbah_architecture_blocks_current_screen->id !== 'appearance_page_misbah-architecture-blocks-guide-page') {
         $misbah_architecture_blocks_comments_theme = wp_get_theme(); ?>
            <div class="misbah-architecture-blocks-notice-wrapper updated notice notice-get-started-class is-dismissible notice-info" data-notice="get_started_notice">
			<div class="misbah-architecture-blocks-notice">
				<div class="misbah-architecture-blocks-notice-content">
					<div class="misbah-architecture-blocks-notice-heading">
						<h2><?php esc_html_e('Thanks For Installing ','misbah-architecture-blocks'); ?><?php echo esc_html( $misbah_architecture_blocks_comments_theme ); ?> <?php esc_html_e('Theme','misbah-architecture-blocks'); ?></h2>
					<p><?php 
                    /* translators: %s: theme name */
                    printf(esc_html__("%s is now installed and ready to use. We've provide some links to get you started.", 'misbah-architecture-blocks'), $misbah_architecture_blocks_comments_theme ); ?></p>
					</div>
					<div class="diplay-flex-btn">
						<a class="button button-primary button-primary theme-install" id="install-activate-button" href="#"><?php _e('GET STARTED', 'misbah-architecture-blocks'); ?></a>
						<a class="button button-primary" target="_blank" href="<?php echo esc_url( MISBAH_ARCHITECTURE_BLOCKS_BUY_NOW ); ?>"><?php echo esc_html__('GO TO PREMIUM','misbah-architecture-blocks'); ?></a>
					</div>
				</div>
				<div class="misbah-architecture-blocks-notice-img">
                    <a href="<?php echo esc_url( MISBAH_ARCHITECTURE_BLOCKS_THEME_BUNDLE ); ?>" target="_blank"><img src="<?php echo esc_url(get_template_directory_uri() . '/assets/images/notification.png'); ?>" alt="<?php esc_attr_e('logo', 'misbah-architecture-blocks'); ?>"></a>
				</div>
			</div>
		</div>
        <?php }
	}
}

add_action( 'admin_notices', 'misbah_architecture_blocks_deprecated_hook_admin_notice' );

// After switching theme, reset dismissed notice option
add_action('after_switch_theme', 'misbah_architecture_blocks_after_switch_theme');
function misbah_architecture_blocks_after_switch_theme() {
    update_option('dismissed-get_started_notice', FALSE);
}

function misbah_architecture_blocks_admin_enqueue_scripts() {
	wp_enqueue_style( 'misbah-architecture-blocks-admin-style', esc_url( get_template_directory_uri() ).'/assets/css/main.css' );
    wp_localize_script( 'misbah-architecture-blocks-admin-script', 'misbah_architecture_blocks_ajax_object',
        array( 'ajax_url' => admin_url( 'admin-ajax.php' ) )
    );
}
add_action( 'admin_enqueue_scripts', 'misbah_architecture_blocks_admin_enqueue_scripts' );

add_action( 'admin_menu', 'misbah_architecture_blocks_getting_started' );
function misbah_architecture_blocks_getting_started() {
	add_theme_page( esc_html__('Get Started', 'misbah-architecture-blocks'), esc_html__('Get Started', 'misbah-architecture-blocks'), 'edit_theme_options', 'misbah-architecture-blocks-guide-page', 'misbah_architecture_blocks_test_guide');
}

if ( ! defined( 'MISBAH_ARCHITECTURE_BLOCKS_DOCS_FREE' ) ) {
define('MISBAH_ARCHITECTURE_BLOCKS_DOCS_FREE',__('https://demo.misbahwp.com/docs/misbah-architecture-blocks-free-docs/','misbah-architecture-blocks'));
}
if ( ! defined( 'MISBAH_ARCHITECTURE_BLOCKS_DOCS_PRO' ) ) {
define('MISBAH_ARCHITECTURE_BLOCKS_DOCS_PRO',__('https://demo.misbahwp.com/docs/misbah-architecture-blocks-pro-docs/','misbah-architecture-blocks'));
}
if ( ! defined( 'MISBAH_ARCHITECTURE_BLOCKS_BUY_NOW' ) ) {
define('MISBAH_ARCHITECTURE_BLOCKS_BUY_NOW',__('https://www.misbahwp.com/products/architecture-wordpress-theme','misbah-architecture-blocks'));
}
if ( ! defined( 'MISBAH_ARCHITECTURE_BLOCKS_SUPPORT_FREE' ) ) {
define('MISBAH_ARCHITECTURE_BLOCKS_SUPPORT_FREE',__('https://wordpress.org/support/theme/misbah-architecture-blocks','misbah-architecture-blocks'));
}
if ( ! defined( 'MISBAH_ARCHITECTURE_BLOCKS_REVIEW_FREE' ) ) {
define('MISBAH_ARCHITECTURE_BLOCKS_REVIEW_FREE',__('https://wordpress.org/support/theme/misbah-architecture-blocks/reviews/#new-post','misbah-architecture-blocks'));
}
if ( ! defined( 'MISBAH_ARCHITECTURE_BLOCKS_DEMO_PRO' ) ) {
define('MISBAH_ARCHITECTURE_BLOCKS_DEMO_PRO',__('https://demo.misbahwp.com/misbah-architecture-blocks/','misbah-architecture-blocks'));
}
if( ! defined( 'MISBAH_ARCHITECTURE_BLOCKS_THEME_BUNDLE' ) ) {
define('MISBAH_ARCHITECTURE_BLOCKS_THEME_BUNDLE',__('https://www.misbahwp.com/products/wordpress-bundle','misbah-architecture-blocks'));
}

function misbah_architecture_blocks_test_guide() { ?>
	<?php $misbah_architecture_blocks_theme = wp_get_theme(); ?>

	<div class="wrap" id="main-page">
		<div id="lefty">
            <div id="lefty-up">
                <div id="description">
                    <h3><?php esc_html_e('Welcome! Thank you for choosing ','misbah-architecture-blocks'); ?><?php echo esc_html( $misbah_architecture_blocks_theme ); ?>  <span><?php esc_html_e('Version: ', 'misbah-architecture-blocks'); ?><?php echo esc_html($misbah_architecture_blocks_theme['Version']);?></span></h3>
                    <div id="description-insidee">
                        <?php
                            $misbah_architecture_blocks_theme = wp_get_theme();
                            echo wp_kses_post( apply_filters( 'misbah_theme_description', esc_html( $misbah_architecture_blocks_theme->get( 'Description' ) ) ) );
                        ?>
                    </div>
                    <div id="admin_links">
                        <h3><?php esc_html_e('Unlock More Features With Premium Version','misbah-architecture-blocks'); ?></h3>
                        <div id="admin_inside_links">
                            <a href="<?php echo esc_url( MISBAH_ARCHITECTURE_BLOCKS_BUY_NOW ); ?>" target="_blank" class="blue-button-1"><?php esc_html_e( 'Get Premium', 'misbah-architecture-blocks' ) ?></a>
                            <a href="<?php echo esc_url( MISBAH_ARCHITECTURE_BLOCKS_DEMO_PRO ); ?>" id="customizer" target="_blank"><?php esc_html_e( 'Live Demo', 'misbah-architecture-blocks' ); ?> </a>
                            <a class="blue-button-1" href="<?php echo esc_url( MISBAH_ARCHITECTURE_BLOCKS_DOCS_PRO ); ?>" target="_blank" class="btn3"><?php esc_html_e( 'Pro Documentation', 'misbah-architecture-blocks' ) ?></a>
                            <a class="blue-button-2" href="<?php echo esc_url( MISBAH_ARCHITECTURE_BLOCKS_THEME_BUNDLE ); ?>" target="_blank" class="btn4"><?php esc_html_e( 'View All Themes', 'misbah-architecture-blocks' ) ?></a>
                        </div>
                    </div>
                </div>
                <div id="theme-img">
                    <img class="img_responsive" style="width: 100%;" src="<?php echo esc_url( $misbah_architecture_blocks_theme->get_screenshot() ); ?>" />
                    <div id="img-btm-box">
                        <h3 class="bundle-box-title"><?php esc_html_e('Get This Premium Theme at Flat 20% OFF','misbah-architecture-blocks'); ?></h3>
                        <div class="bundle-info">
                            <div class="bundle-left">
                                <p class="coupon-text"><?php esc_html_e('Use Coupon Code:','misbah-architecture-blocks'); ?></p>
                                <p class="coupon-code"><?php esc_html_e('HEAT20','misbah-architecture-blocks'); ?></p>
                            </div>
                            <div class="bundle-right">
                                <a class="white-button" href="<?php echo esc_url( MISBAH_ARCHITECTURE_BLOCKS_BUY_NOW ); ?>" target="_blank"><?php esc_html_e( 'Buy Now $40', 'misbah-architecture-blocks' ) ?><span><?php esc_html_e( '$60', 'misbah-architecture-blocks' ) ?></span></a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div id="lefty-down">
                <div id="admin_links">
                    <h3><?php esc_html_e('Important Links','misbah-architecture-blocks'); ?></h3>
                    <p id="description-insidee"><?php esc_html_e('Below are some Important Link, Customize your theme, Get Support, and If you are stuck somewhere get help with the documentation','misbah-architecture-blocks'); ?></p>
                    <div id="admin_inside_links">
                        <a href="<?php echo esc_url( MISBAH_ARCHITECTURE_BLOCKS_DOCS_FREE ); ?>" target="_blank" class="blue-button-1"><?php esc_html_e( 'Misbah Architecture Blocks Documentation', 'misbah-architecture-blocks' ) ?></a>
                        <a href="<?php echo esc_url( admin_url('customize.php') ); ?>" id="customizer" target="_blank"><?php esc_html_e( 'Customize Theme', 'misbah-architecture-blocks' ); ?> </a>
                        <a class="blue-button-1" href="<?php echo esc_url( MISBAH_ARCHITECTURE_BLOCKS_SUPPORT_FREE ); ?>" target="_blank" class="btn3"><?php esc_html_e( 'Get Support', 'misbah-architecture-blocks' ) ?></a>
                        <a class="blue-button-2" href="<?php echo esc_url( MISBAH_ARCHITECTURE_BLOCKS_REVIEW_FREE ); ?>" target="_blank" class="btn4"><?php esc_html_e( 'Review Theme', 'misbah-architecture-blocks' ) ?></a>
                    </div>
                </div>
            </div>
		</div>

		<div id="righty">
			<div class="postboxx donate">
				<h3 class="hndle bundle"><?php esc_html_e( 'Get All Themes', 'misbah-architecture-blocks' ); ?></h3>
				<div class="insidee theme-bundle">
					<img width="100%" src="<?php echo esc_url( get_template_directory_uri() . '/assets/images/bundle-image.png' ); ?>" alt="<?php esc_attr_e('logo', 'misbah-architecture-blocks'); ?>">
					<p class="offer"><?php esc_html_e('Get 110+ Perfect WordPress Theme In A Single Package at just $89.','misbah-architecture-blocks'); ?></p>
					<p class="coupon"><?php esc_html_e('Get Our Theme Pack of 110+ WordPress Themes At 20% Off ','misbah-architecture-blocks'); ?><span><?php esc_html_e('"HEAT20"','misbah-architecture-blocks'); ?></span></p>
				<div id="admin_pro_linkss">
					<a class="blue-button-1" href="<?php echo esc_url( MISBAH_ARCHITECTURE_BLOCKS_THEME_BUNDLE ); ?>" target="_blank"><?php esc_html_e( 'Buy All Themes - $89', 'misbah-architecture-blocks' ) ?></a>
				</div>
				<div class="d-table">
			    <ul class="d-column">
			      <li class="feature"><?php esc_html_e('Features','misbah-architecture-blocks'); ?></li>
			      <li class="free"><?php esc_html_e('Pro','misbah-architecture-blocks'); ?></li>
			      <li class="plus"><?php esc_html_e('Free','misbah-architecture-blocks'); ?></li>
			    </ul>
			    <ul class="d-row">
			      <li class="points"><?php esc_html_e('24hrs Priority Support','misbah-architecture-blocks'); ?></li>
			      <li class="right"><span class="dashicons dashicons-yes"></span></li>
			      <li class="wrong"><span class="dashicons dashicons-yes"></span></li>
			    </ul>
			    <ul class="d-row">
			      <li class="points"><?php esc_html_e('Advance Posttype','misbah-architecture-blocks'); ?></li>
			      <li class="right"><span class="dashicons dashicons-yes"></span></li>
			      <li class="wrong"><span class="dashicons dashicons-no"></span></li>
			    </ul>
			    <ul class="d-row">
			      <li class="points"><?php esc_html_e('Section Reordering','misbah-architecture-blocks'); ?></li>
			      <li class="right"><span class="dashicons dashicons-yes"></span></li>
			      <li class="wrong"><span class="dashicons dashicons-no"></span></li>
			    </ul>
			    <ul class="d-row">
			      <li class="points"><?php esc_html_e('Enable / Disable Option','misbah-architecture-blocks'); ?></li>
			      <li class="right"><span class="dashicons dashicons-yes"></span></li>
			      <li class="wrong"><span class="dashicons dashicons-yes"></span></li>
			    </ul>
			    <ul class="d-row">
			      <li class="points"><?php esc_html_e('Multiple Sections','misbah-architecture-blocks'); ?></li>
			      <li class="right"><span class="dashicons dashicons-yes"></span></li>
			      <li class="wrong"><span class="dashicons dashicons-no"></span></li>
			    </ul>
			    <ul class="d-row">
			      <li class="points"><?php esc_html_e('Advance Color Pallete','misbah-architecture-blocks'); ?></li>
			      <li class="right"><span class="dashicons dashicons-yes"></span></li>
			      <li class="wrong"><span class="dashicons dashicons-no"></span></li>
			    </ul>
			    <ul class="d-row">
			      <li class="points"><?php esc_html_e('Advance Widgets','misbah-architecture-blocks'); ?></li>
			      <li class="right"><span class="dashicons dashicons-yes"></span></li>
			      <li class="wrong"><span class="dashicons dashicons-yes"></span></li>
			    </ul>
			    <ul class="d-row">
			      <li class="points"><?php esc_html_e('Page Templates','misbah-architecture-blocks'); ?></li>
			      <li class="right"><span class="dashicons dashicons-yes"></span></li>
			      <li class="wrong"><span class="dashicons dashicons-no"></span></li>
			    </ul>
			    <ul class="d-row">
			      <li class="points"><?php esc_html_e('Advance Typography','misbah-architecture-blocks'); ?></li>
			      <li class="right"><span class="dashicons dashicons-yes"></span></li>
			      <li class="wrong"><span class="dashicons dashicons-no"></span></li>
			    </ul>
			    <ul class="d-row">
			      <li class="points"><?php esc_html_e('Section Background Image / Color ','misbah-architecture-blocks'); ?></li>
			      <li class="right"><span class="dashicons dashicons-yes"></span></li>
			      <li class="wrong"><span class="dashicons dashicons-no"></span></li>
			    </ul>
	  		</div>
			</div>
		</div>
	</div>
<?php } ?>