Passed
Pull Request — master (#195)
by Jonathan
03:30
created

Object_Sync_Sf_Admin::status()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 54
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 32
c 0
b 0
f 0
dl 0
loc 54
rs 8.7857
cc 6
nc 32
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Class file for the Object_Sync_Sf_Admin class.
4
 *
5
 * @file
6
 */
7
8
if ( ! class_exists( 'Object_Sync_Salesforce' ) ) {
9
	die();
10
}
11
12
/**
13
 * Create default WordPress admin functionality to configure the plugin.
14
 */
15
class Object_Sync_Sf_Admin {
16
17
	protected $wpdb;
18
	protected $version;
19
	protected $login_credentials;
20
	protected $slug;
21
	protected $salesforce;
22
	protected $wordpress;
23
	protected $mappings;
24
	protected $push;
25
	protected $pull;
26
	protected $schedulable_classes;
27
	protected $queue;
28
29
	/**
30
	* @var string
31
	* Default path for the Salesforce authorize URL
32
	*/
33
	public $default_authorize_url_path;
34
35
	/**
36
	* @var string
37
	* Default path for the Salesforce token URL
38
	*/
39
	public $default_token_url_path;
40
41
	/**
42
	* @var string
43
	* What version of the Salesforce API should be the default on the settings screen.
44
	* Users can edit this, but they won't see a correct list of all their available versions until WordPress has
45
	* been authenticated with Salesforce.
46
	*/
47
	public $default_api_version;
48
49
	/**
50
	* @var bool
51
	* Default for whether to limit to triggerable items
52
	* Users can edit this
53
	*/
54
	public $default_triggerable;
55
56
	/**
57
	* @var bool
58
	* Default for whether to limit to updateable items
59
	* Users can edit this
60
	*/
61
	public $default_updateable;
62
63
	/**
64
	* @var int
65
	* Default pull throttle for how often Salesforce can pull
66
	* Users can edit this
67
	*/
68
	public $default_pull_throttle;
69
70
	/**
71
	* Constructor which sets up admin pages
72
	*
73
	* @param object $wpdb
74
	* @param string $version
75
	* @param array $login_credentials
76
	* @param string $slug
77
	* @param object $wordpress
78
	* @param object $salesforce
79
	* @param object $mappings
80
	* @param object $push
81
	* @param object $pull
82
	* @param object $logging
83
	* @param array $schedulable_classes
84
	* @param object $queue
85
	* @throws \Exception
86
	*/
87
	public function __construct( $wpdb, $version, $login_credentials, $slug, $wordpress, $salesforce, $mappings, $push, $pull, $logging, $schedulable_classes, $queue ) {
88
		$this->wpdb                = $wpdb;
89
		$this->version             = $version;
90
		$this->login_credentials   = $login_credentials;
91
		$this->slug                = $slug;
92
		$this->wordpress           = $wordpress;
93
		$this->salesforce          = $salesforce;
94
		$this->mappings            = $mappings;
95
		$this->push                = $push;
96
		$this->pull                = $pull;
97
		$this->logging             = $logging;
0 ignored issues
show
Bug Best Practice introduced by
The property logging does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
98
		$this->schedulable_classes = $schedulable_classes;
99
		$this->queue               = $queue;
100
101
		$this->sfwp_transients = $this->wordpress->sfwp_transients;
0 ignored issues
show
Bug Best Practice introduced by
The property sfwp_transients does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
102
103
		// default authorize url path
104
		$this->default_authorize_url_path = '/services/oauth2/authorize';
105
		// default token url path
106
		$this->default_token_url_path = '/services/oauth2/token';
107
		// what Salesforce API version to start the settings with. This is only used in the settings form
108
		$this->default_api_version = '43.0';
109
		// default pull throttle for avoiding going over api limits
110
		$this->default_pull_throttle = 5;
111
		// default setting for triggerable items
112
		$this->default_triggerable = true;
113
		// default setting for updateable items
114
		$this->default_updateable = true;
115
		// default option prefix
116
		$this->option_prefix = 'object_sync_for_salesforce_';
0 ignored issues
show
Bug Best Practice introduced by
The property option_prefix does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
117
118
		$this->add_actions();
119
120
	}
121
122
	/**
123
	* Create the action hooks to create the admin page(s)
124
	*
125
	*/
126
	public function add_actions() {
127
		add_action( 'admin_init', array( $this, 'salesforce_settings_forms' ) );
0 ignored issues
show
Bug introduced by
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

127
		/** @scrutinizer ignore-call */ 
128
  add_action( 'admin_init', array( $this, 'salesforce_settings_forms' ) );
Loading history...
128
		add_action( 'admin_init', array( $this, 'notices' ) );
129
		add_action( 'admin_post_post_fieldmap', array( $this, 'prepare_fieldmap_data' ) );
130
131
		add_action( 'admin_post_delete_fieldmap', array( $this, 'delete_fieldmap' ) );
132
		add_action( 'wp_ajax_get_salesforce_object_description', array( $this, 'get_salesforce_object_description' ) );
133
		add_action( 'wp_ajax_get_wordpress_object_description', array( $this, 'get_wordpress_object_fields' ) );
134
		add_action( 'wp_ajax_get_wp_sf_object_fields', array( $this, 'get_wp_sf_object_fields' ) );
135
		add_action( 'wp_ajax_push_to_salesforce', array( $this, 'push_to_salesforce' ) );
136
		add_action( 'wp_ajax_pull_from_salesforce', array( $this, 'pull_from_salesforce' ) );
137
		add_action( 'wp_ajax_refresh_mapped_data', array( $this, 'refresh_mapped_data' ) );
138
		add_action( 'wp_ajax_clear_sfwp_cache', array( $this, 'clear_sfwp_cache' ) );
139
140
		add_action( 'edit_user_profile', array( $this, 'show_salesforce_user_fields' ) );
141
		add_action( 'personal_options_update', array( $this, 'save_salesforce_user_fields' ) );
142
		add_action( 'edit_user_profile_update', array( $this, 'save_salesforce_user_fields' ) );
143
144
		// when either field for schedule settings changes
145
		foreach ( $this->schedulable_classes as $key => $value ) {
146
			add_filter( 'update_option_' . $this->option_prefix . $key . '_schedule_number', array( $this, 'change_action_schedule' ), 10, 3 );
0 ignored issues
show
Bug introduced by
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

146
			/** @scrutinizer ignore-call */ 
147
   add_filter( 'update_option_' . $this->option_prefix . $key . '_schedule_number', array( $this, 'change_action_schedule' ), 10, 3 );
Loading history...
147
			add_filter( 'update_option_' . $this->option_prefix . $key . '_schedule_unit', array( $this, 'change_action_schedule' ), 10, 3 );
148
		}
149
150
		add_action( 'admin_post_delete_object_map', array( $this, 'delete_object_map' ) );
151
		add_action( 'admin_post_post_object_map', array( $this, 'prepare_object_map_data' ) );
152
153
		// import and export plugin data
154
		add_action( 'admin_post_object_sync_for_salesforce_import', array( $this, 'import_json_file' ) );
155
		add_action( 'admin_post_object_sync_for_salesforce_export', array( $this, 'export_json_file' ) );
156
157
	}
158
159
	/**
160
	* Recurring task to check Salesforce for data
161
	*
162
	*/
163
	public function change_action_schedule( $old_schedule, $new_schedule, $option_name ) {
164
165
		// exit if nothing changed
166
		if ( $old_schedule === $new_schedule ) {
167
			return;
168
		}
169
170
		// get the current schedule name from the task, based on pattern in the foreach
171
		preg_match( '/' . $this->option_prefix . '(.*)_schedule/', $option_name, $matches );
172
		$schedule_name     = $matches[1];
173
		$action_group_name = $schedule_name . '_check_records';
174
175
		// exit if there is no initializer property on this schedule
176
		if ( ! isset( $this->schedulable_classes[ $schedule_name ]['initializer'] ) ) {
177
			return;
178
		}
179
180
		// cancel previous task
181
		$this->queue->cancel(
182
			$this->schedulable_classes[ $schedule_name ]['initializer'],
183
			array(),
184
			$action_group_name
185
		);
186
187
		// create new recurring task for action-scheduler to check for data to pull from salesforce
188
		$this->queue->schedule_recurring(
189
			current_time( 'timestamp', true ), // plugin seems to expect UTC
0 ignored issues
show
Bug introduced by
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

189
			/** @scrutinizer ignore-call */ 
190
   current_time( 'timestamp', true ), // plugin seems to expect UTC
Loading history...
190
			$this->queue->get_frequency( $schedule_name, 'seconds' ),
191
			$this->schedulable_classes[ $schedule_name ]['initializer'],
192
			array(),
193
			$action_group_name
194
		);
195
	}
196
197
	/**
198
	* Create WordPress admin options page
199
	*
200
	*/
201
	public function create_admin_menu() {
202
		$title = __( 'Salesforce', 'object-sync-for-salesforce' );
0 ignored issues
show
Bug introduced by
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

202
		$title = /** @scrutinizer ignore-call */ __( 'Salesforce', 'object-sync-for-salesforce' );
Loading history...
203
		add_options_page( $title, $title, 'configure_salesforce', 'object-sync-salesforce-admin', array( $this, 'show_admin_page' ) );
0 ignored issues
show
Bug introduced by
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

203
		/** @scrutinizer ignore-call */ 
204
  add_options_page( $title, $title, 'configure_salesforce', 'object-sync-salesforce-admin', array( $this, 'show_admin_page' ) );
Loading history...
204
	}
205
206
	/**
207
	* Render full admin pages in WordPress
208
	* This allows other plugins to add tabs to the Salesforce settings screen
209
	*
210
	* todo: better front end: html, organization of html into templates, css, js
211
	*
212
	*/
213
	public function show_admin_page() {
214
		$get_data = filter_input_array( INPUT_GET, FILTER_SANITIZE_STRING );
215
		echo '<div class="wrap">';
216
		echo '<h1>' . esc_html( get_admin_page_title() ) . '</h1>';
0 ignored issues
show
Bug introduced by
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

216
		echo '<h1>' . esc_html( /** @scrutinizer ignore-call */ get_admin_page_title() ) . '</h1>';
Loading history...
Bug introduced by
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

216
		echo '<h1>' . /** @scrutinizer ignore-call */ esc_html( get_admin_page_title() ) . '</h1>';
Loading history...
217
		$allowed = $this->check_wordpress_admin_permissions();
218
		if ( false === $allowed ) {
219
			return;
220
		}
221
		$tabs = array(
222
			'settings'      => __( 'Settings', 'object-sync-for-salesforce' ),
0 ignored issues
show
Bug introduced by
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

222
			'settings'      => /** @scrutinizer ignore-call */ __( 'Settings', 'object-sync-for-salesforce' ),
Loading history...
223
			'authorize'     => __( 'Authorize', 'object-sync-for-salesforce' ),
224
			'fieldmaps'     => __( 'Fieldmaps', 'object-sync-for-salesforce' ),
225
			'schedule'      => __( 'Scheduling', 'object-sync-for-salesforce' ),
226
			'import-export' => __( 'Import &amp; Export', 'object-sync-for-salesforce' ),
227
		); // this creates the tabs for the admin
228
229
		// optionally make tab(s) for logging and log settings
230
		$logging_enabled      = get_option( $this->option_prefix . 'enable_logging', false );
0 ignored issues
show
Bug introduced by
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

230
		$logging_enabled      = /** @scrutinizer ignore-call */ get_option( $this->option_prefix . 'enable_logging', false );
Loading history...
231
		$tabs['log_settings'] = __( 'Log Settings', 'object-sync-for-salesforce' );
232
233
		$mapping_errors = $this->mappings->get_failed_object_maps();
234
		if ( ! empty( $mapping_errors ) ) {
235
			$tabs['mapping_errors'] = __( 'Mapping Errors', 'object-sync-for-salesforce' );
236
		}
237
238
		// filter for extending the tabs available on the page
239
		// currently it will go into the default switch case for $tab
240
		$tabs = apply_filters( 'object_sync_for_salesforce_settings_tabs', $tabs );
0 ignored issues
show
Bug introduced by
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

240
		$tabs = /** @scrutinizer ignore-call */ apply_filters( 'object_sync_for_salesforce_settings_tabs', $tabs );
Loading history...
241
242
		$tab = isset( $get_data['tab'] ) ? sanitize_key( $get_data['tab'] ) : 'settings';
0 ignored issues
show
Bug introduced by
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

242
		$tab = isset( $get_data['tab'] ) ? /** @scrutinizer ignore-call */ sanitize_key( $get_data['tab'] ) : 'settings';
Loading history...
243
		$this->tabs( $tabs, $tab );
244
245
		$consumer_key    = $this->login_credentials['consumer_key'];
246
		$consumer_secret = $this->login_credentials['consumer_secret'];
247
		$callback_url    = $this->login_credentials['callback_url'];
248
249
		if ( true !== $this->salesforce['is_authorized'] ) {
250
			$url     = esc_url( $callback_url );
0 ignored issues
show
Bug introduced by
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

250
			$url     = /** @scrutinizer ignore-call */ esc_url( $callback_url );
Loading history...
251
			$anchor  = esc_html__( 'Authorize tab', 'object-sync-for-salesforce' );
0 ignored issues
show
Bug introduced by
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

251
			$anchor  = /** @scrutinizer ignore-call */ esc_html__( 'Authorize tab', 'object-sync-for-salesforce' );
Loading history...
252
			$message = sprintf( 'Salesforce needs to be authorized to connect to this website. Use the <a href="%s">%s</a> to connect.', $url, $anchor );
253
			require( plugin_dir_path( __FILE__ ) . '/../templates/admin/error.php' );
0 ignored issues
show
Bug introduced by
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

253
			require( /** @scrutinizer ignore-call */ plugin_dir_path( __FILE__ ) . '/../templates/admin/error.php' );
Loading history...
254
		}
255
256
		if ( 0 === count( $this->mappings->get_fieldmaps() ) ) {
257
			$url     = esc_url( get_admin_url( null, 'options-general.php?page=object-sync-salesforce-admin&tab=fieldmaps' ) );
0 ignored issues
show
Bug introduced by
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

257
			$url     = esc_url( /** @scrutinizer ignore-call */ get_admin_url( null, 'options-general.php?page=object-sync-salesforce-admin&tab=fieldmaps' ) );
Loading history...
258
			$anchor  = esc_html__( 'Fieldmaps tab', 'object-sync-for-salesforce' );
259
			$message = sprintf( 'No fieldmaps exist yet. Use the <a href="%s">%s</a> to map WordPress and Salesforce objects to each other.', $url, $anchor );
260
			require( plugin_dir_path( __FILE__ ) . '/../templates/admin/error.php' );
261
		}
262
263
		$push_schedule_number = get_option( $this->option_prefix . 'salesforce_push_schedule_number', '' );
264
		$push_schedule_unit   = get_option( $this->option_prefix . 'salesforce_push_schedule_unit', '' );
265
		$pull_schedule_number = get_option( $this->option_prefix . 'salesforce_pull_schedule_number', '' );
266
		$pull_schedule_unit   = get_option( $this->option_prefix . 'salesforce_pull_schedule_unit', '' );
267
268
		if ( '' === $push_schedule_number && '' === $push_schedule_unit && '' === $pull_schedule_number && '' === $pull_schedule_unit ) {
269
			$url     = esc_url( get_admin_url( null, 'options-general.php?page=object-sync-salesforce-admin&tab=schedule' ) );
270
			$anchor  = esc_html__( 'Scheduling tab', 'object-sync-for-salesforce' );
271
			$message = sprintf( 'Because the plugin schedule has not been saved, the plugin cannot run automatic operations. Use the <a href="%s">%s</a> to create schedules to run.', $url, $anchor );
272
			require( plugin_dir_path( __FILE__ ) . '/../templates/admin/error.php' );
273
		}
274
275
		try {
276
			switch ( $tab ) {
277
				case 'authorize':
278
					if ( isset( $get_data['code'] ) ) {
279
						// this string is an oauth token
280
						$data          = esc_html( wp_unslash( $get_data['code'] ) );
0 ignored issues
show
Bug introduced by
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

280
						$data          = esc_html( /** @scrutinizer ignore-call */ wp_unslash( $get_data['code'] ) );
Loading history...
281
						$is_authorized = $this->salesforce['sfapi']->request_token( $data );
0 ignored issues
show
Unused Code introduced by
The assignment to $is_authorized is dead and can be removed.
Loading history...
282
						?>
283
						<script>window.location = '<?php echo esc_url_raw( $callback_url ); ?>'</script>
0 ignored issues
show
Bug introduced by
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

283
						<script>window.location = '<?php echo /** @scrutinizer ignore-call */ esc_url_raw( $callback_url ); ?>'</script>
Loading history...
284
						<?php
285
					} elseif ( true === $this->salesforce['is_authorized'] ) {
286
							require_once( plugin_dir_path( __FILE__ ) . '/../templates/admin/authorized.php' );
287
							$this->status( $this->salesforce['sfapi'] );
288
					} elseif ( true === is_object( $this->salesforce['sfapi'] ) && isset( $consumer_key ) && isset( $consumer_secret ) ) {
289
						?>
290
						<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>
291
						<?php
292
					} else {
293
						$url    = esc_url( get_admin_url( null, 'options-general.php?page=object-sync-salesforce-admin&tab=settings' ) );
294
						$anchor = esc_html__( 'Settings', 'object-sync-for-salesforce' );
295
						// translators: placeholders are for the settings tab link: 1) the url, and 2) the anchor text
296
						$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-salesforce' ), $url, $anchor );
0 ignored issues
show
Unused Code introduced by
The assignment to $message is dead and can be removed.
Loading history...
297
						require_once( plugin_dir_path( __FILE__ ) . '/../templates/admin/error.php' );
298
					}
299
					break;
300
				case 'fieldmaps':
301
					if ( isset( $get_data['method'] ) ) {
302
303
						$method      = sanitize_key( $get_data['method'] );
304
						$error_url   = get_admin_url( null, 'options-general.php?page=object-sync-salesforce-admin&tab=fieldmaps&method=' . $method );
0 ignored issues
show
Unused Code introduced by
The assignment to $error_url is dead and can be removed.
Loading history...
305
						$success_url = get_admin_url( null, 'options-general.php?page=object-sync-salesforce-admin&tab=fieldmaps' );
0 ignored issues
show
Unused Code introduced by
The assignment to $success_url is dead and can be removed.
Loading history...
306
307
						if ( isset( $get_data['transient'] ) ) {
308
							$transient = sanitize_key( $get_data['transient'] );
309
							$posted    = $this->sfwp_transients->get( $transient );
310
						}
311
312
						if ( isset( $posted ) && is_array( $posted ) ) {
313
							$map = $posted;
314
						} elseif ( 'edit' === $method || 'clone' === $method || 'delete' === $method ) {
315
							$map = $this->mappings->get_fieldmaps( isset( $get_data['id'] ) ? sanitize_key( $get_data['id'] ) : '' );
316
						}
317
318
						if ( isset( $map ) && is_array( $map ) ) {
319
							$label                           = $map['label'];
0 ignored issues
show
Unused Code introduced by
The assignment to $label is dead and can be removed.
Loading history...
320
							$salesforce_object               = $map['salesforce_object'];
0 ignored issues
show
Unused Code introduced by
The assignment to $salesforce_object is dead and can be removed.
Loading history...
321
							$salesforce_record_types_allowed = maybe_unserialize( $map['salesforce_record_types_allowed'] );
0 ignored issues
show
Unused Code introduced by
The assignment to $salesforce_record_types_allowed is dead and can be removed.
Loading history...
Bug introduced by
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

321
							$salesforce_record_types_allowed = /** @scrutinizer ignore-call */ maybe_unserialize( $map['salesforce_record_types_allowed'] );
Loading history...
322
							$salesforce_record_type_default  = $map['salesforce_record_type_default'];
0 ignored issues
show
Unused Code introduced by
The assignment to $salesforce_record_type_default is dead and can be removed.
Loading history...
323
							$wordpress_object                = $map['wordpress_object'];
0 ignored issues
show
Unused Code introduced by
The assignment to $wordpress_object is dead and can be removed.
Loading history...
324
							$pull_trigger_field              = $map['pull_trigger_field'];
0 ignored issues
show
Unused Code introduced by
The assignment to $pull_trigger_field is dead and can be removed.
Loading history...
325
							$fieldmap_fields                 = $map['fields'];
0 ignored issues
show
Unused Code introduced by
The assignment to $fieldmap_fields is dead and can be removed.
Loading history...
326
							$sync_triggers                   = $map['sync_triggers'];
0 ignored issues
show
Unused Code introduced by
The assignment to $sync_triggers is dead and can be removed.
Loading history...
327
							$push_async                      = $map['push_async'];
0 ignored issues
show
Unused Code introduced by
The assignment to $push_async is dead and can be removed.
Loading history...
328
							$push_drafts                     = $map['push_drafts'];
0 ignored issues
show
Unused Code introduced by
The assignment to $push_drafts is dead and can be removed.
Loading history...
329
							$weight                          = $map['weight'];
0 ignored issues
show
Unused Code introduced by
The assignment to $weight is dead and can be removed.
Loading history...
330
						}
331
332
						if ( 'add' === $method || 'edit' === $method || 'clone' === $method ) {
333
							require_once( plugin_dir_path( __FILE__ ) . '/../templates/admin/fieldmaps-add-edit-clone.php' );
334
						} elseif ( 'delete' === $method ) {
335
							require_once( plugin_dir_path( __FILE__ ) . '/../templates/admin/fieldmaps-delete.php' );
336
						}
337
					} else {
338
						$fieldmaps = $this->mappings->get_fieldmaps();
0 ignored issues
show
Unused Code introduced by
The assignment to $fieldmaps is dead and can be removed.
Loading history...
339
						require_once( plugin_dir_path( __FILE__ ) . '/../templates/admin/fieldmaps-list.php' );
340
					} // End if().
341
					break;
342
				case 'logout':
343
					$this->logout();
344
					break;
345
				case 'clear_cache':
346
					$this->clear_cache();
347
					break;
348
				case 'clear_schedule':
349
					if ( isset( $get_data['schedule_name'] ) ) {
350
						$schedule_name = sanitize_key( $get_data['schedule_name'] );
351
					}
352
					$this->clear_schedule( $schedule_name );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $schedule_name does not seem to be defined for all execution paths leading up to this point.
Loading history...
353
					break;
354
				case 'settings':
355
					require_once( plugin_dir_path( __FILE__ ) . '/../templates/admin/settings.php' );
356
					break;
357
				case 'mapping_errors':
358
					if ( isset( $get_data['method'] ) ) {
359
360
						$method      = sanitize_key( $get_data['method'] );
361
						$error_url   = get_admin_url( null, 'options-general.php?page=object-sync-salesforce-admin&tab=mapping_errors&method=' . $method );
362
						$success_url = get_admin_url( null, 'options-general.php?page=object-sync-salesforce-admin&tab=mapping_errors' );
363
364
						if ( isset( $get_data['map_transient'] ) ) {
365
							$transient = sanitize_key( $get_data['map_transient'] );
366
							$posted    = $this->sfwp_transients->get( $transient );
367
						}
368
369
						if ( isset( $posted ) && is_array( $posted ) ) {
370
							$map_row = $posted;
371
						} elseif ( 'edit' === $method || 'delete' === $method ) {
372
							$map_row = $this->mappings->get_failed_object_map( isset( $get_data['id'] ) ? sanitize_key( $get_data['id'] ) : '' );
373
						}
374
375
						if ( isset( $map_row ) && is_array( $map_row ) ) {
376
							$salesforce_id = $map_row['salesforce_id'];
0 ignored issues
show
Unused Code introduced by
The assignment to $salesforce_id is dead and can be removed.
Loading history...
377
							$wordpress_id  = $map_row['wordpress_id'];
0 ignored issues
show
Unused Code introduced by
The assignment to $wordpress_id is dead and can be removed.
Loading history...
378
						}
379
380
						if ( 'edit' === $method ) {
381
							require_once( plugin_dir_path( __FILE__ ) . '/../templates/admin/mapping-errors-edit.php' );
382
						} elseif ( 'delete' === $method ) {
383
							require_once( plugin_dir_path( __FILE__ ) . '/../templates/admin/mapping-errors-delete.php' );
384
						}
385
					} else {
386
						require_once( plugin_dir_path( __FILE__ ) . '/../templates/admin/mapping-errors.php' );
387
					}
388
					break;
389
				case 'import-export':
390
					require_once( plugin_dir_path( __FILE__ ) . '/../templates/admin/import-export.php' );
391
					break;
392
				default:
393
					$include_settings = apply_filters( 'object_sync_for_salesforce_settings_tab_include_settings', true, $tab );
394
					$content_before   = apply_filters( 'object_sync_for_salesforce_settings_tab_content_before', null, $tab );
395
					$content_after    = apply_filters( 'object_sync_for_salesforce_settings_tab_content_after', null, $tab );
396
					if ( null !== $content_before ) {
397
						echo esc_html( $content_before );
398
					}
399
					if ( true === $include_settings ) {
400
						require_once( plugin_dir_path( __FILE__ ) . '/../templates/admin/settings.php' );
401
					}
402
					if ( null !== $content_after ) {
403
						echo esc_html( $content_after );
404
					}
405
					break;
406
			} // End switch().
407
		} catch ( SalesforceApiException $ex ) {
0 ignored issues
show
Bug introduced by
The type SalesforceApiException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
408
			echo sprintf( '<p>Error <strong>%1$s</strong>: %2$s</p>',
409
				absint( $ex->getCode() ),
0 ignored issues
show
Bug introduced by
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

409
				/** @scrutinizer ignore-call */ 
410
    absint( $ex->getCode() ),
Loading history...
410
				esc_html( $ex->getMessage() )
411
			);
412
		} catch ( Exception $ex ) {
413
			echo sprintf( '<p>Error <strong>%1$s</strong>: %2$s</p>',
414
				absint( $ex->getCode() ),
415
				esc_html( $ex->getMessage() )
416
			);
417
		} // End try().
418
		echo '</div>';
419
	}
420
421
	/**
422
	* Create default WordPress admin settings form for salesforce
423
	* This is for the Settings page/tab
424
	*
425
	*/
426
	public function salesforce_settings_forms() {
427
		$get_data = filter_input_array( INPUT_GET, FILTER_SANITIZE_STRING );
428
		$page     = isset( $get_data['tab'] ) ? sanitize_key( $get_data['tab'] ) : 'settings';
0 ignored issues
show
Bug introduced by
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

428
		$page     = isset( $get_data['tab'] ) ? /** @scrutinizer ignore-call */ sanitize_key( $get_data['tab'] ) : 'settings';
Loading history...
Unused Code introduced by
The assignment to $page is dead and can be removed.
Loading history...
429
		$section  = isset( $get_data['tab'] ) ? sanitize_key( $get_data['tab'] ) : 'settings';
0 ignored issues
show
Unused Code introduced by
The assignment to $section is dead and can be removed.
Loading history...
430
431
		$input_callback_default   = array( $this, 'display_input_field' );
432
		$input_checkboxes_default = array( $this, 'display_checkboxes' );
433
		$input_select_default     = array( $this, 'display_select' );
434
		$link_default             = array( $this, 'display_link' );
435
436
		$all_field_callbacks = array(
437
			'text'       => $input_callback_default,
438
			'checkboxes' => $input_checkboxes_default,
439
			'select'     => $input_select_default,
440
			'link'       => $link_default,
441
		);
442
443
		$this->fields_settings( 'settings', 'settings', $all_field_callbacks );
444
		$this->fields_fieldmaps( 'fieldmaps', 'objects' );
445
		$this->fields_scheduling( 'schedule', 'schedule', $all_field_callbacks );
446
		$this->fields_log_settings( 'log_settings', 'log_settings', $all_field_callbacks );
447
	}
448
449
	/**
450
	* Fields for the Settings tab
451
	* This runs add_settings_section once, as well as add_settings_field and register_setting methods for each option
452
	*
453
	* @param string $page
454
	* @param string $section
455
	* @param string $input_callback
456
	*/
457
	private function fields_settings( $page, $section, $callbacks ) {
458
		add_settings_section( $page, ucwords( $page ), null, $page );
0 ignored issues
show
Bug introduced by
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

458
		/** @scrutinizer ignore-call */ 
459
  add_settings_section( $page, ucwords( $page ), null, $page );
Loading history...
459
		$salesforce_settings = array(
460
			'consumer_key'                   => array(
461
				'title'    => __( 'Consumer Key', 'object-sync-for-salesforce' ),
0 ignored issues
show
Bug introduced by
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

461
				'title'    => /** @scrutinizer ignore-call */ __( 'Consumer Key', 'object-sync-for-salesforce' ),
Loading history...
462
				'callback' => $callbacks['text'],
463
				'page'     => $page,
464
				'section'  => $section,
465
				'args'     => array(
466
					'type'     => 'text',
467
					'validate' => 'sanitize_validate_text',
468
					'desc'     => '',
469
					'constant' => 'OBJECT_SYNC_SF_SALESFORCE_CONSUMER_KEY',
470
				),
471
472
			),
473
			'consumer_secret'                => array(
474
				'title'    => __( 'Consumer Secret', 'object-sync-for-salesforce' ),
475
				'callback' => $callbacks['text'],
476
				'page'     => $page,
477
				'section'  => $section,
478
				'args'     => array(
479
					'type'     => 'text',
480
					'validate' => 'sanitize_validate_text',
481
					'desc'     => '',
482
					'constant' => 'OBJECT_SYNC_SF_SALESFORCE_CONSUMER_SECRET',
483
				),
484
			),
485
			'callback_url'                   => array(
486
				'title'    => __( 'Callback URL', 'object-sync-for-salesforce' ),
487
				'callback' => $callbacks['text'],
488
				'page'     => $page,
489
				'section'  => $section,
490
				'args'     => array(
491
					'type'     => 'url',
492
					'validate' => 'sanitize_validate_text',
493
					// translators: %1$s is the admin URL for the Authorize tab
494
					'desc'     => sprintf( __( 'In most cases, you will want to use %1$s for this value.', 'object-sync-for-salesforce' ),
495
						get_admin_url( null, 'options-general.php?page=object-sync-salesforce-admin&tab=authorize' )
0 ignored issues
show
Bug introduced by
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

495
						/** @scrutinizer ignore-call */ 
496
      get_admin_url( null, 'options-general.php?page=object-sync-salesforce-admin&tab=authorize' )
Loading history...
496
					),
497
					'constant' => 'OBJECT_SYNC_SF_SALESFORCE_CALLBACK_URL',
498
				),
499
			),
500
			'login_base_url'                 => array(
501
				'title'    => __( 'Login Base URL', 'object-sync-for-salesforce' ),
502
				'callback' => $callbacks['text'],
503
				'page'     => $page,
504
				'section'  => $section,
505
				'args'     => array(
506
					'type'     => 'url',
507
					'validate' => 'sanitize_validate_text',
508
					// translators: 1) production salesforce login, 2) sandbox salesforce login
509
					'desc'     => sprintf( __( 'For most Salesforce setups, you should use %1$s for production and %2$s for sandbox.', 'object-sync-for-salesforce' ),
510
						esc_url( 'https://login.salesforce.com' ),
0 ignored issues
show
Bug introduced by
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

510
						/** @scrutinizer ignore-call */ 
511
      esc_url( 'https://login.salesforce.com' ),
Loading history...
511
						esc_url( 'https://test.salesforce.com' )
512
					),
513
					'constant' => 'OBJECT_SYNC_SF_SALESFORCE_LOGIN_BASE_URL',
514
				),
515
			),
516
			'authorize_url_path'             => array(
517
				'title'    => __( 'Authorize URL Path', 'object-sync-for-salesforce' ),
518
				'callback' => $callbacks['text'],
519
				'page'     => $page,
520
				'section'  => $section,
521
				'args'     => array(
522
					'type'     => 'text',
523
					'validate' => 'sanitize_validate_text',
524
					'desc'     => __( 'For most Salesforce installs, this should not be changed.', 'object-sync-for-salesforce' ),
525
					'constant' => 'OBJECT_SYNC_SF_SALESFORCE_AUTHORIZE_URL_PATH',
526
					'default'  => $this->default_authorize_url_path,
527
				),
528
			),
529
			'token_url_path'                 => array(
530
				'title'    => 'Token URL Path',
531
				'callback' => $callbacks['text'],
532
				'page'     => $page,
533
				'section'  => $section,
534
				'args'     => array(
535
					'type'     => 'text',
536
					'validate' => 'sanitize_validate_text',
537
					'desc'     => __( 'For most Salesforce installs, this should not be changed.', 'object-sync-for-salesforce' ),
538
					'constant' => 'OBJECT_SYNC_SF_SALESFORCE_TOKEN_URL_PATH',
539
					'default'  => $this->default_token_url_path,
540
				),
541
			),
542
			'api_version'                    => array(
543
				'title'    => 'Salesforce API Version',
544
				'callback' => $callbacks['text'],
545
				'page'     => $page,
546
				'section'  => $section,
547
				'args'     => array(
548
					'type'     => 'text',
549
					'validate' => 'sanitize_validate_text',
550
					'desc'     => '',
551
					'constant' => 'OBJECT_SYNC_SF_SALESFORCE_API_VERSION',
552
					'default'  => $this->default_api_version,
553
				),
554
			),
555
			'object_filters'                 => array(
556
				'title'    => 'Limit Salesforce Objects',
557
				'callback' => $callbacks['checkboxes'],
558
				'page'     => $page,
559
				'section'  => $section,
560
				'args'     => array(
561
					'type'     => 'checkboxes',
562
					'validate' => 'sanitize_validate_text',
563
					'desc'     => __( 'Allows you to limit which Salesforce objects can be mapped', 'object-sync-for-salesforce' ),
564
					'items'    => array(
565
						'triggerable' => array(
566
							'text'    => 'Only Triggerable objects',
567
							'id'      => 'triggerable',
568
							'desc'    => '',
569
							'default' => $this->default_triggerable,
570
						),
571
						'updateable'  => array(
572
							'text'    => 'Only Updateable objects',
573
							'id'      => 'updateable',
574
							'desc'    => '',
575
							'default' => $this->default_updateable,
576
						),
577
					),
578
				),
579
			),
580
			'salesforce_field_display_value' => array(
581
				'title'    => 'Salesforce Field Display Value',
582
				'callback' => $callbacks['select'],
583
				'page'     => $page,
584
				'section'  => $section,
585
				'args'     => array(
586
					'type'     => 'select',
587
					'validate' => 'sanitize_validate_text',
588
					'desc'     => 'When choosing Salesforce fields to map, this value determines how the dropdown will identify Salesforce fields.',
589
					'constant' => '',
590
					'items'    => array(
591
						'field_label' => array(
592
							'text'  => __( 'Field Label', 'object-sync-for-salesforce' ),
593
							'value' => 'field_label',
594
						),
595
						/*'field_name'  => array(
596
							'text'  => __( 'Field Name', 'object-sync-for-salesforce' ),
597
							'value' => 'field_name',
598
						),*/
599
						'api_name'    => array(
600
							'text'  => __( 'API Name', 'object-sync-for-salesforce' ),
601
							'value' => 'api_name',
602
						),
603
					),
604
				),
605
			),
606
			'pull_throttle'                  => array(
607
				'title'    => 'Pull throttle (seconds)',
608
				'callback' => $callbacks['text'],
609
				'page'     => $page,
610
				'section'  => $section,
611
				'args'     => array(
612
					'type'     => 'number',
613
					'validate' => 'sanitize_validate_text',
614
					'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' ),
615
					'constant' => '',
616
					'default'  => $this->default_pull_throttle,
617
				),
618
			),
619
			'debug_mode'                     => array(
620
				'title'    => 'Debug mode?',
621
				'callback' => $callbacks['text'],
622
				'page'     => $page,
623
				'section'  => $section,
624
				'args'     => array(
625
					'type'     => 'checkbox',
626
					'validate' => 'sanitize_validate_text',
627
					'desc'     => __( 'Debug mode can, combined with the Log Settings, log things like Salesforce API requests. It can create a lot of entries if enabled; it is not recommended to use it in a production environment.', 'object-sync-for-salesforce' ),
628
					'constant' => '',
629
				),
630
			),
631
			'delete_data_on_uninstall'       => array(
632
				'title'    => 'Delete plugin data on uninstall?',
633
				'callback' => $callbacks['text'],
634
				'page'     => $page,
635
				'section'  => $section,
636
				'args'     => array(
637
					'type'     => 'checkbox',
638
					'validate' => 'sanitize_validate_text',
639
					'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' ),
640
					'constant' => '',
641
				),
642
			),
643
		);
644
645
		if ( true === is_object( $this->salesforce['sfapi'] ) && true === $this->salesforce['sfapi']->is_authorized() ) {
646
			$salesforce_settings['api_version'] = array(
647
				'title'    => 'Salesforce API Version',
648
				'callback' => $callbacks['select'],
649
				'page'     => $page,
650
				'section'  => $section,
651
				'args'     => array(
652
					'type'     => 'select',
653
					'validate' => 'sanitize_validate_text',
654
					'desc'     => '',
655
					'constant' => 'OBJECT_SYNC_SF_SALESFORCE_API_VERSION',
656
					'items'    => $this->version_options(),
657
				),
658
			);
659
		}
660
661
		foreach ( $salesforce_settings as $key => $attributes ) {
662
			$id       = $this->option_prefix . $key;
663
			$name     = $this->option_prefix . $key;
664
			$title    = $attributes['title'];
665
			$callback = $attributes['callback'];
666
			$validate = $attributes['args']['validate'];
667
			$page     = $attributes['page'];
668
			$section  = $attributes['section'];
669
			$args     = array_merge(
670
				$attributes['args'],
671
				array(
672
					'title'     => $title,
673
					'id'        => $id,
674
					'label_for' => $id,
675
					'name'      => $name,
676
				)
677
			);
678
679
			// if there is a constant and it is defined, don't run a validate function
680
			if ( isset( $attributes['args']['constant'] ) && defined( $attributes['args']['constant'] ) ) {
681
				$validate = '';
682
			}
683
684
			add_settings_field( $id, $title, $callback, $page, $section, $args );
0 ignored issues
show
Bug introduced by
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

684
			/** @scrutinizer ignore-call */ 
685
   add_settings_field( $id, $title, $callback, $page, $section, $args );
Loading history...
685
			register_setting( $page, $id, array( $this, $validate ) );
0 ignored issues
show
Bug introduced by
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

685
			/** @scrutinizer ignore-call */ 
686
   register_setting( $page, $id, array( $this, $validate ) );
Loading history...
686
		}
687
	}
