|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* GetPaid_Cache_Helper class. |
|
4
|
|
|
* |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
defined( 'ABSPATH' ) || exit; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* GetPaid_Cache_Helper. |
|
11
|
|
|
*/ |
|
12
|
|
|
class GetPaid_Cache_Helper { |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Transients to delete on shutdown. |
|
16
|
|
|
* |
|
17
|
|
|
* @var array Array of transient keys. |
|
18
|
|
|
*/ |
|
19
|
|
|
private static $delete_transients = array(); |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Hook in methods. |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function init() { |
|
25
|
|
|
add_action( 'shutdown', array( __CLASS__, 'delete_transients_on_shutdown' ), 10 ); |
|
26
|
|
|
add_action( 'wp', array( __CLASS__, 'prevent_caching' ) ); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Add a transient to delete on shutdown. |
|
31
|
|
|
* |
|
32
|
|
|
* @since 1.0.19 |
|
33
|
|
|
* @param string|array $keys Transient key or keys. |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function queue_delete_transient( $keys ) { |
|
36
|
|
|
self::$delete_transients = array_unique( array_merge( is_array( $keys ) ? $keys : array( $keys ), self::$delete_transients ) ); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Transients that don't need to be cleaned right away can be deleted on shutdown to avoid repetition. |
|
41
|
|
|
* |
|
42
|
|
|
* @since 1.0.19 |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function delete_transients_on_shutdown() { |
|
45
|
|
|
if ( self::$delete_transients ) { |
|
|
|
|
|
|
46
|
|
|
foreach ( self::$delete_transients as $key ) { |
|
47
|
|
|
delete_transient( $key ); |
|
48
|
|
|
} |
|
49
|
|
|
self::$delete_transients = array(); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get prefix for use with wp_cache_set. Allows all cache in a group to be invalidated at once. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $group Group of cache to get. |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function get_cache_prefix( $group ) { |
|
60
|
|
|
// Get cache key. |
|
61
|
|
|
$prefix = wp_cache_get( 'getpaid_' . $group . '_cache_prefix', $group ); |
|
62
|
|
|
|
|
63
|
|
|
if ( false === $prefix ) { |
|
64
|
|
|
$prefix = microtime(); |
|
65
|
|
|
wp_cache_set( 'getpaid_' . $group . '_cache_prefix', $prefix, $group ); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return 'getpaid_cache_' . $prefix . '_'; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Invalidate cache group. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $group Group of cache to clear. |
|
75
|
|
|
* @since 1.0.19 |
|
76
|
|
|
*/ |
|
77
|
|
|
public static function invalidate_cache_group( $group ) { |
|
78
|
|
|
wp_cache_set( 'getpaid_' . $group . '_cache_prefix', microtime(), $group ); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Prevent caching on certain pages |
|
83
|
|
|
*/ |
|
84
|
|
|
public static function prevent_caching() { |
|
85
|
|
|
if ( ! is_blog_installed() ) { |
|
86
|
|
|
return; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if ( wpinv_is_checkout() || wpinv_is_success_page() || wpinv_is_invoice_history_page() || wpinv_is_subscriptions_history_page() ) { |
|
90
|
|
|
self::set_nocache_constants(); |
|
91
|
|
|
nocache_headers(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get transient version. |
|
98
|
|
|
* |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $group Name for the group of transients we need to invalidate. |
|
101
|
|
|
* @param boolean $refresh true to force a new version. |
|
102
|
|
|
* @return string transient version based on time(), 10 digits. |
|
103
|
|
|
*/ |
|
104
|
|
|
public static function get_transient_version( $group, $refresh = false ) { |
|
105
|
|
|
$transient_name = $group . '-transient-version'; |
|
106
|
|
|
$transient_value = get_transient( $transient_name ); |
|
107
|
|
|
|
|
108
|
|
|
if ( false === $transient_value || true === $refresh ) { |
|
109
|
|
|
$transient_value = (string) time(); |
|
110
|
|
|
|
|
111
|
|
|
set_transient( $transient_name, $transient_value ); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $transient_value; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Set constants to prevent caching by some plugins. |
|
119
|
|
|
* |
|
120
|
|
|
* @param mixed $return Value to return. Previously hooked into a filter. |
|
121
|
|
|
* @return mixed |
|
122
|
|
|
*/ |
|
123
|
|
|
public static function set_nocache_constants( $return = true ) { |
|
124
|
|
|
getpaid_maybe_define_constant( 'DONOTCACHEPAGE', true ); |
|
125
|
|
|
getpaid_maybe_define_constant( 'DONOTCACHEOBJECT', true ); |
|
126
|
|
|
getpaid_maybe_define_constant( 'DONOTCACHEDB', true ); |
|
127
|
|
|
return $return; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
GetPaid_Cache_Helper::init(); |
|
133
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.