Skip to content

Commit 4f1a00d

Browse files
committed
Rename functions and update comments to reflect effective caching headers
1 parent 2190d60 commit 4f1a00d

File tree

3 files changed

+74
-70
lines changed

3 files changed

+74
-70
lines changed

plugins/performance-lab/includes/site-health/effective-asset-cache-headers/helper.php

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
<?php
22
/**
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.
44
*
55
* @package performance-lab
66
* @since n.e.x.t
77
*/
88

9+
// @codeCoverageIgnoreStart
910
if ( ! defined( 'ABSPATH' ) ) {
1011
exit; // Exit if accessed directly.
1112
}
13+
// @codeCoverageIgnoreEnd
1214

1315
/**
14-
* Callback for the far-future caching test.
16+
* Callback for the effective caching headers test.
1517
*
1618
* @since n.e.x.t
1719
* @access private
1820
*
1921
* @return array{label: string, status: string, badge: array{label: string, color: string}, description: string, actions: string, test: string} Result.
2022
*/
21-
function perflab_ffh_assets_test(): array {
23+
function perflab_effective_asset_cache_headers_assets_test(): array {
2224
$result = array(
2325
'label' => __( 'Your site serves static assets with an effective caching strategy', 'performance-lab' ),
2426
'status' => 'good',
@@ -34,7 +36,7 @@ function perflab_ffh_assets_test(): array {
3436
)
3537
),
3638
'actions' => '',
37-
'test' => 'is_far_future_headers_enabled',
39+
'test' => 'is_effective_asset_cache_headers_enabled',
3840
);
3941

4042
// List of assets to check.
@@ -46,17 +48,17 @@ function perflab_ffh_assets_test(): array {
4648
);
4749

4850
/**
49-
* Filters the list of assets to check for far-future headers.
51+
* Filters the list of assets to check for effective caching headers.
5052
*
5153
* @since n.e.x.t
5254
*
5355
* @param string[] $assets List of asset URLs to check.
5456
*/
55-
$assets = apply_filters( 'perflab_ffh_assets_to_check', $assets );
57+
$assets = apply_filters( 'perflab_effective_asset_cache_headers_assets_to_check', $assets );
5658
$assets = array_filter( (array) $assets, 'is_string' );
5759

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 );
6062

6163
if ( 'good' !== $results['final_status'] ) {
6264
$result['status'] = $results['final_status'];
@@ -65,30 +67,30 @@ function perflab_ffh_assets_test(): array {
6567
if ( count( $results['details'] ) > 0 ) {
6668
$result['actions'] = sprintf(
6769
'<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'] ),
7072
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' )
7173
);
7274
}
7375
$result['actions'] .= sprintf(
7476
'<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' )
7678
);
7779
}
7880

7981
return $result;
8082
}
8183

