Passed
Pull Request — release/2.0.0 (#400)
by Jonathan
03:08 queued 18s
created

Object_Sync_Salesforce::add_actions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
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
	 * Login credentials for the Salesforce API; comes from wp-config or from the plugin settings
53
	 *
54
	 * @var array
55
	 */
56
	public $login_credentials;
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
	 * Array of what classes in the plugin can be scheduled to occur with `wp_cron` events
67
	 *
68
	 * @var array
69
	 */
70
	public $queue;
71
72
	/**
73
	 * Tells us if composer has been autoloaded
74
	 *
75
	 * @var bool
76
	 */
77
	private $composer_loaded;
78
79
	/**
80
	 * Object_Sync_Sf_Activate class
81
	 *
82
	 * @var object
83
	 */
84
	private $activated;
85
86
	/**
87
	 * Object_Sync_Sf_Logging class
88
	 *
89
	 * @var object
90
	 */
91
	public $logging;
92
93
	/**
94
	 * Object_Sync_Sf_Mapping class
95
	 *
96
	 * @var object
97
	 */
98
	public $mappings;
99
100
	/**
101
	 * Object_Sync_Sf_WordPress class
102
	 *
103
	 * @var object
104
	 */
105
	public $wordpress;
106
107
	/**
108
	 * Object_Sync_Sf_Salesforce class
109
	 * This contains Salesforce API methods
110
	 *
111
	 * @var array
112
	 */
113
	public $salesforce;
114
115
	/**
116
	 * Object_Sync_Sf_Salesforce_Push class
117
	 *
118
	 * @var object
119
	 */
120
	public $push;
121
122
	/**
123
	 * Object_Sync_Sf_Salesforce_Pull class
124
	 *
125
	 * @var object
126
	 */
127
	public $pull;
128
129
	/**
130
	 * Object_Sync_Sf_Rest class
131
	 *
132
	 * @var object
133
	 */
134
	private $rest;
135
136
	/**
137
	 * Legacy property that holds an instance of the plugin class.
138
	 *
139
	 * @var object
140
	 * @deprecated since 2.0.0
141
	 */
142
	public static $instance;
143
144
	/**
145
	 * This is our constructor
146
	 *
147
	 * @param string $version is the plugin version.
148
	 * @param string $file is the main plugin file.
149
	 * @return void
150
	 */
151
	public function __construct( $version, $file ) {
152
153
		global $wpdb;
154
155
		$this->version             = $version;
156
		$this->file                = $file;
157
		$this->wpdb                = $wpdb;
158
		$this->slug                = 'object-sync-for-salesforce';
159
		$this->option_prefix       = 'object_sync_for_salesforce_';
160
		$this->action_group_suffix = '_check_records';
0 ignored issues
show
Bug Best Practice introduced by
The property action_group_suffix does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
161
162
		$this->schedulable_classes = array(
163
			'salesforce_push' => array(
164
				'label'    => 'Push to Salesforce',
165
				'class'    => 'Object_Sync_Sf_Salesforce_Push',
166
				'callback' => $this->option_prefix . 'push_record',
167
			),
168
			'salesforce_pull' => array(
169
				'label'       => 'Pull from Salesforce',
170
				'class'       => 'Object_Sync_Sf_Salesforce_Pull',
171
				'initializer' => $this->option_prefix . 'pull_check_records',
172
				'callback'    => $this->option_prefix . 'pull_process_records',
173
			),
174
		);
175
176
		// users can modify the list of schedulable classes.
177
		$this->schedulable_classes = apply_filters( $this->option_prefix . 'modify_schedulable_classes', $this->schedulable_classes );
178
179
		/* // phpcs:ignore Squiz.PHP.CommentedOutCode.Found
180
		 * example to modify the array of classes by adding one and removing one
181
		 * add_filter( 'object_sync_for_salesforce_modify_schedulable_classes', 'modify_schedulable_classes', 10, 1 );
182
		 * function modify_schedulable_classes( $schedulable_classes ) {
183
		 * 	$schedulable_classes = array(
184
		 * 		'salesforce_push' => array(
185
		 * 		    'label' => 'Push to Salesforce',
186
		 * 		    'class' => 'Object_Sync_Sf_Salesforce_Push',
187
		 * 		    'callback' => 'salesforce_push_sync_rest',
188
		 * 		),
189
		 * 		'wordpress' => array( // phpcs:ignore WordPress.WP.CapitalPDangit.Misspelled
190
		 * 		    'label' => 'WordPress',
191
		 * 		    'class' => 'Object_Sync_Sf_WordPress',
192
		 * 		),
193
		 * 		'salesforce' => array(
194
		 * 		    'label' => 'Salesforce Authorization',
195
		 * 		    'class' => 'Object_Sync_Sf_Salesforce',
196
		 * 		),
197
		 * 	);
198
		 * 	return $schedulable_classes;
199
		 * }
200
		*/
201
	}
202
203
	/**
204
	 * Load the static $instance property that holds the instance of the class.
205
	 * This is preserved for legacy usage, as the same thing exists in the `object_sync_for_salesforce` function.
206
	 *
207
	 * @return object $plugin
208
	 * @deprecated since 2.0.0
209
	 */
210
	public static function get_instance() {
211
212
		if ( function_exists( 'object_sync_for_salesforce' ) ) {
213
			return object_sync_for_salesforce();
214
		}
215
216
		static $plugin;
217
218
		if ( is_null( $plugin ) ) {
219
			$plugin = new Object_Sync_Salesforce( OBJECT_SYNC_SF_VERSION, OBJECT_SYNC_SF_FILE );
220
		}
221
222
		return $plugin;
223
	}
224
225
	/**
226
	 * Initialize the plugin and start the action hooks.
227
	 * We run this separately because activate can't run without the right priority.
228
	 */
229
	public function init() {
230
231
		// methods for activation.
232
		$this->activated = new Object_Sync_Sf_Activate();
233
234
		// action hooks.
235
		$this->add_actions();
236
	}
237
238
	/**
239
	 * Run non-activation actions.
240
	 * We do this on -10 because ActionScheduler has to have access to plugins_loaded with priority of zero.
241
	 */
242
	private function add_actions() {
243
		// public actions.
244
		add_action( 'plugins_loaded', array( $this, 'run' ), -10 );
245
		add_action( 'plugins_loaded', array( $this, 'textdomain' ) );
246
	}
247
248
	/**
249
	 * Run the plugin, independent of activation methods.
250
	 */
251
	public function run() {
252
253
		$this->composer_loaded = $this->composer_loaded();
254
255
		$this->login_credentials = $this->get_login_credentials();
256
257
		// methods for deactivation.
258
		$deactivate = new Object_Sync_Sf_Deactivate();
259
260
		// logging methods.
261
		$this->logging = new Object_Sync_Sf_Logging();
262
263
		// methods for the ActionScheduler queue.
264
		$this->queue = new Object_Sync_Sf_Queue();
0 ignored issues
show
Documentation Bug introduced by
It seems like new Object_Sync_Sf_Queue() of type Object_Sync_Sf_Queue is incompatible with the declared type array of property $queue.

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..

Loading history...
265
266
		// methods for fieldmaps and object maps.
267
		$this->mappings = new Object_Sync_Sf_Mapping();
268
269
		// methods for WordPress.
270
		$this->wordpress = new Object_Sync_Sf_WordPress();
271
272
		// methods for calling the Salesforce API.
273
		$this->salesforce = $this->salesforce_get_api();
274
275
		// methods to push to Salesforce.
276
		$this->push = new Object_Sync_Sf_Salesforce_Push();
277
278
		// methods to pull from Salesforce.
279
		$this->pull = new Object_Sync_Sf_Salesforce_Pull();
280
281
		$this->rest = new Object_Sync_Sf_Rest();
282
283
		// admin functionality.
284
		new Object_Sync_Sf_Admin();
285
	}
286
287
	/**
288
	 * Autoload things from Composer.
289
	 *
290
	 * @return bool true
291
	 */
292
	private function composer_loaded() {
293
		require_once plugin_dir_path( $this->file ) . 'vendor/autoload.php';
294
		return true;
295
	}
296
297
	/**
298
	 * Get the pre-login Salesforce credentials.
299
	 * These depend on the plugin's settings or constants defined in wp-config.php.
300
	 *
301
	 * @return array $login_credentials
302
	 */
303
	private function get_login_credentials() {
304
305
		$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...
306
		$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...
307
		$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...
308
		$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...
309
		$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...
310
		$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...
311
		$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...
312
313
		$login_credentials = array(
314
			'consumer_key'     => $consumer_key,
315
			'consumer_secret'  => $consumer_secret,
316
			'callback_url'     => $callback_url,
317
			'login_url'        => $login_base_url,
318
			'authorize_path'   => $authorize_url_path,
319
			'token_path'       => $token_url_path,
320
			'rest_api_version' => $api_version,
321
		);
322
323
		return $login_credentials;
324
325
	}
326
327
	/**
328
	 * Public helper to load the Salesforce API and see if it is authenticated.
329
	 * This is public so other plugins can access the same SF API instance we use.
330
	 *
331
	 * @return array
332
	 */
333
	public function salesforce_get_api() {
334
335
		$soap_available = $this->is_soap_available();
336
		$soap_loaded    = $this->is_soap_loaded();
337
338
		$consumer_key    = $this->login_credentials['consumer_key'];
339
		$consumer_secret = $this->login_credentials['consumer_secret'];
340
		$is_authorized   = false;
341
		$sfapi           = '';
342
		if ( $consumer_key && $consumer_secret ) {
343
			$sfapi = new Object_Sync_Sf_Salesforce();
344
			if ( true === $sfapi->is_authorized() ) {
345
				$is_authorized = true;
346
			}
347
		}
348
349
		return array(
350
			'is_authorized'  => $is_authorized,
351
			'sfapi'          => $sfapi,
352
			'soap_available' => $soap_available,
353
			'soap_loaded'    => $soap_loaded,
354
		);
355
	}
356
357
	/**
358
	 * Load textdomain
359
	 */
360
	public function textdomain() {
361
		load_plugin_textdomain( 'object-sync-for-salesforce', false, dirname( plugin_basename( $this->file ) ) . '/languages/' );
362
	}
363
364
	/**
365
	 * Check the server to see if Soap is available
366
	 *
367
	 * @return bool $is_soap_available
368
	 */
369
	private function is_soap_available() {
370
		$is_soap_available = false;
371
		if ( extension_loaded( 'soap' ) && class_exists( 'SoapClient' ) ) {
372
			$is_soap_available = true;
373
		}
374
		return $is_soap_available;
375
	}
376
377
	/**
378
	 * Check the plugin to see if the Soap option has been enabled and the class has been loaded
379
	 *
380
	 * @return bool $is_soap_loaded
381
	 */
382
	private function is_soap_loaded() {
383
		$is_soap_loaded = false;
384
		if ( false === $this->is_soap_available() ) {
385
			return $is_soap_loaded;
386
		}
387
		$use_soap = filter_var( get_option( 'object_sync_for_salesforce_use_soap', false ), FILTER_VALIDATE_BOOLEAN );
388
		if ( false === $use_soap ) {
389
			return $is_soap_loaded;
390
		}
391
		if ( class_exists( 'Object_Sync_Sf_Salesforce_Soap_Partner' ) ) {
392
			$is_soap_loaded = true;
393
		}
394
		return $is_soap_loaded;
395
	}
396
397
} // end class
398