serve_cached_page(); } } /** * Serve cached page if available. */ private function serve_cached_page() { $url = $this->get_current_url(); $cache_path = wp_recache_get_cache_path_with_device($url); if (file_exists($cache_path)) { $modified_time = filemtime($cache_path); $cache_duration = wp_recache_get_option('cache_duration', 3600); if ((time() - $modified_time) < $cache_duration) { header('X-WP-Recache: HIT'); header('X-WP-Recache-Time: ' . date('Y-m-d H:i:s', $modified_time)); readfile($cache_path); exit; } } header('X-WP-Recache: MISS'); ob_start([$this, 'cache_output']); } /** * Cache the output. * * @param string $output Page output. * @return string Original output. */ public function cache_output($output) { if (empty($output)) { return $output; } $url = $this->get_current_url(); $cache_path = wp_recache_get_cache_path_with_device($url); $cache_dir = dirname($cache_path); if (!is_dir($cache_dir)) { wp_mkdir_p($cache_dir); } file_put_contents($cache_path, $output); return $output; } /** * End output buffering. */ public function end_output_buffering() { if (ob_get_level()) { ob_end_flush(); } } /** * Purge cache for a post. * * @param int $post_id Post ID. */ public function purge_post_cache($post_id) { $url = get_permalink($post_id); if ($url) { wp_recache_delete_cache($url); } wp_recache_delete_cache(home_url('/')); } /** * Purge cache on heartbeat. * * @param array $response Heartbeat response. * @return array Modified response. */ public function purge_cache_onheartbeat($response) { if (isset($response['wp_recache_purge'])) { wp_recache_delete_all_cache(); } return $response; } /** * Get current request URL. * * @return string Current URL. */ private function get_current_url() { $protocol = is_ssl() ? 'https' : 'http'; $host = $_SERVER['HTTP_HOST']; $uri = $_SERVER['REQUEST_URI']; return $protocol . '://' . $host . $uri; } }