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