<?php


namespace WPUltimateSecurity\UltimateSecurity\Support\Helpers;


class PluginDetection
{
    // =========================================================================
    // E-COMMERCE PLUGINS
    // =========================================================================

    public static function is_woocommerce_active(): bool
    {
        return class_exists('WooCommerce');
    }

    public static function is_woocommerce_page(): bool
    {
        if (!self::is_woocommerce_active()) {
            return false;
        }
        return is_woocommerce() || is_cart() || is_checkout() || is_account_page();
    }

    public static function is_woocommerce_ajax(): bool
    {
        if (!self::is_woocommerce_active()) {
            return false;
        }
        $wc_ajax = isset($_GET['wc-ajax']) ||
            (defined('WC_DOING_AJAX') && WC_DOING_AJAX);
        return $wc_ajax;
    }

    public static function get_woocommerce_ajax_whitelist(): array
    {
        return [
            'woocommerce_add_to_cart',
            'woocommerce_remove_from_cart',
            'woocommerce_update_cart',
            'woocommerce_checkout',
            'woocommerce_apply_coupon',
        ];
    }

    // =========================================================================
    // SECURITY PLUGINS - CONFLICT DETECTION
    // =========================================================================

    public static function is_wordfence_active(): bool
    {
        return defined('WORDFENCE_VERSION');
    }

