<?php

/**
 * Plugin Name:       BuildsByLuke Facture Électronique for WooCommerce
 * Plugin URI:        https://buildsbyluke.com/plugins/facture-electronique
 * Description:       French e-invoicing (réforme 2026-2027) for WooCommerce: Factur-X/UBL/CII invoices, SIREN capture, credit notes, PA export, e-reporting.
 * Version:           1.1.2
 * Requires at least: 6.2
 * Requires PHP:      7.4
 * Requires Plugins:  woocommerce
 * Author:            Luke W
 * Author URI:        https://buildsbyluke.com
 * License:           GPLv2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       buildsbyluke-facture-electronique-for-woocommerce
 * Domain Path:       /languages
 *
 * @package FEPW
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
if ( function_exists( 'fepw_fs' ) ) {
    // The other build of this plugin (free/premium) is already running. Hand it
    // this file's basename so Freemius can deactivate the redundant one, and
    // stop here: nothing below must run twice. The `true` marks THIS as the
    // premium copy (this is the premium source); the Freemius free-version
    // processor flips it to `false` in the generated wp.org build.
    fepw_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( 'fepw_fs' ) ) {
        /**
         * Freemius SDK access.
         *
         * @return Freemius
         */
        function fepw_fs() {
            global $fepw_fs;
            if ( !isset( $fepw_fs ) ) {
                // Activate multisite network integration.
                if ( !defined( 'WP_FS__PRODUCT_35808_MULTISITE' ) ) {
                    define( 'WP_FS__PRODUCT_35808_MULTISITE', true );
                }
                // Include Freemius SDK.
                require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
                $fepw_fs = fs_dynamic_init( array(
                    'id'               => '35808',
                    'slug'             => 'buildsbyluke-facture-electronique-for-woocommerce',
                    'premium_slug'     => 'buildsbyluke-facture-electronique-for-woocommerce-premium',
                    'type'             => 'plugin',
                    'public_key'       => 'pk_f76ae5f23c9ba68b53ce6c11222c7',
                    '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' => 'fepw',
                    ),
                    'is_live'          => true,
                ) );
            }
            return $fepw_fs;
        }

        // Init Freemius.
        fepw_fs();
        /**
         * Turn the SDK's trial-promotion notice off.
         *
         * Left on, the SDK raises a sticky notice on EVERY admin screen 24 hours
         * after activation ("Test all our awesome premium features with a 14-day
         * free trial"), and raises it again every 30 days thereafter
         * (class-freemius.php, _add_trial_notice()). It is dismissible, so it is
         * arguably within guideline 11 — but it is still a site-wide upsell on
         * screens that have nothing to do with this plugin, and guideline 11 asks
         * for upgrade prompts to be "limited in scope and used sparingly, be that
         * contextually or only on the plugin's setting page".
         *
         * This plugin's upsell therefore lives in exactly one place: the SDK's
         * clearly-labelled "Upgrade" item under the plugin's own menu, which a
         * person only sees if they click it.
         *
         * `show_trial` is the SDK's own documented opt-out (start.php lists the
         * filter as fs_show_trial_{slug}; it is read at class-freemius.php,
         * `if ( ! $this->apply_filters( 'show_trial', true ) )`).
         */
        fepw_fs()->add_filter( 'show_trial', '__return_false' );
        /**
         * Turn the SDK's deactivation-feedback dialog off.
         *
         * Left on, deactivating the plugin opens a "quick feedback" modal asking
         * why, and a submitted reason is posted to api.freemius.com — including
         * by users who never opted in. The readme promises Freemius is contacted
         * only in the four click-through situations it lists, and this dialog
         * would be a fifth. Deactivation should be one click, not a survey.
         *
         * `show_deactivation_feedback_form` is the SDK's own documented opt-out
         * (class-freemius.php: "Developers can optionally hide the deactivation
         * feedback form using the 'show_deactivation_feedback_form' filter").
         */
        fepw_fs()->add_filter( 'show_deactivation_feedback_form', '__return_false' );
        // Signal that SDK was initiated.
        do_action( 'fepw_fs_loaded' );
    }
    // ------------------------------------------------------------------
    // Facture Électronique's own bootstrap.
    // ------------------------------------------------------------------
    define( 'FEPW_VERSION', '1.1.2' );
    define( 'FEPW_FILE', __FILE__ );
    define( 'FEPW_DIR', plugin_dir_path( __FILE__ ) );
    define( 'FEPW_URL', plugin_dir_url( __FILE__ ) );
    // Autoloader: FEPW_* classes + vendored namespaced libraries (class_exists-guarded).
    spl_autoload_register( function ( $class ) {
        if ( 0 === strpos( $class, 'FEPW_' ) ) {
            $file = FEPW_DIR . 'includes/class-' . str_replace( '_', '-', strtolower( $class ) ) . '.php';
            if ( is_readable( $file ) ) {
                require $file;
            }
            return;
        }
        $vendors = array(
            'Atgp\\FacturX\\'     => 'vendor/atgp/facturx/src/',
            'setasign\\Fpdi\\'    => 'vendor/setasign/fpdi/src/',
            'Smalot\\PdfParser\\' => 'vendor/smalot/pdfparser/src/Smalot/PdfParser/',
        );
        foreach ( $vendors as $prefix => $base ) {
            if ( 0 === strpos( $class, $prefix ) ) {
                $file = FEPW_DIR . $base . str_replace( '\\', '/', substr( $class, strlen( $prefix ) ) ) . '.php';
                if ( is_readable( $file ) ) {
                    require $file;
                }
                return;
            }
        }
    } );
    register_activation_hook( __FILE__, array('FEPW_Install', 'activate') );
    /**
     * UNINSTALL. There is deliberately no uninstall.php — WordPress runs either
     * uninstall.php OR the registered uninstall hook (never both), and a present
     * uninstall.php would deregister the Freemius SDK's uninstall handling.
     * Cleanup lives in FEPW_Uninstall: the SDK's `after_uninstall` action is the
     * sanctioned path, and the core `uninstall_{basename}` registration below is
     * the safety net for when the SDK instance cannot be resolved (it no-ops if
     * the SDK path already ran).
     */
    fepw_fs()->add_action( 'after_uninstall', array('FEPW_Uninstall', 'run') );
    add_action( 'uninstall_' . plugin_basename( __FILE__ ), array('FEPW_Uninstall', 'safety_net'), 100 );
    add_action( 'before_woocommerce_init', function () {
        if ( class_exists( '\\Automattic\\WooCommerce\\Utilities\\FeaturesUtil' ) ) {
            \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
        }
    } );
    /**
     * Boot after WooCommerce.
     */
    function fepw_boot() {
        if ( !class_exists( 'WooCommerce' ) ) {
            add_action( 'admin_notices', function () {
                if ( current_user_can( 'activate_plugins' ) ) {
                    printf( '<div class="notice notice-error"><p>%s</p></div>', esc_html__( 'Facture Électronique for WooCommerce requires WooCommerce to be installed and active.', 'buildsbyluke-facture-electronique-for-woocommerce' ) );
                }
            } );
            return;
        }
        FEPW_Install::maybe_upgrade();
        FEPW_Checkout::instance();
        FEPW_Documents::instance();
        FEPW_Export::instance();
        if ( is_admin() ) {
            FEPW_Admin::instance();
        }
        /**
         * Fires once the plugin (and, when licensed, the Pro layer) is loaded.
         */
        do_action( 'fepw_loaded' );
    }

    add_action( 'plugins_loaded', 'fepw_boot', 20 );
}