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