slug => название */ public static function detected(): array { $list = array(); if ( defined( 'WP_ROCKET_VERSION' ) ) { $list['wp-rocket'] = 'WP Rocket'; } if ( defined( 'LSCWP_V' ) ) { $list['litespeed'] = 'LiteSpeed Cache'; } if ( defined( 'W3TC' ) ) { $list['w3tc'] = 'W3 Total Cache'; } if ( defined( 'WPCACHEHOME' ) ) { $list['wp-super-cache'] = 'WP Super Cache'; } if ( class_exists( 'WpFastestCache', false ) ) { $list['wp-fastest-cache'] = 'WP Fastest Cache'; } if ( class_exists( 'Cache_Enabler', false ) ) { $list['cache-enabler'] = 'Cache Enabler'; } if ( defined( 'WPHB_VERSION' ) ) { $list['hummingbird'] = 'Hummingbird'; } if ( defined( 'BREEZE_VERSION' ) ) { $list['breeze'] = 'Breeze'; } if ( function_exists( 'sg_cachepress_purge_cache' ) ) { $list['sg-optimizer'] = 'SiteGround Optimizer'; } if ( ! $list && defined( 'WP_CACHE' ) && WP_CACHE && file_exists( WP_CONTENT_DIR . '/advanced-cache.php' ) ) { $list['advanced-cache'] = __( 'Unknown page cache (advanced-cache.php)', 'botward' ); } return $list; } /** Названия обнаруженных кэшей, которые требуют ручной настройки. */ public static function needs_attention(): array { $s = Botward_Settings::get(); $out = array(); foreach ( self::detected() as $slug => $name ) { if ( 'wp-rocket' === $slug && ! empty( $s['cache_integration'] ) ) { continue; } $out[ $slug ] = $name; } return $out; } /* ------------------------------------------------------------------ */ /* Страница «Caching» */ /* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */ /* Диагностика WP Rocket */ /* ------------------------------------------------------------------ */ /** Проверяем, где именно WP Rocket требует cookie допуска. @return array */ public static function diagnose(): array { $cookie = Botward_Gate::COOKIE; $rows = array(); $add = static function ( string $label, string $level, string $detail ) use ( &$rows ): void { $rows[] = array( 'label' => $label, 'level' => $level, 'detail' => $detail ); }; $server = isset( $_SERVER['SERVER_SOFTWARE'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ) : ''; $add( __( 'Web server', 'botward' ), 'info', '' !== $server ? $server : __( 'Unknown', 'botward' ) ); // 1. Наш фильтр действительно отдаёт cookie. $filtered = apply_filters( 'rocket_cache_mandatory_cookies', array() ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- фильтр стороннего плагина WP Rocket. if ( is_array( $filtered ) && in_array( $cookie, $filtered, true ) ) { $add( __( 'Cookie registered for WP Rocket', 'botward' ), 'ok', __( 'Yes, the WP Rocket filter returns the cookie.', 'botward' ) ); } else { $add( __( 'Cookie registered for WP Rocket', 'botward' ), 'bad', __( 'No, the filter does not return the cookie. Check that protection and cache integration are enabled in Settings.', 'botward' ) ); } // 2. Драйвер кэша. if ( defined( 'WP_CACHE' ) && WP_CACHE && file_exists( WP_CONTENT_DIR . '/advanced-cache.php' ) ) { $add( __( 'Page cache drop-in', 'botward' ), 'ok', __( 'advanced-cache.php is present and WP_CACHE is on.', 'botward' ) ); } else { $add( __( 'Page cache drop-in', 'botward' ), 'bad', __( 'advanced-cache.php is missing or WP_CACHE is off: the WP Rocket page cache is not active.', 'botward' ) ); } // 3. Конфиг WP Rocket (PHP-уровень). $dir = defined( 'WP_ROCKET_CONFIG_PATH' ) ? (string) WP_ROCKET_CONFIG_PATH : trailingslashit( WP_CONTENT_DIR ) . 'wp-rocket-config/'; $files = is_dir( $dir ) ? glob( rtrim( $dir, '/' ) . '/*.php' ) : array(); if ( ! $files ) { $add( __( 'WP Rocket configuration', 'botward' ), 'warn', __( 'The WP Rocket config folder was not found or is empty.', 'botward' ) ); } else { $found = false; foreach ( $files as $file ) { $c = is_readable( $file ) ? file_get_contents( $file ) : false; // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- локальный файл конфигурации, только чтение. if ( false !== $c && false !== strpos( $c, $cookie ) ) { $found = true; break; } } if ( $found ) { $add( __( 'WP Rocket configuration', 'botward' ), 'ok', __( 'The cookie is in the WP Rocket config: cached pages are served only to visitors with the cookie.', 'botward' ) ); } else { $add( __( 'WP Rocket configuration', 'botward' ), 'bad', __( 'The cookie is not in the WP Rocket config. Press "Re-apply WP Rocket settings", or open the WP Rocket settings and save them once.', 'botward' ) ); } } // 4. .htaccess. if ( false !== stripos( $server, 'nginx' ) ) { $add( '.htaccess', 'info', __( 'This server is nginx: .htaccess is not used. Configure the cache bypass in nginx (see the nginx section below).', 'botward' ) ); return $rows; } $home = ( function_exists( 'get_home_path' ) ? get_home_path() : ABSPATH ); $path = trailingslashit( $home ) . '.htaccess'; $htx = is_readable( $path ) ? file_get_contents( $path ) : false; // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- локальный .htaccess, только чтение. if ( false === $htx ) { $add( '.htaccess', 'warn', __( 'The .htaccess file was not found or is not readable.', 'botward' ) ); return $rows; } $start = strpos( $htx, '# BEGIN WP Rocket' ); $end = strpos( $htx, '# END WP Rocket' ); if ( false === $start ) { $add( '.htaccess', 'warn', __( 'No WP Rocket block was found in .htaccess.', 'botward' ) ); return $rows; } $block = false === $end ? substr( $htx, $start ) : substr( $htx, $start, $end - $start ); $serves = false !== strpos( $block, 'cache/wp-rocket' ); if ( ! $serves ) { $add( '.htaccess', 'ok', __( 'WP Rocket does not serve cached pages from .htaccess on this site, so only the PHP-level check above matters.', 'botward' ) ); } elseif ( false !== strpos( $block, $cookie ) ) { $add( '.htaccess', 'ok', __( 'The cache rules in .htaccess require the cookie.', 'botward' ) ); } else { $add( '.htaccess', 'bad', __( 'The cache rules in .htaccess do not require the cookie, so Apache can serve cached pages to visitors without it. Press "Re-apply WP Rocket settings".', 'botward' ) ); } if ( $serves ) { if ( wp_is_writable( $path ) ) { $add( __( 'File permissions', 'botward' ), 'ok', __( 'PHP can write .htaccess.', 'botward' ) ); } else { $add( __( 'File permissions', 'botward' ), 'warn', __( 'PHP cannot write .htaccess, so WP Rocket cannot update its rules automatically. Make the file writable or add the rule by hand.', 'botward' ) ); } } return $rows; } private static function render_diagnostics(): void { if ( ! defined( 'WP_ROCKET_VERSION' ) ) { return; } $rows = self::diagnose(); $labels = array( 'ok' => array( 'green', __( 'OK', 'botward' ) ), 'warn' => array( 'amber', __( 'Warning', 'botward' ) ), 'bad' => array( 'red', __( 'Problem', 'botward' ) ), 'info' => array( 'gray', __( 'Info', 'botward' ) ), ); $bad = false; echo '

' . esc_html__( 'WP Rocket diagnostics', 'botward' ) . '

'; echo ''; foreach ( $rows as $r ) { $bad = $bad || 'bad' === $r['level']; echo ''; } echo '
' . esc_html__( 'Check', 'botward' ) . '' . esc_html__( 'Result', 'botward' ) . '' . esc_html__( 'Details', 'botward' ) . '
' . esc_html( $r['label'] ) . '' . esc_html( $labels[ $r['level'] ][1] ) . '' . esc_html( $r['detail'] ) . '
'; if ( $bad ) { echo '

' . esc_html__( 'Problems found. Press "Re-apply WP Rocket settings" in the WP Rocket section below, then reload this page.', 'botward' ) . '

'; } else { echo '

' . esc_html__( 'No problems found. Finish with the test cycle in "How to test" below: it is the decisive check.', 'botward' ) . '

'; } echo '
'; } private static function code( string $code ): void { echo '
' . esc_html( $code ) . '
'; } private static function section( string $title, callable $body, bool $open = false ): void { echo '
' . esc_html( $title ) . '
'; $body(); echo '
'; } public static function render_page(): void { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Insufficient permissions.', 'botward' ), 403 ); } $s = Botward_Settings::get(); $cookie = Botward_Gate::COOKIE; $found = self::detected(); Botward_Admin::header( __( 'Botward — Caching', 'botward' ) ); echo '

