Issues (713)

classes/class-object-sync-sf-admin.php (165 issues)

1
<?php
2
/**
3
 * Create default WordPress admin functionality to configure the plugin.
4
 *
5
 * @class   Object_Sync_Sf_Admin
6
 * @package Object_Sync_Salesforce
7
 */
8
9
defined( 'ABSPATH' ) || exit;
10
11
/**
12
 * Object_Sync_Sf_Admin class.
13
 */
14
class Object_Sync_Sf_Admin {
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
	 * Login credentials for the Salesforce API; comes from wp-config or from the plugin settings
60
	 *
61
	 * @var array
62
	 */
63
	public $login_credentials;
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 $schedulable_classes;
71
72
	/**
73
	 * Object_Sync_Sf_Queue class
74
	 *
75
	 * @var object
76
	 */
77
	public $queue;
78
79
	/**
80
	 * Object_Sync_Sf_Logging class
81
	 *
82
	 * @var object
83
	 */
84
	public $logging;
85
86
	/**
87
	 * Object_Sync_Sf_Mapping class
88
	 *
89
	 * @var object
90
	 */
91
	public $mappings;
92
93
	/**
94
	 * Object_Sync_Sf_WordPress class
95
	 *
96
	 * @var object
97
	 */
98
	public $wordpress;
99
100
	/**
101
	 * Object_Sync_Sf_Salesforce class
102
	 * This contains Salesforce API methods
103
	 *
104
	 * @var array
105
	 */
106
	public $salesforce;
107
108
	/**
109
	 * Object_Sync_Sf_Salesforce_Push class
110
	 *
111
	 * @var object
112
	 */
113
	public $push;
114
115
	/**
116
	 * Object_Sync_Sf_Salesforce_Pull class
117
	 *
118
	 * @var object
119
	 */
120
	public $pull;
121
122
	/**
123
	 * Object_Sync_Sf_WordPress_Transient class
124
	 *
125
	 * @var object
126
	 */
127
	private $sfwp_transients;
128
129
	/**
130
	 * URL fragment for the plugin's settings page
131
	 *
132
	 * @var string
133
	 */
134
	private $admin_settings_url_param;
135
136
	/**
137
	 * Salesforce access token
138
	 *
139
	 * @var string
140
	 */
141
	private $access_token;
142
143
	/**
144
	 * Salesforce instance URL
145
	 *
146
	 * @var string
147
	 */
148
	private $instance_url;
149
150
	/**
151
	 * Salesforce refresh token
152
	 *
153
	 * @var string
154
	 */
155
	private $refresh_token;
156
157
	/**
158
	 * Default path for the Salesforce authorize URL
159
	 *
160
	 * @var string
161
	 */
162
	public $default_authorize_url_path;
163
164
	/**
165
	 * Default path for the Salesforce token URL
166
	 *
167
	 * @var string
168
	 */
169
	public $default_token_url_path;
170
171
	/**
172
	 * What version of the Salesforce API should be the default on the settings screen.
173
	 * Users can edit what version is used, but they won't see a correct list of all their available versions until WordPress has
174
	 * been authenticated with Salesforce.
175
	 *
176
	 * @var string
177
	 * @deprecated as of 2.2.0; will be removed in version 3.0. This property will stay until 3.0.0 because it is a public value and it could be accessed by other code.
178
	 */
179
	public $default_api_version;
180
181
	/**
182
	 * Default max number of pull records. Users can edit this.
183
	 *
184
	 * @var int
185
	 */
186
	public $default_pull_limit;
187
188
	/**
189
	 * Default throttle for how often to pull from Salesforce. Users can edit this.
190
	 *
191
	 * @var int
192
	 */
193
	public $default_pull_throttle;
194
195
	/**
196
	 * Default for whether to limit to triggerable items. Users can edit this.
197
	 *
198
	 * @var bool
199
	 */
200
	public $default_triggerable;
201
202
	/**
203
	 * Default for whether to limit to items that can be updated. Users can edit this.
204
	 *
205
	 * @var bool
206
	 */
207
	public $default_updateable;
208
209
	/**
210
	 * Constructor for admin class
211
	 */
212
	public function __construct() {
213
		$this->version             = object_sync_for_salesforce()->version;
214
		$this->file                = object_sync_for_salesforce()->file;
215
		$this->wpdb                = object_sync_for_salesforce()->wpdb;
216
		$this->slug                = object_sync_for_salesforce()->slug;
217
		$this->option_prefix       = object_sync_for_salesforce()->option_prefix;
218
		$this->action_group_suffix = object_sync_for_salesforce()->action_group_suffix;
219
220
		$this->login_credentials   = object_sync_for_salesforce()->login_credentials;
221
		$this->wordpress           = object_sync_for_salesforce()->wordpress;
222
		$this->salesforce          = object_sync_for_salesforce()->salesforce;
223
		$this->mappings            = object_sync_for_salesforce()->mappings;
224
		$this->push                = object_sync_for_salesforce()->push;
225
		$this->pull                = object_sync_for_salesforce()->pull;
226
		$this->logging             = object_sync_for_salesforce()->logging;
227
		$this->schedulable_classes = object_sync_for_salesforce()->schedulable_classes;
228
		$this->queue               = object_sync_for_salesforce()->queue;
229
230
		// set the Salesforce API version.
231
		// as of version 2.2.0, this is set by the plugin and is not configurable in the interface.
232
		// this class variable will be removed in 3.0.0.
233
		$this->default_api_version = $this->login_credentials['rest_api_version'];
0 ignored issues
show
Deprecated Code introduced by
The property Object_Sync_Sf_Admin::$default_api_version has been deprecated: as of 2.2.0; will be removed in version 3.0. This property will stay until 3.0.0 because it is a public value and it could be accessed by other code. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

233
		/** @scrutinizer ignore-deprecated */ $this->default_api_version = $this->login_credentials['rest_api_version'];

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
234
235
		$this->sfwp_transients          = object_sync_for_salesforce()->wordpress->sfwp_transients;
236
		$this->admin_settings_url_param = 'object-sync-salesforce-admin';
237
238
		// default authorize url path.
239
		$this->default_authorize_url_path = '/services/oauth2/authorize';
240
		// default token url path.
241
		$this->default_token_url_path = '/services/oauth2/token';
242
		// default pull record limit.
243
		$this->default_pull_limit = 25;
244
		// default pull throttle for avoiding going over api limits.
245
		$this->default_pull_throttle = 5;
246
		// default setting for triggerable items.
247
		$this->default_triggerable = true;
248
		// default setting for updateable items.
249
		$this->default_updateable = true;
250
251
		$this->add_actions();
252
	}
253
254
	/**
255
	 * Create the action hooks to create the admin pages.
256
	 */
257
	public function add_actions() {
258
259
		// settings link.
260
		add_filter( 'plugin_action_links', array( $this, 'plugin_action_links' ), 10, 5 );
0 ignored issues
show
The function add_filter was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

260
		/** @scrutinizer ignore-call */ 
261
  add_filter( 'plugin_action_links', array( $this, 'plugin_action_links' ), 10, 5 );
Loading history...
261
262
		// CSS and Javascript.
263
		add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts_and_styles' ) );
0 ignored issues
show
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

263
		/** @scrutinizer ignore-call */ 
264
  add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts_and_styles' ) );
Loading history...
264
265
		// Settings API forms and notices.
266
		add_action( 'admin_menu', array( $this, 'create_admin_menu' ) );
267
		add_action( 'admin_init', array( $this, 'salesforce_settings_forms' ) );
268
		add_action( 'admin_init', array( $this, 'display_notices' ) );
269
		add_action( 'admin_post_post_fieldmap', array( $this, 'prepare_fieldmap_data' ) );
270
		add_action( 'admin_post_delete_fieldmap', array( $this, 'delete_fieldmap' ) );
271
272
		// Ajax for fieldmap forms.
273
		add_action( 'wp_ajax_get_salesforce_object_description', array( $this, 'get_salesforce_object_description' ), 10, 1 );
274
		add_action( 'wp_ajax_get_salesforce_object_fields', array( $this, 'get_salesforce_object_fields' ), 10, 1 );
275
		add_action( 'wp_ajax_get_wordpress_object_fields', array( $this, 'get_wordpress_object_fields' ), 10, 1 );
276
277
		// Ajax events that can be manually called.
278
		add_action( 'wp_ajax_push_to_salesforce', array( $this, 'push_to_salesforce' ), 10, 3 );
279
		add_action( 'wp_ajax_pull_from_salesforce', array( $this, 'pull_from_salesforce' ), 10, 2 );
280
		add_action( 'wp_ajax_refresh_mapped_data', array( $this, 'refresh_mapped_data' ), 10, 1 );
281
		add_action( 'wp_ajax_clear_sfwp_cache', array( $this, 'clear_sfwp_cache' ) );
282
		add_action( 'wp_ajax_delete_salesforce_api_version', array( $this, 'delete_salesforce_api_version' ) );
283
284
		// we add a Salesforce box on user profiles.
285
		add_action( 'edit_user_profile', array( $this, 'show_salesforce_user_fields' ), 10, 1 );
286
		add_action( 'show_user_profile', array( $this, 'show_salesforce_user_fields' ), 10, 1 );
287
288
		// and we can update Salesforce fields on the user profile box.
289
		add_action( 'personal_options_update', array( $this, 'save_salesforce_user_fields' ), 10, 1 );
290
		add_action( 'edit_user_profile_update', array( $this, 'save_salesforce_user_fields' ), 10, 1 );
291
292
		// when either field for schedule settings changes.
293
		foreach ( $this->schedulable_classes as $key => $value ) {
294
			// if the user doesn't have any action schedule tasks, let's not leave them empty.
295
			add_filter( 'pre_update_option_' . $this->option_prefix . $key . '_schedule_number', array( $this, 'initial_action_schedule' ), 10, 3 );
296
			add_filter( 'pre_update_option_' . $this->option_prefix . $key . '_schedule_unit', array( $this, 'initial_action_schedule' ), 10, 3 );
297
298
			// this is if the user is changing their tasks.
299
			add_filter( 'update_option_' . $this->option_prefix . $key . '_schedule_number', array( $this, 'change_action_schedule' ), 10, 3 );
300
			add_filter( 'update_option_' . $this->option_prefix . $key . '_schedule_unit', array( $this, 'change_action_schedule' ), 10, 3 );
301
		}
302
303
		// when ActionScheduler runs its migration, resave the schedule options.
304
		add_action( 'action_scheduler/migration_complete', array( $this, 'resave_action_schedules' ) );
305
306
		// handle post requests for object maps.
307
		add_action( 'admin_post_delete_object_map', array( $this, 'delete_object_map' ) );
308
		add_action( 'admin_post_post_object_map', array( $this, 'prepare_object_map_data' ) );
309
310
		// import and export plugin data.
311
		add_action( 'admin_post_object_sync_for_salesforce_import', array( $this, 'import_json_file' ) );
312
		add_action( 'admin_post_object_sync_for_salesforce_export', array( $this, 'export_json_file' ) );
313
314
	}
315
316
	/**
317
	 * Display a Settings link on the main Plugins page
318
	 *
319
	 * @param array  $links the array of links for the main plugins page.
320
	 * @param string $file the filename.
321
	 * @return array $links the array of links for the main plugins page
322
	 */
323
	public function plugin_action_links( $links, $file ) {
324
		if ( plugin_basename( $this->file ) === $file ) {
0 ignored issues
show
The function plugin_basename was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

324
		if ( /** @scrutinizer ignore-call */ plugin_basename( $this->file ) === $file ) {
Loading history...
325
			$settings = '<a href="' . get_admin_url() . 'options-general.php?page=' . $this->admin_settings_url_param . '">' . __( 'Settings', 'object-sync-for-salesforce' ) . '</a>';
0 ignored issues
show
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

325
			$settings = '<a href="' . get_admin_url() . 'options-general.php?page=' . $this->admin_settings_url_param . '">' . /** @scrutinizer ignore-call */ __( 'Settings', 'object-sync-for-salesforce' ) . '</a>';
Loading history...
The function get_admin_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

325
			$settings = '<a href="' . /** @scrutinizer ignore-call */ get_admin_url() . 'options-general.php?page=' . $this->admin_settings_url_param . '">' . __( 'Settings', 'object-sync-for-salesforce' ) . '</a>';
Loading history...
326
			array_unshift( $links, $settings );
327
		}
328
		return $links;
329
	}
330
331
	/**
332
	 * Admin styles. Load the CSS and JavaScript for the plugin's settings
333
	 */
334
	public function admin_scripts_and_styles() {
335
336
		// Developers might not want to bother with select2 or selectwoo, so we allow that to be changeable.
337
		$select_library = apply_filters( $this->option_prefix . 'select_library', 'selectwoo' );
0 ignored issues
show
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

337
		$select_library = /** @scrutinizer ignore-call */ apply_filters( $this->option_prefix . 'select_library', 'selectwoo' );
Loading history...
338
339
		/*
340
		 * example to modify the select library
341
		 * add_filter( 'object_sync_for_salesforce_select_library', 'select_library', 10, 1 );
342
		 * function select_library( $select_library ) {
343
		 * 	$select_library = 'select2';
344
		 *  // this could also be empty; in that case we would just use default browser select
345
		 * 	return $select_library;
346
		 * }
347
		*/
348
349
		$javascript_dependencies = array( 'jquery' );
350
		$css_dependencies        = array();
351
		if ( '' !== $select_library ) {
352
			wp_enqueue_script( $select_library . 'js', plugins_url( 'assets/js/vendor/' . $select_library . '.min.js', $this->file ), array( 'jquery' ), filemtime( plugin_dir_path( $this->file ) . 'assets/js/vendor/' . $select_library . '.min.js' ), true );
0 ignored issues
show
The function wp_enqueue_script was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

352
			/** @scrutinizer ignore-call */ 
353
   wp_enqueue_script( $select_library . 'js', plugins_url( 'assets/js/vendor/' . $select_library . '.min.js', $this->file ), array( 'jquery' ), filemtime( plugin_dir_path( $this->file ) . 'assets/js/vendor/' . $select_library . '.min.js' ), true );
Loading history...
The function plugins_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

352
			wp_enqueue_script( $select_library . 'js', /** @scrutinizer ignore-call */ plugins_url( 'assets/js/vendor/' . $select_library . '.min.js', $this->file ), array( 'jquery' ), filemtime( plugin_dir_path( $this->file ) . 'assets/js/vendor/' . $select_library . '.min.js' ), true );
Loading history...
The function plugin_dir_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

352
			wp_enqueue_script( $select_library . 'js', plugins_url( 'assets/js/vendor/' . $select_library . '.min.js', $this->file ), array( 'jquery' ), filemtime( /** @scrutinizer ignore-call */ plugin_dir_path( $this->file ) . 'assets/js/vendor/' . $select_library . '.min.js' ), true );
Loading history...
353
			$javascript_dependencies[] = $select_library . 'js';
354
			wp_enqueue_style( $select_library . 'css', plugins_url( 'assets/css/vendor/' . $select_library . '.min.css', $this->file ), array(), filemtime( plugin_dir_path( $this->file ) . 'assets/css/vendor/' . $select_library . '.min.css' ), 'all' );
0 ignored issues
show
The function wp_enqueue_style was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

354
			/** @scrutinizer ignore-call */ 
355
   wp_enqueue_style( $select_library . 'css', plugins_url( 'assets/css/vendor/' . $select_library . '.min.css', $this->file ), array(), filemtime( plugin_dir_path( $this->file ) . 'assets/css/vendor/' . $select_library . '.min.css' ), 'all' );
Loading history...
355
			$css_dependencies[] = $select_library . 'css';
356
		}
357
358
		wp_enqueue_script( $this->slug . '-admin', plugins_url( 'assets/js/object-sync-for-salesforce-admin.min.js', $this->file ), $javascript_dependencies, filemtime( plugin_dir_path( $this->file ) . 'assets/js/object-sync-for-salesforce-admin.min.js' ), true );
359
		wp_enqueue_style( $this->slug . '-admin', plugins_url( 'assets/css/object-sync-for-salesforce-admin.css', $this->file ), $css_dependencies, filemtime( plugin_dir_path( $this->file ) . 'assets/css/object-sync-for-salesforce-admin.css' ), 'all' );
360
	}
361
362
	/**
363
	 * Initial recurring tasks for ActionScheduler
364
	 *
365
	 * @param string $new_schedule the new, unserialized option value.
366
	 * @param string $old_schedule the old option value.
367
	 * @param string $option_name option name.
368
	 * @return string $new_schedule
369
	 */
370
	public function initial_action_schedule( $new_schedule, $old_schedule, $option_name ) {
371
372
		// get the current schedule name from the task, based on pattern in the foreach.
373
		preg_match( '/' . $this->option_prefix . '(.*)_schedule/', $option_name, $matches );
374
		$schedule_name     = $matches[1];
375
		$action_group_name = $schedule_name . $this->action_group_suffix;
376
377
		// make sure there are no tasks already.
378
		$current_tasks = as_get_scheduled_actions(
379
			array(
380
				'hook'  => $this->schedulable_classes[ $schedule_name ]['initializer'],
381
				'group' => $action_group_name,
382
			),
383
			ARRAY_A
0 ignored issues
show
The constant ARRAY_A was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
384
		);
385
386
		// exit if there are already tasks; they'll be saved if the option data changed.
387
		if ( ! empty( $current_tasks ) ) {
388
			return $new_schedule;
389
		}
390
391
		$this->set_action_schedule( $schedule_name, $action_group_name );
392
393
		return $new_schedule;
394
395
	}
396
397
	/**
398
	 * Update recurring tasks for ActionScheduler if options change
399
	 *
400
	 * @param string $old_schedule the old option value.
401
	 * @param string $new_schedule the new, unserialized option value.
402
	 * @param string $option_name option name.
403
	 */
404
	public function change_action_schedule( $old_schedule, $new_schedule, $option_name ) {
405
406
		// this method does not run if the option's data is unchanged.
407
408
		// get the current schedule name from the task, based on pattern in the foreach.
409
		preg_match( '/' . $this->option_prefix . '(.*)_schedule/', $option_name, $matches );
410
		$schedule_name     = $matches[1];
411
		$action_group_name = $schedule_name . $this->action_group_suffix;
412
413
		$this->set_action_schedule( $schedule_name, $action_group_name );
414
415
	}
416
417
	/**
418
	 * Set up recurring tasks for ActionScheduler
419
	 *
420
	 * @param string $schedule_name the name of the schedule.
421
	 * @param string $action_group_name the group's name.
422
	 */
423
	private function set_action_schedule( $schedule_name, $action_group_name ) {
424
		// exit if there is no initializer property on this schedule.
425
		if ( ! isset( $this->schedulable_classes[ $schedule_name ]['initializer'] ) ) {
426
			return;
427
		}
428
429
		// cancel previous task.
430
		$this->queue->cancel(
431
			$this->schedulable_classes[ $schedule_name ]['initializer'],
432
			array(),
433
			$action_group_name
434
		);
435
436
		// create new recurring task for ActionScheduler to check for data to pull from Salesforce.
437
		$this->queue->schedule_recurring(
438
			time(), // plugin seems to expect UTC.
439
			$this->queue->get_frequency( $schedule_name, 'seconds' ),
440
			$this->schedulable_classes[ $schedule_name ]['initializer'],
441
			array(),
442
			$action_group_name
443
		);
444
	}
445
446
	/**
447
	 * When it finishes its migration, resave the scheduled tasks for ActionScheduler.
448
	 */
449
	public function resave_action_schedules() {
450
		// for each schedulable action, go ahead and resave it.
451
		$schedules_updated  = array();
452
		$schedules_restored = array();
453
		foreach ( $this->schedulable_classes as $key => $value ) {
454
			// make sure it has an initializer property; this is used on recurring tasks.
455
			if ( isset( $value['initializer'] ) ) {
456
				// toggle the schedule number setting.
457
				$schedule_option_name  = $this->option_prefix . $key . '_schedule_number';
458
				$previous_option_value = get_option( $schedule_option_name, 0 );
0 ignored issues
show
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

458
				$previous_option_value = /** @scrutinizer ignore-call */ get_option( $schedule_option_name, 0 );
Loading history...
459
				$previous_option_value = filter_var( $previous_option_value, FILTER_SANITIZE_NUMBER_INT );
460
				$new_option_value      = $previous_option_value + 1;
461
				$schedule_updated      = update_option( $schedule_option_name, $new_option_value );
0 ignored issues
show
The function update_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

461
				$schedule_updated      = /** @scrutinizer ignore-call */ update_option( $schedule_option_name, $new_option_value );
Loading history...
462
				if ( true === $schedule_updated ) {
463
					$schedules_updated[] = $key;
464
					$schedule_restored   = update_option( $schedule_option_name, $previous_option_value );
465
					if ( true === $schedule_restored ) {
466
						$schedules_restored[] = $key;
467
					}
468
				}
469
			}
470
		}
471
472
		// create a log entry from the updated scheduled tasks.
473
		if ( ! empty( $schedules_updated ) || ! empty( $schedules_restored ) ) {
474
			$status = 'success';
475
		} else {
476
			$status = 'error';
477
		}
478
479
		$body = sprintf( esc_html__( 'These are the scheduled tasks that were updated: ', 'object-sync-for-salesforce' ) . '<ul>' );
0 ignored issues
show
The function esc_html__ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

479
		$body = sprintf( /** @scrutinizer ignore-call */ esc_html__( 'These are the scheduled tasks that were updated: ', 'object-sync-for-salesforce' ) . '<ul>' );
Loading history...
480
		foreach ( $schedules_updated as $schedule_updated ) {
481
			$body .= sprintf(
482
				// translators: placeholders are: 1) the schedule name.
483
				'<li>' . esc_html__( 'Schedule name: %1$s.', 'object-sync-for-salesforce' ) . '</li>',
484
				esc_attr( $schedule_updated )
0 ignored issues
show
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

484
				/** @scrutinizer ignore-call */ 
485
    esc_attr( $schedule_updated )
Loading history...
485
			);
486
		}
487
		$body .= '</ul>';
488
489
		$body .= sprintf( esc_html__( 'These are the scheduled tasks that have the same frequency as they had pre-migration: ', 'object-sync-for-salesforce' ) . '<ul>' );
490
		foreach ( $schedules_restored as $schedule_restored ) {
491
			$body .= sprintf(
492
				// translators: placeholders are: 1) the schedule name.
493
				'<li>' . esc_html__( 'Schedule name: %1$s.', 'object-sync-for-salesforce' ) . '</li>',
494
				esc_attr( $schedule_restored )
495
			);
496
		}
497
		$body .= '</ul>';
498
499
		$body .= sprintf( esc_html__( 'If any tasks were not updated, or were not able to keep the same frequency they had before, go to the Scheduling tab to update them.', 'object-sync-for-salesforce' ) );
500
		$body .= sprintf(
501
			// translators: %1$s is the schedule settings URL.
502
			wp_kses_post( 'If any tasks were not updated, or were not able to keep the same frequency they had before, go to the <a href="' . admin_url( 'options-general.php?page=object-sync-salesforce-admin&tab=schedule' ) . '">%1$s</a> tab to update them.', 'object-sync-for-salesforce' ),
0 ignored issues
show
The function admin_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

502
			wp_kses_post( 'If any tasks were not updated, or were not able to keep the same frequency they had before, go to the <a href="' . /** @scrutinizer ignore-call */ admin_url( 'options-general.php?page=object-sync-salesforce-admin&tab=schedule' ) . '">%1$s</a> tab to update them.', 'object-sync-for-salesforce' ),
Loading history...
The function wp_kses_post was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

502
			/** @scrutinizer ignore-call */ 
503
   wp_kses_post( 'If any tasks were not updated, or were not able to keep the same frequency they had before, go to the <a href="' . admin_url( 'options-general.php?page=object-sync-salesforce-admin&tab=schedule' ) . '">%1$s</a> tab to update them.', 'object-sync-for-salesforce' ),
Loading history...
503
			esc_html__( 'Scheduling', 'object-sync-for-salesforce' )
504
		);
505
506
		$this->logging->setup(
507
			sprintf(
508
				// translators: %1$s is the log status, %2$s is the name of a WordPress object. %3$s is the id of that object.
509
				esc_html__( '%1$s ActionScheduler: the ActionScheduler library has completed its migration. See the log entry content for status on each recurring task.', 'object-sync-for-salesforce' ),
510
				ucfirst( esc_attr( $status ) )
511
			),
512
			$body,
513
			0,
514
			0,
515
			$status
516
		);
517
	}
518
519
	/**
520
	 * Create the WordPress admin options page
521
	 */
522
	public function create_admin_menu() {
523
		$title = __( 'Salesforce', 'object-sync-for-salesforce' );
0 ignored issues
show
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

523
		$title = /** @scrutinizer ignore-call */ __( 'Salesforce', 'object-sync-for-salesforce' );
Loading history...
524
		add_options_page( $title, $title, 'configure_salesforce', $this->admin_settings_url_param, array( $this, 'show_admin_page' ) );
0 ignored issues
show
The function add_options_page was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

524
		/** @scrutinizer ignore-call */ 
525
  add_options_page( $title, $title, 'configure_salesforce', $this->admin_settings_url_param, array( $this, 'show_admin_page' ) );
Loading history...
525
	}
526
527
	/**
528
	 * Render the admin pages in WordPress. This also allows other plugins to add tabs to this plugin's settings screen
529
	 */
530
	public function show_admin_page() {
531
		$get_data = filter_input_array( INPUT_GET, FILTER_SANITIZE_SPECIAL_CHARS );
532
		echo '<div class="wrap">';
533
		echo '<h1>' . esc_html( get_admin_page_title() ) . '</h1>';
0 ignored issues
show
The function esc_html was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

533
		echo '<h1>' . /** @scrutinizer ignore-call */ esc_html( get_admin_page_title() ) . '</h1>';
Loading history...
The function get_admin_page_title was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

533
		echo '<h1>' . esc_html( /** @scrutinizer ignore-call */ get_admin_page_title() ) . '</h1>';
Loading history...
534
		$allowed = $this->check_wordpress_admin_permissions();
535
		if ( false === $allowed ) {
536
			return;
537
		}
538
		$tabs = array(
539
			'settings'      => __( 'Settings', 'object-sync-for-salesforce' ),
0 ignored issues
show
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

539
			'settings'      => /** @scrutinizer ignore-call */ __( 'Settings', 'object-sync-for-salesforce' ),
Loading history...
540
			'authorize'     => __( 'Authorize', 'object-sync-for-salesforce' ),
541
			'fieldmaps'     => __( 'Fieldmaps', 'object-sync-for-salesforce' ),
542
			'schedule'      => __( 'Scheduling', 'object-sync-for-salesforce' ),
543
			'import-export' => __( 'Import &amp; Export', 'object-sync-for-salesforce' ),
544
		); // this creates the tabs for the admin.
545
546
		// optionally make tab(s) for logging and log settings.
547
		$logging_enabled      = get_option( $this->option_prefix . 'enable_logging', false );
0 ignored issues
show
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

547
		$logging_enabled      = /** @scrutinizer ignore-call */ get_option( $this->option_prefix . 'enable_logging', false );
Loading history...
548
		$tabs['log_settings'] = __( 'Log Settings', 'object-sync-for-salesforce' );
549
550
		$mapping_errors       = $this->mappings->get_failed_object_maps();
551
		$mapping_errors_total = isset( $mapping_errors['total'] ) ? $mapping_errors['total'] : 0;
552
		if ( 0 < $mapping_errors_total ) {
553
			$tabs['mapping_errors'] = __( 'Mapping Errors', 'object-sync-for-salesforce' );
554
		}
555
556
		// filter for extending the tabs available on the page
557
		// currently it will go into the default switch case for $tab.
558
		$tabs = apply_filters( $this->option_prefix . 'settings_tabs', $tabs );
0 ignored issues
show
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

558
		$tabs = /** @scrutinizer ignore-call */ apply_filters( $this->option_prefix . 'settings_tabs', $tabs );
Loading history...
559
560
		$tab = isset( $get_data['tab'] ) ? sanitize_key( $get_data['tab'] ) : 'settings';
0 ignored issues
show
The function sanitize_key was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

560
		$tab = isset( $get_data['tab'] ) ? /** @scrutinizer ignore-call */ sanitize_key( $get_data['tab'] ) : 'settings';
Loading history...
561
		$this->tabs( $tabs, $tab );
562
563
		$consumer_key    = $this->login_credentials['consumer_key'];
564
		$consumer_secret = $this->login_credentials['consumer_secret'];
565
		$callback_url    = $this->login_credentials['callback_url'];
566
567
		if ( true !== $this->salesforce['is_authorized'] ) {
568
			$url     = esc_url( $callback_url );
0 ignored issues
show
The function esc_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

568
			$url     = /** @scrutinizer ignore-call */ esc_url( $callback_url );
Loading history...
569
			$anchor  = esc_html__( 'Authorize tab', 'object-sync-for-salesforce' );
0 ignored issues
show
The function esc_html__ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

569
			$anchor  = /** @scrutinizer ignore-call */ esc_html__( 'Authorize tab', 'object-sync-for-salesforce' );
Loading history...
570
			$message = sprintf( 'Salesforce needs to be authorized to connect to this website. Use the <a href="%s">%s</a> to connect.', $url, $anchor );
571
			require plugin_dir_path( $this->file ) . '/templates/admin/error.php';
0 ignored issues
show
The function plugin_dir_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

571
			require /** @scrutinizer ignore-call */ plugin_dir_path( $this->file ) . '/templates/admin/error.php';
Loading history...
572
		}
573
574
		if ( 0 === count( $this->mappings->get_fieldmaps() ) ) {
575
			$url     = esc_url( get_admin_url( null, 'options-general.php?page=' . $this->admin_settings_url_param . '&tab=fieldmaps' ) );
0 ignored issues
show
The function get_admin_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

575
			$url     = esc_url( /** @scrutinizer ignore-call */ get_admin_url( null, 'options-general.php?page=' . $this->admin_settings_url_param . '&tab=fieldmaps' ) );
Loading history...
576
			$anchor  = esc_html__( 'Fieldmaps tab', 'object-sync-for-salesforce' );
577
			$message = sprintf( 'No fieldmaps exist yet. Use the <a href="%s">%s</a> to map WordPress and Salesforce objects to each other.', $url, $anchor );
578
			require plugin_dir_path( $this->file ) . '/templates/admin/error.php';
579
		}
580
581
		try {
582
			switch ( $tab ) {
583
				case 'authorize':
584
					if ( isset( $get_data['code'] ) ) {
585
						// this string is an oauth token.
586
						$data          = esc_html( wp_unslash( $get_data['code'] ) );
0 ignored issues
show
The function wp_unslash was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

586
						$data          = esc_html( /** @scrutinizer ignore-call */ wp_unslash( $get_data['code'] ) );
Loading history...
587
						$is_authorized = $this->salesforce['sfapi']->request_token( $data );
588
						?>
589
						<script>window.location = '<?php echo esc_url_raw( $callback_url ); ?>'</script>
0 ignored issues
show
The function esc_url_raw was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

589
						<script>window.location = '<?php echo /** @scrutinizer ignore-call */ esc_url_raw( $callback_url ); ?>'</script>
Loading history...
590
						<?php
591
					} elseif ( true === $this->salesforce['is_authorized'] ) {
592
							require_once plugin_dir_path( $this->file ) . '/templates/admin/authorized.php';
593
							$this->status( $this->salesforce['sfapi'] );
594
					} elseif ( true === is_object( $this->salesforce['sfapi'] ) && isset( $consumer_key ) && isset( $consumer_secret ) ) {
595
						?>
596
						<p><a class="button button-primary" href="<?php echo esc_url( $this->salesforce['sfapi']->get_authorization_code() ); ?>"><?php echo esc_html__( 'Connect to Salesforce', 'object-sync-for-salesforce' ); ?></a></p>
597
						<?php
598
					} else {
599
						$url    = esc_url( get_admin_url( null, 'options-general.php?page=' . $this->admin_settings_url_param . '&tab=settings' ) );
600
						$anchor = esc_html__( 'Settings', 'object-sync-for-salesforce' );
601
						// translators: placeholders are for the settings tab link: 1) the url, and 2) the anchor text.
602
						$message = sprintf( esc_html__( 'Salesforce needs to be authorized to connect to this website but the credentials are missing. Use the <a href="%1$s">%2$s</a> tab to add them.', 'object-sync-for-salesforce' ), $url, $anchor );
603
						require_once plugin_dir_path( $this->file ) . '/templates/admin/error.php';
604
					}
605
					break;
606
				case 'fieldmaps':
607
					if ( isset( $get_data['method'] ) ) {
608
609
						$method      = sanitize_key( $get_data['method'] );
610
						$error_url   = get_admin_url( null, 'options-general.php?page=' . $this->admin_settings_url_param . '&tab=fieldmaps&method=' . $method );
611
						$success_url = get_admin_url( null, 'options-general.php?page=' . $this->admin_settings_url_param . '&tab=fieldmaps' );
612
613
						$disable_mapped_fields = get_option( $this->option_prefix . 'disable_mapped_fields', false );
614
						$disable_mapped_fields = filter_var( $disable_mapped_fields, FILTER_VALIDATE_BOOLEAN );
615
						$fieldmap_class        = 'fieldmap';
616
						if ( true === $disable_mapped_fields ) {
617
							$fieldmap_class .= ' fieldmap-disable-mapped-fields';
618
						}
619
620
						if ( isset( $get_data['transient'] ) ) {
621
							$transient = sanitize_key( $get_data['transient'] );
622
							$posted    = $this->sfwp_transients->get( $transient );
623
						}
624
625
						if ( isset( $posted ) && is_array( $posted ) ) {
626
							$map = $posted;
627
						} elseif ( 'edit' === $method || 'clone' === $method || 'delete' === $method ) {
628
							$map = $this->mappings->get_fieldmaps( isset( $get_data['id'] ) ? sanitize_key( $get_data['id'] ) : '' );
629
						}
630
631
						if ( 'add' === $method || ( isset( $map ) && is_array( $map ) && isset( $map['id'] ) ) ) {
632
							if ( isset( $map ) && is_array( $map ) && isset( $map['id'] ) ) {
633
								$label                               = $map['label'];
634
								$fieldmap_status                     = $map['fieldmap_status'];
635
								$salesforce_object                   = $map['salesforce_object'];
636
								$salesforce_record_types_allowed     = maybe_unserialize( $map['salesforce_record_types_allowed'] );
0 ignored issues
show
The function maybe_unserialize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

636
								$salesforce_record_types_allowed     = /** @scrutinizer ignore-call */ maybe_unserialize( $map['salesforce_record_types_allowed'] );
Loading history...
637
								$salesforce_record_type_default      = $map['salesforce_record_type_default'];
638
								$wordpress_object                    = $map['wordpress_object'];
639
								$pull_trigger_field                  = $map['pull_trigger_field'];
640
								$fieldmap_fields                     = $map['fields'];
641
								$sync_triggers                       = $map['sync_triggers'];
642
								$always_delete_object_maps_on_delete = $map['always_delete_object_maps_on_delete'];
643
								$push_async                          = $map['push_async'];
644
								$push_drafts                         = $map['push_drafts'];
645
								$pull_to_drafts                      = $map['pull_to_drafts'];
646
								$weight                              = $map['weight'];
647
							}
648
							if ( 'add' === $method || 'edit' === $method || 'clone' === $method ) {
649
								require_once plugin_dir_path( $this->file ) . '/templates/admin/fieldmaps-add-edit-clone.php';
650
							} elseif ( 'delete' === $method ) {
651
								require_once plugin_dir_path( $this->file ) . '/templates/admin/fieldmaps-delete.php';
652
							}
653
						} else {
654
							$no_fieldmap_url = get_admin_url( null, 'options-general.php?page=' . $this->admin_settings_url_param . '&tab=fieldmaps&missing_fieldmap=true' );
655
							wp_safe_redirect( $no_fieldmap_url );
0 ignored issues
show
The function wp_safe_redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

655
							/** @scrutinizer ignore-call */ 
656
       wp_safe_redirect( $no_fieldmap_url );
Loading history...
656
							exit();
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
657
						}
658
					} else {
659
						$fieldmaps = $this->mappings->get_fieldmaps();
660
						require_once plugin_dir_path( $this->file ) . '/templates/admin/fieldmaps-list.php';
661
					} // End if statement.
662
					break;
663
				case 'logout':
664
					$this->logout();
665
					break;
666
				case 'clear_cache':
667
					$this->clear_cache();
668
					break;
669
				case 'clear_schedule':
670
					if ( isset( $get_data['schedule_name'] ) ) {
671
						$schedule_name = sanitize_key( $get_data['schedule_name'] );
672
					}
673
					$this->clear_schedule( $schedule_name );
674
					break;
675
				case 'settings':
676
					require_once plugin_dir_path( $this->file ) . '/templates/admin/settings.php';
677
					break;
678
				case 'mapping_errors':
679
					if ( isset( $get_data['method'] ) ) {
680
681
						$method      = sanitize_key( $get_data['method'] );
682
						$error_url   = get_admin_url( null, 'options-general.php?page=' . $this->admin_settings_url_param . '&tab=mapping_errors&method=' . $method );
683
						$success_url = get_admin_url( null, 'options-general.php?page=' . $this->admin_settings_url_param . '&tab=mapping_errors' );
684
685
						if ( isset( $get_data['map_transient'] ) ) {
686
							$transient = sanitize_key( $get_data['map_transient'] );
687
							$posted    = $this->sfwp_transients->get( $transient );
688
						}
689
690
						if ( isset( $posted ) && is_array( $posted ) ) {
691
							$map_row = $posted;
692
						} elseif ( 'edit' === $method || 'delete' === $method ) {
693
							$map_row = $this->mappings->get_failed_object_map( isset( $get_data['id'] ) ? sanitize_key( $get_data['id'] ) : '' );
694
						}
695
696
						if ( isset( $map_row ) && is_array( $map_row ) ) {
697
							$salesforce_id    = $map_row['salesforce_id'];
698
							$wordpress_id     = $map_row['wordpress_id'];
699
							$wordpress_object = $map_row['wordpress_object'];
700
						}
701
702
						if ( 'edit' === $method ) {
703
							require_once plugin_dir_path( $this->file ) . '/templates/admin/mapping-errors-edit.php';
704
						} elseif ( 'delete' === $method ) {
705
							require_once plugin_dir_path( $this->file ) . '/templates/admin/mapping-errors-delete.php';
706
						}
707
					} else {
708
709
						if ( isset( $get_data['mapping_error_transient'] ) ) {
710
							$transient = sanitize_key( $get_data['mapping_error_transient'] );
711
							$posted    = $this->sfwp_transients->get( $transient );
712
						}
713
714
						$ids_string = '';
715
						$ids        = array();
716
						if ( isset( $posted['delete'] ) ) {
717
							$ids_string = maybe_serialize( $posted['delete'] );
0 ignored issues
show
The function maybe_serialize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

717
							$ids_string = /** @scrutinizer ignore-call */ maybe_serialize( $posted['delete'] );
Loading history...
718
							$ids        = $posted['delete'];
719
						}
720
721
						$error_url   = get_admin_url( null, 'options-general.php?page=' . $this->admin_settings_url_param . '&tab=mapping_errors&ids=' . $ids_string );
722
						$success_url = get_admin_url( null, 'options-general.php?page=' . $this->admin_settings_url_param . '&tab=mapping_errors' );
723
						require_once plugin_dir_path( $this->file ) . '/templates/admin/mapping-errors.php';
724
					}
725
					break;
726
				case 'import-export':
727
					require_once plugin_dir_path( $this->file ) . '/templates/admin/import-export.php';
728
					break;
729
				default:
730
					$include_settings = apply_filters( $this->option_prefix . 'settings_tab_include_settings', true, $tab );
731
					$content_before   = apply_filters( $this->option_prefix . 'settings_tab_content_before', null, $tab );
732
					$content_after    = apply_filters( $this->option_prefix . 'settings_tab_content_after', null, $tab );
733
					if ( null !== $content_before ) {
734
						echo esc_html( $content_before );
735
					}
736
					if ( true === $include_settings ) {
737
						require_once plugin_dir_path( $this->file ) . '/templates/admin/settings.php';
738
					}
739
					if ( null !== $content_after ) {
740
						echo esc_html( $content_after );
741
					}
742
					break;
743
			} // End switch statement.
744
		} catch ( Object_Sync_Sf_Exception $ex ) {
745
			echo sprintf(
746
				'<p>Error <strong>%1$s</strong>: %2$s</p>',
747
				absint( $ex->getCode() ),
0 ignored issues
show
The function absint was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

747
				/** @scrutinizer ignore-call */ 
748
    absint( $ex->getCode() ),
Loading history...
748
				esc_html( $ex->getMessage() )
749
			);
750
		} // End try for menu/page setup.
751
		echo '</div>';
752
	}
753
754
	/**
755
	 * Create default WordPress admin settings form. This runs the Settings page.
756
	 */
757
	public function salesforce_settings_forms() {
758
		$get_data = filter_input_array( INPUT_GET, FILTER_SANITIZE_SPECIAL_CHARS );
759
		$page     = isset( $get_data['tab'] ) ? sanitize_key( $get_data['tab'] ) : 'settings';
0 ignored issues
show
The function sanitize_key was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

759
		$page     = isset( $get_data['tab'] ) ? /** @scrutinizer ignore-call */ sanitize_key( $get_data['tab'] ) : 'settings';
Loading history...
760
		$section  = isset( $get_data['tab'] ) ? sanitize_key( $get_data['tab'] ) : 'settings';
761
762
		$input_callback_default   = array( $this, 'display_input_field' );
763
		$input_checkboxes_default = array( $this, 'display_checkboxes' );
764
		$input_select_default     = array( $this, 'display_select' );
765
		$link_default             = array( $this, 'display_link' );
766
767
		$all_field_callbacks = array(
768
			'text'       => $input_callback_default,
769
			'checkboxes' => $input_checkboxes_default,
770
			'select'     => $input_select_default,
771
			'link'       => $link_default,
772
		);
773
774
		$this->fields_settings( 'settings', 'settings', $all_field_callbacks );
775
		$this->fields_fieldmaps( 'fieldmaps', 'objects' );
776
		$this->fields_scheduling( 'schedule', 'schedule', $all_field_callbacks );
777
		$this->fields_log_settings( 'log_settings', 'log_settings', $all_field_callbacks );
778
		$this->fields_errors( 'mapping_errors', 'mapping_errors', $all_field_callbacks );
779
	}
780
781
	/**
782
	 * Fields for the Settings tab
783
	 * This runs add_settings_section once, as well as add_settings_field and register_setting methods for each option
784
	 *
785
	 * @param string $page what page we're on.
786
	 * @param string $section what section of the page.
787
	 * @param array  $callbacks method to call.
788
	 */
789
	private function fields_settings( $page, $section, $callbacks ) {
790
		add_settings_section( $page, ucwords( $page ), null, $page );
0 ignored issues
show
The function add_settings_section was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

790
		/** @scrutinizer ignore-call */ 
791
  add_settings_section( $page, ucwords( $page ), null, $page );
Loading history...
791
		$salesforce_settings = array(
792
			'consumer_key'                   => array(
793
				'title'    => __( 'Consumer Key', 'object-sync-for-salesforce' ),
0 ignored issues
show
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

793
				'title'    => /** @scrutinizer ignore-call */ __( 'Consumer Key', 'object-sync-for-salesforce' ),
Loading history...
794
				'callback' => $callbacks['text'],
795
				'page'     => $page,
796
				'section'  => $section,
797
				'args'     => array(
798
					'type'     => 'text',
799
					'validate' => 'sanitize_validate_text',
800
					'desc'     => '',
801
					'constant' => 'OBJECT_SYNC_SF_SALESFORCE_CONSUMER_KEY',
802
				),
803
804
			),
805
			'consumer_secret'                => array(
806
				'title'    => __( 'Consumer Secret', 'object-sync-for-salesforce' ),
807
				'callback' => $callbacks['text'],
808
				'page'     => $page,
809
				'section'  => $section,
810
				'args'     => array(
811
					'type'     => 'text',
812
					'validate' => 'sanitize_validate_text',
813
					'desc'     => '',
814
					'constant' => 'OBJECT_SYNC_SF_SALESFORCE_CONSUMER_SECRET',
815
				),
816
			),
817
			'callback_url'                   => array(
818
				'title'    => __( 'Callback URL', 'object-sync-for-salesforce' ),
819
				'callback' => $callbacks['text'],
820
				'page'     => $page,
821
				'section'  => $section,
822
				'args'     => array(
823
					'type'     => 'url',
824
					'validate' => 'sanitize_validate_text',
825
					'desc'     => sprintf(
826
						// translators: %1$s is the admin URL for the Authorize tab.
827
						__( 'In most cases, you will want to use %1$s for this value.', 'object-sync-for-salesforce' ),
828
						get_admin_url( null, 'options-general.php?page=' . $this->admin_settings_url_param . '&tab=authorize' )
0 ignored issues
show
The function get_admin_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

828
						/** @scrutinizer ignore-call */ 
829
      get_admin_url( null, 'options-general.php?page=' . $this->admin_settings_url_param . '&tab=authorize' )
Loading history...
829
					),
830
					'constant' => 'OBJECT_SYNC_SF_SALESFORCE_CALLBACK_URL',
831
				),
832
			),
833
			'login_base_url'                 => array(
834
				'title'    => __( 'Login Base URL', 'object-sync-for-salesforce' ),
835
				'callback' => $callbacks['text'],
836
				'page'     => $page,
837
				'section'  => $section,
838
				'args'     => array(
839
					'type'     => 'url',
840
					'validate' => 'sanitize_validate_text',
841
					'desc'     => sprintf(
842
						// translators: 1) production salesforce login, 2) sandbox salesforce login.
843
						__( 'For most Salesforce setups, you should use %1$s for production and %2$s for sandbox. If you try to use an instance name as the URL, you may encounter Salesforce errors.', 'object-sync-for-salesforce' ),
844
						esc_url( 'https://login.salesforce.com' ),
0 ignored issues
show
The function esc_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

844
						/** @scrutinizer ignore-call */ 
845
      esc_url( 'https://login.salesforce.com' ),
Loading history...
845
						esc_url( 'https://test.salesforce.com' )
846
					),
847
					'constant' => 'OBJECT_SYNC_SF_SALESFORCE_LOGIN_BASE_URL',
848
				),
849
			),
850
			'authorize_url_path'             => array(
851
				'title'    => __( 'Authorize URL Path', 'object-sync-for-salesforce' ),
852
				'callback' => $callbacks['text'],
853
				'page'     => $page,
854
				'section'  => $section,
855
				'args'     => array(
856
					'type'     => 'text',
857
					'validate' => 'sanitize_validate_text',
858
					'desc'     => __( 'For most Salesforce installs, this should not be changed.', 'object-sync-for-salesforce' ),
859
					'constant' => 'OBJECT_SYNC_SF_SALESFORCE_AUTHORIZE_URL_PATH',
860
					'default'  => $this->default_authorize_url_path,
861
				),
862
			),
863
			'token_url_path'                 => array(
864
				'title'    => __( 'Token URL Path', 'object-sync-for-salesforce' ),
865
				'callback' => $callbacks['text'],
866
				'page'     => $page,
867
				'section'  => $section,
868
				'args'     => array(
869
					'type'     => 'text',
870
					'validate' => 'sanitize_validate_text',
871
					'desc'     => __( 'For most Salesforce installs, this should not be changed.', 'object-sync-for-salesforce' ),
872
					'constant' => 'OBJECT_SYNC_SF_SALESFORCE_TOKEN_URL_PATH',
873
					'default'  => $this->default_token_url_path,
874
				),
875
			),
876
			'object_filters'                 => array(
877
				'title'    => __( 'Limit Salesforce Objects', 'object-sync-for-salesforce' ),
878
				'callback' => $callbacks['checkboxes'],
879
				'page'     => $page,
880
				'section'  => $section,
881
				'args'     => array(
882
					'type'     => 'checkboxes',
883
					'validate' => 'sanitize_validate_text',
884
					'desc'     => __( 'Allows you to limit which Salesforce objects can be mapped', 'object-sync-for-salesforce' ),
885
					'items'    => array(
886
						'triggerable' => array(
887
							'text'    => __( 'Only Triggerable objects', 'object-sync-for-salesforce' ),
888
							'id'      => 'triggerable',
889
							'desc'    => '',
890
							'default' => $this->default_triggerable,
891
						),
892
						'updateable'  => array(
893
							'text'    => __( 'Only Updateable objects', 'object-sync-for-salesforce' ),
894
							'id'      => 'updateable',
895
							'desc'    => '',
896
							'default' => $this->default_updateable,
897
						),
898
					),
899
				),
900
			),
901
			'salesforce_field_display_value' => array(
902
				'title'    => __( 'Salesforce Field Display Value', 'object-sync-for-salesforce' ),
903
				'callback' => $callbacks['select'],
904
				'page'     => $page,
905
				'section'  => $section,
906
				'args'     => array(
907
					'type'     => 'select',
908
					'validate' => 'sanitize_validate_text',
909
					'desc'     => __( 'When choosing Salesforce fields to map, this value determines how the dropdown will identify Salesforce fields.', 'object-sync-for-salesforce' ),
910
					'constant' => '',
911
					'items'    => array(
912
						'field_label' => array(
913
							'text'  => __( 'Field Label', 'object-sync-for-salesforce' ),
914
							'value' => 'field_label',
915
						),
916
						'api_name'    => array(
917
							'text'  => __( 'API Name', 'object-sync-for-salesforce' ),
918
							'value' => 'api_name',
919
						),
920
					),
921
				),
922
			),
923
			'disable_mapped_fields'          => array(
924
				'title'    => __( 'Prevent Duplicate Field Mapping?', 'object-sync-for-salesforce' ),
925
				'callback' => $callbacks['text'],
926
				'page'     => $page,
927
				'section'  => $section,
928
				'args'     => array(
929
					'type'     => 'checkbox',
930
					'validate' => 'sanitize_text_field',
931
					'desc'     => __( 'If checked, any WordPress or Salesforce field that has already been mapped, or that is selected while creating or editing a fieldmap, cannot be mapped again.', 'object-sync-for-salesforce' ),
932
					'constant' => '',
933
				),
934
			),
935
			'pull_query_limit'               => array(
936
				'title'    => __( 'Pull Query Record Limit', 'object-sync-for-salesforce' ),
937
				'callback' => $callbacks['text'],
938
				'page'     => $page,
939
				'section'  => $section,
940
				'args'     => array(
941
					'type'     => 'number',
942
					'validate' => 'absint',
943
					'desc'     => __( 'Limit the number of records that can be pulled from Salesforce in a single query.', 'object-sync-for-salesforce' ),
944
					'constant' => '',
945
					'default'  => $this->default_pull_limit,
946
				),
947
			),
948
			'pull_throttle'                  => array(
949
				'title'    => __( 'Pull Throttle (In Seconds)', 'object-sync-for-salesforce' ),
950
				'callback' => $callbacks['text'],
951
				'page'     => $page,
952
				'section'  => $section,
953
				'args'     => array(
954
					'type'     => 'number',
955
					'validate' => 'sanitize_validate_text',
956
					'desc'     => __( 'Number of seconds to wait between repeated salesforce pulls. Prevents the webserver from becoming overloaded in case of too many cron runs, or webhook usage.', 'object-sync-for-salesforce' ),
957
					'constant' => '',
958
					'default'  => $this->default_pull_throttle,
959
				),
960
			),
961
		);
962
963
		// only show soap settings if the soap extension is enabled on the server.
964
		if ( true === $this->salesforce['soap_available'] ) {
965
			$salesforce_settings['use_soap']       = array(
966
				'title'    => __( 'Enable the Salesforce SOAP API?', 'object-sync-for-salesforce' ),
967
				'callback' => $callbacks['text'],
968
				'page'     => $page,
969
				'section'  => $section,
970
				'class'    => 'object-sync-for-salesforce-enable-soap',
971
				'args'     => array(
972
					'type'     => 'checkbox',
973
					'validate' => 'sanitize_text_field',
974
					'desc'     => __( 'Check this to enable the SOAP API and use it instead of the REST API when the plugin supports it. https://developer.salesforce.com/blogs/tech-pubs/2011/10/salesforce-apis-what-they-are-when-to-use-them.html to compare the two. Note: if you need to detect Salesforce merges in this plugin, you will need to enable SOAP.', 'object-sync-for-salesforce' ),
975
					'constant' => '',
976
				),
977
			);
978
			$salesforce_settings['soap_wsdl_path'] = array(
979
				'title'    => __( 'Path to SOAP WSDL File', 'object-sync-for-salesforce' ),
980
				'callback' => $callbacks['text'],
981
				'page'     => $page,
982
				'section'  => $section,
983
				'class'    => 'object-sync-for-salesforce-soap-wsdl-path',
984
				'args'     => array(
985
					'type'     => 'text',
986
					'validate' => 'sanitize_text_field',
987
					'desc'     => __( 'Optionally add a path to your own WSDL file. If you do not, the plugin will use the default partner.wsdl.xml from the Force.com toolkit.', 'object-sync-for-salesforce' ),
988
					'constant' => '',
989
				),
990
			);
991
		}
992
993
		$salesforce_settings['debug_mode']               = array(
994
			'title'    => __( 'Debug Mode?', 'object-sync-for-salesforce' ),
995
			'callback' => $callbacks['text'],
996
			'page'     => $page,
997
			'section'  => $section,
998
			'args'     => array(
999
				'type'     => 'checkbox',
1000
				'validate' => 'sanitize_text_field',
1001
				'desc'     => __( 'Debug mode activates logging for plugin events like Salesforce API requests and WordPress data operations. This can create a lot of log entries; it is not recommended to use it long-term in a production environment.', 'object-sync-for-salesforce' ),
1002
				'constant' => '',
1003
			),
1004
		);
1005
		$salesforce_settings['delete_data_on_uninstall'] = array(
1006
			'title'    => __( 'Delete Plugin Data on Uninstall?', 'object-sync-for-salesforce' ),
1007
			'callback' => $callbacks['text'],
1008
			'page'     => $page,
1009
			'section'  => $section,
1010
			'args'     => array(
1011
				'type'     => 'checkbox',
1012
				'validate' => 'sanitize_text_field',
1013
				'desc'     => __( 'If checked, the plugin will delete the tables and other data it creates when you uninstall it. Unchecking this field can be useful if you need to reactivate the plugin for any reason without losing data.', 'object-sync-for-salesforce' ),
1014
				'constant' => '',
1015
			),
1016
		);
1017
1018
		foreach ( $salesforce_settings as $key => $attributes ) {
1019
			$id       = $this->option_prefix . $key;
1020
			$name     = $this->option_prefix . $key;
1021
			$title    = $attributes['title'];
1022
			$callback = $attributes['callback'];
1023
			$validate = $attributes['args']['validate'];
1024
			$page     = $attributes['page'];
1025
			$section  = $attributes['section'];
1026
			$class    = isset( $attributes['class'] ) ? $attributes['class'] : '';
1027
			$args     = array_merge(
1028
				$attributes['args'],
1029
				array(
1030
					'title'     => $title,
1031
					'id'        => $id,
1032
					'label_for' => $id,
1033
					'name'      => $name,
1034
					'class'     => $class,
1035
				)
1036
			);
1037
1038
			// if there is a constant and it is defined, don't run a validate function.
1039
			if ( isset( $attributes['args']['constant'] ) && defined( $attributes['args']['constant'] ) ) {
1040
				$validate = '';
1041
			}
1042
1043
			add_settings_field( $id, $title, $callback, $page, $section, $args );
0 ignored issues
show
The function add_settings_field was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1043
			/** @scrutinizer ignore-call */ 
1044
   add_settings_field( $id, $title, $callback, $page, $section, $args );
Loading history...
1044
			register_setting( $page, $id, array( $this, $validate ) );
0 ignored issues
show
The function register_setting was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1044
			/** @scrutinizer ignore-call */ 
1045
   register_setting( $page, $id, array( $this, $validate ) );
Loading history...
1045
		}
1046
	}
1047
1048
	/**
1049
	 * Fields for the Fieldmaps tab
1050
	 * This runs add_settings_section once, as well as add_settings_field and register_setting methods for each option
1051
	 *
1052
	 * @param string $page what page we're on.
1053
	 * @param string $section what section of the page.
1054
	 * @param string $input_callback method to call.
1055
	 */
1056
	private function fields_fieldmaps( $page, $section, $input_callback = '' ) {
1057
		add_settings_section( $page, ucwords( $page ), null, $page );
0 ignored issues
show
The function add_settings_section was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1057
		/** @scrutinizer ignore-call */ 
1058
  add_settings_section( $page, ucwords( $page ), null, $page );
Loading history...
1058
	}
1059
1060
	/**
1061
	 * Fields for the Scheduling tab
1062
	 * This runs add_settings_section once, as well as add_settings_field and register_setting methods for each option
1063
	 *
1064
	 * @param string $page what page we're on.
1065
	 * @param string $section what section of the page.
1066
	 * @param array  $callbacks method to call.
1067
	 */
1068
	private function fields_scheduling( $page, $section, $callbacks ) {
1069
1070
		add_settings_section( 'batch', __( 'Batch Settings', 'object-sync-for-salesforce' ), null, $page );
0 ignored issues
show
The function add_settings_section was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1070
		/** @scrutinizer ignore-call */ 
1071
  add_settings_section( 'batch', __( 'Batch Settings', 'object-sync-for-salesforce' ), null, $page );
Loading history...
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1070
		add_settings_section( 'batch', /** @scrutinizer ignore-call */ __( 'Batch Settings', 'object-sync-for-salesforce' ), null, $page );
Loading history...
1071
		$section           = 'batch';
1072
		$schedule_settings = array(
1073
			'action_scheduler_batch_size'         => array(
1074
				'title'    => __( 'Batch Size', 'object-sync-for-salesforce' ),
1075
				'callback' => $callbacks['text'],
1076
				'page'     => $page,
1077
				'section'  => $section,
1078
				'args'     => array(
1079
					'type'     => 'number',
1080
					'validate' => 'absint',
1081
					'default'  => 5,
1082
					'desc'     => __( 'Set how many actions (checking for data changes, syncing a record, etc. all count as individual actions) can be run in a batch. Start with a low number here, like 5, if you are unsure.', 'object-sync-for-salesforce' ),
1083
					'constant' => '',
1084
				),
1085
1086
			),
1087
			'action_scheduler_concurrent_batches' => array(
1088
				'title'    => __( 'Concurrent Batches', 'object-sync-for-salesforce' ),
1089
				'callback' => $callbacks['text'],
1090
				'page'     => $page,
1091
				'section'  => $section,
1092
				'args'     => array(
1093
					'type'     => 'number',
1094
					'validate' => 'absint',
1095
					'default'  => 3,
1096
					'desc'     => __( 'Set how many batches of actions can be run at once. Start with a low number here, like 3, if you are unsure.', 'object-sync-for-salesforce' ),
1097
					'constant' => '',
1098
				),
1099
			),
1100
		);
1101
1102
		foreach ( $this->schedulable_classes as $key => $value ) {
1103
			add_settings_section( $key, $value['label'], null, $page );
1104
			if ( isset( $value['initializer'] ) ) {
1105
				$schedule_settings[ $key . '_schedule_number' ] = array(
1106
					'title'    => __( 'Run Schedule Every', 'object-sync-for-salesforce' ),
1107
					'callback' => $callbacks['text'],
1108
					'page'     => $page,
1109
					'section'  => $key,
1110
					'args'     => array(
1111
						'type'     => 'number',
1112
						'validate' => 'absint',
1113
						'desc'     => '',
1114
						'constant' => '',
1115
					),
1116
				);
1117
				$schedule_settings[ $key . '_schedule_unit' ]   = array(
1118
					'title'    => __( 'Time Unit', 'object-sync-for-salesforce' ),
1119
					'callback' => $callbacks['select'],
1120
					'page'     => $page,
1121
					'section'  => $key,
1122
					'args'     => array(
1123
						'type'     => 'select',
1124
						'validate' => 'sanitize_validate_text',
1125
						'desc'     => '',
1126
						'items'    => array(
1127
							'minutes' => array(
1128
								'text'  => __( 'Minutes', 'object-sync-for-salesforce' ),
1129
								'value' => 'minutes',
1130
							),
1131
							'hours'   => array(
1132
								'text'  => __( 'Hours', 'object-sync-for-salesforce' ),
1133
								'value' => 'hours',
1134
							),
1135
							'days'    => array(
1136
								'text'  => __( 'Days', 'object-sync-for-salesforce' ),
1137
								'value' => 'days',
1138
							),
1139
						),
1140
					),
1141
				);
1142
			}
1143
			$schedule_settings[ $key . '_clear_button' ] = array(
1144
				// translators: $this->get_schedule_count is an integer showing how many items are in the current queue.
1145
				'title'    => sprintf( 'This Queue Has ' . _n( '%s Item', '%s Items', $this->get_schedule_count( $key ), 'object-sync-for-salesforce' ), $this->get_schedule_count( $key ) ),
0 ignored issues
show
The function _n was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1145
				'title'    => sprintf( 'This Queue Has ' . /** @scrutinizer ignore-call */ _n( '%s Item', '%s Items', $this->get_schedule_count( $key ), 'object-sync-for-salesforce' ), $this->get_schedule_count( $key ) ),
Loading history...
1146
				'callback' => $callbacks['link'],
1147
				'page'     => $page,
1148
				'section'  => $key,
1149
				'args'     => array(
1150
					'label'      => __( 'Clear this queue', 'object-sync-for-salesforce' ),
1151
					'desc'       => '',
1152
					'url'        => esc_url( '?page=' . $this->admin_settings_url_param . '&amp;tab=clear_schedule&amp;schedule_name=' . $key ),
0 ignored issues
show
The function esc_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1152
					'url'        => /** @scrutinizer ignore-call */ esc_url( '?page=' . $this->admin_settings_url_param . '&amp;tab=clear_schedule&amp;schedule_name=' . $key ),
Loading history...
1153
					'link_class' => 'button button-secondary',
1154
				),
1155
			);
1156
			foreach ( $schedule_settings as $key => $attributes ) {
1157
				$id       = $this->option_prefix . $key;
1158
				$name     = $this->option_prefix . $key;
1159
				$title    = $attributes['title'];
1160
				$callback = $attributes['callback'];
1161
				$page     = $attributes['page'];
1162
				$section  = $attributes['section'];
1163
				$args     = array_merge(
1164
					$attributes['args'],
1165
					array(
1166
						'title'     => $title,
1167
						'id'        => $id,
1168
						'label_for' => $id,
1169
						'name'      => $name,
1170
					)
1171
				);
1172
				add_settings_field( $id, $title, $callback, $page, $section, $args );
0 ignored issues
show
The function add_settings_field was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1172
				/** @scrutinizer ignore-call */ 
1173
    add_settings_field( $id, $title, $callback, $page, $section, $args );
Loading history...
1173
				register_setting( $page, $id );
0 ignored issues
show
The function register_setting was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1173
				/** @scrutinizer ignore-call */ 
1174
    register_setting( $page, $id );
Loading history...
1174
			}
1175
		} // End foreach statement.
1176
	}
1177
1178
	/**
1179
	 * Fields for the Log Settings tab
1180
	 * This runs add_settings_section once, as well as add_settings_field and register_setting methods for each option
1181
	 *
1182
	 * @param string $page what page we're on.
1183
	 * @param string $section what section of the page.
1184
	 * @param array  $callbacks method to call.
1185
	 */
1186
	private function fields_log_settings( $page, $section, $callbacks ) {
1187
		add_settings_section( $page, ucwords( str_replace( '_', ' ', $page ) ), null, $page );
0 ignored issues
show
The function add_settings_section was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1187
		/** @scrutinizer ignore-call */ 
1188
  add_settings_section( $page, ucwords( str_replace( '_', ' ', $page ) ), null, $page );
Loading history...
1188
		$log_settings = array(
1189
			'enable_logging'        => array(
1190
				'title'    => __( 'Enable Logging?', 'object-sync-for-salesforce' ),
0 ignored issues
show
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1190
				'title'    => /** @scrutinizer ignore-call */ __( 'Enable Logging?', 'object-sync-for-salesforce' ),
Loading history...
1191
				'callback' => $callbacks['text'],
1192
				'page'     => $page,
1193
				'section'  => $section,
1194
				'args'     => array(
1195
					'type'     => 'checkbox',
1196
					'validate' => 'absint',
1197
					'desc'     => __( 'This determines whether to create plugin log events in standard operation. If Debug Mode is enabled in the plugin settings, logging will occur regardless of this setting.', 'object-sync-for-salesforce' ),
1198
					'constant' => '',
1199
				),
1200
			),
1201
			'statuses_to_log'       => array(
1202
				'title'    => __( 'Statuses to Log', 'object-sync-for-salesforce' ),
1203
				'callback' => $callbacks['checkboxes'],
1204
				'page'     => $page,
1205
				'section'  => $section,
1206
				'args'     => array(
1207
					'type'     => 'checkboxes',
1208
					'validate' => 'sanitize_validate_text',
1209
					'desc'     => '',
1210
					'items'    => array(
1211
						'error'   => array(
1212
							'text' => __( 'Error', 'object-sync-for-salesforce' ),
1213
							'id'   => 'error',
1214
							'desc' => '',
1215
						),
1216
						'success' => array(
1217
							'text' => __( 'Success', 'object-sync-for-salesforce' ),
1218
							'id'   => 'success',
1219
							'desc' => '',
1220
						),
1221
						'notice'  => array(
1222
							'text' => __( 'Notice', 'object-sync-for-salesforce' ),
1223
							'id'   => 'notice',
1224
							'desc' => '',
1225
						),
1226
					),
1227
				),
1228
			),
1229
			'prune_logs'            => array(
1230
				'title'    => __( 'Automatically Delete Old Log Entries?', 'object-sync-for-salesforce' ),
1231
				'callback' => $callbacks['text'],
1232
				'page'     => $page,
1233
				'section'  => $section,
1234
				'args'     => array(
1235
					'type'     => 'checkbox',
1236
					'validate' => 'absint',
1237
					'desc'     => '',
1238
					'constant' => '',
1239
				),
1240
			),
1241
			'logs_how_old'          => array(
1242
				'title'    => __( 'Age to Delete Log Entries', 'object-sync-for-salesforce' ),
1243
				'callback' => $callbacks['text'],
1244
				'page'     => $page,
1245
				'section'  => $section,
1246
				'args'     => array(
1247
					'type'     => 'text',
1248
					'validate' => 'sanitize_validate_text',
1249
					'desc'     => __( 'If automatic deleting is enabled, it will affect logs this old.', 'object-sync-for-salesforce' ),
1250
					'default'  => '2 weeks',
1251
					'constant' => '',
1252
				),
1253
			),
1254
			'logs_how_often_number' => array(
1255
				'title'    => __( 'Check For Old Logs Every', 'object-sync-for-salesforce' ),
1256
				'callback' => $callbacks['text'],
1257
				'page'     => $page,
1258
				'section'  => $section,
1259
				'args'     => array(
1260
					'type'     => 'number',
1261
					'validate' => 'absint',
1262
					'desc'     => '',
1263
					'default'  => '1',
1264
					'constant' => '',
1265
				),
1266
			),
1267
			'logs_how_often_unit'   => array(
1268
				'title'    => __( 'Time Unit', 'object-sync-for-salesforce' ),
1269
				'callback' => $callbacks['select'],
1270
				'page'     => $page,
1271
				'section'  => $section,
1272
				'args'     => array(
1273
					'type'     => 'select',
1274
					'validate' => 'sanitize_validate_text',
1275
					'desc'     => __( 'These two fields are how often the site will check for logs to delete.', 'object-sync-for-salesforce' ),
1276
					'items'    => array(
1277
						'minutes' => array(
1278
							'text'  => __( 'Minutes', 'object-sync-for-salesforce' ),
1279
							'value' => 'minutes',
1280
						),
1281
						'hours'   => array(
1282
							'text'  => __( 'Hours', 'object-sync-for-salesforce' ),
1283
							'value' => 'hours',
1284
						),
1285
						'days'    => array(
1286
							'text'  => __( 'Days', 'object-sync-for-salesforce' ),
1287
							'value' => 'days',
1288
						),
1289
					),
1290
				),
1291
			),
1292
			'logs_how_many_number'  => array(
1293
				'title'    => __( 'Clear This Many Log Entries', 'object-sync-for-salesforce' ),
1294
				'callback' => $callbacks['text'],
1295
				'page'     => $page,
1296
				'section'  => $section,
1297
				'args'     => array(
1298
					'type'     => 'number',
1299
					'validate' => 'absint',
1300
					'desc'     => __( 'This number is how many log entries the plugin will try to clear at a time. If you do not enter a number, the default is 100.', 'object-sync-for-salesforce' ),
1301
					'default'  => '100',
1302
					'constant' => '',
1303
				),
1304
			),
1305
			'triggers_to_log'       => array(
1306
				'title'    => __( 'Triggers to Log', 'object-sync-for-salesforce' ),
1307
				'callback' => $callbacks['checkboxes'],
1308
				'page'     => $page,
1309
				'section'  => $section,
1310
				'args'     => array(
1311
					'type'     => 'checkboxes',
1312
					'validate' => 'sanitize_validate_text',
1313
					'desc'     => __( 'These are the triggers to log. When the plugin is in debug mode (see the settings tab), all triggers will be considered to be triggers to log, even if they are not checked here.', 'object-sync-for-salesforce' ),
1314
					'items'    => array(
1315
						$this->mappings->sync_wordpress_create => array(
1316
							'text' => __( 'WordPress Create', 'object-sync-for-salesforce' ),
1317
							'id'   => 'wordpress_create',
1318
							'desc' => '',
1319
						),
1320
						$this->mappings->sync_wordpress_update => array(
1321
							'text' => __( 'WordPress Update', 'object-sync-for-salesforce' ),
1322
							'id'   => 'wordpress_update',
1323
							'desc' => '',
1324
						),
1325
						$this->mappings->sync_wordpress_delete => array(
1326
							'text' => __( 'WordPress Delete', 'object-sync-for-salesforce' ),
1327
							'id'   => 'wordpress_delete',
1328
							'desc' => '',
1329
						),
1330
						$this->mappings->sync_sf_create => array(
1331
							'text' => __( 'Salesforce Create', 'object-sync-for-salesforce' ),
1332
							'id'   => 'sf_create',
1333
							'desc' => '',
1334
						),
1335
						$this->mappings->sync_sf_update => array(
1336
							'text' => __( 'Salesforce Update', 'object-sync-for-salesforce' ),
1337
							'id'   => 'sf_update',
1338
							'desc' => '',
1339
						),
1340
						$this->mappings->sync_sf_delete => array(
1341
							'text' => __( 'Salesforce Delete', 'object-sync-for-salesforce' ),
1342
							'id'   => 'sf_delete',
1343
							'desc' => '',
1344
						),
1345
					),
1346
				),
1347
			),
1348
		);
1349
		foreach ( $log_settings as $key => $attributes ) {
1350
			$id       = $this->option_prefix . $key;
1351
			$name     = $this->option_prefix . $key;
1352
			$title    = $attributes['title'];
1353
			$callback = $attributes['callback'];
1354
			$page     = $attributes['page'];
1355
			$section  = $attributes['section'];
1356
			$args     = array_merge(
1357
				$attributes['args'],
1358
				array(
1359
					'title'     => $title,
1360
					'id'        => $id,
1361
					'label_for' => $id,
1362
					'name'      => $name,
1363
				)
1364
			);
1365
			add_settings_field( $id, $title, $callback, $page, $section, $args );
0 ignored issues
show
The function add_settings_field was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1365
			/** @scrutinizer ignore-call */ 
1366
   add_settings_field( $id, $title, $callback, $page, $section, $args );
Loading history...
1366
			register_setting( $page, $id );
0 ignored issues
show
The function register_setting was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1366
			/** @scrutinizer ignore-call */ 
1367
   register_setting( $page, $id );
Loading history...
1367
		}
1368
	}
1369
1370
	/**
1371
	 * Fields for the Mapping Errors tab
1372
	 * This runs add_settings_section once, as well as add_settings_field and register_setting methods for each option
1373
	 *
1374
	 * @param string $page what page we're on.
1375
	 * @param string $section what section of the page.
1376
	 * @param array  $callbacks method to call.
1377
	 */
1378
	private function fields_errors( $page, $section, $callbacks ) {
1379
1380
		add_settings_section( $section, __( 'Mapping Error Settings', 'object-sync-for-salesforce' ), null, $page );
0 ignored issues
show
The function add_settings_section was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1380
		/** @scrutinizer ignore-call */ 
1381
  add_settings_section( $section, __( 'Mapping Error Settings', 'object-sync-for-salesforce' ), null, $page );
Loading history...
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1380
		add_settings_section( $section, /** @scrutinizer ignore-call */ __( 'Mapping Error Settings', 'object-sync-for-salesforce' ), null, $page );
Loading history...
1381
		$error_settings = array(
1382
			'errors_per_page' => array(
1383
				'title'    => __( 'Errors per page', 'object-sync-for-salesforce' ),
1384
				'callback' => $callbacks['text'],
1385
				'page'     => $page,
1386
				'section'  => $section,
1387
				'args'     => array(
1388
					'type'     => 'number',
1389
					'validate' => 'absint',
1390
					'default'  => 50,
1391
					'desc'     => __( 'Set how many mapping errors to show on a single page.', 'object-sync-for-salesforce' ),
1392
					'constant' => '',
1393
				),
1394
			),
1395
		);
1396
1397
		foreach ( $error_settings as $key => $attributes ) {
1398
			$id       = $this->option_prefix . $key;
1399
			$name     = $this->option_prefix . $key;
1400
			$title    = $attributes['title'];
1401
			$callback = $attributes['callback'];
1402
			$page     = $attributes['page'];
1403
			$section  = $attributes['section'];
1404
			$args     = array_merge(
1405
				$attributes['args'],
1406
				array(
1407
					'title'     => $title,
1408
					'id'        => $id,
1409
					'label_for' => $id,
1410
					'name'      => $name,
1411
				)
1412
			);
1413
			add_settings_field( $id, $title, $callback, $page, $section, $args );
0 ignored issues
show
The function add_settings_field was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1413
			/** @scrutinizer ignore-call */ 
1414
   add_settings_field( $id, $title, $callback, $page, $section, $args );
Loading history...
1414
			register_setting( $page, $id );
0 ignored issues
show
The function register_setting was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1414
			/** @scrutinizer ignore-call */ 
1415
   register_setting( $page, $id );
Loading history...
1415
		} // End foreach() method.
1416
	}
1417
1418
	/**
1419
	 * Create and return the data for notices.
1420
	 *
1421
	 * @return array $notices is the array of notices.
1422
	 */
1423
	public function get_notices_data() {
1424
		$notices = array(
1425
			'permission'              => array(
1426
				'condition'   => ( false === $this->check_wordpress_admin_permissions() ),
1427
				'message'     => __( "Your account does not have permission to edit the Object Sync for Salesforce plugin's settings.", 'object-sync-for-salesforce' ),
0 ignored issues
show
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1427
				'message'     => /** @scrutinizer ignore-call */ __( "Your account does not have permission to edit the Object Sync for Salesforce plugin's settings.", 'object-sync-for-salesforce' ),
Loading history...
1428
				'type'        => 'error',
1429
				'dismissible' => false,
1430
			),
1431
			'not_secure'              => array(
1432
				'condition'   => ( false === $this->check_wordpress_ssl() && false === $this->check_wordpress_ssl_support() ),
1433
				'message'     => esc_html__( 'At least the admin area of your website must use HTTPS to connect with Salesforce. WordPress reports that your site environment does not, and cannot, use HTTPS. You may need to work with your hosting company to make the switch before you can use this plugin.', 'object-sync-for-salesforce' ),
0 ignored issues
show
The function esc_html__ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1433
				'message'     => /** @scrutinizer ignore-call */ esc_html__( 'At least the admin area of your website must use HTTPS to connect with Salesforce. WordPress reports that your site environment does not, and cannot, use HTTPS. You may need to work with your hosting company to make the switch before you can use this plugin.', 'object-sync-for-salesforce' ),
Loading history...
1434
				'type'        => 'error',
1435
				'dismissible' => false,
1436
			),
1437
			'secure_supported'        => array(
1438
				'condition'   => ( false === $this->check_wordpress_ssl() && true === $this->check_wordpress_ssl_support() ),
1439
				'message'     => sprintf(
1440
					// translators: 1) is the site health URL, and 2) is the text for the site health page title.
1441
					__( 'Your website is not currently using HTTPS, but your environment does support it. Visit your website\'s <a href="%1$s">%2$s</a> for more information. If you have just migrated to HTTPS, WordPress may take some time to update this detection.', 'object-sync-for-salesforce' ),
1442
					esc_url( admin_url( 'site-health.php' ) ),
0 ignored issues
show
The function esc_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1442
					/** @scrutinizer ignore-call */ 
1443
     esc_url( admin_url( 'site-health.php' ) ),
Loading history...
The function admin_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1442
					esc_url( /** @scrutinizer ignore-call */ admin_url( 'site-health.php' ) ),
Loading history...
1443
					esc_html__( 'Site Health screen', 'object-sync-for-salesforce' )
1444
				),
1445
				'type'        => 'error',
1446
				'dismissible' => false,
1447
			),
1448
			'deprecated_api_version'  => array(
1449
				'condition'   => ( isset( $this->login_credentials['using_deprecated_option'] ) && true === $this->login_credentials['using_deprecated_option'] ),
1450
				'message'     => sprintf(
1451
					// translators: 1) is the version number of the Salesforce REST API, 2) is the option key for where the deprecated version is stored, 3) is the prefixed options table name, 4) is the link to delete the option, 5) is the default API version to use, and 6) is the name of the wp-config value.
1452
					esc_html__( 'Object Sync for Salesforce is using version %1$s of the Salesforce REST API, which is configured from a previous version. This value is no longer configurable in the plugin settings, and in version 3.0.0, previously saved values will be removed. You can delete the %2$s field from the %3$s table on your own, use %4$s, set it to %5$s so the plugin can delete it, or wait until that release. If the %6$s value is in your wp-config.php file instead, you should delete it from there as well.', 'object-sync-for-salesforce' ),
1453
					esc_attr( $this->login_credentials['rest_api_version'] ),
0 ignored issues
show
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1453
					/** @scrutinizer ignore-call */ 
1454
     esc_attr( $this->login_credentials['rest_api_version'] ),
Loading history...
1454
					'<code>' . esc_attr( $this->option_prefix . 'api_version' ) . '</code>',
1455
					'<code>' . esc_attr( $this->wpdb->prefix . 'options' ) . '</code>',
1456
					'<a href="#" id="salesforce-delete-rest-api-version">this link that will attempt to delete it</a>',
1457
					'<code>' . esc_attr( OBJECT_SYNC_SF_DEFAULT_API_VERSION ) . '</code>',
1458
					'<code>' . esc_attr( 'OBJECT_SYNC_SF_DEFAULT_API_VERSION' ) . '</code>'
1459
				),
1460
				'type'        => 'error',
1461
				'dismissible' => true,
1462
			),
1463
			'fieldmap'                => array(
1464
				'condition'   => isset( $get_data['transient'] ),
1465
				'message'     => esc_html__( 'Errors kept this fieldmap from being saved.', 'object-sync-for-salesforce' ),
1466
				'type'        => 'error',
1467
				'dismissible' => true,
1468
			),
1469
			'fieldmap_missing'        => array(
1470
				'condition'   => isset( $get_data['missing_fieldmap'] ),
1471
				'message'     => __( 'There is no fieldmap with the supplied ID. Instead, the list of all available fieldmaps is displayed.', 'object-sync-for-salesforce' ),
1472
				'type'        => 'error',
1473
				'dismissible' => true,
1474
			),
1475
			'object_map'              => array(
1476
				'condition'   => isset( $get_data['map_transient'] ),
1477
				'message'     => esc_html__( 'Errors kept this object map from being saved.', 'object-sync-for-salesforce' ),
1478
				'type'        => 'error',
1479
				'dismissible' => true,
1480
			),
1481
			'data_saved'              => array(
1482
				'condition'   => isset( $get_data['data_saved'] ) && 'true' === $get_data['data_saved'],
1483
				'message'     => esc_html__( 'This data was successfully saved.', 'object-sync-for-salesforce' ),
1484
				'type'        => 'success',
1485
				'dismissible' => true,
1486
			),
1487
			'data_save_partial'       => array(
1488
				'condition'   => isset( $get_data['data_saved'] ) && 'partial' === $get_data['data_saved'],
1489
				'message'     => __( 'This data was partially successfully saved. This means some of the data was unable to save. If you have enabled logging or debug mode in the plugin settings, there should be a log entry with further details.', 'object-sync-for-salesforce' ),
1490
				'type'        => 'error',
1491
				'dismissible' => true,
1492
			),
1493
			'data_save_error'         => array(
1494
				'condition'   => isset( $get_data['data_saved'] ) && 'false' === $get_data['data_saved'],
1495
				'message'     => esc_html__( 'This data was not successfully saved. Try again.', 'object-sync-for-salesforce' ),
1496
				'type'        => 'error',
1497
				'dismissible' => true,
1498
			),
1499
			'mapping_error_transient' => array(
1500
				'condition'   => isset( $get_data['mapping_error_transient'] ),
1501
				'message'     => esc_html__( 'Errors kept these mapping errors from being deleted.', 'object-sync-for-salesforce' ),
1502
				'type'        => 'error',
1503
				'dismissible' => true,
1504
			),
1505
		);
1506
		return $notices;
1507
	}
1508
1509
	/**
1510
	 * Create the notices, settings, and conditions by which admin notices should appear
1511
	 */
1512
	public function display_notices() {
1513
1514
		// before a notice is displayed, we should make sure we are on a page related to this plugin.
1515
		if ( ! isset( $_GET['page'] ) || $this->admin_settings_url_param !== $_GET['page'] ) {
1516
			return;
1517
		}
1518
1519
		$get_data = filter_input_array( INPUT_GET, FILTER_SANITIZE_SPECIAL_CHARS );
1520
		$notices  = $this->get_notices_data();
1521
1522
		foreach ( $notices as $key => $value ) {
1523
1524
			$condition = (bool) $value['condition'];
1525
			$message   = $value['message'];
1526
1527
			if ( isset( $value['dismissible'] ) ) {
1528
				$dismissible = $value['dismissible'];
1529
			} else {
1530
				$dismissible = false;
1531
			}
1532
1533
			if ( isset( $value['type'] ) ) {
1534
				$type = $value['type'];
1535
			} else {
1536
				$type = '';
1537
			}
1538
1539
			if ( ! isset( $value['template'] ) ) {
1540
				$template = '';
1541
			}
1542
1543
			if ( $condition ) {
1544
				new Object_Sync_Sf_Admin_Notice( $condition, $message, $dismissible, $type, $template );
1545
			}
1546
		}
1547
1548
	}
1549
1550
	/**
1551
	 * Get all the Salesforce object settings for fieldmapping
1552
	 * This takes either the $_POST array via ajax, or can be directly called with a $data array
1553
	 *
1554
	 * @param array $data must contain a Salesforce object, can optionally contain a type.
1555
	 * @return array $object_settings
1556
	 */
1557
	public function get_salesforce_object_description( $data = array() ) {
1558
		$ajax = false;
1559
		if ( empty( $data ) ) {
1560
			$data = filter_input_array( INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS );
1561
			$ajax = true;
1562
		}
1563
1564
		$object_description = array();
1565
1566
		if ( ! empty( $data['salesforce_object'] ) ) {
1567
			$object = $this->salesforce['sfapi']->object_describe( esc_attr( $data['salesforce_object'] ) );
0 ignored issues
show
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1567
			$object = $this->salesforce['sfapi']->object_describe( /** @scrutinizer ignore-call */ esc_attr( $data['salesforce_object'] ) );
Loading history...
1568
1569
			$object_fields        = array();
1570
			$include_record_types = array();
1571
1572
			// these can come from ajax.
1573
			$include = isset( $data['include'] ) ? (array) $data['include'] : array();
1574
			$include = array_map( 'esc_attr', $include );
1575
1576
			if ( in_array( 'fields', $include, true ) || empty( $include ) ) {
1577
				$type = isset( $data['field_type'] ) ? esc_attr( $data['field_type'] ) : ''; // can come from ajax.
1578
1579
				// here, we should respect the decision of whether to show the API name or the label.
1580
				$display_value = get_option( $this->option_prefix . 'salesforce_field_display_value', 'field_label' );
0 ignored issues
show
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1580
				$display_value = /** @scrutinizer ignore-call */ get_option( $this->option_prefix . 'salesforce_field_display_value', 'field_label' );
Loading history...
1581
				if ( 'api_name' === $display_value ) {
1582
					$visible_label_field = 'name';
1583
				} else {
1584
					$visible_label_field = 'label';
1585
				}
1586
				$attributes = array( 'name', $visible_label_field );
1587
1588
				foreach ( $object['data']['fields'] as $key => $value ) {
1589
					if ( '' === $type || $type === $value['type'] ) {
1590
						$object_fields[ $key ] = $value;
1591
						if ( isset( $attributes ) ) {
1592
							$object_fields[ $key ] = array_intersect_key( $value, array_flip( $attributes ) );
1593
						}
1594
					}
1595
				}
1596
				$object_description['fields'] = $object_fields;
1597
			}
1598
1599
			if ( in_array( 'recordTypeInfos', $include, true ) ) {
1600
				if ( isset( $object['data']['recordTypeInfos'] ) && count( $object['data']['recordTypeInfos'] ) > 1 ) {
1601
					foreach ( $object['data']['recordTypeInfos'] as $type ) {
1602
						$object_record_types[ $type['recordTypeId'] ] = $type['name'];
1603
					}
1604
					$object_description['recordTypeInfos'] = $object_record_types;
1605
				}
1606
			}
1607
		}
1608
1609
		if ( true === $ajax ) {
1610
			wp_send_json_success( $object_description );
0 ignored issues
show
The function wp_send_json_success was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1610
			/** @scrutinizer ignore-call */ 
1611
   wp_send_json_success( $object_description );
Loading history...
1611
		} else {
1612
			return $object_description;
1613
		}
1614
	}
1615
1616
	/**
1617
	 * Get all the Salesforce fields settings for fieldmapping
1618
	 * This takes either the $_POST array via ajax, or can be directly called with a $data array
1619
	 *
1620
	 * @param array $data must contain a Salesforce object unless it is Ajax, can optionally contain a type.
1621
	 * @return array $object_fields
1622
	 */
1623
	public function get_salesforce_object_fields( $data = array() ) {
1624
		$ajax      = false;
1625
		$post_data = filter_input_array( INPUT_POST, FILTER_UNSAFE_RAW );
1626
		if ( empty( $data ) ) {
1627
			$salesforce_object = isset( $post_data['salesforce_object'] ) ? sanitize_text_field( wp_unslash( $post_data['salesforce_object'] ) ) : '';
0 ignored issues
show
The function sanitize_text_field was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1627
			$salesforce_object = isset( $post_data['salesforce_object'] ) ? /** @scrutinizer ignore-call */ sanitize_text_field( wp_unslash( $post_data['salesforce_object'] ) ) : '';
Loading history...
The function wp_unslash was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1627
			$salesforce_object = isset( $post_data['salesforce_object'] ) ? sanitize_text_field( /** @scrutinizer ignore-call */ wp_unslash( $post_data['salesforce_object'] ) ) : '';
Loading history...
1628
			$ajax              = true;
1629
			// here, we should respect the decision of whether to show the API name or the label.
1630
			$display_value = get_option( $this->option_prefix . 'salesforce_field_display_value', 'field_label' );
0 ignored issues
show
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1630
			$display_value = /** @scrutinizer ignore-call */ get_option( $this->option_prefix . 'salesforce_field_display_value', 'field_label' );
Loading history...
1631
			if ( 'api_name' === $display_value ) {
1632
				$visible_label_field = 'name';
1633
			} else {
1634
				$visible_label_field = 'label';
1635
			}
1636
			$attributes = array( 'name', $visible_label_field );
1637
		} else {
1638
			$salesforce_object = isset( $data['salesforce_object'] ) ? sanitize_text_field( wp_unslash( $data['salesforce_object'] ) ) : '';
1639
		}
1640
		$object_fields = array();
1641
		if ( ! empty( $salesforce_object ) ) {
1642
			$object               = $this->salesforce['sfapi']->object_describe( esc_attr( $salesforce_object ) );
0 ignored issues
show
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1642
			$object               = $this->salesforce['sfapi']->object_describe( /** @scrutinizer ignore-call */ esc_attr( $salesforce_object ) );
Loading history...
1643
			$object_fields        = array();
1644
			$type                 = isset( $data['type'] ) ? esc_attr( $data['type'] ) : '';
1645
			$include_record_types = isset( $data['include_record_types'] ) ? esc_attr( $data['include_record_types'] ) : false;
1646
			foreach ( $object['data']['fields'] as $key => $value ) {
1647
				if ( '' === $type || $type === $value['type'] ) {
1648
					$object_fields[ $key ] = $value;
1649
					if ( isset( $attributes ) ) {
1650
						$object_fields[ $key ] = array_intersect_key( $value, array_flip( $attributes ) );
1651
					}
1652
				}
1653
			}
1654
			if ( true === $include_record_types ) {
1655
				$object_record_types = array();
1656
				if ( isset( $object['data']['recordTypeInfos'] ) && count( $object['data']['recordTypeInfos'] ) > 1 ) {
1657
					foreach ( $object['data']['recordTypeInfos'] as $type ) {
1658
						$object_record_types[ $type['recordTypeId'] ] = $type['name'];
1659
					}
1660
				}
1661
			}
1662
		}
1663
1664
		if ( true === $ajax ) {
1665
			$ajax_response = array(
1666
				'fields' => $object_fields,
1667
			);
1668
			wp_send_json_success( $ajax_response );
0 ignored issues
show
The function wp_send_json_success was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1668
			/** @scrutinizer ignore-call */ 
1669
   wp_send_json_success( $ajax_response );
Loading history...
1669
		} else {
1670
			return $object_fields;
1671
		}
1672
1673
	}
1674
1675
	/**
1676
	 * Get WordPress object fields for fieldmapping
1677
	 * This takes either the $_POST array via ajax, or can be directly called with a $wordpress_object field
1678
	 *
1679
	 * @param string $wordpress_object is the name of the WordPress object.
1680
	 * @return array $object_fields
1681
	 */
1682
	public function get_wordpress_object_fields( $wordpress_object = '' ) {
1683
		$ajax      = false;
1684
		$post_data = filter_input_array( INPUT_POST, FILTER_UNSAFE_RAW );
1685
		if ( empty( $wordpress_object ) ) {
1686
			$wordpress_object = isset( $post_data['wordpress_object'] ) ? sanitize_text_field( wp_unslash( $post_data['wordpress_object'] ) ) : '';
0 ignored issues
show
The function wp_unslash was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1686
			$wordpress_object = isset( $post_data['wordpress_object'] ) ? sanitize_text_field( /** @scrutinizer ignore-call */ wp_unslash( $post_data['wordpress_object'] ) ) : '';
Loading history...
The function sanitize_text_field was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1686
			$wordpress_object = isset( $post_data['wordpress_object'] ) ? /** @scrutinizer ignore-call */ sanitize_text_field( wp_unslash( $post_data['wordpress_object'] ) ) : '';
Loading history...
1687
			$ajax             = true;
1688
		}
1689
1690
		$object_fields = $this->wordpress->get_wordpress_object_fields( $wordpress_object );
1691
1692
		if ( true === $ajax ) {
1693
			$ajax_response = array(
1694
				'fields' => $object_fields,
1695
			);
1696
			wp_send_json_success( $ajax_response );
0 ignored issues
show
The function wp_send_json_success was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1696
			/** @scrutinizer ignore-call */ 
1697
   wp_send_json_success( $ajax_response );
Loading history...
1697
		} else {
1698
			return $object_fields;
1699
		}
1700
	}
1701
1702
	/**
1703
	 * Manually push the WordPress object to Salesforce
1704
	 * This takes either the $_POST array via ajax, or can be directly called with $wordpress_object and $wordpress_id fields
1705
	 *
1706
	 * @param string $wordpress_object is the name of the WordPress object.
1707
	 * @param int    $wordpress_id is the ID of the WordPress record.
1708
	 * @param bool   $force_return Force the method to return json instead of outputting it.
1709
	 */
1710
	public function push_to_salesforce( $wordpress_object = '', $wordpress_id = '', $force_return = false ) {
1711
		$post_data = filter_input_array( INPUT_POST, FILTER_UNSAFE_RAW );
1712
		if ( empty( $wordpress_object ) && empty( $wordpress_id ) ) {
1713
			$wordpress_object = isset( $post_data['wordpress_object'] ) ? sanitize_text_field( wp_unslash( $post_data['wordpress_object'] ) ) : '';
0 ignored issues
show
The function wp_unslash was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1713
			$wordpress_object = isset( $post_data['wordpress_object'] ) ? sanitize_text_field( /** @scrutinizer ignore-call */ wp_unslash( $post_data['wordpress_object'] ) ) : '';
Loading history...
The function sanitize_text_field was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1713
			$wordpress_object = isset( $post_data['wordpress_object'] ) ? /** @scrutinizer ignore-call */ sanitize_text_field( wp_unslash( $post_data['wordpress_object'] ) ) : '';
Loading history...
1714
			$wordpress_id     = isset( $post_data['wordpress_id'] ) ? absint( $post_data['wordpress_id'] ) : '';
0 ignored issues
show
The function absint was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1714
			$wordpress_id     = isset( $post_data['wordpress_id'] ) ? /** @scrutinizer ignore-call */ absint( $post_data['wordpress_id'] ) : '';
Loading history...
1715
		}
1716
1717
		// clarify what that variable is in this context.
1718
		$object_type = $wordpress_object;
1719
1720
		// When objects are already mapped, there is a Salesforce id as well. Otherwise, it's blank.
1721
		$salesforce_id = isset( $post_data['salesforce_id'] ) ? sanitize_text_field( $post_data['salesforce_id'] ) : '';
1722
		if ( '' === $salesforce_id ) {
1723
			$method = 'POST';
1724
		} else {
1725
			$method = 'PUT';
1726
		}
1727
1728
		$result = $this->push->manual_push( $object_type, $wordpress_id, $method );
1729
1730
		if ( false === $force_return && ! empty( $post_data['wordpress_object'] ) && ! empty( $post_data['wordpress_id'] ) ) {
1731
			wp_send_json_success( $result );
0 ignored issues
show
The function wp_send_json_success was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1731
			/** @scrutinizer ignore-call */ 
1732
   wp_send_json_success( $result );
Loading history...
1732
		} else {
1733
			return $result;
1734
		}
1735
1736
	}
1737
1738
	/**
1739
	 * Manually pull the Salesforce object into WordPress
1740
	 * This takes either the $_POST array via ajax, or can be directly called with $salesforce_id fields
1741
	 *
1742
	 * @param string $salesforce_id is the ID of the Salesforce record.
1743
	 * @param string $wordpress_object is the name of the WordPress object.
1744
	 */
1745
	public function pull_from_salesforce( $salesforce_id = '', $wordpress_object = '' ) {
1746
		$post_data = filter_input_array( INPUT_POST, FILTER_UNSAFE_RAW );
1747
		if ( empty( $wordpress_object ) && empty( $salesforce_id ) ) {
1748
			$wordpress_object = isset( $post_data['wordpress_object'] ) ? sanitize_text_field( wp_unslash( $post_data['wordpress_object'] ) ) : '';
0 ignored issues
show
The function sanitize_text_field was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1748
			$wordpress_object = isset( $post_data['wordpress_object'] ) ? /** @scrutinizer ignore-call */ sanitize_text_field( wp_unslash( $post_data['wordpress_object'] ) ) : '';
Loading history...
The function wp_unslash was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1748
			$wordpress_object = isset( $post_data['wordpress_object'] ) ? sanitize_text_field( /** @scrutinizer ignore-call */ wp_unslash( $post_data['wordpress_object'] ) ) : '';
Loading history...
1749
			$salesforce_id    = isset( $post_data['salesforce_id'] ) ? sanitize_text_field( wp_unslash( $post_data['salesforce_id'] ) ) : '';
1750
		}
1751
		$type   = $this->salesforce['sfapi']->get_sobject_type( $salesforce_id );
1752
		$result = $this->pull->manual_pull( $type, $salesforce_id, $wordpress_object ); // we want the wp object to make sure we get the right fieldmap.
1753
		if ( ! empty( $post_data ) ) {
1754
			wp_send_json_success( $result );
0 ignored issues
show
The function wp_send_json_success was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1754
			/** @scrutinizer ignore-call */ 
1755
   wp_send_json_success( $result );
Loading history...
1755
		} else {
1756
			return $result;
1757
		}
1758
	}
1759
1760
	/**
1761
	 * Manually pull the Salesforce object into WordPress
1762
	 * This takes an id for a mapping object row
1763
	 *
1764
	 * @param int $mapping_id is the ID of the mapping object record.
1765
	 */
1766
	public function refresh_mapped_data( $mapping_id = '' ) {
1767
		$post_data = filter_input_array( INPUT_POST, FILTER_UNSAFE_RAW );
1768
		if ( empty( $mapping_id ) ) {
1769
			$mapping_id = isset( $post_data['mapping_id'] ) ? absint( $post_data['mapping_id'] ) : '';
0 ignored issues
show
The function absint was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1769
			$mapping_id = isset( $post_data['mapping_id'] ) ? /** @scrutinizer ignore-call */ absint( $post_data['mapping_id'] ) : '';
Loading history...
1770
		}
1771
		$result = $this->mappings->get_all_object_maps(
1772
			array(
1773
				'id' => $mapping_id,
1774
			)
1775
		);
1776
1777
		$object_map = array();
1778
1779
		// result is an array of arrays, not just one array.
1780
		if ( 1 === count( $result ) ) {
1781
			$object_map = $result[0];
1782
		}
1783
1784
		if ( ! empty( $post_data ) ) {
1785
			wp_send_json_success( $object_map );
0 ignored issues
show
The function wp_send_json_success was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1785
			/** @scrutinizer ignore-call */ 
1786
   wp_send_json_success( $object_map );
Loading history...
1786
		} else {
1787
			return $object_map;
1788
		}
1789
	}
1790
1791
	/**
1792
	 * Prepare fieldmap data and redirect after processing
1793
	 * This runs when the create or update forms are submitted
1794
	 * It is public because it depends on an admin hook
1795
	 * It then calls the Object_Sync_Sf_Mapping class and sends prepared data over to it, then redirects to the correct page
1796
	 * This method does include error handling, by loading the submission in a transient if there is an error, and then deleting it upon success
1797
	 */
1798
	public function prepare_fieldmap_data() {
1799
		$error     = false;
1800
		$post_data = filter_input_array( INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS );
1801
		$cachekey  = wp_json_encode( $post_data );
0 ignored issues
show
The function wp_json_encode was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1801
		$cachekey  = /** @scrutinizer ignore-call */ wp_json_encode( $post_data );
Loading history...
1802
		if ( false !== $cachekey ) {
1803
			$cachekey = md5( $cachekey );
1804
		}
1805
1806
		if ( ! isset( $post_data['label'] ) || ! isset( $post_data['salesforce_object'] ) || ! isset( $post_data['wordpress_object'] ) ) {
1807
			$error = true;
1808
		}
1809
		if ( true === $error ) {
1810
			$this->sfwp_transients->set( $cachekey, $post_data, $this->wordpress->options['cache_expiration'] );
1811
			if ( '' !== $cachekey ) {
1812
				$url = esc_url_raw( $post_data['redirect_url_error'] ) . '&transient=' . $cachekey;
0 ignored issues
show
The function esc_url_raw was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1812
				$url = /** @scrutinizer ignore-call */ esc_url_raw( $post_data['redirect_url_error'] ) . '&transient=' . $cachekey;
Loading history...
1813
			}
1814
		} else { // there are no errors
1815
			// send the row to the fieldmap class
1816
			// if it is add or clone, use the create method.
1817
			$method            = esc_attr( $post_data['method'] );
0 ignored issues
show
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1817
			$method            = /** @scrutinizer ignore-call */ esc_attr( $post_data['method'] );
Loading history...
1818
			$salesforce_fields = $this->get_salesforce_object_fields(
1819
				array(
1820
					'salesforce_object' => $post_data['salesforce_object'],
1821
				)
1822
			);
1823
			$wordpress_fields  = $this->get_wordpress_object_fields( $post_data['wordpress_object'] );
1824
			if ( 'add' === $method || 'clone' === $method ) {
1825
				$result = $this->mappings->create_fieldmap( $post_data, $wordpress_fields, $salesforce_fields );
1826
			} elseif ( 'edit' === $method ) { // if it is edit, use the update method.
1827
				$id     = esc_attr( $post_data['id'] );
1828
				$result = $this->mappings->update_fieldmap( $post_data, $wordpress_fields, $salesforce_fields, $id );
1829
			}
1830
			if ( false === $result ) { // if the database didn't save, it's still an error.
1831
				$this->sfwp_transients->set( $cachekey, $post_data, $this->wordpress->options['cache_expiration'] );
1832
				if ( '' !== $cachekey ) {
1833
					$url = esc_url_raw( $post_data['redirect_url_error'] ) . '&transient=' . $cachekey;
1834
				}
1835
			} else {
1836
				// if the user has saved a fieldmap, clear the currently running query value if there is one.
1837
				if ( '' !== get_option( $this->option_prefix . 'currently_pulling_query_' . $post_data['salesforce_object'], '' ) ) {
0 ignored issues
show
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1837
				if ( '' !== /** @scrutinizer ignore-call */ get_option( $this->option_prefix . 'currently_pulling_query_' . $post_data['salesforce_object'], '' ) ) {
Loading history...
1838
					$this->pull->clear_current_type_query( $post_data['salesforce_object'] );
1839
				}
1840
				if ( isset( $post_data['transient'] ) ) { // there was previously an error saved. can delete it now.
1841
					$this->sfwp_transients->delete( esc_attr( $post_data['map_transient'] ) );
1842
				}
1843
				// then send the user to the list of fieldmaps.
1844
				$url = esc_url_raw( $post_data['redirect_url_success'] );
1845
			}
1846
		}
1847
		wp_safe_redirect( $url );
0 ignored issues
show
The function wp_safe_redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1847
		/** @scrutinizer ignore-call */ 
1848
  wp_safe_redirect( $url );
Loading history...
1848
		exit();
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
1849
	}
1850
1851
	/**
1852
	 * Delete fieldmap data and redirect after processing
1853
	 * This runs when the delete link is clicked, after the user confirms
1854
	 * It is public because it depends on an admin hook
1855
	 * It then calls the Object_Sync_Sf_Mapping class and the delete method
1856
	 */
1857
	public function delete_fieldmap() {
1858
		$post_data = filter_input_array( INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS );
1859
		if ( $post_data['id'] ) {
1860
			$result = $this->mappings->delete_fieldmap( $post_data['id'] );
1861
			if ( true === $result ) {
1862
				$url = esc_url_raw( $post_data['redirect_url_success'] );
0 ignored issues
show
The function esc_url_raw was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1862
				$url = /** @scrutinizer ignore-call */ esc_url_raw( $post_data['redirect_url_success'] );
Loading history...
1863
			} else {
1864
				$url = esc_url_raw( $post_data['redirect_url_error'] . '&id=' . $post_data['id'] );
1865
			}
1866
			wp_safe_redirect( $url );
0 ignored issues
show
The function wp_safe_redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1866
			/** @scrutinizer ignore-call */ 
1867
   wp_safe_redirect( $url );
Loading history...
1867
			exit();
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
1868
		}
1869
	}
1870
1871
	/**
1872
	 * Prepare object data and redirect after processing
1873
	 * This runs when the update form is submitted
1874
	 * It is public because it depends on an admin hook
1875
	 * It then calls the Object_Sync_Sf_Mapping class and sends prepared data over to it, then redirects to the correct page
1876
	 * This method does include error handling, by loading the submission in a transient if there is an error, and then deleting it upon success
1877
	 */
1878
	public function prepare_object_map_data() {
1879
		$error     = false;
1880
		$post_data = filter_input_array( INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS );
1881
		$cachekey  = wp_json_encode( $post_data );
0 ignored issues
show
The function wp_json_encode was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1881
		$cachekey  = /** @scrutinizer ignore-call */ wp_json_encode( $post_data );
Loading history...
1882
		if ( false !== $cachekey ) {
1883
			$cachekey = md5( $cachekey );
1884
		}
1885
1886
		if ( ! isset( $post_data['wordpress_id'] ) || ! isset( $post_data['salesforce_id'] ) ) {
1887
			$error = true;
1888
		}
1889
		if ( true === $error ) {
1890
			$this->sfwp_transients->set( $cachekey, $post_data, $this->wordpress->options['cache_expiration'] );
1891
			if ( '' !== $cachekey ) {
1892
				$url = esc_url_raw( $post_data['redirect_url_error'] ) . '&map_transient=' . $cachekey;
0 ignored issues
show
The function esc_url_raw was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1892
				$url = /** @scrutinizer ignore-call */ esc_url_raw( $post_data['redirect_url_error'] ) . '&map_transient=' . $cachekey;
Loading history...
1893
			}
1894
		} else { // there are no errors
1895
			// send the row to the object map class.
1896
			$method = esc_attr( $post_data['method'] );
0 ignored issues
show
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1896
			$method = /** @scrutinizer ignore-call */ esc_attr( $post_data['method'] );
Loading history...
1897
			if ( 'edit' === $method ) { // if it is edit, use the update method.
1898
				$id     = esc_attr( $post_data['id'] );
1899
				$result = $this->mappings->update_object_map( $post_data, $id );
1900
			}
1901
			if ( false === $result ) { // if the database didn't save, it's still an error.
1902
				$this->sfwp_transients->set( $cachekey, $post_data, $this->wordpress->options['cache_expiration'] );
1903
				if ( '' !== $cachekey ) {
1904
					$url = esc_url_raw( $post_data['redirect_url_error'] ) . '&map_transient=' . $cachekey;
1905
				}
1906
			} else {
1907
				if ( isset( $post_data['map_transient'] ) ) { // there was previously an error saved. can delete it now.
1908
					$this->sfwp_transients->delete( esc_attr( $post_data['map_transient'] ) );
1909
				}
1910
				// then send the user to the success redirect url.
1911
				$url = esc_url_raw( $post_data['redirect_url_success'] );
1912
			}
1913
		}
1914
		wp_safe_redirect( $url );
0 ignored issues
show
The function wp_safe_redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1914
		/** @scrutinizer ignore-call */ 
1915
  wp_safe_redirect( $url );
Loading history...
1915
		exit();
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
1916
	}
1917
1918
	/**
1919
	 * Delete object map data and redirect after processing
1920
	 * This runs when the delete link is clicked on an error row, after the user confirms
1921
	 * It is public because it depends on an admin hook
1922
	 * It then calls the Object_Sync_Sf_Mapping class and the delete method
1923
	 */
1924
	public function delete_object_map() {
1925
		$post_data = filter_input_array( INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS );
1926
		if ( isset( $post_data['id'] ) ) {
1927
			$result = $this->mappings->delete_object_map( $post_data['id'] );
1928
			if ( true === $result ) {
1929
				$url = esc_url_raw( $post_data['redirect_url_success'] );
0 ignored issues
show
The function esc_url_raw was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1929
				$url = /** @scrutinizer ignore-call */ esc_url_raw( $post_data['redirect_url_success'] );
Loading history...
1930
			} else {
1931
				$url = esc_url_raw( $post_data['redirect_url_error'] . '&id=' . $post_data['id'] );
1932
			}
1933
			wp_safe_redirect( $url );
0 ignored issues
show
The function wp_safe_redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1933
			/** @scrutinizer ignore-call */ 
1934
   wp_safe_redirect( $url );
Loading history...
1934
			exit();
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
1935
		} elseif ( $post_data['delete'] ) {
1936
			$post_data = filter_input_array( INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS );
1937
			$cachekey  = wp_json_encode( $post_data );
0 ignored issues
show
The function wp_json_encode was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1937
			$cachekey  = /** @scrutinizer ignore-call */ wp_json_encode( $post_data );
Loading history...
1938
			if ( false !== $cachekey ) {
1939
				$cachekey = md5( $cachekey );
1940
			}
1941
			$error = false;
1942
			if ( ! isset( $post_data['delete'] ) ) {
1943
				$error = true;
1944
			}
1945
			if ( true === $error ) {
1946
				$this->sfwp_transients->set( $cachekey, $post_data, $this->wordpress->options['cache_expiration'] );
1947
				if ( '' !== $cachekey ) {
1948
					$url = esc_url_raw( $post_data['redirect_url_error'] ) . '&mapping_error_transient=' . $cachekey;
1949
				}
1950
			} else { // there are no errors.
1951
				$result = $this->mappings->delete_object_map( array_keys( $post_data['delete'] ) );
1952
				if ( true === $result ) {
1953
					$url = esc_url_raw( $post_data['redirect_url_success'] );
1954
				}
1955
1956
				if ( false === $result ) { // if the database didn't save, it's still an error.
1957
					$this->sfwp_transients->set( $cachekey, $post_data, $this->wordpress->options['cache_expiration'] );
1958
					if ( '' !== $cachekey ) {
1959
						$url = esc_url_raw( $post_data['redirect_url_error'] ) . '&mapping_error_transient=' . $cachekey;
1960
					}
1961
				} else {
1962
					if ( isset( $post_data['mapping_error_transient'] ) ) { // there was previously an error saved. can delete it now.
1963
						$this->sfwp_transients->delete( esc_attr( $post_data['mapping_error_transient'] ) );
0 ignored issues
show
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1963
						$this->sfwp_transients->delete( /** @scrutinizer ignore-call */ esc_attr( $post_data['mapping_error_transient'] ) );
Loading history...
1964
					}
1965
					// then send the user to the list of fieldmaps.
1966
					$url = esc_url_raw( $post_data['redirect_url_success'] );
1967
				}
1968
			}
1969
			wp_safe_redirect( $url );
1970
			exit();
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
1971
		}
1972
	}
1973
1974
	/**
1975
	 * Import a json file and use it for plugin data
1976
	 */
1977
	public function import_json_file() {
1978
1979
		if ( ! wp_verify_nonce( $_POST['object_sync_for_salesforce_nonce_import'], 'object_sync_for_salesforce_nonce_import' ) ) {
0 ignored issues
show
The function wp_verify_nonce was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1979
		if ( ! /** @scrutinizer ignore-call */ wp_verify_nonce( $_POST['object_sync_for_salesforce_nonce_import'], 'object_sync_for_salesforce_nonce_import' ) ) {
Loading history...
1980
			return;
1981
		}
1982
		if ( ! current_user_can( 'manage_options' ) ) {
0 ignored issues
show
The function current_user_can was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1982
		if ( ! /** @scrutinizer ignore-call */ current_user_can( 'manage_options' ) ) {
Loading history...
1983
			return;
1984
		}
1985
		$path      = $_FILES['import_file']['name'];
1986
		$extension = pathinfo( $path, PATHINFO_EXTENSION );
1987
		if ( 'json' !== $extension ) {
1988
			wp_die( esc_html__( 'Please upload a valid .json file', 'object-sync-for-salesforce' ) );
0 ignored issues
show
The function wp_die was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1988
			/** @scrutinizer ignore-call */ 
1989
   wp_die( esc_html__( 'Please upload a valid .json file', 'object-sync-for-salesforce' ) );
Loading history...
The function esc_html__ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1988
			wp_die( /** @scrutinizer ignore-call */ esc_html__( 'Please upload a valid .json file', 'object-sync-for-salesforce' ) );
Loading history...
1989
		}
1990
1991
		$import_file = $_FILES['import_file']['tmp_name'];
1992
		if ( empty( $import_file ) ) {
1993
			wp_die( esc_html__( 'Please upload a file to import', 'object-sync-for-salesforce' ) );
1994
		}
1995
1996
		// Retrieve the data from the file and convert the json object to an array.
1997
		$data = (array) json_decode( file_get_contents( $import_file ), true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
1998
1999
		$overwrite = isset( $_POST['overwrite'] ) ? esc_attr( $_POST['overwrite'] ) : '';
0 ignored issues
show
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1999
		$overwrite = isset( $_POST['overwrite'] ) ? /** @scrutinizer ignore-call */ esc_attr( $_POST['overwrite'] ) : '';
Loading history...
2000
		if ( true === filter_var( $overwrite, FILTER_VALIDATE_BOOLEAN ) ) {
2001
			if ( isset( $data['fieldmaps'] ) ) {
2002
				$fieldmaps = $this->mappings->get_fieldmaps();
2003
				foreach ( $fieldmaps as $fieldmap ) {
2004
					$id     = $fieldmap['id'];
2005
					$delete = $this->mappings->delete_fieldmap( $id );
2006
				}
2007
			}
2008
			if ( isset( $data['object_maps'] ) ) {
2009
				$object_maps = $this->mappings->get_all_object_maps();
2010
				foreach ( $object_maps as $object_map ) {
2011
					$id     = $object_map['id'];
2012
					$delete = $this->mappings->delete_object_map( $id );
2013
				}
2014
			}
2015
			if ( isset( $data['plugin_settings'] ) ) {
2016
				foreach ( $data['plugin_settings'] as $key => $value ) {
2017
					delete_option( $value['option_name'] );
0 ignored issues
show
The function delete_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2017
					/** @scrutinizer ignore-call */ 
2018
     delete_option( $value['option_name'] );
Loading history...
2018
				}
2019
			}
2020
		}
2021
2022
		// if the option says to, set all the imported fieldmaps to inactive.
2023
		$import_fieldmaps_inactive = isset( $_POST['import_fieldmaps_inactive'] ) ? esc_attr( $_POST['import_fieldmaps_inactive'] ) : '';
2024
		if ( true === filter_var( $import_fieldmaps_inactive, FILTER_VALIDATE_BOOLEAN ) ) {
2025
			if ( isset( $data['fieldmaps'] ) ) {
2026
				foreach ( $data['fieldmaps'] as $key => $fieldmap ) {
2027
					$data['fieldmaps'][ $key ]['fieldmap_status'] = 'inactive';
2028
				}
2029
			}
2030
		}
2031
2032
		$success = true;
2033
2034
		if ( isset( $data['fieldmaps'] ) ) {
2035
			$successful_fieldmaps = array();
2036
			$error_fieldmaps      = array();
2037
			foreach ( $data['fieldmaps'] as $fieldmap ) {
2038
				unset( $fieldmap['id'] );
2039
				$create = $this->mappings->create_fieldmap( $fieldmap );
2040
				if ( false === $create ) {
2041
					$success = false;
2042
				}
2043
				if ( false === $create ) {
2044
					$error_fieldmaps[] = $fieldmap;
2045
				} else {
2046
					$successful_fieldmaps[] = $create;
2047
				}
2048
			}
2049
		}
2050
2051
		if ( isset( $data['object_maps'] ) ) {
2052
			$successful_object_maps = array();
2053
			$error_object_maps      = array();
2054
			foreach ( $data['object_maps'] as $object_map ) {
2055
				unset( $object_map['id'] );
2056
				if ( isset( $object_map['object_type'] ) ) {
2057
					$sf_sync_trigger = $this->mappings->sync_sf_create;
2058
					$create          = $this->pull->salesforce_pull_process_records( $object_map['object_type'], $object_map['salesforce_id'], $sf_sync_trigger );
2059
				} else {
2060
					$create = $this->mappings->create_object_map( $object_map );
2061
				}
2062
				if ( false === $create ) {
2063
					$error_object_maps[] = $object_map;
2064
				} else {
2065
					$successful_object_maps[] = $create;
2066
				}
2067
			}
2068
		}
2069
2070
		if ( isset( $data['plugin_settings'] ) ) {
2071
			foreach ( $data['plugin_settings'] as $key => $value ) {
2072
				update_option( $value['option_name'], maybe_unserialize( $value['option_value'] ), $value['autoload'] );
0 ignored issues
show
The function maybe_unserialize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2072
				update_option( $value['option_name'], /** @scrutinizer ignore-call */ maybe_unserialize( $value['option_value'] ), $value['autoload'] );
Loading history...
The function update_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2072
				/** @scrutinizer ignore-call */ 
2073
    update_option( $value['option_name'], maybe_unserialize( $value['option_value'] ), $value['autoload'] );
Loading history...
2073
			}
2074
		}
2075
2076
		if ( ! empty( $error_fieldmaps ) && ! empty( $error_object_maps ) ) {
2077
			$status = 'error';
2078
			$body   = sprintf( esc_html__( 'These are the import items that were not able to save: ', 'object-sync-for-salesforce' ) . '<ul>' );
2079
			foreach ( $error_fieldmaps as $fieldmap ) {
2080
				$body .= sprintf(
2081
					// translators: placeholders are: 1) the fieldmap row ID, 2) the Salesforce object type, 3) the WordPress object type.
2082
					'<li>' . esc_html__( 'Fieldmap id (if it exists): %1$s. Salesforce object type: %2$s. WordPress object type: %3$s', 'object-sync-for-salesforce' ) . '</li>',
2083
					isset( $fieldmap['id'] ) ? absint( $fieldmap['id'] ) : '',
0 ignored issues
show
The function absint was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2083
					isset( $fieldmap['id'] ) ? /** @scrutinizer ignore-call */ absint( $fieldmap['id'] ) : '',
Loading history...
2084
					esc_attr( $fieldmap['salesforce_object'] ),
2085
					esc_attr( $fieldmap['wordpress_object'] )
2086
				);
2087
			}
2088
			foreach ( $error_object_maps as $mapping_object ) {
2089
				$body .= sprintf(
2090
					// translators: placeholders are: 1) the mapping object row ID, 2) the ID of the Salesforce object, 3) the WordPress object type.
2091
					'<li>' . esc_html__( 'Mapping object id (if it exists): %1$s. Salesforce Id: %2$s. WordPress object type: %3$s', 'object-sync-for-salesforce' ) . '</li>',
2092
					isset( $mapping_object['id'] ) ? absint( $mapping_object['id'] ) : '',
2093
					esc_attr( $mapping_object['salesforce_id'] ),
2094
					esc_attr( $mapping_object['wordpress_object'] )
2095
				);
2096
			}
2097
			$body .= sprintf( '</ul>' );
2098
2099
			$this->logging->setup(
2100
				sprintf(
2101
					// translators: %1$s is the log status.
2102
					esc_html__( '%1$s on import: some of the rows were unable to save. Read this post for details.', 'object-sync-for-salesforce' ),
2103
					ucfirst( esc_attr( $status ) )
2104
				),
2105
				$body,
2106
				0,
2107
				0,
2108
				$status
2109
			);
2110
		}
2111
2112
		if ( empty( $error_fieldmaps ) && empty( $error_object_maps ) && ( ! empty( $successful_fieldmaps ) || ! empty( $successful_object_maps ) ) ) {
2113
			$this->clear_cache( false );
2114
			wp_safe_redirect( get_admin_url( null, 'options-general.php?page=' . $this->admin_settings_url_param . '&tab=import-export&data_saved=true' ) );
0 ignored issues
show
The function get_admin_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2114
			wp_safe_redirect( /** @scrutinizer ignore-call */ get_admin_url( null, 'options-general.php?page=' . $this->admin_settings_url_param . '&tab=import-export&data_saved=true' ) );
Loading history...
The function wp_safe_redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2114
			/** @scrutinizer ignore-call */ 
2115
   wp_safe_redirect( get_admin_url( null, 'options-general.php?page=' . $this->admin_settings_url_param . '&tab=import-export&data_saved=true' ) );
Loading history...
2115
			exit;
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
2116
		} elseif ( ! empty( $error_fieldmaps ) && ! empty( $successful_fieldmaps ) ) {
2117
			$this->clear_cache( false );
2118
			wp_safe_redirect( get_admin_url( null, 'options-general.php?page=' . $this->admin_settings_url_param . '&tab=import-export&data_saved=partial' ) );
2119
		} elseif ( ! empty( $error_object_maps ) && ! empty( $successful_object_maps ) ) {
2120
			$this->clear_cache( false );
2121
			wp_safe_redirect( get_admin_url( null, 'options-general.php?page=' . $this->admin_settings_url_param . '&tab=import-export&data_saved=partial' ) );
2122
		} else {
2123
			wp_safe_redirect( get_admin_url( null, 'options-general.php?page=' . $this->admin_settings_url_param . '&tab=import-export&data_saved=false' ) );
2124
			exit;
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
2125
		}
2126
2127
	}
2128
2129
	/**
2130
	 * Create a json file for exporting
2131
	 */
2132
	public function export_json_file() {
2133
2134
		if ( ! wp_verify_nonce( $_POST['object_sync_for_salesforce_nonce_export'], 'object_sync_for_salesforce_nonce_export' ) ) {
0 ignored issues
show
The function wp_verify_nonce was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2134
		if ( ! /** @scrutinizer ignore-call */ wp_verify_nonce( $_POST['object_sync_for_salesforce_nonce_export'], 'object_sync_for_salesforce_nonce_export' ) ) {
Loading history...
2135
			return;
2136
		}
2137
		if ( ! current_user_can( 'manage_options' ) ) {
0 ignored issues
show
The function current_user_can was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2137
		if ( ! /** @scrutinizer ignore-call */ current_user_can( 'manage_options' ) ) {
Loading history...
2138
			return;
2139
		}
2140
		$post_data = filter_input_array( INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS );
2141
		$export    = array();
2142
		if ( in_array( 'fieldmaps', $post_data['export'], true ) ) {
2143
			$export['fieldmaps'] = $this->mappings->get_fieldmaps();
2144
		}
2145
		if ( in_array( 'object_maps', $post_data['export'], true ) ) {
2146
			$export['object_maps'] = $this->mappings->get_all_object_maps();
2147
		}
2148
		if ( in_array( 'plugin_settings', $post_data['export'], true ) ) {
2149
			$wpdb                      = $this->wpdb;
2150
			$export_results            = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM `{$wpdb->base_prefix}options` WHERE option_name LIKE %s;", $wpdb->esc_like( $this->option_prefix ) . '%' ), ARRAY_A );
0 ignored issues
show
The constant ARRAY_A was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
2151
			$export['plugin_settings'] = $export_results;
2152
		}
2153
		nocache_headers();
0 ignored issues
show
The function nocache_headers was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2153
		/** @scrutinizer ignore-call */ 
2154
  nocache_headers();
Loading history...
2154
		header( 'Content-Type: application/json; charset=utf-8' );
2155
		header( 'Content-Disposition: attachment; filename=object-sync-for-salesforce-data-export-' . gmdate( 'm-d-Y' ) . '.json' );
2156
		header( 'Expires: 0' );
2157
		echo wp_json_encode( $export );
0 ignored issues
show
The function wp_json_encode was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2157
		echo /** @scrutinizer ignore-call */ wp_json_encode( $export );
Loading history...
2158
		exit;
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
2159
	}
2160
2161
	/**
2162
	 * Default display for <input> fields
2163
	 *
2164
	 * @param array $args is the arguments to create the field.
2165
	 */
2166
	public function display_input_field( $args ) {
2167
		$type    = $args['type'];
2168
		$id      = $args['label_for'];
2169
		$name    = $args['name'];
2170
		$desc    = $args['desc'];
2171
		$checked = '';
2172
2173
		$class = 'regular-text';
2174
2175
		if ( 'checkbox' === $type ) {
2176
			$class = 'checkbox';
2177
		}
2178
2179
		if ( isset( $args['class'] ) ) {
2180
			$class = $args['class'];
2181
		}
2182
2183
		if ( ! isset( $args['constant'] ) || ! defined( $args['constant'] ) ) {
2184
			$value = esc_attr( get_option( $id, '' ) );
0 ignored issues
show
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2184
			$value = esc_attr( /** @scrutinizer ignore-call */ get_option( $id, '' ) );
Loading history...
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2184
			$value = /** @scrutinizer ignore-call */ esc_attr( get_option( $id, '' ) );
Loading history...
2185
			if ( 'checkbox' === $type ) {
2186
				$value = filter_var( get_option( $id, false ), FILTER_VALIDATE_BOOLEAN );
2187
				if ( true === $value ) {
2188
					$checked = 'checked ';
2189
				}
2190
				$value = 1;
2191
			}
2192
			if ( '' === $value && isset( $args['default'] ) && '' !== $args['default'] ) {
2193
				$value = $args['default'];
2194
			}
2195
2196
			echo sprintf(
2197
				'<input type="%1$s" value="%2$s" name="%3$s" id="%4$s" class="%5$s"%6$s>',
2198
				esc_attr( $type ),
2199
				esc_attr( $value ),
2200
				esc_attr( $name ),
2201
				esc_attr( $id ),
2202
				sanitize_html_class( $class . esc_html( ' code' ) ),
0 ignored issues
show
The function esc_html was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2202
				sanitize_html_class( $class . /** @scrutinizer ignore-call */ esc_html( ' code' ) ),
Loading history...
The function sanitize_html_class was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2202
				/** @scrutinizer ignore-call */ 
2203
    sanitize_html_class( $class . esc_html( ' code' ) ),
Loading history...
2203
				esc_html( $checked )
2204
			);
2205
			if ( '' !== $desc ) {
2206
				echo sprintf(
2207
					'<p class="description">%1$s</p>',
2208
					esc_html( $desc )
2209
				);
2210
			}
2211
		} else {
2212
			echo sprintf(
2213
				'<p><code>%1$s</code></p>',
2214
				esc_html__( 'Defined in wp-config.php', 'object-sync-for-salesforce' )
0 ignored issues
show
The function esc_html__ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2214
				/** @scrutinizer ignore-call */ 
2215
    esc_html__( 'Defined in wp-config.php', 'object-sync-for-salesforce' )
Loading history...
2215
			);
2216
		}
2217
	}
2218
2219
	/**
2220
	 * Display for multiple checkboxes
2221
	 * Above method can handle a single checkbox as it is
2222
	 *
2223
	 * @param array $args is the arguments to create the checkboxes.
2224
	 */
2225
	public function display_checkboxes( $args ) {
2226
		$type       = 'checkbox';
2227
		$name       = $args['name'];
2228
		$options    = get_option( $name, array() );
0 ignored issues
show
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2228
		$options    = /** @scrutinizer ignore-call */ get_option( $name, array() );
Loading history...
2229
		$group_desc = $args['desc'];
2230
		foreach ( $args['items'] as $key => $value ) {
2231
			$text    = $value['text'];
2232
			$id      = $value['id'];
2233
			$desc    = $value['desc'];
2234
			$checked = '';
2235
			if ( is_array( $options ) && in_array( (string) $key, $options, true ) ) {
2236
				$checked = 'checked';
2237
			} elseif ( is_array( $options ) && empty( $options ) ) {
2238
				if ( isset( $value['default'] ) && true === $value['default'] ) {
2239
					$checked = 'checked';
2240
				}
2241
			}
2242
			echo sprintf(
2243
				'<div class="checkbox"><label><input type="%1$s" value="%2$s" name="%3$s[]" id="%4$s"%5$s>%6$s</label></div>',
2244
				esc_attr( $type ),
0 ignored issues
show
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2244
				/** @scrutinizer ignore-call */ 
2245
    esc_attr( $type ),
Loading history...
2245
				esc_attr( $key ),
2246
				esc_attr( $name ),
2247
				esc_attr( $id ),
2248
				esc_html( $checked ),
0 ignored issues
show
The function esc_html was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2248
				/** @scrutinizer ignore-call */ 
2249
    esc_html( $checked ),
Loading history...
2249
				esc_html( $text )
2250
			);
2251
			if ( '' !== $desc ) {
2252
				echo sprintf(
2253
					'<p class="description">%1$s</p>',
2254
					esc_html( $desc )
2255
				);
2256
			}
2257
		}
2258
		if ( '' !== $group_desc ) {
2259
			echo sprintf(
2260
				'<p class="description">%1$s</p>',
2261
				esc_html( $group_desc )
2262
			);
2263
		}
2264
	}
2265
2266
	/**
2267
	 * Display for a dropdown
2268
	 *
2269
	 * @param array $args is the arguments needed to create the dropdown.
2270
	 */
2271
	public function display_select( $args ) {
2272
		$type = $args['type'];
2273
		$id   = $args['label_for'];
2274
		$name = $args['name'];
2275
		$desc = $args['desc'];
2276
		if ( ! isset( $args['constant'] ) || ! defined( $args['constant'] ) ) {
2277
			$current_value = get_option( $name );
0 ignored issues
show
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2277
			$current_value = /** @scrutinizer ignore-call */ get_option( $name );
Loading history...
2278
2279
			echo sprintf(
2280
				'<div class="select"><select id="%1$s" name="%2$s"><option value="">- ' . esc_html__( 'Select one', 'object-sync-for-salesforce' ) . ' -</option>',
0 ignored issues
show
The function esc_html__ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2280
				'<div class="select"><select id="%1$s" name="%2$s"><option value="">- ' . /** @scrutinizer ignore-call */ esc_html__( 'Select one', 'object-sync-for-salesforce' ) . ' -</option>',
Loading history...
2281
				esc_attr( $id ),
0 ignored issues
show
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2281
				/** @scrutinizer ignore-call */ 
2282
    esc_attr( $id ),
Loading history...
2282
				esc_attr( $name )
2283
			);
2284
2285
			foreach ( $args['items'] as $key => $value ) {
2286
				$text     = $value['text'];
2287
				$value    = $value['value'];
2288
				$selected = '';
2289
				if ( $key === $current_value || $value === $current_value ) {
2290
					$selected = ' selected';
2291
				}
2292
2293
				echo sprintf(
2294
					'<option value="%1$s"%2$s>%3$s</option>',
2295
					esc_attr( $value ),
2296
					esc_attr( $selected ),
2297
					esc_html( $text )
0 ignored issues
show
The function esc_html was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2297
					/** @scrutinizer ignore-call */ 
2298
     esc_html( $text )
Loading history...
2298
				);
2299
2300
			}
2301
			echo '</select>';
2302
			if ( '' !== $desc ) {
2303
				echo sprintf(
2304
					'<p class="description">%1$s</p>',
2305
					esc_html( $desc )
2306
				);
2307
			}
2308
			echo '</div>';
2309
		} else {
2310
			echo sprintf(
2311
				'<p><code>%1$s</code></p>',
2312
				esc_html__( 'Defined in wp-config.php', 'object-sync-for-salesforce' )
2313
			);
2314
		}
2315
	}
2316
2317
	/**
2318
	 * Default display for <a href> links
2319
	 *
2320
	 * @param array $args is the arguments to make the link.
2321
	 */
2322
	public function display_link( $args ) {
2323
		$label = $args['label'];
2324
		$desc  = $args['desc'];
2325
		$url   = $args['url'];
2326
		if ( isset( $args['link_class'] ) ) {
2327
			echo sprintf(
2328
				'<p><a class="%1$s" href="%2$s">%3$s</a></p>',
2329
				esc_attr( $args['link_class'] ),
0 ignored issues
show
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2329
				/** @scrutinizer ignore-call */ 
2330
    esc_attr( $args['link_class'] ),
Loading history...
2330
				esc_url( $url ),
0 ignored issues
show
The function esc_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2330
				/** @scrutinizer ignore-call */ 
2331
    esc_url( $url ),
Loading history...
2331
				esc_html( $label )
0 ignored issues
show
The function esc_html was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2331
				/** @scrutinizer ignore-call */ 
2332
    esc_html( $label )
Loading history...
2332
			);
2333
		} else {
2334
			echo sprintf(
2335
				'<p><a href="%1$s">%2$s</a></p>',
2336
				esc_url( $url ),
2337
				esc_html( $label )
2338
			);
2339
		}
2340
2341
		if ( '' !== $desc ) {
2342
			echo sprintf(
2343
				'<p class="description">%1$s</p>',
2344
				esc_html( $desc )
2345
			);
2346
		}
2347
2348
	}
2349
2350
	/**
2351
	 * Allow for a standard sanitize/validate method. We could use more specific ones if need be, but this one provides a baseline.
2352
	 *
2353
	 * @param string $option is the option value.
2354
	 * @return string $option is the sanitized option value.
2355
	 */
2356
	public function sanitize_validate_text( $option ) {
2357
		if ( is_array( $option ) ) {
0 ignored issues
show
The condition is_array($option) is always false.
Loading history...
2358
			$options = array();
2359
			foreach ( $option as $key => $value ) {
2360
				$options[ $key ] = sanitize_text_field( $value );
0 ignored issues
show
The function sanitize_text_field was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2360
				$options[ $key ] = /** @scrutinizer ignore-call */ sanitize_text_field( $value );
Loading history...
2361
			}
2362
			return $options;
2363
		}
2364
		$option = sanitize_text_field( $option );
2365
		return $option;
2366
	}
2367
2368
	/**
2369
	 * Run a demo of Salesforce API call on the authenticate tab after WordPress has authenticated with it
2370
	 *
2371
	 * @param object $sfapi this is the Salesforce API object.
2372
	 */
2373
	private function status( $sfapi ) {
2374
2375
		$contacts = $sfapi->query( 'SELECT Name, Id from Contact LIMIT 100' );
2376
2377
		if ( 200 !== $contacts['code'] ) {
2378
			$contacts_apicall_summary = esc_html__( 'The plugin was unable to load a sample of Contacts from Salesforce. This may mean that there are connection issues, or that the authorization has expired.', 'object-sync-for-salesforce' );
0 ignored issues
show
The function esc_html__ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2378
			$contacts_apicall_summary = /** @scrutinizer ignore-call */ esc_html__( 'The plugin was unable to load a sample of Contacts from Salesforce. This may mean that there are connection issues, or that the authorization has expired.', 'object-sync-for-salesforce' );
Loading history...
2379
		} else {
2380
2381
			// format this array into html so users can see the contacts.
2382
			if ( true === $contacts['cached'] ) {
2383
				$contacts_is_cached = esc_html__( 'They are cached, and', 'object-sync-for-salesforce' );
2384
			} else {
2385
				$contacts_is_cached = esc_html__( 'They are not cached, but', 'object-sync-for-salesforce' );
2386
			}
2387
2388
			if ( true === $contacts['from_cache'] ) {
2389
				$contacts_from_cache = esc_html__( 'they were loaded from the cache', 'object-sync-for-salesforce' );
2390
			} else {
2391
				$contacts_from_cache = esc_html__( 'they were not loaded from the cache', 'object-sync-for-salesforce' );
2392
			}
2393
2394
			if ( true === $contacts['is_redo'] ) {
2395
				$contacts_refreshed_token = esc_html__( 'This request did require refreshing the Salesforce token', 'object-sync-for-salesforce' );
2396
			} else {
2397
				$contacts_refreshed_token = esc_html__( 'This request did not require refreshing the Salesforce token', 'object-sync-for-salesforce' );
2398
			}
2399
2400
			// display contact summary if there are any contacts.
2401
			if ( 0 < absint( $contacts['data']['totalSize'] ) ) {
0 ignored issues
show
The function absint was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2401
			if ( 0 < /** @scrutinizer ignore-call */ absint( $contacts['data']['totalSize'] ) ) {
Loading history...
2402
				$contacts_apicall_summary = sprintf(
2403
					// translators: 1) $contacts['data']['totalSize'] is the number of items loaded, 2) $contacts['data']['records'][0]['attributes']['type'] is the name of the Salesforce object, 3) $contacts_is_cached is the "They are/are not cached, and/but" line, 4) $contacts_from_cache is the "they were/were not loaded from the cache" line, 5) is the "this request did/did not require refreshing the Salesforce token" line.
2404
					esc_html__( 'Salesforce successfully returned %1$s %2$s records. %3$s %4$s. %5$s.', 'object-sync-for-salesforce' ),
2405
					absint( $contacts['data']['totalSize'] ),
2406
					esc_html( $contacts['data']['records'][0]['attributes']['type'] ),
0 ignored issues
show
The function esc_html was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2406
					/** @scrutinizer ignore-call */ 
2407
     esc_html( $contacts['data']['records'][0]['attributes']['type'] ),
Loading history...
2407
					$contacts_is_cached,
2408
					$contacts_from_cache,
2409
					$contacts_refreshed_token
2410
				);
2411
			} else {
2412
				$contacts_apicall_summary = '';
2413
			}
2414
		}
2415
2416
		require_once plugin_dir_path( $this->file ) . '/templates/admin/status.php';
0 ignored issues
show
The function plugin_dir_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2416
		require_once /** @scrutinizer ignore-call */ plugin_dir_path( $this->file ) . '/templates/admin/status.php';
Loading history...
2417
2418
	}
2419
2420
	/**
2421
	 * Deauthorize WordPress from Salesforce.
2422
	 * This deletes the tokens from the database; it does not currently do anything in Salesforce
2423
	 * For this plugin at this time, that is the decision we are making: don't do any kind of authorization stuff inside Salesforce
2424
	 */
2425
	private function logout() {
2426
		$delete_access_token = delete_option( $this->option_prefix . 'access_token' );
0 ignored issues
show
The function delete_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2426
		$delete_access_token = /** @scrutinizer ignore-call */ delete_option( $this->option_prefix . 'access_token' );
Loading history...
2427
		if ( true === $delete_access_token ) {
2428
			$this->access_token = '';
2429
		}
2430
		$delete_instance_url = delete_option( $this->option_prefix . 'instance_url' );
2431
		if ( true === $delete_instance_url ) {
2432
			$this->instance_url = '';
2433
		}
2434
		$delete_refresh_token = delete_option( $this->option_prefix . 'refresh_token' );
2435
		if ( true === $delete_refresh_token ) {
2436
			$this->refresh_token = '';
2437
		}
2438
		echo sprintf(
2439
			'<p>You have been logged out. You can use the <a href="%1$s">%2$s</a> tab to log in again.</p>',
2440
			esc_url( get_admin_url( null, 'options-general.php?page=' . $this->admin_settings_url_param . '&tab=authorize' ) ),
0 ignored issues
show
The function esc_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2440
			/** @scrutinizer ignore-call */ 
2441
   esc_url( get_admin_url( null, 'options-general.php?page=' . $this->admin_settings_url_param . '&tab=authorize' ) ),
Loading history...
The function get_admin_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2440
			esc_url( /** @scrutinizer ignore-call */ get_admin_url( null, 'options-general.php?page=' . $this->admin_settings_url_param . '&tab=authorize' ) ),
Loading history...
2441
			esc_html__( 'Authorize', 'object-sync-for-salesforce' )
0 ignored issues
show
The function esc_html__ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2441
			/** @scrutinizer ignore-call */ 
2442
   esc_html__( 'Authorize', 'object-sync-for-salesforce' )
Loading history...
2442
		);
2443
	}
2444
2445
	/**
2446
	 * Ajax call to clear the plugin cache.
2447
	 */
2448
	public function clear_sfwp_cache() {
2449
		$result   = $this->clear_cache( true );
2450
		$response = array(
2451
			'message' => $result['message'],
2452
			'success' => $result['success'],
2453
		);
2454
		wp_send_json_success( $response );
0 ignored issues
show
The function wp_send_json_success was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2454
		/** @scrutinizer ignore-call */ 
2455
  wp_send_json_success( $response );
Loading history...
2455
	}
2456
2457
	/**
2458
	 * Ajax call to delete the object_sync_for_salesforce_api_version value from wp_options
2459
	 *
2460
	 * @deprecated and will be removed in 3.0.0.
2461
	 */
2462
	public function delete_salesforce_api_version() {
2463
		$deprecated_option_key = $this->option_prefix . 'api_version';
2464
		$result                = delete_option( $deprecated_option_key );
0 ignored issues
show
The function delete_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2464
		$result                = /** @scrutinizer ignore-call */ delete_option( $deprecated_option_key );
Loading history...
2465
		if ( true === $result ) {
2466
			$message = sprintf(
2467
				// translators: the parameter is the option key where the API version was stored.
2468
				esc_html__( 'The %1$s value was successfully deleted.', 'object-sync-for-salesforce' ),
0 ignored issues
show
The function esc_html__ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2468
				/** @scrutinizer ignore-call */ 
2469
    esc_html__( 'The %1$s value was successfully deleted.', 'object-sync-for-salesforce' ),
Loading history...
2469
				'<code>' . $deprecated_option_key . '</code>'
2470
			);
2471
		} else {
2472
			$message = sprintf(
2473
				// translators: the parameter is the option key where the API version was stored.
2474
				esc_html__( 'There was an error in deleting the %1$s value. This may mean you have stored the value in a wp-config.php file instaed of in the options table. If this is not the case, you may want to reload the page and try again.', 'object-sync-for-salesforce' ),
2475
				'<code>' . $deprecated_option_key . '</code>'
2476
			);
2477
		}
2478
		$response = array(
2479
			'message' => $message,
2480
			'success' => $result,
2481
		);
2482
		wp_send_json_success( $response );
0 ignored issues
show
The function wp_send_json_success was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2482
		/** @scrutinizer ignore-call */ 
2483
  wp_send_json_success( $response );
Loading history...
2483
	}
2484
2485
	/**
2486
	 * Clear the plugin's cache.
2487
	 * This uses the flush method contained in the WordPress cache to clear all of this plugin's cached data.
2488
	 *
2489
	 * @param bool $ajax Whether this is an Ajax request or not.
2490
	 * @return array
2491
	 */
2492
	private function clear_cache( $ajax = false ) {
2493
		$result  = $this->wordpress->sfwp_transients->flush();
2494
		$success = $result['success'];
2495
		if ( 0 < $result['count'] ) {
2496
			if ( true === $success ) {
2497
				$message = __( 'The plugin cache has been cleared.', 'object-sync-for-salesforce' );
0 ignored issues
show
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2497
				$message = /** @scrutinizer ignore-call */ __( 'The plugin cache has been cleared.', 'object-sync-for-salesforce' );
Loading history...
2498
			} else {
2499
				$message = __( 'There was an error clearing the plugin cache. Try refreshing this page.', 'object-sync-for-salesforce' );
2500
			}
2501
		} else {
2502
			$success = true;
2503
			$message = __( 'The cache was not cleared because it is empty. You can try again later.', 'object-sync-for-salesforce' );
2504
		}
2505
		if ( false === $ajax ) {
2506
			echo '<p>' . esc_html( $message ) . '</p>';
0 ignored issues
show
The function esc_html was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2506
			echo '<p>' . /** @scrutinizer ignore-call */ esc_html( $message ) . '</p>';
Loading history...
2507
		} else {
2508
			return array(
2509
				'message' => esc_html( $message ),
2510
				'success' => $success,
2511
			);
2512
		}
2513
	}
2514
2515
	/**
2516
	 * Check WordPress Admin permissions
2517
	 * Check if the current user is allowed to access the Salesforce plugin options
2518
	 */
2519
	private function check_wordpress_admin_permissions() {
2520
2521
		// one programmatic way to give this capability to additional user roles is the
2522
		// object_sync_for_salesforce_roles_configure_salesforce hook
2523
		// it runs on activation of this plugin, and will assign the below capability to any role
2524
		// coming from the hook.
2525
2526
		// alternatively, other roles can get this capability in whatever other way you like
2527
		// point is: to administer this plugin, you need this capability.
2528
2529
		if ( ! current_user_can( 'configure_salesforce' ) ) {
0 ignored issues
show
The function current_user_can was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2529
		if ( ! /** @scrutinizer ignore-call */ current_user_can( 'configure_salesforce' ) ) {
Loading history...
2530
			return false;
2531
		} else {
2532
			return true;
2533
		}
2534
2535
	}
2536
2537
	/**
2538
	 * Check WordPress SSL status.
2539
	 * HTTPS is required to connect to Salesforce.
2540
	 *
2541
	 * @return bool $secure_admin
2542
	 */
2543
	private function check_wordpress_ssl() {
2544
2545
		// the wp_is_using_https() function was added in WordPress 5.7.
2546
		if ( ! function_exists( 'wp_is_using_https' ) ) {
2547
			return true;
2548
		}
2549
2550
		// the whole site is already using SSL.
2551
		if ( true === wp_is_using_https() ) {
2552
			return true;
2553
		}
2554
2555
		$secure_admin = false;
2556
2557
		// the admin is already forced to use SSL.
2558
		if ( true === FORCE_SSL_ADMIN ) {
0 ignored issues
show
The constant FORCE_SSL_ADMIN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
2559
			$secure_admin = true;
2560
		}
2561
2562
		// the server reports that the current URL is using HTTPS.
2563
		if ( isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ) {
2564
			$secure_admin = true;
2565
		}
2566
2567
		return $secure_admin;
2568
	}
2569
2570
	/**
2571
	 * Check WordPress SSL support.
2572
	 * This allows us to show a different message if SSL is supported, but is not in use.
2573
	 *
2574
	 * @return bool $https_supported
2575
	 */
2576
	private function check_wordpress_ssl_support() {
2577
2578
		// the wp_is_https_supported() function was added in WordPress 5.7.
2579
		if ( ! function_exists( 'wp_is_https_supported' ) ) {
2580
			return true;
2581
		}
2582
2583
		if ( true === wp_is_https_supported() ) {
2584
			return true;
2585
		}
2586
2587
		$https_supported = false;
2588
		return $https_supported;
2589
2590
	}
2591
2592
	/**
2593
	 * Show what we know about this user's relationship to a Salesforce object, if any
2594
	 *
2595
	 * @param object $user this is the user object from WordPress.
2596
	 */
2597
	public function show_salesforce_user_fields( $user ) {
2598
		$get_data = filter_input_array( INPUT_GET, FILTER_SANITIZE_SPECIAL_CHARS );
2599
		if ( true === $this->check_wordpress_admin_permissions() ) {
2600
			$mappings = $this->mappings->load_all_by_wordpress( 'user', $user->ID );
2601
			$fieldmap = $this->mappings->get_fieldmaps(
2602
				null, // id field must be null for multiples.
2603
				array(
2604
					'wordpress_object' => 'user',
2605
				)
2606
			);
2607
			if ( count( $mappings ) > 0 ) {
2608
				foreach ( $mappings as $mapping ) {
2609
					if ( isset( $mapping['id'] ) && ! isset( $get_data['edit_salesforce_mapping'] ) && ! isset( $get_data['delete_salesforce_mapping'] ) ) {
2610
						require_once plugin_dir_path( $this->file ) . '/templates/admin/user-profile-salesforce.php';
0 ignored issues
show
The function plugin_dir_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2610
						require_once /** @scrutinizer ignore-call */ plugin_dir_path( $this->file ) . '/templates/admin/user-profile-salesforce.php';
Loading history...
2611
					} elseif ( ! empty( $fieldmap ) ) { // is the user mapped to something already?
2612
						if ( isset( $get_data['edit_salesforce_mapping'] ) && true === filter_var( $get_data['edit_salesforce_mapping'], FILTER_VALIDATE_BOOLEAN ) ) {
2613
							require_once plugin_dir_path( $this->file ) . '/templates/admin/user-profile-salesforce-change.php';
2614
						} elseif ( isset( $get_data['delete_salesforce_mapping'] ) && true === filter_var( $get_data['delete_salesforce_mapping'], FILTER_VALIDATE_BOOLEAN ) ) {
2615
							require_once plugin_dir_path( $this->file ) . '/templates/admin/user-profile-salesforce-delete.php';
2616
						} else {
2617
							require_once plugin_dir_path( $this->file ) . '/templates/admin/user-profile-salesforce-map.php';
2618
						}
2619
					}
2620
				}
2621
			} else {
2622
				require_once plugin_dir_path( $this->file ) . '/templates/admin/user-profile-salesforce-map.php';
2623
			}
2624
		}
2625
	}
2626
2627
	/**
2628
	 * If the user profile has been mapped to Salesforce, do it
2629
	 *
2630
	 * @param int $user_id the ID of the WordPress user.
2631
	 */
2632
	public function save_salesforce_user_fields( $user_id ) {
2633
		$post_data = filter_input_array( INPUT_POST, FILTER_UNSAFE_RAW );
2634
		if ( isset( $post_data['salesforce_update_mapped_user'] ) && true === filter_var( $post_data['salesforce_update_mapped_user'], FILTER_VALIDATE_BOOLEAN ) ) {
2635
			$mapping_objects = $this->mappings->get_all_object_maps(
2636
				array(
2637
					'wordpress_id'     => $user_id,
2638
					'wordpress_object' => 'user',
2639
				)
2640
			);
2641
			foreach ( $mapping_objects as $mapping_object ) {
2642
				$mapping_object['salesforce_id'] = $post_data['salesforce_id'];
2643
				$result                          = $this->mappings->update_object_map( $mapping_object, $mapping_object['id'] );
2644
			}
2645
		} elseif ( isset( $post_data['salesforce_create_mapped_user'] ) && true === filter_var( $post_data['salesforce_create_mapped_user'], FILTER_VALIDATE_BOOLEAN ) ) {
2646
			// if a Salesforce ID was entered.
2647
			if ( isset( $post_data['salesforce_id'] ) && ! empty( $post_data['salesforce_id'] ) ) {
2648
				$mapping_object = $this->create_object_map( $user_id, 'user', $post_data['salesforce_id'] );
2649
			} elseif ( isset( $post_data['push_new_user_to_salesforce'] ) ) {
2650
				// otherwise, create a new record in Salesforce.
2651
				$result = $this->push_to_salesforce( 'user', $user_id );
2652
			}
2653
		} elseif ( isset( $post_data['salesforce_delete_mapped_user'] ) && true === filter_var( $post_data['salesforce_delete_mapped_user'], FILTER_VALIDATE_BOOLEAN ) ) {
2654
			// if a Salesforce ID was entered.
2655
			if ( isset( $post_data['mapping_id'] ) && ! empty( $post_data['mapping_id'] ) ) {
2656
				$delete = $this->mappings->delete_object_map( $post_data['mapping_id'] );
2657
			}
2658
		}
2659
	}
2660
2661
	/**
2662
	 * Render tabs for settings pages in admin
2663
	 *
2664
	 * @param array  $tabs is the tabs for the settings menu.
2665
	 * @param string $tab is a single tab.
2666
	 */
2667
	private function tabs( $tabs, $tab = '' ) {
2668
2669
		$get_data        = filter_input_array( INPUT_GET, FILTER_SANITIZE_SPECIAL_CHARS );
2670
		$consumer_key    = $this->login_credentials['consumer_key'];
2671
		$consumer_secret = $this->login_credentials['consumer_secret'];
2672
		$callback_url    = $this->login_credentials['callback_url'];
2673
2674
		$current_tab = $tab;
2675
		echo '<h2 class="nav-tab-wrapper">';
2676
		foreach ( $tabs as $tab_key => $tab_caption ) {
2677
			$active = $current_tab === $tab_key ? ' nav-tab-active' : '';
2678
2679
			if ( true === $this->salesforce['is_authorized'] ) {
2680
				echo sprintf(
2681
					'<a class="nav-tab%1$s" href="%2$s">%3$s</a>',
2682
					esc_attr( $active ),
0 ignored issues
show
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2682
					/** @scrutinizer ignore-call */ 
2683
     esc_attr( $active ),
Loading history...
2683
					esc_url( '?page=' . $this->admin_settings_url_param . '&tab=' . $tab_key ),
0 ignored issues
show
The function esc_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2683
					/** @scrutinizer ignore-call */ 
2684
     esc_url( '?page=' . $this->admin_settings_url_param . '&tab=' . $tab_key ),
Loading history...
2684
					esc_html( $tab_caption )
0 ignored issues
show
The function esc_html was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2684
					/** @scrutinizer ignore-call */ 
2685
     esc_html( $tab_caption )
Loading history...
2685
				);
2686
			} elseif ( 'settings' === $tab_key || ( 'authorize' === $tab_key && isset( $consumer_key ) && isset( $consumer_secret ) && ! empty( $consumer_key ) && ! empty( $consumer_secret ) ) ) {
2687
				echo sprintf(
2688
					'<a class="nav-tab%1$s" href="%2$s">%3$s</a>',
2689
					esc_attr( $active ),
2690
					esc_url( '?page=' . $this->admin_settings_url_param . '&tab=' . $tab_key ),
2691
					esc_html( $tab_caption )
2692
				);
2693
			}
2694
		}
2695
		echo '</h2>';
2696
2697
		if ( isset( $get_data['tab'] ) ) {
2698
			$tab = sanitize_key( $get_data['tab'] );
0 ignored issues
show
The function sanitize_key was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2698
			$tab = /** @scrutinizer ignore-call */ sanitize_key( $get_data['tab'] );
Loading history...
2699
		} else {
2700
			$tab = '';
2701
		}
2702
	}
2703
2704
	/**
2705
	 * Clear schedule
2706
	 * This clears the schedule if the user clicks the button
2707
	 *
2708
	 * @param string $schedule_name is the name of the schedule being cleared.
2709
	 */
2710
	private function clear_schedule( $schedule_name = '' ) {
2711
		if ( '' !== $schedule_name ) {
2712
			$this->queue->cancel( $schedule_name );
2713
			// translators: $schedule_name is the name of the current queue. Defaults: salesforce_pull, salesforce_push, salesforce.
2714
			echo sprintf( esc_html__( 'You have cleared the %s schedule.', 'object-sync-for-salesforce' ), esc_html( $schedule_name ) );
0 ignored issues
show
The function esc_html was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2714
			echo sprintf( esc_html__( 'You have cleared the %s schedule.', 'object-sync-for-salesforce' ), /** @scrutinizer ignore-call */ esc_html( $schedule_name ) );
Loading history...
The function esc_html__ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2714
			echo sprintf( /** @scrutinizer ignore-call */ esc_html__( 'You have cleared the %s schedule.', 'object-sync-for-salesforce' ), esc_html( $schedule_name ) );
Loading history...
2715
		} else {
2716
			echo esc_html__( 'You need to specify the name of the schedule you want to clear.', 'object-sync-for-salesforce' );
2717
		}
2718
	}
2719
2720
	/**
2721
	 * Get count of schedule items
2722
	 *
2723
	 * @param string $schedule_name is the name of the schedule.
2724
	 * @return int $count
2725
	 */
2726
	private function get_schedule_count( $schedule_name = '' ) {
2727
		if ( '' !== $schedule_name ) {
2728
			$count       = count(
2729
				$this->queue->search(
2730
					array(
2731
						'group'  => $schedule_name,
2732
						'status' => ActionScheduler_Store::STATUS_PENDING,
2733
					),
2734
					'ARRAY_A'
2735
				)
2736
			);
2737
			$group_count = count(
2738
				$this->queue->search(
2739
					array(
2740
						'group'  => $schedule_name . $this->action_group_suffix,
2741
						'status' => ActionScheduler_Store::STATUS_PENDING,
2742
					),
2743
					'ARRAY_A'
2744
				)
2745
			);
2746
			return $count + $group_count;
2747
		} else {
2748
			return 0;
2749
		}
2750
	}
2751
2752
	/**
2753
	 * Create an object map between a WordPress object and a Salesforce object
2754
	 *
2755
	 * @param int    $wordpress_id Unique identifier for the WordPress object.
2756
	 * @param string $wordpress_object What kind of object is it.
2757
	 * @param string $salesforce_id Unique identifier for the Salesforce object.
2758
	 * @param string $action Did we push or pull.
2759
	 * @return int   $wpdb->insert_id This is the database row for the map object
2760
	 */
2761
	private function create_object_map( $wordpress_id, $wordpress_object, $salesforce_id, $action = '' ) {
2762
		// Create object map and save it.
2763
		$mapping_object = $this->mappings->create_object_map(
2764
			array(
2765
				'wordpress_id'      => $wordpress_id, // wordpress unique id.
2766
				'salesforce_id'     => $salesforce_id, // salesforce unique id. we don't care what kind of object it is at this point.
2767
				'wordpress_object'  => $wordpress_object, // keep track of what kind of wp object this is.
2768
				'last_sync'         => current_time( 'mysql' ),
0 ignored issues
show
The function current_time was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2768
				'last_sync'         => /** @scrutinizer ignore-call */ current_time( 'mysql' ),
Loading history...
2769
				'last_sync_action'  => $action,
2770
				'last_sync_status'  => $this->mappings->status_success,
2771
				'last_sync_message' => __( 'Mapping object updated via function: ', 'object-sync-for-salesforce' ) . __FUNCTION__,
0 ignored issues
show
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2771
				'last_sync_message' => /** @scrutinizer ignore-call */ __( 'Mapping object updated via function: ', 'object-sync-for-salesforce' ) . __FUNCTION__,
Loading history...
2772
			)
2773
		);
2774
2775
		return $mapping_object;
2776
2777
	}
2778
2779
}
2780