serve_file($cache_file); } /** * Serve a cached HTML file * * @param string $file_path Path to the cached file */ private function serve_file($file_path) { // Set headers header('Content-Type: text/html; charset=utf-8'); header('X-WP-To-HTML: served'); header('Cache-Control: public, max-age=3600'); // Get file modification time for caching headers $mtime = filemtime($file_path); header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $mtime) . ' GMT'); // Handle conditional requests if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { $if_modified = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']); if ($if_modified >= $mtime) { header('HTTP/1.1 304 Not Modified'); exit; } } // Output the file readfile($file_path); exit; } /** * Check if current request is being served from cache * * @return bool */ public static function is_serving_from_cache() { return isset($_SERVER['HTTP_X_WP_TO_HTML']) || (function_exists('apache_request_headers') && isset(apache_request_headers()['X-WP-To-HTML'])); } }