name). */ const CACHE_PLUGINS = array( 'wp-rocket/wp-rocket.php' => 'WP Rocket', 'wp-super-cache/wp-cache.php' => 'WP Super Cache', 'w3-total-cache/w3-total-cache.php' => 'W3 Total Cache', 'cache-enabler/cache-enabler.php' => 'Cache Enabler', 'surge/surge.php' => 'Surge', 'flying-press/flying-press.php' => 'FlyingPress', 'swift-performance-lite/performance.php' => 'Swift Performance', 'cachify/cachify.php' => 'Cachify', 'lws-optimize/lws-optimize.php' => 'LWS Optimize', 'wpspeed/wpspeed.php' => 'WPSpeed', 'rabbit-loader/rabbit-loader.php' => 'RabbitLoader', 'core-web-vitals-pagespeed-booster/core-web-vitals-pagespeed-booster.php' => 'Core Web Vitals & PageSpeed Booster', ); /** * Plugins that cache pages only while their own page cache is switched on: an idle install leaves caching to us * (main file => name, option, key inside the option or '' for the option itself, with "/" between nested keys, * whether it caches when that setting has never been saved). Their advanced-cache.php, when they write one, is caught by its own check. */ const CACHE_PLUGIN_SETTINGS = array( 'wp-fastest-cache/wpFastestCache.php' => array( 'WP Fastest Cache', 'WpFastestCache', 'wpFastestCacheStatus', false ), 'comet-cache/comet-cache.php' => array( 'Comet Cache', 'comet_cache_options', 'enable', false ), 'powered-cache/powered-cache.php' => array( 'Powered Cache', 'powered_cache_settings', 'enable_page_cache', true ), 'jch-optimize/jch-optimize.php' => array( 'JCH Optimize', 'jch-optimize_settings', 'cache_enable', false ), 'clearfy/clearfy.php' => array( 'Clearfy', 'wbcr_clearfy_enable_cache', '', false ), 'atec-cache-apcu/atec-cache-apcu.php' => array( 'atec Cache APCu', 'atec_WPCA_settings', 'p_cache', false ), 'perfmatters/perfmatters.php' => array( 'Perfmatters', 'perfmatters_options', 'cache/page', false ), ); /** * Text found near the top of each plugin's advanced-cache.php => that plugin's name, so a page cache that is * not ours can be named, even when the plugin that installed it has been deactivated. */ const DROPIN_OWNERS = array( 'WP_ROCKET_ADVANCED_CACHE' => 'WP Rocket', 'WP SUPER CACHE' => 'WP Super Cache', 'W3 Total Cache' => 'W3 Total Cache', 'Cache Enabler' => 'Cache Enabler', 'Comet Cache' => 'Comet Cache', 'Powered Cache' => 'Powered Cache', 'WP-Optimize advanced-cache' => 'WP-Optimize', 'sg-cachepress/core/File_Cacher' => 'SiteGround Speed Optimizer', 'SPEEDYCACHE' => 'SpeedyCache', 'BREEZE_ADVANCED_CACHE' => 'Breeze', 'Boost Cache Plugin' => 'Jetpack Boost', 'NITROPACK_ADVANCED_CACHE' => 'NitroPack', 'SWCFPC_ADVANCED_CACHE' => 'Super Page Cache', 'Hummingbird Advanced Tools module' => 'Hummingbird', 'TENWEB_SO_ADVANCED_CACHE' => '10Web Booster', 'A2 Optimized advanced cache' => 'A2 Optimized', 'seraphinite-accelerator' => 'Seraphinite Accelerator', 'XSPEED_DROPIN' => 'xSpeed', 'ezCache Advanced Cache' => 'ezCache', 'WPS_IC_CACHE' => 'WP Compress', 'SBP_ADVANCED_CACHE' => 'Speed Booster Pack', 'hyper_cache' => 'Hyper Cache', 'PageSpeed Ninja Caching' => 'PageSpeed Ninja', 'FASTPIXEL_ADVANCED_CACHE' => 'FastPixel', 'BerqWP Advanced Cache' => 'BerqWP', 'SolidWP\\Performance' => 'Solid Performance', 'RunCache WordPress plugin' => 'RunCache', 'Falcon\\Components\\Cache' => 'Falcon', 'Easy Optimizer' => 'Easy Optimizer', 'SC_ADVANCED_CACHE' => 'Simple Cache', 'BATCACHE' => 'Batcache', 'atec-apcu-page-cache' => 'atec Cache APCu', 'Surge' => 'Surge', 'Perfmatters Cache Drop-in' => 'Perfmatters', ); /** * Each drop-in owner's plugin folder(s), to tell a drop-in whose plugin is still active from one left behind. */ const DROPIN_PLUGINS = array( 'WP Rocket' => array( 'wp-rocket' ), 'WP Super Cache' => array( 'wp-super-cache' ), 'W3 Total Cache' => array( 'w3-total-cache' ), 'Cache Enabler' => array( 'cache-enabler' ), 'Comet Cache' => array( 'comet-cache', 'comet-cache-pro' ), 'Powered Cache' => array( 'powered-cache' ), 'WP-Optimize' => array( 'wp-optimize', 'wp-optimize-premium' ), 'SiteGround Speed Optimizer' => array( 'sg-cachepress' ), 'SpeedyCache' => array( 'speedycache', 'speedycache-pro' ), 'Breeze' => array( 'breeze' ), 'Jetpack Boost' => array( 'jetpack-boost' ), 'NitroPack' => array( 'nitropack' ), 'Super Page Cache' => array( 'wp-cloudflare-page-cache' ), 'Hummingbird' => array( 'hummingbird-performance', 'wp-hummingbird' ), '10Web Booster' => array( 'tenweb-speed-optimizer' ), 'A2 Optimized' => array( 'a2-optimized-wp' ), 'Seraphinite Accelerator' => array( 'seraphinite-accelerator', 'seraphinite-accelerator-ext' ), 'xSpeed' => array( 'xspeed' ), 'ezCache' => array( 'ezcache' ), 'WP Compress' => array( 'wp-compress-image-optimizer' ), 'Speed Booster Pack' => array( 'speed-booster-pack' ), 'Hyper Cache' => array( 'hyper-cache' ), 'PageSpeed Ninja' => array( 'psn-pagespeed-ninja' ), 'FastPixel' => array( 'fastpixel-website-accelerator' ), 'BerqWP' => array( 'searchpro' ), 'Solid Performance' => array( 'solid-performance' ), 'RunCache' => array( 'runcache' ), 'Falcon' => array( 'falcon' ), 'Easy Optimizer' => array( 'easy-optimizer' ), 'Simple Cache' => array( 'simple-cache' ), 'Batcache' => array( 'batcache' ), 'atec Cache APCu' => array( 'atec-cache-apcu' ), 'Surge' => array( 'surge' ), 'Perfmatters' => array( 'perfmatters' ), ); /** * Drop-ins usually put in place by the host rather than by a plugin: never replaced without the owner's click. */ const DROPIN_HOST_INSTALLED = array( 'Batcache' ); /** * Response headers whose value contains "hit" when a cache in front served the page. */ const HIT_HEADERS = array( 'x-cache', 'x-cache-status', 'x-proxy-cache', 'x-litespeed-cache', 'x-srcache-fetch-status', 'x-kinsta-cache', 'x-qc-cache', 'x-hcdn-cache-status', 'x-sucuri-cache', 'x-ac', 'x-aruba-cache', 'x-perfmatters-cache', ); /** * The full verdict. With $probe, the home page's headers are checked now; otherwise the last check (at * most a day old) is reused. * * @param bool $probe Request the home page now. * @return array{verdict:string, reason:string, name:string, note:string, probe:array} */ public static function check( $probe = false ) { $state = GorillaCache_Installer::state(); $result = self::in_process(); $last = isset( $state['probe'] ) && is_array( $state['probe'] ) ? $state['probe'] : array(); if ( $probe ) { $last = self::probe( (string) $state['token'] ); } elseif ( empty( $last['verdict'] ) || ( 'off' !== $last['verdict'] && (int) $last['at'] < time() - DAY_IN_SECONDS ) ) { // A check that found another cache stays in force until a new check says otherwise; only // "stay on" notes expire. $last = array(); } $result['probe'] = $last; if ( 'on' === $result['verdict'] && 'forced' !== $result['reason'] && ! empty( $last['verdict'] ) ) { if ( 'off' === $last['verdict'] ) { $result['verdict'] = 'off'; $result['reason'] = $last['reason']; $result['name'] = $last['name']; } elseif ( '' === $result['note'] && in_array( $last['reason'], array( 'cloudflare_edge', 'weak_headers', 'probe_failed' ), true ) ) { $result['note'] = $last['reason']; $result['name'] = '' !== $result['name'] ? $result['name'] : $last['name']; } } // A finding that rests on request headers (Varnish adds X-Varnish to the request it passes on) can only be // seen, and so only be overturned, in an administrator's own request: a background check keeps it. $header_finding = 'off' === $state['verdict'] && ( ( 'varnish' === $state['reason'] && 'CloudPanel' !== $state['name'] ) || ( 'host' === $state['reason'] && 'Cloudways' === $state['name'] ) ); if ( $header_finding && 'on' === $result['verdict'] && ! self::headers_trusted() ) { $result = array_merge( $result, self::verdict( 'off', $state['reason'], $state['name'] ) ); } return $result; } /** * Whether this request's headers can be believed. Headers come from whoever sent the request: WP-Cron runs in * any visitor's request, so a stranger could send "X-Varnish" and make the plugin stand down. Only an * administrator's own page load counts; WP-Cron and WP-CLI never do. * * @return bool */ public static function headers_trusted() { if ( wp_doing_cron() || ( defined( 'WP_CLI' ) && WP_CLI ) ) { return false; } return function_exists( 'is_user_logged_in' ) && is_user_logged_in() && current_user_can( 'activate_plugins' ); } /** * Varnish passed this request on (it adds X-Varnish), unless it was told to pass it through uncached. Read only * in a request whose headers can be believed. * * @return bool */ private static function varnish_request_header() { return self::headers_trusted() && '' !== self::server( 'HTTP_X_VARNISH' ) && ! in_array( strtolower( self::server( 'HTTP_X_APPLICATION' ) ), array( 'varnishpass', 'bypass' ), true ); } /** * The signals inside WordPress, the most certain first. * * @return array{verdict:string, reason:string, name:string, note:string} */ public static function in_process() { // The owner's pause comes first: it is the most recent and most deliberate choice. if ( class_exists( 'GorillaCache_Site' ) && GorillaCache_Site::paused() ) { return self::verdict( 'off', 'paused' ); } if ( defined( 'GORILLA_CACHE_FORCE_ENABLE' ) && GORILLA_CACHE_FORCE_ENABLE ) { return self::verdict( 'on', 'forced' ); } $active = (array) get_option( 'active_plugins', array() ); foreach ( self::CACHE_PLUGINS as $file => $name ) { if ( in_array( $file, $active, true ) ) { return self::verdict( 'off', 'cache_plugin', $name ); } } foreach ( self::CACHE_PLUGIN_SETTINGS as $file => $setting ) { if ( in_array( $file, $active, true ) && self::plugin_cache_on( $setting ) ) { return self::verdict( 'off', 'cache_plugin', $setting[0] ); } } if ( in_array( 'sg-cachepress/sg-cachepress.php', $active, true ) && 1 === (int) get_option( 'siteground_optimizer_enable_cache' ) ) { return self::verdict( 'off', 'host', 'SiteGround' ); } $nginx_helper = get_option( 'rt_wp_nginx_helper_options', array() ); if ( in_array( 'nginx-helper/nginx-helper.php', $active, true ) && is_array( $nginx_helper ) && ! empty( $nginx_helper['enable_purge'] ) ) { return self::verdict( 'off', 'nginx' ); } if ( 'foreign' === GorillaCache_Installer::dropin_state() ) { return self::verdict( 'off', 'foreign_dropin', self::dropin_owner() ); } $host = self::managed_host(); if ( '' !== $host ) { return self::verdict( 'off', 'host', $host ); } if ( self::cloudpanel_varnish_enabled() ) { return self::verdict( 'off', 'varnish', 'CloudPanel' ); } if ( self::varnish_request_header() ) { return self::verdict( 'off', 'varnish' ); } $litespeed = 0 === stripos( self::server( 'SERVER_SOFTWARE' ), 'litespeed' ) || 0 === stripos( self::server( 'LSWS_EDITION' ), 'openlitespeed' ); if ( $litespeed && '' !== self::server( 'X-LSCACHE' ) ) { return self::verdict( 'off', 'litespeed' ); } $result = self::verdict( 'on', 'none_found' ); if ( $litespeed ) { $result['note'] = 'litespeed_no_cache'; } elseif ( in_array( 'litespeed-cache/litespeed-cache.php', $active, true ) ) { $result['note'] = 'litespeed_plugin_idle'; } return $result; } /** * A managed host with its own page cache, by the signals its platform sets (constants, server variables and * files that only that host's platform has). * * @return string Host name, or empty. */ public static function managed_host() { if ( '' !== self::server( 'KINSTA_CACHE_ZONE' ) || defined( 'KINSTAMU_VERSION' ) ) { return 'Kinsta'; } if ( class_exists( 'WpeCommon' ) || defined( 'WPE_APIKEY' ) ) { return 'WP Engine'; } if ( defined( 'ATOMIC_SITE_ID' ) && defined( 'ATOMIC_CLIENT_ID' ) ) { return defined( 'IS_PRESSABLE' ) && IS_PRESSABLE ? 'Pressable' : 'WordPress.com'; } if ( defined( 'IS_PRESSABLE' ) && IS_PRESSABLE ) { return 'Pressable'; } if ( defined( 'PANTHEON_ENVIRONMENT' ) || false !== getenv( 'PANTHEON_ENVIRONMENT' ) ) { return 'Pantheon'; } if ( class_exists( '\WPaas\Plugin' ) ) { return 'GoDaddy'; } if ( class_exists( '\Savvii\CacheFlusherPlugin' ) ) { return 'Savvii'; } if ( 0 === strpos( self::server( 'DH_USER' ), 'wp_' ) && 0 === strpos( (string) gethostname(), 'dp-' ) ) { return 'DreamPress'; } if ( (int) get_option( 'newfold_cache_level', 0 ) >= 2 ) { return 'Bluehost'; } if ( defined( 'SPINUPWP_CACHE_PATH' ) || false !== getenv( 'SPINUPWP_CACHE_PATH' ) ) { return 'SpinupWP'; } $cloudways = false !== strpos( self::server( 'DOCUMENT_ROOT' ), 'cloudwaysapps' ) || false !== getenv( 'FPC_ENV' ); if ( $cloudways && self::varnish_request_header() ) { return 'Cloudways'; } return ''; } /** * Whether a cache plugin's own page-cache switch is on. * * @param array $setting Name, option, key ('' for the option itself), and the answer when never saved. * @return bool */ private static function plugin_cache_on( array $setting ) { $value = get_option( $setting[1], null ); if ( is_string( $value ) && '{' === substr( ltrim( $value ), 0, 1 ) ) { $value = json_decode( $value, true ); // WP Fastest Cache keeps its settings as JSON text. } if ( '' !== $setting[2] ) { foreach ( explode( '/', $setting[2] ) as $key ) { if ( ! is_array( $value ) || ! array_key_exists( $key, $value ) ) { return (bool) $setting[3]; } $value = $value[ $key ]; } } elseif ( null === $value ) { return (bool) $setting[3]; } return ! empty( $value ) && 'off' !== $value; } /** * Which plugin installed the advanced-cache.php that is not ours, from the text near its top. * * @return string Plugin name, or '' when it cannot be told. */ public static function dropin_owner() { $file = GorillaCache_Installer::dropin_file(); $head = is_readable( $file ) ? substr( (string) GorillaCache_Installer::filesystem()->get_contents( $file ), 0, 4096 ) : ''; foreach ( self::DROPIN_OWNERS as $text => $name ) { if ( false !== stripos( $head, $text ) ) { return $name; } } return ''; } /** * What the official Cloudflare plugin is set to do, read from its own settings (no network call): whether it is * active, signed in (an API key and an email, in its settings or its wp-config.php constants), and whether APO or * Auto Purge is on. Its purge does nothing unless one of the two is on, and it reports no result. * * @return array{plugin:bool, signed_in:bool, apo:bool, auto_purge:bool} */ public static function cloudflare() { $on = static function ( $option ) { $setting = get_option( $option ); return is_array( $setting ) && isset( $setting['value'] ) && false !== $setting['value'] && 'off' !== $setting['value']; }; $key = defined( 'CLOUDFLARE_API_KEY' ) && '' !== (string) constant( 'CLOUDFLARE_API_KEY' ) ? constant( 'CLOUDFLARE_API_KEY' ) : get_option( 'cloudflare_api_key' ); $email = defined( 'CLOUDFLARE_EMAIL' ) && '' !== (string) constant( 'CLOUDFLARE_EMAIL' ) ? constant( 'CLOUDFLARE_EMAIL' ) : get_option( 'cloudflare_api_email' ); return array( 'plugin' => defined( 'CLOUDFLARE_PLUGIN_DIR' ), 'signed_in' => is_string( $key ) && '' !== $key && is_string( $email ) && '' !== $email, 'apo' => $on( 'automatic_platform_optimization' ), 'auto_purge' => $on( 'plugin_specific_cache' ), ); } /** * Whether Perfmatters' Cloudflare Edge Cache is on. While it is, Perfmatters sends a Cache-Control header that * forbids caching on every page ("no-cache" with its Cloudflare token, "private, no-store" without), and a page * that says so is never stored, so Gorilla Cache stores nothing. * * @return bool */ public static function perfmatters_edge_cache() { $options = get_option( 'perfmatters_options' ); return defined( 'PERFMATTERS_VERSION' ) && is_array( $options ) && ! empty( $options['cache']['cloudflare'] ); } /** * Whether asking the Cloudflare plugin to purge everything makes it purge. * * @return bool */ public static function cloudflare_purges() { $cloudflare = self::cloudflare(); return $cloudflare['plugin'] && $cloudflare['signed_in'] && ( $cloudflare['apo'] || $cloudflare['auto_purge'] ); } /** * Where the plugin that owns a drop-in stands: "active" (a plugin or must-use plugin from its folder runs), * "inactive" (installed, switched off), "deleted" (not installed) or "unknown" (a drop-in we cannot name). * * @param string $name Owner's name, from dropin_owner(). * @return string */ public static function dropin_owner_plugin( $name ) { if ( ! isset( self::DROPIN_PLUGINS[ $name ] ) ) { return 'unknown'; } if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $found = 'deleted'; foreach ( self::DROPIN_PLUGINS[ $name ] as $folder ) { foreach ( array_keys( get_plugins() ) as $file ) { if ( 0 === strpos( $file, $folder . '/' ) ) { if ( is_plugin_active( $file ) ) { return 'active'; } $found = 'inactive'; } } foreach ( array_keys( get_mu_plugins() ) as $file ) { if ( false !== strpos( $file, $folder ) ) { return 'active'; // Loaded from mu-plugins (a host or an agency set it up so). } } } return $found; } /** * Whether Gorilla Cache may replace the drop-in left by another plugin: with the owner's click when that plugin is * installed but switched off, by itself when it has been deleted, never when it is active * or the drop-in cannot be named. A host-installed drop-in (Batcache) is replaced only with a click. * * @param bool $clicked The owner asked for it (the Status tab's button, WP-CLI). * @return bool */ public static function may_replace_dropin( $clicked ) { if ( 'foreign' !== GorillaCache_Installer::dropin_state() ) { return false; } $name = self::dropin_owner(); $status = self::dropin_owner_plugin( $name ); if ( $clicked ) { return in_array( $status, array( 'inactive', 'deleted' ), true ); } return 'deleted' === $status && ! in_array( $name, self::DROPIN_HOST_INSTALLED, true ); } /** * Whether CloudPanel's Varnish is switched on for this site. CloudPanel keeps the switch in settings.json; the * file existing does not mean Varnish is on. Read from disk, so it works without a web request (a password on * the site turns those away) and without CloudPanel's own WordPress plugin. * * @return bool */ public static function cloudpanel_varnish_enabled() { $file = self::cloudpanel_settings_file( GORILLA_CACHE_PATH ); // Any path inside the site's htdocs folder. if ( '' === $file ) { return false; } $settings = json_decode( (string) GorillaCache_Installer::filesystem()->get_contents( $file ), true ); return is_array( $settings ) && ! empty( $settings['enabled'] ); } /** * Finds CloudPanel's Varnish settings for this site. * * CloudPanel's own WordPress plugin loads the ClpVarnish class, which names the folder. A site without that * plugin still has the file: CloudPanel keeps it in the site user's home, /home//.varnish-cache/, above * the htdocs/ folder that holds every site of that user. * * @param string $abspath WordPress's folder (ABSPATH; a parameter so it can be tested). * @return string Readable settings file, or ''. */ public static function cloudpanel_settings_file( $abspath ) { $candidates = array(); if ( class_exists( 'ClpVarnish', false ) ) { $reflection = new ReflectionClass( 'ClpVarnish' ); $candidates[] = dirname( (string) $reflection->getFileName() ) . '/settings.json'; } $at = strpos( (string) $abspath, '/htdocs/' ); if ( false !== $at ) { $candidates[] = substr( (string) $abspath, 0, $at ) . '/.varnish-cache/settings.json'; } foreach ( $candidates as $file ) { if ( self::open_basedir_allows( $file ) && is_readable( $file ) ) { return $file; } } return ''; } /** * Whether PHP's open_basedir lets us look at a file (checking outside it prints a warning). * * @param string $file Absolute path. * @return bool */ private static function open_basedir_allows( $file ) { $allowed = (string) ini_get( 'open_basedir' ); if ( '' === $allowed ) { return true; } foreach ( explode( PATH_SEPARATOR, $allowed ) as $dir ) { $dir = rtrim( $dir, '/' ); if ( '' !== $dir && ( $file === $dir || 0 === strpos( $file, $dir . '/' ) ) ) { return true; } } return false; } /** * Requests the home page twice and reads what is in front of the site. Our own cache steps aside for the * request because it carries the site's secret probe token. * * @param string $token Probe token. * @return array{verdict:string, reason:string, name:string, at:int} */ public static function probe( $token ) { $args = array( 'timeout' => 10, 'redirection' => 3, 'sslverify' => (bool) apply_filters( 'https_local_ssl_verify', false ), // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- core's own filter, read, not added. 'headers' => array( 'X-Gorilla-Cache-Probe' => (string) $token, 'Cache-Control' => 'no-cache', ), ); $responses = array(); foreach ( array( 1, 2 ) as $attempt ) { $response = wp_remote_get( home_url( '/' ), $args ); if ( is_wp_error( $response ) ) { return array( 'verdict' => 'on', 'reason' => 'probe_failed', 'name' => '', 'at' => time(), ); } $headers = array(); foreach ( wp_remote_retrieve_headers( $response ) as $name => $value ) { $headers[ strtolower( (string) $name ) ] = strtolower( is_array( $value ) ? implode( ', ', $value ) : (string) $value ); } $headers[':status'] = (string) wp_remote_retrieve_response_code( $response ); $responses[] = $headers; } $result = self::classify( $responses[0], $responses[1] ); $result['at'] = time(); return $result; } /** * Reads two consecutive responses for evidence of a page cache in front of the site. * * @param array $first Headers of the first response (lower-case names and values, ":status" included). * @param array $second Headers of the second response. * @return array{verdict:string, reason:string, name:string} */ public static function classify( array $first, array $second ) { $h = $second; if ( '200' !== ( isset( $h[':status'] ) ? $h[':status'] : '' ) || false === strpos( isset( $h['content-type'] ) ? $h['content-type'] : 'text/html', 'text/html' ) ) { return self::verdict( 'on', 'probe_inconclusive' ); } if ( isset( $h['x-gorilla-cache'] ) && 0 === strpos( $h['x-gorilla-cache'], 'hit' ) ) { return self::verdict( 'on', 'probe_inconclusive' ); // Our own copy answered: the token was not honoured. } if ( isset( $h['cf-cache-status'] ) && 'hit' === trim( $h['cf-cache-status'] ) ) { return self::verdict( 'on', 'cloudflare_edge', 'Cloudflare' ); } foreach ( self::HIT_HEADERS as $name ) { if ( isset( $h[ $name ] ) && false !== strpos( $h[ $name ], 'hit' ) ) { return self::verdict( 'off', 'server_cache', $name ); } } if ( isset( $h['x-nananana'] ) && false !== strpos( $h['x-nananana'], 'batcache' ) ) { return self::verdict( 'off', 'server_cache', 'x-nananana' ); } if ( isset( $h['x-varnish'] ) && 1 === preg_match( '/^\d+\s+\d+$/', trim( $h['x-varnish'] ) ) ) { return self::verdict( 'off', 'server_cache', 'x-varnish' ); } if ( isset( $h['x-cache-age'] ) ) { return self::verdict( 'off', 'server_cache', 'x-cache-age' ); } $age_first = isset( $first['age'] ) ? (int) $first['age'] : -1; $age_second = isset( $h['age'] ) ? (int) $h['age'] : 0; if ( $age_second > 0 && $age_first < $age_second ) { return self::verdict( 'off', 'server_cache', 'age' ); } if ( isset( $h['cache-control'] ) && 1 === preg_match( '/(s-maxage|max-age)=[1-9]/', $h['cache-control'] ) ) { return self::verdict( 'on', 'weak_headers', 'cache-control' ); } return self::verdict( 'on', 'none_found' ); } /** * The one sentence the owner sees for a state. * * @param array $state State from GorillaCache_Installer::state(). * @return string */ public static function message( array $state ) { $name = (string) $state['name']; switch ( $state['reason'] ) { case 'cache_plugin': /* translators: %s: name of another cache plugin. */ return sprintf( __( '%s is already caching your pages, so Gorilla Cache is staying off to avoid caching twice.', 'gorilla-cache' ), $name ); case 'foreign_dropin': if ( '' !== $name && 'active' !== self::dropin_owner_plugin( $name ) ) { /* translators: %s: name of another cache plugin. */ return sprintf( __( "%s is not active, but its page cache file (wp-content/advanced-cache.php) is still installed, so Gorilla Cache is staying off. Replace it with Gorilla Cache's: the old file is kept in Gorilla Cache's cache folder.", 'gorilla-cache' ), $name ); } if ( '' !== $name ) { /* translators: %s: name of another cache plugin. */ return sprintf( __( "%s's page cache is installed on this site (wp-content/advanced-cache.php), so Gorilla Cache is staying off. If you no longer use it, deactivate that plugin or delete the file, then click Check again.", 'gorilla-cache' ), $name ); } return __( 'Another page cache is installed on this site (wp-content/advanced-cache.php), so Gorilla Cache is staying off.', 'gorilla-cache' ); case 'host': /* translators: %s: hosting company. */ return sprintf( __( 'Your host, %s, already caches your pages at the server, so Gorilla Cache is staying off.', 'gorilla-cache' ), $name ); case 'varnish': return 'CloudPanel' === $name ? __( 'Varnish is caching this site in front of WordPress (CloudPanel), so Gorilla Cache is staying off.', 'gorilla-cache' ) : __( 'A Varnish cache sits in front of this site, so Gorilla Cache is staying off.', 'gorilla-cache' ); case 'nginx': return __( 'Your server caches pages with nginx (Nginx Helper is clearing it), so Gorilla Cache is staying off.', 'gorilla-cache' ); case 'litespeed': return __( 'Your server is LiteSpeed, which caches pages itself; install the free LiteSpeed Cache plugin to use it, and Gorilla Cache will stay off.', 'gorilla-cache' ); case 'server_cache': /* translators: %s: name of a response header. */ return sprintf( __( 'A cache in front of your site (%s header) is already serving cached pages, so Gorilla Cache is staying off.', 'gorilla-cache' ), $name ); case 'wp_config_unwritable': case 'wp_config_missing': return __( "Almost there: add define( 'WP_CACHE', true ); to wp-config.php, above the line that says \"That's all, stop editing\", to turn caching on.", 'gorilla-cache' ); case 'paused': return defined( 'GORILLA_CACHE_PAUSED' ) && GORILLA_CACHE_PAUSED ? __( 'Caching is turned off on this site by GORILLA_CACHE_PAUSED in wp-config.php. Remove that line to turn it on.', 'gorilla-cache' ) : __( 'You turned caching off on this site. Turn it back on with the switch. A copy of the site elsewhere (production or staging) is not affected.', 'gorilla-cache' ); case 'wp_cache_off_at_runtime': return __( 'wp-config.php turns page caching on, but WordPress runs with it off: the WP_CACHE line may sit inside a condition, or PHP is using an old copy of wp-config.php. Check that line, or ask your host to restart PHP.', 'gorilla-cache' ); case 'wp_config_foreign_false': return __( "wp-config.php turns page caching off: change define( 'WP_CACHE', false ); to true to turn caching on.", 'gorilla-cache' ); case 'cache_dir_unwritable': case 'dropin_unwritable': return __( 'Gorilla Cache cannot write to wp-content/. Ask your host to make it writable.', 'gorilla-cache' ); case 'install_busy': return __( 'Gorilla Cache is being set up by another request. Reload this page in a minute.', 'gorilla-cache' ); case 'forced': return __( 'Caching is on because it has been forced on in your site\'s configuration.', 'gorilla-cache' ); default: return __( 'No other page cache was found, so Gorilla Cache serves your pages.', 'gorilla-cache' ); } } /** * The extra sentence for a note that does not change the verdict. * * @param string $note Note code. * @return string */ public static function note_message( $note ) { switch ( $note ) { case 'cloudflare_edge': return __( 'Cloudflare caches your pages and is not purged when Gorilla Cache clears: install the Cloudflare plugin, or exclude HTML from Cloudflare\'s cache. Check that your Cloudflare rules skip the cart, checkout and logged-in pages.', 'gorilla-cache' ); case 'litespeed_no_cache': return __( 'Your server is LiteSpeed but its page cache is not available to this site, so Gorilla Cache is caching your pages.', 'gorilla-cache' ); case 'litespeed_plugin_idle': return __( 'LiteSpeed Cache is active but cannot cache pages on this server, so Gorilla Cache is caching them.', 'gorilla-cache' ); case 'probe_failed': return __( 'Gorilla Cache could not check for a cache in front of your site (the request to your own home page failed), so it is caching your pages.', 'gorilla-cache' ); case 'weak_headers': return __( 'Your server sends caching headers but no page cache answered, so Gorilla Cache is caching your pages.', 'gorilla-cache' ); default: return ''; } } /** * Reads a server variable as a string. * * @param string $key Key. * @return string */ private static function server( $key ) { return isset( $_SERVER[ $key ] ) ? sanitize_text_field( wp_unslash( $_SERVER[ $key ] ) ) : ''; } /** * Builds a verdict. * * @param string $verdict "on" or "off". * @param string $reason Reason code. * @param string $name Name of what was found. * @return array{verdict:string, reason:string, name:string, note:string} */ private static function verdict( $verdict, $reason, $name = '' ) { return array( 'verdict' => $verdict, 'reason' => $reason, 'name' => $name, 'note' => '', ); } }