Passed
Pull Request — release/2.0.0 (#400)
by Jonathan
04:21
created

Object_Sync_Salesforce::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
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
	 * The salesforce_get_api() helper method result
124
	 * This contains Salesforce API methods and flags.
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
	 * Get the pre-login Salesforce credentials.
306
	 * These depend on the plugin's settings or constants defined in wp-config.php.
307
	 *
308
	 * @return array $login_credentials
309
	 */
310
	private function get_login_credentials() {
311
312
		$consumer_key       = defined( 'OBJECT_SYNC_SF_SALESFORCE_CONSUMER_KEY' ) ? OBJECT_SYNC_SF_SALESFORCE_CONSUMER_KEY : get_option( $this->option_prefix . 'consumer_key', '' );
0 ignored issues
show
Bug introduced by
The constant OBJECT_SYNC_SF_SALESFORCE_CONSUMER_KEY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
313
		$consumer_secret    = defined( 'OBJECT_SYNC_SF_SALESFORCE_CONSUMER_SECRET' ) ? OBJECT_SYNC_SF_SALESFORCE_CONSUMER_SECRET : get_option( $this->option_prefix . 'consumer_secret', '' );
0 ignored issues
show
Bug introduced by
The constant OBJECT_SYNC_SF_SALESFORCE_CONSUMER_SECRET was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
314
		$callback_url       = defined( 'OBJECT_SYNC_SF_SALESFORCE_CALLBACK_URL' ) ? OBJECT_SYNC_SF_SALESFORCE_CALLBACK_URL : get_option( $this->option_prefix . 'callback_url', '' );
0 ignored issues
show
Bug introduced by
The constant OBJECT_SYNC_SF_SALESFORCE_CALLBACK_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
315
		$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', '' );
0 ignored issues
show
Bug introduced by
The constant OBJECT_SYNC_SF_SALESFORCE_LOGIN_BASE_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
316
		$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', '' );
0 ignored issues
show
Bug introduced by
The constant OBJECT_SYNC_SF_SALESFORCE_AUTHORIZE_URL_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
317
		$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', '' );
0 ignored issues
show
Bug introduced by
The constant OBJECT_SYNC_SF_SALESFORCE_TOKEN_URL_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
318
		$api_version        = defined( 'OBJECT_SYNC_SF_SALESFORCE_API_VERSION' ) ? OBJECT_SYNC_SF_SALESFORCE_API_VERSION : get_option( $this->option_prefix . 'api_version', '' );
0 ignored issues
show
Bug introduced by
The constant OBJECT_SYNC_SF_SALESFORCE_API_VERSION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
319
320
		$login_credentials = array(
321
			'consumer_key'     => $consumer_key,
322
			'consumer_secret'  => $consumer_secret,
323
			'callback_url'     => $callback_url,
324
			'login_url'        => $login_base_url,
325
			'authorize_path'   => $authorize_url_path,
326
			'token_path'       => $token_url_path,
327
			'rest_api_version' => $api_version,
328
		);
329
330
		return $login_credentials;
331
332
	}
333
334
	/**
335
	 * Public helper to load the Salesforce API and see if it is authenticated.
336
	 * This is public so other plugins can access the same SF API instance we use.
337
	 *
338
	 * @return array
339
	 */
340
	public function salesforce_get_api() {
341
342
		$soap_available = $this->is_soap_available();
343
		$soap_loaded    = $this->is_soap_loaded();
344
345
		$consumer_key    = $this->login_credentials['consumer_key'];
346
		$consumer_secret = $this->login_credentials['consumer_secret'];
347
		$is_authorized   = false;
348
		$sfapi           = '';
349
		if ( $consumer_key && $consumer_secret ) {
350
			$sfapi = new Object_Sync_Sf_Salesforce();
351
			if ( true === $sfapi->is_authorized() ) {
352
				$is_authorized = true;
353
			}
354
		}
355
356
		return array(
357
			'is_authorized'  => $is_authorized,
358
			'sfapi'          => $sfapi,
359
			'soap_available' => $soap_available,
360
			'soap_loaded'    => $soap_loaded,
361
		);
362
	}
363
364
	/**
365
	 * Load textdomain
366
	 */
367
	public function textdomain() {
368
		load_plugin_textdomain( 'object-sync-for-salesforce', false, dirname( plugin_basename( $this->file ) ) . '/languages/' );
369
	}
370
371
	/**
372
	 * Check the server to see if Soap is available
373
	 *
374
	 * @return bool $is_soap_available
375
	 */
376
	private function is_soap_available() {
377
		$is_soap_available = false;
378
		if ( extension_loaded( 'soap' ) && class_exists( 'SoapClient' ) ) {
379
			$is_soap_available = true;
380
		}
381
		return $is_soap_available;
382
	}
383
384
	/**
385
	 * Check the plugin to see if the Soap option has been enabled and the class has been loaded
386
	 *
387
	 * @return bool $is_soap_loaded
388
	 */
389
	private function is_soap_loaded() {
390
		$is_soap_loaded = false;
391
		if ( false === $this->is_soap_available() ) {
392
			return $is_soap_loaded;
393
		}
394
		$use_soap = filter_var( get_option( 'object_sync_for_salesforce_use_soap', false ), FILTER_VALIDATE_BOOLEAN );
395
		if ( false === $use_soap ) {
396
			return $is_soap_loaded;
397
		}
398
		if ( class_exists( 'Object_Sync_Sf_Salesforce_Soap_Partner' ) ) {
399
			$is_soap_loaded = true;
400
		}
401
		return $is_soap_loaded;
402
	}
403
404
} // end class
405