<?php
/**
 * Plugin Name:       UltraGuard – Security Suite
 * Plugin URI:        https://ultraguard.net
 * Description:       WordPress security suite with firewall protection, malware scanning, login hardening, file integrity monitoring, and security alerts.
 * Version:           6.7.2
 * Requires at least: 5.6
 * Requires PHP:      8.1
 * Author:            Vikas Bhardwaj
 * Author URI:        https://vikasbhardwaj.in
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       ultraguard-security-suite
 * Domain Path:       /languages
 * Network:           true
 *
 * @package UltraguardPro
 */

namespace UltraguardPro;

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

// ─── Constants ────────────────────────────────────────────────────────────────
define( 'ULTRAGUARD_PRO_VERSION',     '6.7.2' );
define( 'ULTRAGUARD_PRO_URL',         plugin_dir_url( __FILE__ ) );
define( 'ULTRAGUARD_PRO_PATH',        plugin_dir_path( __FILE__ ) );
define( 'ULTRAGUARD_PRO_BASENAME',    plugin_basename( __FILE__ ) );
define( 'ULTRAGUARD_PRO_MIN_PHP',     '8.1' );
define( 'ULTRAGUARD_PRO_MIN_WP',      '5.6' );
define( 'ULTRAGUARD_PRO_DB_VERSION',  '1.3.0' );

// ─── PSR-4 Autoloader ─────────────────────────────────────────────────────────
spl_autoload_register( function ( string $class ) {
    $prefix   = 'UltraguardPro\\';
    $base_dir = __DIR__ . '/includes/';

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

    $relative   = substr( $class, strlen( $prefix ) );
    $parts      = explode( '\\', $relative );
    $class_name = array_pop( $parts );
    $dir        = $base_dir . ( $parts ? strtolower( implode( '/', $parts ) ) . '/' : '' );

    // PascalCase → kebab-case  e.g. CacheManager → cache-manager
    $kebab = strtolower( preg_replace( '/(?<!^)[A-Z]/', '-$0', $class_name ) );
    $candidates = [
        $dir . 'class-'     . $kebab . '.php',
        $dir . 'interface-' . $kebab . '.php',
        $dir . 'trait-'     . $kebab . '.php',
        $dir . 'abstract-'  . $kebab . '.php',
    ];

    // For interfaces named InterfaceFoo → also try interface-foo.php
    if ( str_starts_with( $kebab, 'interface-' ) ) {
        $candidates[] = $dir . $kebab . '.php';
    }

    foreach ( $candidates as $file ) {
        if ( file_exists( $file ) ) {
            require_once $file;
            return;
        }
    }
} );

// ─── Bootstrap ────────────────────────────────────────────────────────────────
require_once ULTRAGUARD_PRO_PATH . 'includes/class-plugin.php';

/**
 * Main accessor — returns the singleton Plugin instance.
 *
 * @return Plugin
 */
function ultraguard_pro(): Plugin {
    return Plugin::get_instance();
}

// Convenience helpers (backwards-compatible)
function ultraguard_get_option( string $key, $default = false ) {
    return ultraguard_pro()->get_option( $key, $default );
}
function ultraguard_update_option( string $key, $value ): bool {
    return ultraguard_pro()->update_option( $key, $value );
}
function ultraguard_get_capability(): string {
    return ultraguard_pro()->get_capability();
}

function ultraguard_current_user_can_manage(): bool {
    if ( current_user_can( 'manage_options' ) ) {
        return true;
    }

    return is_multisite() && current_user_can( 'manage_network_options' );
}

// Start
add_action( 'plugins_loaded', __NAMESPACE__ . '\\ultraguard_pro', 5 );
add_action( 'wp_initialize_site', __NAMESPACE__ . '\\ultraguard_pro_initialize_new_site', 20 );

// ─── Activation / Deactivation / Uninstall ────────────────────────────────────
require_once ULTRAGUARD_PRO_PATH . 'includes/lifecycle.php';

register_activation_hook(   __FILE__, __NAMESPACE__ . '\\ultraguard_pro_activate'   );
register_deactivation_hook( __FILE__, __NAMESPACE__ . '\\ultraguard_pro_deactivate' );
register_uninstall_hook(    __FILE__, __NAMESPACE__ . '\\ultraguard_pro_uninstall'  );