    public static function get_conflicting_plugins(): array
    {
        $conflicts = [];

        // Tier 1: Major Security Suites
        if (defined('WORDFENCE_VERSION')) {
            $conflicts[] = [
                'name' => 'Wordfence Security',
                'slug' => 'wordfence',
                'features' => ['firewall', '2fa', 'captcha', 'login_limit', 'malware_scan', 'rate_limit']
            ];
        }

        if (class_exists('ITSEC_Core')) {
            $conflicts[] = [
                'name' => 'Solid Security (iThemes)',
                'slug' => 'better-wp-security',
                'features' => ['2fa', 'login_limit', 'hide_login', 'hardening']
            ];
        }

        if (class_exists('AIO_WP_Security')) {
            $conflicts[] = [
                'name' => 'All In One WP Security',
                'slug' => 'all-in-one-wp-security-and-firewall',
                'features' => ['firewall', '2fa', 'login_limit', 'captcha', 'file_security']
            ];
        }

        if (defined('SUCURISCAN_VERSION')) {
            $conflicts[] = [
                'name' => 'Sucuri Security',
                'slug' => 'sucuri-scanner',
                'features' => ['firewall', 'malware_scan', 'file_integrity', 'hardening']
            ];
        }

        if (class_exists('Jetpack') && self::jetpack_has_security()) {
            $conflicts[] = [
                'name' => 'Jetpack (Security features)',
                'slug' => 'jetpack',
                'features' => ['brute_force', 'downtime_monitor']
            ];
        }

        // Tier 2: Popular Security
        if (defined('LLA_PLUGIN_DIR') || class_exists('Limit_Login_Attempts_Reloaded')) {
            $conflicts[] = [
                'name' => 'Limit Login Attempts Reloaded',
                'slug' => 'limit-login-attempts-reloaded',
                'features' => ['login_limit']
            ];
        }

        if (defined('DEVELOPER_LOGINIZER') || class_exists('Loginizer_Security')) {
            $conflicts[] = [
                'name' => 'Loginizer',
                'slug' => 'loginizer',
                'features' => ['login_limit', '2fa', 'captcha', 'hide_login']
            ];
        }

        if (class_exists('MalCare') || defined('FLAVOR_STARTER')) {
            $conflicts[] = [
                'name' => 'MalCare Security',
                'slug' => 'malcare-security',
                'features' => ['firewall', 'malware_scan', 'login_protection']
            ];
        }

        if (class_exists('WP_Defender') || defined('DEFENDER_VERSION')) {
            $conflicts[] = [
                'name' => 'Defender Security',
                'slug' => 'defender-security',
                'features' => ['2fa', 'firewall', 'malware_scan', 'login_limit', 'headers']
            ];
        }

        if (defined('NFW_ENGINE_VERSION')) {
            $conflicts[] = [
                'name' => 'NinjaFirewall',
                'slug' => 'ninjafirewall',
                'features' => ['firewall', 'file_integrity', 'brute_force']
            ];
        }

        if (defined('ICWP_DS')) {
            $conflicts[] = [
                'name' => 'Shield Security',
                'slug' => 'wp-simple-firewall',
                'features' => ['firewall', '2fa', 'login_protection', 'bot_protection']
            ];
        }

        if (defined('SECUPRESS_VERSION')) {
            $conflicts[] = [
                'name' => 'SecuPress',
                'slug' => 'secupress',
                'features' => ['firewall', 'malware_scan', 'login_protection', 'hardening']
            ];
        }

        if (defined('CERBER_VER')) {
            $conflicts[] = [
                'name' => 'WP Cerber Security',
                'slug' => 'wp-cerber',
                'features' => ['login_limit', '2fa', 'captcha', 'anti_spam', 'malware_scan']
            ];
        }

        // Tier 3: 2FA Plugins
        if (class_exists('Two_Factor_Core')) {
            $conflicts[] = [
                'name' => 'Two-Factor',
                'slug' => 'two-factor',
                'features' => ['2fa']
            ];
        }

        if (defined('WP2FA_VERSION')) {
            $conflicts[] = [
                'name' => 'WP 2FA',
                'slug' => 'wp-2fa',
                'features' => ['2fa']
            ];
        }

        if (class_exists('Miniorange_Authentication')) {
            $conflicts[] = [
                'name' => 'miniOrange 2FA',
                'slug' => 'miniorange-2-factor-authentication',
                'features' => ['2fa']
            ];
        }

        if (class_exists('GoogleAuthenticator')) {
            $conflicts[] = [
                'name' => 'Google Authenticator',
                'slug' => 'google-authenticator',
                'features' => ['2fa']
            ];
        }

        // Tier 4: Login Security
        if (defined('WPS_LIMIT_LOGIN_VERSION')) {
            $conflicts[] = [
                'name' => 'WPS Limit Login',
                'slug' => 'wps-limit-login',
                'features' => ['login_limit']
            ];
        }

        if (defined('LOGIN_LOCKDOWN_VERSION')) {
            $conflicts[] = [
                'name' => 'Login LockDown',
                'slug' => 'login-lockdown',
                'features' => ['login_limit', 'captcha']
            ];
        }

        // Tier 5: CAPTCHA Plugins
        if (defined('GGL_CAPTCHA_VERSION')) {
            $conflicts[] = [
                'name' => 'reCaptcha by BestWebSoft',
                'slug' => 'google-captcha',
                'features' => ['captcha']
            ];
        }

        if (defined('AGRC_VERSION')) {
            $conflicts[] = [
                'name' => 'Advanced Google reCAPTCHA',
                'slug' => 'advanced-google-recaptcha',
                'features' => ['captcha']
            ];
        }

        if (defined('HCAPTCHA_VERSION')) {
            $conflicts[] = [
                'name' => 'hCaptcha for WP',
                'slug' => 'hcaptcha-for-forms-and-more',
                'features' => ['captcha']
            ];
        }

        if (defined('SIMPLE_CLOUDFLARE_TURNSTILE_VERSION')) {
            $conflicts[] = [
                'name' => 'Simple Cloudflare Turnstile',
                'slug' => 'simple-cloudflare-turnstile',
                'features' => ['captcha']
            ];
        }

        // Tier 6: Firewall
        if (defined('BBQ_VERSION')) {
            $conflicts[] = [
                'name' => 'BBQ Firewall',
                'slug' => 'block-bad-queries',
                'features' => ['firewall']
            ];
        }

        // Tier 7: Anti-Spam
        if (class_exists('Akismet')) {
            $conflicts[] = [
                'name' => 'Akismet Anti-Spam',
                'slug' => 'akismet',
                'features' => ['anti_spam']
            ];
        }

        if (class_exists('Antispam_Bee')) {
            $conflicts[] = [
                'name' => 'Antispam Bee',
                'slug' => 'antispam-bee',
                'features' => ['anti_spam']
            ];
        }

        if (defined('APBCT_VERSION')) {
            $conflicts[] = [
                'name' => 'CleanTalk Anti-Spam',
                'slug' => 'cleantalk-spam-protect',
                'features' => ['anti_spam']
            ];
        }

        // Tier 8: Hide Login
        if (defined('WPS_HIDE_LOGIN_VERSION')) {
            $conflicts[] = [
                'name' => 'WPS Hide Login',
                'slug' => 'wps-hide-login',
                'features' => ['hide_login']
            ];
        }

        if (defined('WPH_VERSION')) {
            $conflicts[] = [
                'name' => 'WP Hide & Security Enhancer',
                'slug' => 'wp-hide-security-enhancer',
                'features' => ['hide_login', 'hardening']
            ];
        }

        if (defined('HMW_VERSION')) {
            $conflicts[] = [
                'name' => 'Hide My WP',
                'slug' => 'hide-my-wp',
                'features' => ['hide_login', 'hardening']
            ];
        }

        // Tier 9: Malware Scanners
        if (defined('GOTMLS_VERSION')) {
            $conflicts[] = [
                'name' => 'Anti-Malware Security',
                'slug' => 'gotmls',
                'features' => ['malware_scan', 'brute_force']
            ];
        }

        if (class_exists('QutteraScanPlugin')) {
            $conflicts[] = [
                'name' => 'Quttera Web Malware Scanner',
                'slug' => 'quttera-web-malware-scanner',
                'features' => ['malware_scan']
            ];
        }

        // Tier 10: Hosting-Specific
        if (defined('SG_SECURITY_VERSION')) {
            $conflicts[] = [
                'name' => 'SiteGround Security',
                'slug' => 'sg-security',
                'features' => ['login_limit', '2fa', 'activity_log']
            ];
        }

        // Tier 11: Activity Logs
        if (class_exists('WpSecurityAuditLog')) {
            $conflicts[] = [
                'name' => 'WP Activity Log',
                'slug' => 'wp-security-audit-log',
                'features' => ['activity_log', 'session_management']
            ];
        }

        // Tier 12: Regional/Niche
        if (defined('SITEGUARD_VERSION')) {
            $conflicts[] = [
                'name' => 'SiteGuard WP Plugin',
                'slug' => 'siteguard',
                'features' => ['login_protection']
            ];
        }

        if (defined('XO_SECURITY_VERSION')) {
            $conflicts[] = [
                'name' => 'XO Security',
                'slug' => 'xo-security',
                'features' => ['login_protection', 'xml_rpc', 'rest_api']
            ];
        }

        return $conflicts;
    }