688
689
	/**
690
	* Fields for the Fieldmaps tab
691
	* This runs add_settings_section once, as well as add_settings_field and register_setting methods for each option
692
	*
693
	* @param string $page
694
	* @param string $section
695
	* @param string $input_callback
696
	*/
697
	private function fields_fieldmaps( $page, $section, $input_callback = '' ) {
698
		add_settings_section( $page, ucwords( $page ), null, $page );
0 ignored issues
show
Bug introduced by
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

698
		/** @scrutinizer ignore-call */ 
699
  add_settings_section( $page, ucwords( $page ), null, $page );
Loading history...
699
	}
700
701
	/**
702
	* Fields for the Scheduling tab
703
	* This runs add_settings_section once, as well as add_settings_field and register_setting methods for each option
704
	*
705
	* @param string $page
706
	* @param string $section
707
	* @param string $input_callback
708
	*/
709
	private function fields_scheduling( $page, $section, $callbacks ) {
710
		foreach ( $this->schedulable_classes as $key => $value ) {
711
			add_settings_section( $key, $value['label'], null, $page );
0 ignored issues
show
Bug introduced by
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

711
			/** @scrutinizer ignore-call */ 
712
   add_settings_section( $key, $value['label'], null, $page );
Loading history...
712
			$schedule_settings = array();
713
			if ( isset( $value['initializer'] ) ) {
714
				$schedule_settings[ $key . '_schedule_number' ] = array(
715
					'title'    => __( 'Run schedule every', 'object-sync-for-salesforce' ),
0 ignored issues
show
Bug introduced by
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

715
					'title'    => /** @scrutinizer ignore-call */ __( 'Run schedule every', 'object-sync-for-salesforce' ),
Loading history...
716
					'callback' => $callbacks['text'],
717
					'page'     => $page,
718
					'section'  => $key,
719
					'args'     => array(
720
						'type'     => 'number',
721
						'validate' => 'absint',
722
						'desc'     => '',
723
						'constant' => '',
724
					),
725
				);
726
				$schedule_settings[ $key . '_schedule_unit' ]   = array(
727
					'title'    => __( 'Time unit', 'object-sync-for-salesforce' ),
728
					'callback' => $callbacks['select'],
729
					'page'     => $page,
730
					'section'  => $key,
731
					'args'     => array(
732
						'type'     => 'select',
733
						'validate' => 'sanitize_validate_text',
734
						'desc'     => '',
735
						'items'    => array(
736
							'minutes' => array(
737
								'text'  => __( 'Minutes', 'object-sync-for-salesforce' ),
738
								'value' => 'minutes',
739
							),
740
							'hours'   => array(
741
								'text'  => __( 'Hours', 'object-sync-for-salesforce' ),
742
								'value' => 'hours',
743
							),
744
							'days'    => array(
745
								'text'  => __( 'Days', 'object-sync-for-salesforce' ),
746
								'value' => 'days',
747
							),
748
						),
749
					),
750
				);
751
				$schedule_settings[ $key . '_clear_button' ]    = array(
752
					// translators: $this->get_schedule_count is an integer showing how many items are in the current queue
753
					'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
Bug introduced by
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

753
					'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...
754
					'callback' => $callbacks['link'],
755
					'page'     => $page,
756
					'section'  => $key,
757
					'args'     => array(
758
						'label'      => __( 'Clear this queue', 'object-sync-for-salesforce' ),
759
						'desc'       => '',
760
						'url'        => esc_url( '?page=object-sync-salesforce-admin&amp;tab=clear_schedule&amp;schedule_name=' . $key ),
0 ignored issues
show
Bug introduced by
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

760
						'url'        => /** @scrutinizer ignore-call */ esc_url( '?page=object-sync-salesforce-admin&amp;tab=clear_schedule&amp;schedule_name=' . $key ),
Loading history...
761
						'link_class' => 'button button-secondary',
762
					),
763
				);
764
			}
765
			$schedule_settings[ $key . '_clear_button' ] = array(
766
				// translators: $this->get_schedule_count is an integer showing how many items are in the current queue
767
				'title'    => sprintf( 'This queue has ' . _n( '%s item', '%s items', $this->get_schedule_count( $key ), 'object-sync-for-salesforce' ), $this->get_schedule_count( $key ) ),
768
				'callback' => $callbacks['link'],
769
				'page'     => $page,
770
				'section'  => $key,
771
				'args'     => array(
772
					'label'      => __( 'Clear this queue', 'object-sync-for-salesforce' ),
773
					'desc'       => '',
774
					'url'        => esc_url( '?page=object-sync-salesforce-admin&amp;tab=clear_schedule&amp;schedule_name=' . $key ),
775
					'link_class' => 'button button-secondary',
776
				),
777
			);
778
			foreach ( $schedule_settings as $key => $attributes ) {
0 ignored issues
show
Comprehensibility Bug introduced by
$key is overwriting a variable from outer foreach loop.
Loading history...
779
				$id       = $this->option_prefix . $key;
780
				$name     = $this->option_prefix . $key;
781
				$title    = $attributes['title'];
782
				$callback = $attributes['callback'];
783
				$page     = $attributes['page'];
784
				$section  = $attributes['section'];
785
				$args     = array_merge(
786
					$attributes['args'],
787
					array(
788
						'title'     => $title,
789
						'id'        => $id,
790
						'label_for' => $id,
791
						'name'      => $name,
792
					)
793
				);
794
				add_settings_field( $id, $title, $callback, $page, $section, $args );
0 ignored issues
show
Bug introduced by
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

794
				/** @scrutinizer ignore-call */ 
795
    add_settings_field( $id, $title, $callback, $page, $section, $args );
Loading history...
795
				register_setting( $page, $id );
0 ignored issues
show
Bug introduced by
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

795
				/** @scrutinizer ignore-call */ 
796
    register_setting( $page, $id );
Loading history...
796
			}
797
		} // End foreach().
798
	}
