'The "Combine JavaScript files" option is not compatible and must be deactivated.', //'manual_preload' => 'The "Activate Preloading" option will automatically cache your pages between flushes. Please be aware tha you must flush caches to see changes on your front-end.', //'defer_all_js' => 'The "Defer All JavaScript files" option creates processing delays and must be deactivated.', //'delay_js' => 'The "Delay JavaScript" option creates significant overlay delays. The overlay will take an extra second or two to load.', ]; /** * Supported cache plugins and their defining constants. * * @var array */ private $cache_plugins = [ 'wp_rocket' => [ 'constant' => 'WP_ROCKET_VERSION', 'file' => 'wp-rocket/wp-rocket.php', 'flush_function' => 'rocket_clean_domain', 'name' => 'WP Rocket', ], 'w3_total_cache' => [ 'constant' => 'W3TC', 'file' => 'w3-total-cache/w3-total-cache.php', 'flush_function' => 'w3tc_flush_all', 'name' => 'W3 Total Cache', ], 'wp_super_cache' => [ 'constant' => 'WPCACHEHOME', 'file' => 'wp-super-cache/wp-super-cache.php', 'flush_function' => 'wp_cache_clear_cache', 'name' => 'WP Super Cache', ], 'litespeed_cache' => [ 'constant' => 'LITESPEED_CACHE_VERSION', 'file' => 'litespeed-cache/litespeed-cache.php', 'flush_function' => ['LiteSpeed\Purge', 'purge_all'], 'name' => 'LiteSpeed Cache', ], 'wp_fastest_cache' => [ 'constant' => 'WPFC_MAIN_PATH', 'file' => 'wp-fastest-cache/wpFastestCache.php', // Updated path 'flush_function' => 'wpfc_clear_all_cache', 'name' => 'WP Fastest Cache', ], 'wp_optimize' => [ 'constant' => 'WP_OPTIMIZE_VERSION', 'file' => 'wp-optimize/wp-optimize.php', 'flush_function' => [__CLASS__, 'flush_wp_optimize_cache'], 'name' => 'WP-Optimize', ], 'speedycache' => [ 'constant' => 'SPEEDYCACHE_VERSION', 'file' => 'speedycache/speedycache.php', 'flush_function' => ['SpeedyCache\Delete', 'all_cache'], 'name' => 'SpeedyCache', ], 'cache_enabler' => [ 'constant' => 'CACHE_ENABLER_VERSION', 'file' => 'cache-enabler/cache-enabler.php', 'flush_function' => ['Cache_Enabler', 'clear_complete_cache'], 'name' => 'Cache Enabler', ], 'breeze' => [ 'constant' => 'BREEZE_VERSION', 'file' => 'breeze/breeze.php', 'flush_function' => ['Breeze_PurgeCache', 'breeze_cache_flush'], 'name' => 'Breeze', ], 'sg_cachepress' => [ 'constant' => 'SiteGround_Optimizer\VERSION', 'file' => 'sg-cachepress/sg-cachepress.php', 'flush_function' => ['SiteGround_Optimizer\Supercacher\Supercacher', 'purge_cache'], 'name' => 'Speed Optimizer', ], ]; /** * Flush WP-Optimize cache. * * @return void */ public static function flush_wp_optimize_cache() { if (class_exists('WP_Optimize') && method_exists('WP_Optimize', 'get_page_cache')) { $wp_optimize = WP_Optimize::instance(); if ($wp_optimize && method_exists($wp_optimize, 'get_page_cache')) { $wp_optimize->get_page_cache()->purge(); PluginLogger::log('debug', 'WP-Optimize cache flushed successfully.'); } else { PluginLogger::log('warning', 'Failed to flush WP-Optimize cache: get_page_cache method not found.'); } } else { PluginLogger::log('warning', 'Failed to flush WP-Optimize cache: WP_Optimize class or method not found.'); } } /** * Compatability constructor. */ public function __construct() { add_action('admin_init', [$this, 'check_compatibility']); add_action('carbon_fields_theme_options_container_saved', [$this, 'notify_admin_to_flush_cache']); add_action('admin_init', [$this, 'handle_cache_flush_request']); add_action('admin_post_disable_wp_rocket_conflicts', [$this, 'disable_wp_rocket_conflicts']); add_action('admin_get_disable_wp_rocket_conflicts', [$this, 'disable_wp_rocket_conflicts']); add_action('admin_post_disable_breeze_delay_js', [$this, 'disable_breeze_delay_js']); } /** * Check compatibility with installed caching plugins. * * @return void */ public function check_compatibility() { if (!function_exists('is_plugin_active')) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $this->check_wp_rocket_compatibility(); $this->check_breeze_compatibility(); } /** * Check Breeze for conflicting delay JS setting. * * @return void */ private function check_breeze_compatibility() { if ( !defined('BREEZE_VERSION') || !is_plugin_active('breeze/breeze.php') || !class_exists('\Breeze_Options_Reader') ) { return; } $delay_js_enabled = \Breeze_Options_Reader::get_option_value('breeze-delay-all-js'); if (!$delay_js_enabled) { return; } $message = '

