1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Store all theme/plugin transients as an array in one WordPress transient |
4
|
|
|
* |
5
|
|
|
* @class Object_Sync_Sf_WordPress_Transient |
6
|
|
|
* @package Object_Sync_Salesforce |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
defined( 'ABSPATH' ) || exit; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Object_Sync_Sf_WordPress_Transient class. |
13
|
|
|
*/ |
14
|
|
|
class Object_Sync_Sf_WordPress_Transient { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Name of the field that lists the cache keys |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
public $name; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Prefix for plugin cache keys |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public $cache_prefix; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor which sets cache options and the name of the field that lists this plugin's cache keys. |
32
|
|
|
* |
33
|
|
|
* @param string $name The name of the field that lists all cache keys. |
34
|
|
|
*/ |
35
|
|
|
public function __construct( $name ) { |
36
|
|
|
$this->name = $name; |
37
|
|
|
$this->cache_prefix = esc_sql( 'sfwp_' ); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the transient that lists all the other transients for this plugin. |
42
|
|
|
* |
43
|
|
|
* @return array value of transient. Sets an empty array if the transient returns false. |
44
|
|
|
*/ |
45
|
|
|
public function all_keys() { |
46
|
|
|
$result = get_transient( $this->name ); |
|
|
|
|
47
|
|
|
if ( false === $result ) { |
48
|
|
|
$result = array(); |
49
|
|
|
} |
50
|
|
|
return $result; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Set individual transient, and add its key to the list of this plugin's transients. |
55
|
|
|
* |
56
|
|
|
* @param string $cachekey the key for this cache item. |
57
|
|
|
* @param mixed $value the value of the cache item. |
58
|
|
|
* @param int $cache_expiration How long the plugin key cache, and this individual item cache, should last before expiring. |
59
|
|
|
* @return mixed value of transient. False of empty, otherwise array. |
60
|
|
|
*/ |
61
|
|
|
public function set( $cachekey, $value, $cache_expiration = 0 ) { |
62
|
|
|
|
63
|
|
|
$prefix = $this->cache_prefix; |
64
|
|
|
$cachekey = $prefix . $cachekey; |
65
|
|
|
|
66
|
|
|
$keys = $this->all_keys(); |
67
|
|
|
$keys[] = $cachekey; |
68
|
|
|
set_transient( $this->name, $keys, $cache_expiration ); |
|
|
|
|
69
|
|
|
|
70
|
|
|
return set_transient( $cachekey, $value, $cache_expiration ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the individual cache value |
75
|
|
|
* |
76
|
|
|
* @param string $cachekey the key for this cache item. |
77
|
|
|
* @return mixed value of transient. False of empty, otherwise array. |
78
|
|
|
*/ |
79
|
|
|
public function get( $cachekey ) { |
80
|
|
|
$prefix = $this->cache_prefix; |
81
|
|
|
$cachekey = $prefix . $cachekey; |
82
|
|
|
return get_transient( $cachekey ); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Delete the individual cache value |
87
|
|
|
* |
88
|
|
|
* @param string $cachekey the key for this cache item. |
89
|
|
|
* @return bool True if successful, false otherwise. |
90
|
|
|
*/ |
91
|
|
|
public function delete( $cachekey ) { |
92
|
|
|
$prefix = $this->cache_prefix; |
93
|
|
|
$cachekey = $prefix . $cachekey; |
94
|
|
|
return delete_transient( $cachekey ); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Delete the entire cache for this plugin |
99
|
|
|
* |
100
|
|
|
* @return array $result has the success result and how many entries were cleared. |
101
|
|
|
*/ |
102
|
|
|
public function flush() { |
103
|
|
|
$keys = $this->all_keys(); |
104
|
|
|
$success = true; |
105
|
|
|
$count = 0; |
106
|
|
|
if ( ! empty( $keys ) ) { |
107
|
|
|
foreach ( $keys as $key ) { |
108
|
|
|
$success = delete_transient( $key ); |
|
|
|
|
109
|
|
|
$count++; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
$success = delete_transient( $this->name ); |
113
|
|
|
if ( true === $success ) { |
114
|
|
|
$count++; |
115
|
|
|
} |
116
|
|
|
$result = array(); |
117
|
|
|
$result['success'] = $success; |
118
|
|
|
$result['count'] = $count; |
119
|
|
|
return $result; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|