8284
/**
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.
8486
*
8587
* @since n.e.x.t
8688
* @access private
8789
*
8890
* @param string[] $assets List of asset URLs to check.
8991
* @return array{final_status: string, details: array{filename: string, reason: string}[]} Final status and details.
9092
*/
91-
function perflab_ffh_check_assets( array $assets ): array {
93+
function perflab_effective_asset_cache_headers_check_assets( array $assets ): array {
9294
$final_status = 'good';
9395
$fail_details = array(); // Array of arrays with 'filename' and 'reason'.
9496

@@ -120,26 +122,26 @@ function perflab_ffh_check_assets( array $assets ): array {
120122
continue;
121123
}
122124

123-
$check = perflab_ffh_check_headers( $headers );
125+
$check = perflab_effective_asset_cache_headers_check_headers( $headers );
124126
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.
126128
continue;
127129
}
128130

129131
// If not passed, decide whether to try conditional request.
130132
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 );
133135
$final_status = 'recommended';
134136
if ( ! $conditional_pass ) {
135137
$fail_details[] = array(
136138
'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' ),
138140
);
139141
} else {
140142
$fail_details[] = array(
141143
'filename' => $filename,
142-
'reason' => __( 'No far-future headers but conditionally cached', 'performance-lab' ),
144+
'reason' => __( 'No effective caching headers but conditionally cached', 'performance-lab' ),
143145
);
144146
}
145147
} else {
@@ -159,23 +161,23 @@ function perflab_ffh_check_assets( array $assets ): array {
159161
}
160162

161163
/**
162-
* Checks if far-future expiration headers are enabled.
164+
* Checks if effective caching headers are enabled.
163165
*
164166
* @since n.e.x.t
165167
* @access private
166168
*
167169
* @param WpOrg\Requests\Utility\CaseInsensitiveDictionary|array<string, string|array<string>> $headers Response headers.
168170
* @return array{passed: bool, reason: string, missing_max_age: bool} Detailed result of the check.
169171
*/
170-
function perflab_ffh_check_headers( $headers ): array {
172+
function perflab_effective_asset_cache_headers_check_headers( $headers ): array {
171173
/**
172-
* Filters the threshold for far-future headers.
174+
* Filters the threshold for effective caching headers.
173175
*
174176
* @since n.e.x.t
175177
*
176178
* @param int $threshold Threshold in seconds.
177179
*/
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 );
179181

180182
$cache_control = $headers['cache-control'] ?? '';
181183
$expires = $headers['expires'] ?? '';
@@ -271,7 +273,7 @@ function perflab_ffh_check_headers( $headers ): array {
271273
* @param WpOrg\Requests\Utility\CaseInsensitiveDictionary|array<string, string|array<string>> $headers The initial response headers.
272274
* @return bool True if a 304 response was received.
273275
*/
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 {
275277
$etag = $headers['etag'] ?? '';
276278
$last_modified = $headers['last-modified'] ?? '';
277279

@@ -300,15 +302,15 @@ function perflab_ffh_try_conditional_request( string $url, $headers ): bool {
300302
}
301303

302304
/**
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.
304306
*
305307
* @since n.e.x.t
306308
* @access private
307309
*
308310
* @param array<array{filename: string, reason: string}> $fail_details Array of arrays with 'filename' and 'reason'.
309311
* @return string HTML formatted table.
310312
*/
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 {
312314
$html_table = sprintf(
313315
'<table class="widefat striped"><thead><tr><th scope="col">%s</th><th scope="col">%s</th></tr></thead><tbody>',
314316
esc_html__( 'File', 'performance-lab' ),
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<?php
22
/**
3-
* Hook callbacks used for far-future headers.
3+
* Hook callbacks used for effective caching headers.
44
*
55
* @package performance-lab
66
* @since n.e.x.t
77
*/
88

9+
// @codeCoverageIgnoreStart
910
if ( ! defined( 'ABSPATH' ) ) {
1011
exit; // Exit if accessed directly.
1112
}
13+
// @codeCoverageIgnoreEnd
1214

1315
/**
1416
* Adds tests to site health.
@@ -19,11 +21,11 @@
1921
* @param array{direct: array<string, array{label: string, test: string}>} $tests Site Health Tests.
2022
* @return array{direct: array<string, array{label: string, test: string}>} Amended tests.
2123
*/
22-
function perflab_ffh_add_test( array $tests ): array {
23-
$tests['direct']['far_future_headers'] = array(
24+
function perflab_effective_asset_cache_headers_add_test( array $tests ): array {
25+
$tests['direct']['effective_asset_cache_headers'] = array(
2426
'label' => __( 'Effective Caching Headers', 'performance-lab' ),
25-
'test' => 'perflab_ffh_assets_test',
27+
'test' => 'perflab_effective_asset_cache_headers_assets_test',
2628
);
2729
return $tests;
2830
}
29-
add_filter( 'site_status_tests', 'perflab_ffh_add_test' );
31+
add_filter( 'site_status_tests', 'perflab_effective_asset_cache_headers_add_test' );

0 commit comments

Comments
 (0)