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

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

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

// Define constant.
define('WP_FATT_CLOUD_STOCK_NAME', 'POP to Fatture in Cloud - Manage Stock');
define('WP_FATT_CLOUD_STOCK_TEXTDOMAIN', 'wfc_ms');
define('WP_FATT_CLOUD_STOCK_VERSION', '2.1.4');
define('WP_FATT_CLOUD_STOCK_API', get_option('wfc_api_url'));
// Plugin DIR
define('WP_FATT_CLOUD_STOCK_PLUGIN_DIR', basename(plugin_dir_path(__FILE__)));
// Dirs
define('WP_FATT_CLOUD_STOCK_DIR', plugin_dir_path(__FILE__));
// Uri
define('WP_FATT_CLOUD_STOCK_URL', plugin_dir_url(__FILE__));

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

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

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

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

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

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

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

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

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

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