799
800
	/**
801
	* Fields for the Log Settings tab
802
	* This runs add_settings_section once, as well as add_settings_field and register_setting methods for each option
803
	*
804
	* @param string $page
805
	* @param string $section
806
	* @param array $callbacks
807
	*/
808
	private function fields_log_settings( $page, $section, $callbacks ) {
809
		add_settings_section( $page, ucwords( str_replace( '_', ' ', $page ) ), null, $page );
0 ignored issues
show
Bug introduced by
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

809
		/** @scrutinizer ignore-call */ 
810
  add_settings_section( $page, ucwords( str_replace( '_', ' ', $page ) ), null, $page );
Loading history...
810
		$log_settings = array(
811
			'enable_logging'        => array(
812
				'title'    => __( 'Enable Logging?', 'object-sync-for-salesforce' ),
0 ignored issues
show
Bug introduced by
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

812
				'title'    => /** @scrutinizer ignore-call */ __( 'Enable Logging?', 'object-sync-for-salesforce' ),
Loading history...
813
				'callback' => $callbacks['text'],
814
				'page'     => $page,
815
				'section'  => $section,
816
				'args'     => array(
817
					'type'     => 'checkbox',
818
					'validate' => 'absint',
819
					'desc'     => '',
820
					'constant' => '',
821
				),
822
			),
823
			'statuses_to_log'       => array(
824
				'title'    => __( 'Statuses to log', 'object-sync-for-salesforce' ),
825
				'callback' => $callbacks['checkboxes'],
826
				'page'     => $page,
827
				'section'  => $section,
828
				'args'     => array(
829
					'type'     => 'checkboxes',
830
					'validate' => 'sanitize_validate_text',
831
					'desc'     => __( 'these are the statuses to log', 'object-sync-for-salesforce' ),
832
					'items'    => array(
833
						'error'   => array(
834
							'text' => __( 'Error', 'object-sync-for-salesforce' ),
835
							'id'   => 'error',
836
							'desc' => '',
837
						),
838
						'success' => array(
839
							'text' => __( 'Success', 'object-sync-for-salesforce' ),
840
							'id'   => 'success',
841
							'desc' => '',
842
						),
843
						'notice'  => array(
844
							'text' => __( 'Notice', 'object-sync-for-salesforce' ),
845
							'id'   => 'notice',
846
							'desc' => '',
847
						),
848
						'debug'   => array(
849
							'text' => __( 'Debug', 'object-sync-for-salesforce' ),
850
							'id'   => 'debug',
851
							'desc' => '',
852
						),
853
					),
854
				),
855
			),
856
			'prune_logs'            => array(
857
				'title'    => __( 'Automatically delete old log entries?', 'object-sync-for-salesforce' ),
858
				'callback' => $callbacks['text'],
859
				'page'     => $page,
860
				'section'  => $section,
861
				'args'     => array(
862
					'type'     => 'checkbox',
863
					'validate' => 'absint',
864
					'desc'     => '',
865
					'constant' => '',
866
				),
867
			),
868
			'logs_how_old'          => array(
869
				'title'    => __( 'Age to delete log entries', 'object-sync-for-salesforce' ),
870
				'callback' => $callbacks['text'],
871
				'page'     => $page,
872
				'section'  => $section,
873
				'args'     => array(
874
					'type'     => 'text',
875
					'validate' => 'sanitize_validate_text',
876
					'desc'     => __( 'If automatic deleting is enabled, it will affect logs this old.', 'object-sync-for-salesforce' ),
877
					'default'  => '2 weeks',
878
					'constant' => '',
879
				),
880
			),
881
			'logs_how_often_number' => array(
882
				'title'    => __( 'Check for old logs every', 'object-sync-for-salesforce' ),
883
				'callback' => $callbacks['text'],
884
				'page'     => $page,
885
				'section'  => $section,
886
				'args'     => array(
887
					'type'     => 'number',
888
					'validate' => 'absint',
889
					'desc'     => '',
890
					'default'  => '1',
891
					'constant' => '',
892
				),
893
			),
894
			'logs_how_often_unit'   => array(
895
				'title'    => __( 'Time unit', 'object-sync-for-salesforce' ),
896
				'callback' => $callbacks['select'],
897
				'page'     => $page,
898
				'section'  => $section,
899
				'args'     => array(
900
					'type'     => 'select',
901
					'validate' => 'sanitize_validate_text',
902
					'desc'     => __( 'These two fields are how often the site will check for logs to delete.', 'object-sync-for-salesforce' ),
903
					'items'    => array(
904
						'minutes' => array(
905
							'text'  => __( 'Minutes', 'object-sync-for-salesforce' ),
906
							'value' => 'minutes',
907
						),
908
						'hours'   => array(
909
							'text'  => __( 'Hours', 'object-sync-for-salesforce' ),
910
							'value' => 'hours',
911
						),
912
						'days'    => array(
913
							'text'  => __( 'Days', 'object-sync-for-salesforce' ),
914
							'value' => 'days',
915
						),
916
					),
917
				),
918
			),
919
			'triggers_to_log'       => array(
920
				'title'    => __( 'Triggers to log', 'object-sync-for-salesforce' ),
921
				'callback' => $callbacks['checkboxes'],
922
				'page'     => $page,
923
				'section'  => $section,
924
				'args'     => array(
925
					'type'     => 'checkboxes',
926
					'validate' => 'sanitize_validate_text',
927
					'desc'     => __( 'these are the triggers to log', 'object-sync-for-salesforce' ),
928
					'items'    => array(
929
						$this->mappings->sync_wordpress_create => array(
930
							'text' => __( 'WordPress create', 'object-sync-for-salesforce' ),
931
							'id'   => 'wordpress_create',
932
							'desc' => '',
933
						),
934
						$this->mappings->sync_wordpress_update => array(
935
							'text' => __( 'WordPress update', 'object-sync-for-salesforce' ),
936
							'id'   => 'wordpress_update',
937
							'desc' => '',
938
						),
939
						$this->mappings->sync_wordpress_delete => array(
940
							'text' => __( 'WordPress delete', 'object-sync-for-salesforce' ),
941
							'id'   => 'wordpress_delete',
942
							'desc' => '',
943
						),
944
						$this->mappings->sync_sf_create => array(
945
							'text' => __( 'Salesforce create', 'object-sync-for-salesforce' ),
946
							'id'   => 'sf_create',
947
							'desc' => '',
948
						),
949
						$this->mappings->sync_sf_update => array(
950
							'text' => __( 'Salesforce update', 'object-sync-for-salesforce' ),
951
							'id'   => 'sf_update',
952
							'desc' => '',
953
						),
954
						$this->mappings->sync_sf_delete => array(
955
							'text' => __( 'Salesforce delete', 'object-sync-for-salesforce' ),
956
							'id'   => 'sf_delete',
957
							'desc' => '',
958
						),
959
					),
960
				),
961
			),
962
		);
963
		foreach ( $log_settings as $key => $attributes ) {
964
			$id       = $this->option_prefix . $key;
965
			$name     = $this->option_prefix . $key;
966
			$title    = $attributes['title'];
967
			$callback = $attributes['callback'];
968
			$page     = $attributes['page'];
969
			$section  = $attributes['section'];
970
			$args     = array_merge(
971
				$attributes['args'],
972
				array(
973
					'title'     => $title,
974
					'id'        => $id,
975
					'label_for' => $id,
976
					'name'      => $name,
977
				)
978
			);
979
			add_settings_field( $id, $title, $callback, $page, $section, $args );
0 ignored issues
show
Bug introduced by
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

979
			/** @scrutinizer ignore-call */ 
980
   add_settings_field( $id, $title, $callback, $page, $section, $args );
Loading history...
980
			register_setting( $page, $id );
0 ignored issues
show
Bug introduced by
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

980
			/** @scrutinizer ignore-call */ 
981
   register_setting( $page, $id );
Loading history...
981
		}
982
	}
