WC_Cache_Helper

WC_Cache_Helper class.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-cache-helper.php  
  1. class WC_Cache_Helper { 
  2.  
  3. /** 
  4. * Hook in methods. 
  5. */ 
  6. public static function init() { 
  7. add_action( 'template_redirect', array( __CLASS__, 'geolocation_ajax_redirect' ) ); 
  8. add_action( 'before_woocommerce_init', array( __CLASS__, 'prevent_caching' ) ); 
  9. add_action( 'admin_notices', array( __CLASS__, 'notices' ) ); 
  10. add_action( 'delete_version_transients', array( __CLASS__, 'delete_version_transients' ) ); 
  11.  
  12. /** 
  13. * Get prefix for use with wp_cache_set. Allows all cache in a group to be invalidated at once. 
  14. * @param string $group 
  15. * @return string 
  16. */ 
  17. public static function get_cache_prefix( $group ) { 
  18. // Get cache key - uses cache key wc_orders_cache_prefix to invalidate when needed 
  19. $prefix = wp_cache_get( 'wc_' . $group . '_cache_prefix', $group ); 
  20.  
  21. if ( false === $prefix ) { 
  22. $prefix = 1; 
  23. wp_cache_set( 'wc_' . $group . '_cache_prefix', $prefix, $group ); 
  24.  
  25. return 'wc_cache_' . $prefix . '_'; 
  26.  
  27. /** 
  28. * Increment group cache prefix (invalidates cache). 
  29. * @param string $group 
  30. */ 
  31. public static function incr_cache_prefix( $group ) { 
  32. wp_cache_incr( 'wc_' . $group . '_cache_prefix', 1, $group ); 
  33.  
  34. /** 
  35. * Get a hash of the customer location. 
  36. * @return string 
  37. */ 
  38. public static function geolocation_ajax_get_location_hash() { 
  39. $customer = new WC_Customer( 0, true ); 
  40. $location = array(); 
  41. $location['country'] = $customer->get_billing_country(); 
  42. $location['state'] = $customer->get_billing_state(); 
  43. $location['postcode'] = $customer->get_billing_postcode(); 
  44. $location['city'] = $customer->get_billing_city(); 
  45. return substr( md5( implode( '', $location ) ), 0, 12 ); 
  46.  
  47. /** 
  48. * When using geolocation via ajax, to bust cache, redirect if the location hash does not equal the querystring. 
  49. * This prevents caching of the wrong data for this request. 
  50. */ 
  51. public static function geolocation_ajax_redirect() { 
  52. if ( 'geolocation_ajax' === get_option( 'woocommerce_default_customer_address' ) && ! is_checkout() && ! is_cart() && ! is_account_page() && ! is_ajax() && empty( $_POST ) ) { 
  53. $location_hash = self::geolocation_ajax_get_location_hash(); 
  54. $current_hash = isset( $_GET['v'] ) ? wc_clean( $_GET['v'] ) : ''; 
  55. if ( empty( $current_hash ) || $current_hash !== $location_hash ) { 
  56. global $wp; 
  57.  
  58. $redirect_url = trailingslashit( home_url( $wp->request ) ); 
  59.  
  60. if ( ! empty( $_SERVER['QUERY_STRING'] ) ) { 
  61. $redirect_url = add_query_arg( $_SERVER['QUERY_STRING'], '', $redirect_url ); 
  62.  
  63. if ( ! get_option( 'permalink_structure' ) ) { 
  64. $redirect_url = add_query_arg( $wp->query_string, '', $redirect_url ); 
  65.  
  66. $redirect_url = add_query_arg( 'v', $location_hash, remove_query_arg( 'v', $redirect_url ) ); 
  67.  
  68. wp_safe_redirect( esc_url_raw( $redirect_url ), 307 ); 
  69. exit; 
  70.  
  71. /** 
  72. * Get transient version. 
  73. * When using transients with unpredictable names, e.g. those containing an md5. 
  74. * hash in the name, we need a way to invalidate them all at once. 
  75. * When using default WP transients we're able to do this with a DB query to. 
  76. * delete transients manually. 
  77. * With external cache however, this isn't possible. Instead, this function is used. 
  78. * to append a unique string (based on time()) to each transient. When transients. 
  79. * are invalidated, the transient version will increment and data will be regenerated. 
  80. * Raised in issue https://github.com/woocommerce/woocommerce/issues/5777. 
  81. * Adapted from ideas in http://tollmanz.com/invalidation-schemes/. 
  82. * @param string $group Name for the group of transients we need to invalidate 
  83. * @param boolean $refresh true to force a new version 
  84. * @return string transient version based on time(), 10 digits 
  85. */ 
  86. public static function get_transient_version( $group, $refresh = false ) { 
  87. $transient_name = $group . '-transient-version'; 
  88. $transient_value = get_transient( $transient_name ); 
  89.  
  90. if ( false === $transient_value || true === $refresh ) { 
  91. self::delete_version_transients( $transient_value ); 
  92. set_transient( $transient_name, $transient_value = time() ); 
  93. return $transient_value; 
  94.  
  95. /** 
  96. * When the transient version increases, this is used to remove all past transients to avoid filling the DB. 
  97. * Note; this only works on transients appended with the transient version, and when object caching is not being used. 
  98. * @since 2.3.10 
  99. */ 
  100. public static function delete_version_transients( $version = '' ) { 
  101. if ( ! wp_using_ext_object_cache() && ! empty( $version ) ) { 
  102. global $wpdb; 
  103.  
  104. $limit = apply_filters( 'woocommerce_delete_version_transients_limit', 1000 ); 
  105. $affected = $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s ORDER BY option_id LIMIT %d;", "\_transient\_%" . $version, $limit ) ); 
  106.  
  107. // If affected rows is equal to limit, there are more rows to delete. Delete in 10 secs. 
  108. if ( $affected === $limit ) { 
  109. wp_schedule_single_event( time() + 10, 'delete_version_transients', array( $version ) ); 
  110.  
  111. /** 
  112. * Get the page name/id for a WC page. 
  113. * @param string $wc_page 
  114. * @return array 
  115. */ 
  116. private static function get_page_uris( $wc_page ) { 
  117. $wc_page_uris = array(); 
  118.  
  119. if ( ( $page_id = wc_get_page_id( $wc_page ) ) && $page_id > 0 && ( $page = get_post( $page_id ) ) ) { 
  120. $wc_page_uris[] = 'p=' . $page_id; 
  121. $wc_page_uris[] = '/' . $page->post_name . '/'; 
  122.  
  123. return $wc_page_uris; 
  124.  
  125. /** 
  126. * Prevent caching on dynamic pages. 
  127. */ 
  128. public static function prevent_caching() { 
  129.  
  130. if ( ! is_blog_installed() ) { 
  131. return; 
  132.  
  133. if ( false === ( $wc_page_uris = get_transient( 'woocommerce_cache_excluded_uris' ) ) ) { 
  134. $wc_page_uris = array_filter( array_merge( self::get_page_uris( 'cart' ), self::get_page_uris( 'checkout' ), self::get_page_uris( 'myaccount' ) ) ); 
  135. set_transient( 'woocommerce_cache_excluded_uris', $wc_page_uris ); 
  136.  
  137. if ( isset( $_GET['download_file'] ) ) { 
  138. self::nocache(); 
  139. } elseif ( is_array( $wc_page_uris ) ) { 
  140. foreach ( $wc_page_uris as $uri ) { 
  141. if ( stristr( trailingslashit( $_SERVER['REQUEST_URI'] ), $uri ) ) { 
  142. self::nocache(); 
  143. break; 
  144.  
  145. /** 
  146. * Set nocache constants and headers. 
  147. * @access private 
  148. */ 
  149. private static function nocache() { 
  150. if ( ! defined( 'DONOTCACHEPAGE' ) ) { 
  151. define( "DONOTCACHEPAGE", true ); 
  152. if ( ! defined( 'DONOTCACHEOBJECT' ) ) { 
  153. define( "DONOTCACHEOBJECT", true ); 
  154. if ( ! defined( 'DONOTCACHEDB' ) ) { 
  155. define( "DONOTCACHEDB", true ); 
  156. nocache_headers(); 
  157.  
  158. /** 
  159. * notices function. 
  160. */ 
  161. public static function notices() { 
  162. if ( ! function_exists( 'w3tc_pgcache_flush' ) || ! function_exists( 'w3_instance' ) ) { 
  163. return; 
  164.  
  165. $config = w3_instance( 'W3_Config' ); 
  166. $enabled = $config->get_integer( 'dbcache.enabled' ); 
  167. $settings = array_map( 'trim', $config->get_array( 'dbcache.reject.sql' ) ); 
  168.  
  169. if ( $enabled && ! in_array( '_wc_session_', $settings ) ) { 
  170. ?> 
  171. <div class="error"> 
  172. <p><?php printf( __( '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>', admin_url( 'admin.php?page=w3tc_dbcache' ) ); ?></p> 
  173. </div> 
  174. <?php