1
1
<?php
2
2
/**
3
- * Helper function to detect if static assets have far-future expiration headers.
3
+ * Helper function to detect if static assets have effective caching headers.
4
4
*
5
5
* @package performance-lab
6
6
* @since n.e.x.t
7
7
*/
8
8
9
+ // @codeCoverageIgnoreStart
9
10
if ( ! defined ( 'ABSPATH ' ) ) {
10
11
exit ; // Exit if accessed directly.
11
12
}
13
+ // @codeCoverageIgnoreEnd
12
14
13
15
/**
14
- * Callback for the far-future caching test.
16
+ * Callback for the effective caching headers test.
15
17
*
16
18
* @since n.e.x.t
17
19
* @access private
18
20
*
19
21
* @return array{label: string, status: string, badge: array{label: string, color: string}, description: string, actions: string, test: string} Result.
20
22
*/
21
- function perflab_ffh_assets_test (): array {
23
+ function perflab_effective_asset_cache_headers_assets_test (): array {
22
24
$ result = array (
23
25
'label ' => __ ( 'Your site serves static assets with an effective caching strategy ' , 'performance-lab ' ),
24
26
'status ' => 'good ' ,
@@ -34,7 +36,7 @@ function perflab_ffh_assets_test(): array {
34
36
)
35
37
),
36
38
'actions ' => '' ,
37
- 'test ' => 'is_far_future_headers_enabled ' ,
39
+ 'test ' => 'is_effective_asset_cache_headers_enabled ' ,
38
40
);
39
41
40
42
// List of assets to check.
@@ -46,17 +48,17 @@ function perflab_ffh_assets_test(): array {
46
48
);
47
49
48
50
/**
49
- * Filters the list of assets to check for far-future headers.
51
+ * Filters the list of assets to check for effective caching headers.
50
52
*
51
53
* @since n.e.x.t
52
54
*
53
55
* @param string[] $assets List of asset URLs to check.
54
56
*/
55
- $ assets = apply_filters ( 'perflab_ffh_assets_to_check ' , $ assets );
57
+ $ assets = apply_filters ( 'perflab_effective_asset_cache_headers_assets_to_check ' , $ assets );
56
58
$ assets = array_filter ( (array ) $ assets , 'is_string ' );
57
59
58
- // Check if far-future headers are enabled for all assets.
59
- $ results = perflab_ffh_check_assets ( $ assets );
60
+ // Check if effective caching headers are enabled for all assets.
61
+ $ results = perflab_effective_asset_cache_headers_check_assets ( $ assets );
60
62
61
63
if ( 'good ' !== $ results ['final_status ' ] ) {
62
64
$ result ['status ' ] = $ results ['final_status ' ];
@@ -65,30 +67,30 @@ function perflab_ffh_assets_test(): array {
65
67
if ( count ( $ results ['details ' ] ) > 0 ) {
66
68
$ result ['actions ' ] = sprintf (
67
69
'<p>%s</p>%s<p>%s</p> ' ,
68
- esc_html__ ( 'The following file types do not have the recommended far-future headers. Consider adding or adjusting Cache-Control or Expires headers for these asset types. ' , 'performance-lab ' ),
69
- perflab_ffh_get_extensions_table ( $ results ['details ' ] ),
70
+ esc_html__ ( 'The following file types do not have the recommended effective Cache-Control or Expires headers. Consider adding or adjusting Cache-Control or Expires headers for these asset types. ' , 'performance-lab ' ),
71
+ perflab_effective_asset_cache_headers_get_status_table ( $ results ['details ' ] ),
70
72
esc_html__ ( 'Note: "Conditionally cached" means that the browser can re-validate the resource using ETag or Last-Modified headers. This results in fewer full downloads but still requires the browser to make requests, unlike far-future expiration headers that allow the browser to fully rely on its local cache for a longer duration. ' , 'performance-lab ' )
71
73
);
72
74
}
73
75
$ result ['actions ' ] .= sprintf (
74
76
'<p>%s</p> ' ,
75
- esc_html__ ( 'Far-future Cache-Control or Expires headers can be added or adjusted with a small configuration change by your hosting provider. ' , 'performance-lab ' )
77
+ esc_html__ ( 'Effective Cache-Control or Expires headers can be added or adjusted with a small configuration change by your hosting provider. ' , 'performance-lab ' )
76
78
);
77
79
}
78
80
79
81
return $ result ;
80
82
}
81
83
82
84
/**
83
- * Checks if far-future expiration headers are enabled for a list of assets.
85
+ * Checks if effective caching headers are enabled for a list of assets.
84
86
*
85
87
* @since n.e.x.t
86
88
* @access private
87
89
*
88
90
* @param string[] $assets List of asset URLs to check.
89
91
* @return array{final_status: string, details: array{filename: string, reason: string}[]} Final status and details.
90
92
*/
91
- function perflab_ffh_check_assets ( array $ assets ): array {
93
+ function perflab_effective_asset_cache_headers_check_assets ( array $ assets ): array {
92
94
$ final_status = 'good ' ;
93
95
$ fail_details = array (); // Array of arrays with 'filename' and 'reason'.
94
96
@@ -120,26 +122,26 @@ function perflab_ffh_check_assets( array $assets ): array {
120
122
continue ;
121
123
}
122
124
123
- $ check = perflab_ffh_check_headers ( $ headers );
125
+ $ check = perflab_effective_asset_cache_headers_check_headers ( $ headers );
124
126
if ( $ check ['passed ' ] ) {
125
- // This asset passed far-future headers test, no action needed.
127
+ // This asset passed effective caching headers test, no action needed.
126
128
continue ;
127
129
}
128
130
129
131
// If not passed, decide whether to try conditional request.
130
132
if ( $ check ['missing_max_age ' ] ) {
131
- // Only if no far-future headers at all, we try conditional request.
132
- $ conditional_pass = perflab_ffh_try_conditional_request ( $ asset , $ headers );
133
+ // Only if no effective caching headers at all, we try conditional request.
134
+ $ conditional_pass = perflab_effective_asset_cache_headers_try_conditional_request ( $ asset , $ headers );
133
135
$ final_status = 'recommended ' ;
134
136
if ( ! $ conditional_pass ) {
135
137
$ fail_details [] = array (
136
138
'filename ' => $ filename ,
137
- 'reason ' => __ ( 'No far-future headers and no conditional caching ' , 'performance-lab ' ),
139
+ 'reason ' => __ ( 'No effective caching headers and no conditional caching ' , 'performance-lab ' ),
138
140
);
139
141
} else {
140
142
$ fail_details [] = array (
141
143
'filename ' => $ filename ,
142
- 'reason ' => __ ( 'No far-future headers but conditionally cached ' , 'performance-lab ' ),
144
+ 'reason ' => __ ( 'No effective caching headers but conditionally cached ' , 'performance-lab ' ),
143
145
);
144
146
}
145
147
} else {
@@ -159,23 +161,23 @@ function perflab_ffh_check_assets( array $assets ): array {
159
161
}
160
162
161
163
/**
162
- * Checks if far-future expiration headers are enabled.
164
+ * Checks if effective caching headers are enabled.
163
165
*
164
166
* @since n.e.x.t
165
167
* @access private
166
168
*
167
169
* @param WpOrg\Requests\Utility\CaseInsensitiveDictionary|array<string, string|array<string>> $headers Response headers.
168
170
* @return array{passed: bool, reason: string, missing_max_age: bool} Detailed result of the check.
169
171
*/
170
- function perflab_ffh_check_headers ( $ headers ): array {
172
+ function perflab_effective_asset_cache_headers_check_headers ( $ headers ): array {
171
173
/**
172
- * Filters the threshold for far-future headers.
174
+ * Filters the threshold for effective caching headers.
173
175
*
174
176
* @since n.e.x.t
175
177
*
176
178
* @param int $threshold Threshold in seconds.
177
179
*/
178
- $ threshold = apply_filters ( 'perflab_far_future_headers_threshold ' , YEAR_IN_SECONDS );
180
+ $ threshold = apply_filters ( 'perflab_effective_asset_cache_headers_expiration_threshold ' , YEAR_IN_SECONDS );
179
181
180
182
$ cache_control = $ headers ['cache-control ' ] ?? '' ;
181
183
$ expires = $ headers ['expires ' ] ?? '' ;
@@ -271,7 +273,7 @@ function perflab_ffh_check_headers( $headers ): array {
271
273
* @param WpOrg\Requests\Utility\CaseInsensitiveDictionary|array<string, string|array<string>> $headers The initial response headers.
272
274
* @return bool True if a 304 response was received.
273
275
*/
274
- function perflab_ffh_try_conditional_request ( string $ url , $ headers ): bool {
276
+ function perflab_effective_asset_cache_headers_try_conditional_request ( string $ url , $ headers ): bool {
275
277
$ etag = $ headers ['etag ' ] ?? '' ;
276
278
$ last_modified = $ headers ['last-modified ' ] ?? '' ;
277
279
@@ -300,15 +302,15 @@ function perflab_ffh_try_conditional_request( string $url, $headers ): bool {
300
302
}
301
303
302
304
/**
303
- * Generate a table listing files that need far-future headers, including reasons.
305
+ * Generate a table listing files that need effective caching headers, including reasons.
304
306
*
305
307
* @since n.e.x.t
306
308
* @access private
307
309
*
308
310
* @param array<array{filename: string, reason: string}> $fail_details Array of arrays with 'filename' and 'reason'.
309
311
* @return string HTML formatted table.
310
312
*/
311
- function perflab_ffh_get_extensions_table ( array $ fail_details ): string {
313
+ function perflab_effective_asset_cache_headers_get_status_table ( array $ fail_details ): string {
312
314
$ html_table = sprintf (
313
315
'<table class="widefat striped"><thead><tr><th scope="col">%s</th><th scope="col">%s</th></tr></thead><tbody> ' ,
314
316
esc_html__ ( 'File ' , 'performance-lab ' ),
0 commit comments