<?php
/**
 * Plugin Name: Shop Explorer
 * Plugin URI:  https://wordpress.org/plugins/shop-explorer
 * Description: Unified WooCommerce management system for comprehensive e-commerce management including orders, products, and multivendor support.
 * Version: 1.2.0
 * Requires at least: 6.7
 * Requires PHP:      7.4
 * Author: wpazleen
 * Author URI: https://profiles.wordpress.org/wpazleen/
 * Text Domain: shop-explorer
 * Domain Path: /languages/
 * License: GPL-2.0+
 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
 *
 * @package ShopExplorer
 * @since 1.0.0
 */

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



// Plugin constants
define( 'SHOPEX_VERSION', '1.2.0' );
define( 'SHOPEX_FILE', __FILE__ );
define( 'SHOPEX_PATH', plugin_dir_path( __FILE__ ) );
define( 'SHOPEX_URL', plugin_dir_url( __FILE__ ) );
define( 'SHOPEX_BASENAME', plugin_basename( __FILE__ ) );
define( 'SHOPEX_MIN_PHP_VERSION', '7.4' );
define( 'SHOPEX_MIN_WP_VERSION', '6.0' );
define( 'SHOPEX_MIN_WC_VERSION', '8.0.0' );


if ( ! function_exists( 'shopex_fs' ) ) { // Freemium SDK function also prefixed.
	/**
	 * Freemius SDK with smart initialization - "Pro Takes Ownership, Free Steps Aside" pattern.
	 *
	 * @since 1.1.0
	 * @return object Freemius SDK object.
	 */
	function shopex_fs() {
		global $shopex_fs;

		if ( ! isset( $shopex_fs ) ) {
			// Check if Pro version is active - if so, let Pro handle Freemius
			if ( is_plugin_active( 'shop-explorer-pro/shop-explorer-pro.php' ) ) {
				// Pro is active - Free steps aside, only load analytics
				return null; // Pro will handle all Freemius functionality
			}

			// Include Freemius SDK.
			require_once __DIR__ . '/vendor/freemius/start.php';

			$shopex_fs = fs_dynamic_init( array(
				'id'                  => '22815',
				'slug'                => 'shop-explorer',
				'premium_slug'        => 'shop-explorer-pro',
				'type'                => 'plugin',
				'public_key'          => 'pk_6ccb74a833c41d26a0bb7d589a248',
				'is_premium'          => false,
				'has_premium_version' => true,
				'has_paid_plans'      => true,
				'is_org_compliant'    => true,
				'menu'                => array(
					'slug'           => 'shop-explorer',
					'first-path'     => 'admin.php?page=shop-explorer',
					'network'        => true,
					'pricing'        => true,
					'contact'        => true,
					'support'        => true,
				),

				'parallel_activation' => array(
                    'enabled'                  => true,
                    'premium_version_basename' => 'shop-explorer-pro/shop-explorer-pro.php',
                ),

				// 'is_live'             => true,
			) );
		}

		return $shopex_fs;
	}
	
	// Only initialize if Pro is not active
	if ( ! is_plugin_active( 'shop-explorer-pro/shop-explorer-pro.php' ) ) {
		shopex_fs();
		do_action( 'shopex_fs_loaded' );
	}
}

/**
 * Main Plugin Bootstrap Class
 *
 * @since 1.0.0
 */
final class SHOPEX_Plugin_Bootstrap {

	/**
	 * Constructor
	 */
	public function __construct() {
		// Check requirements
		if ( ! $this->check_requirements() ) {
			return;
		}

		// Load Composer autoloader
		$this->load_composer();

		// Initialize hooks
		$this->init_hooks();
	}

	/**
	 * Check plugin requirements
	 *
	 * @return bool
	 */
	private function check_requirements() {
		// Check PHP version
		if ( version_compare( PHP_VERSION, SHOPEX_MIN_PHP_VERSION, '<' ) ) {
			add_action( 'admin_notices', [ $this, 'php_version_notice' ] );
			return false;
		}

		// Check WordPress version
		if ( version_compare( get_bloginfo( 'version' ), SHOPEX_MIN_WP_VERSION, '<' ) ) {
			add_action( 'admin_notices', [ $this, 'wp_version_notice' ] );
			return false;
		}

		return true;
	}

	/**
	 * Load Composer autoloader
	 */
	private function load_composer() {
		if ( file_exists( SHOPEX_PATH . 'vendor/autoload.php' ) ) {
			require_once SHOPEX_PATH . 'vendor/autoload.php';
		} else {
			add_action( 'admin_notices', [ $this, 'composer_notice' ] );
		}
	}

	/**
	 * Initialize WordPress hooks
	 */
	private function init_hooks() {
		add_action( 'admin_notices', [ $this, 'admin_notices' ] );
		add_action( 'plugins_loaded', [ $this, 'plugins_loaded' ] );
		register_activation_hook( SHOPEX_FILE, [ $this, 'activate' ] );
		register_deactivation_hook( SHOPEX_FILE, [ $this, 'deactivate' ] );
	}

	/**
	 * PHP version notice
	 */
	public function php_version_notice() {
		?>
		<div class="notice notice-error">
			<p>
				<?php
				printf(
					/* translators: %s: Minimum required PHP version */
					esc_html__( 'Shop Explorer requires PHP version %s or higher.', 'shop-explorer' ),
					esc_html( SHOPEX_MIN_PHP_VERSION )
				);
				?>
			</p>
		</div>
		<?php
	}