983
984
	/**
985
	* Create the notices, settings, and conditions by which admin notices should appear
986
	*
987
	*/
988
	public function notices() {
989
990
		// before a notice is displayed, we should make sure we are on a page related to this plugin
991
		if ( ! isset( $_GET['page'] ) || 'object-sync-salesforce-admin' !== $_GET['page'] ) {
992
			return;
993
		}
994
995
		$get_data = filter_input_array( INPUT_GET, FILTER_SANITIZE_STRING );
996
		require_once plugin_dir_path( __FILE__ ) . '../classes/admin-notice.php';
0 ignored issues
show
Bug introduced by
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

996
		require_once /** @scrutinizer ignore-call */ plugin_dir_path( __FILE__ ) . '../classes/admin-notice.php';
Loading history...
997
998
		$notices = array(
999
			'permission'      => array(
1000
				'condition'   => false === $this->check_wordpress_admin_permissions(),
1001
				'message'     => __( "Your account does not have permission to edit the Salesforce REST API plugin's settings.", 'object-sync-for-salesforce' ),
0 ignored issues
show
Bug introduced by
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

1001
				'message'     => /** @scrutinizer ignore-call */ __( "Your account does not have permission to edit the Salesforce REST API plugin's settings.", 'object-sync-for-salesforce' ),
Loading history...
1002
				'type'        => 'error',
1003
				'dismissible' => false,
1004
			),
1005
			'fieldmap'        => array(
1006
				'condition'   => isset( $get_data['transient'] ),
1007
				'message'     => __( 'Errors kept this fieldmap from being saved.', 'object-sync-for-salesforce' ),
1008
				'type'        => 'error',
1009
				'dismissible' => true,
1010
			),
1011
			'object_map'      => array(
1012
				'condition'   => isset( $get_data['map_transient'] ),
1013
				'message'     => __( 'Errors kept this object map from being saved.', 'object-sync-for-salesforce' ),
1014
				'type'        => 'error',
1015
				'dismissible' => true,
1016
			),
1017
			'data_saved'      => array(
1018
				'condition'   => isset( $get_data['data_saved'] ) && 'true' === $get_data['data_saved'],
1019
				'message'     => __( 'This data was successfully saved.', 'object-sync-for-salesforce' ),
1020
				'type'        => 'success',
1021
				'dismissible' => true,
1022
			),
1023
			'data_save_error' => array(
1024
				'condition'   => isset( $get_data['data_saved'] ) && 'false' === $get_data['data_saved'],
1025
				'message'     => __( 'This data was not successfully saved. Try again.', 'object-sync-for-salesforce' ),
1026
				'type'        => 'error',
1027
				'dismissible' => true,
1028
			),
1029
		);
1030
1031
		foreach ( $notices as $key => $value ) {
1032
1033
			$condition = $value['condition'];
1034
			$message   = $value['message'];
1035
1036
			if ( isset( $value['dismissible'] ) ) {
1037
				$dismissible = $value['dismissible'];
1038
			} else {
1039
				$dismissible = false;
1040
			}
1041
1042
			if ( isset( $value['type'] ) ) {
1043
				$type = $value['type'];
1044
			} else {
1045
				$type = '';
1046
			}
1047
1048
			if ( ! isset( $value['template'] ) ) {
1049
				$template = '';
1050
			}
1051
1052
			if ( $condition ) {
1053
				new Object_Sync_Sf_Admin_Notice( $condition, $message, $dismissible, $type, $template );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $template does not seem to be defined for all execution paths leading up to this point.
Loading history...
1054
			}
1055
		}
1056
1057
	}
1058
1059
	/**
1060
	* Get all the Salesforce object settings for fieldmapping
1061
	* This takes either the $_POST array via ajax, or can be directly called with a $data array
1062
	*
1063
	* @param array $data
1064
	* data must contain a salesforce_object
1065
	* can optionally contain a type
1066
	* @return array $object_settings
1067
	*/
1068
	public function get_salesforce_object_description( $data = array() ) {
1069
		$ajax = false;
1070
		if ( empty( $data ) ) {
1071
			$data = filter_input_array( INPUT_POST, FILTER_SANITIZE_STRING );
1072
			$ajax = true;
1073
		}
1074
1075
		$object_description = array();
1076
1077
		if ( ! empty( $data['salesforce_object'] ) ) {
1078
			$object = $this->salesforce['sfapi']->object_describe( esc_attr( $data['salesforce_object'] ) );
0 ignored issues
show
Bug introduced by
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

1078
			$object = $this->salesforce['sfapi']->object_describe( /** @scrutinizer ignore-call */ esc_attr( $data['salesforce_object'] ) );
Loading history...
1079
1080
			$object_fields        = array();
1081
			$include_record_types = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $include_record_types is dead and can be removed.
Loading history...
1082
1083
			// these can come from ajax
1084
			$include = isset( $data['include'] ) ? (array) $data['include'] : array();
1085
			$include = array_map( 'esc_attr', $include );
1086
1087
			if ( in_array( 'fields', $include, true ) || empty( $include ) ) {
1088
				$type = isset( $data['field_type'] ) ? esc_attr( $data['field_type'] ) : ''; // can come from ajax
1089
				foreach ( $object['data']['fields'] as $key => $value ) {
1090
					if ( '' === $type || $type === $value['type'] ) {
1091
						$object_fields[ $key ] = $value;
1092
					}
1093
				}
1094
				$object_description['fields'] = $object_fields;
1095
			}
1096
1097
			if ( in_array( 'recordTypeInfos', $include, true ) ) {
1098
				if ( isset( $object['data']['recordTypeInfos'] ) && count( $object['data']['recordTypeInfos'] ) > 1 ) {
1099
					foreach ( $object['data']['recordTypeInfos'] as $type ) {
1100
						$object_record_types[ $type['recordTypeId'] ] = $type['name'];
1101
					}
1102
					$object_description['recordTypeInfos'] = $object_record_types;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $object_record_types seems to be defined by a foreach iteration on line 1099. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
1103
				}
1104
			}
1105
		}
1106
1107
		if ( true === $ajax ) {
1108
			wp_send_json_success( $object_description );
0 ignored issues
show
Bug introduced by
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

1108
			/** @scrutinizer ignore-call */ 
1109
   wp_send_json_success( $object_description );
Loading history...
1109
		} else {
1110
			return $object_description;
1111
		}
1112
	}
1113
1114
	/**
1115
	* Get Salesforce object fields for fieldmapping
1116
	*
1117
	* @param array $data
1118
	* data must contain a salesforce_object
1119
	* can optionally contain a type for the field
1120
	* @return array $object_fields
1121
	*/
