Set header cache-control for cart/checkout pages closes #29484

This commit is contained in:
roykho 2021-05-14 12:06:07 -07:00
parent 67821771e3
commit 9f6104598b
No known key found for this signature in database
GPG Key ID: 7B36C0EA25795714
1 changed files with 22 additions and 2 deletions

View File

@ -42,14 +42,34 @@ class WC_Cache_Helper {
*/ */
public static function additional_nocache_headers( $headers ) { public static function additional_nocache_headers( $headers ) {
$agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$set_cache = false;
/** /**
* Allow plugins to enable nocache headers. Enabled for Google weblight. * Allow plugins to enable nocache headers. Enabled for Google weblight.
* *
* @see https://support.google.com/webmasters/answer/1061943?hl=en
* @param bool $enable_nocache_headers Flag indicating whether to add nocache headers. Default: false. * @param bool $enable_nocache_headers Flag indicating whether to add nocache headers. Default: false.
*/ */
if ( false !== strpos( $agent, 'googleweblight' ) || apply_filters( 'woocommerce_enable_nocache_headers', false ) ) { if ( apply_filters( 'woocommerce_enable_nocache_headers', false ) ) {
$set_cache = true;
}
/**
* Enabled for Google weblight.
*
* @see https://support.google.com/webmasters/answer/1061943?hl=en
*/
if ( false !== strpos( $agent, 'googleweblight' ) ) {
// no-transform: Opt-out of Google weblight. https://support.google.com/webmasters/answer/6211428?hl=en. // no-transform: Opt-out of Google weblight. https://support.google.com/webmasters/answer/6211428?hl=en.
$set_cache = true;
}
// Note that is_cart() will return true even for checkout page.
if ( false !== strpos( $agent, 'Chrome' ) && is_cart() ) {
$set_cache = true;
}
if ( $set_cache ) {
$headers['Cache-Control'] = 'no-transform, no-cache, no-store, must-revalidate'; $headers['Cache-Control'] = 'no-transform, no-cache, no-store, must-revalidate';
} }
return $headers; return $headers;