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
|
|
|
* Constructor for option records class |
39
|
|
|
*/ |
40
|
|
|
public function __construct() { |
41
|
|
|
$this->version = object_sync_for_salesforce()->version; |
42
|
|
|
$this->option_prefix = object_sync_for_salesforce()->option_prefix; |
43
|
|
|
|
44
|
|
|
$this->direction = 'pull'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Generate an option key |
49
|
|
|
* |
50
|
|
|
* @param array $params the pieces to put together. |
51
|
|
|
* @param bool $legacy whether this is a legacy key. |
52
|
|
|
* @return string $key the full option key. |
53
|
|
|
*/ |
54
|
|
|
private function generate_option_key( $params, $legacy = false ) { |
55
|
|
|
array_unshift( $params, substr( $this->option_prefix, 0, -1 ), $this->direction ); // add the prefixes. |
56
|
|
|
$params = array_filter( $params, fn( $value ) => ! is_null( $value ) && '' !== $value ); // remove null and empty values. |
57
|
|
|
$key = implode( '_', $params ); // make the key string. |
58
|
|
|
|
59
|
|
|
// allow developers to filter the key. |
60
|
|
|
$key = apply_filters( $this->option_prefix . 'pull_option_key', $key, $params ); |
61
|
|
|
|
62
|
|
|
/* // phpcs:ignore Squiz.PHP.CommentedOutCode.Found |
63
|
|
|
add_filter( 'object_sync_for_salesforce_pull_option_key', 'change_pull_option_key', 10, 2 ); |
64
|
|
|
function change_pull_query( $key, $params ) { |
65
|
|
|
$key = 'my_key_name'; |
66
|
|
|
return $key; |
67
|
|
|
} |
68
|
|
|
*/ |
69
|
|
|
|
70
|
|
|
if ( true === $legacy ) { |
71
|
|
|
// allow developers to filter the legacy key. |
72
|
|
|
$key = apply_filters( $this->option_prefix . 'pull_option_legacy_key', $params ); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$key = esc_attr( $key ); // make sure it's escaped. |
76
|
|
|
|
77
|
|
|
return $key; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Set individual option records for sync operations |
82
|
|
|
* |
83
|
|
|
* @param string $operation what is the option related to? last pull, current pull, merge, delete, etc. |
84
|
|
|
* @param string $object_type the Salesforce object type. |
85
|
|
|
* @param int $fieldmap_id the ID of the specific fieldmap that is running. |
86
|
|
|
* @param mixed $value the value to be saved in the option. |
87
|
|
|
* @param bool $autoload whether to autoload the option value. |
88
|
|
|
* @return bool $result value of the save operation. |
89
|
|
|
*/ |
90
|
|
|
public function set( $operation, $object_type = '', $fieldmap_id = '', $value = '', $autoload = true ) { |
91
|
|
|
// generate the option key parameters. |
92
|
|
|
$params = array( |
93
|
|
|
$operation, |
94
|
|
|
$object_type, |
95
|
|
|
$fieldmap_id, |
96
|
|
|
); |
97
|
|
|
$key = $this->generate_option_key( $params ); |
98
|
|
|
$value = isset( $value ) ? $value : ''; |
99
|
|
|
|
100
|
|
|
/* |
101
|
|
|
* examples |
102
|
|
|
* object_sync_for_salesforce_pull_last_sync_Contact_1 |
103
|
|
|
* object_sync_for_salesforce_currently_pulling_query_Contact_1 |
104
|
|
|
* object_sync_for_salesforce_pull_merge_last_Contact_1 |
105
|
|
|
* object_sync_for_salesforce_pull_delete_last_Contact_1 |
106
|
|
|
*/ |
107
|
|
|
|
108
|
|
|
$result = update_option( $key, $value, $autoload ); |
109
|
|
|
return $result; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get individual option records for sync operations |
114
|
|
|
* |
115
|
|
|
* @param string $operation what is the option related to? last pull, current pull, merge, delete, etc. |
116
|
|
|
* @param string $object_type the Salesforce object type. |
117
|
|
|
* @param int $fieldmap_id the ID of the specific fieldmap that is running. |
118
|
|
|
* @param mixed $default the default value for the option. |
119
|
|
|
* @return mixed $value the value of the item. False if it's empty. |
120
|
|
|
*/ |
121
|
|
|
public function get( $operation, $object_type = '', $fieldmap_id = '', $default = false ) { |
122
|
|
|
// generate the option key parameters. |
123
|
|
|
$params = array( |
124
|
|
|
$operation, |
125
|
|
|
$object_type, |
126
|
|
|
$fieldmap_id, |
127
|
|
|
); |
128
|
|
|
$key = $this->generate_option_key( $params ); |
129
|
|
|
error_log( 'key is ' . $key ); |
130
|
|
|
$value = get_option( $key, $default ); |
131
|
|
|
|
132
|
|
|
/* |
133
|
|
|
* examples |
134
|
|
|
* object_sync_for_salesforce_pull_last_sync_Contact_1 |
135
|
|
|
* object_sync_for_salesforce_currently_pulling_query_Contact_1 |
136
|
|
|
* object_sync_for_salesforce_pull_merge_last_Contact_1 |
137
|
|
|
* object_sync_for_salesforce_pull_delete_last_Contact_1 |
138
|
|
|
*/ |
139
|
|
|
|
140
|
|
|
// if the new option does not exist, try to load the old one and save it as a new value. |
141
|
|
|
if ( false === $value ) { |
142
|
|
|
error_log( 'load the legacy value' ); |
143
|
|
|
$legacy_value = $this->legacy_get( $key, $operation, $object_type, $fieldmap_id ); |
|
|
|
|
144
|
|
|
if ( false !== $legacy_value ) { |
145
|
|
|
error_log( 'there is a legacy value' ); |
146
|
|
|
$value = $legacy_value; |
147
|
|
|
$result = $this->set( $operation, $object_type, $fieldmap_id, $value ); |
|
|
|
|
148
|
|
|
if ( true === $result ) { |
149
|
|
|
error_log( 'delete the legacy value' ); |
150
|
|
|
$this->legacy_delete( $operation, $object_type ); |
151
|
|
|
} |
152
|
|
|
} else { |
153
|
|
|
error_log( 'there is not a legacy value' ); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $value; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get legacy named 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
|
|
|
* @return mixed $value the value of the item. False if it's empty. |
167
|
|
|
*/ |
168
|
|
|
public function legacy_get( $operation, $object_type = '', $fieldmap_id ) { |
169
|
|
|
// generate the option key parameters. |
170
|
|
|
$params = array( |
171
|
|
|
$operation, |
172
|
|
|
$object_type, |
173
|
|
|
$fieldmap_id, |
174
|
|
|
); |
175
|
|
|
$key = $this->generate_option_key( $params, true ); |
176
|
|
|
$value = get_option( $key, false ); |
177
|
|
|
|
178
|
|
|
return $value; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Delete the individual option records for sync operation |
183
|
|
|
* |
184
|
|
|
* @param string $operation what is the option related to? last pull, current pull, merge, delete, etc. |
185
|
|
|
* @param string $object_type the Salesforce object type. |
186
|
|
|
* @param int $fieldmap_id the ID of the specific fieldmap that is running. |
187
|
|
|
* @return bool $result True if successful, false otherwise. |
188
|
|
|
*/ |
189
|
|
|
public function delete( $operation, $object_type = '', $fieldmap_id = '' ) { |
190
|
|
|
// generate the option key parameters. |
191
|
|
|
$params = array( |
192
|
|
|
$operation, |
193
|
|
|
$object_type, |
194
|
|
|
$fieldmap_id, |
195
|
|
|
); |
196
|
|
|
$key = $this->generate_option_key( $params ); |
197
|
|
|
|
198
|
|
|
$result = delete_option( $key ); |
199
|
|
|
return $result; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Delete the legacy individual option records for sync operation |
204
|
|
|
* |
205
|
|
|
* @param string $operation what is the option related to? last pull, current pull, merge, delete, etc. |
206
|
|
|
* @param string $object_type the Salesforce object type. |
207
|
|
|
* @param int $fieldmap_id the ID of the specific fieldmap that is running. |
208
|
|
|
* @return bool $result True if successful, false otherwise. |
209
|
|
|
*/ |
210
|
|
|
public function legacy_delete( $operation, $object_type = '', $fieldmap_id = '' ) { |
211
|
|
|
// generate the option key parameters. |
212
|
|
|
$params = array( |
213
|
|
|
$operation, |
214
|
|
|
$object_type, |
215
|
|
|
$fieldmap_id, |
216
|
|
|
); |
217
|
|
|
$key = $this->generate_option_key( $params, true ); |
218
|
|
|
|
219
|
|
|
$result = delete_option( $key ); |
220
|
|
|
return $result; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Delete the entire set of current sync data |
225
|
|
|
* |
226
|
|
|
* @return array $result has the success result and how many values were cleared. |
227
|
|
|
*/ |
228
|
|
|
public function flush() { |
229
|
|
|
$keys = $this->all_keys(); |
|
|
|
|
230
|
|
|
$success = true; |
231
|
|
|
$count = 0; |
232
|
|
|
if ( ! empty( $keys ) ) { |
233
|
|
|
foreach ( $keys as $key ) { |
234
|
|
|
$success = delete_transient( $key ); |
235
|
|
|
$count++; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
$success = delete_transient( $this->name ); |
|
|
|
|
239
|
|
|
if ( true === $success ) { |
240
|
|
|
$count++; |
241
|
|
|
} |
242
|
|
|
$result = array(); |
243
|
|
|
$result['success'] = $success; |
244
|
|
|
$result['count'] = $count; |
245
|
|
|
return $result; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|