    /**
     * Check if Jetpack has security features enabled
     */
    private static function jetpack_has_security(): bool
    {
        if (!class_exists('Jetpack')) {
            return false;
        }
        return \Jetpack::is_module_active('protect') ||
            \Jetpack::is_module_active('monitor');
    }

    /**
     * Check if any security plugin is active
     */
    public static function has_conflicting_plugin(): bool
    {
        return !empty(self::get_conflicting_plugins());
    }

    /**
     * Get conflicting features for a specific plugin
     */
    public static function get_feature_conflicts(array $our_features): array
    {
        $conflicts = self::get_conflicting_plugins();
        $feature_conflicts = [];

        foreach ($conflicts as $plugin) {
            $overlapping = array_intersect($our_features, $plugin['features']);
            if (!empty($overlapping)) {
                $feature_conflicts[] = [
                    'plugin' => $plugin['name'],
                    'features' => $overlapping
                ];
            }
        }

        return $feature_conflicts;
    }

    public static function is_sucuri_active(): bool
    {
        return defined('SUCURISCAN_VERSION');
    }

    public static function is_ithemes_security_active(): bool
    {
        return class_exists('ITSEC_Core');
    }

    public static function is_all_in_one_security_active(): bool
    {
        return class_exists('AIO_WP_Security');
    }

    public static function is_jetpack_active(): bool
    {
        return class_exists('Jetpack');
    }