	/**
	 * WordPress version notice
	 */
	public function wp_version_notice() {
		?>
		<div class="notice notice-error">
			<p>
				<?php
				printf(
					/* translators: %s: Minimum required WordPress version */
					esc_html__( 'Shop Explorer requires WordPress version %s or higher.', 'shop-explorer' ),
					esc_html( SHOPEX_MIN_WP_VERSION )
				);
				?>
			</p>
		</div>
		<?php
	}

	/**
	 * Composer dependencies notice
	 */
	public function composer_notice() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}
		?>
		<div class="notice notice-warning">
			<p>
				<?php
				printf(
					/* translators: %s: composer install command */
					esc_html__( 'Shop Explorer: Composer dependencies are not installed. Please run %s in the plugin directory to install required libraries.', 'shop-explorer' ),
					'<code>composer install</code>'
				);
				?>
			</p>
		</div>
		<?php
	}

	/**
	 * Admin notices
	 */
	public function admin_notices() {
		// Show notice if activation failed due to missing WooCommerce
		if ( get_transient( 'shopex_activation_error' ) ) {
			$error = get_transient( 'shopex_activation_error' );
			delete_transient( 'shopex_activation_error' );

			if ( $error === 'woocommerce_missing' ) {
				deactivate_plugins( SHOPEX_BASENAME );
				?>
				<div class="notice notice-error is-dismissible">
					<p>
						<strong><?php esc_html_e( 'Shop Explorer', 'shop-explorer' ); ?></strong>
						<?php esc_html_e( 'could not be activated because WooCommerce is not installed or activated. Please install and activate WooCommerce first.', 'shop-explorer' ); ?>
					</p>
				</div>
				<?php
			}
		}

		// Show notice if plugin was deactivated due to WooCommerce
		if ( get_transient( 'shopex_wc_deactivated_notice' ) ) {
			delete_transient( 'shopex_wc_deactivated_notice' );
			?>
			<div class="notice notice-warning is-dismissible">
				<p>
					<strong><?php esc_html_e( 'Shop Explorer', 'shop-explorer' ); ?></strong>
					<?php esc_html_e( 'has been deactivated because WooCommerce is not active. Please install and activate WooCommerce to use Shop Explorer.', 'shop-explorer' ); ?>
				</p>
			</div>
			<?php
		}
	}

	/**
	 * Plugins loaded callback
	 */
	public function plugins_loaded() {
		// Check if WooCommerce is active
		if ( ! class_exists( 'WooCommerce' ) ) {
			deactivate_plugins( SHOPEX_BASENAME );
			set_transient( 'shopex_wc_deactivated_notice', true, 60 );
			return;
		}

		// Initialize the plugin
		$this->init_plugin();
	}

	/**
	 * Initialize the main plugin
	 */
	private function init_plugin() {
		if ( ! class_exists( 'SHOPEXPLORER\\Core\\SHOPEX_ShopExplorer' ) ) {
			require_once SHOPEX_PATH . 'inc/Core/ShopExplorer.php';
		}

		SHOPEXPLORER\Core\SHOPEX_ShopExplorer::get_instance();
	}

	/**
	 * Plugin activation
	 */
	public function activate() {
		// Check if WooCommerce is active during activation
		if ( ! class_exists( 'WooCommerce' ) ) {
			set_transient( 'shopex_activation_error', 'woocommerce_missing', 60 );
			return;
		}

		// Load Activate class
		if ( ! class_exists( 'SHOPEXPLORER\\Inc\\SHOPEX_Activate' ) ) {
			require_once SHOPEX_PATH . 'inc/Activate.php';
		}

		if ( class_exists( 'SHOPEXPLORER\\Inc\\SHOPEX_Activate' ) ) {
			SHOPEXPLORER\Inc\SHOPEX_Activate::activate();
		}
	}

	/**
	 * Plugin deactivation
	 */
	public function deactivate() {
		// Load Deactivate class
		if ( ! class_exists( 'SHOPEXPLORER\\Inc\\SHOPEX_Deactivate' ) ) {
			require_once SHOPEX_PATH . 'inc/Deactivate.php';
		}

		if ( class_exists( 'SHOPEXPLORER\\Inc\\SHOPEX_Deactivate' ) ) {
			SHOPEXPLORER\Inc\SHOPEX_Deactivate::deactivate();
		}
	}
}

// Initialize the plugin
new SHOPEX_Plugin_Bootstrap();



/**
 * Helper function to check if Free should handle analytics when Pro is active.
 * Even when Pro takes ownership, Free can still collect analytics data.
 *
 * @since 1.1.0
 * @return bool Whether Free should handle analytics.
 */
function shopex_should_handle_analytics() {
	// Free always handles its own analytics, even when Pro is active
	return true;
}

/**
 * Helper function to check if Freemius is available (either from Free or Pro).
 *
 * @since 1.1.0
 * @return object|null Freemius SDK object or null.
 */
function shopex_get_freemius_instance() {
	// If Pro is active, get Freemius instance from Pro
	if ( is_plugin_active( 'shop-explorer-pro/shop-explorer-pro.php' ) ) {
		// Pro handles Freemius, so get it from Pro's function
		if ( function_exists( 'shopex_pro_get_freemius_instance' ) ) {
			return shopex_pro_get_freemius_instance();
		}
		// Fallback: try to get Pro's Freemius instance directly
		return function_exists( 'shopex_fs' ) ? shopex_fs() : null;
	}
	
	// Otherwise, Free handles it
	return function_exists( 'shopex_fs' ) ? shopex_fs() : null;
}
