Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGS-8085] Use filtered value when sending cache control headers #45

Merged
merged 4 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion inc/pantheon-page-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,8 @@ public function view_settings_page() {
*/
private function get_cache_control_header_value() {
if ( ! is_admin() && ! is_user_logged_in() ) {
$ttl = absint( $this->options['default_ttl'] );
// Use the filtered default max-age if applicable.
$ttl = apply_filters( 'pantheon_cache_default_max_age', absint( $this->options['default_ttl'] ) );
if ( $ttl < 60 && isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && 'live' === $_ENV['PANTHEON_ENVIRONMENT'] ) {
$ttl = 60;
}
Expand Down
42 changes: 42 additions & 0 deletions tests/phpunit/test-page-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,46 @@ public function test_pantheon_cache_default_max_age_filter() {
// Remove the filter.
remove_all_filters( 'pantheon_cache_default_max_age' );
}

/**
* Helper function to get the max-age from the Cache-Control header of a REST API response.
*
* We're using a REST response here because it is a publicly accessible method that adds the Cache Control value from get_cache_control_header_value() to the response it returns.
*
* @return int The max-age value.
*/
private function get_max_age_from_rest_dispatch() {
// Set up a rest request.
$response = new WP_REST_Response();

// We're using the rest_post_dispatch callback here because it's a publicly accessible method that adds Cache Control to the response it returns.
$api_response = $this->pantheon_cache->filter_rest_post_dispatch_send_cache_control( $response );
$headers = $api_response->get_headers();
$cache_control_header_values = explode( ', ', $headers['Cache-Control'] );
$max_age = (int) str_replace( 'max-age=', '', $cache_control_header_values[1] );

return $max_age;
}

/**
* Test the Cache-Control header max-age.
*/
public function test_cache_control_headers() {
jazzsequence marked this conversation as resolved.
Show resolved Hide resolved
$max_age = $this->get_max_age_from_rest_dispatch();

// Assert the default max-age.
$this->assertEquals( WEEK_IN_SECONDS, $max_age );

// Filter the default max-age to 120 seconds.
add_filter( 'pantheon_cache_default_max_age', function () {
return 120;
} );

// Get the max-age after the filter.
$max_age = $this->get_max_age_from_rest_dispatch();

// Assert the filtered max-age.
$this->assertNotEquals( WEEK_IN_SECONDS, $max_age );
$this->assertEquals( 120, $max_age );
jazzsequence marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading