First Commit
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
/**
|
||||
* Server Class
|
||||
*
|
||||
* Handles serving static HTML files as a PHP fallback when .htaccess rules don't work.
|
||||
*/
|
||||
|
||||
// Prevent direct access
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class WP_To_HTML_Server {
|
||||
|
||||
/**
|
||||
* Single instance
|
||||
*/
|
||||
private static $instance = null;
|
||||
|
||||
/**
|
||||
* Get singleton instance
|
||||
*/
|
||||
public static function get_instance() {
|
||||
if (null === self::$instance) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
private function __construct() {
|
||||
// Hook early to serve cached content before WordPress runs
|
||||
add_action('template_redirect', array($this, 'maybe_serve_cached'), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if we should serve a cached file and do so if appropriate
|
||||
*/
|
||||
public function maybe_serve_cached() {
|
||||
// Check if generation is enabled
|
||||
$settings = WP_To_HTML::get_settings();
|
||||
if (!$settings['enabled']) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Don't serve cached content for logged-in users
|
||||
if (is_user_logged_in()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Don't serve for POST requests
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
return;
|
||||
}
|
||||
|
||||
// Don't serve for requests with query strings
|
||||
if (!empty($_SERVER['QUERY_STRING'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Don't serve for admin pages
|
||||
if (is_admin()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Don't serve for AJAX requests
|
||||
if (wp_doing_ajax()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Don't serve for REST API requests
|
||||
if (defined('REST_REQUEST') && REST_REQUEST) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the current request URI
|
||||
$request_uri = $_SERVER['REQUEST_URI'];
|
||||
|
||||
// Remove leading slash and get cache path
|
||||
$path = trim(parse_url($request_uri, PHP_URL_PATH), '/');
|
||||
|
||||
if (empty($path)) {
|
||||
$cache_file = WP_TO_HTML_CACHE_DIR . 'index.html';
|
||||
} else {
|
||||
$cache_file = WP_TO_HTML_CACHE_DIR . $path . '/index.html';
|
||||
}
|
||||
|
||||
// Check if cached file exists
|
||||
if (!file_exists($cache_file)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Serve the cached file
|
||||
$this->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']));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user