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