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
|
|
|
$params = array_filter( $params, fn( $value ) => ! is_null( $value ) && '' !== $value ); // remove null and empty values. |
58
|
|
|
|
59
|
|
|
// legacy keys don't have a fieldmap. |
60
|
|
|
if ( true === $legacy && isset( $params['fieldmap_id'] ) ) { |
61
|
|
|
unset( $params['fieldmap_id'] ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// make the key a string. |
65
|
|
|
$key = implode( '_', $params ); |
66
|
|
|
|
67
|
|
|
// note: the WordPress codex indicates that option names do not need to be escaped. |
68
|
|
|
// see: https://developer.wordpress.org/reference/functions/update_option/. |
69
|
|
|
|
70
|
|
|
return $key; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set individual transient records for sync operations |
75
|
|
|
* |
76
|
|
|
* @param string $operation what is the transient related to? last pull, current pull, merge, delete, etc. |
77
|
|
|
* @param string $object_type the Salesforce object type. |
78
|
|
|
* @param int $fieldmap_id the ID of the specific fieldmap that is running. |
79
|
|
|
* @param mixed $value the value to be saved in the option. |
80
|
|
|
* @param int $expiration whether to expire the transient. |
81
|
|
|
* @return bool $result value of the save operation. |
82
|
|
|
*/ |
83
|
|
|
public function set( $operation, $object_type = '', $fieldmap_id = '', $value = '', $expiration = 0 ) { |
84
|
|
|
// generate the option key parameters. |
85
|
|
|
$params = array( |
86
|
|
|
'operation' => $operation, |
87
|
|
|
'object_type' => $object_type, |
88
|
|
|
'fieldmap_id' => $fieldmap_id, |
89
|
|
|
); |
90
|
|
|
$key = $this->generate_transient_key( $params ); |
91
|
|
|
$value = isset( $value ) ? $value : ''; |
92
|
|
|
|
93
|
|
|
/* |
94
|
|
|
* examples |
95
|
|
|
* object_sync_for_salesforce_pull_last_sync_Contact_1 |
96
|
|
|
* object_sync_for_salesforce_currently_pulling_query_Contact_1 |
97
|
|
|
* object_sync_for_salesforce_pull_merge_last_Contact_1 |
98
|
|
|
* object_sync_for_salesforce_pull_delete_last_Contact_1 |
99
|
|
|
*/ |
100
|
|
|
|
101
|
|
|
$result = set_transient( $key, $value, $expiration ); |
102
|
|
|
|
103
|
|
|
if ( true === $result ) { |
104
|
|
|
$legacy_key = $this->generate_transient_key( $params, true ); |
105
|
|
|
// if the legacy key exists and the keys are not the same, we might need to upgrade. |
106
|
|
|
if ( get_transient( $legacy_key ) && $key !== $legacy_key ) { |
107
|
|
|
$this->legacy_transient_upgrade( $operation, $object_type, $fieldmap_id ); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
return $result; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set individual transient records for sync operations |
115
|
|
|
* |
116
|
|
|
* @param string $operation what is the option related to? last pull, current pull, merge, delete, etc. |
117
|
|
|
* @param string $object_type the Salesforce object type. |
118
|
|
|
* @param int $fieldmap_id the ID of the specific fieldmap that is running. |
119
|
|
|
* @param mixed $value the value to be saved in the option. |
120
|
|
|
* @param int $expiration whether to expire the transient. |
121
|
|
|
* @return bool $result value of the save operation. |
122
|
|
|
* @deprecated this was added in 2.1.0 to upgrade old transient keys, but will be removed in a future version. |
123
|
|
|
*/ |
124
|
|
|
private function legacy_transient_upgrade( $operation, $object_type = '', $fieldmap_id = '', $value = '', $expiration = 0 ) { |
|
|
|
|
125
|
|
|
$result = false; |
126
|
|
|
$legacy_value = $this->legacy_get( $operation, $object_type, $fieldmap_id ); |
|
|
|
|
127
|
|
|
if ( false !== $legacy_value ) { |
128
|
|
|
// generate the option key parameters. |
129
|
|
|
$params = array( |
130
|
|
|
'operation' => $operation, |
131
|
|
|
'object_type' => $object_type, |
132
|
|
|
'fieldmap_id' => $fieldmap_id, |
133
|
|
|
); |
134
|
|
|
$key = $this->generate_transient_key( $params, true ); |
135
|
|
|
$this->add_upgradeable_key( $key ); |
|
|
|
|
136
|
|
|
$result = $this->set( $operation, $object_type, $fieldmap_id, $legacy_value ); |
|
|
|
|
137
|
|
|
if ( true === $result ) { |
138
|
|
|
$this->legacy_delete( $key ); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
return $result; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get individual transient records for sync operations |
146
|
|
|
* |
147
|
|
|
* @param string $operation what is the option related to? last pull, current pull, merge, delete, etc. |
148
|
|
|
* @param string $object_type the WordPress or Salesforce object type. |
149
|
|
|
* @param int $fieldmap_id the ID of the specific fieldmap that is running. |
150
|
|
|
* @param mixed $default the default value for the transient. |
151
|
|
|
* @return mixed $value the value of the item. False if it's empty. |
152
|
|
|
*/ |
153
|
|
|
public function get( $operation, $object_type = '', $fieldmap_id = '', $default = false ) { |
154
|
|
|
// generate the transient key parameters. |
155
|
|
|
$params = array( |
156
|
|
|
'operation' => $operation, |
157
|
|
|
'object_type' => $object_type, |
158
|
|
|
'fieldmap_id' => $fieldmap_id, |
159
|
|
|
); |
160
|
|
|
$key = $this->generate_transient_key( $params ); |
161
|
|
|
$value = get_transient( $key ); |
162
|
|
|
|
163
|
|
|
/* |
164
|
|
|
* examples |
165
|
|
|
* object_sync_for_salesforce_pull_last_sync_Contact_1 |
166
|
|
|
* object_sync_for_salesforce_currently_pulling_query_Contact_1 |
167
|
|
|
* object_sync_for_salesforce_pull_merge_last_Contact_1 |
168
|
|
|
* object_sync_for_salesforce_pull_delete_last_Contact_1 |
169
|
|
|
*/ |
170
|
|
|
|
171
|
|
|
// if the new transient value does exist but it has a default value, try to upgrade the old one. |
172
|
|
|
if ( get_transient( $key ) && $default === $value ) { |
173
|
|
|
$legacy_key = $this->generate_transient_key( $params, true ); |
174
|
|
|
// if the keys are not the same, we might need to upgrade. |
175
|
|
|
if ( $key !== $legacy_key ) { |
176
|
|
|
$this->legacy_transient_upgrade( $operation, $object_type, $fieldmap_id, $value ); |
|
|
|
|
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
return $value; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get legacy named individual transiuent records for sync operations |
184
|
|
|
* |
185
|
|
|
* @param string $operation what is the transient related to? last pull, current pull, merge, delete, etc. |
186
|
|
|
* @param string $object_type the WordPress or Salesforce object type. |
187
|
|
|
* @param int $fieldmap_id the ID of the specific fieldmap that is running. |
188
|
|
|
* @return mixed $value the value of the item. False if it's empty. |
189
|
|
|
* @deprecated this was added in 2.1.0 to upgrade old option keys, but will be removed in a future version. |
190
|
|
|
*/ |
191
|
|
|
public function legacy_get( $operation, $object_type = '', $fieldmap_id ) { |
192
|
|
|
// generate the transient key parameters. |
193
|
|
|
$params = array( |
194
|
|
|
'operation' => $operation, |
195
|
|
|
'object_type' => $object_type, |
196
|
|
|
'fieldmap_id' => $fieldmap_id, |
197
|
|
|
); |
198
|
|
|
$key = $this->generate_transient_key( $params, true ); |
199
|
|
|
$value = get_transient( $key ); |
200
|
|
|
return $value; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Delete the individual transient records for sync operation |
205
|
|
|
* |
206
|
|
|
* @param string $operation what is the transient related to? last pull, current pull, merge, delete, etc. |
207
|
|
|
* @param string $object_type the WordPress or Salesforce object type. |
208
|
|
|
* @param int $fieldmap_id the ID of the specific fieldmap that is running. |
209
|
|
|
* @return bool $result True if successful, false otherwise. |
210
|
|
|
*/ |
211
|
|
|
public function delete( $operation, $object_type = '', $fieldmap_id = '' ) { |
212
|
|
|
// generate the transient key parameters. |
213
|
|
|
$params = array( |
214
|
|
|
'operation' => $operation, |
215
|
|
|
'object_type' => $object_type, |
216
|
|
|
'fieldmap_id' => $fieldmap_id, |
217
|
|
|
); |
218
|
|
|
$key = $this->generate_transient_key( $params ); |
219
|
|
|
$result = delete_transient( $key ); |
220
|
|
|
return $result; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Delete the legacy individual transient records for sync operation |
225
|
|
|
* |
226
|
|
|
* @param string $key the legacy key to delete. |
227
|
|
|
* @return bool $result True if successful, false otherwise. |
228
|
|
|
* @deprecated this was added in 2.1.0 to upgrade old transient keys, but will be removed in a future version. |
229
|
|
|
*/ |
230
|
|
|
public function legacy_delete( $key ) { |
231
|
|
|
$result = delete_transient( $key ); |
232
|
|
|
if ( true === $result ) { |
233
|
|
|
$this->remove_upgradeable_key( $key ); |
|
|
|
|
234
|
|
|
} |
235
|
|
|
return $result; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Add an transient key to the array of upgradeable keys. |
240
|
|
|
* |
241
|
|
|
* @param string $key the key to add to the array. |
242
|
|
|
* @return array $this->upgradeable_keys the array of keys. |
243
|
|
|
* @deprecated this was added in 2.1.0 to upgrade old transient keys, but will be removed in a future version. |
244
|
|
|
*/ |
245
|
|
|
private function add_upgradeable_key( $key ) { |
246
|
|
|
$keys = $this->get_upgradeable_keys(); |
|
|
|
|
247
|
|
|
$keys[] = $key; |
248
|
|
|
$keys = array_unique( $keys ); |
249
|
|
|
$result = update_option( $this->option_prefix . 'upgradeable_keys', $keys ); |
250
|
|
|
if ( true === $result ) { |
251
|
|
|
$this->upgradeable_keys = $keys; |
|
|
|
|
252
|
|
|
return $this->upgradeable_keys; |
|
|
|
|
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Remove a transient key from the array of upgradeable keys. |
258
|
|
|
* |
259
|
|
|
* @param string $key the key to remove from the array. |
260
|
|
|
* @return array $this->upgradeable_keys the array of keys. |
261
|
|
|
* @deprecated this was added in 2.1.0 to upgrade old transient keys, but will be removed in a future version. |
262
|
|
|
*/ |
263
|
|
|
private function remove_upgradeable_key( $key ) { |
264
|
|
|
$keys = $this->get_upgradeable_keys(); |
|
|
|
|
265
|
|
|
$array_key = array_search( $key, $keys, true ); |
266
|
|
|
if ( false !== $array_key ) { |
267
|
|
|
unset( $keys[ $array_key ] ); |
268
|
|
|
} |
269
|
|
|
$result = update_option( $this->option_prefix . 'upgradeable_keys', $keys ); |
270
|
|
|
if ( true === $result ) { |
271
|
|
|
$this->upgradeable_keys = $keys; |
|
|
|
|
272
|
|
|
if ( empty( $keys ) ) { |
273
|
|
|
delete_option( $this->option_prefix . 'upgradeable_keys' ); |
274
|
|
|
} |
275
|
|
|
return $this->upgradeable_keys; |
|
|
|
|
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Get the array of upgradeable keys. |
281
|
|
|
* |
282
|
|
|
* @return array $this->upgradeable_keys the array of keys. |
283
|
|
|
* @deprecated this was added in 2.1.0 to upgrade old transient keys, but will be removed in a future version. |
284
|
|
|
*/ |
285
|
|
|
private function get_upgradeable_keys() { |
286
|
|
|
$keys = get_option( $this->option_prefix . 'upgradeable_keys', array() ); |
287
|
|
|
$keys = array_unique( $keys ); |
|
|
|
|
288
|
|
|
$this->upgradeable_keys = $keys; |
|
|
|
|
289
|
|
|
return $this->upgradeable_keys; |
|
|
|
|
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|
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..