    public static function get_conflicting_security_plugins(): array
    {
        $conflicts = [];
        if (self::is_wordfence_active()) {
            $conflicts[] = ['name' => 'Wordfence', 'features' => ['firewall', '2fa', 'captcha', 'login_limit']];
        }
        if (self::is_sucuri_active()) {
            $conflicts[] = ['name' => 'Sucuri', 'features' => ['firewall', 'malware_scan']];
        }
        if (self::is_ithemes_security_active()) {
            $conflicts[] = ['name' => 'iThemes Security', 'features' => ['2fa', 'login_limit', 'file_permissions']];
        }
        if (self::is_all_in_one_security_active()) {
            $conflicts[] = ['name' => 'All In One WP Security', 'features' => ['firewall', 'login_limit', 'captcha']];
        }
        return $conflicts;
    }

    // =========================================================================
    // PAGE BUILDER PLUGINS
    // =========================================================================

    public static function is_elementor_active(): bool
    {
        return defined('ELEMENTOR_VERSION');
    }

    public static function is_elementor_editor(): bool
    {
        if (!self::is_elementor_active()) {
            return false;
        }
        return isset($_GET['elementor']) ||
            isset($_GET['elementor-preview']) ||
            (isset($_GET['action']) && $_GET['action'] === 'elementor');
    }

    public static function get_elementor_ajax_whitelist(): array
    {
        return [
            'elementor_ajax',
            'elementor_library_data',
            'elementor_get_template',
            'elementor_save_builder',
        ];
    }

    public static function is_divi_active(): bool
    {
        return defined('ET_BUILDER_VERSION') || function_exists('et_setup_theme');
    }

    public static function is_divi_editor(): bool
    {
        if (!self::is_divi_active()) {
            return false;
        }
        return isset($_GET['et_fb']) ||
            isset($_GET['et_pb_preview']) ||
            (isset($_POST['action']) && strpos($_POST['action'], 'et_') === 0);
    }

    public static function get_divi_ajax_whitelist(): array
    {
        return [
            'et_fb_ajax_save',
            'et_fb_get_saved_templates',
            'et_fb_retrieve_builder_data',
            'et_pb_execute_content_shortcodes',
            'et_pb_ab_builder_data',
        ];
    }

    public static function is_avada_active(): bool
    {
        return defined('AVADA_VERSION') || class_exists('Avada');
    }

    public static function is_avada_editor(): bool
    {
        if (!self::is_avada_active()) {
            return false;
        }
        return isset($_GET['fb-edit']) || isset($_GET['builder']);
    }

    public static function get_avada_ajax_whitelist(): array
    {
        return [
            'fusion_builder_save_layout',
            'fusion_builder_load_templates',
            'fusion_builder_clone_element',
            'fusion_app_get_data',
        ];
    }

    public static function is_wpbakery_active(): bool
    {
        return defined('WPB_VC_VERSION');
    }

    public static function is_wpbakery_editor(): bool
    {
        if (!self::is_wpbakery_active()) {
            return false;
        }
        return isset($_GET['vc_editable']) || isset($_GET['vc_action']);
    }

    public static function get_wpbakery_ajax_whitelist(): array
    {
        return [
            'vc_edit_form',
            'vc_save',
            'vc_load_shortcode',
            'vc_get_vc_grid_data',
        ];
    }

    public static function is_beaver_builder_active(): bool
    {
        return class_exists('FLBuilderLoader') || defined('FL_BUILDER_VERSION');
    }

    public static function is_beaver_builder_editor(): bool
    {
        if (!self::is_beaver_builder_active()) {
            return false;
        }
        return isset($_GET['fl_builder']);
    }

    public static function get_beaver_builder_ajax_whitelist(): array
    {
        return [
            'fl_builder_save_layout',
            'fl_builder_save_settings',
            'fl_builder_render_module_settings',
        ];
    }

    public static function is_brizy_active(): bool
    {
        return defined('BRIZY_VERSION');
    }

    public static function is_brizy_editor(): bool
    {
        if (!self::is_brizy_active()) {
            return false;
        }
        return isset($_GET['brizy-edit']) || isset($_GET['brizy-edit-iframe']);
    }

    public static function is_oxygen_active(): bool
    {
        return defined('CT_VERSION');
    }

