<?php
/**
 * Plugin Name: WPForms to Mailjet Automation
 * Plugin URI: https://yourwebsite.com/wpforms-mailjet-automation
 * Description: Automatically add WPForms submissions to Mailjet lists based on form field answers. Set and forget automation with a beautiful wizard interface.
 * Version: 1.0.2
 * Author: Your Name
 * Author URI: https://yourwebsite.com
 * License: GPL-2.0+
 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain: wpforms-mailjet-automation
 * Domain Path: /languages
 * Requires at least: 5.8
 * Requires PHP: 7.4
 * WC requires at least: 5.0
 * WC tested up to: 8.0
 */

// If this file is called directly, abort.
if (!defined('WPINC')) {
    die;
}

/**
 * Current plugin version.
 * Version 1.0.2 - All security issues resolved + configuration enhancements (2025-10-16)
 */
define('WPFMJ_VERSION', '1.0.2');
define('WPFMJ_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('WPFMJ_PLUGIN_URL', plugin_dir_url(__FILE__));
define('WPFMJ_PLUGIN_BASENAME', plugin_basename(__FILE__));

/**
 * The code that runs during plugin activation.
 */
function activate_wpfmj() {
    require_once WPFMJ_PLUGIN_DIR . 'includes/class-wpfmj-activator.php';
    WPFMJ_Activator::activate();
}

/**
 * The code that runs during plugin deactivation.
 */
function deactivate_wpfmj() {
    require_once WPFMJ_PLUGIN_DIR . 'includes/class-wpfmj-deactivator.php';
    WPFMJ_Deactivator::deactivate();
}

register_activation_hook(__FILE__, 'activate_wpfmj');
register_deactivation_hook(__FILE__, 'deactivate_wpfmj');

/**
 * Load custom configuration if exists.
 * This allows users to override default settings without modifying core files.
 */
if (file_exists(WPFMJ_PLUGIN_DIR . 'wpfmj-config.php')) {
    require_once WPFMJ_PLUGIN_DIR . 'wpfmj-config.php';
}

/**
 * The core plugin class.
 */
require WPFMJ_PLUGIN_DIR . 'includes/class-wpfmj-core.php';

/**
 * Begins execution of the plugin.
 */
function run_wpfmj() {
    $plugin = new WPFMJ_Core();
    $plugin->run();
}

// Check for required plugins before running
add_action('plugins_loaded', function() {
    // Check if WPForms is active
    if (!function_exists('wpforms')) {
        add_action('admin_notices', function() {
            ?>
            <div class="notice notice-error">
                <p><?php esc_html_e('WPForms to Mailjet Automation requires WPForms to be installed and activated.', 'wpforms-mailjet-automation'); ?></p>
            </div>
            <?php
        });
        return;
    }
    
    // All requirements met, run the plugin
    run_wpfmj();
}, 20);

/**
 * Declare HPOS compatibility for WooCommerce.
 */
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);
    }
});
