|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Handles getting and setting the transients. |
|
4
|
|
|
* |
|
5
|
|
|
* @class Object_Sync_Sf_Sync_Transients |
|
6
|
|
|
* @package Object_Sync_Salesforce |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
defined( 'ABSPATH' ) || exit; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Object_Sync_Sf_Sync_Transients class. |
|
13
|
|
|
*/ |
|
14
|
|
|
class Object_Sync_Sf_Sync_Transients { |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Current version of the plugin |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
public $version; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The plugin's prefix when saving options to the database |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
public $option_prefix; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Transient keys that can be upgraded |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
* @deprecated this was added in 2.1.0 to upgrade old option keys, but will be removed in a future version. |
|
35
|
|
|
*/ |
|
36
|
|
|
private $upgradeable_keys; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Constructor for transient records class |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct() { |
|
42
|
|
|
$this->version = object_sync_for_salesforce()->version; |
|
43
|
|
|
$this->option_prefix = object_sync_for_salesforce()->option_prefix; |
|
44
|
|
|
$this->upgradeable_keys = $this->get_upgradeable_keys(); |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Generate a transient key |
|
50
|
|
|
* |
|
51
|
|
|
* @param array $params the pieces to put together. |
|
52
|
|
|
* @param bool $legacy whether this is a legacy key. This is for deprecated keys and will be removed in a future version. |
|
53
|
|
|
* @return string $key the full transient key. |
|
54
|
|
|
*/ |
|
55
|
|
|
private function generate_transient_key( $params, $legacy = false ) { |
|
56
|
|
|
array_unshift( $params, substr( $this->option_prefix, 0, -1 ) ); // add the prefixes. |
|
57
|
|
|
// remove null and empty values. |
|
58
|
|
|
$params = array_filter( |
|
59
|
|
|
$params, |
|
60
|
|
|
function( $value ) { |
|
61
|
|
|
return ! is_null( $value ) && '' !== $value; |
|
62
|
|
|
} |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
// legacy keys don't have a fieldmap. |
|
66
|
|
|
if ( true === $legacy && isset( $params['fieldmap_id'] ) ) { |
|
67
|
|
|
unset( $params['fieldmap_id'] ); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// make the key a string. |
|
71
|
|
|
$key = implode( '_', $params ); |
|
72
|
|
|
|
|
73
|
|
|
// note: the WordPress codex indicates that option names do not need to be escaped. |
|
74
|
|
|
// see: https://developer.wordpress.org/reference/functions/update_option/. |
|
75
|
|
|
|
|
76
|
|
|
return $key; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Set individual transient records for sync operations |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $operation what is the transient related to? last pull, current pull, merge, delete, etc. |
|
83
|
|
|
* @param string $object_type the Salesforce object type. |
|
84
|
|
|
* @param int $fieldmap_id the ID of the specific fieldmap that is running. |
|
85
|
|
|
* @param mixed $value the value to be saved in the option. |
|
86
|
|
|
* @param int $expiration whether to expire the transient. |
|
87
|
|
|
* @return bool $result value of the save operation. |
|
88
|
|
|
*/ |
|
89
|
|
|
public function set( $operation, $object_type = '', $fieldmap_id = '', $value = '', $expiration = 0 ) { |
|
90
|
|
|
// generate the option key parameters. |
|
91
|
|
|
$params = array( |
|
92
|
|
|
'operation' => $operation, |
|
93
|
|
|
'object_type' => $object_type, |
|
94
|
|
|
'fieldmap_id' => $fieldmap_id, |
|
95
|
|
|
); |
|
96
|
|
|
$key = $this->generate_transient_key( $params ); |
|
97
|
|
|
$value = isset( $value ) ? $value : ''; |
|
98
|
|
|
|
|
99
|
|
|
/* |
|
100
|
|
|
* examples |
|
101
|
|
|
* object_sync_for_salesforce_pull_last_sync_Contact_1 |
|
102
|
|
|
* object_sync_for_salesforce_currently_pulling_query_Contact_1 |
|
103
|
|
|
* object_sync_for_salesforce_pull_merge_last_Contact_1 |
|
104
|
|
|
* object_sync_for_salesforce_pull_delete_last_Contact_1 |
|
105
|
|
|
*/ |
|
106
|
|
|
|
|
107
|
|
|
$result = set_transient( $key, $value, $expiration ); |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
if ( true === $result ) { |
|
110
|
|
|
$legacy_key = $this->generate_transient_key( $params, true ); |
|
111
|
|
|
// if the legacy key exists and the keys are not the same, we might need to upgrade. |
|
112
|
|
|
if ( get_transient( $legacy_key ) && $key !== $legacy_key ) { |
|
|
|
|
|
|
113
|
|
|
$this->legacy_transient_upgrade( $operation, $object_type, $fieldmap_id ); |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
return $result; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Set individual transient records for sync operations |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $operation what is the option related to? last pull, current pull, merge, delete, etc. |
|
123
|
|
|
* @param string $object_type the Salesforce object type. |
|
124
|
|
|
* @param int $fieldmap_id the ID of the specific fieldmap that is running. |
|
125
|
|
|
* @param mixed $value the value to be saved in the option. |
|
126
|
|
|
* @param int $expiration whether to expire the transient. |
|
127
|
|
|
* @return bool $result value of the save operation. |
|
128
|
|
|
* @deprecated this was added in 2.1.0 to upgrade old transient keys, but will be removed in a future version. |
|
129
|
|
|
*/ |
|
130
|
|
|
private function legacy_transient_upgrade( $operation, $object_type = '', $fieldmap_id = '', $value = '', $expiration = 0 ) { |
|
|
|
|
|
|
131
|
|
|
$result = false; |
|
132
|
|
|
$legacy_value = $this->legacy_get( $operation, $object_type, $fieldmap_id ); |
|
|
|
|
|
|
133
|
|
|
if ( false !== $legacy_value ) { |
|
134
|
|
|
// generate the option key parameters. |
|
135
|
|
|
$params = array( |
|
136
|
|
|
'operation' => $operation, |
|
137
|
|
|
'object_type' => $object_type, |
|
138
|
|
|
'fieldmap_id' => $fieldmap_id, |
|
139
|
|
|
); |
|
140
|
|
|
$key = $this->generate_transient_key( $params, true ); |
|
141
|
|
|
$this->add_upgradeable_key( $key ); |
|
|
|
|
|
|
142
|
|
|
$result = $this->set( $operation, $object_type, $fieldmap_id, $legacy_value ); |
|
|
|
|
|
|
143
|
|
|
if ( true === $result ) { |
|
144
|
|
|
$this->legacy_delete( $key ); |
|
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
return $result; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Get individual transient records for sync operations |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $operation what is the option related to? last pull, current pull, merge, delete, etc. |
|
154
|
|
|
* @param string $object_type the WordPress or Salesforce object type. |
|
155
|
|
|
* @param int $fieldmap_id the ID of the specific fieldmap that is running. |
|
156
|
|
|
* @param mixed $default the default value for the transient. |
|
157
|
|
|
* @return mixed $value the value of the item. False if it's empty. |
|
158
|
|
|
*/ |
|
159
|
|
|
public function get( $operation, $object_type = '', $fieldmap_id = '', $default = false ) { |
|
160
|
|
|
// generate the transient key parameters. |
|
161
|
|
|
$params = array( |
|
162
|
|
|
'operation' => $operation, |
|
163
|
|
|
'object_type' => $object_type, |
|
164
|
|
|
'fieldmap_id' => $fieldmap_id, |
|
165
|
|
|
); |
|
166
|
|
|
$key = $this->generate_transient_key( $params ); |
|
167
|
|
|
$value = get_transient( $key ); |
|
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
/* |
|
170
|
|
|
* examples |
|
171
|
|
|
* object_sync_for_salesforce_pull_last_sync_Contact_1 |
|
172
|
|
|
* object_sync_for_salesforce_currently_pulling_query_Contact_1 |
|
173
|
|
|
* object_sync_for_salesforce_pull_merge_last_Contact_1 |
|
174
|
|
|
* object_sync_for_salesforce_pull_delete_last_Contact_1 |
|
175
|
|
|
*/ |
|
176
|
|
|
|
|
177
|
|
|
// if the new transient value does exist but it has a default value, try to upgrade the old one. |
|
178
|
|
|
if ( get_transient( $key ) && $default === $value ) { |
|
179
|
|
|
$legacy_key = $this->generate_transient_key( $params, true ); |
|
180
|
|
|
// if the keys are not the same, we might need to upgrade. |
|
181
|
|
|
if ( $key !== $legacy_key ) { |
|
182
|
|
|
$this->legacy_transient_upgrade( $operation, $object_type, $fieldmap_id, $value ); |
|
|
|
|
|
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
return $value; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Get legacy named individual transiuent records for sync operations |
|
190
|
|
|
* |
|
191
|
|
|
* @param string $operation what is the transient related to? last pull, current pull, merge, delete, etc. |
|
192
|
|
|
* @param string $object_type the WordPress or Salesforce object type. |
|
193
|
|
|
* @param int $fieldmap_id the ID of the specific fieldmap that is running. |
|
194
|
|
|
* @return mixed $value the value of the item. False if it's empty. |
|
195
|
|
|
* @deprecated this was added in 2.1.0 to upgrade old option keys, but will be removed in a future version. |
|
196
|
|
|
*/ |
|
197
|
|
|
public function legacy_get( $operation, $object_type, $fieldmap_id ) { |
|
198
|
|
|
// generate the transient key parameters. |
|
199
|
|
|
$params = array( |
|
200
|
|
|
'operation' => $operation, |
|
201
|
|
|
'object_type' => $object_type, |
|
202
|
|
|
'fieldmap_id' => $fieldmap_id, |
|
203
|
|
|
); |
|
204
|
|
|
$key = $this->generate_transient_key( $params, true ); |
|
205
|
|
|
$value = get_transient( $key ); |
|
|
|
|
|
|
206
|
|
|
return $value; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Delete the individual transient records for sync operation |
|
211
|
|
|
* |
|
212
|
|
|
* @param string $operation what is the transient related to? last pull, current pull, merge, delete, etc. |
|
213
|
|
|
* @param string $object_type the WordPress or Salesforce object type. |
|
214
|
|
|
* @param int $fieldmap_id the ID of the specific fieldmap that is running. |
|
215
|
|
|
* @return bool $result True if successful, false otherwise. |
|
216
|
|
|
*/ |
|
217
|
|
|
public function delete( $operation, $object_type = '', $fieldmap_id = '' ) { |
|
218
|
|
|
// generate the transient key parameters. |
|
219
|
|
|
$params = array( |
|
220
|
|
|
'operation' => $operation, |
|
221
|
|
|
'object_type' => $object_type, |
|
222
|
|
|
'fieldmap_id' => $fieldmap_id, |
|
223
|
|
|
); |
|
224
|
|
|
$key = $this->generate_transient_key( $params ); |
|
225
|
|
|
$result = delete_transient( $key ); |
|
|
|
|
|
|
226
|
|
|
return $result; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Delete the legacy individual transient records for sync operation |
|
231
|
|
|
* |
|
232
|
|
|
* @param string $key the legacy key to delete. |
|
233
|
|
|
* @return bool $result True if successful, false otherwise. |
|
234
|
|
|
* @deprecated this was added in 2.1.0 to upgrade old transient keys, but will be removed in a future version. |
|
235
|
|
|
*/ |
|
236
|
|
|
public function legacy_delete( $key ) { |
|
237
|
|
|
$result = delete_transient( $key ); |
|
|
|
|
|
|
238
|
|
|
if ( true === $result ) { |
|
239
|
|
|
$this->remove_upgradeable_key( $key ); |
|
|
|
|
|
|
240
|
|
|
} |
|
241
|
|
|
return $result; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Add an transient key to the array of upgradeable keys. |
|
246
|
|
|
* |
|
247
|
|
|
* @param string $key the key to add to the array. |
|
248
|
|
|
* @return array $this->upgradeable_keys the array of keys. |
|
249
|
|
|
* @deprecated this was added in 2.1.0 to upgrade old transient keys, but will be removed in a future version. |
|
250
|
|
|
*/ |
|
251
|
|
|
private function add_upgradeable_key( $key ) { |
|
252
|
|
|
$keys = $this->get_upgradeable_keys(); |
|
|
|
|
|
|
253
|
|
|
$keys[] = $key; |
|
254
|
|
|
$keys = array_unique( $keys ); |
|
255
|
|
|
$result = update_option( $this->option_prefix . 'upgradeable_keys', $keys ); |
|
|
|
|
|
|
256
|
|
|
if ( true === $result ) { |
|
257
|
|
|
$this->upgradeable_keys = $keys; |
|
|
|
|
|
|
258
|
|
|
return $this->upgradeable_keys; |
|
|
|
|
|
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Remove a transient key from the array of upgradeable keys. |
|
264
|
|
|
* |
|
265
|
|
|
* @param string $key the key to remove from the array. |
|
266
|
|
|
* @return array $this->upgradeable_keys the array of keys. |
|
267
|
|
|
* @deprecated this was added in 2.1.0 to upgrade old transient keys, but will be removed in a future version. |
|
268
|
|
|
*/ |
|
269
|
|
|
private function remove_upgradeable_key( $key ) { |
|
270
|
|
|
$keys = $this->get_upgradeable_keys(); |
|
|
|
|
|
|
271
|
|
|
$array_key = array_search( $key, $keys, true ); |
|
272
|
|
|
if ( false !== $array_key ) { |
|
273
|
|
|
unset( $keys[ $array_key ] ); |
|
274
|
|
|
} |
|
275
|
|
|
$result = update_option( $this->option_prefix . 'upgradeable_keys', $keys ); |
|
|
|
|
|
|
276
|
|
|
if ( true === $result ) { |
|
277
|
|
|
$this->upgradeable_keys = $keys; |
|
|
|
|
|
|
278
|
|
|
if ( empty( $keys ) ) { |
|
279
|
|
|
delete_option( $this->option_prefix . 'upgradeable_keys' ); |
|
|
|
|
|
|
280
|
|
|
} |
|
281
|
|
|
return $this->upgradeable_keys; |
|
|
|
|
|
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* Get the array of upgradeable keys. |
|
287
|
|
|
* |
|
288
|
|
|
* @return array $this->upgradeable_keys the array of keys. |
|
289
|
|
|
* @deprecated this was added in 2.1.0 to upgrade old transient keys, but will be removed in a future version. |
|
290
|
|
|
*/ |
|
291
|
|
|
private function get_upgradeable_keys() { |
|
292
|
|
|
$keys = get_option( $this->option_prefix . 'upgradeable_keys', array() ); |
|
|
|
|
|
|
293
|
|
|
$keys = array_unique( $keys ); |
|
294
|
|
|
$this->upgradeable_keys = $keys; |
|
|
|
|
|
|
295
|
|
|
return $this->upgradeable_keys; |
|
|
|
|
|
|
296
|
|
|
} |
|
297
|
|
|
} |
|
298
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..