<?php

/**
 * Plugin Name: Integration for Freshsales CRM
 * Plugin URI: https://integrazo.com/products/freshworks-crm-integration-with-any-form
 * Description: Connect Contact Form 7, WPForms, Elementor Forms, Gravity Forms, and more form submissions with Freshsales CRM.
 * Version: 1.0.15
 * Author: Integrazo
 * Author URI: https://integrazo.com/
 * License: GPL2
 * Text Domain: crm-integration-freshworks-any-form
 */

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

// Define plugin constants.
define('INTEGRAZO_FWCRM_FORM_VERSION', '1.0.15');
define('INTEGRAZO_FWCRM_FORM_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('INTEGRAZO_FWCRM_FORM_PLUGIN_URL', plugin_dir_url(__FILE__));

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


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

    // 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__, 'integrazo_fwcrm_form_activate');



/**
 * Function to handle tasks during plugin deactivation
 */
function integrazo_fwcrm_form_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
            integrazo_fwcrm_form_uninstall(); // Call uninstall function for each site
            restore_current_blog(); // Restore the original site context
        }
    } else {
        // Handle single site deactivation
        integrazo_fwcrm_form_uninstall(); // Call uninstall function for the current site
    }
}

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