    public static function is_oxygen_editor(): bool
    {
        if (!self::is_oxygen_active()) {
            return false;
        }
        return isset($_GET['ct_builder']) || isset($_GET['oxygen_iframe']);
    }

    public static function is_bricks_active(): bool
    {
        return defined('BRICKS_VERSION');
    }

    public static function is_bricks_editor(): bool
    {
        if (!self::is_bricks_active()) {
            return false;
        }
        return isset($_GET['bricks']) && $_GET['bricks'] === 'run';
    }

    public static function is_any_page_builder_editor(): bool
    {
        return self::is_elementor_editor() ||
            self::is_divi_editor() ||
            self::is_avada_editor() ||
            self::is_wpbakery_editor() ||
            self::is_beaver_builder_editor() ||
            self::is_brizy_editor() ||
            self::is_oxygen_editor() ||
            self::is_bricks_editor();
    }

    // =========================================================================
    // FORM PLUGINS
    // =========================================================================

    public static function is_cf7_active(): bool
    {
        return defined('WPCF7_VERSION');
    }

    public static function cf7_has_recaptcha(): bool
    {
        if (!self::is_cf7_active()) {
            return false;
        }
        return class_exists('WPCF7_RECAPTCHA');
    }

    public static function get_cf7_ajax_whitelist(): array
    {
        return [
            'wpcf7_submit',
            'wpcf7_validate',
        ];
    }

    public static function is_wpforms_active(): bool
    {
        return defined('WPFORMS_VERSION');
    }

    public static function get_wpforms_ajax_whitelist(): array
    {
        return [
            'wpforms_submit',
            'wpforms_file_upload',
            'wpforms_file_chunks_upload',
        ];
    }

    public static function is_gravity_forms_active(): bool
    {
        return class_exists('GFForms');
    }

    public static function get_gravity_forms_ajax_whitelist(): array
    {
        return [
            'gf_do_ajax',
            'gform_submit',
            'gform_resend_notifications',
        ];
    }

    public static function is_formidable_active(): bool
    {
        return class_exists('FrmAppHelper');
    }

    public static function is_ninja_forms_active(): bool
    {
        return class_exists('Ninja_Forms');
    }

    public static function is_fluent_forms_active(): bool
    {
        return defined('FLUENTFORM_VERSION');
    }

    public static function get_fluent_forms_ajax_whitelist(): array
    {
        return [
            'fluentform_submit',
            'fluentform_file_upload',
        ];
    }

    // =========================================================================
    // CACHING PLUGINS
    // =========================================================================

    public static function is_litespeed_active(): bool
    {
        return defined('LSCWP_V');
    }

    public static function is_wp_super_cache_active(): bool
    {
        return function_exists('wp_cache_clear_cache');
    }

    public static function is_w3_total_cache_active(): bool
    {
        return function_exists('w3tc_flush_all');
    }

    public static function is_wp_rocket_active(): bool
    {
        return defined('WP_ROCKET_VERSION');
    }

    public static function is_wp_fastest_cache_active(): bool
    {
        return class_exists('WpFastestCache');
    }

    public static function purge_all_caches(): void
    {
        // LiteSpeed Cache
        if (class_exists('LiteSpeed_Cache_API')) {
            \LiteSpeed_Cache_API::purge_all();
        }

        // WP Super Cache
        if (function_exists('wp_cache_clear_cache')) {
            wp_cache_clear_cache();
        }

        // W3 Total Cache
        if (function_exists('w3tc_flush_all')) {
            w3tc_flush_all();
        }

        // WP Rocket
        if (function_exists('rocket_clean_domain')) {
            rocket_clean_domain();
        }

        // WP Fastest Cache
        if (class_exists('WpFastestCache')) {
            $wpfc = new \WpFastestCache();
            $wpfc->deleteCache();
        }
    }

    public static function set_no_cache_headers(): void
    {
        if (self::is_litespeed_active()) {
            header('X-LiteSpeed-Cache-Control: no-cache');
        }
        if (!defined('DONOTCACHEPAGE')) {
            define('DONOTCACHEPAGE', true);
        }
        nocache_headers();
    }

