<?php

/**
 * Plugin Name: Integration for Zoho Campaigns
 * Plugin URI: https://integrazo.com/products/integrate-with-zoho-campaigns
 * Description: Connect Contact Form 7, WPForms, Elementor Forms, Gravity Forms, and more form submissions with Zoho Campaigns.
 * Version: 1.0.11
 * Author: Integrazo
 * Author URI: https://integrazo.com/
 * License: GPL2
 * Text Domain: integrate-with-zoho-campaigns
 */

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

// Define plugin constants.
define('IAFWZCAI_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('IAFWZCAI_PLUGIN_URL', plugin_dir_url(__FILE__));

// Include core functionality files.
require_once IAFWZCAI_PLUGIN_DIR . 'includes/includes.php';


/**
 * Plugin activation callback.
 *
 * @param bool $network_wide Indicates if the activation is for a network of sites.
 */
function iafwzcai_activate($network_wide)
{
    // List of class instances for table creation
    $db_classes = [
        'IAFWZCAI_Account_Table',
        'IAFWZCAI_Field_Mapping',
        'IAFWZCAI_Error_Log',
    ];

    // Helper function to create tables for each class
    $create_tables = function () use ($db_classes) {
        foreach ($db_classes as $class_name) {
            if (class_exists($class_name)) {
                $instance = new $class_name();
                if (method_exists($instance, 'create_table')) {
                    $instance->create_table();
                }
            }
        }
    };

    // Multisite handling: loop through all sites in the network
    if (is_multisite() && $network_wide) {
        foreach (get_sites() as $site) {
            switch_to_blog($site->blog_id);
            $create_tables(); // Create tables for the current site
            restore_current_blog();
        }
    } else {
        $create_tables(); // Single-site activation
    }
}
// Activation and deactivation hooks.
register_activation_hook(__FILE__, 'iafwzcai_activate');
/**
 * Function to handle tasks during plugin deactivation
 */
function iafwzcai_deactivate($network_wide)
{
    if (is_multisite() && $network_wide) {
        // Handle network-wide deactivation
        $sites = get_sites(array('fields' => 'ids')); // Fetch only site IDs for efficiency
        foreach ($sites as $site_id) {
            switch_to_blog($site_id); // Switch to the current site in the loop
            iafwzcai_uninstall(); // Call uninstall function for each site
            restore_current_blog(); // Restore the original site context
        }
    } else {
        // Handle single site deactivation
        iafwzcai_uninstall(); // Call uninstall function for the current site
    }
}

// Hook for plugin deactivation
register_deactivation_hook(__FILE__, 'iafwzcai_deactivate');
/**
 * Add a "Settings" link to the plugin action links on the Plugins page.
 *
 * @param array $links Existing plugin action links.
 * @return array Modified plugin action links.
 */
function iafwzcai_plugin_action_links($links)
{
    // Add a "Settings" link.
    $settings_link = '<a href="admin.php?page=integrate-with-zoho-campaigns&tab=accounts">' . esc_html__('Settings', 'integrate-with-zoho-campaigns') . '</a>';
    array_unshift($links, $settings_link); // Add the settings link at the beginning of the links array.
    return $links;
}
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'iafwzcai_plugin_action_links');
