@@ -26,44 +26,52 @@ class WC_Cache_Helper {
2626 * Hook in methods.
2727 */
2828 public static function init () {
29- add_filter ( 'nocache_headers ' , array ( __CLASS__ , 'additional_nocache_headers ' ), 10 );
29+ add_action ( 'wp_headers ' , array ( __CLASS__ , 'prevent_caching ' ) );
3030 add_action ( 'shutdown ' , array ( __CLASS__ , 'delete_transients_on_shutdown ' ), 10 );
3131 add_action ( 'template_redirect ' , array ( __CLASS__ , 'geolocation_ajax_redirect ' ) );
3232 add_action ( 'wc_ajax_update_order_review ' , array ( __CLASS__ , 'update_geolocation_hash ' ), 5 );
3333 add_action ( 'admin_notices ' , array ( __CLASS__ , 'notices ' ) );
3434 add_action ( 'delete_version_transients ' , array ( __CLASS__ , 'delete_version_transients ' ), 10 );
35- add_action ( 'wp ' , array ( __CLASS__ , 'prevent_caching ' ) );
3635 add_action ( 'clean_term_cache ' , array ( __CLASS__ , 'clean_term_cache ' ), 10 , 2 );
3736 add_action ( 'edit_terms ' , array ( __CLASS__ , 'clean_term_cache ' ), 10 , 2 );
3837 }
3938
4039 /**
41- * Set additional nocache headers .
40+ * Prevent caching on certain pages .
4241 *
43- * @param array $headers Header names and field values.
4442 * @since 3.6.0
43+ *
44+ * @param array<string, string> $headers Header names and field values.
45+ * @return array<string, string> Filtered headers.
4546 */
46- public static function additional_nocache_headers ( $ headers ) {
47- /**
48- * Allow plugins to enable nocache headers.
49- *
50- * @param bool $enable_nocache_headers Flag indicating whether to add nocache headers. Default: false.
51- */
52- $ set_cache = (bool ) apply_filters ( 'woocommerce_enable_nocache_headers ' , false );
53-
54- if ( $ set_cache ) {
55- $ new_directives = array (
56- 'no-transform ' ,
57- 'no-cache ' ,
58- 'no-store ' ,
59- 'must-revalidate ' ,
60- );
61- $ old_directives = array ();
62- if ( isset ( $ headers ['Cache-Control ' ] ) ) {
63- $ old_directives = preg_split ( '/\s*,\s*/ ' , $ headers ['Cache-Control ' ] );
64- }
65- $ headers ['Cache-Control ' ] = implode ( ', ' , array_unique ( array_merge ( $ old_directives , $ new_directives ) ) );
47+ public static function prevent_caching ( $ headers ) {
48+ if ( ! is_blog_installed () ) {
49+ return $ headers ;
50+ }
51+ $ page_ids = array_filter ( array ( wc_get_page_id ( 'cart ' ), wc_get_page_id ( 'checkout ' ), wc_get_page_id ( 'myaccount ' ) ) );
52+
53+ if ( ! is_page ( $ page_ids ) ) {
54+ return $ headers ;
55+ }
56+
57+ self ::set_nocache_constants ();
58+
59+ // These directives are all included in `wp_get_nocache_headers()`, with the exclusion of `no-store` for the sake of bfcache.
60+ $ new_directives = array (
61+ // Prevent caching the response in reverse proxies.
62+ 'private ' ,
63+
64+ // Ensure freshness of the response but without `no-store` so that bfcache won't be disabled.
65+ 'no-cache ' ,
66+ 'must-revalidate ' ,
67+ 'max-age=0 ' ,
68+ );
69+ $ old_directives = array ();
70+ if ( isset ( $ headers ['Cache-Control ' ] ) ) {
71+ $ old_directives = preg_split ( '/\s*,\s*/ ' , $ headers ['Cache-Control ' ] );
6672 }
73+ $ headers ['Cache-Control ' ] = implode ( ', ' , array_unique ( array_merge ( $ old_directives , $ new_directives ) ) );
74+
6775 return $ headers ;
6876 }
6977
@@ -131,21 +139,6 @@ public static function geolocation_ajax_get_location_hash() {
131139 return apply_filters ( 'woocommerce_geolocation_ajax_get_location_hash ' , $ location_hash , $ location , $ customer );
132140 }
133141
134- /**
135- * Prevent caching on certain pages
136- */
137- public static function prevent_caching () {
138- if ( ! is_blog_installed () ) {
139- return ;
140- }
141- $ page_ids = array_filter ( array ( wc_get_page_id ( 'checkout ' ), wc_get_page_id ( 'myaccount ' ) ) );
142-
143- if ( is_page ( $ page_ids ) ) {
144- self ::set_nocache_constants ();
145- nocache_headers ();
146- }
147- }
148-
149142 /**
150143 * When using geolocation via ajax, to bust cache, redirect if the location hash does not equal the querystring.
151144 *
0 commit comments