    // =========================================================================
    // SEO PLUGINS
    // =========================================================================

    public static function is_yoast_active(): bool
    {
        return defined('WPSEO_VERSION');
    }

    public static function is_rank_math_active(): bool
    {
        return class_exists('RankMath');
    }

    public static function is_aioseo_active(): bool
    {
        return defined('AIOSEO_VERSION');
    }

    // =========================================================================
    // MEMBERSHIP PLUGINS
    // =========================================================================

    public static function is_memberpress_active(): bool
    {
        return defined('MEPR_VERSION');
    }

    public static function get_memberpress_ajax_whitelist(): array
    {
        return [
            'mepr_stripe_create_checkout_session',
            'mepr_update_billing_info',
            'mepr_login_submit',
        ];
    }

    public static function is_restrict_content_pro_active(): bool
    {
        return class_exists('Restrict_Content_Pro');
    }

    public static function is_paid_memberships_pro_active(): bool
    {
        return defined('PMPRO_VERSION');
    }

    public static function is_wishlist_member_active(): bool
    {
        return defined('WLM3_VERSION');
    }

    // =========================================================================
    // LMS PLUGINS
    // =========================================================================

    public static function is_learndash_active(): bool
    {
        return defined('LEARNDASH_VERSION');
    }

    public static function is_tutor_lms_active(): bool
    {
        return defined('TUTOR_VERSION');
    }

    public static function is_lifter_lms_active(): bool
    {
        return class_exists('LifterLMS');
    }

    // =========================================================================
    // BOOKING PLUGINS
    // =========================================================================

    public static function is_amelia_active(): bool
    {
        return defined('AMELIA_VERSION');
    }

    public static function get_amelia_ajax_whitelist(): array
    {
        return [
            'wpamelia_api',
        ];
    }

    public static function is_bookly_active(): bool
    {
        return class_exists('Bookly_Lib_Plugin');
    }

    public static function is_simply_schedule_appointments_active(): bool
    {
        return class_exists('Simply_Schedule_Appointments');
    }

    // =========================================================================
    // THEME DETECTION
    // =========================================================================

    public static function get_current_theme(): string
    {
        $theme = wp_get_theme();
        return $theme->get_template();
    }

    public static function is_theme_active(string $theme_slug): bool
    {
        return self::get_current_theme() === $theme_slug;
    }

    public static function is_divi_theme_active(): bool
    {
        return self::is_theme_active('Divi') || self::is_divi_active();
    }

    public static function is_avada_theme_active(): bool
    {
        return self::is_theme_active('Avada') || self::is_avada_active();
    }

    public static function is_astra_theme_active(): bool
    {
        return self::is_theme_active('astra');
    }

    public static function is_generatepress_theme_active(): bool
    {
        return self::is_theme_active('generatepress');
    }

    public static function is_flavor_theme_active(): bool
    {
        $flavor_themes = ['flavor', 'flavor-starter', 'flavor-developer'];
        return in_array(self::get_current_theme(), $flavor_themes, true);
    }

    public static function is_flavor_developer_theme_active(): bool
    {
        return self::is_theme_active('flavor-developer');
    }

    public static function is_flavor_starter_theme_active(): bool
    {
        return self::is_theme_active('flavor-starter');
    }

    public static function is_flavor_developer_editor(): bool
    {
        if (!self::is_flavor_developer_theme_active()) {
            return false;
        }
        return isset($_GET['flavor-edit']) || isset($_GET['fd-builder']);
    }

    public static function is_flavor_starter_editor(): bool
    {
        if (!self::is_flavor_starter_theme_active()) {
            return false;
        }
        return isset($_GET['flavor-edit']) || isset($_GET['fs-builder']);
    }

    public static function is_flavor_editor(): bool
    {
        return self::is_flavor_developer_editor() || self::is_flavor_starter_editor();
    }

    public static function get_flavor_ajax_whitelist(): array
    {
        return [
            'flavor_ajax_save',
            'flavor_load_template',
            'fd_builder_save',
            'fd_get_data',
            'fs_builder_save',
            'fs_get_data',
        ];
    }

