-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStarResponseController.php
More file actions
306 lines (268 loc) · 10.1 KB
/
Copy pathStarResponseController.php
File metadata and controls
306 lines (268 loc) · 10.1 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
declare(strict_types=1);
namespace StarCache;
/**
* StarResponseController — Response Header Manager
*
* The ONLY place in StarCache that sets Cache-Control response headers.
* Inspired by WordPress VIP's TTL_Manager, but self-contained and
* WordPress-VIP-free.
*
* Eligibility rules (strict, evaluated in order)
* -----------------------------------------------
* 1. Authenticated user → no-cache (hard rule, no exceptions)
* 2. Method is not GET or HEAD → no-cache
* 3. DONOTCACHEPAGE defined and truthy → no-cache
* 4. Cache-Control or Expires header already present → do nothing
* (respects upstream decisions made by plugins, WooCommerce, REST API, etc.)
* 5. starcache_bypass_page_cache filter returns true → no-cache
* 6. Otherwise → apply the configured public-cache policy
*
* Default policy
* --------------
* Cache-Control: public, max-age=300, stale-while-revalidate=30
* Vary: Cookie, Accept-Encoding
* X-StarCache-Context: {context-hash}
* X-Cache: MISS
*
* Overriding TTL values
* ---------------------
* Use WP filters before headers are sent:
* add_filter('starcache_max_age', fn() => 600);
* add_filter('starcache_stale_while_revalidate', fn() => 60);
*
* @package StarCache
* @author MaximillianGroup (Max Barrett) <maximilliangroup@gmail.com>
* @version 2.1.1
* @license Apache 2.0
*/
class StarResponseController
{
// -------------------------------------------------------------------------
// Default policy values
// -------------------------------------------------------------------------
/** Default public max-age in seconds (5 minutes). */
public const DEFAULT_MAX_AGE = 300;
/** Default stale-while-revalidate window in seconds. */
public const DEFAULT_STALE_WHILE_REVALIDATE = 30;
/** @var bool Whether headers have been applied for this request. */
private static bool $applied = false;
/**
* Whether upstream Cache-Control/Expires headers existed at the moment
* apply() ran — captured BEFORE StarCache sets its own headers.
*
* null = apply() has not been called yet.
*
* @var bool|null
*/
private static ?bool $hadUpstreamHeaders = null;
// -------------------------------------------------------------------------
// Public API
// -------------------------------------------------------------------------
/**
* Apply the appropriate cache headers for the current response.
*
* Idempotent – subsequent calls are no-ops once headers have been applied
* or once headers_sent() returns true.
*
* Called on the WordPress 'send_headers' action at priority 999 so it runs
* AFTER default-priority (10) plugin/theme callbacks. This ensures that
* upstream Cache-Control/Expires decisions made by other plugins are already
* present in headers_list() when Gate 4 executes, making the upstream-header
* gate reliable.
*/
public static function apply(): void
{
if (self::$applied || headers_sent()) {
return;
}
// Capture upstream state BEFORE StarCache sets any headers of its own.
// StarPageCache::capturePageOutput() (ob callback, runs at PHP shutdown)
// uses hadUpstreamHeadersBeforeApply() so it does not mistake StarCache's
// own Cache-Control for an upstream no-cache decision.
self::$hadUpstreamHeaders = self::upstreamHeadersExist();
self::$applied = true;
// Gate 1: authenticated users are NEVER served cached content
if (function_exists('is_user_logged_in') && is_user_logged_in()) {
self::sendNoCache();
return;
}
// Gate 2: only GET and HEAD are cacheable
$method = strtoupper($_SERVER['REQUEST_METHOD'] ?? 'GET');
if ($method !== 'GET' && $method !== 'HEAD') {
self::sendNoCache();
return;
}
// Gate 3: explicit no-cache flag
if (defined('DONOTCACHEPAGE') && DONOTCACHEPAGE) {
self::sendNoCache();
return;
}
// Gate 4: respect upstream headers – if Cache-Control or Expires are
// already set by another plugin / framework, do not override them
if (self::upstreamHeadersExist()) {
return;
}
// Gate 5: plugin/theme opt-out
if ((bool) apply_filters('starcache_bypass_page_cache', false)) {
self::sendNoCache();
return;
}
// All gates passed – apply the public-cache policy
self::sendPublicCacheHeaders();
}
/**
* Explicitly mark the current response as not cacheable.
*
* Call this from any plugin or theme that produces uncacheable output.
* Safe to call at any point before headers_sent().
*/
public static function doNotCache(): void
{
if (!headers_sent()) {
self::sendNoCache();
}
self::$applied = true;
}
/**
* Returns true when the current request is eligible for page caching.
*
* This method does NOT set any headers; it is a pure eligibility check
* used by StarPageCache to decide whether to start output buffering.
*/
public static function isEligible(): bool
{
// Hard rule: authenticated users bypass cache
if (StarCacheContext::shouldBypass()) {
return false;
}
// Only GET and HEAD
$method = strtoupper($_SERVER['REQUEST_METHOD'] ?? 'GET');
if ($method !== 'GET' && $method !== 'HEAD') {
return false;
}
// Explicit no-cache flag
if (defined('DONOTCACHEPAGE') && DONOTCACHEPAGE) {
return false;
}
// WordPress admin area
if (function_exists('is_admin') && is_admin()) {
return false;
}
// AJAX requests
if (defined('DOING_AJAX') && DOING_AJAX) {
return false;
}
// WP-CLI
if (defined('WP_CLI') && WP_CLI) {
return false;
}
// WooCommerce dynamic pages
if (function_exists('is_woocommerce')) {
if (function_exists('is_cart') && is_cart()) {
return false;
}
if (function_exists('is_checkout') && is_checkout()) {
return false;
}
if (function_exists('is_account_page') && is_account_page()) {
return false;
}
}
// Plugin/theme opt-out
return !(bool) apply_filters('starcache_bypass_page_cache', false);
}
/**
* Reset internal state (used in tests / when switching blogs).
*/
public static function reset(): void
{
self::$applied = false;
self::$hadUpstreamHeaders = null;
}
// -------------------------------------------------------------------------
// Private helpers
// -------------------------------------------------------------------------
/**
* Emit the default public-cache policy headers.
* This is the ONLY place Cache-Control is written for cacheable responses.
*/
private static function sendPublicCacheHeaders(): void
{
$maxAge = self::sanitizeDirectiveSeconds(
apply_filters('starcache_max_age', self::DEFAULT_MAX_AGE),
self::DEFAULT_MAX_AGE
);
$swr = self::sanitizeDirectiveSeconds(
apply_filters('starcache_stale_while_revalidate', self::DEFAULT_STALE_WHILE_REVALIDATE),
self::DEFAULT_STALE_WHILE_REVALIDATE
);
header('Cache-Control: public, max-age=' . $maxAge . ', stale-while-revalidate=' . $swr);
// Vary on Cookie + Accept-Encoding so that CDNs serve correct context buckets.
header('Vary: Cookie, Accept-Encoding');
header('X-Cache: MISS');
// Debug header: context hash for cache-bucket verification.
header('X-StarCache-Context: ' . StarCacheContext::hash());
}
/**
* Normalize cache directive durations to a safe, non-negative integer.
*
* Negative or non-sensical values from third-party filters can result in
* invalid Cache-Control headers. This guard keeps header output valid and
* predictable while still allowing plugin-level TTL customization.
*
* @param mixed $value Requested TTL value from filter callbacks.
* @param int $fallback Default value used when the request is invalid.
*/
private static function sanitizeDirectiveSeconds(mixed $value, int $fallback): int
{
if (!is_numeric($value)) {
return max(0, $fallback);
}
$seconds = (int) $value;
if ($seconds < 0) {
return max(0, $fallback);
}
return $seconds;
}
/**
* Emit no-cache headers.
*/
private static function sendNoCache(): void
{
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Pragma: no-cache');
header('X-Cache: BYPASS');
}
/**
* Return true when Cache-Control or Expires was already set by an upstream
* plugin at the time apply() ran — i.e. BEFORE StarCache set its own headers.
*
* Use this in ob callbacks (e.g. capturePageOutput) where StarCache's own
* Cache-Control is already in headers_list() and calling upstreamHeadersExist()
* directly would always return true.
*
* Returns false if apply() has not been called yet.
*/
public static function hadUpstreamHeadersBeforeApply(): bool
{
return self::$hadUpstreamHeaders ?? false;
}
/**
* Return true when Cache-Control or Expires is already set (upstream decision).
*
* Public so that StarPageCache can consult the same gate before persisting
* a captured response – preventing HTML from being stored when another plugin
* has already declared the response non-cacheable.
*/
public static function upstreamHeadersExist(): bool
{
foreach (headers_list() as $header) {
$lower = strtolower($header);
if (str_starts_with($lower, 'cache-control:') || str_starts_with($lower, 'expires:')) {
return true;
}
}
return false;
}
}