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