1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The main plugin class |
4
|
|
|
* |
5
|
|
|
* @class Object_Sync_Salesforce |
6
|
|
|
* @package Object_Sync_Salesforce |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
defined( 'ABSPATH' ) || exit; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Object_Sync_Salesforce class. |
13
|
|
|
*/ |
14
|
|
|
class Object_Sync_Salesforce { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Current version of the plugin |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
public $version; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The main plugin file |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public $file; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Global object of `$wpdb`, the WordPress database |
32
|
|
|
* |
33
|
|
|
* @var object |
34
|
|
|
*/ |
35
|
|
|
public $wpdb; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The plugin's slug so we can include it when necessary |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
public $slug; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The plugin's prefix when saving options to the database |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
public $option_prefix; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Suffix for group name in ActionScheduler |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
public $action_group_suffix; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Array of what classes in the plugin can be scheduled to occur with `wp_cron` events |
60
|
|
|
* |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
public $schedulable_classes; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Legacy property that holds an instance of the plugin class. |
67
|
|
|
* |
68
|
|
|
* @var object |
69
|
|
|
* @deprecated since 2.0.0 |
70
|
|
|
*/ |
71
|
|
|
public static $instance; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Object_Sync_Sf_Queue class |
75
|
|
|
* |
76
|
|
|
* @var object |
77
|
|
|
*/ |
78
|
|
|
public $queue; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Object_Sync_Sf_Activate class |
82
|
|
|
* |
83
|
|
|
* @var object |
84
|
|
|
*/ |
85
|
|
|
private $activated; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Tells us if composer has been autoloaded |
89
|
|
|
* |
90
|
|
|
* @var bool |
91
|
|
|
*/ |
92
|
|
|
private $composer_loaded; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Login credentials for the Salesforce API; comes from wp-config or from the plugin settings |
96
|
|
|
* |
97
|
|
|
* @var array |
98
|
|
|
*/ |
99
|
|
|
public $login_credentials; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Object_Sync_Sf_Logging class |
103
|
|
|
* |
104
|
|
|
* @var object |
105
|
|
|
*/ |
106
|
|
|
public $logging; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Object_Sync_Sf_Mapping class |
110
|
|
|
* |
111
|
|
|
* @var object |
112
|
|
|
*/ |
113
|
|
|
public $mappings; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Object_Sync_Sf_WordPress class |
117
|
|
|
* |
118
|
|
|
* @var object |
119
|
|
|
*/ |
120
|
|
|
public $wordpress; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Object_Sync_Sf_Salesforce class |
124
|
|
|
* This contains Salesforce API methods |
125
|
|
|
* |
126
|
|
|
* @var array |
127
|
|
|
*/ |
128
|
|
|
public $salesforce; |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Object_Sync_Sf_Salesforce_Push class |
132
|
|
|
* |
133
|
|
|
* @var object |
134
|
|
|
*/ |
135
|
|
|
public $push; |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Object_Sync_Sf_Salesforce_Pull class |
139
|
|
|
* |
140
|
|
|
* @var object |
141
|
|
|
*/ |
142
|
|
|
public $pull; |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Object_Sync_Sf_Rest class |
146
|
|
|
* |
147
|
|
|
* @var object |
148
|
|
|
*/ |
149
|
|
|
private $rest; |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* This is our constructor |
153
|
|
|
* |
154
|
|
|
* @param string $version is the plugin version. |
155
|
|
|
* @param string $file is the main plugin file. |
156
|
|
|
* @return void |
157
|
|
|
*/ |
158
|
|
|
public function __construct( $version, $file ) { |
159
|
|
|
|
160
|
|
|
global $wpdb; |
161
|
|
|
|
162
|
|
|
$this->version = $version; |
163
|
|
|
$this->file = $file; |
164
|
|
|
$this->wpdb = $wpdb; |
165
|
|
|
$this->slug = 'object-sync-for-salesforce'; |
166
|
|
|
$this->option_prefix = 'object_sync_for_salesforce_'; |
167
|
|
|
$this->action_group_suffix = '_check_records'; |
168
|
|
|
|
169
|
|
|
$this->schedulable_classes = array( |
170
|
|
|
'salesforce_push' => array( |
171
|
|
|
'label' => 'Push to Salesforce', |
172
|
|
|
'class' => 'Object_Sync_Sf_Salesforce_Push', |
173
|
|
|
'callback' => $this->option_prefix . 'push_record', |
174
|
|
|
), |
175
|
|
|
'salesforce_pull' => array( |
176
|
|
|
'label' => 'Pull from Salesforce', |
177
|
|
|
'class' => 'Object_Sync_Sf_Salesforce_Pull', |
178
|
|
|
'initializer' => $this->option_prefix . 'pull_check_records', |
179
|
|
|
'callback' => $this->option_prefix . 'pull_process_records', |
180
|
|
|
), |
181
|
|
|
); |
182
|
|
|
|
183
|
|
|
// users can modify the list of schedulable classes. |
184
|
|
|
$this->schedulable_classes = apply_filters( $this->option_prefix . 'modify_schedulable_classes', $this->schedulable_classes ); |
|
|
|
|
185
|
|
|
|
186
|
|
|
/* // phpcs:ignore Squiz.PHP.CommentedOutCode.Found |
187
|
|
|
* example to modify the array of classes by adding one and removing one |
188
|
|
|
* add_filter( 'object_sync_for_salesforce_modify_schedulable_classes', 'modify_schedulable_classes', 10, 1 ); |
189
|
|
|
* function modify_schedulable_classes( $schedulable_classes ) { |
190
|
|
|
* $schedulable_classes = array( |
191
|
|
|
* 'salesforce_push' => array( |
192
|
|
|
* 'label' => 'Push to Salesforce', |
193
|
|
|
* 'class' => 'Object_Sync_Sf_Salesforce_Push', |
194
|
|
|
* 'callback' => 'salesforce_push_sync_rest', |
195
|
|
|
* ), |
196
|
|
|
* 'wordpress' => array( // phpcs:ignore WordPress.WP.CapitalPDangit.Misspelled |
197
|
|
|
* 'label' => 'WordPress', |
198
|
|
|
* 'class' => 'Object_Sync_Sf_WordPress', |
199
|
|
|
* ), |
200
|
|
|
* 'salesforce' => array( |
201
|
|
|
* 'label' => 'Salesforce Authorization', |
202
|
|
|
* 'class' => 'Object_Sync_Sf_Salesforce', |
203
|
|
|
* ), |
204
|
|
|
* ); |
205
|
|
|
* return $schedulable_classes; |
206
|
|
|
* } |
207
|
|
|
*/ |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Load the static $instance property that holds the instance of the class. |
212
|
|
|
* This is preserved for legacy usage, as the same thing exists in the `object_sync_for_salesforce` function. |
213
|
|
|
* |
214
|
|
|
* @return object $plugin |
215
|
|
|
* @deprecated since 2.0.0 |
216
|
|
|
*/ |
217
|
|
|
public static function get_instance() { |
218
|
|
|
|
219
|
|
|
if ( function_exists( 'object_sync_for_salesforce' ) ) { |
220
|
|
|
return object_sync_for_salesforce(); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
static $plugin; |
224
|
|
|
|
225
|
|
|
if ( is_null( $plugin ) ) { |
226
|
|
|
$plugin = new Object_Sync_Salesforce( OBJECT_SYNC_SF_VERSION, OBJECT_SYNC_SF_FILE ); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $plugin; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Initialize the plugin and start the action hooks. |
234
|
|
|
* We run this separately because activate can't run without the right priority. |
235
|
|
|
*/ |
236
|
|
|
public function init() { |
237
|
|
|
|
238
|
|
|
// methods for the ActionScheduler queue. This needs to be loaded early because it is used during activation. |
239
|
|
|
$this->queue = new Object_Sync_Sf_Queue(); |
240
|
|
|
|
241
|
|
|
// methods for activation. |
242
|
|
|
$this->activated = new Object_Sync_Sf_Activate(); |
243
|
|
|
|
244
|
|
|
// action hooks. |
245
|
|
|
$this->add_actions(); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Run non-activation actions. |
250
|
|
|
* We do this on -10 because ActionScheduler has to have access to plugins_loaded with priority of zero. |
251
|
|
|
*/ |
252
|
|
|
private function add_actions() { |
253
|
|
|
// public actions. |
254
|
|
|
add_action( 'plugins_loaded', array( $this, 'run' ), -10 ); |
|
|
|
|
255
|
|
|
add_action( 'plugins_loaded', array( $this, 'textdomain' ) ); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Run the plugin, independent of activation methods. |
260
|
|
|
*/ |
261
|
|
|
public function run() { |
262
|
|
|
|
263
|
|
|
$this->composer_loaded = $this->composer_loaded(); |
264
|
|
|
|
265
|
|
|
$this->login_credentials = $this->get_login_credentials(); |
266
|
|
|
|
267
|
|
|
// methods for deactivation. |
268
|
|
|
$deactivate = new Object_Sync_Sf_Deactivate(); |
269
|
|
|
|
270
|
|
|
// logging methods. |
271
|
|
|
$this->logging = new Object_Sync_Sf_Logging(); |
272
|
|
|
|
273
|
|
|
// methods for fieldmaps and object maps. |
274
|
|
|
$this->mappings = new Object_Sync_Sf_Mapping(); |
275
|
|
|
|
276
|
|
|
// methods for WordPress. |
277
|
|
|
$this->wordpress = new Object_Sync_Sf_WordPress(); |
278
|
|
|
|
279
|
|
|
// methods for calling the Salesforce API. |
280
|
|
|
$this->salesforce = $this->salesforce_get_api(); |
281
|
|
|
|
282
|
|
|
// methods to push to Salesforce. |
283
|
|
|
$this->push = new Object_Sync_Sf_Salesforce_Push(); |
284
|
|
|
|
285
|
|
|
// methods to pull from Salesforce. |
286
|
|
|
$this->pull = new Object_Sync_Sf_Salesforce_Pull(); |
287
|
|
|
|
288
|
|
|
$this->rest = new Object_Sync_Sf_Rest(); |
289
|
|
|
|
290
|
|
|
// admin functionality. |
291
|
|
|
new Object_Sync_Sf_Admin(); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Autoload things from Composer. |
296
|
|
|
* |
297
|
|
|
* @return bool true |
298
|
|
|
*/ |
299
|
|
|
private function composer_loaded() { |
300
|
|
|
require_once plugin_dir_path( $this->file ) . 'vendor/autoload.php'; |
|
|
|
|
301
|
|
|
return true; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Set the Salesforce API version. |
306
|
|
|
* As of version 2.2.0, this is set by the plugin and is not configurable in the interface, though it can be overridden by developer hook. |
307
|
|
|
* Until version 3.0, this value will be overridden by pre-existing settings. |
308
|
|
|
* |
309
|
|
|
* @return array $sf_api_version_data what we know about the API version that is in use. |
310
|
|
|
*/ |
311
|
|
|
public function get_salesforce_api_version() { |
312
|
|
|
$sf_api_version = OBJECT_SYNC_SF_DEFAULT_API_VERSION; |
313
|
|
|
$sf_api_version_data = array( |
314
|
|
|
'rest_api_version' => $sf_api_version, |
315
|
|
|
'using_deprecated_option' => false, |
316
|
|
|
'using_developer_filter' => false, |
317
|
|
|
); |
318
|
|
|
|
319
|
|
|
// check for a deprecated version. if provided, use it. |
320
|
|
|
$deprecated_api_version = $this->check_deprecated_salesforce_api_version( $sf_api_version ); |
|
|
|
|
321
|
|
|
if ( '' !== $deprecated_api_version ) { |
322
|
|
|
$sf_api_version_data['rest_api_version'] = $deprecated_api_version; |
323
|
|
|
$sf_api_version_data['using_deprecated_option'] = true; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
// developers can modify the active API version. if provided, this takes priority. |
327
|
|
|
$developer_api_version = apply_filters( $this->option_prefix . 'modify_salesforce_api_version', $sf_api_version ); |
|
|
|
|
328
|
|
|
if ( $developer_api_version !== $sf_api_version ) { |
329
|
|
|
$sf_api_version_data['rest_api_version'] = $developer_api_version; |
330
|
|
|
$sf_api_version_data['using_developer_filter'] = true; |
331
|
|
|
} |
332
|
|
|
/* // phpcs:ignore Squiz.PHP.CommentedOutCode.Found |
333
|
|
|
* example to change the REST API version by filter |
334
|
|
|
* add_filter( 'object_sync_for_salesforce_modify_salesforce_api_version', 'modify_salesforce_api_version', 10, 1 ); |
335
|
|
|
* function modify_salesforce_api_version( $sf_api_version ) { |
336
|
|
|
* $sf_api_version = '52.0'; |
337
|
|
|
* return $sf_api_version; |
338
|
|
|
* } |
339
|
|
|
*/ |
340
|
|
|
|
341
|
|
|
// return array that includes: 1) what version, 2) if it is a deprecated version, 3) if it is developer overridden. |
342
|
|
|
return $sf_api_version_data; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Check deprecated methods for a Salesforce API version. |
347
|
|
|
* |
348
|
|
|
* @param string $sf_api_version the active Salesforce version. |
349
|
|
|
* @return string $deprecated_api_version the deprecated Salesforce version, if it exists. |
350
|
|
|
* @deprecated and will be removed in version 3.0. |
351
|
|
|
*/ |
352
|
|
|
private function check_deprecated_salesforce_api_version( $sf_api_version ) { |
353
|
|
|
$deprecated_api_version = defined( 'OBJECT_SYNC_SF_SALESFORCE_API_VERSION' ) ? OBJECT_SYNC_SF_SALESFORCE_API_VERSION : ''; |
|
|
|
|
354
|
|
|
// if the constant exists. |
355
|
|
|
if ( '' === $deprecated_api_version ) { |
356
|
|
|
$deprecated_option_key = $this->option_prefix . 'api_version'; |
357
|
|
|
$deprecated_api_option = get_option( $deprecated_option_key, 'not-exists' ); |
|
|
|
|
358
|
|
|
// if the option exists (the value in the database is not not-exists). |
359
|
|
|
if ( 'not-exists' !== $deprecated_api_option ) { |
360
|
|
|
$deprecated_api_version = $deprecated_api_option; |
361
|
|
|
// if it exists, and is set to "" or exists and is is the same as the default version, just delete it. |
362
|
|
|
if ( '' === $deprecated_api_version || $deprecated_api_version === $sf_api_version ) { |
363
|
|
|
$deprecated_api_version = ''; |
364
|
|
|
delete_option( $deprecated_option_key ); |
|
|
|
|
365
|
|
|
} |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
return $deprecated_api_version; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Get the pre-login Salesforce credentials. |
373
|
|
|
* These depend on the plugin's settings or constants defined in wp-config.php. |
374
|
|
|
* |
375
|
|
|
* @return array $login_credentials |
376
|
|
|
*/ |
377
|
|
|
private function get_login_credentials() { |
378
|
|
|
|
379
|
|
|
$consumer_key = defined( 'OBJECT_SYNC_SF_SALESFORCE_CONSUMER_KEY' ) ? OBJECT_SYNC_SF_SALESFORCE_CONSUMER_KEY : get_option( $this->option_prefix . 'consumer_key', '' ); |
|
|
|
|
380
|
|
|
$consumer_secret = defined( 'OBJECT_SYNC_SF_SALESFORCE_CONSUMER_SECRET' ) ? OBJECT_SYNC_SF_SALESFORCE_CONSUMER_SECRET : get_option( $this->option_prefix . 'consumer_secret', '' ); |
|
|
|
|
381
|
|
|
$callback_url = defined( 'OBJECT_SYNC_SF_SALESFORCE_CALLBACK_URL' ) ? OBJECT_SYNC_SF_SALESFORCE_CALLBACK_URL : get_option( $this->option_prefix . 'callback_url', '' ); |
|
|
|
|
382
|
|
|
$login_base_url = defined( 'OBJECT_SYNC_SF_SALESFORCE_LOGIN_BASE_URL' ) ? OBJECT_SYNC_SF_SALESFORCE_LOGIN_BASE_URL : get_option( $this->option_prefix . 'login_base_url', '' ); |
|
|
|
|
383
|
|
|
$authorize_url_path = defined( 'OBJECT_SYNC_SF_SALESFORCE_AUTHORIZE_URL_PATH' ) ? OBJECT_SYNC_SF_SALESFORCE_AUTHORIZE_URL_PATH : get_option( $this->option_prefix . 'authorize_url_path', '' ); |
|
|
|
|
384
|
|
|
$token_url_path = defined( 'OBJECT_SYNC_SF_SALESFORCE_TOKEN_URL_PATH' ) ? OBJECT_SYNC_SF_SALESFORCE_TOKEN_URL_PATH : get_option( $this->option_prefix . 'token_url_path', '' ); |
|
|
|
|
385
|
|
|
$sf_api_version_data = $this->get_salesforce_api_version(); |
386
|
|
|
|
387
|
|
|
$login_credentials = array( |
388
|
|
|
'consumer_key' => $consumer_key, |
389
|
|
|
'consumer_secret' => $consumer_secret, |
390
|
|
|
'callback_url' => $callback_url, |
391
|
|
|
'login_url' => $login_base_url, |
392
|
|
|
'authorize_path' => $authorize_url_path, |
393
|
|
|
'token_path' => $token_url_path, |
394
|
|
|
); |
395
|
|
|
$login_credentials = array_merge( $login_credentials, $sf_api_version_data ); |
396
|
|
|
return $login_credentials; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Public helper to load the Salesforce API and see if it is authenticated. |
401
|
|
|
* This is public so other plugins can access the same SF API instance we use. |
402
|
|
|
* |
403
|
|
|
* @return array |
404
|
|
|
*/ |
405
|
|
|
public function salesforce_get_api() { |
406
|
|
|
|
407
|
|
|
$soap_available = $this->is_soap_available(); |
408
|
|
|
$soap_loaded = $this->is_soap_loaded(); |
409
|
|
|
|
410
|
|
|
$consumer_key = $this->login_credentials['consumer_key']; |
411
|
|
|
$consumer_secret = $this->login_credentials['consumer_secret']; |
412
|
|
|
$is_authorized = false; |
413
|
|
|
$sfapi = ''; |
414
|
|
|
if ( $consumer_key && $consumer_secret ) { |
415
|
|
|
$sfapi = new Object_Sync_Sf_Salesforce(); |
416
|
|
|
if ( true === $sfapi->is_authorized() ) { |
417
|
|
|
$is_authorized = true; |
418
|
|
|
} |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
return array( |
422
|
|
|
'is_authorized' => $is_authorized, |
423
|
|
|
'sfapi' => $sfapi, |
424
|
|
|
'soap_available' => $soap_available, |
425
|
|
|
'soap_loaded' => $soap_loaded, |
426
|
|
|
); |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* Load textdomain |
431
|
|
|
*/ |
432
|
|
|
public function textdomain() { |
433
|
|
|
load_plugin_textdomain( 'object-sync-for-salesforce', false, dirname( plugin_basename( $this->file ) ) . '/languages/' ); |
|
|
|
|
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Check the server to see if Soap is available |
438
|
|
|
* |
439
|
|
|
* @return bool $is_soap_available |
440
|
|
|
*/ |
441
|
|
|
private function is_soap_available() { |
442
|
|
|
$is_soap_available = false; |
443
|
|
|
if ( extension_loaded( 'soap' ) && class_exists( 'SoapClient' ) ) { |
444
|
|
|
$is_soap_available = true; |
445
|
|
|
} |
446
|
|
|
return $is_soap_available; |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
/** |
450
|
|
|
* Check the plugin to see if the Soap option has been enabled and the class has been loaded |
451
|
|
|
* |
452
|
|
|
* @return bool $is_soap_loaded |
453
|
|
|
*/ |
454
|
|
|
private function is_soap_loaded() { |
455
|
|
|
$is_soap_loaded = false; |
456
|
|
|
if ( false === $this->is_soap_available() ) { |
457
|
|
|
return $is_soap_loaded; |
458
|
|
|
} |
459
|
|
|
$use_soap = filter_var( get_option( 'object_sync_for_salesforce_use_soap', false ), FILTER_VALIDATE_BOOLEAN ); |
|
|
|
|
460
|
|
|
if ( false === $use_soap ) { |
461
|
|
|
return $is_soap_loaded; |
462
|
|
|
} |
463
|
|
|
if ( class_exists( 'Object_Sync_Sf_Salesforce_Soap_Partner' ) ) { |
464
|
|
|
$is_soap_loaded = true; |
465
|
|
|
} |
466
|
|
|
return $is_soap_loaded; |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
} // end class |
470
|
|
|
|