AdBlock Guard has detected an incompatible Breeze setting:

' . ''; $notices = Notices::instance(); if ($notices) { $notices->add_notice( 'breeze_delay_js_incompatibility', $message, 'error', false, false, [ 'type' => 'button', 'label' => 'Disable Delay JS and Flush Cache', 'url' => esc_url_raw(add_query_arg( [ 'action' => 'disable_breeze_delay_js', '_wpnonce' => wp_create_nonce('disable_breeze_delay_js'), 'redirect_to' => urlencode(wp_get_referer() ?: admin_url()), ], admin_url('admin-post.php') )), ] ); } else { PluginLogger::log('warning', 'Failed to add Breeze delay JS incompatibility notice: Notices instance not available.'); } } /** * Disable Breeze delay JS setting and flush cache. * * @return void */ public function disable_breeze_delay_js() { $nonce = isset($_GET['_wpnonce']) ? sanitize_text_field(wp_unslash($_GET['_wpnonce'])) : ''; if ( $nonce === '' || !wp_verify_nonce($nonce, 'disable_breeze_delay_js') || !current_user_can('manage_options') || !defined('BREEZE_VERSION') ) { PluginLogger::log('warning', 'Unauthorized attempt to disable Breeze delay JS.'); wp_safe_redirect(wp_get_referer() ?: admin_url()); exit; } // Update breeze_advanced_settings $advanced_settings = get_option('breeze_advanced_settings', []); if (!is_array($advanced_settings)) { $advanced_settings = []; } $advanced_settings['breeze-delay-all-js'] = 0; if (!update_option('breeze_advanced_settings', $advanced_settings, false)) { PluginLogger::log('warning', 'Failed to update breeze_advanced_settings option.'); } // Update breeze_file_settings $file_settings = get_option('breeze_file_settings', []); if (!is_array($file_settings)) { $file_settings = []; } $file_settings['breeze-delay-all-js'] = 0; if (!update_option('breeze_file_settings', $file_settings, false)) { PluginLogger::log('warning', 'Failed to update breeze_file_settings option.'); } // Clear Redis/WordPress object cache wp_cache_delete('breeze_advanced_settings', 'options'); wp_cache_delete('breeze_file_settings', 'options'); wp_cache_delete('alloptions', 'options'); // Flush Breeze cache using the same method as handle_cache_flush_request $flush_function = ['Breeze_PurgeCache', 'breeze_cache_flush']; if (is_callable($flush_function)) { call_user_func($flush_function, true, true, true); PluginLogger::log('debug', 'Breeze cache flushed successfully.'); } else { PluginLogger::log('warning', 'Breeze cache flush failed: Breeze_PurgeCache::breeze_cache_flush is not callable.'); } // Clear the incompatibility notice $notices = Notices::instance(); if ($notices) { $notices->clear_notice('breeze_delay_js_incompatibility'); } wp_safe_redirect(wp_get_referer() ?: admin_url()); exit; } /** * Check WP Rocket for conflicting settings. * * @return void */ private function check_wp_rocket_compatibility() { if ( !defined('WP_ROCKET_VERSION') || !is_plugin_active('wp-rocket/wp-rocket.php') || !($rocket_settings = get_option('wp_rocket_settings', [])) ) { return; } $conflicts = array_filter( $this->wp_rocket_conflicting_settings, fn($key) => !empty($rocket_settings[$key]), ARRAY_FILTER_USE_KEY ); if (empty($conflicts)) { return; } $message = '

AdBlock Guard has detected issues with WP Rocket settings:

