<?php

/**
 * Plugin Name: Integration for Zoho Desk
 * Plugin URI: https://integrazo.com/products/integrate-with-zoho-desk
 * Description: Connect Contact Form 7, WPForms, Elementor Forms, Gravity Forms, and more form submissions with Zoho Desk.
 * Version: 1.0.14
 * 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-with-zoho-desk
 * Requires at least: 6.0
 * Requires PHP:      7.0
 */

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

// Define plugin constants.
define('IAFWZDESK_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('IAFWZDESK_PLUGIN_URL', plugin_dir_url(__FILE__));

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


/**
 * Plugin activation callback.
 *
 * @param bool $network_wide Indicates if the activation is for a network of sites.
 */
function iafwzdesk_activate($network_wide)
{
    // List of class instances for table creation
    $db_classes = [
        'IAFWZDESK_Account_Table',
        'IAFWZDESK_Field_Mapping',
        'IAFWZDESK_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__, 'iafwzdesk_activate');
/**
 * Function to handle tasks during plugin deactivation
 */
function iafwzdesk_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
            iafwzdesk_uninstall(); // Call uninstall function for each site
            restore_current_blog(); // Restore the original site context
        }
    } else {
        // Handle single site deactivation
        iafwzdesk_uninstall(); // Call uninstall function for the current site
    }
}

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