<?php

/**
 * Plugin Name:       GoFactoX E-Invoicing for WooCommerce
 * Plugin URI:        https://gofactox.com
 * Description:       Generate Factur-X, CII and UBL compliant e-invoices (PDF/A-3 + XML) from WooCommerce orders. French legal compliance built-in.
 * Version:           1.4.2
 * Requires at least: 6.4
 * Requires PHP:      8.0
 * Requires Plugins:  woocommerce
 * Author:            GoFactoX
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       gofactox-e-invoicing-for-woocommerce
 * Domain Path:       /languages
 * WC requires at least: 8.5
 * WC tested up to:      9.6
 *
 * @package FacturX_For_WooCommerce
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
if ( function_exists( 'gofactox_fs' ) ) {
    gofactox_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( 'gofactox_fs' ) ) {
        // Create a helper function for easy SDK access.
        function gofactox_fs() {
            global $gofactox_fs;
            if ( !isset( $gofactox_fs ) ) {
                // Include Freemius SDK.
                require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
                $gofactox_fs = fs_dynamic_init( array(
                    'id'               => '28234',
                    'slug'             => 'gofactox-e-invoicing-for-woocommerce',
                    'type'             => 'plugin',
                    'public_key'       => 'pk_58a0d7e398e55f7daa829f860e62e',
                    'is_premium'       => false,
                    'has_addons'       => false,
                    'has_paid_plans'   => true,
                    'is_org_compliant' => true,
                    'menu'             => array(
                        'slug'       => 'wfx-dashboard',
                        'first-path' => 'admin.php?page=wc-settings&tab=facturx',
                    ),
                    'is_live'          => true,
                ) );
            }
            return $gofactox_fs;
        }

        // Init Freemius.
        gofactox_fs();
        // Signal that SDK was initiated.
        do_action( 'gofactox_fs_loaded' );
    }
    define( 'GOFACTOX_VERSION', '1.4.2' );
    define( 'GOFACTOX_PLUGIN_FILE', __FILE__ );
    define( 'GOFACTOX_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
    define( 'GOFACTOX_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
    define( 'GOFACTOX_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
    /**
     * Declare HPOS 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 );
        }
    } );
    /**
     * Check requirements before loading the plugin.
     */
    function gofactox_check_requirements() : bool {
        if ( !class_exists( 'WooCommerce' ) ) {
            add_action( 'admin_notices', function () {
                echo '<div class="notice notice-error"><p>';
                esc_html_e( 'Factur-X for WooCommerce requires WooCommerce to be installed and active.', 'gofactox-e-invoicing-for-woocommerce' );
                echo '</p></div>';
            } );
            return false;
        }
        if ( version_compare( PHP_VERSION, '8.0', '<' ) ) {
            add_action( 'admin_notices', function () {
                echo '<div class="notice notice-error"><p>';
                esc_html_e( 'Factur-X for WooCommerce requires PHP 8.0 or higher.', 'gofactox-e-invoicing-for-woocommerce' );
                echo '</p></div>';
            } );
            return false;
        }
        return true;
    }

    /**
     * Load composer autoloader.
     */
    function gofactox_load_autoloader() : bool {
        $autoloader = GOFACTOX_PLUGIN_DIR . 'vendor/autoload.php';
        if ( file_exists( $autoloader ) ) {
            require_once $autoloader;
            return true;
        }
        add_action( 'admin_notices', function () {
            echo '<div class="notice notice-error"><p>';
            esc_html_e( 'Factur-X for WooCommerce: Composer dependencies are missing. Please run "composer install" in the plugin directory.', 'gofactox-e-invoicing-for-woocommerce' );
            echo '</p></div>';
        } );
        return false;
    }

    /**
     * Bootstrap the plugin.
     */
    function gofactox_init() : void {
        if ( !gofactox_check_requirements() ) {
            return;
        }
        if ( !gofactox_load_autoloader() ) {
            return;
        }
        // Load includes (classmap autoload may not include these if composer was run elsewhere).
        $includes = [
            'class-gofactox-activator',
            'class-gofactox-license',
            'class-gofactox-settings',
            'class-gofactox-payment-terms',
            'class-gofactox-invoice',
            'class-gofactox-numbering',
            'class-gofactox-xml-generator',
            'class-gofactox-pdf-generator',
            'class-gofactox-facturx-merger',
            'class-gofactox-checkout-fields',
            'class-gofactox-order-hooks',
            'class-gofactox-download',
            'class-gofactox-my-account',
            'class-gofactox-pdp-connector',
            'class-gofactox-pdp-manager',
            'class-gofactox-dashboard',
            'class-gofactox-bulk',
            'class-gofactox-fec-export',
            'class-gofactox-rest-api',
            'class-gofactox-sepa-qr',
            'class-gofactox-thank-you',
            'class-gofactox-gdpr'
        ];
        foreach ( $includes as $file ) {
            $path = GOFACTOX_PLUGIN_DIR . 'includes/' . $file . '.php';
            if ( file_exists( $path ) ) {
                require_once $path;
            }
        }
        // Core classes.
        GOFACTOX_Settings::init();
        GOFACTOX_Invoice::init();
        GOFACTOX_Numbering::init();
        GOFACTOX_Checkout_Fields::init();
        GOFACTOX_Order_Hooks::init();
        GOFACTOX_My_Account::init();
        GOFACTOX_Download::init();
        GOFACTOX_PDP_Manager::init();
        if ( class_exists( 'GOFACTOX_Rest_Api' ) ) {
            GOFACTOX_Rest_Api::init();
        }
        if ( class_exists( 'GOFACTOX_FEC_Export' ) ) {
            GOFACTOX_FEC_Export::init();
        }
        if ( class_exists( 'GOFACTOX_Thank_You' ) ) {
            GOFACTOX_Thank_You::init();
        }
        if ( class_exists( 'GOFACTOX_GDPR' ) ) {
            GOFACTOX_GDPR::init();
        }
        // Load PDP connectors (Business-tier feature).
        if ( class_exists( 'GOFACTOX_License' ) && GOFACTOX_License::is_business() ) {
            $connectors_dir = GOFACTOX_PLUGIN_DIR . 'includes/connectors/';
            if ( is_dir( $connectors_dir ) ) {
                foreach ( glob( $connectors_dir . 'class-gofactox-*.php' ) as $file ) {
                    require_once $file;
                }
            }
        }
        if ( is_admin() ) {
            GOFACTOX_Dashboard::init();
            GOFACTOX_Bulk::init();
        }
        // On version change (typically a plugin upgrade), flush rewrite rules once so
        // routes added in newer versions (eg. /wfx-credit-note/ in 1.3.0) are picked up
        // without requiring the user to visit Settings > Permalinks.
        $stored_version = get_option( 'gofactox_plugin_version', '' );
        if ( $stored_version !== GOFACTOX_VERSION ) {
            add_action( 'init', static function () {
                flush_rewrite_rules( false );
            }, 99 );
            update_option( 'gofactox_plugin_version', GOFACTOX_VERSION, false );
        }
    }

    add_action( 'plugins_loaded', 'gofactox_init', 20 );
    /**
     * Activation hook: require activator directly since autoloader isn't loaded yet.
     */
    register_activation_hook( __FILE__, function () {
        require_once GOFACTOX_PLUGIN_DIR . 'includes/class-gofactox-activator.php';
        require_once GOFACTOX_PLUGIN_DIR . 'includes/class-gofactox-invoice.php';
        require_once GOFACTOX_PLUGIN_DIR . 'includes/class-gofactox-numbering.php';
        GOFACTOX_Activator::activate();
    } );
    /**
     * Deactivation hook.
     */
    register_deactivation_hook( __FILE__, function () {
        require_once GOFACTOX_PLUGIN_DIR . 'includes/class-gofactox-activator.php';
        GOFACTOX_Activator::deactivate();
    } );
}