<?php
/**
 * Plugin Name:       Guardian Shield
 * Plugin URI:        https://www.vlabtech.com
 * Description:       Advanced WordPress security plugin protecting against brute force, malware, SQL injection, XSS, file modification attacks, bot traffic, and more.
 * Version:           1.2.10
 * Requires at least: 6.0
 * Requires PHP:      8.0
 * Author:            VLabTech
 * Author URI:        https://www.vlabtech.com
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       guardian-shield
 * Domain Path:       /languages
 * Network:           false
 *
 * @package GuardianShield
 */

// Prevent direct file access — a critical security gate.
if ( ! defined( 'ABSPATH' ) ) {
	exit( 'No direct script access allowed.' );
}

// ──────────────────────────────────────────────
// Plugin Constants
// ──────────────────────────────────────────────
define( 'GS_VERSION',      '1.2.10' );
define( 'GS_PLUGIN_FILE',  __FILE__ );
define( 'GS_PLUGIN_DIR',   plugin_dir_path( __FILE__ ) );
define( 'GS_PLUGIN_URL',   plugin_dir_url( __FILE__ ) );
define( 'GS_PLUGIN_BASE',  plugin_basename( __FILE__ ) );
define( 'GS_INCLUDES_DIR', GS_PLUGIN_DIR . 'includes/' );
define( 'GS_ADMIN_DIR',    GS_PLUGIN_DIR . 'admin/' );
define( 'GS_ASSETS_URL',   GS_PLUGIN_URL . 'assets/' );
define( 'GS_LOG_DIR',      WP_CONTENT_DIR . '/guardian-shield-logs/' );
define( 'GS_QUARANTINE',   WP_CONTENT_DIR . '/guardian-shield-quarantine/' );
define( 'GS_MIN_PHP',      '8.0' );
define( 'GS_MIN_WP',       '6.0' );

// ──────────────────────────────────────────────
// PHP/WP Version Gate
// ──────────────────────────────────────────────
if ( version_compare( PHP_VERSION, GS_MIN_PHP, '<' ) ) {
	add_action( 'admin_notices', function () {
		$msg = sprintf(
			/* translators: 1: required PHP version, 2: current PHP version */
			esc_html__( 'Guardian Shield requires PHP %1$s or higher. Your server is running PHP %2$s.', 'guardian-shield' ),
			GS_MIN_PHP,
			PHP_VERSION
		);
		echo '<div class="notice notice-error"><p>' . esc_html( $msg ) . '</p></div>';
	} );
	return;
}

// ──────────────────────────────────────────────
// Load global helper functions (not autoloadable — procedural file).
require_once GS_INCLUDES_DIR . 'functions.php';

// Class-map autoloader — explicit mapping, no string-transform guessing.
// ─────────────────────────────────────────────────────────────────────────────
spl_autoload_register( function ( string $class ): void {

	static $map = [
		'GuardianShield\\Plugin'           => 'includes/class-plugin.php',
		'GuardianShield\\Installer'        => 'includes/class-installer.php',
		'GuardianShield\\Firewall'         => 'includes/class-firewall.php',
		'GuardianShield\\Logger'           => 'includes/class-logger.php',
		'GuardianShield\\LoginSecurity'    => 'includes/class-login-security.php',
		'GuardianShield\\Scanner'          => 'includes/class-scanner.php',
		'GuardianShield\\FileMonitor'      => 'includes/class-file-monitor.php',
		'GuardianShield\\RateLimiter'      => 'includes/class-rate-limiter.php',
		'GuardianShield\\BotDetector'      => 'includes/class-bot-detector.php',
		'GuardianShield\\SecurityHeaders'  => 'includes/class-security-headers.php',
		'GuardianShield\\XmlRpcGuard'      => 'includes/class-xmlrpc-guard.php',
		'GuardianShield\\RestApiGuard'     => 'includes/class-rest-api-guard.php',
		'GuardianShield\\Mailer'           => 'includes/class-mailer.php',
		'GuardianShield\\Admin\\Dashboard'  => 'admin/class-dashboard.php',
	];

	if ( isset( $map[ $class ] ) ) {
		$file = GS_PLUGIN_DIR . $map[ $class ];
		if ( file_exists( $file ) ) {
			require_once $file;
		}
	}
} );

// Activation / Deactivation / Uninstall Hooks
// ─────────────────────────────────────────────────────────────────────────────
register_activation_hook(   GS_PLUGIN_FILE, [ 'GuardianShield\\Installer', 'activate' ] );
register_deactivation_hook( GS_PLUGIN_FILE, [ 'GuardianShield\\Installer', 'deactivate' ] );

// Bootstrap the plugin after all plugins are loaded.
// ─────────────────────────────────────────────────────────────────────────────
add_action( 'plugins_loaded', function (): void {
	// Load translations.
	load_plugin_textdomain(
		'guardian-shield',
		false,
		dirname( GS_PLUGIN_BASE ) . '/languages'
	);

	// Boot the main plugin instance — singleton pattern.
	GuardianShield\Plugin::get_instance();
}, 1 ); // Priority 1 = very early, so the firewall runs before other plugins.