' . esc_html__( 'Why this matters', 'botward' ) . '

'; /* translators: %s: cookie name */ echo '

' . wp_kses( sprintf( __( 'Page caching serves a stored copy of a page before WordPress runs, so the challenge never appears. Cached pages must not be served to visitors who do not have the pass cookie %s.', 'botward' ), esc_html( $cookie ) ), array( 'code' => array() ) ) . '

'; echo '

' . esc_html__( 'Cache-level rules can only check that the cookie exists, not that it is valid. A targeted bot that forges the cookie can still read cached pages. Uncached pages, forms, cart and checkout stay fully protected. Challenge pages themselves are never cached.', 'botward' ) . '

'; self::render_diagnostics(); // Обнаруженные плагины. echo '

' . esc_html__( 'Detected caching', 'botward' ) . '

'; if ( ! $found ) { echo '

' . esc_html__( 'No page caching plugin was detected. If your host or CDN caches pages (nginx FastCGI cache, Varnish, Cloudflare), use the matching section below.', 'botward' ) . '

'; } else { echo ''; foreach ( $found as $slug => $name ) { if ( 'wp-rocket' === $slug ) { $on = ! empty( $s['cache_integration'] ) && ! empty( $s['enabled'] ); $cls = $on ? 'green' : 'amber'; $badge = $on ? __( 'Configured automatically', 'botward' ) : __( 'Needs setup', 'botward' ); $action = $on ? __( 'Nothing. The mandatory cookie is registered in WP Rocket.', 'botward' ) : __( 'Enable cache integration in Settings.', 'botward' ); } elseif ( 'litespeed' === $slug ) { $cls = 'amber'; $badge = __( 'Manual rule required', 'botward' ); $action = __( 'Add the .htaccess rule from the LiteSpeed section below.', 'botward' ); } else { $cls = 'red'; $badge = __( 'Not compatible by default', 'botward' ); $action = __( 'This plugin cannot serve its cache only to visitors with a cookie. Disable its page cache, switch to WP Rocket or LiteSpeed Cache, or add a server-level rule below.', 'botward' ); } echo ''; } echo '
' . esc_html__( 'Plugin', 'botward' ) . '' . esc_html__( 'Status', 'botward' ) . '' . esc_html__( 'What to do', 'botward' ) . '
' . esc_html( $name ) . '' . esc_html( $badge ) . '' . esc_html( $action ) . '
'; } echo '
'; // Инструкции. echo '