1122
	public function get_salesforce_object_fields( $data = array() ) {
1123
1124
		if ( ! empty( $data['salesforce_object'] ) ) {
1125
			$object               = $this->salesforce['sfapi']->object_describe( esc_attr( $data['salesforce_object'] ) );
0 ignored issues
show
Bug introduced by
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

1125
			$object               = $this->salesforce['sfapi']->object_describe( /** @scrutinizer ignore-call */ esc_attr( $data['salesforce_object'] ) );
Loading history...
1126
			$object_fields        = array();
1127
			$type                 = isset( $data['type'] ) ? esc_attr( $data['type'] ) : '';
1128
			$include_record_types = isset( $data['include_record_types'] ) ? esc_attr( $data['include_record_types'] ) : false;
1129
			foreach ( $object['data']['fields'] as $key => $value ) {
1130
				if ( '' === $type || $type === $value['type'] ) {
1131
					$object_fields[ $key ] = $value;
1132
				}
1133
			}
1134
			if ( true === $include_record_types ) {
1135
				$object_record_types = array();
1136
				if ( isset( $object['data']['recordTypeInfos'] ) && count( $object['data']['recordTypeInfos'] ) > 1 ) {
1137
					foreach ( $object['data']['recordTypeInfos'] as $type ) {
1138
						$object_record_types[ $type['recordTypeId'] ] = $type['name'];
1139
					}
1140
				}
1141
			}
1142
		}
1143
1144
		return $object_fields;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $object_fields does not seem to be defined for all execution paths leading up to this point.
Loading history...
1145
1146
	}
1147
1148
	/**
1149
	* Get WordPress object fields for fieldmapping
1150
	* This takes either the $_POST array via ajax, or can be directly called with a $wordpress_object field
1151
	*
1152
	* @param string $wordpress_object
1153
	* @return array $object_fields
1154
	*/
1155
	public function get_wordpress_object_fields( $wordpress_object = '' ) {
1156
		$ajax      = false;
1157
		$post_data = filter_input_array( INPUT_POST, FILTER_SANITIZE_STRING );
1158
		if ( empty( $wordpress_object ) ) {
1159
			$wordpress_object = isset( $post_data['wordpress_object'] ) ? sanitize_text_field( wp_unslash( $post_data['wordpress_object'] ) ) : '';
0 ignored issues
show
Bug introduced by
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

1159
			$wordpress_object = isset( $post_data['wordpress_object'] ) ? /** @scrutinizer ignore-call */ sanitize_text_field( wp_unslash( $post_data['wordpress_object'] ) ) : '';
Loading history...
Bug introduced by
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

1159
			$wordpress_object = isset( $post_data['wordpress_object'] ) ? sanitize_text_field( /** @scrutinizer ignore-call */ wp_unslash( $post_data['wordpress_object'] ) ) : '';
Loading history...
1160
			$ajax             = true;
1161
		}
1162
1163
		$object_fields = $this->wordpress->get_wordpress_object_fields( $wordpress_object );
1164
1165
		if ( true === $ajax ) {
1166
			wp_send_json_success( $object_fields );
0 ignored issues
show
Bug introduced by
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

1166
			/** @scrutinizer ignore-call */ 
1167
   wp_send_json_success( $object_fields );
Loading history...
1167
		} else {
1168
			return $object_fields;
1169
		}
1170
	}
1171
1172
	/**
1173
	* Get WordPress and Salesforce object fields together for fieldmapping
1174
	* This takes either the $_POST array via ajax, or can be directly called with $wordpress_object and $salesforce_object fields
1175
	*
1176
	* @param string $wordpress_object
1177
	* @param string $salesforce_object
1178
	* @return array $object_fields
1179
	*/
1180
	public function get_wp_sf_object_fields( $wordpress_object = '', $salesforce = '' ) {
1181
		$post_data = filter_input_array( INPUT_POST, FILTER_SANITIZE_STRING );
1182
		if ( empty( $wordpress_object ) ) {
1183
			$wordpress_object = isset( $post_data['wordpress_object'] ) ? sanitize_text_field( wp_unslash( $post_data['wordpress_object'] ) ) : '';
0 ignored issues
show
Bug introduced by
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

1183
			$wordpress_object = isset( $post_data['wordpress_object'] ) ? sanitize_text_field( /** @scrutinizer ignore-call */ wp_unslash( $post_data['wordpress_object'] ) ) : '';
Loading history...
Bug introduced by
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

1183
			$wordpress_object = isset( $post_data['wordpress_object'] ) ? /** @scrutinizer ignore-call */ sanitize_text_field( wp_unslash( $post_data['wordpress_object'] ) ) : '';
Loading history...
1184
		}
1185
		if ( empty( $salesforce_object ) ) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $salesforce_object seems to never exist and therefore empty should always be true.
Loading history...
1186
			$salesforce_object = isset( $post_data['salesforce_object'] ) ? sanitize_text_field( wp_unslash( $post_data['salesforce_object'] ) ) : '';
1187
		}
1188
1189
		$object_fields['wordpress']  = $this->get_wordpress_object_fields( $wordpress_object );
0 ignored issues
show
Comprehensibility Best Practice introduced by
$object_fields was never initialized. Although not strictly required by PHP, it is generally a good practice to add $object_fields = array(); before regardless.
Loading history...
1190
		$object_fields['salesforce'] = $this->get_salesforce_object_fields(
1191
			array(
1192
				'salesforce_object' => $salesforce_object,
1193
			)
1194
		);
1195
1196
		if ( ! empty( $post_data ) ) {
1197
			wp_send_json_success( $object_fields );
0 ignored issues
show
Bug introduced by
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

1197
			/** @scrutinizer ignore-call */ 
1198
   wp_send_json_success( $object_fields );
Loading history...
1198
		} else {
1199
			return $object_fields;
1200
		}
1201
	}
1202
1203
	/**
1204
	* Manually push the WordPress object to Salesforce
1205
	* This takes either the $_POST array via ajax, or can be directly called with $wordpress_object and $wordpress_id fields
1206
	*
1207
	* @param string $wordpress_object
1208
	* @param int $wordpress_id
1209
	*/
1210
	public function push_to_salesforce( $wordpress_object = '', $wordpress_id = '' ) {
1211
		$post_data = filter_input_array( INPUT_POST, FILTER_SANITIZE_STRING );
1212
		if ( empty( $wordpress_object ) && empty( $wordpress_id ) ) {
1213
			$wordpress_object = isset( $post_data['wordpress_object'] ) ? sanitize_text_field( wp_unslash( $post_data['wordpress_object'] ) ) : '';
0 ignored issues
show
Bug introduced by
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

1213
			$wordpress_object = isset( $post_data['wordpress_object'] ) ? sanitize_text_field( /** @scrutinizer ignore-call */ wp_unslash( $post_data['wordpress_object'] ) ) : '';
Loading history...
Bug introduced by
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

1213
			$wordpress_object = isset( $post_data['wordpress_object'] ) ? /** @scrutinizer ignore-call */ sanitize_text_field( wp_unslash( $post_data['wordpress_object'] ) ) : '';
Loading history...
1214
			$wordpress_id     = isset( $post_data['wordpress_id'] ) ? absint( $post_data['wordpress_id'] ) : '';
0 ignored issues
show
Bug introduced by
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

1214
			$wordpress_id     = isset( $post_data['wordpress_id'] ) ? /** @scrutinizer ignore-call */ absint( $post_data['wordpress_id'] ) : '';
Loading history...
1215
		}
1216
		$data   = $this->wordpress->get_wordpress_object_data( $wordpress_object, $wordpress_id );
1217
		$result = $this->push->manual_object_update( $data, $wordpress_object );
1218
1219
		if ( ! empty( $post_data['wordpress_object'] ) && ! empty( $post_data['wordpress_id'] ) ) {
1220
			wp_send_json_success( $result );
0 ignored issues
show
Bug introduced by
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

1220
			/** @scrutinizer ignore-call */ 
1221
   wp_send_json_success( $result );
Loading history...
1221
		} else {
1222
			return $result;
1223
		}
1224
1225
	}
1226
1227
	/**
1228
	* Manually pull the Salesforce object into WordPress
1229
	* This takes either the $_POST array via ajax, or can be directly called with $salesforce_id fields
1230
	*
1231
	* @param string $salesforce_id
1232
	* @param string $wordpress_object
1233
	*/
1234
	public function pull_from_salesforce( $salesforce_id = '', $wordpress_object = '' ) {
1235
		$post_data = filter_input_array( INPUT_POST, FILTER_SANITIZE_STRING );
1236
		if ( empty( $wordpress_object ) && empty( $salesforce_id ) ) {
1237
			$wordpress_object = isset( $post_data['wordpress_object'] ) ? sanitize_text_field( wp_unslash( $post_data['wordpress_object'] ) ) : '';
0 ignored issues
show
Bug introduced by
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

1237
			$wordpress_object = isset( $post_data['wordpress_object'] ) ? /** @scrutinizer ignore-call */ sanitize_text_field( wp_unslash( $post_data['wordpress_object'] ) ) : '';
Loading history...
Bug introduced by
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

1237
			$wordpress_object = isset( $post_data['wordpress_object'] ) ? sanitize_text_field( /** @scrutinizer ignore-call */ wp_unslash( $post_data['wordpress_object'] ) ) : '';
Loading history...
1238
			$salesforce_id    = isset( $post_data['salesforce_id'] ) ? sanitize_text_field( wp_unslash( $post_data['salesforce_id'] ) ) : '';
1239
		}
1240
		$type   = $this->salesforce['sfapi']->get_sobject_type( $salesforce_id );
1241
		$result = $this->pull->manual_pull( $type, $salesforce_id, $wordpress_object ); // we want the wp object to make sure we get the right fieldmap
1242
		if ( ! empty( $post_data ) ) {
1243
			wp_send_json_success( $result );
0 ignored issues
show
Bug introduced by
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

1243
			/** @scrutinizer ignore-call */ 
1244
   wp_send_json_success( $result );
Loading history...
1244
		} else {
1245
			return $result;
1246
		}
1247
	}
1248
1249
	/**
1250
	* Manually pull the Salesforce object into WordPress
1251
	* This takes an id for a mapping object row
1252
	*
1253
	* @param int $mapping_id
1254
	*/
1255
	public function refresh_mapped_data( $mapping_id = '' ) {
1256
		$post_data = filter_input_array( INPUT_POST, FILTER_SANITIZE_STRING );
1257
		if ( empty( $mapping_id ) ) {
1258
			$mapping_id = isset( $post_data['mapping_id'] ) ? absint( $post_data['mapping_id'] ) : '';
0 ignored issues
show
Bug introduced by
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

1258
			$mapping_id = isset( $post_data['mapping_id'] ) ? /** @scrutinizer ignore-call */ absint( $post_data['mapping_id'] ) : '';
Loading history...
1259
		}
1260
		$result = $this->mappings->get_object_maps(
1261
			array(
1262
				'id' => $mapping_id,
1263
			)
1264
		);
1265
		if ( ! empty( $post_data ) ) {
1266
			wp_send_json_success( $result );
0 ignored issues
show
Bug introduced by
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

1266
			/** @scrutinizer ignore-call */ 
1267
   wp_send_json_success( $result );
Loading history...
1267
		} else {
1268
			return $result;
1269
		}
1270
	}
1271
1272
	/**
1273
	* Prepare fieldmap data and redirect after processing
1274
	* This runs when the create or update forms are submitted
1275
	* It is public because it depends on an admin hook
1276
	* It then calls the Object_Sync_Sf_Mapping class and sends prepared data over to it, then redirects to the correct page
1277
	* This method does include error handling, by loading the submission in a transient if there is an error, and then deleting it upon success
1278
	*
1279
	*/
1280
	public function prepare_fieldmap_data() {
1281
		$error     = false;
1282
		$post_data = filter_input_array( INPUT_POST, FILTER_SANITIZE_STRING );
1283
		$cachekey  = md5( wp_json_encode( $post_data ) );
0 ignored issues
show
Bug introduced by
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

1283
		$cachekey  = md5( /** @scrutinizer ignore-call */ wp_json_encode( $post_data ) );
Loading history...
1284
1285
		if ( ! isset( $post_data['label'] ) || ! isset( $post_data['salesforce_object'] ) || ! isset( $post_data['wordpress_object'] ) ) {
1286
			$error = true;
1287
		}
1288
		if ( true === $error ) {
1289
			$this->sfwp_transients->set( $cachekey, $post_data, $this->wordpress->options['cache_expiration'] );
1290
			if ( '' !== $cachekey ) {
1291
				$url = esc_url_raw( $post_data['redirect_url_error'] ) . '&transient=' . $cachekey;
0 ignored issues
show
Bug introduced by
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

1291
				$url = /** @scrutinizer ignore-call */ esc_url_raw( $post_data['redirect_url_error'] ) . '&transient=' . $cachekey;
Loading history...
1292
			}
1293
		} else { // there are no errors
1294
			// send the row to the fieldmap class
1295
			// if it is add or clone, use the create method
1296
			$method            = esc_attr( $post_data['method'] );
0 ignored issues
show
Bug introduced by
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

1296
			$method            = /** @scrutinizer ignore-call */ esc_attr( $post_data['method'] );
Loading history...
1297
			$salesforce_fields = $this->get_salesforce_object_fields(
1298
				array(
1299
					'salesforce_object' => $post_data['salesforce_object'],
1300
				)
1301
			);
1302
			$wordpress_fields  = $this->get_wordpress_object_fields( $post_data['wordpress_object'] );
1303
			if ( 'add' === $method || 'clone' === $method ) {
1304
				$result = $this->mappings->create_fieldmap( $post_data, $wordpress_fields, $salesforce_fields );
1305
			} elseif ( 'edit' === $method ) { // if it is edit, use the update method
1306
				$id     = esc_attr( $post_data['id'] );
1307
				$result = $this->mappings->update_fieldmap( $post_data, $wordpress_fields, $salesforce_fields, $id );
1308
			}
1309
			if ( false === $result ) { // if the database didn't save, it's still an error
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.
Loading history...
1310
				$this->sfwp_transients->set( $cachekey, $post_data, $this->wordpress->options['cache_expiration'] );
1311
				if ( '' !== $cachekey ) {
1312
					$url = esc_url_raw( $post_data['redirect_url_error'] ) . '&transient=' . $cachekey;
1313
				}
1314
			} else {
1315
				if ( isset( $post_data['transient'] ) ) { // there was previously an error saved. can delete it now.
1316
					$this->sfwp_transients->delete( esc_attr( $post_data['map_transient'] ) );
1317
				}
1318
				// then send the user to the list of fieldmaps
1319
				$url = esc_url_raw( $post_data['redirect_url_success'] );
1320
			}
1321
		}
1322
		wp_safe_redirect( $url );
0 ignored issues
show
Bug introduced by
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

1322
		/** @scrutinizer ignore-call */ 
1323
  wp_safe_redirect( $url );
Loading history...
Comprehensibility Best Practice introduced by
The variable $url does not seem to be defined for all execution paths leading up to this point.
Loading history...
1323
		exit();
0 ignored issues
show
Best Practice introduced by
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...
1324
	}