'; $notices = Notices::instance(); if ($notices) { $notices->add_notice( 'wp_rocket_incompatibility', $message, 'error', false, true, [ 'type' => 'button', 'label' => 'Disable conflicting settings', 'url' => esc_url_raw(add_query_arg( [ 'action' => 'disable_wp_rocket_conflicts', '_wpnonce' => wp_create_nonce('disable_wp_rocket_conflicts'), 'redirect_to' => urlencode(wp_get_referer() ?: admin_url()), ], admin_url('admin-post.php') )), ] ); } else { PluginLogger::log('warning', 'Failed to add WP Rocket incompatibility notice: Notices instance not available.'); } } /** * Disable conflicting WP Rocket settings. * * @return void */ public function disable_wp_rocket_conflicts() { $nonce = isset($_GET['_wpnonce']) ? sanitize_text_field(wp_unslash($_GET['_wpnonce'])) : ''; if ( $nonce === '' || !wp_verify_nonce($nonce, 'disable_wp_rocket_conflicts') || !current_user_can('manage_options') || !defined('WP_ROCKET_VERSION') ) { wp_safe_redirect(wp_get_referer() ?: admin_url()); exit; } $rocket_settings = get_option('wp_rocket_settings', []); foreach (array_keys($this->wp_rocket_conflicting_settings) as $key) { if (function_exists('update_rocket_option')) { update_rocket_option($key, 0); } else { $rocket_settings[$key] = 0; } } if (!function_exists('update_rocket_option')) { update_option('wp_rocket_settings', $rocket_settings); } if (function_exists('rocket_clean_domain')) { rocket_clean_domain(); PluginLogger::log('debug', 'WP Rocket cache flushed successfully.'); } $notices = Notices::instance(); if ($notices) { $notices->clear_notice('wp_rocket_incompatibility'); } wp_safe_redirect(wp_get_referer() ?: admin_url()); exit; } /** * Notify admin to flush cache for active caching plugins. * * @return void */ public function notify_admin_to_flush_cache() { $is_enabled = get_option('_wuadblockguard_enable') === 'yes'; // Exit early if form wasn't submitted or _wuadblockguard_enable is not "on" if (!$is_enabled) { PluginLogger::log('debug', 'AdBlock Guard not enabled in form submission, skipping cache flush notices.', [ '_wuadblockguard_enable' => (string) get_option('_wuadblockguard_enable', 'not set'), ]); return; } if (!function_exists('is_plugin_active')) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $notices = Notices::instance(); if (!$notices) { PluginLogger::log('warning', 'Failed to add cache flush notices: Notices instance not available.'); return; } foreach ($this->cache_plugins as $plugin_key => $plugin) { // Check if plugin is active $is_active = is_plugin_active($plugin['file']) || class_exists(str_replace(' ', '', $plugin['name'])); // Special check for WP Fastest Cache if ($plugin_key === 'wp_fastest_cache' && $is_active) { $wpfc_settings_raw = get_option('WpFastestCache', false); $wpfc_settings = is_array($wpfc_settings_raw) ? $wpfc_settings_raw : (is_string($wpfc_settings_raw) ? json_decode($wpfc_settings_raw, true) : []); if (empty($wpfc_settings['wpFastestCacheStatus']) || $wpfc_settings['wpFastestCacheStatus'] !== 'on') { PluginLogger::log('debug', 'WP Fastest Cache caching is disabled, skipping notice.', [ 'plugin_key' => $plugin_key, 'wpFastestCacheStatus' => isset($wpfc_settings['wpFastestCacheStatus']) ? $wpfc_settings['wpFastestCacheStatus'] : 'not set', ]); continue; } } // Special check for SpeedyCache if ($plugin_key === 'speedycache' && $is_active) { $speedycache_options = get_option('speedycache_options', []); if (empty($speedycache_options['status'])) { PluginLogger::log('debug', 'SpeedyCache caching is disabled, skipping notice.', [ 'plugin_key' => $plugin_key, 'speedycache_status' => isset($speedycache_options['status']) ? $speedycache_options['status'] : 'not set', ]); continue; } } // Special check for LiteSpeed Cache if ($plugin_key === 'litespeed_cache' && $is_active) { $litespeed_options = get_option('litespeed.conf', []); $is_cache_enabled = false; if (!empty($litespeed_options['cache'])) { if ($litespeed_options['cache'] == 1) { $is_cache_enabled = true; } elseif (is_multisite() && $litespeed_options['cache'] == 2) { $primary_options = get_blog_option(BLOG_ID_CURRENT_SITE, 'litespeed.conf', []); $is_cache_enabled = !empty($primary_options['cache']) && $primary_options['cache'] == 1; } } if (!$is_cache_enabled || !defined('LITESPEED_ALLOWED') || !LITESPEED_ALLOWED) { PluginLogger::log('debug', 'LiteSpeed Cache caching is disabled or unsupported, skipping notice.', [ 'plugin_key' => $plugin_key, 'cache_enabled' => isset($litespeed_options['cache']) ? $litespeed_options['cache'] : 'not set', 'litespeed_allowed' => defined('LITESPEED_ALLOWED') ? LITESPEED_ALLOWED : 'not set', ]); continue; } } // Special check for Cache Enabler if ($plugin_key === 'cache_enabler' && $is_active) { $cache_enabler_settings = get_option('cache_enabler', []); $is_cache_enabled = !empty($cache_enabler_settings['version']) && $cache_enabler_settings['version'] === CACHE_ENABLER_VERSION && file_exists(WP_CONTENT_DIR . '/advanced-cache.php') && defined('WP_CACHE') && WP_CACHE; if (!$is_cache_enabled) { PluginLogger::log('debug', 'Cache Enabler caching is disabled or not properly configured, skipping notice.', [ 'plugin_key' => $plugin_key, 'cache_enabler_version' => isset($cache_enabler_settings['version']) ? $cache_enabler_settings['version'] : 'not set', 'wp_cache_defined' => defined('WP_CACHE') ? WP_CACHE : 'not set', 'advanced_cache_exists' => file_exists(WP_CONTENT_DIR . '/advanced-cache.php') ? 'yes' : 'no', ]); continue; } } // Special check for Breeze if ($plugin_key === 'breeze' && $is_active) { $breeze_enabled = class_exists('\Breeze_Options_Reader') ? \Breeze_Options_Reader::get_option_value('breeze-active') : false; if (empty($breeze_enabled)) { PluginLogger::log('debug', 'Breeze caching is disabled, skipping notice.', [ 'plugin_key' => $plugin_key, 'breeze_active' => $breeze_enabled ?: 'not set', ]); continue; } } // Special check for Speed Optimizer (SG CachePress) if ($plugin_key === 'sg_cachepress' && $is_active) { // Clear Redis cache for options to avoid stale data wp_cache_delete('siteground_optimizer_enable_cache', 'options'); wp_cache_delete('siteground_optimizer_file_caching', 'options'); wp_cache_delete('siteground_optimizer_enable_memcached', 'options'); wp_cache_delete('alloptions', 'options'); $enable_cache = get_option('siteground_optimizer_enable_cache', 0); $file_caching = get_option('siteground_optimizer_file_caching', 0); $enable_memcached = get_option('siteground_optimizer_enable_memcached', 0); $caching_enabled = !empty($enable_cache) || !empty($file_caching) || !empty($enable_memcached); if (!$caching_enabled) { PluginLogger::log('debug', 'Speed Optimizer caching is disabled, skipping notice.', [ 'plugin_key' => $plugin_key, 'enable_cache' => $enable_cache, 'file_caching' => $file_caching, 'enable_memcached' => $enable_memcached, ]); continue; } } if (!$is_active) { continue; } // Special handling for WP Rocket if ($plugin_key === 'wp_rocket' && function_exists('rocket_renew_box') && function_exists('rocket_warning_plugin_modification')) { rocket_renew_box('rocket_warning_plugin_modification'); continue; } // For WP-Optimize, only show notice if page caching is enabled if ($plugin_key === 'wp_optimize') { if ( !class_exists('WP_Optimize') || !method_exists('WP_Optimize', 'get_page_cache') || !WP_Optimize::instance()->get_page_cache()->is_enabled() ) { continue; } } $notice_id = "{$plugin_key}_flush_notice"; $notices->add_notice( $notice_id, sprintf('Flush the %s cache after completing all customizations to apply changes.', esc_html($plugin['name'])), 'warning', true, // Persistent true, // Not dismissible [ 'label' => sprintf('Flush %s Cache', esc_html($plugin['name'])), 'url' => esc_url_raw(add_query_arg( [ 'action' => "flush_{$plugin_key}_cache", '_wpnonce' => wp_create_nonce("flush_{$plugin_key}_cache_nonce"), 'redirect_to' => urlencode(add_query_arg(null, null)), ], admin_url('admin.php') )), 'type' => 'button', ] ); PluginLogger::log('debug', "Added cache flush notice for {$plugin['name']}", [ 'notice_id' => $notice_id, ]); } } /** * Handle cache flush requests for all supported plugins. * * @return void */ public function handle_cache_flush_request() { foreach ($this->cache_plugins as $plugin_key => $plugin) { $action = "flush_{$plugin_key}_cache"; $nonce = "flush_{$plugin_key}_cache_nonce"; $request_action = isset($_GET['action']) ? sanitize_text_field(wp_unslash($_GET['action'])) : ''; $request_nonce = isset($_GET['_wpnonce']) ? sanitize_text_field(wp_unslash($_GET['_wpnonce'])) : ''; if ( $request_action !== $action || $request_nonce === '' || !wp_verify_nonce($request_nonce, $nonce) || !current_user_can('manage_options') ) { continue; } if (is_callable($plugin['flush_function'])) { if (is_array($plugin['flush_function'])) { if ($plugin_key === 'breeze') { call_user_func($plugin['flush_function'], true, true, true); } else { call_user_func($plugin['flush_function']); } } else { $plugin['flush_function'](); } PluginLogger::log('debug', "Cache flushed successfully for {$plugin['name']}."); } else { PluginLogger::log('warning', "Failed to flush cache for {$plugin['name']}: Flush function not callable."); } $notices = Notices::instance(); if ($notices) { $notices->clear_notice("{$plugin_key}_flush_notice"); } $redirect_url = isset($_GET['redirect_to']) ? esc_url_raw(rawurldecode(sanitize_text_field(wp_unslash($_GET['redirect_to'])))) : admin_url(); wp_safe_redirect($redirect_url); exit; } } }