<?php

/**
 * Plugin Name:       SkyServers Product Sync for eMAG
 * Plugin URI:        https://www.sky-servers.com/skyservers-product-sync-for-emag
 * Description:       Sync prices, stock and publish products from WooCommerce to eMAG Marketplace. Automatic margin control, category mapping, Buy Button tracking, and more.
 * Version:           3.9.3
 * Author:            Sky Servers SRL
 * Author URI:        https://www.sky-servers.com
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       skyservers-product-sync-for-emag
 * Domain Path:       /languages
 * Requires at least: 5.8
 * Requires PHP:      7.4
 * WC requires at least: 7.0
 * WC tested up to:   9.9
 *
 * @package SkyEmagSync
 *
 * Freemius build-time strip directive: when the Freemius processor
 * derives the Free version of this plugin (the one distributed via
 * WordPress.org), the paths listed below are physically removed from
 * the resulting Free ZIP. Premium-only namespaces, admin pages, and
 * helpers stay out of the Free build entirely so the WP.org Plugin
 * Directory guidelines (Guideline 5 â€” no trialware) are satisfied.
 *
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
// â”€â”€ Freemius SDK: free / pro switch â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€
// When the pro version is active, epsbss_fs() is already defined.
// This prevents loading two copies of the plugin simultaneously.
if ( function_exists( 'epsbss_fs' ) ) {
    epsbss_fs()->set_basename( false, __FILE__ );
} else {
    /**
     * DO NOT REMOVE THIS IF, IT IS ESSENTIAL FOR THE
     * `function_exists` CALL ABOVE TO PROPERLY WORK.
     */
    if ( !function_exists( 'epsbss_fs' ) ) {
        // Only load Freemius if the SDK is present (pro build).
        if ( file_exists( dirname( __FILE__ ) . '/vendor/freemius/start.php' ) ) {
            /**
             * Create a helper function for easy SDK access.
             *
             * The name "epsbss_fs" is the prefix the Freemius SDK auto-
             * generates for this specific plugin slug and is required to
             * remain unchanged for the SDK's internal cross-references to
             * resolve. Plugin Check's prefix rule does not apply to vendor
             * SDK glue code.
             *
             * @return \Freemius
             */
            // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Freemius SDK glue, see docblock above.
            function epsbss_fs() {
                global $epsbss_fs;
                if ( !isset( $epsbss_fs ) ) {
                    // Include Freemius SDK.
                    require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
                    $epsbss_fs = fs_dynamic_init( array(
                        'id'               => '25648',
                        'slug'             => 'skyservers-product-sync-for-emag',
                        'type'             => 'plugin',
                        'public_key'       => 'pk_278a08a49656cfe686cfa35428fca',
                        'is_premium'       => false,
                        'premium_suffix'   => 'Premium',
                        'has_addons'       => false,
                        'has_paid_plans'   => true,
                        'is_org_compliant' => true,
                        'menu'             => array(
                            'slug'    => 'skyservers-product-sync-for-emag',
                            'support' => false,
                        ),
                        'is_live'          => true,
                    ) );
                }
                return $epsbss_fs;
            }

            // Init Freemius.
            epsbss_fs();
            // Signal that SDK was initiated. The hook name "epsbss_fs_loaded"
            // is the SDK-generated identifier for this plugin slug and
            // is consumed by Freemius internals; renaming it would break
            // SDK event wiring.
            // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Freemius SDK hook, see comment above.
            do_action( 'epsbss_fs_loaded' );
            // Hook uninstall cleanup to Freemius (replaces uninstall.php for pro build).
            require_once __DIR__ . '/includes/uninstall-cleanup.php';
            epsbss_fs()->add_action( 'after_uninstall', 'skyemag_uninstall_cleanup' );
        }
        // end if SDK exists
    }
    // â”€â”€ Constants â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€
    define( 'SKYEMAG_VERSION', '3.9.3' );
    define( 'SKYEMAG_PLUGIN_FILE', __FILE__ );
    define( 'SKYEMAG_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
    define( 'SKYEMAG_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
    define( 'SKYEMAG_MIN_PHP', '7.4' );
    define( 'SKYEMAG_MIN_WC', '7.0' );
    // â”€â”€ Setup wizard auto-redirect â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€
    // Fires on Free + Pro activation and on plugin upgrades. Sets a one-shot
    // transient consumed by Plugin::maybe_setup_wizard_redirect() on the
    // next admin_init. The 30-second TTL means a meandering post-activation
    // path won't hijack a redirect later.
    register_activation_hook( __FILE__, function () {
        // 5-minute TTL so the redirect survives a Freemius opt-in detour.
        // Was 30 s historically, which expired before the seller finished
        // opt-in on first activation and they never landed on the wizard.
        set_transient( 'skyemag_setup_redirect', '1', 5 * MINUTE_IN_SECONDS );
    } );
    // â”€â”€ WooCommerce HPOS + Cart/Checkout Blocks compatibility â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€
    add_action( 'before_woocommerce_init', function () {
        if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
            \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
            \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__, true );
        }
    } );
    // â”€â”€ Bootstrap â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€
    add_action( 'plugins_loaded', function () {
        if ( version_compare( PHP_VERSION, SKYEMAG_MIN_PHP, '<' ) ) {
            add_action( 'admin_notices', function () {
                printf( '<div class="notice notice-error"><p>%s</p></div>', esc_html( sprintf( 
                    /* translators: %1$s: required PHP version, %2$s: current PHP version */
                    __( 'SkyServers Product Sync for eMAG requires PHP %1$s or higher. You are running PHP %2$s.', 'skyservers-product-sync-for-emag' ),
                    SKYEMAG_MIN_PHP,
                    PHP_VERSION
                 ) ) );
            } );
            return;
        }
        if ( !class_exists( 'WooCommerce' ) ) {
            add_action( 'admin_notices', function () {
                echo '<div class="notice notice-error"><p>';
                esc_html_e( 'SkyServers Product Sync for eMAG requires WooCommerce to be active.', 'skyservers-product-sync-for-emag' );
                echo '</p></div>';
            } );
            return;
        }
        if ( version_compare( WC()->version, SKYEMAG_MIN_WC, '<' ) ) {
            add_action( 'admin_notices', function () {
                printf( '<div class="notice notice-error"><p>%s</p></div>', esc_html( sprintf( 
                    /* translators: %1$s: required WC version, %2$s: current WC version */
                    __( 'SkyServers Product Sync for eMAG requires WooCommerce %1$s or higher. You are running WooCommerce %2$s.', 'skyservers-product-sync-for-emag' ),
                    SKYEMAG_MIN_WC,
                    WC()->version
                 ) ) );
            } );
            return;
        }
        require_once SKYEMAG_PLUGIN_DIR . 'includes/Autoloader.php';
        SkyEmagSync\Autoloader::register();
        SkyEmagSync\Plugin::instance()->init();
    } );
    // â”€â”€ Activation / Deactivation â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€
    register_activation_hook( __FILE__, function () {
        require_once SKYEMAG_PLUGIN_DIR . 'includes/Autoloader.php';
        SkyEmagSync\Autoloader::register();
        SkyEmagSync\Installer::activate();
    } );
    register_deactivation_hook( __FILE__, function () {
        require_once SKYEMAG_PLUGIN_DIR . 'includes/Autoloader.php';
        SkyEmagSync\Autoloader::register();
        SkyEmagSync\Installer::deactivate();
    } );
}