' . esc_html__( 'Setup instructions', 'botward' ) . '

'; self::section( 'WP Rocket', static function () use ( $cookie, $s ) { /* translators: %s: cookie name */ echo '

' . wp_kses( sprintf( __( 'With cache integration enabled, the plugin registers %s as a mandatory cookie. WP Rocket then builds and serves cached pages only for visitors who have the cookie, so first-time visitors always get a live page and see the challenge.', 'botward' ), esc_html( $cookie ) ), array( 'code' => array() ) ) . '

'; echo '

' . esc_html__( 'This is not compatible with Cloudflare APO, Cloudflare Cache Everything or Varnish. Use the Cloudflare or Varnish rule below instead.', 'botward' ) . '

'; echo '

' . esc_html__( 'The WP Rocket configuration is refreshed automatically when you change the integration or the protection status. If the challenge does not appear, re-apply it manually:', 'botward' ) . '

'; if ( defined( 'WP_ROCKET_VERSION' ) ) { $url = wp_nonce_url( admin_url( 'admin-post.php?action=botward_cache_refresh' ), 'botward_cache_refresh' ); echo '

' . esc_html__( 'Re-apply WP Rocket settings', 'botward' ) . '

'; } if ( empty( $s['cache_integration'] ) ) { echo '

' . esc_html__( 'Cache integration is currently disabled in Settings.', 'botward' ) . '