    public static function is_flavor_ajax(): bool
    {
        if (!self::is_flavor_theme_active()) {
            return false;
        }
        if (!isset($_REQUEST['action'])) {
            return false;
        }
        $action = sanitize_text_field(wp_unslash($_REQUEST['action']));
        return in_array($action, self::get_flavor_ajax_whitelist(), true);
    }

    public static function is_flavor_request(): bool
    {
        return self::is_flavor_editor() || self::is_flavor_ajax();
    }

    public static function is_flavor_studio_active(): bool
    {
        return defined('FLAVOR_STUDIO_VERSION') || class_exists('FlavorStudio');
    }

    public static function get_flavor_studio_ajax_whitelist(): array
    {
        return [
            'flavor_studio_save',
            'flavor_studio_load',
            'flavor_studio_export',
            'flavor_studio_import',
        ];
    }

    public static function is_flavor_studio_editor(): bool
    {
        if (!self::is_flavor_studio_active()) {
            return false;
        }
        return isset($_GET['flavor-studio']) || isset($_GET['fs-editor']);
    }

    public static function is_flavor_studio_ajax(): bool
    {
        if (!self::is_flavor_studio_active()) {
            return false;
        }
        if (!isset($_REQUEST['action'])) {
            return false;
        }
        $action = sanitize_text_field(wp_unslash($_REQUEST['action']));
        return in_array($action, self::get_flavor_studio_ajax_whitelist(), true);
    }

    public static function is_flavor_studio_request(): bool
    {
        return self::is_flavor_studio_editor() || self::is_flavor_studio_ajax();
    }

    // =========================================================================
    // COMBINED WHITELISTS
    // =========================================================================

    public static function get_all_ajax_whitelist(): array
    {
        $whitelist = [];

        if (self::is_woocommerce_active()) {
            $whitelist = array_merge($whitelist, self::get_woocommerce_ajax_whitelist());
        }
        if (self::is_elementor_active()) {
            $whitelist = array_merge($whitelist, self::get_elementor_ajax_whitelist());
        }
        if (self::is_divi_active()) {
            $whitelist = array_merge($whitelist, self::get_divi_ajax_whitelist());
        }
        if (self::is_avada_active()) {
            $whitelist = array_merge($whitelist, self::get_avada_ajax_whitelist());
        }
        if (self::is_wpbakery_active()) {
            $whitelist = array_merge($whitelist, self::get_wpbakery_ajax_whitelist());
        }
        if (self::is_beaver_builder_active()) {
            $whitelist = array_merge($whitelist, self::get_beaver_builder_ajax_whitelist());
        }
        if (self::is_cf7_active()) {
            $whitelist = array_merge($whitelist, self::get_cf7_ajax_whitelist());
        }
        if (self::is_wpforms_active()) {
            $whitelist = array_merge($whitelist, self::get_wpforms_ajax_whitelist());
        }
        if (self::is_gravity_forms_active()) {
            $whitelist = array_merge($whitelist, self::get_gravity_forms_ajax_whitelist());
        }
        if (self::is_fluent_forms_active()) {
            $whitelist = array_merge($whitelist, self::get_fluent_forms_ajax_whitelist());
        }
        if (self::is_memberpress_active()) {
            $whitelist = array_merge($whitelist, self::get_memberpress_ajax_whitelist());
        }
        if (self::is_amelia_active()) {
            $whitelist = array_merge($whitelist, self::get_amelia_ajax_whitelist());
        }
        if (self::is_flavor_theme_active()) {
            $whitelist = array_merge($whitelist, self::get_flavor_ajax_whitelist());
        }
        if (self::is_flavor_studio_active()) {
            $whitelist = array_merge($whitelist, self::get_flavor_studio_ajax_whitelist());
        }

        return array_unique($whitelist);
    }

    public static function is_whitelisted_ajax_action(): bool
    {
        if (!isset($_REQUEST['action'])) {
            return false;
        }

        $action = sanitize_text_field(wp_unslash($_REQUEST['action']));
        return in_array($action, self::get_all_ajax_whitelist(), true);
    }
}