request_is_cacheable() ) { return; } // WordPress 6.9+ owns the output buffer and hands us the finished page. We never open or close one ourselves. add_filter( 'wp_template_enhancement_output_buffer', array( $this, 'capture' ), 1000 ); } private function request_is_cacheable(): bool { $ok = ! is_user_logged_in() && ! is_404() && ! is_search() && ! is_feed() && ! is_preview() && ! is_trackback() && ! is_robots() && ! is_embed() && ! is_customize_preview() && ! is_admin() && ! wp_doing_ajax() && ! ( defined( 'REST_REQUEST' ) && REST_REQUEST ) && ! ( defined( 'DONOTCACHEPAGE' ) && DONOTCACHEPAGE ) && 'utf-8' === strtolower( (string) get_bloginfo( 'charset' ) ); // Drop-in serves UTF-8. if ( $ok && is_singular() && post_password_required() ) { $ok = false; } // WooCommerce: pages that are personal by nature. if ( $ok && function_exists( 'is_cart' ) && ( is_cart() || is_checkout() || is_account_page() ) ) { $ok = false; } /** * Filter: return false to keep the current page out of the cache. * * @param bool $ok Whether the page may be cached. */ return (bool) apply_filters( 'cache_shark_should_cache', $ok ); } /** * Filter callback for the finished page. It always returns the page * unchanged, so the visitor sees it even if caching fails. * * @param string $buffer Rendered page. * @return string */ public function capture( $buffer ) { try { $target = $GLOBALS['cache_shark_target'] ?? ''; $status = (int) http_response_code(); if ( '' !== $target && self::should_store( (string) $buffer, $status, headers_list() ) && ! ( defined( 'DONOTCACHEPAGE' ) && DONOTCACHEPAGE ) ) { // Optimise only pages that are being cached. The visitor who triggers the // cache write gets exactly the page every later visitor will get. $out = Optimizer::run( (string) $buffer ); CacheStore::write_file( $target, $out . "\n\n" ); return $out; } } catch ( \Throwable $e ) { // Never break the page because caching failed. unset( $e ); } return $buffer; } /** * Pure decision on the finished response (unit-testable). * * @param string $buffer Response body. * @param int $status HTTP status code. * @param string[] $headers Raw header lines, e.g. "Set-Cookie: a=b". */ public static function should_store( string $buffer, int $status, array $headers ): bool { if ( 200 !== $status || strlen( $buffer ) < 255 || false === stripos( $buffer, '