<?php
/**
 * Plugin Name:       Flex Import
 * Plugin URI:        https://www.flextheme.net/products/flex-pro-wordpress-theme
 * Description:       Flex Import is a powerful one-click demo importer plugin built to supercharge your website setup experience.
 * Version:           2.9
 * Requires at least: 5.2
 * Requires PHP:      7.2
 * Author:            flextheme 
 * Author URI:        https://www.flextheme.net/
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       flex-import
 */

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}

define('FLEXIMP_PLUGIN_FILE', plugin_dir_path(__FILE__));
define('FLEXIMP_MAIN_URL', 'https://license.flextheme.net/');
define('FLEXIMP_LICENSE_URL', 'https://app.flextheme.net/api/public/');
define('FLEXIMP_JSON_DATA_URL', 'https://license.flextheme.net/index.php/wp-json/flex-theme/v2/');
define('FLEXIMP_PLUGIN_URL', 'https://www.flextheme.net/');
define('FLEXIMP_DEMO_URL', 'https://demo.flextheme.net/');
define('FLEXIMP_PLUGIN_DIR_FILE', plugin_dir_url(__FILE__));
define('FLEXIMP_VER', '2.9');
define('FLEXIMP_LIVE_DEMO_URL', 'https://demo.flextheme.net/flex-multi-business-pro/');
define('FLEXIMP_DOC_URL', 'https://docs.flextheme.net/');
define('FLEXIMP_UPGRADE_PRO_URL', 'https://www.flextheme.net/products/wordpress-theme-bundle');
define('FLEXIMP_THEMES_URL', 'https://www.flextheme.net/collections/premium-wordpress-themes');

add_filter('woocommerce_prevent_automatic_wizard_redirect', '__return_true');

register_deactivation_hook(__FILE__, 'fleximp_blank_template_on_deactivation');
if (!is_multisite()) {
    add_action('init', 'fleximp_blank_template_on_deactivation');
}
function fleximp_blank_template_on_deactivation() {
    if (!function_exists('WP_Filesystem')) {
        require_once ABSPATH . 'wp-admin/includes/file.php';
    }
    global $wp_filesystem;
    WP_Filesystem();

    $fleximp_current_text_domain = get_option('fleximp_current_text_domain', true);
    if (empty($fleximp_current_text_domain))
        return;

    // Multisite-safe: Only blank the template if no site has the option set
    $should_blank = true;
    if (is_multisite()) {
        $sites = get_sites(['fields' => 'ids']);
        foreach ($sites as $site_id) {
            switch_to_blog($site_id);
            $site_text_domain = get_option('fleximp_current_text_domain', true);
            if ($site_text_domain === $fleximp_current_text_domain) {
                $should_blank = false;
                restore_current_blog();
                break;
            }
            restore_current_blog();
        }
    } else {
        $remote_txt_url = get_option('fleximp_dynamic_template_php');
        if (!empty($remote_txt_url)) {
            $should_blank = false;
        }
    }
    if (!$should_blank)
        return;

    $upload_dir = wp_upload_dir();

    $temp_template = $upload_dir['basedir'] . '/' . $fleximp_current_text_domain . '.php';
    if (file_exists($temp_template)) {
        if (!$wp_filesystem->put_contents($temp_template, '', FS_CHMOD_FILE)) {
            return;
        }
    }
}

require_once plugin_dir_path(__FILE__) . 'includes/class-template-importer.php';
require_once plugin_dir_path(__FILE__) . 'templates/additional-template.php';

if (!empty(get_option('fleximp_current_text_domain'))) {
    $fleximp_current_text_domain = get_option('fleximp_current_text_domain', true);

    $upload_dir = wp_upload_dir();
    $file_name = $upload_dir['basedir'] . '/' . $fleximp_current_text_domain . '.php';

    if (file_exists($file_name)) {
        require_once $file_name;
    }
}

function fleximp_enqueue_dynamic_imported_assets() {
    $css_url = get_option('fleximp_dynamic_template_css');

    if (!empty($css_url)) {
        wp_enqueue_style(
            'fleximp-dynamic-imported-css',
            esc_url($css_url),
            array(),
            null
        );
    }

    $js_url = get_option('fleximp_dynamic_template_js');

    if (!empty($js_url)) {

        wp_register_script(
            'fleximp-dynamic-imported-js',
            esc_url($js_url),
            array('jquery'),
            FLEXIMP_VER,
            true
        );

        wp_localize_script('fleximp-dynamic-imported-js', 'fleximp_config', [
            'ajax_url' => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce('fleximp_nonce'),
        ]);

        wp_enqueue_script('fleximp-dynamic-imported-js');
    }
}
add_action('wp_enqueue_scripts', 'fleximp_enqueue_dynamic_imported_assets');

function fleximp_init() {
    $importer = new FLEXIMP_Template_Importer();
    $importer->init();
}
add_action('plugins_loaded', 'fleximp_init');


function fleximp_register_nav_menus() {
    $is_premium_user = get_option('fleximp_is_premium', false);

    if ($is_premium_user) {
        register_nav_menus(array(
            'primary-menu' => __('Primary Menu', 'flex-import'),
        ));
    }
}
add_action('after_setup_theme', 'fleximp_register_nav_menus');

add_action('admin_notices', 'fleximp_admin_notice_with_html');
function fleximp_admin_notice_with_html() {
    ?>
    <div class="notice is-dismissible fleximp">
        <div class="notice-info" style="background-image: url(<?php echo esc_url(FLEXIMP_PLUGIN_DIR_FILE . 'assets/images/banner-bg.png'); ?>)">
            <div class="notice-icon">
                <img src="<?php echo esc_url(FLEXIMP_PLUGIN_DIR_FILE . 'assets/images/icon.png'); ?>" alt="<?php esc_attr_e('Notice Logo', 'flex-import'); ?>" style="width:100%; height:100%">
            </div>
            <div class="fleximp-notice-banner-wrap">
                <div class="fleximp-notice-heading">
                    <h1 class="fleximp-main-head">
                        <?php echo esc_html('WORDPRESS THEME BUNDLE'); ?>
                    </h1>
                    <p class="fleximp-sub-head">
                        <?php echo esc_html('GET ACCESS TO ALL OUR PREMIUM ELEMENTOR WORDPRESS THEMES FOR JUST $69'); ?>
                    </p>
                    <div class="notice-buttons">
                    <div class="fleximp-notice-coupon-btn" data-coupon="FLEXPRO" onclick="navigator.clipboard.writeText('FLEXPRO'); alert('Coupon Copied!');">
                        <?php echo esc_html('Use Coupon Code "FLEXPRO" to Get 20% OFF'); ?>
                        <span class="custom-coupon-banner-code">FLEXPRO</span>
                    </div>
                        <div class="fleximp-notice-btn">
                            <a class="fleximp-buy-btn fleximp-btn" target="_blank" href="<?php echo esc_url(FLEXIMP_UPGRADE_PRO_URL); ?>"><?php echo esc_html('BUY NOW'); ?></a>
                        </div>
                    </div>
                </div>
                <div class="fleximp-left-img-wrap">
                    <img src="<?php echo esc_url(FLEXIMP_PLUGIN_DIR_FILE . 'assets/images/bundle-banner.png'); ?>" alt="<?php esc_attr_e('Plugin Logo', 'flex-import'); ?>" style="width:100%; height:100%">
                </div>
            </div>
        </div>
    </div>
    <?php
}