<?php

/**
 * Plugin Name: Integration for Zoho CRM and Zoho Bigin
 * Plugin URI: https://integrazo.com/products/integrate-any-form-with-zoho-crm
 * Description: Connect Contact Form 7, WPForms, Elementor Forms, Gravity Forms, and other form submissions with Zoho CRM and Zoho Bigin.
 * Version: 1.0.18
 * Author: Integrazo
 * Author URI: https://integrazo.com/
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: integrate-any-form-with-zoho-crm
 * Requires at least: 6.0
 * Requires PHP: 7.0
 */

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

// Define plugin constants.
define('IAFWZC_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('IAFWZC_PLUGIN_URL', plugin_dir_url(__FILE__));
define('IAFWZC_PLUGIN_FILE', __FILE__);
define('IAFWZC_VERSION', '1.0.18');
// Include core functionality files.
require_once IAFWZC_PLUGIN_DIR . 'includes/includes.php';

/**
 * Plugin activation callback.
 *
 * @param bool $network_wide Indicates if the activation is for a network of sites.
 */
function iafwzc_activate($network_wide)
{
    // List of class instances for table creation
    $db_classes = [
        'IAFWZC_Account_Table',
        'IAFWZC_Field_Mapping',
        'IAFWZC_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__, 'iafwzc_activate');

/**
 * Function to handle tasks during plugin deactivation
 */
function iafwzc_deactivate($network_wide)
{
    // CALL EMAIL ALERT
    iafwzc_send_deactivation_alert();
    //
    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
            iafwzc_uninstall(); // Call uninstall function for each site
            restore_current_blog(); // Restore the original site context
        }
    } else {
        // Handle single site deactivation
        iafwzc_uninstall(); // Call uninstall function for the current site
    }
}

// Hook for plugin deactivation
register_deactivation_hook(__FILE__, 'iafwzc_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 iafwzc_plugin_action_links($links)
{
    // Add a "Settings" link.
    $settings_link = '<a href="admin.php?page=integrate-any-form-with-zoho-crm&tab=accounts">' . esc_html__('Settings', 'integrate-any-form-with-zoho-crm') . '</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__), 'iafwzc_plugin_action_links');
