-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache-everything.php
165 lines (139 loc) · 6.78 KB
/
cache-everything.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* Plugin Name: Cache Everything
* Plugin URI: https://github.com/AsyncAlchemist
* Description: A simple plugin to cache everything in Wordpress.
* Version: 0.29
* Author: Taylor Selden
* Author URI: https://github.com/AsyncAlchemist
*/
define('CACHE_EVERYTHING_JS_URL', 'wp-content/plugins/cache-everything/js');
define('CACHE_EVERYTHING_CSS_URL', 'wp-content/plugins/cache-everything/css');
define('CACHE_EVERYTHING_VERSION', '0.29');
require_once(plugin_dir_path(__FILE__) . 'handle-js-request.php');
require_once(plugin_dir_path(__FILE__) . 'handle-css-request.php');
require_once(plugin_dir_path(__FILE__) . 'helpers.php');
require_once(plugin_dir_path(__FILE__) . 'admin/menu.php');
require_once(plugin_dir_path(__FILE__) . 'admin/cache-settings.php');
require_once(plugin_dir_path(__FILE__) . 'admin/prefetch-settings.php');
require_once(plugin_dir_path(__FILE__) . 'admin/readme.php');
require_once(plugin_dir_path(__FILE__) . 'admin/css-classes.php');
/**
* Flushes rewrite rules on plugin activation and deactivation.
*/
function cache_everything_flush_rewrite_rules() {
// Add the rewrite rule and then flush
cache_everything_add_rewrite_rule();
flush_rewrite_rules();
}
register_activation_hook(__FILE__, 'cache_everything_flush_rewrite_rules');
register_activation_hook(__FILE__, 'cache_everything_activate');
register_activation_hook(__FILE__, 'cache_everything_set_default_prefetch_settings');
register_deactivation_hook(__FILE__, 'cache_everything_flush_rewrite_rules');
/**
* Registers a query variable for handling JavaScript requests.
*
* @param array $vars The array of existing query variables.
* @return array The modified array including the new query variable.
*/
function cache_everything_register_query_var($vars) {
$vars[] = 'cache_everything_js';
$vars[] = 'cache_everything_css';
return $vars;
}
add_filter('query_vars', 'cache_everything_register_query_var');
/**
* Adds a rewrite rule for serving the dynamic JavaScript file without the .js extension,
* simulating a path that includes the plugin directory.
*/
function cache_everything_add_rewrite_rule() {
// This rule simulates the path. Note that this does not actually place the file in that directory.
add_rewrite_rule(CACHE_EVERYTHING_JS_URL . '$', 'index.php?cache_everything_js=1', 'top');
add_rewrite_rule(CACHE_EVERYTHING_CSS_URL . '$', 'index.php?cache_everything_css=1', 'top');
}
add_action('init', 'cache_everything_add_rewrite_rule');
add_action('template_redirect', 'cache_everything_handle_js_request');
add_action('template_redirect', 'cache_everything_handle_css_request');
/**
* Enqueues the dynamic JavaScript file.
*/
function cache_everything_enqueue_scripts() {
$all_roles = cache_everything_get_role_slugs();
wp_enqueue_script('cache-everything', plugins_url('/public/js/cache-everything.js', __FILE__), array(), null, true);
// Determine if the site's permalink structure uses trailing slashes
$permalink_structure = get_option('permalink_structure');
$use_trailing_slashes = substr($permalink_structure, -1) === '/';
// Ensure the site URL does not end with a slash
$site_url = rtrim(site_url(), '/');
// Combine site URL with the CACHE_EVERYTHING_JS_URL to send a full URL without risking a double slash
// Append a trailing slash if the site uses trailing slashes in the permalink structure
$js_full_url = $site_url . '/' . CACHE_EVERYTHING_JS_URL . ($use_trailing_slashes ? '/' : '');
// Combine site URL with the CACHE_EVERYTHING_CSS_URL to send a full URL without risking a double slash
// Append a trailing slash if the site uses trailing slashes in the permalink structure
$css_full_url = $site_url . '/' . CACHE_EVERYTHING_CSS_URL . ($use_trailing_slashes ? '/' : '');
wp_enqueue_style('cache-everything', $css_full_url, array(), null, 'all');
$site_prefix = get_site_prefix(); // Retrieve the site prefix using the helper function
$debug_mode = get_option('cache_everything_debug_mode', '0'); // Default to '0' if not set
// Retrieve the prefetching options
$prefetch_enabled = get_option('cache_everything_prefetch_enabled', '0');
$prefetch_patterns = get_option('cache_everything_prefetch_patterns', array());
$patterns_starts_with = array();
$patterns_contains = array();
$patterns_regex = array();
foreach ($prefetch_patterns as $pattern_config) {
switch ($pattern_config['operator']) {
case 'starts_with':
$patterns_starts_with[] = $pattern_config['pattern'];
break;
case 'contains':
$patterns_contains[] = $pattern_config['pattern'];
break;
case 'regex':
$patterns_regex[] = $pattern_config['pattern'];
break;
}
}
wp_localize_script('cache-everything', 'wce_Data', array(
'roles' => $all_roles,
'jsUrl' => $js_full_url,
'cssUrl' => $css_full_url,
'sitePrefix' => $site_prefix,
'debugMode' => $debug_mode,
'prefetchEnabled' => $prefetch_enabled,
'prefetchStartsWith' => $patterns_starts_with,
'prefetchContains' => $patterns_contains,
'prefetchRegex' => $patterns_regex,
));
}
add_action('wp_enqueue_scripts', 'cache_everything_enqueue_scripts');
function cache_everything_modify_headers() {
// Check if the admin bar is showing, and if so, return early to avoid caching
if (is_admin_bar_showing()) {
return;
}
// Check if the response code is not 200, then return early to avoid caching
if (http_response_code() != 200) {
return;
}
// Check if the URL contains the word "streamer", then return early to avoid caching
if (strpos($_SERVER['REQUEST_URI'], 'streamer') !== false) {
return;
}
// Retrieve the options with a default to an empty array if not set
$options = get_option('cache_everything_cache_options', []);
// Retrieve cache times with defaults
$max_age = get_option('cache_everything_max_age', 28800);
$stale_while_revalidate = get_option('cache_everything_stale_while_revalidate', 86400);
// Calculate the Expires header value
$expires_time = gmdate('D, d M Y H:i:s', time() + $max_age) . ' GMT';
// Loop through each option and apply headers if enabled
foreach ($options as $option => $value) {
if ($value && function_exists($option) && call_user_func($option)) {
header("Cache-Control: public, max-age=$max_age, stale-while-revalidate=$stale_while_revalidate");
header("Expires: $expires_time");
header("X-WCE-Cache: public, max-age=$max_age, stale-while-revalidate=$stale_while_revalidate");
break; // Stop the loop once a match is found and header is set
}
}
}
add_action('template_redirect', 'cache_everything_modify_headers');