'; } }, isset( $found['wp-rocket'] ) ); self::section( 'LiteSpeed Cache / LiteSpeed Web Server', static function () { echo '

' . esc_html__( 'The challenge pages already send the X-LiteSpeed-Cache-Control: no-cache header. To stop LiteSpeed serving cached pages to visitors without the cookie, add this at the very top of .htaccess (above # BEGIN WordPress):', 'botward' ) . '

'; self::code( "\nRewriteEngine On\nRewriteCond %{HTTP_COOKIE} !botward_pass [NC]\nRewriteRule .* - [E=Cache-Control:no-cache]\n" ); }, isset( $found['litespeed'] ) ); self::section( __( 'nginx (FastCGI or proxy cache)', 'botward' ), static function () { echo '

' . esc_html__( 'Bypass the cache when the cookie is missing. Add this to the server or location block that uses the cache, then reload nginx:', 'botward' ) . '

'; self::code( "set \$das_skip_cache 1;\nif (\$http_cookie ~* \"botward_pass\") {\n set \$das_skip_cache 0;\n}\nfastcgi_cache_bypass \$das_skip_cache;\nfastcgi_no_cache \$das_skip_cache;" ); echo '

' . esc_html__( 'For proxy_cache use proxy_cache_bypass and proxy_no_cache with the same variable.', 'botward' ) . '

'; } ); self::section( 'Cloudflare', static function () { echo '

' . esc_html__( 'Create a Cache Rule (Caching → Cache Rules) with this expression and the action "Bypass cache":', 'botward' ) . '

'; self::code( 'not http.cookie contains "botward_pass"' ); echo '

' . esc_html__( 'Also set the IP source to Cloudflare in the plugin Settings, otherwise every visitor looks like one IP.', 'botward' ) . '

'; } ); self::section( 'Varnish', static function () { echo '

' . esc_html__( 'In vcl_recv, pass requests that have no pass cookie:', 'botward' ) . '

'; self::code( "sub vcl_recv {\n if (req.http.Cookie !~ \"botward_pass\") {\n return (pass);\n }\n}" ); } ); self::section( __( 'Other caching plugins', 'botward' ), static function () { echo '

' . esc_html__( 'W3 Total Cache, WP Super Cache, WP Fastest Cache, Cache Enabler, Hummingbird, Breeze and SiteGround Optimizer can only exclude pages for visitors who HAVE a cookie, not for visitors who lack one. Options:', 'botward' ) . '

'; echo ''; } ); self::section( __( 'How to test', 'botward' ), static function () { $home = home_url( '/' ); echo '

' . esc_html__( 'Test cycle. Use two private windows, logged out, from an IP that is not whitelisted (a whitelisted IP never sees the challenge):', 'botward' ) . '

'; echo '
  1. ' . esc_html__( 'Window A: open the site. The challenge appears. Solve it.', 'botward' ) . '
  2. ' . '
  3. ' . esc_html__( 'Window A: open the same page twice more. In the page source of the last load, a WP Rocket comment with "cached@" appears: the page is now cached.', 'botward' ) . '
  4. ' . '
  5. ' . esc_html__( 'Window B (a new private window, no cookies): open the same page. The challenge must appear again, not the site.', 'botward' ) . '
  6. ' . '
  7. ' . esc_html__( 'If window B shows the site, the cache is served without the cookie: check the diagnostics table above.', 'botward' ) . '
'; echo '

' . esc_html__( 'From a terminal, a request without cookies must return the challenge page:', 'botward' ) . '

'; self::code( 'curl -s ' . $home . ' | grep -i -E "botward_token|cached@"' ); } ); echo '
'; } }