1325
1326
	/**
1327
	* Delete fieldmap data and redirect after processing
1328
	* This runs when the delete link is clicked, after the user confirms
1329
	* It is public because it depends on an admin hook
1330
	* It then calls the Object_Sync_Sf_Mapping class and the delete method
1331
	*
1332
	*/
1333
	public function delete_fieldmap() {
1334
		$post_data = filter_input_array( INPUT_POST, FILTER_SANITIZE_STRING );
1335
		if ( $post_data['id'] ) {
1336
			$result = $this->mappings->delete_fieldmap( $post_data['id'] );
1337
			if ( true === $result ) {
1338
				$url = esc_url_raw( $post_data['redirect_url_success'] );
0 ignored issues
show
Bug introduced by
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

1338
				$url = /** @scrutinizer ignore-call */ esc_url_raw( $post_data['redirect_url_success'] );
Loading history...
1339
			} else {
1340
				$url = esc_url_raw( $post_data['redirect_url_error'] . '&id=' . $post_data['id'] );
1341
			}
1342
			wp_safe_redirect( $url );
0 ignored issues
show
Bug introduced by
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

1342
			/** @scrutinizer ignore-call */ 
1343
   wp_safe_redirect( $url );
Loading history...
1343
			exit();
0 ignored issues
show
Best Practice introduced by
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...
1344
		}
1345
	}
1346
1347
	/**
1348
	* Prepare object data and redirect after processing
1349
	* This runs when the update form is submitted
1350
	* It is public because it depends on an admin hook
1351
	* It then calls the Object_Sync_Sf_Mapping class and sends prepared data over to it, then redirects to the correct page
1352
	* This method does include error handling, by loading the submission in a transient if there is an error, and then deleting it upon success
1353
	*
1354
	*/
1355
	public function prepare_object_map_data() {
1356
		$error     = false;
1357
		$post_data = filter_input_array( INPUT_POST, FILTER_SANITIZE_STRING );
1358
		$cachekey  = md5( wp_json_encode( $post_data ) );
0 ignored issues
show
Bug introduced by
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

1358
		$cachekey  = md5( /** @scrutinizer ignore-call */ wp_json_encode( $post_data ) );
Loading history...
1359
1360
		if ( ! isset( $post_data['wordpress_id'] ) || ! isset( $post_data['salesforce_id'] ) ) {
1361
			$error = true;
1362
		}
1363
		if ( true === $error ) {
1364
			$this->sfwp_transients->set( $cachekey, $post_data, $this->wordpress->options['cache_expiration'] );
1365
			if ( '' !== $cachekey ) {
1366
				$url = esc_url_raw( $post_data['redirect_url_error'] ) . '&map_transient=' . $cachekey;
0 ignored issues
show
Bug introduced by
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

1366
				$url = /** @scrutinizer ignore-call */ esc_url_raw( $post_data['redirect_url_error'] ) . '&map_transient=' . $cachekey;
Loading history...
1367
			}
1368
		} else { // there are no errors
1369
			// send the row to the object map class
1370
			$method = esc_attr( $post_data['method'] );
0 ignored issues
show
Bug introduced by
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

1370
			$method = /** @scrutinizer ignore-call */ esc_attr( $post_data['method'] );
Loading history...
1371
			if ( 'edit' === $method ) { // if it is edit, use the update method
1372
				$id     = esc_attr( $post_data['id'] );
1373
				$result = $this->mappings->update_object_map( $post_data, $id );
1374
			}
1375
			if ( false === $result ) { // if the database didn't save, it's still an error
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.
Loading history...
1376
				$this->sfwp_transients->set( $cachekey, $post_data, $this->wordpress->options['cache_expiration'] );
1377
				if ( '' !== $cachekey ) {
1378
					$url = esc_url_raw( $post_data['redirect_url_error'] ) . '&map_transient=' . $cachekey;
1379
				}
1380
			} else {
1381
				if ( isset( $post_data['map_transient'] ) ) { // there was previously an error saved. can delete it now.
1382
					$this->sfwp_transients->delete( esc_attr( $post_data['map_transient'] ) );
1383
				}
1384
				// then send the user to the success redirect url
1385
				$url = esc_url_raw( $post_data['redirect_url_success'] );
1386
			}
1387
		}
1388
		wp_safe_redirect( $url );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $url does not seem to be defined for all execution paths leading up to this point.
Loading history...
Bug introduced by
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

1388
		/** @scrutinizer ignore-call */ 
1389
  wp_safe_redirect( $url );
Loading history...
1389
		exit();
0 ignored issues
show
Best Practice introduced by
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...
1390
	}
1391
1392
	/**
1393
	* Delete object map data and redirect after processing
1394
	* This runs when the delete link is clicked on an error row, after the user confirms
1395
	* It is public because it depends on an admin hook
1396
	* It then calls the Object_Sync_Sf_Mapping class and the delete method
1397
	*
1398
	*/
1399
	public function delete_object_map() {
1400
		$post_data = filter_input_array( INPUT_POST, FILTER_SANITIZE_STRING );
1401
		if ( $post_data['id'] ) {
1402
			$result = $this->mappings->delete_object_map( $post_data['id'] );
1403
			if ( true === $result ) {
1404
				$url = esc_url_raw( $post_data['redirect_url_success'] );
0 ignored issues
show
Bug introduced by
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

1404
				$url = /** @scrutinizer ignore-call */ esc_url_raw( $post_data['redirect_url_success'] );
Loading history...
1405
			} else {
1406
				$url = esc_url_raw( $post_data['redirect_url_error'] . '&id=' . $post_data['id'] );
1407
			}
1408
			wp_safe_redirect( $url );
0 ignored issues
show
Bug introduced by
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

1408
			/** @scrutinizer ignore-call */ 
1409
   wp_safe_redirect( $url );
Loading history...
1409
			exit();
0 ignored issues
show
Best Practice introduced by
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...
1410
		}
1411
	}
1412
1413
	/**
1414
	* Import a json file and use it for plugin data
1415
	*
1416
	*/
