abstracts
3 years ago
admin
3 years ago
blocks
5 years ago
cli
3 years ago
customizer
4 years ago
data-stores
4 years ago
emails
4 years ago
export
4 years ago
gateways
3 years ago
import
4 years ago
integrations
4 years ago
interfaces
4 years ago
legacy
3 years ago
libraries
6 years ago
log-handlers
4 years ago
payment-tokens
5 years ago
queue
4 years ago
react-admin
3 years ago
rest-api
3 years ago
shipping
4 years ago
shortcodes
4 years ago
theme-support
3 years ago
tracks
3 years ago
traits
5 years ago
walkers
5 years ago
wccom-site
5 years ago
widgets
4 years ago
class-wc-ajax.php
4 years ago
class-wc-api.php
4 years ago
class-wc-auth.php
4 years ago
class-wc-autoloader.php
5 years ago
class-wc-background-emailer.php
5 years ago
class-wc-background-updater.php
5 years ago
class-wc-breadcrumb.php
5 years ago
class-wc-cache-helper.php
4 years ago
class-wc-cart-fees.php
5 years ago
class-wc-cart-session.php
4 years ago
class-wc-cart-totals.php
4 years ago
class-wc-cart.php
4 years ago
class-wc-checkout.php
4 years ago
class-wc-cli.php
4 years ago
class-wc-comments.php
3 years ago
class-wc-countries.php
4 years ago
class-wc-coupon.php
4 years ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
4 years ago
class-wc-customer.php
4 years ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
6 years ago
class-wc-datetime.php
4 years ago
class-wc-deprecated-action-hooks.php
8 years ago
class-wc-deprecated-filter-hooks.php
4 years ago
class-wc-discounts.php
3 years ago
class-wc-download-handler.php
4 years ago
class-wc-emails.php
5 years ago
class-wc-embed.php
5 years ago
class-wc-form-handler.php
4 years ago
class-wc-frontend-scripts.php
4 years ago
class-wc-geo-ip.php
4 years ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
4 years ago
class-wc-https.php
4 years ago
class-wc-install.php
3 years ago
class-wc-integrations.php
5 years ago
class-wc-log-levels.php
5 years ago
class-wc-logger.php
4 years ago
class-wc-meta-data.php
4 years ago
class-wc-order-factory.php
5 years ago
class-wc-order-item-coupon.php
4 years ago
class-wc-order-item-fee.php
4 years ago
class-wc-order-item-meta.php
4 years ago
class-wc-order-item-product.php
4 years ago
class-wc-order-item-shipping.php
4 years ago
class-wc-order-item-tax.php
4 years ago
class-wc-order-item.php
4 years ago
class-wc-order-query.php
4 years ago
class-wc-order-refund.php
4 years ago
class-wc-order.php
4 years ago
class-wc-payment-gateways.php
4 years ago
class-wc-payment-tokens.php
5 years ago
class-wc-post-data.php
4 years ago
class-wc-post-types.php
3 years ago
class-wc-privacy-background-process.php
5 years ago
class-wc-privacy-erasers.php
4 years ago
class-wc-privacy-exporters.php
4 years ago
class-wc-privacy.php
4 years ago
class-wc-product-attribute.php
4 years ago
class-wc-product-download.php
4 years ago
class-wc-product-external.php
5 years ago
class-wc-product-factory.php
5 years ago
class-wc-product-grouped.php
8 years ago
class-wc-product-query.php
5 years ago
class-wc-product-simple.php
5 years ago
class-wc-product-variable.php
4 years ago
class-wc-product-variation.php
4 years ago
class-wc-query.php
4 years ago
class-wc-rate-limiter.php
4 years ago
class-wc-regenerate-images-request.php
5 years ago
class-wc-regenerate-images.php
5 years ago
class-wc-register-wp-admin-settings.php
4 years ago
class-wc-rest-authentication.php
5 years ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
4 years ago
class-wc-shipping-rate.php
4 years ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
5 years ago
class-wc-shipping.php
4 years ago
class-wc-shortcodes.php
5 years ago
class-wc-structured-data.php
5 years ago
class-wc-tax.php
4 years ago
class-wc-template-loader.php
4 years ago
class-wc-tracker.php
4 years ago
class-wc-validation.php
5 years ago
class-wc-webhook.php
5 years ago
class-woocommerce.php
3 years ago
wc-account-functions.php
5 years ago
wc-attribute-functions.php
3 years ago
wc-cart-functions.php
4 years ago
wc-conditional-functions.php
4 years ago
wc-core-functions.php
3 years ago
wc-coupon-functions.php
5 years ago
wc-deprecated-functions.php
4 years ago
wc-formatting-functions.php
4 years ago
wc-notice-functions.php
5 years ago
wc-order-functions.php
3 years ago
wc-order-item-functions.php
5 years ago
wc-page-functions.php
5 years ago
wc-product-functions.php
4 years ago
wc-rest-functions.php
3 years ago
wc-stock-functions.php
4 years ago
wc-template-functions.php
3 years ago
wc-template-hooks.php
5 years ago
wc-term-functions.php
4 years ago
wc-update-functions.php
3 years ago
wc-user-functions.php
3 years ago
wc-webhook-functions.php
4 years ago
wc-widget-functions.php
5 years ago
class-wc-cache-helper.php
361 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WC_Cache_Helper class. |
| 4 | * |
| 5 | * @package WooCommerce\Classes |
| 6 | */ |
| 7 | |
| 8 | defined( 'ABSPATH' ) || exit; |
| 9 | |
| 10 | /** |
| 11 | * WC_Cache_Helper. |
| 12 | */ |
| 13 | class WC_Cache_Helper { |
| 14 | |
| 15 | /** |
| 16 | * Transients to delete on shutdown. |
| 17 | * |
| 18 | * @var array Array of transient keys. |
| 19 | */ |
| 20 | private static $delete_transients = array(); |
| 21 | |
| 22 | /** |
| 23 | * Hook in methods. |
| 24 | */ |
| 25 | public static function init() { |
| 26 | add_filter( 'nocache_headers', array( __CLASS__, 'additional_nocache_headers' ), 10 ); |
| 27 | add_action( 'shutdown', array( __CLASS__, 'delete_transients_on_shutdown' ), 10 ); |
| 28 | add_action( 'template_redirect', array( __CLASS__, 'geolocation_ajax_redirect' ) ); |
| 29 | add_action( 'wc_ajax_update_order_review', array( __CLASS__, 'update_geolocation_hash' ), 5 ); |
| 30 | add_action( 'admin_notices', array( __CLASS__, 'notices' ) ); |
| 31 | add_action( 'delete_version_transients', array( __CLASS__, 'delete_version_transients' ), 10 ); |
| 32 | add_action( 'wp', array( __CLASS__, 'prevent_caching' ) ); |
| 33 | add_action( 'clean_term_cache', array( __CLASS__, 'clean_term_cache' ), 10, 2 ); |
| 34 | add_action( 'edit_terms', array( __CLASS__, 'clean_term_cache' ), 10, 2 ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Set additional nocache headers. |
| 39 | * |
| 40 | * @param array $headers Header names and field values. |
| 41 | * @since 3.6.0 |
| 42 | */ |
| 43 | public static function additional_nocache_headers( $headers ) { |
| 44 | global $wp_query; |
| 45 | |
| 46 | $agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 47 | |
| 48 | $set_cache = false; |
| 49 | |
| 50 | /** |
| 51 | * Allow plugins to enable nocache headers. Enabled for Google weblight. |
| 52 | * |
| 53 | * @param bool $enable_nocache_headers Flag indicating whether to add nocache headers. Default: false. |
| 54 | */ |
| 55 | if ( apply_filters( 'woocommerce_enable_nocache_headers', false ) ) { |
| 56 | $set_cache = true; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Enabled for Google weblight. |
| 61 | * |
| 62 | * @see https://support.google.com/webmasters/answer/1061943?hl=en |
| 63 | */ |
| 64 | if ( false !== strpos( $agent, 'googleweblight' ) ) { |
| 65 | // no-transform: Opt-out of Google weblight. https://support.google.com/webmasters/answer/6211428?hl=en. |
| 66 | $set_cache = true; |
| 67 | } |
| 68 | |
| 69 | if ( false !== strpos( $agent, 'Chrome' ) && isset( $wp_query ) && is_cart() ) { |
| 70 | $set_cache = true; |
| 71 | } |
| 72 | |
| 73 | if ( $set_cache ) { |
| 74 | $headers['Cache-Control'] = 'no-transform, no-cache, no-store, must-revalidate'; |
| 75 | } |
| 76 | return $headers; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Add a transient to delete on shutdown. |
| 81 | * |
| 82 | * @since 3.6.0 |
| 83 | * @param string|array $keys Transient key or keys. |
| 84 | */ |
| 85 | public static function queue_delete_transient( $keys ) { |
| 86 | self::$delete_transients = array_unique( array_merge( is_array( $keys ) ? $keys : array( $keys ), self::$delete_transients ) ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Transients that don't need to be cleaned right away can be deleted on shutdown to avoid repetition. |
| 91 | * |
| 92 | * @since 3.6.0 |
| 93 | */ |
| 94 | public static function delete_transients_on_shutdown() { |
| 95 | if ( self::$delete_transients ) { |
| 96 | foreach ( self::$delete_transients as $key ) { |
| 97 | delete_transient( $key ); |
| 98 | } |
| 99 | self::$delete_transients = array(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Used to clear layered nav counts based on passed attribute names. |
| 105 | * |
| 106 | * @since 3.6.0 |
| 107 | * @param array $attribute_keys Attribute keys. |
| 108 | */ |
| 109 | public static function invalidate_attribute_count( $attribute_keys ) { |
| 110 | if ( $attribute_keys ) { |
| 111 | foreach ( $attribute_keys as $attribute_key ) { |
| 112 | self::queue_delete_transient( 'wc_layered_nav_counts_' . $attribute_key ); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Get prefix for use with wp_cache_set. Allows all cache in a group to be invalidated at once. |
| 119 | * |
| 120 | * @param string $group Group of cache to get. |
| 121 | * @return string |
| 122 | */ |
| 123 | public static function get_cache_prefix( $group ) { |
| 124 | // Get cache key - uses cache key wc_orders_cache_prefix to invalidate when needed. |
| 125 | $prefix = wp_cache_get( 'wc_' . $group . '_cache_prefix', $group ); |
| 126 | |
| 127 | if ( false === $prefix ) { |
| 128 | $prefix = microtime(); |
| 129 | wp_cache_set( 'wc_' . $group . '_cache_prefix', $prefix, $group ); |
| 130 | } |
| 131 | |
| 132 | return 'wc_cache_' . $prefix . '_'; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Increment group cache prefix (invalidates cache). |
| 137 | * |
| 138 | * @param string $group Group of cache to clear. |
| 139 | */ |
| 140 | public static function incr_cache_prefix( $group ) { |
| 141 | wc_deprecated_function( 'WC_Cache_Helper::incr_cache_prefix', '3.9.0', 'WC_Cache_Helper::invalidate_cache_group' ); |
| 142 | self::invalidate_cache_group( $group ); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Invalidate cache group. |
| 147 | * |
| 148 | * @param string $group Group of cache to clear. |
| 149 | * @since 3.9.0 |
| 150 | */ |
| 151 | public static function invalidate_cache_group( $group ) { |
| 152 | wp_cache_set( 'wc_' . $group . '_cache_prefix', microtime(), $group ); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Get a hash of the customer location. |
| 157 | * |
| 158 | * @return string |
| 159 | */ |
| 160 | public static function geolocation_ajax_get_location_hash() { |
| 161 | $customer = new WC_Customer( 0, true ); |
| 162 | $location = array(); |
| 163 | $location['country'] = $customer->get_billing_country(); |
| 164 | $location['state'] = $customer->get_billing_state(); |
| 165 | $location['postcode'] = $customer->get_billing_postcode(); |
| 166 | $location['city'] = $customer->get_billing_city(); |
| 167 | return apply_filters( 'woocommerce_geolocation_ajax_get_location_hash', substr( md5( implode( '', $location ) ), 0, 12 ), $location, $customer ); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Prevent caching on certain pages |
| 172 | */ |
| 173 | public static function prevent_caching() { |
| 174 | if ( ! is_blog_installed() ) { |
| 175 | return; |
| 176 | } |
| 177 | $page_ids = array_filter( array( wc_get_page_id( 'cart' ), wc_get_page_id( 'checkout' ), wc_get_page_id( 'myaccount' ) ) ); |
| 178 | |
| 179 | if ( is_page( $page_ids ) ) { |
| 180 | self::set_nocache_constants(); |
| 181 | nocache_headers(); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * When using geolocation via ajax, to bust cache, redirect if the location hash does not equal the querystring. |
| 187 | * |
| 188 | * This prevents caching of the wrong data for this request. |
| 189 | */ |
| 190 | public static function geolocation_ajax_redirect() { |
| 191 | if ( 'geolocation_ajax' === get_option( 'woocommerce_default_customer_address' ) && ! is_checkout() && ! is_cart() && ! is_account_page() && ! wp_doing_ajax() && empty( $_POST ) ) { // WPCS: CSRF ok, input var ok. |
| 192 | $location_hash = self::geolocation_ajax_get_location_hash(); |
| 193 | $current_hash = isset( $_GET['v'] ) ? wc_clean( wp_unslash( $_GET['v'] ) ) : ''; // WPCS: sanitization ok, input var ok, CSRF ok. |
| 194 | if ( empty( $current_hash ) || $current_hash !== $location_hash ) { |
| 195 | global $wp; |
| 196 | |
| 197 | $redirect_url = trailingslashit( home_url( $wp->request ) ); |
| 198 | |
| 199 | if ( ! empty( $_SERVER['QUERY_STRING'] ) ) { // WPCS: Input var ok. |
| 200 | $redirect_url = add_query_arg( wp_unslash( $_SERVER['QUERY_STRING'] ), '', $redirect_url ); // WPCS: sanitization ok, Input var ok. |
| 201 | } |
| 202 | |
| 203 | if ( ! get_option( 'permalink_structure' ) ) { |
| 204 | $redirect_url = add_query_arg( $wp->query_string, '', $redirect_url ); |
| 205 | } |
| 206 | |
| 207 | $redirect_url = add_query_arg( 'v', $location_hash, remove_query_arg( 'v', $redirect_url ) ); |
| 208 | |
| 209 | wp_safe_redirect( esc_url_raw( $redirect_url ), 307 ); |
| 210 | exit; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Updates the `woocommerce_geo_hash` cookie, which is used to help ensure we display |
| 217 | * the correct pricing etc to customers, according to their billing country. |
| 218 | * |
| 219 | * Note that: |
| 220 | * |
| 221 | * A) This only sets the cookie if the default customer address is set to "Geolocate (with |
| 222 | * Page Caching Support)". |
| 223 | * |
| 224 | * B) It is hooked into the `wc_ajax_update_order_review` action, which has the benefit of |
| 225 | * ensuring we update the cookie any time the billing country is changed. |
| 226 | */ |
| 227 | public static function update_geolocation_hash() { |
| 228 | if ( 'geolocation_ajax' === get_option( 'woocommerce_default_customer_address' ) ) { |
| 229 | wc_setcookie( 'woocommerce_geo_hash', static::geolocation_ajax_get_location_hash(), time() + HOUR_IN_SECONDS ); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Get transient version. |
| 235 | * |
| 236 | * When using transients with unpredictable names, e.g. those containing an md5 |
| 237 | * hash in the name, we need a way to invalidate them all at once. |
| 238 | * |
| 239 | * When using default WP transients we're able to do this with a DB query to |
| 240 | * delete transients manually. |
| 241 | * |
| 242 | * With external cache however, this isn't possible. Instead, this function is used |
| 243 | * to append a unique string (based on time()) to each transient. When transients |
| 244 | * are invalidated, the transient version will increment and data will be regenerated. |
| 245 | * |
| 246 | * Raised in issue https://github.com/woocommerce/woocommerce/issues/5777. |
| 247 | * Adapted from ideas in http://tollmanz.com/invalidation-schemes/. |
| 248 | * |
| 249 | * @param string $group Name for the group of transients we need to invalidate. |
| 250 | * @param boolean $refresh true to force a new version. |
| 251 | * @return string transient version based on time(), 10 digits. |
| 252 | */ |
| 253 | public static function get_transient_version( $group, $refresh = false ) { |
| 254 | $transient_name = $group . '-transient-version'; |
| 255 | $transient_value = get_transient( $transient_name ); |
| 256 | |
| 257 | if ( false === $transient_value || true === $refresh ) { |
| 258 | $transient_value = (string) time(); |
| 259 | |
| 260 | set_transient( $transient_name, $transient_value ); |
| 261 | } |
| 262 | |
| 263 | return $transient_value; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Set constants to prevent caching by some plugins. |
| 268 | * |
| 269 | * @param mixed $return Value to return. Previously hooked into a filter. |
| 270 | * @return mixed |
| 271 | */ |
| 272 | public static function set_nocache_constants( $return = true ) { |
| 273 | wc_maybe_define_constant( 'DONOTCACHEPAGE', true ); |
| 274 | wc_maybe_define_constant( 'DONOTCACHEOBJECT', true ); |
| 275 | wc_maybe_define_constant( 'DONOTCACHEDB', true ); |
| 276 | return $return; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Notices function. |
| 281 | */ |
| 282 | public static function notices() { |
| 283 | if ( ! function_exists( 'w3tc_pgcache_flush' ) || ! function_exists( 'w3_instance' ) ) { |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | $config = w3_instance( 'W3_Config' ); |
| 288 | $enabled = $config->get_integer( 'dbcache.enabled' ); |
| 289 | $settings = array_map( 'trim', $config->get_array( 'dbcache.reject.sql' ) ); |
| 290 | |
| 291 | if ( $enabled && ! in_array( '_wc_session_', $settings, true ) ) { |
| 292 | ?> |
| 293 | <div class="error"> |
| 294 | <p> |
| 295 | <?php |
| 296 | /* translators: 1: key 2: URL */ |
| 297 | 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' ) ) ) ); |
| 298 | ?> |
| 299 | </p> |
| 300 | </div> |
| 301 | <?php |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Clean term caches added by WooCommerce. |
| 307 | * |
| 308 | * @since 3.3.4 |
| 309 | * @param array|int $ids Array of ids or single ID to clear cache for. |
| 310 | * @param string $taxonomy Taxonomy name. |
| 311 | */ |
| 312 | public static function clean_term_cache( $ids, $taxonomy ) { |
| 313 | if ( 'product_cat' === $taxonomy ) { |
| 314 | $ids = is_array( $ids ) ? $ids : array( $ids ); |
| 315 | |
| 316 | $clear_ids = array( 0 ); |
| 317 | |
| 318 | foreach ( $ids as $id ) { |
| 319 | $clear_ids[] = $id; |
| 320 | $clear_ids = array_merge( $clear_ids, get_ancestors( $id, 'product_cat', 'taxonomy' ) ); |
| 321 | } |
| 322 | |
| 323 | $clear_ids = array_unique( $clear_ids ); |
| 324 | |
| 325 | foreach ( $clear_ids as $id ) { |
| 326 | wp_cache_delete( 'product-category-hierarchy-' . $id, 'product_cat' ); |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * When the transient version increases, this is used to remove all past transients to avoid filling the DB. |
| 333 | * |
| 334 | * Note; this only works on transients appended with the transient version, and when object caching is not being used. |
| 335 | * |
| 336 | * @deprecated 3.6.0 Adjusted transient usage to include versions within the transient values, making this cleanup obsolete. |
| 337 | * @since 2.3.10 |
| 338 | * @param string $version Version of the transient to remove. |
| 339 | */ |
| 340 | public static function delete_version_transients( $version = '' ) { |
| 341 | if ( ! wp_using_ext_object_cache() && ! empty( $version ) ) { |
| 342 | global $wpdb; |
| 343 | |
| 344 | $limit = apply_filters( 'woocommerce_delete_version_transients_limit', 1000 ); |
| 345 | |
| 346 | if ( ! $limit ) { |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | $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. |
| 351 | |
| 352 | // If affected rows is equal to limit, there are more rows to delete. Delete in 30 secs. |
| 353 | if ( $affected === $limit ) { |
| 354 | wp_schedule_single_event( time() + 30, 'delete_version_transients', array( $version ) ); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | WC_Cache_Helper::init(); |
| 361 |