<?php
/**
 * Plugin Name: Xerotact SHIELD-X Security
 * Description: Shared-hosting security with WAF logging, opt-in enforcement, malware scanning, login protection, hardening, backups, and audit tools.
 * Version: 2.0.175
 * Tested up to: 7.1
 * Plugin URI: https://xerotact.com/shield-x
 * Requires at least: 6.5
 * Requires PHP: 7.4
 * Author: Xerotact
 * Author URI: https://xerotact.com/
 * License: GPLv3
 * License URI: https://www.gnu.org/licenses/gpl-3.0.html
 * Text Domain: xerotact-shield-x-security
 * Domain Path: /languages
 */

declare(strict_types=1);

/*
 * Filesystem note for reviewers:
 * SHIELD-X reads WordPress files directly for local malware scans, restore
 * manifests, and plugin-owned runtime metadata outside the plugin directory.
 * Verifier archives and extracted source files use transient system-temp
 * workspaces only and are removed after each operation.
 * Admin-facing writes to host-managed files such as .htaccess, wp-config.php,
 * and lockdown permission changes use WordPress filesystem helpers
 * where credentials may be required, or fail closed with admin instructions.
 */

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

if (defined('SHIELDX_VERSION')) {
    return;
}

define('SHIELDX_VERSION', '2.0.175');
define('SHIELDX_PLUGIN_FILE', __FILE__);
define('SHIELDX_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('SHIELDX_PLUGIN_URL', plugin_dir_url(__FILE__));
$shieldxPluginsDir = rtrim(str_replace('\\', '/', dirname(plugin_dir_path(__FILE__))), '/') . '/';
$shieldxContentDir = '';
if (function_exists('wp_upload_dir')) {
    $shieldxUploads = wp_upload_dir(null, false);
    if (is_array($shieldxUploads) && !empty($shieldxUploads['basedir']) && is_scalar($shieldxUploads['basedir'])) {
        $shieldxUploadsDir = rtrim(str_replace('\\', '/', (string) $shieldxUploads['basedir']), '/');
        $shieldxUploadsMarker = '/uploads';
        $shieldxUploadsPos = strpos($shieldxUploadsDir . '/', $shieldxUploadsMarker . '/');
        if ($shieldxUploadsPos !== false) {
            $shieldxContentDir = rtrim(substr($shieldxUploadsDir, 0, (int) $shieldxUploadsPos), '/') . '/';
        }
    }
}
if ($shieldxContentDir === '') {
    $shieldxContentDir = rtrim(str_replace('\\', '/', dirname(rtrim($shieldxPluginsDir, '/'))), '/') . '/';
}
define('SHIELDX_PLUGINS_DIR', $shieldxPluginsDir);
define('SHIELDX_CONTENT_DIR', $shieldxContentDir);
define('SHIELDX_PLUGIN_BASENAME', plugin_basename(__FILE__));
define('SHIELDX_TEXT_DOMAIN', 'xerotact-shield-x-security');
define('SHIELDX_MANAGE_CAPABILITY', 'manage_shieldx');
define('SHIELDX_ADDON_API_VERSION', 1);

unset($shieldxPluginsDir, $shieldxContentDir, $shieldxUploads, $shieldxUploadsDir, $shieldxUploadsMarker, $shieldxUploadsPos);

require_once SHIELDX_PLUGIN_DIR . 'includes/Autoloader.php';

\ShieldX\Autoloader::register(SHIELDX_PLUGIN_DIR . 'includes');

register_activation_hook(__FILE__, [\ShieldX\Core\Activator::class, 'activate']);
register_deactivation_hook(__FILE__, [\ShieldX\Core\Deactivator::class, 'deactivate']);

add_filter(
    'map_meta_cap',
    static function (array $caps, string $cap, int $userId, array $args): array {
        if ($cap === SHIELDX_MANAGE_CAPABILITY) {
            return ['manage_options'];
        }

        return $caps;
    },
    10,
    4
);

add_action(
    'plugins_loaded',
    static function (): void {
        \ShieldX\Core\Plugin::instance()->boot();
    },
    -100
);
