abstracts
1 month ago
admin
6 days ago
blocks
11 months ago
cli
1 month ago
customizer
4 months ago
data-stores
1 month ago
emails
5 days ago
export
1 year ago
gateways
1 month ago
import
3 months ago
integrations
2 months ago
interfaces
4 months ago
legacy
4 months ago
libraries
1 month ago
log-handlers
1 year ago
payment-tokens
5 years ago
product-usage
1 year ago
queue
4 months ago
react-admin
1 month ago
rest-api
1 month ago
shipping
3 months ago
shortcodes
1 month ago
theme-support
2 years ago
tracks
1 month ago
traits
5 years ago
walkers
5 years ago
wccom-site
1 month ago
widgets
2 months ago
class-wc-ajax.php
1 month ago
class-wc-auth.php
1 year ago
class-wc-autoloader.php
9 months ago
class-wc-background-emailer.php
2 months ago
class-wc-background-updater.php
5 years ago
class-wc-brands-brand-settings-manager.php
1 year ago
class-wc-brands-coupons.php
1 year ago
class-wc-brands.php
1 month ago
class-wc-breadcrumb.php
4 months ago
class-wc-cache-helper.php
1 month ago
class-wc-cart-fees.php
2 years ago
class-wc-cart-session.php
3 months ago
class-wc-cart-totals.php
11 months ago
class-wc-cart.php
1 month ago
class-wc-checkout.php
1 month ago
class-wc-cli.php
10 months ago
class-wc-comments.php
4 months ago
class-wc-countries.php
2 months ago
class-wc-coupon.php
2 months ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
1 year ago
class-wc-customer.php
1 day ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
3 years ago
class-wc-datetime.php
4 years ago
class-wc-deprecated-action-hooks.php
2 years ago
class-wc-deprecated-filter-hooks.php
3 months ago
class-wc-discounts.php
11 months ago
class-wc-download-handler.php
1 month ago
class-wc-emails.php
1 month ago
class-wc-embed.php
1 year ago
class-wc-form-handler.php
3 months ago
class-wc-frontend-scripts.php
2 months ago
class-wc-geo-ip.php
9 months ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
2 months ago
class-wc-https.php
3 years ago
class-wc-install.php
6 days ago
class-wc-integrations.php
5 years ago
class-wc-log-levels.php
2 years ago
class-wc-logger.php
4 months ago
class-wc-meta-data.php
1 month ago
class-wc-order-factory.php
2 months ago
class-wc-order-item-coupon.php
4 years ago
class-wc-order-item-fee.php
5 months ago
class-wc-order-item-meta.php
4 years ago
class-wc-order-item-product.php
1 month ago
class-wc-order-item-shipping.php
5 months ago
class-wc-order-item-tax.php
1 month ago
class-wc-order-item.php
1 month ago
class-wc-order-query.php
1 month ago
class-wc-order-refund.php
1 year ago
class-wc-order.php
1 month ago
class-wc-payment-gateways.php
1 month ago
class-wc-payment-tokens.php
3 years ago
class-wc-post-data.php
2 months ago
class-wc-post-types.php
1 month ago
class-wc-privacy-background-process.php
1 year ago
class-wc-privacy-erasers.php
10 months ago
class-wc-privacy-exporters.php
4 years ago
class-wc-privacy.php
1 year ago
class-wc-product-attribute.php
4 months ago
class-wc-product-download.php
4 months ago
class-wc-product-external.php
1 year ago
class-wc-product-factory.php
3 months ago
class-wc-product-grouped.php
3 months ago
class-wc-product-query.php
4 months ago
class-wc-product-simple.php
11 months ago
class-wc-product-variable.php
1 month ago
class-wc-product-variation.php
1 month ago
class-wc-query.php
1 month ago
class-wc-rate-limiter.php
4 years ago
class-wc-regenerate-images-request.php
3 years ago
class-wc-regenerate-images.php
2 years ago
class-wc-register-wp-admin-settings.php
5 years ago
class-wc-rest-authentication.php
2 years ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
3 months ago
class-wc-shipping-rate.php
1 year ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
7 months ago
class-wc-shipping.php
2 months ago
class-wc-shortcodes.php
1 year ago
class-wc-structured-data.php
1 month ago
class-wc-tax.php
2 months ago
class-wc-template-loader.php
7 months ago
class-wc-tracker.php
1 month ago
class-wc-validation.php
2 years ago
class-wc-webhook.php
1 month ago
class-woocommerce.php
1 day ago
wc-account-functions.php
1 month ago
wc-attribute-functions.php
1 month ago
wc-brands-functions.php
1 year ago
wc-cart-functions.php
1 month ago
wc-conditional-functions.php
11 months ago
wc-core-functions.php
2 months ago
wc-coupon-functions.php
5 months ago
wc-deprecated-functions.php
1 month ago
wc-formatting-functions.php
1 month ago
wc-interactivity-api-functions.php
2 months ago
wc-notice-functions.php
5 months ago
wc-order-functions.php
1 month ago
wc-order-item-functions.php
3 years ago
wc-order-step-logger-functions.php
4 months ago
wc-page-functions.php
1 month ago
wc-product-functions.php
2 months ago
wc-rest-functions.php
7 months ago
wc-stock-functions.php
7 months ago
wc-template-functions.php
1 month ago
wc-template-hooks.php
10 months ago
wc-term-functions.php
2 months ago
wc-update-functions.php
6 days ago
wc-user-functions.php
1 month ago
wc-webhook-functions.php
1 month ago
wc-widget-functions.php
5 years ago
class-wc-cache-helper.php
364 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WC_Cache_Helper class. |
| 4 | * |
| 5 | * @package WooCommerce\Classes |
| 6 | */ |
| 7 | |
| 8 | use Automattic\WooCommerce\Caching\CacheNameSpaceTrait; |
| 9 | use Automattic\WooCommerce\Enums\DefaultCustomerAddress; |
| 10 | |
| 11 | defined( 'ABSPATH' ) || exit; |
| 12 | |
| 13 | /** |
| 14 | * WC_Cache_Helper. |
| 15 | */ |
| 16 | class WC_Cache_Helper { |
| 17 | use CacheNameSpaceTrait; |
| 18 | |
| 19 | /** |
| 20 | * Transients to delete on shutdown. |
| 21 | * |
| 22 | * @var array Array of transient keys. |
| 23 | */ |
| 24 | private static $delete_transients = array(); |
| 25 | |
| 26 | /** |
| 27 | * Hook in methods. |
| 28 | */ |
| 29 | public static function init() { |
| 30 | add_action( 'wp_headers', array( __CLASS__, 'prevent_caching' ), 5 ); // Lower priority than default to facilitate plugins enforcing `no-store` if desired. |
| 31 | add_action( 'shutdown', array( __CLASS__, 'delete_transients_on_shutdown' ), 10 ); |
| 32 | add_action( 'template_redirect', array( __CLASS__, 'geolocation_ajax_redirect' ) ); |
| 33 | add_action( 'wc_ajax_update_order_review', array( __CLASS__, 'update_geolocation_hash' ), 5 ); |
| 34 | add_action( 'admin_notices', array( __CLASS__, 'notices' ) ); |
| 35 | add_action( 'delete_version_transients', array( __CLASS__, 'delete_version_transients' ), 10 ); |
| 36 | add_action( 'clean_term_cache', array( __CLASS__, 'clean_term_cache' ), 10, 2 ); |
| 37 | add_action( 'edit_terms', array( __CLASS__, 'clean_term_cache' ), 10, 2 ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Prevent caching on certain pages. |
| 42 | * |
| 43 | * @since 3.6.0 |
| 44 | * @since 10.1.0 This is now a callback for the `wp_headers` filter as opposed to a callback for the `wp` action. |
| 45 | * |
| 46 | * @param array<string, string> $headers Header names and field values. |
| 47 | * @return array<string, string> Filtered headers. |
| 48 | */ |
| 49 | public static function prevent_caching( $headers ) { |
| 50 | if ( ! is_blog_installed() ) { |
| 51 | return $headers; |
| 52 | } |
| 53 | |
| 54 | // Optimization note: this is the earliest stage at which we can bulk-prime pages data. As a result, we |
| 55 | // prime data for all pages used in is_page checks across the core, even if only a subset is required here. |
| 56 | $pages = array( 'cart', 'checkout', 'coming_soon', 'myaccount', 'shop', 'terms' ); |
| 57 | $woocommerce_page_options = array_map( static fn ( string $page ) => sprintf( 'woocommerce_%s_page_id', $page ), $pages ); |
| 58 | wp_prime_option_caches( $woocommerce_page_options ); |
| 59 | $woocommerce_page_ids = array_combine( $pages, array_map( static fn ( string $page ) => wc_get_page_id( $page ), $pages ) ); |
| 60 | _prime_post_caches( array_values( array_filter( $woocommerce_page_ids ) ), false, false ); |
| 61 | |
| 62 | $page_ids = array_filter( array( $woocommerce_page_ids['cart'], $woocommerce_page_ids['checkout'], $woocommerce_page_ids['myaccount'] ) ); |
| 63 | if ( ! is_page( $page_ids ) ) { |
| 64 | return $headers; |
| 65 | } |
| 66 | |
| 67 | self::set_nocache_constants(); |
| 68 | |
| 69 | // Gather the original Cache-Control directives as well as the nocache ones to merge into one new Cache-Control header. |
| 70 | if ( isset( $headers['Cache-Control'] ) ) { |
| 71 | $old_directives = preg_split( '/\s*,\s*/', trim( $headers['Cache-Control'] ) ); |
| 72 | } else { |
| 73 | $old_directives = array(); |
| 74 | } |
| 75 | $nocache_headers = wp_get_nocache_headers(); |
| 76 | if ( isset( $nocache_headers['Cache-Control'] ) ) { |
| 77 | $new_directives = preg_split( '/\s*,\s*/', trim( $nocache_headers['Cache-Control'] ) ); |
| 78 | } else { |
| 79 | $new_directives = array(); |
| 80 | } |
| 81 | |
| 82 | $headers = array_merge( $headers, $nocache_headers ); |
| 83 | |
| 84 | /* |
| 85 | * If the user is not logged in, remove the `no-store` directive so that bfcache is not blocked for visitors, |
| 86 | * allowing them to benefit from instant back/forward navigations in the storefront. This essentially undoes |
| 87 | * <https://core.trac.wordpress.org/ticket/61942> which seems to have been excessive since the `private` |
| 88 | * directive was already being sent to prevent the page from being cached in a proxy server. |
| 89 | * |
| 90 | * Note that <https://core.trac.wordpress.org/ticket/63636> proposes removing `no-store` for logged-in users as |
| 91 | * well. When that happens, the following if statement can be removed since core would no longer be sending |
| 92 | * `no-store` in the first place. |
| 93 | * |
| 94 | * If a site really wants to enforce the `no-store` directive for some reason, they can do so by making sure |
| 95 | * that the `no-store` directive is added to the `Cache-Control` header via the `wp_headers` filter, for |
| 96 | * example: |
| 97 | * |
| 98 | * add_filter( 'wp_headers', function ( $headers ) { |
| 99 | * if ( isset( $headers['Cache-Control'] ) ) { |
| 100 | * $directives = preg_split( ':\s*,\s*:', trim( $headers['Cache-Control'] ) ); |
| 101 | * if ( in_array( 'private', $directives, true ) ) { |
| 102 | * $headers['Cache-Control'] = join( |
| 103 | * ', ', |
| 104 | * array_unique( |
| 105 | * array_merge( |
| 106 | * $directives, |
| 107 | * array( 'no-store' ) |
| 108 | * ) |
| 109 | * ) |
| 110 | * ); |
| 111 | * } |
| 112 | * } |
| 113 | * return $headers; |
| 114 | * } ); |
| 115 | */ |
| 116 | if ( ! is_user_logged_in() ) { |
| 117 | $new_directives = array_diff( $new_directives, array( 'no-store' ) ); |
| 118 | } |
| 119 | |
| 120 | $headers['Cache-Control'] = implode( ', ', array_unique( array_merge( $old_directives, $new_directives ) ) ); |
| 121 | |
| 122 | return $headers; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Add a transient to delete on shutdown. |
| 127 | * |
| 128 | * @since 3.6.0 |
| 129 | * @param string|array $keys Transient key or keys. |
| 130 | */ |
| 131 | public static function queue_delete_transient( $keys ) { |
| 132 | self::$delete_transients = array_unique( array_merge( is_array( $keys ) ? $keys : array( $keys ), self::$delete_transients ) ); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Transients that don't need to be cleaned right away can be deleted on shutdown to avoid repetition. |
| 137 | * |
| 138 | * @since 3.6.0 |
| 139 | */ |
| 140 | public static function delete_transients_on_shutdown() { |
| 141 | if ( self::$delete_transients ) { |
| 142 | foreach ( self::$delete_transients as $key ) { |
| 143 | delete_transient( $key ); |
| 144 | } |
| 145 | self::$delete_transients = array(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Used to clear layered nav counts based on passed attribute names. |
| 151 | * |
| 152 | * @since 3.6.0 |
| 153 | * @param array $attribute_keys Attribute keys. |
| 154 | */ |
| 155 | public static function invalidate_attribute_count( $attribute_keys ) { |
| 156 | if ( $attribute_keys ) { |
| 157 | foreach ( $attribute_keys as $attribute_key ) { |
| 158 | self::queue_delete_transient( 'wc_layered_nav_counts_' . $attribute_key ); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Get a hash of the customer location. |
| 165 | * |
| 166 | * @return string |
| 167 | */ |
| 168 | public static function geolocation_ajax_get_location_hash() { |
| 169 | $customer = new WC_Customer( 0, true ); |
| 170 | $location = array(); |
| 171 | $location['country'] = $customer->get_billing_country(); |
| 172 | $location['state'] = $customer->get_billing_state(); |
| 173 | $location['postcode'] = $customer->get_billing_postcode(); |
| 174 | $location['city'] = $customer->get_billing_city(); |
| 175 | $location_hash = substr( md5( strtolower( implode( '', $location ) ) ), 0, 12 ); |
| 176 | |
| 177 | /** |
| 178 | * Controls the location hash used in geolocation-based caching. |
| 179 | * |
| 180 | * @since 3.6.0 |
| 181 | * |
| 182 | * @param string $location_hash The hash used for geolocation. |
| 183 | * @param array $location The location/address data. |
| 184 | * @param WC_Customer $customer The current customer object. |
| 185 | */ |
| 186 | return apply_filters( 'woocommerce_geolocation_ajax_get_location_hash', $location_hash, $location, $customer ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * When using geolocation via ajax, to bust cache, redirect if the location hash does not equal the querystring. |
| 191 | * |
| 192 | * This prevents caching of the wrong data for this request. |
| 193 | */ |
| 194 | public static function geolocation_ajax_redirect() { |
| 195 | if ( DefaultCustomerAddress::GEOLOCATION_AJAX === get_option( 'woocommerce_default_customer_address' ) && ! is_checkout() && ! is_cart() && ! is_account_page() && ! is_robots() && ! wp_doing_ajax() && empty( $_POST ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 196 | $location_hash = self::geolocation_ajax_get_location_hash(); |
| 197 | $current_hash = isset( $_GET['v'] ) ? wc_clean( wp_unslash( $_GET['v'] ) ) : ''; // WPCS: sanitization ok, input var ok, CSRF ok. |
| 198 | if ( empty( $current_hash ) || $current_hash !== $location_hash ) { |
| 199 | global $wp; |
| 200 | |
| 201 | $redirect_url = trailingslashit( home_url( $wp->request ) ); |
| 202 | |
| 203 | if ( ! empty( $_SERVER['QUERY_STRING'] ) ) { // WPCS: Input var ok. |
| 204 | $redirect_url = add_query_arg( wp_unslash( $_SERVER['QUERY_STRING'] ), '', $redirect_url ); // WPCS: sanitization ok, Input var ok. |
| 205 | } |
| 206 | |
| 207 | if ( ! get_option( 'permalink_structure' ) ) { |
| 208 | $redirect_url = add_query_arg( $wp->query_string, '', $redirect_url ); |
| 209 | } |
| 210 | |
| 211 | $redirect_url = add_query_arg( 'v', $location_hash, remove_query_arg( array( 'v', 'add-to-cart' ), $redirect_url ) ); |
| 212 | wp_safe_redirect( esc_url_raw( $redirect_url ), 307 ); |
| 213 | exit; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Updates the `woocommerce_geo_hash` cookie, which is used to help ensure we display |
| 220 | * the correct pricing etc to customers, according to their billing country. |
| 221 | * |
| 222 | * Note that: |
| 223 | * |
| 224 | * A) This only sets the cookie if the default customer address is set to "Geolocate (with |
| 225 | * Page Caching Support)". |
| 226 | * |
| 227 | * B) It is hooked into the `wc_ajax_update_order_review` action, which has the benefit of |
| 228 | * ensuring we update the cookie any time the billing country is changed. |
| 229 | */ |
| 230 | public static function update_geolocation_hash() { |
| 231 | if ( DefaultCustomerAddress::GEOLOCATION_AJAX === get_option( 'woocommerce_default_customer_address' ) ) { |
| 232 | wc_setcookie( 'woocommerce_geo_hash', static::geolocation_ajax_get_location_hash(), time() + HOUR_IN_SECONDS ); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Get transient version. |
| 238 | * |
| 239 | * When using transients with unpredictable names, e.g. those containing an md5 |
| 240 | * hash in the name, we need a way to invalidate them all at once. |
| 241 | * |
| 242 | * When using default WP transients we're able to do this with a DB query to |
| 243 | * delete transients manually. |
| 244 | * |
| 245 | * With external cache however, this isn't possible. Instead, this function is used |
| 246 | * to append a unique string (based on time()) to each transient. When transients |
| 247 | * are invalidated, the transient version will increment and data will be regenerated. |
| 248 | * |
| 249 | * Raised in issue https://github.com/woocommerce/woocommerce/issues/5777. |
| 250 | * Adapted from ideas in http://tollmanz.com/invalidation-schemes/. |
| 251 | * |
| 252 | * @param string $group Name for the group of transients we need to invalidate. |
| 253 | * @param boolean $refresh true to force a new version. |
| 254 | * @return string transient version based on time(), 10 digits. |
| 255 | */ |
| 256 | public static function get_transient_version( $group, $refresh = false ) { |
| 257 | $transient_name = $group . '-transient-version'; |
| 258 | $transient_value = get_transient( $transient_name ); |
| 259 | |
| 260 | if ( false === $transient_value || true === $refresh ) { |
| 261 | $transient_value = (string) time(); |
| 262 | |
| 263 | set_transient( $transient_name, $transient_value ); |
| 264 | } |
| 265 | |
| 266 | return $transient_value; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Set constants to prevent caching by some plugins. |
| 271 | * |
| 272 | * @param mixed $return Value to return. Previously hooked into a filter. |
| 273 | * @return mixed |
| 274 | */ |
| 275 | public static function set_nocache_constants( $return = true ) { |
| 276 | wc_maybe_define_constant( 'DONOTCACHEPAGE', true ); |
| 277 | wc_maybe_define_constant( 'DONOTCACHEOBJECT', true ); |
| 278 | wc_maybe_define_constant( 'DONOTCACHEDB', true ); |
| 279 | return $return; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Notices function. |
| 284 | */ |
| 285 | public static function notices() { |
| 286 | if ( ! function_exists( 'w3tc_pgcache_flush' ) || ! function_exists( 'w3_instance' ) ) { |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | $config = w3_instance( 'W3_Config' ); |
| 291 | $enabled = $config->get_integer( 'dbcache.enabled' ); |
| 292 | $settings = array_map( 'trim', $config->get_array( 'dbcache.reject.sql' ) ); |
| 293 | |
| 294 | if ( $enabled && ! in_array( '_wc_session_', $settings, true ) ) { |
| 295 | ?> |
| 296 | <div class="error"> |
| 297 | <p> |
| 298 | <?php |
| 299 | /* translators: 1: key 2: URL */ |
| 300 | echo wp_kses_post( sprintf( __( 'In order for <strong>database caching</strong> to work with WooCommerce you must add %1$s to the "Ignored Query Strings" option in <a href="%2$s">W3 Total Cache settings</a>.', 'woocommerce' ), '<code>_wc_session_</code>', esc_url( admin_url( 'admin.php?page=w3tc_dbcache' ) ) ) ); |
| 301 | ?> |
| 302 | </p> |
| 303 | </div> |
| 304 | <?php |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Clean term caches added by WooCommerce. |
| 310 | * |
| 311 | * @since 3.3.4 |
| 312 | * @param array|int $ids Array of ids or single ID to clear cache for. |
| 313 | * @param string $taxonomy Taxonomy name. |
| 314 | */ |
| 315 | public static function clean_term_cache( $ids, $taxonomy ) { |
| 316 | if ( 'product_cat' === $taxonomy ) { |
| 317 | $ids = is_array( $ids ) ? $ids : array( $ids ); |
| 318 | |
| 319 | $clear_ids = array( 0 ); |
| 320 | |
| 321 | foreach ( $ids as $id ) { |
| 322 | $clear_ids[] = $id; |
| 323 | $clear_ids = array_merge( $clear_ids, get_ancestors( $id, 'product_cat', 'taxonomy' ) ); |
| 324 | } |
| 325 | |
| 326 | $clear_ids = array_unique( $clear_ids ); |
| 327 | |
| 328 | foreach ( $clear_ids as $id ) { |
| 329 | wp_cache_delete( 'product-category-hierarchy-' . $id, 'product_cat' ); |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * When the transient version increases, this is used to remove all past transients to avoid filling the DB. |
| 336 | * |
| 337 | * Note; this only works on transients appended with the transient version, and when object caching is not being used. |
| 338 | * |
| 339 | * @deprecated 3.6.0 Adjusted transient usage to include versions within the transient values, making this cleanup obsolete. |
| 340 | * @since 2.3.10 |
| 341 | * @param string $version Version of the transient to remove. |
| 342 | */ |
| 343 | public static function delete_version_transients( $version = '' ) { |
| 344 | if ( ! wp_using_ext_object_cache() && ! empty( $version ) ) { |
| 345 | global $wpdb; |
| 346 | |
| 347 | $limit = apply_filters( 'woocommerce_delete_version_transients_limit', 1000 ); |
| 348 | |
| 349 | if ( ! $limit ) { |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | $affected = $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s LIMIT %d;", '\_transient\_%' . $version, $limit ) ); // WPCS: cache ok, db call ok. |
| 354 | |
| 355 | // If affected rows is equal to limit, there are more rows to delete. Delete in 30 secs. |
| 356 | if ( $affected === $limit ) { |
| 357 | wp_schedule_single_event( time() + 30, 'delete_version_transients', array( $version ) ); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | WC_Cache_Helper::init(); |
| 364 |