1417
	public function import_json_file() {
1418
1419
		if ( ! wp_verify_nonce( $_POST['object_sync_for_salesforce_nonce_import'], 'object_sync_for_salesforce_nonce_import' ) ) {
0 ignored issues
show
Bug introduced by
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

1419
		if ( ! /** @scrutinizer ignore-call */ wp_verify_nonce( $_POST['object_sync_for_salesforce_nonce_import'], 'object_sync_for_salesforce_nonce_import' ) ) {
Loading history...
1420
			return;
1421
		}
1422
		if ( ! current_user_can( 'manage_options' ) ) {
0 ignored issues
show
Bug introduced by
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

1422
		if ( ! /** @scrutinizer ignore-call */ current_user_can( 'manage_options' ) ) {
Loading history...
1423
			return;
1424
		}
1425
		$path      = $_FILES['import_file']['name'];
1426
		$extension = pathinfo( $path, PATHINFO_EXTENSION );
1427
		if ( 'json' !== $extension ) {
1428
			wp_die( __( 'Please upload a valid .json file' ) );
0 ignored issues
show
Bug introduced by
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

1428
			wp_die( /** @scrutinizer ignore-call */ __( 'Please upload a valid .json file' ) );
Loading history...
Bug introduced by
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

1428
			/** @scrutinizer ignore-call */ 
1429
   wp_die( __( 'Please upload a valid .json file' ) );
Loading history...
1429
		}
1430
1431
		$import_file = $_FILES['import_file']['tmp_name'];
1432
		if ( empty( $import_file ) ) {
1433
			wp_die( __( 'Please upload a file to import' ) );
1434
		}
1435
1436
		// Retrieve the data from the file and convert the json object to an array.
1437
		$data = (array) json_decode( file_get_contents( $import_file ), true );
1438
1439
		// if there is only one object map, fix the array
1440
		if ( isset( $data['object_maps'] ) ) {
1441
			if ( count( $data['object_maps'] ) === count( $data['object_maps'], COUNT_RECURSIVE ) ) {
1442
				$data['object_maps'] = array( 0 => $data['object_maps'] );
1443
			}
1444
		}
1445
1446
		$overwrite = isset( $_POST['overwrite'] ) ? esc_attr( $_POST['overwrite'] ) : '';
0 ignored issues
show
Bug introduced by
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

1446
		$overwrite = isset( $_POST['overwrite'] ) ? /** @scrutinizer ignore-call */ esc_attr( $_POST['overwrite'] ) : '';
Loading history...
1447
		if ( '1' === $overwrite ) {
1448
			if ( isset( $data['fieldmaps'] ) ) {
1449
				$fieldmaps = $this->mappings->get_fieldmaps();
1450
				foreach ( $fieldmaps as $fieldmap ) {
1451
					$id     = $fieldmap['id'];
1452
					$delete = $this->mappings->delete_fieldmap( $id );
0 ignored issues
show
Unused Code introduced by
The assignment to $delete is dead and can be removed.
Loading history...
1453
				}
1454
			}
1455
			if ( isset( $data['object_maps'] ) ) {
1456
				$object_maps = $this->mappings->get_object_maps();
1457
1458
				// if there is only one existing object map, fix the array
1459
				if ( count( $object_maps ) === count( $object_maps, COUNT_RECURSIVE ) ) {
1460
					$object_maps = array( 0 => $object_maps );
1461
				}
1462
1463
				foreach ( $object_maps as $object_map ) {
1464
					$id     = $object_map['id'];
1465
					$delete = $this->mappings->delete_object_map( $id );
1466
				}
1467
			}
1468
			if ( isset( $data['plugin_settings'] ) ) {
1469
				foreach ( $data['plugin_settings'] as $key => $value ) {
1470
					delete_option( $value['option_name'] );
0 ignored issues
show
Bug introduced by
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

1470
					/** @scrutinizer ignore-call */ 
1471
     delete_option( $value['option_name'] );
Loading history...
1471
				}
1472
			}
1473
		}
1474
1475
		$success = true;
1476
1477
		if ( isset( $data['fieldmaps'] ) ) {
1478
			foreach ( $data['fieldmaps'] as $fieldmap ) {
1479
				unset( $fieldmap['id'] );
1480
				$create = $this->mappings->create_fieldmap( $fieldmap );
1481
				if ( false === $create ) {
1482
					$success = false;
1483
				}
1484
			}
1485
		}
1486
1487
		if ( isset( $data['object_maps'] ) ) {
1488
			foreach ( $data['object_maps'] as $object_map ) {
1489
				unset( $object_map['id'] );
1490
				$create = $this->mappings->create_object_map( $object_map );
1491
				if ( false === $create ) {
1492
					$success = false;
1493
				}
1494
			}
1495
		}
1496
1497
		if ( isset( $data['plugin_settings'] ) ) {
1498
			foreach ( $data['plugin_settings'] as $key => $value ) {
1499
				update_option( $value['option_name'], maybe_unserialize( $value['option_value'] ), $value['autoload'] );
0 ignored issues
show
Bug introduced by
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

1499
				/** @scrutinizer ignore-call */ 
1500
    update_option( $value['option_name'], maybe_unserialize( $value['option_value'] ), $value['autoload'] );
Loading history...
Bug introduced by
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

1499
				update_option( $value['option_name'], /** @scrutinizer ignore-call */ maybe_unserialize( $value['option_value'] ), $value['autoload'] );
Loading history...
1500
			}
1501
		}
1502
1503
		if ( true === $success ) {
1504
			wp_safe_redirect( get_admin_url( null, 'options-general.php?page=object-sync-salesforce-admin&tab=import-export&data_saved=true' ) );
0 ignored issues
show
Bug introduced by
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

1504
			/** @scrutinizer ignore-call */ 
1505
   wp_safe_redirect( get_admin_url( null, 'options-general.php?page=object-sync-salesforce-admin&tab=import-export&data_saved=true' ) );
Loading history...
Bug introduced by
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

1504
			wp_safe_redirect( /** @scrutinizer ignore-call */ get_admin_url( null, 'options-general.php?page=object-sync-salesforce-admin&tab=import-export&data_saved=true' ) );
Loading history...
1505
			exit;
0 ignored issues
show
Best Practice introduced by
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...
1506
		} else {
1507
			wp_safe_redirect( get_admin_url( null, 'options-general.php?page=object-sync-salesforce-admin&tab=import-export&data_saved=false' ) );
1508
			exit;
0 ignored issues
show
Best Practice introduced by
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...
1509
		}
1510
1511
	}
1512
1513
	/**
1514
	* Create a json file for exporting
1515
	*
1516
	*/
1517
	public function export_json_file() {
1518
1519
		if ( ! wp_verify_nonce( $_POST['object_sync_for_salesforce_nonce_export'], 'object_sync_for_salesforce_nonce_export' ) ) {
0 ignored issues
show
Bug introduced by
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

1519
		if ( ! /** @scrutinizer ignore-call */ wp_verify_nonce( $_POST['object_sync_for_salesforce_nonce_export'], 'object_sync_for_salesforce_nonce_export' ) ) {
Loading history...
1520
			return;
1521
		}
1522
		if ( ! current_user_can( 'manage_options' ) ) {
0 ignored issues
show
Bug introduced by
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

1522
		if ( ! /** @scrutinizer ignore-call */ current_user_can( 'manage_options' ) ) {
Loading history...
1523
			return;
1524
		}
1525
		$post_data = filter_input_array( INPUT_POST, FILTER_SANITIZE_STRING );
1526
		$export    = array();
1527
		if ( in_array( 'fieldmaps', $post_data['export'] ) ) {
1528
			$export['fieldmaps'] = $this->mappings->get_fieldmaps();
1529
		}
1530
		if ( in_array( 'object_maps', $post_data['export'] ) ) {
1531
			$export['object_maps'] = $this->mappings->get_object_maps();
1532
		}
1533
		if ( in_array( 'plugin_settings', $post_data['export'] ) ) {
1534
			$export['plugin_settings'] = $this->wpdb->get_results( 'SELECT * FROM ' . $this->wpdb->prefix . 'options' . ' WHERE option_name like "' . $this->option_prefix . '%"', ARRAY_A );
0 ignored issues
show
Bug introduced by
The constant ARRAY_A was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
1535
		}
1536
		nocache_headers();
0 ignored issues
show
Bug introduced by
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

1536
		/** @scrutinizer ignore-call */ 
1537
  nocache_headers();
Loading history...
1537
		header( 'Content-Type: application/json; charset=utf-8' );
1538
		header( 'Content-Disposition: attachment; filename=object-sync-for-salesforce-data-export-' . date( 'm-d-Y' ) . '.json' );
1539
		header( 'Expires: 0' );
1540
		echo wp_json_encode( $export );
0 ignored issues
show
Bug introduced by
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

1540
		echo /** @scrutinizer ignore-call */ wp_json_encode( $export );
Loading history...
1541
		exit;
0 ignored issues
show
Best Practice introduced by
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...
1542
	}
1543
1544
	/**
1545
	* Default display for <input> fields
1546
	*
1547
	* @param array $args
1548
	*/
1549
	public function display_input_field( $args ) {
1550
		$type    = $args['type'];
1551
		$id      = $args['label_for'];
1552
		$name    = $args['name'];
1553
		$desc    = $args['desc'];
1554
		$checked = '';
1555
1556
		$class = 'regular-text';
1557
1558
		if ( 'checkbox' === $type ) {
1559
			$class = 'checkbox';
1560
		}
1561
1562
		if ( ! isset( $args['constant'] ) || ! defined( $args['constant'] ) ) {
1563
			$value = esc_attr( get_option( $id, '' ) );
0 ignored issues
show
Bug introduced by
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

1563
			$value = esc_attr( /** @scrutinizer ignore-call */ get_option( $id, '' ) );
Loading history...
Bug introduced by
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

1563
			$value = /** @scrutinizer ignore-call */ esc_attr( get_option( $id, '' ) );
Loading history...
1564
			if ( 'checkbox' === $type ) {
1565
				if ( '1' === $value ) {
1566
					$checked = 'checked ';
1567
				}
1568
				$value = 1;
1569
			}
1570
			if ( '' === $value && isset( $args['default'] ) && '' !== $args['default'] ) {
1571
				$value = $args['default'];
1572
			}
1573
1574
			echo sprintf( '<input type="%1$s" value="%2$s" name="%3$s" id="%4$s" class="%5$s"%6$s>',
1575
				esc_attr( $type ),
1576
				esc_attr( $value ),
1577
				esc_attr( $name ),
1578
				esc_attr( $id ),
1579
				sanitize_html_class( $class . esc_html( ' code' ) ),
0 ignored issues
show
Bug introduced by
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

1579
				sanitize_html_class( $class . /** @scrutinizer ignore-call */ esc_html( ' code' ) ),
Loading history...
Bug introduced by
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

1579
				/** @scrutinizer ignore-call */ 
1580
    sanitize_html_class( $class . esc_html( ' code' ) ),
Loading history...
1580
				esc_html( $checked )
1581
			);
1582
			if ( '' !== $desc ) {
1583
				echo sprintf( '<p class="description">%1$s</p>',
1584
					esc_html( $desc )
1585
				);
1586
			}
1587
		} else {
1588
			echo sprintf( '<p><code>%1$s</code></p>',
1589
				esc_html__( 'Defined in wp-config.php', 'object-sync-for-salesforce' )
0 ignored issues
show
Bug introduced by
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

1589
				/** @scrutinizer ignore-call */ 
1590
    esc_html__( 'Defined in wp-config.php', 'object-sync-for-salesforce' )
Loading history...
1590
			);
1591
		}
1592
	}
1593
1594
	/**
1595
	* Display for multiple checkboxes
1596
	* Above method can handle a single checkbox as it is
1597
	*
1598
	* @param array $args
1599
	*/
1600
	public function display_checkboxes( $args ) {
1601
		$type    = 'checkbox';
1602
		$name    = $args['name'];
1603
		$options = get_option( $name, array() );
0 ignored issues
show
Bug introduced by
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

1603
		$options = /** @scrutinizer ignore-call */ get_option( $name, array() );
Loading history...
1604
		foreach ( $args['items'] as $key => $value ) {
1605
			$text    = $value['text'];
1606
			$id      = $value['id'];
1607
			$desc    = $value['desc'];
1608
			$checked = '';
1609
			if ( is_array( $options ) && in_array( (string) $key, $options, true ) ) {
1610
				$checked = 'checked';
1611
			} elseif ( is_array( $options ) && empty( $options ) ) {
1612
				if ( isset( $value['default'] ) && true === $value['default'] ) {
1613
					$checked = 'checked';
1614
				}
1615
			}
1616
			echo sprintf( '<div class="checkbox"><label><input type="%1$s" value="%2$s" name="%3$s[]" id="%4$s"%5$s>%6$s</label></div>',
1617
				esc_attr( $type ),
0 ignored issues
show
Bug introduced by
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

1617
				/** @scrutinizer ignore-call */ 
1618
    esc_attr( $type ),
Loading history...
1618
				esc_attr( $key ),
1619
				esc_attr( $name ),
1620
				esc_attr( $id ),
1621
				esc_html( $checked ),
0 ignored issues
show
Bug introduced by
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

1621
				/** @scrutinizer ignore-call */ 
1622
    esc_html( $checked ),
Loading history...
1622
				esc_html( $text )
1623
			);
1624
			if ( '' !== $desc ) {
1625
				echo sprintf( '<p class="description">%1$s</p>',
1626
					esc_html( $desc )
1627
				);
1628
			}
1629
		}
1630
	}
1631
1632
	/**
1633
	* Display for a dropdown
1634
	*
1635
	* @param array $args
1636
	*/
1637
	public function display_select( $args ) {
1638
		$type = $args['type'];
0 ignored issues
show
Unused Code introduced by
The assignment to $type is dead and can be removed.
Loading history...
1639
		$id   = $args['label_for'];
1640
		$name = $args['name'];
1641
		$desc = $args['desc'];
1642
		if ( ! isset( $args['constant'] ) || ! defined( $args['constant'] ) ) {
1643
			$current_value = get_option( $name );
0 ignored issues
show
Bug introduced by
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

1643
			$current_value = /** @scrutinizer ignore-call */ get_option( $name );
Loading history...
1644
1645
			echo sprintf( '<div class="select"><select id="%1$s" name="%2$s"><option value="">- ' . __( 'Select one', 'object-sync-for-salesforce' ) . ' -</option>',
0 ignored issues
show
Bug introduced by
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

1645
			echo sprintf( '<div class="select"><select id="%1$s" name="%2$s"><option value="">- ' . /** @scrutinizer ignore-call */ __( 'Select one', 'object-sync-for-salesforce' ) . ' -</option>',
Loading history...
1646
				esc_attr( $id ),
0 ignored issues
show
Bug introduced by
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

1646
				/** @scrutinizer ignore-call */ 
1647
    esc_attr( $id ),
Loading history...
1647
				esc_attr( $name )
1648
			);
1649
1650
			foreach ( $args['items'] as $key => $value ) {
1651
				$text     = $value['text'];
1652
				$value    = $value['value'];
1653
				$selected = '';
1654
				if ( $key === $current_value || $value === $current_value ) {
1655
					$selected = ' selected';
1656
				}
1657
1658
				echo sprintf( '<option value="%1$s"%2$s>%3$s</option>',
1659
					esc_attr( $value ),
1660
					esc_attr( $selected ),
1661
					esc_html( $text )
0 ignored issues
show
Bug introduced by
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

1661
					/** @scrutinizer ignore-call */ 
1662
     esc_html( $text )
Loading history...
1662
				);
1663
1664
			}
1665
			echo '</select>';
1666
			if ( '' !== $desc ) {
1667
				echo sprintf( '<p class="description">%1$s</p>',
1668
					esc_html( $desc )
1669
				);
1670
			}
1671
			echo '</div>';
1672
		} else {
1673
			echo sprintf( '<p><code>%1$s</code></p>',
1674
				esc_html__( 'Defined in wp-config.php', 'object-sync-for-salesforce' )
0 ignored issues
show
Bug introduced by
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

1674
				/** @scrutinizer ignore-call */ 
1675
    esc_html__( 'Defined in wp-config.php', 'object-sync-for-salesforce' )
Loading history...
1675
			);
1676
		}
1677
	}
1678
1679
	/**
1680
	* Dropdown formatted list of Salesforce API versions
1681
	*
1682
	* @return array $args
1683
	*/
1684
	private function version_options() {
1685
		$versions = $this->salesforce['sfapi']->get_api_versions();
1686
		$args     = array();
1687
		foreach ( $versions['data'] as $key => $value ) {
1688
			$args[] = array(
1689
				'value' => $value['version'],
1690
				'text'  => $value['label'] . ' (' . $value['version'] . ')',
1691
			);
1692
		}
1693
		return $args;
1694
	}
1695
1696
	/**
1697
	* Default display for <a href> links
1698
	*
1699
	* @param array $args
1700
	*/
1701
	public function display_link( $args ) {
1702
		$label = $args['label'];
1703
		$desc  = $args['desc'];
1704
		$url   = $args['url'];
1705
		if ( isset( $args['link_class'] ) ) {
1706
			echo sprintf( '<p><a class="%1$s" href="%2$s">%3$s</a></p>',
1707
				esc_attr( $args['link_class'] ),
0 ignored issues
show
Bug introduced by
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

1707
				/** @scrutinizer ignore-call */ 
1708
    esc_attr( $args['link_class'] ),
Loading history...
1708
				esc_url( $url ),
0 ignored issues
show
Bug introduced by
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

1708
				/** @scrutinizer ignore-call */ 
1709
    esc_url( $url ),
Loading history...
1709
				esc_html( $label )
0 ignored issues
show
Bug introduced by
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

1709
				/** @scrutinizer ignore-call */ 
1710
    esc_html( $label )
Loading history...
1710
			);
1711
		} else {
1712
			echo sprintf( '<p><a href="%1$s">%2$s</a></p>',
1713
				esc_url( $url ),
1714
				esc_html( $label )
1715
			);
1716
		}
1717
1718
		if ( '' !== $desc ) {
1719
			echo sprintf( '<p class="description">%1$s</p>',
1720
				esc_html( $desc )
1721
			);
1722
		}
1723
1724
	}
1725
1726
	/**
1727
	* Allow for a standard sanitize/validate method. We could use more specific ones if need be, but this one provides a baseline.
1728
	*
1729
	* @param string $option
1730
	* @return string $option
1731
	*/
1732
	public function sanitize_validate_text( $option ) {
1733
		if ( is_array( $option ) ) {
0 ignored issues
show
introduced by
The condition is_array($option) is always false.
Loading history...
1734
			$options = array();
1735
			foreach ( $option as $key => $value ) {
1736
				$options[ $key ] = sanitize_text_field( $value );
0 ignored issues
show
Bug introduced by
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

1736
				$options[ $key ] = /** @scrutinizer ignore-call */ sanitize_text_field( $value );
Loading history...
1737
			}
1738
			return $options;
1739
		}
1740
		$option = sanitize_text_field( $option );
1741
		return $option;
1742
	}
1743
1744
	/**
1745
	* Run a demo of Salesforce API call on the authenticate tab after WordPress has authenticated with it
1746
	*
1747
	* @param object $sfapi
1748
	*/
1749
	private function status( $sfapi ) {
1750
1751
		$versions = $sfapi->get_api_versions();
1752
1753
		// format this array into text so users can see the versions
1754
		if ( true === $versions['cached'] ) {
1755
			$versions_is_cached = esc_html__( 'This list is cached, and', 'object-sync-salesforce' );
0 ignored issues
show
Bug introduced by
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

1755
			$versions_is_cached = /** @scrutinizer ignore-call */ esc_html__( 'This list is cached, and', 'object-sync-salesforce' );
Loading history...
1756
		} else {
1757
			$versions_is_cached = esc_html__( 'This list is not cached, but', 'object-sync-salesforce' );
1758
		}
1759
1760
		if ( true === $versions['from_cache'] ) {
1761
			$versions_from_cache = esc_html__( 'items were loaded from the cache', 'object-sync-salesforce' );
1762
		} else {
1763
			$versions_from_cache = esc_html__( 'items were not loaded from the cache', 'object-sync-salesforce' );
1764
		}
1765
1766
		// translators: 1) $versions_is_cached is the "This list is/is not cached, and/but" line, 2) $versions_from_cache is the "items were/were not loaded from the cache" line
1767
		$versions_apicall_summary = sprintf( esc_html__( 'Available Salesforce API versions. %1$s %2$s. This is not an authenticated request, so it does not touch the Salesforce token.', 'object-sync-for-salesforce' ),
0 ignored issues
show
Unused Code introduced by
The assignment to $versions_apicall_summary is dead and can be removed.
Loading history...
1768
			$versions_is_cached,
1769
			$versions_from_cache
1770
		);
1771
1772
		$contacts = $sfapi->query( 'SELECT Name, Id from Contact LIMIT 100' );
1773
1774
		// format this array into html so users can see the contacts
1775
		if ( true === $contacts['cached'] ) {
1776
			$contacts_is_cached = esc_html__( 'They are cached, and', 'object-sync-salesforce' );
1777
		} else {
1778
			$contacts_is_cached = esc_html__( 'They are not cached, but', 'object-sync-salesforce' );
1779
		}
1780
1781
		if ( true === $contacts['from_cache'] ) {
1782
			$contacts_from_cache = esc_html__( 'they were loaded from the cache', 'object-sync-salesforce' );
1783
		} else {
1784
			$contacts_from_cache = esc_html__( 'they were not loaded from the cache', 'object-sync-salesforce' );
1785
		}
1786
1787
		if ( true === $contacts['is_redo'] ) {
1788
			$contacts_refreshed_token = esc_html__( 'This request did require refreshing the Salesforce token', 'object-sync-salesforce' );
1789
		} else {
1790
			$contacts_refreshed_token = esc_html__( 'This request did not require refreshing the Salesforce token', 'object-sync-salesforce' );
1791
		}
1792
1793
		// 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
1794
		$contacts_apicall_summary = sprintf( esc_html__( 'Salesforce successfully returned %1$s %2$s records. %3$s %4$s. %5$s.', 'object-sync-for-salesforce' ),
0 ignored issues
show
Unused Code introduced by
The assignment to $contacts_apicall_summary is dead and can be removed.
Loading history...
1795
			absint( $contacts['data']['totalSize'] ),
0 ignored issues
show
Bug introduced by
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

1795
			/** @scrutinizer ignore-call */ 
1796
   absint( $contacts['data']['totalSize'] ),
Loading history...
1796
			esc_html( $contacts['data']['records'][0]['attributes']['type'] ),
0 ignored issues
show
Bug introduced by
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

1796
			/** @scrutinizer ignore-call */ 
1797
   esc_html( $contacts['data']['records'][0]['attributes']['type'] ),
Loading history...
1797
			$contacts_is_cached,
1798
			$contacts_from_cache,
1799
			$contacts_refreshed_token
1800
		);
1801
1802
		require_once( plugin_dir_path( __FILE__ ) . '/../templates/admin/status.php' );
0 ignored issues
show
Bug introduced by
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

1802
		require_once( /** @scrutinizer ignore-call */ plugin_dir_path( __FILE__ ) . '/../templates/admin/status.php' );
Loading history...
1803
1804
	}
1805
1806
	/**
1807
	* Deauthorize WordPress from Salesforce.
1808
	* This deletes the tokens from the database; it does not currently do anything in Salesforce
1809
	* For this plugin at this time, that is the decision we are making: don't do any kind of authorization stuff inside Salesforce
1810
	*/
1811
	private function logout() {
1812
		$this->access_token  = delete_option( $this->option_prefix . 'access_token' );
0 ignored issues
show
Bug Best Practice introduced by
The property access_token does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug introduced by
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

1812
		$this->access_token  = /** @scrutinizer ignore-call */ delete_option( $this->option_prefix . 'access_token' );
Loading history...
1813
		$this->instance_url  = delete_option( $this->option_prefix . 'instance_url' );
0 ignored issues
show
Bug Best Practice introduced by
The property instance_url does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
1814
		$this->refresh_token = delete_option( $this->option_prefix . 'refresh_token' );
0 ignored issues
show
Bug Best Practice introduced by
The property refresh_token does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
1815
		echo sprintf( '<p>You have been logged out. You can use the <a href="%1$s">%2$s</a> tab to log in again.</p>',
1816
			esc_url( get_admin_url( null, 'options-general.php?page=object-sync-salesforce-admin&tab=authorize' ) ),
0 ignored issues
show
Bug introduced by
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

1816
			esc_url( /** @scrutinizer ignore-call */ get_admin_url( null, 'options-general.php?page=object-sync-salesforce-admin&tab=authorize' ) ),
Loading history...
Bug introduced by
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

1816
			/** @scrutinizer ignore-call */ 
1817
   esc_url( get_admin_url( null, 'options-general.php?page=object-sync-salesforce-admin&tab=authorize' ) ),
Loading history...
1817
			esc_html__( 'Authorize', 'object-sync-for-salesforce' )
0 ignored issues
show
Bug introduced by
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

1817
			/** @scrutinizer ignore-call */ 
1818
   esc_html__( 'Authorize', 'object-sync-for-salesforce' )
Loading history...
1818
		);
1819
	}
1820
1821
	/**
1822
	* Ajax call to clear the plugin cache.
1823
	*/
1824
	public function clear_sfwp_cache() {
1825
		$result   = $this->clear_cache( true );
1826
		$response = array(
1827
			'message' => $result['message'],
1828
			'success' => $result['success'],
1829
		);
1830
		wp_send_json_success( $response );
0 ignored issues
show
Bug introduced by
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

1830
		/** @scrutinizer ignore-call */ 
1831
  wp_send_json_success( $response );
Loading history...
1831
	}
1832
1833
	/**
1834
	* Clear the plugin's cache.
1835
	* This uses the flush method contained in the WordPress cache to clear all of this plugin's cached data.
1836
	*/
1837
	private function clear_cache( $ajax = false ) {
1838
		$result = (bool) $this->wordpress->sfwp_transients->flush();
1839
		if ( true === $result ) {
1840
			$message = __( 'The plugin cache has been cleared.', 'object-sync-for-salesforce' );
0 ignored issues
show
Bug introduced by
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

1840
			$message = /** @scrutinizer ignore-call */ __( 'The plugin cache has been cleared.', 'object-sync-for-salesforce' );
Loading history...
1841
		} else {
1842
			$message = __( 'There was an error clearing the plugin cache. Try refreshing this page.', 'object-sync-for-salesforce' );
1843
		}
1844
		if ( false === $ajax ) {
1845
			// translators: parameter 1 is the result message
1846
			echo sprintf( '<p>%1$s</p>', $message );
1847
		} else {
1848
			return array(
1849
				'message' => $message,
1850
				'success' => $result,
1851
			);
1852
		}
1853
	}
1854
1855
	/**
1856
	* Check WordPress Admin permissions
1857
	* Check if the current user is allowed to access the Salesforce plugin options
1858
	*/
1859
	private function check_wordpress_admin_permissions() {
1860
1861
		// one programmatic way to give this capability to additional user roles is the
1862
		// object_sync_for_salesforce_roles_configure_salesforce hook
1863
		// it runs on activation of this plugin, and will assign the below capability to any role
1864
		// coming from the hook
1865
1866
		// alternatively, other roles can get this capability in whatever other way you like
1867
		// point is: to administer this plugin, you need this capability
1868
1869
		if ( ! current_user_can( 'configure_salesforce' ) ) {
0 ignored issues
show
Bug introduced by
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

1869
		if ( ! /** @scrutinizer ignore-call */ current_user_can( 'configure_salesforce' ) ) {
Loading history...
1870
			return false;
1871
		} else {
1872
			return true;
1873
		}
1874
1875
	}
1876
1877
	/**
1878
	* Show what we know about this user's relationship to a Salesforce object, if any
1879
	* @param object $user
1880
	*
1881
	*/
1882
	public function show_salesforce_user_fields( $user ) {
1883
		$get_data = filter_input_array( INPUT_GET, FILTER_SANITIZE_STRING );
1884
		if ( true === $this->check_wordpress_admin_permissions() ) {
1885
			$mapping  = $this->mappings->load_by_wordpress( 'user', $user->ID );
1886
			$fieldmap = $this->mappings->get_fieldmaps(
1887
				null, // id field must be null for multiples
1888
				array(
1889
					'wordpress_object' => 'user',
1890
				)
1891
			);
1892
			if ( isset( $mapping['id'] ) && ! isset( $get_data['edit_salesforce_mapping'] ) ) {
1893
				require_once( plugin_dir_path( __FILE__ ) . '/../templates/admin/user-profile-salesforce.php' );
0 ignored issues
show
Bug introduced by
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

1893
				require_once( /** @scrutinizer ignore-call */ plugin_dir_path( __FILE__ ) . '/../templates/admin/user-profile-salesforce.php' );
Loading history...
1894
			} elseif ( ! empty( $fieldmap ) ) { // is the user mapped to something already?
1895
				if ( isset( $get_data['edit_salesforce_mapping'] ) && 'true' === sanitize_key( $get_data['edit_salesforce_mapping'] ) ) {
0 ignored issues
show
Bug introduced by
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

1895
				if ( isset( $get_data['edit_salesforce_mapping'] ) && 'true' === /** @scrutinizer ignore-call */ sanitize_key( $get_data['edit_salesforce_mapping'] ) ) {
Loading history...
1896
					require_once( plugin_dir_path( __FILE__ ) . '/../templates/admin/user-profile-salesforce-change.php' );
1897
				} else {
1898
					require_once( plugin_dir_path( __FILE__ ) . '/../templates/admin/user-profile-salesforce-map.php' );
1899
				}
1900
			}
1901
		}
1902
	}
1903
1904
	/**
1905
	* If the user profile has been mapped to Salesforce, do it
1906
	* @param int $user_id
1907
	*
1908
	*/
1909
	public function save_salesforce_user_fields( $user_id ) {
1910
		$post_data = filter_input_array( INPUT_POST, FILTER_SANITIZE_STRING );
1911
		if ( isset( $post_data['salesforce_update_mapped_user'] ) && '1' === rawurlencode( $post_data['salesforce_update_mapped_user'] ) ) {
1912
			$mapping_object                  = $this->mappings->get_object_maps(
1913
				array(
1914
					'wordpress_id'     => $user_id,
1915
					'wordpress_object' => 'user',
1916
				)
1917
			);
1918
			$mapping_object['salesforce_id'] = $post_data['salesforce_id'];
1919
1920
			$result = $this->mappings->update_object_map( $mapping_object, $mapping_object['id'] );
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
1921
		} elseif ( isset( $post_data['salesforce_create_mapped_user'] ) && '1' === rawurlencode( $post_data['salesforce_create_mapped_user'] ) ) {
1922
			// if a Salesforce ID was entered
1923
			if ( isset( $post_data['salesforce_id'] ) && ! empty( $post_data['salesforce_id'] ) ) {
1924
				$mapping_object = $this->create_object_map( $user_id, 'user', $post_data['salesforce_id'] );
0 ignored issues
show
Unused Code introduced by
The assignment to $mapping_object is dead and can be removed.
Loading history...
1925
			} elseif ( isset( $post_data['push_new_user_to_salesforce'] ) ) {
1926
				// otherwise, create a new record in Salesforce
1927
				$result = $this->push_to_salesforce( 'user', $user_id );
1928
			}
1929
		}
1930
	}
1931
1932
	/**
1933
	* Render tabs for settings pages in admin
1934
	* @param array $tabs
1935
	* @param string $tab
1936
	*/
1937
	private function tabs( $tabs, $tab = '' ) {
1938
1939
		$get_data        = filter_input_array( INPUT_GET, FILTER_SANITIZE_STRING );
1940
		$consumer_key    = $this->login_credentials['consumer_key'];
1941
		$consumer_secret = $this->login_credentials['consumer_secret'];
1942
		$callback_url    = $this->login_credentials['callback_url'];
0 ignored issues
show
Unused Code introduced by
The assignment to $callback_url is dead and can be removed.
Loading history...
1943
1944
		$current_tab = $tab;
1945
		echo '<h2 class="nav-tab-wrapper">';
1946
		foreach ( $tabs as $tab_key => $tab_caption ) {
1947
			$active = $current_tab === $tab_key ? ' nav-tab-active' : '';
1948
			if ( 'settings' === $tab_key || ( isset( $consumer_key ) && isset( $consumer_secret ) && ! empty( $consumer_key ) && ! empty( $consumer_secret ) ) ) {
1949
				echo sprintf( '<a class="nav-tab%1$s" href="%2$s">%3$s</a>',
1950
					esc_attr( $active ),
0 ignored issues
show
Bug introduced by
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

1950
					/** @scrutinizer ignore-call */ 
1951
     esc_attr( $active ),
Loading history...
1951
					esc_url( '?page=object-sync-salesforce-admin&tab=' . $tab_key ),
0 ignored issues
show
Bug introduced by
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

1951
					/** @scrutinizer ignore-call */ 
1952
     esc_url( '?page=object-sync-salesforce-admin&tab=' . $tab_key ),
Loading history...
1952
					esc_html( $tab_caption )
0 ignored issues
show
Bug introduced by
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

1952
					/** @scrutinizer ignore-call */ 
1953
     esc_html( $tab_caption )
Loading history...
1953
				);
1954
			}
1955
		}
1956
		echo '</h2>';
1957
1958
		if ( isset( $get_data['tab'] ) ) {
1959
			$tab = sanitize_key( $get_data['tab'] );
0 ignored issues
show
Bug introduced by
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

1959
			$tab = /** @scrutinizer ignore-call */ sanitize_key( $get_data['tab'] );
Loading history...
Unused Code introduced by
The assignment to $tab is dead and can be removed.
Loading history...
1960
		} else {
1961
			$tab = '';
1962
		}
1963
	}
1964
1965
	/**
1966
	* Clear schedule
1967
	* This clears the schedule if the user clicks the button
1968
	* @param string $schedule_name
1969
	*/
1970
	private function clear_schedule( $schedule_name = '' ) {
1971
		if ( '' !== $schedule_name ) {
1972
			$this->queue->cancel( $schedule_name );
1973
			// translators: $schedule_name is the name of the current queue. Defaults: salesforce_pull, salesforce_push, salesforce
1974
			echo sprintf( esc_html__( 'You have cleared the %s schedule.', 'object-sync-for-salesforce' ), esc_html( $schedule_name ) );
0 ignored issues
show
Bug introduced by
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

1974
			echo sprintf( /** @scrutinizer ignore-call */ esc_html__( 'You have cleared the %s schedule.', 'object-sync-for-salesforce' ), esc_html( $schedule_name ) );
Loading history...
Bug introduced by
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

1974
			echo sprintf( esc_html__( 'You have cleared the %s schedule.', 'object-sync-for-salesforce' ), /** @scrutinizer ignore-call */ esc_html( $schedule_name ) );
Loading history...
1975
		} else {
1976
			echo esc_html__( 'You need to specify the name of the schedule you want to clear.', 'object-sync-for-salesforce' );
1977
		}
1978
	}
1979
1980
	/**
1981
	* Get count of schedule items
1982
	* @param string $schedule_name
1983
	* @return int $count
1984
	*/
1985
	private function get_schedule_count( $schedule_name = '' ) {
1986
		if ( '' !== $schedule_name ) {
1987
			$args  = array(
1988
				'group'  => $schedule_name,
1989
				'status' => ActionScheduler_Store::STATUS_PENDING,
1990
			);
1991
			$count = count( $this->queue->search( $args, 'ARRAY_A' ) );
1992
			return $count;
1993
		} else {
1994
			return 'unknown';
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'unknown' returns the type string which is incompatible with the documented return type integer.
Loading history...
1995
		}
1996
	}
1997
1998
	/**
1999
	* Create an object map between a WordPress object and a Salesforce object
2000
	*
2001
	* @param int $wordpress_id
2002
	*   Unique identifier for the WordPress object
2003
	* @param string $wordpress_object
2004
	*   What kind of object is it?
2005
	* @param string $salesforce_id
2006
	*   Unique identifier for the Salesforce object
2007
	* @param string $action
2008
	*   Did we push or pull?
2009
	*
2010
	* @return int $wpdb->insert_id
2011
	*   This is the database row for the map object
2012
	*
2013
	*/
2014
	private function create_object_map( $wordpress_id, $wordpress_object, $salesforce_id, $action = '' ) {
2015
		// Create object map and save it
2016
		$mapping_object = $this->mappings->create_object_map(
2017
			array(
2018
				'wordpress_id'      => $wordpress_id, // wordpress unique id
2019
				'salesforce_id'     => $salesforce_id, // salesforce unique id. we don't care what kind of object it is at this point
2020
				'wordpress_object'  => $wordpress_object, // keep track of what kind of wp object this is
2021
				'last_sync'         => current_time( 'mysql' ),
0 ignored issues
show
Bug introduced by
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

2021
				'last_sync'         => /** @scrutinizer ignore-call */ current_time( 'mysql' ),
Loading history...
2022
				'last_sync_action'  => $action,
2023
				'last_sync_status'  => $this->mappings->status_success,
2024
				'last_sync_message' => __( 'Mapping object updated via function: ', 'object-sync-for-salesforce' ) . __FUNCTION__,
0 ignored issues
show
Bug introduced by
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

2024
				'last_sync_message' => /** @scrutinizer ignore-call */ __( 'Mapping object updated via function: ', 'object-sync-for-salesforce' ) . __FUNCTION__,
Loading history...
2025
			)
2026
		);
2027
2028
		return $mapping_object;
2029
2030
	}
2031
2032
}
2033