Skip to content

Commit

Permalink
Ensure REST API responses are not cached for authenticated users. For…
Browse files Browse the repository at this point in the history
  • Loading branch information
John Spellman authored and Pantheon Automation committed Aug 30, 2022
1 parent 120a584 commit 50ce361
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions wp-content/mu-plugins/pantheon/pantheon-page-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,7 @@ protected function setup() {

add_action( 'admin_post_pantheon_cache_flush_site', array( $this, 'flush_site' ) );

if ( ! is_admin() && function_exists( 'is_user_logged_in' ) && ! is_user_logged_in() ) {
add_action( 'send_headers', array( $this, 'cache_add_headers' ) );
}
else {
add_action( 'send_headers', array( $this, 'no_cache_add_headers' ) );
}
add_action( 'send_headers', array( $this, 'cache_add_headers' ) );
add_filter( 'rest_post_dispatch', array( $this, 'filter_rest_post_dispatch_send_cache_control' ), 10, 2 );

add_action( 'admin_notices', function(){
Expand Down Expand Up @@ -333,15 +328,24 @@ public function view_settings_page() {
}

/**
* Set a stronger cache-control header for admin or logged in requests.
* Get the cache-control header value
*
* This removes "max-age=0" which could hypothetically be used by
* Varnish on an immediate subsequent request.
*
* @return void
*/
public function no_cache_add_headers() {
header( 'cache-control: no-cache, no-store, must-revalidate');
private function get_cache_control_header_value() {
if ( ! is_admin() && ! is_user_logged_in() ) {
$ttl = absint( $this->options['default_ttl'] );
if ( $ttl < 60 && isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && 'live' === $_ENV['PANTHEON_ENVIRONMENT'] ) {
$ttl = 60;
}

return sprintf( 'public, max-age=%d', $ttl );
} else {
return 'no-cache, no-store, must-revalidate';
}
}

/**
Expand All @@ -350,23 +354,17 @@ public function no_cache_add_headers() {
* @return void
*/
public function cache_add_headers() {
$ttl = absint( $this->options['default_ttl'] );
if ( $ttl < 60 && isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && 'live' === $_ENV['PANTHEON_ENVIRONMENT'] ) {
$ttl = 60;
}

header( 'cache-control: public, max-age=' . $ttl );
header( sprintf( 'cache-control: %s', $this->get_cache_control_header_value() ) );
}

/**
* Send the cache control header for REST API requests
*
* @param WP_REST_Response $response Response.
* @return WP_REST_Response Response.
*/
public function filter_rest_post_dispatch_send_cache_control( $response, $server ) {
$ttl = absint( $this->options['default_ttl'] );
if ( $ttl < 60 && isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && 'live' === $_ENV['PANTHEON_ENVIRONMENT'] ) {
$ttl = 60;
}
$response->header( 'Cache-Control', 'public, max-age=' . $ttl );
public function filter_rest_post_dispatch_send_cache_control( $response ) {
$response->header( 'Cache-Control', $this->get_cache_control_header_value() );
return $response;
}

Expand Down

0 comments on commit 50ce361

Please sign in to comment.