<?php
/**
 * Plugin Name: HumanGate
 * Plugin URI: https://wordpress.org/plugins/humangate/
 * Description: Lightweight but enforceable AI & Indexing Refusal + Selective Friction layer for WordPress. Global refusal signals, emergency lockdown, and scraping cost amplification without CAPTCHAs or heavy databases.
 * Version: 1.1.0
 * Author: DarkAI
 * Author URI: https://darkai.ca/
 * License: GPL-2.0+
 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain: humangate
 * Requires at least: 5.0
 * Requires PHP: 7.4
 */

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

// Define plugin constants
define('HUMANG_PLUGIN_VERSION', '1.1.0');
define('HUMANG_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('HUMANG_PLUGIN_URL', plugin_dir_url(__FILE__));
define('HUMANG_PLUGIN_BASENAME', plugin_basename(__FILE__));

// Autoloader
spl_autoload_register(function ($class) {
    $prefix = 'HumanGate\\';
    $base_dir = HUMANG_PLUGIN_DIR . 'includes/';
    
    $len = strlen($prefix);
    if (strncmp($prefix, $class, $len) !== 0) {
        return;
    }
    
    $relative_class = substr($class, $len);
    $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
    
    if (file_exists($file)) {
        require $file;
    }
});

// Initialize plugin
function humang_plugin_init() {
    // Load content control (must be initialized before refusal layer)
    $content_control = new HumanGate\ContentControl();
    $content_control->init();
    
    // Load core classes
    $refusal_layer = new HumanGate\RefusalLayer();
    $refusal_layer->init();
    
    $lockdown = new HumanGate\EmergencyLockdown();
    $lockdown->init();
    
    $friction = new HumanGate\SelectiveFriction();
    $friction->init();
    
    $telemetry = new HumanGate\Telemetry();
    $telemetry->init();
    
    // Admin interface
    if (is_admin()) {
        $admin = new HumanGate\Admin();
        $admin->init();
    }
}

// Initialize on plugins_loaded
add_action('plugins_loaded', 'humang_plugin_init');

// Activation hook
register_activation_hook(__FILE__, 'humang_activation');

/**
 * Activation callback - sets default options
 */
function humang_activation() {
    // Set default options
    add_option('humang_block_search_engines', true);
    add_option('humang_global_refusal', true);
    add_option('humang_enforcement_mode', 'signals_only');
    add_option('humang_verify_search_engine_bots', false);
    add_option('humang_lockdown_mode', false);
    add_option('humang_friction_enabled', true);
    add_option('humang_friction_burst_pages', 12);
    add_option('humang_friction_burst_seconds', 5);
    add_option('humang_friction_rate_pages', 40);
    add_option('humang_friction_rate_seconds', 60);
    add_option('humang_lockdown_require_login', false);
    add_option('humang_disable_robots_txt', false);
    add_option('humang_stats_retention', 'weekly');
    
    // Set default post type settings (all enabled by default)
    // Use function_exists check and validate result to avoid fatal errors
    if (function_exists('get_post_types')) {
        $post_types = get_post_types(['public' => true], 'names');
        if (!empty($post_types) && is_array($post_types)) {
            foreach ($post_types as $post_type) {
                if (!empty($post_type)) {
                    add_option('humang_refusal_post_types_' . $post_type, true);
                }
            }
        }
    }
}

// Deactivation hook
register_deactivation_hook(__FILE__, function() {
    // Clean up transients and scheduled events
    delete_transient('humang_daily_counters');
    delete_transient('humang_weekly_counters');
    wp_clear_scheduled_hook('humang_weekly_cleanup');
});

// Uninstall hook - removes all plugin data when plugin is deleted
register_uninstall_hook(__FILE__, 'humang_uninstall');

/**
 * Clean up all plugin data on uninstall
 * 
 * This function removes all options, transients, scheduled events, and post meta
 * created by HumanGate when the plugin is deleted.
 */
function humang_uninstall() {
    // Security check: ensure this is being called from WordPress
    if (!defined('WP_UNINSTALL_PLUGIN')) {
        return;
    }
    
    // Delete all plugin options
    $options = [
        'humang_block_search_engines',
        'humang_global_refusal',
        'humang_enforcement_mode',
        'humang_verify_search_engine_bots',
        'humang_lockdown_mode',
        'humang_friction_enabled',
        'humang_friction_burst_pages',
        'humang_friction_burst_seconds',
        'humang_friction_rate_pages',
        'humang_friction_rate_seconds',
        'humang_lockdown_require_login',
        'humang_disable_robots_txt',
        'humang_stats_retention',
    ];
    
    foreach ($options as $option) {
        delete_option($option);
    }
    
    // Delete post type settings (only if WordPress is loaded)
    if (function_exists('get_post_types')) {
        $post_types = get_post_types(['public' => true], 'names');
        if (is_array($post_types)) {
            foreach ($post_types as $post_type) {
                if ($post_type) {
                    delete_option('humang_refusal_post_types_' . $post_type);
                }
            }
        }
    }
    
    // Clear scheduled cron events
    if (function_exists('wp_clear_scheduled_hook')) {
        wp_clear_scheduled_hook('humang_weekly_cleanup');
    }
    
    // Note: Transients will expire automatically and don't need manual deletion
    // Post meta can be left as it doesn't cause issues if orphaned
}

