<?php
/**
 * Plugin Name: ZA Creative Login Shield
 * Description: Enterprise login security with 2FA, WebAuthn, risk scoring, analytics, file integrity, and vulnerability scanning.
 * Version:     6.0.1
 * Author:      Sipho Nhlapo
 * License:     GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: za-creative-login-shield
 * Domain Path: /languages
 * Requires WP: 6.0
 * Requires PHP: 7.4
 * Network:     true
 *
 * @package ZA_Creative_Login_Shield
 */

defined( 'ABSPATH' ) || exit;

define( 'ZACLS_VERSION', '6.0.1' );
define( 'ZACLS_PLUGIN_FILE', __FILE__ );
define( 'ZACLS_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'ZACLS_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'ZACLS_DB_VERSION', '2.2.0' );

function zacls_autoload( $class ) {
	$prefix   = 'ZACLS_';
	$base_dir = ZACLS_PLUGIN_DIR . 'includes/';

	if ( 0 !== strncmp( $prefix, $class, strlen( $prefix ) ) ) {
		return;
	}

	$class_name = str_replace( $prefix, '', $class );
	$file_map   = array(
		'Install'               => 'class-zacls-install.php',
		'Main'                  => 'class-zacls-main.php',
		'TwoFA'                 => 'class-zacls-2fa.php',
		'Login_Limiter'         => 'class-zacls-login-limiter.php',
		'IP_Blocker'            => 'class-zacls-ip-blocker.php',
		'Login_URL'             => 'class-zacls-login-url.php',
		'Email_Notifications'   => 'class-zacls-email-notifications.php',
		'Session'               => 'class-zacls-session.php',
		'Settings'              => 'class-zacls-settings.php',
		'Device'                => 'class-zacls-device.php',
		'Password_Policy'       => 'class-zacls-password-policy.php',
		'Emergency'             => 'class-zacls-emergency.php',
		'Export'                => 'class-zacls-export.php',
		'Admin'                 => ZACLS_PLUGIN_DIR . 'admin/class-zacls-admin.php',
		'Audit_Trail'           => 'class-zacls-audit-trail.php',
		'Privacy'               => 'class-zacls-privacy.php',
		'Base32'                => 'class-zacls-base32.php',
		'Health'                => 'class-zacls-health.php',
		'WebAuthn'              => 'class-zacls-webauthn.php',
		'WebAuthn_Verifier'     => 'class-zacls-webauthn-verifier.php',
		'Risk_Scoring'          => 'class-zacls-risk-scoring.php',
		'Analytics'             => 'class-zacls-analytics.php',
		'File_Monitor'          => 'class-zacls-file-monitor.php',
		'Vulnerability_Scanner' => 'class-zacls-vulnerability-scanner.php',
		'Notifications'         => 'class-zacls-notifications.php',
		'Branding'              => 'class-zacls-branding.php',
		'Email_Templates'       => 'class-zacls-email-templates.php',
		'REST_API'              => 'class-zacls-rest-api.php',
		'Background'            => 'class-zacls-background.php',
		'PDF'                   => 'class-zacls-pdf.php',
		'Multisite'             => 'class-zacls-multisite.php',
		'AI_Assistant'          => 'class-zacls-ai-assistant.php',
		'Performance'           => 'class-zacls-performance.php',
		'Auto_Cleanup'          => 'class-zacls-auto-cleanup.php',
	);

	if ( isset( $file_map[ $class_name ] ) ) {
		$file = $file_map[ $class_name ];
		if ( file_exists( $file ) ) {
			require_once $file;
		} elseif ( file_exists( $base_dir . $file ) ) {
			require_once $base_dir . $file;
		}
	}
}
spl_autoload_register( 'zacls_autoload' );

function zacls_init() {
	static $instance = null;
	if ( null === $instance ) {
		ZACLS_Auto_Cleanup::init();
		$instance = new ZACLS_Main();
	}
	return $instance;
}
add_action( 'plugins_loaded', 'zacls_init' );

register_activation_hook( __FILE__, array( 'ZACLS_Install', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'ZACLS_Install', 'deactivate' ) );

function zacls_check_db_upgrade() {
	if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
		return;
	}
	if ( ! current_user_can( 'manage_options' ) ) {
		return;
	}
	ZACLS_Install::maybe_upgrade();
}
add_action( 'admin_init', 'zacls_check_db_upgrade' );

add_action( 'wp_ajax_zacls_dismiss_notification', array( 'ZACLS_Notifications', 'ajax_dismiss' ) );
add_action( 'zacls_process_queue', array( 'ZACLS_Background', 'process_queue' ) );
