<?php
/**
 * ADDON - POP to Fatture in Cloud
 */

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

if (! defined('WP_FATT_CLOUD')) {
    define('WP_FATT_CLOUD', 'prod');
}

if (! defined('WP_FATT_CLOUD_LOG')) {
    define('WP_FATT_CLOUD_LOG', false);
}

if (! defined('WP_FATT_CLOUD_DELETE_DATA_WHEN_DEACTIVATE')) {
    define('WP_FATT_CLOUD_DELETE_DATA_WHEN_DEACTIVATE', true);
}

// Define constant.
define('WP_FATT_CLOUD_NAME', 'POP to Fatture in Cloud');
define('WP_FATT_CLOUD_TEXTDOMAIN', 'wfc');
define('WP_FATT_CLOUD_VERSION', '2.6.10');
define('WP_FATT_CLOUD_API', get_option('wfc_api_url'));
// Plugin DIR
define('WP_FATT_CLOUD_PLUGIN_DIR', basename(plugin_dir_path(__FILE__)));
// Dirs
define('WP_FATT_CLOUD_DIR', plugin_dir_path(__FILE__));
// Uri
define('WP_FATT_CLOUD_URL', plugin_dir_url(__FILE__));

// Base Requirements.
require_once untrailingslashit(WP_FATT_CLOUD_DIR . 'src/Plugin.php');
require_once untrailingslashit(WP_FATT_CLOUD_DIR . 'requires.php');

// Register the activation and deactivation hook.
register_activation_hook(__FILE__, array('WooPoPToFattureInCloud\\Activate', 'activate'));
register_deactivation_hook(__FILE__, array('WooPoPToFattureInCloud\\Deactivate', 'deactivate'));

// Init
$bootstrap = function () {
    // Prevent init plugin in other ajax action
    if (! is_admin() && defined('DOING_AJAX') && DOING_AJAX) {
        return;
    }

    if (method_exists(\WooPoPToFattureInCloud\Plugin::class, 'load_plugin_textdomain')) {
        // Load Localisation files.
        \WooPoPToFattureInCloud\Plugin::load_plugin_textdomain();
    }

    // Check for the dependency.
    if (\WooPoPToFattureInCloud\Functions\isWooCommerceActive() &&
        class_exists('WcElectronInvoice\\Plugin')
    ) :
        $filters = array();

        // Global filter
        $filters = array_merge(
            $filters,
            include \WooPoPToFattureInCloud\Plugin::getPluginDirPath('/inc/filters.php')
        );

        // Admin or Front filter
        if (is_admin()) {
            $filters = array_merge(
                $filters,
                include \WooPoPToFattureInCloud\Plugin::getPluginDirPath('/inc/filtersAdmin.php')
            );
        } else {
            $filters = array_merge(
                $filters,
                include \WooPoPToFattureInCloud\Plugin::getPluginDirPath('/inc/filtersFront.php')
            );
        }

        if (\WooPoPToFattureInCloud\Functions\isAjaxRequest()) {
            $filters = array_merge(
                $filters,
                include \WooPoPToFattureInCloud\Plugin::getPluginDirPath('/inc/filtersAjax.php')
            );
        }

        // Loader init.
        $init = new WooPoPToFattureInCloud\Init(new WooPoPToFattureInCloud\Loader(), $filters);
        $init->init();
    endif;

};

if (did_action('init')) {
    $bootstrap();
} else {
    add_action('init', $bootstrap, 20);
}
