<?php

/**
 * Plugin Name:       A1 Marketplace Sync with eBay for WooCommerce
 * Plugin URI:        https://a1plugins.com/a1-ebay-integration-for-woocommerce/
 * Description:       Connects WooCommerce to eBay via eBay REST APIs.
 * Version:           1.2.9
 * Requires at least: 6.0
 * Requires PHP:      8.2
 * Requires Plugins:  woocommerce
 * Author:            Richard Rives
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       a1-marketplace-sync-with-ebay-for-woocommerce
 *
 * @package A1_WooBay
 */
if ( !function_exists( 'aw_fs' ) ) {
    // Create a helper function for easy SDK access.
    function aw_fs() {
        global $aw_fs;
        if ( !isset( $aw_fs ) ) {
            // Include Freemius SDK.
            require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
            $aw_fs = fs_dynamic_init( array(
                'id'               => '35001',
                'slug'             => 'a1-woobay',
                'premium_slug'     => 'a1-marketplace-sync-with-ebay-for-woocommerce-premium',
                'type'             => 'plugin',
                'public_key'       => 'pk_9b2ab651f80f028f11b3d60f12156',
                'is_premium'       => false,
                'premium_suffix'   => 'Pro',
                'has_addons'       => false,
                'has_paid_plans'   => true,
                'is_org_compliant' => true,
                'trial'            => array(
                    'days'               => 14,
                    'is_require_payment' => false,
                ),
                'menu'             => array(
                    'slug'    => 'a1woobay-settings',
                    'account' => true,
                    'support' => false,
                ),
                'is_live'          => true,
            ) );
        }
        return $aw_fs;
    }

    // Init Freemius.
    aw_fs();
    // Signal that SDK was initiated.
    do_action( 'aw_fs_loaded' );
}
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
define( 'A1WOOBAY_VERSION', '1.2.9' );
define( 'A1WOOBAY_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'A1WOOBAY_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'A1WOOBAY_PLUGIN_FILE', __FILE__ );
add_action( 'admin_notices', function () {
    if ( !class_exists( 'WooCommerce' ) ) {
        echo '<div class="notice notice-error"><p>' . esc_html__( 'A1 Marketplace Sync with eBay for WooCommerce requires WooCommerce to be installed and active.', 'a1-marketplace-sync-with-ebay-for-woocommerce' ) . '</p></div>';
    }
} );
add_action( 'plugins_loaded', function () {
    if ( !class_exists( 'WooCommerce' ) ) {
        return;
    }
    A1WooBay_Bridge::get_instance();
} );
class A1WooBay_Bridge {
    private static ?A1WooBay_Bridge $instance = null;

    public static function get_instance() : A1WooBay_Bridge {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function __construct() {
        $this->load_dependencies();
        $this->init_components();
    }

    private function load_dependencies() : void {
        require_once A1WOOBAY_PLUGIN_DIR . 'includes/class-a1woobay-license.php';
        require_once A1WOOBAY_PLUGIN_DIR . 'includes/class-a1woobay-auth.php';
        require_once A1WOOBAY_PLUGIN_DIR . 'includes/class-a1woobay-api.php';
        require_once A1WOOBAY_PLUGIN_DIR . 'includes/class-a1woobay-profiles.php';
        require_once A1WOOBAY_PLUGIN_DIR . 'includes/class-a1woobay-product-meta.php';
        require_once A1WOOBAY_PLUGIN_DIR . 'admin/class-a1woobay-admin.php';
        require_once A1WOOBAY_PLUGIN_DIR . 'admin/class-a1woobay-ajax.php';
        require_once A1WOOBAY_PLUGIN_DIR . 'includes/class-a1woobay-sync.php';
        require_once A1WOOBAY_PLUGIN_DIR . 'includes/class-a1woobay-order-meta.php';
        require_once A1WOOBAY_PLUGIN_DIR . 'includes/class-a1woobay-compliance.php';
    }

    private function init_components() : void {
        A1WooBay_Auth::get_instance();
        A1WooBay_API::get_instance();
        A1WooBay_Profiles::get_instance();
        A1WooBay_Sync::get_instance();
        A1WooBay_Compliance::get_instance();
        if ( is_admin() ) {
            A1WooBay_Admin::get_instance();
            A1WooBay_Product_Meta::get_instance();
            new A1WooBay_Ajax();
        }
    }

}

// Inject "Free Trial" button on Freemius Account page
add_action( 'admin_enqueue_scripts', function () {
    // phpcs:ignore WordPress.Security.NonceVerification
    if ( !isset( $_GET['page'] ) || 'a1woobay-settings-account' !== $_GET['page'] ) {
        return;
    }
    wp_enqueue_script(
        'a1woobay-trial-button',
        A1WOOBAY_PLUGIN_URL . 'assets/js/trial-button.js',
        array('jquery'),
        A1WOOBAY_VERSION,
        true
    );
    wp_localize_script( 'a1woobay-trial-button', 'a1woobayTrial', array(
        'trialUrl' => admin_url( 'admin.php?page=a1woobay-settings-pricing' ),
    ) );
} );