Object_Sync_Sf_Mapping::update_object_map()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
c 0
b 0
f 0
nc 8
nop 2
dl 0
loc 19
rs 9.7998
1
<?php
2
/**
3
 * Map objects and records between WordPress and Salesforce
4
 *
5
 * @class   Object_Sync_Sf_Mapping
6
 * @package Object_Sync_Salesforce
7
 */
8
9
defined( 'ABSPATH' ) || exit;
10
11
/**
12
 * Object_Sync_Sf_Mapping class.
13
 */
14
class Object_Sync_Sf_Mapping {
15
16
	/**
17
	 * Current version of the plugin
18
	 *
19
	 * @var string
20
	 */
21
	public $version;
22
23
	/**
24
	 * The main plugin file
25
	 *
26
	 * @var string
27
	 */
28
	public $file;
29
30
	/**
31
	 * Global object of `$wpdb`, the WordPress database
32
	 *
33
	 * @var object
34
	 */
35
	public $wpdb;
36
37
	/**
38
	 * The plugin's slug so we can include it when necessary
39
	 *
40
	 * @var string
41
	 */
42
	public $slug;
43
44
	/**
45
	 * The plugin's prefix when saving options to the database
46
	 *
47
	 * @var string
48
	 */
49
	public $option_prefix;
50
51
	/**
52
	 * Object_Sync_Sf_Logging class
53
	 *
54
	 * @var object
55
	 */
56
	public $logging;
57
58
	/**
59
	 * The database table for fieldmaps
60
	 *
61
	 * @var string
62
	 */
63
	public $fieldmap_table;
64
65
	/**
66
	 * The database table for object maps
67
	 *
68
	 * @var string
69
	 */
70
	public $object_map_table;
71
72
	/**
73
	 * Possible status values for fieldmaps
74
	 *
75
	 * @var array
76
	 */
77
	public $fieldmap_statuses;
78
79
	/**
80
	 * The active status values for fieldmaps
81
	 *
82
	 * @var string
83
	 */
84
	public $active_fieldmap_conditions;
85
86
	/**
87
	 * Bitmap value for when sync is off
88
	 *
89
	 * @var string
90
	 */
91
	public $sync_off;
92
93
	/**
94
	 * Bitmap value for when sync is is on for WordPress create events
95
	 *
96
	 * @var string
97
	 */
98
	public $sync_wordpress_create;
99
100
	/**
101
	 * Bitmap value for when sync is is on for WordPress update events
102
	 *
103
	 * @var string
104
	 */
105
	public $sync_wordpress_update;
106
107
	/**
108
	 * Bitmap value for when sync is is on for WordPress delete events
109
	 *
110
	 * @var string
111
	 */
112
	public $sync_wordpress_delete;
113
114
	/**
115
	 * Bitmap value for when sync is is on for Salesforce create events
116
	 *
117
	 * @var string
118
	 */
119
	public $sync_sf_create;
120
121
	/**
122
	 * Bitmap value for when sync is is on for Salesforce update events
123
	 *
124
	 * @var string
125
	 */
126
	public $sync_sf_update;
127
128
	/**
129
	 * Bitmap value for when sync is is on for Salesforce delete events
130
	 *
131
	 * @var string
132
	 */
133
	public $sync_sf_delete;
134
135
	/**
136
	 * Which events are run by WordPress
137
	 *
138
	 * @var array
139
	 */
140
	public $wordpress_events;
141
142
	/**
143
	 * Which events are run by Salesforce
144
	 *
145
	 * @var array
146
	 */
147
	public $salesforce_events;
148
149
	/**
150
	 * The direction from WordPress to Salesforce
151
	 *
152
	 * @var string
153
	 */
154
	public $direction_wordpress_sf;
155
156
	/**
157
	 * The direction from Salesforce to WordPress
158
	 *
159
	 * @var string
160
	 */
161
	public $direction_sf_wordpress;
162
163
	/**
164
	 * The direction to sync both ways
165
	 *
166
	 * @var string
167
	 */
168
	public $direction_sync;
169
170
	/**
171
	 * WordPress directions, including sync
172
	 *
173
	 * @var array
174
	 */
175
	public $direction_wordpress;
176
177
	/**
178
	 * Salesforce directions, including sync
179
	 *
180
	 * @var array
181
	 */
182
	public $direction_salesforce;
183
184
	/**
185
	 * Default record type when using a Salesforce object that has a default or Master record type
186
	 *
187
	 * @var string
188
	 */
189
	public $salesforce_default_record_type;
190
191
	/**
192
	 * Delimiter for arrays coming from Salesforce
193
	 *
194
	 * @var string
195
	 */
196
	public $array_delimiter;
197
198
	/**
199
	 * Data in Salesforce that is stored as an array
200
	 *
201
	 * @var array
202
	 */
203
	public $array_types_from_salesforce;
204
205
	/**
206
	 * Data in Salesforce that is stored as a date
207
	 *
208
	 * @var array
209
	 */
210
	public $date_types_from_salesforce;
211
212
	/**
213
	 * Data in Salesforce that is stored as an integer
214
	 *
215
	 * @var array
216
	 */
217
	public $int_types_from_salesforce;
218
219
	/**
220
	 * How long can a mapping field be
221
	 *
222
	 * @var int
223
	 */
224
	public $name_length;
225
226
	/**
227
	 * Status flag for success
228
	 *
229
	 * @var int
230
	 */
231
	public $status_success;
232
233
	/**
234
	 * Status flag for error
235
	 *
236
	 * @var int
237
	 */
238
	public $status_error;
239
240
	/**
241
	 * Option value for whether the plugin is in debug mode
242
	 *
243
	 * @var bool
244
	 */
245
	public $debug;
246
247
	/**
248
	 * Deprecated hex parameter which defines when syncing should occur on each field map.
249
	 *
250
	 * @var int
251
	 */
252
	public $sync_off_v1;
253
254
	/**
255
	 * Deprecated hex parameter which defines when syncing should occur on each field map.
256
	 *
257
	 * @var int
258
	 */
259
	public $sync_wordpress_create_v1;
260
261
	/**
262
	 * Deprecated hex parameter which defines when syncing should occur on each field map.
263
	 *
264
	 * @var int
265
	 */
266
	public $sync_wordpress_update_v1;
267
268
	/**
269
	 * Deprecated hex parameter which defines when syncing should occur on each field map.
270
	 *
271
	 * @var int
272
	 */
273
	public $sync_wordpress_delete_v1;
274
275
	/**
276
	 * Deprecated hex parameter which defines when syncing should occur on each field map.
277
	 *
278
	 * @var int
279
	 */
280
	public $sync_sf_create_v1;
281
282
	/**
283
	 * Deprecated hex parameter which defines when syncing should occur on each field map.
284
	 *
285
	 * @var int
286
	 */
287
	public $sync_sf_update_v1;
288
289
	/**
290
	 * Deprecated hex parameter which defines when syncing should occur on each field map.
291
	 *
292
	 * @var int
293
	 */
294
	public $sync_sf_delete_v1;
295
296
	/**
297
	 * Deprecated hex event values.
298
	 *
299
	 * @var array
300
	 */
301
	public $wordpress_events_v1;
302
303
	/**
304
	 * Deprecated hex event values.
305
	 *
306
	 * @var array
307
	 */
308
	public $salesforce_events_v1;
309
310
	/**
311
	 * Constructor for mapping class
312
	 */
313
	public function __construct() {
314
		$this->version       = object_sync_for_salesforce()->version;
315
		$this->file          = object_sync_for_salesforce()->file;
316
		$this->wpdb          = object_sync_for_salesforce()->wpdb;
317
		$this->slug          = object_sync_for_salesforce()->slug;
318
		$this->option_prefix = object_sync_for_salesforce()->option_prefix;
319
320
		$this->logging = object_sync_for_salesforce()->logging;
321
322
		$this->fieldmap_table   = $this->wpdb->prefix . 'object_sync_sf_field_map';
323
		$this->object_map_table = $this->wpdb->prefix . 'object_sync_sf_object_map';
324
325
		$this->fieldmap_statuses          = array(
326
			'active'   => esc_html__( 'Active', '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

326
			'active'   => /** @scrutinizer ignore-call */ esc_html__( 'Active', 'object-sync-for-salesforce' ),
Loading history...
327
			'inactive' => esc_html__( 'Inactive', 'object-sync-for-salesforce' ),
328
			'any'      => '',
329
		);
330
		$this->active_fieldmap_conditions = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('fieldmap_status' => 'active') of type array<string,string> is incompatible with the declared type string of property $active_fieldmap_conditions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
331
			'fieldmap_status' => 'active',
332
		);
333
334
		/*
335
		 * These parameters are how we define when syncing should occur on each field map.
336
		 * They get used in the admin settings, as well as the push/pull methods to see if something should happen.
337
		*/
338
		$this->sync_off              = 'off';
339
		$this->sync_wordpress_create = 'wp_create';
340
		$this->sync_wordpress_update = 'wp_update';
341
		$this->sync_wordpress_delete = 'wp_delete';
342
		$this->sync_sf_create        = 'sf_create';
343
		$this->sync_sf_update        = 'sf_update';
344
		$this->sync_sf_delete        = 'sf_delete';
345
346
		// deprecated bit flags from version 1.x. Deprecated since 2.0.0.
347
		$this->sync_off_v1              = 0x0000;
348
		$this->sync_wordpress_create_v1 = 0x0001;
349
		$this->sync_wordpress_update_v1 = 0x0002;
350
		$this->sync_wordpress_delete_v1 = 0x0004;
351
		$this->sync_sf_create_v1        = 0x0008;
352
		$this->sync_sf_update_v1        = 0x0010;
353
		$this->sync_sf_delete_v1        = 0x0020;
354
355
		// Define which events are initialized by which system.
356
		$this->wordpress_events  = array( $this->sync_wordpress_create, $this->sync_wordpress_update, $this->sync_wordpress_delete );
357
		$this->salesforce_events = array( $this->sync_sf_create, $this->sync_sf_update, $this->sync_sf_delete );
358
359
		// deprecated bit flags from version 1.x. Deprecated since 2.0.0.
360
		$this->wordpress_events_v1  = array( $this->sync_wordpress_create_v1, $this->sync_wordpress_update_v1, $this->sync_wordpress_delete_v1 );
361
		$this->salesforce_events_v1 = array( $this->sync_sf_create_v1, $this->sync_sf_update_v1, $this->sync_sf_delete_v1 );
362
363
		// Constants for the directions to map things.
364
		$this->direction_wordpress_sf = 'wp_sf';
365
		$this->direction_sf_wordpress = 'sf_wp';
366
		$this->direction_sync         = 'sync';
367
368
		$this->direction_wordpress  = array( $this->direction_wordpress_sf, $this->direction_sync );
369
		$this->direction_salesforce = array( $this->direction_sf_wordpress, $this->direction_sync );
370
371
		// This is used when we map a record with default or Master.
372
		$this->salesforce_default_record_type = 'default';
373
374
		// Salesforce has multipicklists and they have a delimiter.
375
		$this->array_delimiter = ';';
376
		// What data types in Salesforce should be an array?
377
		$this->array_types_from_salesforce = array( 'multipicklist' );
378
		// What data types in Salesforce should be a date field?
379
		$this->date_types_from_salesforce = array( 'date', 'datetime' );
380
		// What data types in Salesforce should be an integer?
381
		$this->int_types_from_salesforce = array( 'integer', 'boolean' );
382
383
		// Max length for a mapping field.
384
		$this->name_length = 128;
385
386
		// Statuses for object sync.
387
		$this->status_success = 1;
388
		$this->status_error   = 0;
389
390
		// use the option value for whether we're in debug mode.
391
		$this->debug = filter_var( get_option( $this->option_prefix . 'debug_mode', false ), FILTER_VALIDATE_BOOLEAN );
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

391
		$this->debug = filter_var( /** @scrutinizer ignore-call */ get_option( $this->option_prefix . 'debug_mode', false ), FILTER_VALIDATE_BOOLEAN );
Loading history...
392
393
	}
394
395
	/**
396
	 * Create a fieldmap row between a WordPress and Salesforce object
397
	 *
398
	 * @param array $posted The results of $_POST.
399
	 * @param array $wordpress_fields The fields for the WordPress side of the mapping.
400
	 * @param array $salesforce_fields The fields for the Salesforce side of the mapping.
401
	 * @return int  the last inserted ID.
402
	 */
403
	public function create_fieldmap( $posted = array(), $wordpress_fields = array(), $salesforce_fields = array() ) {
404
		$data = $this->setup_fieldmap_data( $posted, $wordpress_fields, $salesforce_fields );
405
		if ( ! isset( $posted['version'] ) && version_compare( $this->version, '1.2.5', '>=' ) ) {
406
			$data['version'] = $this->version;
407
		}
408
		$insert = $this->wpdb->insert( $this->fieldmap_table, $data );
409
		if ( 1 === $insert ) {
410
			return $this->wpdb->insert_id;
411
		} else {
412
			return false;
413
		}
414
	}
415
416
	/**
417
	 * Get one or more fieldmap rows between a WordPress and Salesforce object
418
	 *
419
	 * @param int   $id The ID of a desired mapping.
420
	 * @param array $conditions Array of key=>value to match the mapping by.
421
	 * @param bool  $reset Unused parameter.
422
	 * @return array $map a single mapping or $mappings, an array of mappings.
423
	 */
424
	public function get_fieldmaps( $id = null, $conditions = array(), $reset = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $reset is not used and could be removed. ( Ignorable by Annotation )

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

424
	public function get_fieldmaps( $id = null, $conditions = array(), /** @scrutinizer ignore-unused */ $reset = false ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
425
		$table = $this->fieldmap_table;
426
		if ( null !== $id ) { // get one fieldmap.
427
			$map        = $this->wpdb->get_row( 'SELECT * FROM ' . $table . ' WHERE id = ' . $id, 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...
428
			$mappings   = array();
429
			$mappings[] = $map;
430
			$mappings   = $this->prepare_fieldmap_data( $mappings );
431
			$map        = $mappings[0];
432
			return $map;
433
		} elseif ( ! empty( $conditions ) ) { // get multiple but with a limitation.
434
			$mappings    = array();
435
			$record_type = '';
436
437
			// Assemble the SQL.
438
			if ( ! empty( $conditions ) ) {
439
				$where = '';
440
				$i     = 0;
441
				foreach ( $conditions as $key => $value ) {
442
					// if 'any' is the value for fieldmap status, we keep it off the WHERE statement.
443
					if ( 'fieldmap_status' === $key && 'any' === $value ) {
444
						continue;
445
					}
446
					if ( 'salesforce_record_type' === $key ) {
447
						$record_type = 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

447
						$record_type = /** @scrutinizer ignore-call */ sanitize_text_field( $value );
Loading history...
448
					} else {
449
						$i++;
450
						if ( $i > 1 ) {
451
							$where .= ' AND ';
452
						}
453
						$where .= '`' . $key . '` = "' . $value . '"';
454
					}
455
				}
456
				if ( '' !== $where ) {
0 ignored issues
show
introduced by
The condition '' !== $where is always false.
Loading history...
457
					$where = ' WHERE ' . $where;
458
				}
459
			} else {
460
				$where = '';
461
			}
462
463
			$mappings = $this->wpdb->get_results( 'SELECT * FROM ' . $table . $where . ' ORDER BY `fieldmap_status`, `weight`', ARRAY_A );
464
465
			if ( ! empty( $mappings ) ) {
466
				$mappings = $this->prepare_fieldmap_data( $mappings, $record_type );
467
			}
468
469
			return $mappings;
470
471
		} else { // get all of the mappings. ALL THE MAPPINGS.
472
473
			// This is the default query for loading fieldmaps.
474
			$mappings = $this->wpdb->get_results( "SELECT * FROM $table ORDER BY `fieldmap_status`", ARRAY_A );
475
476
			// lower than 2.0.0 versions.
477
			// @deprecated
478
			// will be removed in version 3.0.0.
479
			if ( version_compare( $this->version, '2.0.0', '<' ) ) {
480
				// if the version is greater than or equal to 1.5.0, the fieldmap table has a pull_to_drafts column.
481
				if ( version_compare( $this->version, '1.5.0', '>=' ) ) {
482
					$mappings = $this->wpdb->get_results( "SELECT `id`, `label`, `wordpress_object`, `salesforce_object`, `salesforce_record_types_allowed`, `salesforce_record_type_default`, `fields`, `pull_trigger_field`, `sync_triggers`, `push_async`, `push_drafts`, `pull_to_drafts`, `weight`, `version` FROM $table", ARRAY_A );
483
				} elseif ( version_compare( $this->version, '1.2.5', '>=' ) ) {
484
					// if the version is greater than or equal to 1.2.5, the fieldmap table has a version column.
485
					$mappings = $this->wpdb->get_results( "SELECT `id`, `label`, `wordpress_object`, `salesforce_object`, `salesforce_record_types_allowed`, `salesforce_record_type_default`, `fields`, `pull_trigger_field`, `sync_triggers`, `push_async`, `push_drafts`, `weight`, `version` FROM $table", ARRAY_A );
486
				} else {
487
					$mappings = $this->wpdb->get_results( "SELECT `id`, `label`, `wordpress_object`, `salesforce_object`, `salesforce_record_types_allowed`, `salesforce_record_type_default`, `fields`, `pull_trigger_field`, `sync_triggers`, `push_async`, `push_drafts`, `weight` FROM $table", ARRAY_A );
488
				}
489
			}
490
491
			if ( ! empty( $mappings ) ) {
492
				$mappings = $this->prepare_fieldmap_data( $mappings );
493
			}
494
495
			return $mappings;
496
		} // End if statement.
497
	}
498
499
	/**
500
	 * For a mapping, get the fieldmaps associated with it.
501
	 *
502
	 * @param array $mapping The mapping for which we are getting the fieldmaps.
503
	 * @param array $directions The direction of the mapping: from WP to SF or vice-versa.
504
	 * @see Object_Sync_Sf_Salesforce_Pull::get_pull_query()
505
	 * @return array of mapped fields
506
	 */
507
	public function get_mapped_fields( $mapping, $directions = array() ) {
508
		$mapped_fields = array();
509
		foreach ( $mapping['fields'] as $fields ) {
510
			if ( empty( $directions ) || in_array( $fields['direction'], $directions, true ) ) {
511
512
				// in version 1.2.0, we provided an option for API name vs label for Salesforce fields.
513
				if ( version_compare( $this->version, '1.2.0', '>=' ) && isset( $fields['salesforce_field']['name'] ) ) {
514
					$array_key = 'name';
515
				} else {
516
					$array_key = 'label';
517
				}
518
519
				// Some field map types (Relation) store a collection of SF objects.
520
				if ( is_array( $fields['salesforce_field'] ) && ! isset( $fields['salesforce_field'][ $array_key ] ) ) {
521
					foreach ( $fields['salesforce_field'] as $sf_field ) {
522
						$mapped_fields[ $sf_field[ $array_key ] ] = $sf_field[ $array_key ];
523
					}
524
				} else { // The rest are just a name/value pair.
525
					$mapped_fields[ $fields['salesforce_field'][ $array_key ] ] = $fields['salesforce_field'][ $array_key ];
526
				}
527
			}
528
		}
529
530
		if ( ! empty( $this->get_mapped_record_types ) ) {
531
			$mapped_fields['RecordTypeId'] = 'RecordTypeId';
532
		}
533
534
		return $mapped_fields;
535
	}
536
537
	/**
538
	 * Get the mapped record types for a given mapping.
539
	 *
540
	 * @param array $mapping A mapping from which we wish to estract the record type.
541
	 * @return array of mappings. Empty if the mapping's record type is default, else full of the record types.
542
	 */
543
	public function get_mapped_record_types( $mapping ) {
544
		return $mapping['salesforce_record_type_default'] === $this->salesforce_default_record_type ? array() : array_filter( maybe_unserialize( $mapping['salesforce_record_types_allowed'] ) );
0 ignored issues
show
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

544
		return $mapping['salesforce_record_type_default'] === $this->salesforce_default_record_type ? array() : array_filter( /** @scrutinizer ignore-call */ maybe_unserialize( $mapping['salesforce_record_types_allowed'] ) );
Loading history...
545
	}
546
547
	/**
548
	 * Update a fieldmap row between a WordPress and Salesforce object
549
	 *
550
	 * @param array $posted It's $_POST.
551
	 * @param array $wordpress_fields The fields for the WordPress side of the mapping.
552
	 * @param array $salesforce_fields The fields for the Salesforce side of the mapping.
553
	 * @param int   $id The ID of the mapping.
554
	 * @return boolean whether it was updated
555
	 */
556
	public function update_fieldmap( $posted = array(), $wordpress_fields = array(), $salesforce_fields = array(), $id = '' ) {
557
		$data = $this->setup_fieldmap_data( $posted, $wordpress_fields, $salesforce_fields );
558
		if ( version_compare( $this->version, '1.2.5', '>=' ) && ! isset( $data['updated'] ) ) {
559
			$data['version'] = $this->version;
560
		}
561
		$update = $this->wpdb->update(
562
			$this->fieldmap_table,
563
			$data,
564
			array(
565
				'id' => $id,
566
			)
567
		);
568
		if ( false === $update ) {
569
			return false;
570
		} else {
571
			return true;
572
		}
573
	}
574
575
	/**
576
	 * Setup fieldmap data
577
	 * Sets up the database entry for mapping the object types between Salesforce and WordPress
578
	 *
579
	 * @param array $posted It's $_POST.
580
	 * @param array $wordpress_fields The fields for the WordPress side of the mapping.
581
	 * @param array $salesforce_fields The fields for the Salesforce side of the mapping.
582
	 * @return array $data the fieldmap's data for the database
583
	 */
584
	private function setup_fieldmap_data( $posted = array(), $wordpress_fields = array(), $salesforce_fields = array() ) {
585
		$data = array(
586
			'label'             => $posted['label'],
587
			'name'              => sanitize_title( $posted['label'] ),
0 ignored issues
show
Bug introduced by
The function sanitize_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

587
			'name'              => /** @scrutinizer ignore-call */ sanitize_title( $posted['label'] ),
Loading history...
588
			'salesforce_object' => $posted['salesforce_object'],
589
			'wordpress_object'  => $posted['wordpress_object'],
590
		);
591
		// when importing a fieldmap, there might already be a saved version. if there is, keep it.
592
		$data['version'] = isset( $posted['version'] ) ? $posted['version'] : $this->version;
593
		if ( isset( $posted['wordpress_field'] ) && is_array( $posted['wordpress_field'] ) && isset( $posted['salesforce_field'] ) && is_array( $posted['salesforce_field'] ) ) {
594
			$setup['fields'] = array();
595
			foreach ( $posted['wordpress_field'] as $key => $value ) {
596
				$method_key = array_search( $value, array_column( $wordpress_fields, 'key' ), true );
597
				if ( ! isset( $posted['direction'][ $key ] ) ) {
598
					$posted['direction'][ $key ] = 'sync';
599
				}
600
				if ( ! isset( $posted['is_prematch'][ $key ] ) ) {
601
					$posted['is_prematch'][ $key ] = false;
602
				}
603
				if ( ! isset( $posted['is_key'][ $key ] ) ) {
604
					$posted['is_key'][ $key ] = false;
605
				}
606
				if ( ! isset( $posted['is_delete'][ $key ] ) ) {
607
					$posted['is_delete'][ $key ] = false;
608
				}
609
				if ( false === $posted['is_delete'][ $key ] ) {
610
					// I think it's good to over-mention that updateable is really how the Salesforce api spells it.
611
					$updateable_key = array_search( $posted['salesforce_field'][ $key ], array_column( $salesforce_fields, 'name' ), true );
612
613
					$salesforce_field_attributes = array();
614
					foreach ( $salesforce_fields[ $updateable_key ] as $sf_key => $sf_value ) {
615
						if ( isset( $sf_value ) && ! is_array( $sf_value ) ) {
616
							$salesforce_field_attributes[ $sf_key ] = esc_attr( $sf_value );
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

616
							$salesforce_field_attributes[ $sf_key ] = /** @scrutinizer ignore-call */ esc_attr( $sf_value );
Loading history...
617
						} elseif ( ! empty( $sf_value ) && is_array( $sf_value ) ) {
618
							$salesforce_field_attributes[ $sf_key ] = maybe_unserialize( $sf_value );
0 ignored issues
show
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

618
							$salesforce_field_attributes[ $sf_key ] = /** @scrutinizer ignore-call */ maybe_unserialize( $sf_value );
Loading history...
619
						} else {
620
							$salesforce_field_attributes[ $sf_key ] = '';
621
						}
622
					}
623
624
					$setup['fields'][ $key ] = array(
625
						'wordpress_field'  => array(
626
							'label'    => sanitize_text_field( $posted['wordpress_field'][ $key ] ),
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

626
							'label'    => /** @scrutinizer ignore-call */ sanitize_text_field( $posted['wordpress_field'][ $key ] ),
Loading history...
627
							'methods'  => maybe_unserialize( $wordpress_fields[ $method_key ]['methods'] ),
628
							'type'     => isset( $wordpress_fields[ $method_key ]['type'] ) ? sanitize_text_field( $wordpress_fields[ $method_key ]['type'] ) : 'text',
629
							'editable' => isset( $wordpress_fields[ $method_key ]['editable'] ) ? (bool) $wordpress_fields[ $method_key ]['editable'] : true,
630
						),
631
						'salesforce_field' => $salesforce_field_attributes,
632
						'is_prematch'      => sanitize_text_field( $posted['is_prematch'][ $key ] ),
633
						'is_key'           => sanitize_text_field( $posted['is_key'][ $key ] ),
634
						'direction'        => sanitize_text_field( $posted['direction'][ $key ] ),
635
						'is_delete'        => sanitize_text_field( $posted['is_delete'][ $key ] ),
636
					);
637
638
					// If the WordPress key or the Salesforce key are blank, remove this incomplete mapping.
639
					// This prevents https://github.com/MinnPost/object-sync-for-salesforce/issues/82 .
640
					if (
641
						empty( $setup['fields'][ $key ]['wordpress_field']['label'] )
642
						||
643
						empty( $setup['fields'][ $key ]['salesforce_field']['name'] )
644
					) {
645
						unset( $setup['fields'][ $key ] );
646
					}
647
				}
648
			} // End foreach() on WordPress fields.
649
			$data['fields'] = maybe_serialize( $setup['fields'] );
0 ignored issues
show
Bug introduced by
The function maybe_serialize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

649
			$data['fields'] = /** @scrutinizer ignore-call */ maybe_serialize( $setup['fields'] );
Loading history...
650
		} elseif ( isset( $posted['fields'] ) && is_array( $posted['fields'] ) ) {
651
			// if $posted['fields'] is already set, use that.
652
			$data['fields'] = maybe_serialize( $posted['fields'] );
653
		} // End if() WordPress fields are present.
654
655
		if ( isset( $posted['salesforce_record_types_allowed'] ) ) {
656
			$data['salesforce_record_types_allowed'] = maybe_serialize( $posted['salesforce_record_types_allowed'] );
657
		} else {
658
			$data['salesforce_record_types_allowed'] = maybe_serialize(
659
				array(
660
					$this->salesforce_default_record_type => $this->salesforce_default_record_type,
661
				)
662
			);
663
		}
664
		if ( isset( $posted['salesforce_record_type_default'] ) ) {
665
			$data['salesforce_record_type_default'] = $posted['salesforce_record_type_default'];
666
		} else {
667
			$data['salesforce_record_type_default'] = maybe_serialize( $this->salesforce_default_record_type );
668
		}
669
		if ( isset( $posted['pull_trigger_field'] ) ) {
670
			$data['pull_trigger_field'] = $posted['pull_trigger_field'];
671
		}
672
		if ( isset( $posted['sync_triggers'] ) && is_array( $posted['sync_triggers'] ) ) {
673
			$setup['sync_triggers'] = array();
674
			foreach ( $posted['sync_triggers'] as $key => $value ) {
675
				$setup['sync_triggers'][ $key ] = esc_html( $posted['sync_triggers'][ $key ] );
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

675
				$setup['sync_triggers'][ $key ] = /** @scrutinizer ignore-call */ esc_html( $posted['sync_triggers'][ $key ] );
Loading history...
676
			}
677
			// format the sync triggers. if necessary, update the database.
678
			$sync_triggers          = $this->maybe_upgrade_sync_triggers( $setup['sync_triggers'], $data['version'] );
679
			$setup['sync_triggers'] = $sync_triggers;
680
		} else {
681
			$setup['sync_triggers'] = array();
682
		}
683
		$data['sync_triggers'] = maybe_serialize( $setup['sync_triggers'] );
684
		if ( isset( $posted['pull_trigger_field'] ) ) {
685
			$data['pull_trigger_field'] = $posted['pull_trigger_field'];
686
		}
687
		$data['fieldmap_status']                     = isset( $posted['fieldmap_status'] ) ? $posted['fieldmap_status'] : 'active';
688
		$data['push_async']                          = isset( $posted['push_async'] ) ? $posted['push_async'] : '';
689
		$data['push_drafts']                         = isset( $posted['push_drafts'] ) ? $posted['push_drafts'] : '';
690
		$data['pull_to_drafts']                      = isset( $posted['pull_to_drafts'] ) ? $posted['pull_to_drafts'] : '';
691
		$data['weight']                              = isset( $posted['weight'] ) ? $posted['weight'] : '';
692
		$data['always_delete_object_maps_on_delete'] = isset( $posted['always_delete_object_maps_on_delete'] ) ? $posted['always_delete_object_maps_on_delete'] : '0';
693
		return $data;
694
	}
695
696
	/**
697
	 * Delete a fieldmap row between a WordPress and Salesforce object
698
	 *
699
	 * @param int $id The ID of a field mapping.
700
	 * @return bool whether it was deleted
701
	 */
702
	public function delete_fieldmap( $id = '' ) {
703
		$data   = array(
704
			'id' => $id,
705
		);
706
		$delete = $this->wpdb->delete( $this->fieldmap_table, $data );
707
		if ( 1 === $delete ) {
708
			return true;
709
		} else {
710
			return false;
711
		}
712
	}
713
714
	/**
715
	 * Create an object map row between a WordPress and Salesforce object
716
	 *
717
	 * @param array $posted It's $_POST.
718
	 * @return false|int of field mapping between WordPress and Salesforce objects
719
	 */
720
	public function create_object_map( $posted = array() ) {
721
		$data            = $this->setup_object_map_data( $posted );
722
		$data['created'] = 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

722
		$data['created'] = /** @scrutinizer ignore-call */ current_time( 'mysql' );
Loading history...
723
		// Check to see if we don't know the salesforce id and it is not a temporary id, or if this is pending.
724
		// If it is using a temporary id, the map will get updated after it finishes running; it won't call this method unless there's an error, which we should log.
725
		if ( substr( $data['salesforce_id'], 0, 7 ) !== 'tmp_sf_' || ( isset( $data['action'] ) && 'pending' === $data['action'] ) ) {
726
			unset( $data['action'] );
727
			$insert = $this->wpdb->insert( $this->object_map_table, $data );
728
		} else {
729
			$status = 'error';
730
			$this->logging->setup(
731
				sprintf(
732
					// translators: %1$s is the log status, %2$s is the name of a WordPress object. %3$s is the id of that object.
733
					esc_html__( '%1$s Mapping: error caused by trying to map the WordPress %2$s with ID of %3$s to Salesforce ID starting with "tmp_sf_", which is invalid.', '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

733
					/** @scrutinizer ignore-call */ 
734
     esc_html__( '%1$s Mapping: error caused by trying to map the WordPress %2$s with ID of %3$s to Salesforce ID starting with "tmp_sf_", which is invalid.', 'object-sync-for-salesforce' ),
Loading history...
734
					ucfirst( esc_attr( $status ) ),
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

734
					ucfirst( /** @scrutinizer ignore-call */ esc_attr( $status ) ),
Loading history...
735
					esc_attr( $data['wordpress_object'] ),
736
					absint( $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

736
					/** @scrutinizer ignore-call */ 
737
     absint( $data['wordpress_id'] )
Loading history...
737
				),
738
				'',
739
				0,
740
				0,
741
				$status
742
			);
743
			return false;
744
		}
745
		if ( 1 === $insert ) {
746
			return $this->wpdb->insert_id;
747
		} elseif ( false !== strpos( $this->wpdb->last_error, 'Duplicate entry' ) ) {
748
			// this error should never happen now, I think. But let's watch and see.
749
			$mapping = $this->load_object_maps_by_salesforce_id( $data['salesforce_id'] )[0];
750
			$id      = $mapping['id'];
751
			$status  = 'error';
752
			$this->logging->setup(
753
				sprintf(
754
					// translators: %1$s is the status word "Error". %2$s is the Id of a Salesforce object. %3$s is the ID of a mapping object.
755
					esc_html__( '%1$s: Mapping: there is already a WordPress object mapped to the Salesforce object %2$s and the mapping object ID is %3$s', 'object-sync-for-salesforce' ),
756
					ucfirst( esc_attr( $status ) ),
757
					esc_attr( $data['salesforce_id'] ),
758
					absint( $id )
759
				),
760
				print_r( $mapping, true ), // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
761
				0,
762
				0,
763
				$status
764
			);
765
			return $id;
766
		} else {
767
			return false;
768
		}
769
	}
770
771
	/**
772
	 * Get all object map rows between WordPress and Salesforce objects.
773
	 *
774
	 * This replaces previous functionality that would return a single object map if there was only one, rather than a multi-dimensional array.
775
	 *
776
	 * @param array $conditions Limitations on the SQL query for object mapping rows.
777
	 * @param bool  $reset Unused parameter.
778
	 * @return $mappings
0 ignored issues
show
Documentation Bug introduced by
The doc comment $mappings at position 0 could not be parsed: Unknown type name '$mappings' at position 0 in $mappings.
Loading history...
779
	 */
780
	public function get_all_object_maps( $conditions = array(), $reset = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $reset is not used and could be removed. ( Ignorable by Annotation )

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

780
	public function get_all_object_maps( $conditions = array(), /** @scrutinizer ignore-unused */ $reset = false ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
781
		$table = $this->object_map_table;
782
		$order = ' ORDER BY object_updated, created';
783
		if ( ! empty( $conditions ) ) { // get multiple but with a limitation.
784
			$mappings = array();
785
786
			if ( ! empty( $conditions ) ) {
787
				$where = ' WHERE ';
788
				$i     = 0;
789
				foreach ( $conditions as $key => $value ) {
790
					$i++;
791
					if ( $i > 1 ) {
792
						$where .= ' AND ';
793
					}
794
					$where .= '`' . $key . '` = "' . $value . '"';
795
				}
796
			} else {
797
				$where = '';
798
			}
799
800
			$mappings = $this->wpdb->get_results( 'SELECT * FROM ' . $table . $where . $order, 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...
801
		} else { // get all of the mappings. ALL THE MAPPINGS.
802
			$mappings = $this->wpdb->get_results( 'SELECT * FROM ' . $table . $order, ARRAY_A );
803
		}
804
805
		return $mappings;
806
807
	}
808
809
	/**
810
	 * Get one or more object map rows between WordPress and Salesforce objects
811
	 *
812
	 * @param array $conditions Limitations on the SQL query for object mapping rows.
813
	 * @param bool  $reset Unused parameter.
814
	 * @return array $map or $mappings
815
	 * @deprecated since 1.8.0
816
	 */
817
	public function get_object_maps( $conditions = array(), $reset = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $reset is not used and could be removed. ( Ignorable by Annotation )

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

817
	public function get_object_maps( $conditions = array(), /** @scrutinizer ignore-unused */ $reset = false ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
818
		$table = $this->object_map_table;
819
		$order = ' ORDER BY object_updated, created';
820
		if ( ! empty( $conditions ) ) { // get multiple but with a limitation.
821
			$mappings = array();
822
823
			if ( ! empty( $conditions ) ) {
824
				$where = ' WHERE ';
825
				$i     = 0;
826
				foreach ( $conditions as $key => $value ) {
827
					$i++;
828
					if ( $i > 1 ) {
829
						$where .= ' AND ';
830
					}
831
					$where .= '`' . $key . '` = "' . $value . '"';
832
				}
833
			} else {
834
				$where = '';
835
			}
836
837
			$mappings = $this->wpdb->get_results( 'SELECT * FROM ' . $table . $where . $order, 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...
838
			if ( ! empty( $mappings ) && 1 === $this->wpdb->num_rows ) {
839
				$mappings = $mappings[0];
840
			}
841
		} else { // get all of the mappings. ALL THE MAPPINGS.
842
			$mappings = $this->wpdb->get_results( 'SELECT * FROM ' . $table . $order, ARRAY_A );
843
			if ( ! empty( $mappings ) && 1 === $this->wpdb->num_rows ) {
844
				$mappings = $mappings[0];
845
			}
846
		}
847
848
		return $mappings;
849
850
	}
851
852
	/**
853
	 * Update an object map row between a WordPress and Salesforce object
854
	 *
855
	 * @param array $posted It's $_POST.
856
	 * @param array $id The ID of the object map row.
857
	 * @return boolean whether it was updated
858
	 */
859
	public function update_object_map( $posted = array(), $id = '' ) {
860
		$data = $this->setup_object_map_data( $posted );
861
		if ( ! isset( $data['object_updated'] ) ) {
862
			$data['object_updated'] = 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

862
			$data['object_updated'] = /** @scrutinizer ignore-call */ current_time( 'mysql' );
Loading history...
863
		}
864
		if ( isset( $data['action'] ) ) {
865
			unset( $data['action'] );
866
		}
867
		$update = $this->wpdb->update(
868
			$this->object_map_table,
869
			$data,
870
			array(
871
				'id' => $id,
872
			)
873
		);
874
		if ( false === $update ) {
875
			return false;
876
		} else {
877
			return true;
878
		}
879
	}
880
881
	/**
882
	 * Setup the data for the object map
883
	 *
884
	 * @param array $posted It's $_POST.
885
	 * @return array $data Filtered array with only the keys that are in the object map database table. Strips out things from WordPress form if they're present.
886
	 */
887
	private function setup_object_map_data( $posted = array() ) {
888
		$allowed_fields   = $this->wpdb->get_col( "DESC {$this->object_map_table}", 0 );
889
		$allowed_fields[] = 'action'; // we use this in both directions even though it isn't in the database; we remove it from the array later if it is present.
890
891
		$data = array_intersect_key( $posted, array_flip( $allowed_fields ) );
892
		return $data;
893
	}
894
895
	/**
896
	 * Delete an object map row between a WordPress and Salesforce object
897
	 *
898
	 * @param int|array $id The ID or IDs of the object map row(s).
899
	 * @return bool whether it was deleted
900
	 */
901
	public function delete_object_map( $id = '' ) {
902
		if ( is_string( $id ) || is_int( $id ) ) {
903
			$data   = array(
904
				'id' => $id,
905
			);
906
			$delete = $this->wpdb->delete( $this->object_map_table, $data );
907
			if ( 1 === $delete ) {
908
				return true;
909
			} else {
910
				return false;
911
			}
912
		} elseif ( is_array( $id ) ) {
0 ignored issues
show
introduced by
The condition is_array($id) is always true.
Loading history...
913
			$ids    = implode( ',', array_map( 'absint', $id ) );
914
			$delete = $this->wpdb->query( "DELETE FROM $this->object_map_table WHERE ID IN ($ids)" );
915
			if ( false !== $delete ) {
916
				return true;
917
			} else {
918
				return false;
919
			}
920
		}
921
	}
922
923
	/**
924
	 * Generate a temporary ID to store while waiting for a push or pull to complete, before the record has been assigned a new ID
925
	 *
926
	 * @param string $direction Whether this is part of a push or pull action.
927
	 * @return string $id is a temporary string that will be replaced if the modification is successful.
928
	 */
929
	public function generate_temporary_id( $direction ) {
930
		if ( 'push' === $direction ) {
931
			$prefix = 'tmp_sf_';
932
		} elseif ( 'pull' === $direction ) {
933
			$prefix = 'tmp_wp_';
934
		}
935
		$id = uniqid( $prefix, true );
936
		return $id;
937
	}
938
939
	/**
940
	 * Returns Salesforce object mappings for a given WordPress object.
941
	 *
942
	 * @param string $object_type Type of object to load.
943
	 * @param int    $object_id Unique identifier of the target object to load.
944
	 * @param bool   $reset Whether or not the cache should be cleared and fetch from current data.
945
	 *
946
	 * @return array of object maps
947
	 */
948
	public function load_all_by_wordpress( $object_type, $object_id, $reset = false ) {
949
		$conditions = array(
950
			'wordpress_id'     => $object_id,
951
			'wordpress_object' => $object_type,
952
		);
953
		return $this->get_all_object_maps( $conditions, $reset );
954
	}
955
956
	/**
957
	 * Returns Salesforce object mappings for a given Salesforce object.
958
	 *
959
	 * @param string $salesforce_id Type of object to load.
960
	 * @param array  $fieldmap the fieldmap this object map works with.
961
	 * @param bool   $reset Whether or not the cache should be cleared and fetch from current data.
962
	 *
963
	 * @return array $maps all the object maps that match the Salesforce Id
964
	 */
965
	public function load_object_maps_by_salesforce_id( $salesforce_id, $fieldmap = array(), $reset = false ) {
966
		$conditions = array(
967
			'salesforce_id' => $salesforce_id,
968
		);
969
		if ( is_array( $fieldmap ) && ! empty( $fieldmap ) ) {
970
			$conditions['wordpress_object'] = $fieldmap['wordpress_object'];
971
		}
972
		$maps = $this->get_all_object_maps( $conditions, $reset );
973
974
		return $maps;
975
	}
976
977
	/**
978
	 * Returns Salesforce object mappings for a given Salesforce object.
979
	 *
980
	 * @param string $salesforce_id Type of object to load.
981
	 * @param bool   $reset Whether or not the cache should be cleared and fetch from current data.
982
	 *
983
	 * @return array $maps all the object maps that match the Salesforce Id
984
	 * @deprecated since 2.1.0. Will be removed in 3.0.0.
985
	 */
986
	public function load_all_by_salesforce( $salesforce_id, $reset = false ) {
987
		$maps = $this->load_object_maps_by_salesforce_id( $salesforce_id, array(), $reset );
988
		return $maps;
989
	}
990
991
	/**
992
	 * Map values between WordPress and Salesforce objects.
993
	 *
994
	 * @param array  $mapping Mapping object.
995
	 * @param array  $object WordPress or Salesforce object data.
996
	 * @param array  $trigger The thing that triggered this mapping.
997
	 * @param bool   $use_soap Flag to enforce use of the SOAP API.
998
	 * @param bool   $is_new Indicates whether a mapping object for this entity already exists.
999
	 * @param string $object_id_field optionally pass the object id field name.
1000
	 * @return array Associative array of key value pairs.
1001
	 */
1002
	public function map_params( $mapping, $object, $trigger, $use_soap = false, $is_new = true, $object_id_field = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $is_new is not used and could be removed. ( Ignorable by Annotation )

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

1002
	public function map_params( $mapping, $object, $trigger, $use_soap = false, /** @scrutinizer ignore-unused */ $is_new = true, $object_id_field = '' ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1003
1004
		$params = array();
1005
1006
		$has_missing_required_salesforce_field = false;
1007
		foreach ( $mapping['fields'] as $fieldmap ) {
1008
1009
			$wordpress_haystack  = array_values( $this->wordpress_events );
1010
			$salesforce_haystack = array_values( $this->salesforce_events );
1011
1012
			$fieldmap['wordpress_field']['methods'] = maybe_unserialize( $fieldmap['wordpress_field']['methods'] );
0 ignored issues
show
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

1012
			$fieldmap['wordpress_field']['methods'] = /** @scrutinizer ignore-call */ maybe_unserialize( $fieldmap['wordpress_field']['methods'] );
Loading history...
1013
1014
			$wordpress_field = $fieldmap['wordpress_field']['label'];
1015
1016
			if ( version_compare( $this->version, '1.2.0', '>=' ) && isset( $fieldmap['salesforce_field']['name'] ) ) {
1017
				$salesforce_field = $fieldmap['salesforce_field']['name'];
1018
				// Load the type of the Salesforce field. We can use this to handle Salesforce field value issues that come up based on what the field sends into WordPress or expects from WordPress.
1019
				$salesforce_field_type = $fieldmap['salesforce_field']['type'];
1020
			} else {
1021
				$salesforce_field = $fieldmap['salesforce_field']['label'];
1022
			}
1023
1024
			// A WordPress event caused this.
1025
			if ( in_array( $trigger, array_values( $wordpress_haystack ), true ) ) {
1026
1027
				// Is the field in WordPress an array, if we unserialize it? Salesforce wants it to be an imploded string.
1028
				if ( is_array( maybe_unserialize( $object[ $wordpress_field ] ) ) ) {
1029
					// if the WordPress field is a list of capabilities (the source field is wp_capabilities), we need to get the array keys from WordPress to send them to Salesforce.
1030
					if ( $this->wpdb->prefix . 'capabilities' === $wordpress_field ) {
1031
						$object[ $wordpress_field ] = implode( $this->array_delimiter, array_keys( $object[ $wordpress_field ] ) );
1032
					} else {
1033
						$object[ $wordpress_field ] = implode( $this->array_delimiter, $object[ $wordpress_field ] );
1034
					}
1035
				}
1036
1037
				if ( isset( $salesforce_field_type ) ) {
1038
					// Is the Salesforce field a date, and is the WordPress value a valid date?
1039
					// According to https://salesforce.stackexchange.com/questions/57032/date-format-with-salesforce-rest-api.
1040
					if ( in_array( $salesforce_field_type, $this->date_types_from_salesforce, true ) ) {
1041
						if ( '' === $object[ $wordpress_field ] ) {
1042
							$object[ $wordpress_field ] = null;
1043
						} else {
1044
							if ( false !== strtotime( $object[ $wordpress_field ] ) ) {
1045
								$timestamp = strtotime( $object[ $wordpress_field ] );
1046
							} else {
1047
								$timestamp = $object[ $wordpress_field ];
1048
							}
1049
							if ( 'datetime' === $salesforce_field_type ) {
1050
								$object[ $wordpress_field ] = date_i18n( 'c', $timestamp );
0 ignored issues
show
Bug introduced by
The function date_i18n was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1050
								$object[ $wordpress_field ] = /** @scrutinizer ignore-call */ date_i18n( 'c', $timestamp );
Loading history...
1051
							} else {
1052
								$object[ $wordpress_field ] = date_i18n( 'Y-m-d', $timestamp );
1053
							}
1054
						}
1055
					}
1056
1057
					// Boolean SF fields only want real boolean values. NULL is also not allowed.
1058
					if ( 'boolean' === $salesforce_field_type ) {
1059
						$object[ $wordpress_field ] = (bool) $object[ $wordpress_field ];
1060
					}
1061
				}
1062
1063
				$params[ $salesforce_field ] = $object[ $wordpress_field ];
1064
1065
				// If the field is a key in Salesforce, remove it from $params to avoid upsert errors from Salesforce,
1066
				// but still put its name in the params array so we can check for it later.
1067
				if ( '1' === $fieldmap['is_key'] ) {
1068
					if ( ! $use_soap ) {
1069
						unset( $params[ $salesforce_field ] );
1070
					}
1071
					$params['key'] = array(
1072
						'salesforce_field' => $salesforce_field,
1073
						'wordpress_field'  => $wordpress_field,
1074
						'value'            => $object[ $wordpress_field ],
1075
					);
1076
				}
1077
1078
				// If the field is a prematch in Salesforce, put its name in the params array so we can check for it later.
1079
				if ( '1' === $fieldmap['is_prematch'] ) {
1080
					$params['prematch'] = array(
1081
						'salesforce_field' => $salesforce_field,
1082
						'wordpress_field'  => $wordpress_field,
1083
						'value'            => $object[ $wordpress_field ],
1084
					);
1085
				}
1086
1087
				// Skip fields that aren't being pushed to Salesforce.
1088
				if ( ! in_array( $fieldmap['direction'], array_values( $this->direction_wordpress ), true ) ) {
1089
					// The trigger is a WordPress trigger, but the fieldmap direction is not a WordPress direction.
1090
					unset( $params[ $salesforce_field ] );
1091
				}
1092
1093
				// I think it's good to over-mention that updateable is really how the Salesforce api spells it.
1094
				// Skip fields that aren't updateable when mapping params because Salesforce will error otherwise.
1095
				// This happens after dealing with the field types because key and prematch should still be available to the plugin, even if the values are not updateable in Salesforce.
1096
				if ( 1 !== (int) $fieldmap['salesforce_field']['updateable'] ) {
1097
					unset( $params[ $salesforce_field ] );
1098
				}
1099
1100
				// This case means the following:
1101
				// this field is expected by the fieldmap
1102
				// Salesforce's api reports that this field is required
1103
				// we do not have a WordPress value for this field, or it's empty
1104
				// it also means the field has not been unset by prematch, updateable, key, or directional flags prior to this check.
1105
				// When this happens, we should flag that we're missing a required Salesforce field.
1106
				if ( in_array( $salesforce_field, $params, true ) && false === filter_var( $fieldmap['salesforce_field']['nillable'], FILTER_VALIDATE_BOOLEAN ) && ( ! isset( $object[ $wordpress_field ] ) || '' === $object[ $wordpress_field ] ) ) {
1107
					$has_missing_required_salesforce_field = true;
1108
				}
1109
1110
				// we don't need a continue with the unset methods because there's no array being created down here.
1111
			} elseif ( in_array( $trigger, $salesforce_haystack, true ) ) {
1112
1113
				// A Salesforce event caused this.
1114
1115
				if ( isset( $salesforce_field_type ) && isset( $object[ $salesforce_field ] ) && ! is_null( $object[ $salesforce_field ] ) ) {
1116
					// Salesforce provides multipicklist values as a delimited string. If the
1117
					// destination field in WordPress accepts multiple values, explode the string into an array and then serialize it.
1118
					if ( in_array( $salesforce_field_type, $this->array_types_from_salesforce, true ) ) {
1119
						$object[ $salesforce_field ] = explode( $this->array_delimiter, $object[ $salesforce_field ] );
1120
						// if the WordPress field is a list of capabilities (the destination field is wp_capabilities), we need to set the array for WordPress to save it.
1121
						if ( $this->wpdb->prefix . 'capabilities' === $wordpress_field ) {
1122
							$capabilities = array();
1123
							foreach ( $object[ $salesforce_field ] as $capability ) {
1124
								$capabilities[ $capability ] = true;
1125
							}
1126
							$object[ $salesforce_field ] = $capabilities;
1127
						}
1128
					}
1129
1130
					// Handle specific data types from Salesforce.
1131
					switch ( $salesforce_field_type ) {
1132
						case ( in_array( $salesforce_field_type, $this->date_types_from_salesforce, true ) ):
1133
							$format = get_option( 'date_format', 'U' );
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

1133
							$format = /** @scrutinizer ignore-call */ get_option( 'date_format', 'U' );
Loading history...
1134
							if ( isset( $fieldmap['wordpress_field']['type'] ) && 'datetime' === $fieldmap['wordpress_field']['type'] ) {
1135
								$format = 'Y-m-d H:i:s';
1136
							}
1137
							if ( 'tribe_events' === $mapping['wordpress_object'] && class_exists( 'Tribe__Events__Main' ) ) {
1138
								$format = 'Y-m-d H:i:s';
1139
							}
1140
							if ( 'datetime' === $salesforce_field_type ) {
1141
								// Note: the Salesforce REST API appears to always return datetimes as GMT values. We should retrieve them that way, then format them to deal with them in WordPress appropriately.
1142
								// We should not do any converting unless it's a datetime, because if it's a date, Salesforce stores it as midnight. We don't want to convert that.
1143
								$object[ $salesforce_field ] = get_date_from_gmt( $object[ $salesforce_field ], 'Y-m-d\TH:i:s\Z' ); // convert from GMT to local date/time based on WordPress time zone setting.
0 ignored issues
show
Bug introduced by
The function get_date_from_gmt was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1143
								$object[ $salesforce_field ] = /** @scrutinizer ignore-call */ get_date_from_gmt( $object[ $salesforce_field ], 'Y-m-d\TH:i:s\Z' ); // convert from GMT to local date/time based on WordPress time zone setting.
Loading history...
1144
							}
1145
							$object[ $salesforce_field ] = date_i18n( $format, strtotime( $object[ $salesforce_field ] ) );
1146
							break;
1147
						case ( in_array( $salesforce_field_type, $this->int_types_from_salesforce, true ) ):
1148
							$object[ $salesforce_field ] = isset( $object[ $salesforce_field ] ) ? (int) $object[ $salesforce_field ] : 0;
1149
							break;
1150
						case 'text':
1151
							$object[ $salesforce_field ] = (string) $object[ $salesforce_field ];
1152
							break;
1153
						case 'url':
1154
							$object[ $salesforce_field ] = esc_url_raw( $object[ $salesforce_field ] );
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

1154
							$object[ $salesforce_field ] = /** @scrutinizer ignore-call */ esc_url_raw( $object[ $salesforce_field ] );
Loading history...
1155
							break;
1156
					}
1157
				}
1158
1159
				// Make an array because we need to store the methods for each field as well.
1160
				if ( isset( $object[ $salesforce_field ] ) ) {
1161
					$params[ $wordpress_field ]          = array();
1162
					$params[ $wordpress_field ]['value'] = $object[ $salesforce_field ];
1163
				} elseif ( is_null( $object[ $salesforce_field ] ) ) {
1164
					// Salesforce returns blank fields as null fields; set them to blank.
1165
					$params[ $wordpress_field ]          = array();
1166
					$params[ $wordpress_field ]['value'] = '';
1167
				} else {
1168
					// prevent fields that don't exist from being passed.
1169
					continue;
1170
				}
1171
1172
				// If the field is a key in Salesforce, disregard since this is caused by a Salesforce event. We're setting up data to be stored in WordPress here, and WordPress is not concerned with external key designations in Salesforce.
1173
1174
				// If the field is a prematch in Salesforce, put its name in the params array so we can check for it later.
1175
				if ( '1' === $fieldmap['is_prematch'] ) {
1176
					$params['prematch'] = array(
1177
						'salesforce_field' => $salesforce_field,
1178
						'wordpress_field'  => $wordpress_field,
1179
						'value'            => $object[ $salesforce_field ],
1180
						'method_match'     => isset( $fieldmap['wordpress_field']['methods']['match'] ) ? $fieldmap['wordpress_field']['methods']['match'] : $fieldmap['wordpress_field']['methods']['read'],
1181
						'method_read'      => $fieldmap['wordpress_field']['methods']['read'],
1182
						'method_create'    => $fieldmap['wordpress_field']['methods']['create'],
1183
						'method_update'    => $fieldmap['wordpress_field']['methods']['update'],
1184
					);
1185
				}
1186
1187
				// Skip fields that aren't being pulled from Salesforce.
1188
				if ( ! in_array( $fieldmap['direction'], array_values( $this->direction_salesforce ), true ) ) {
1189
					// The trigger is a Salesforce trigger, but the fieldmap direction is not a Salesforce direction.
1190
					unset( $params[ $wordpress_field ] );
1191
					// we also need to continue here, so it doesn't create an empty array below for fields that are WordPress -> Salesforce only.
1192
					continue;
1193
				}
1194
1195
				// Skip fields that aren't editable by the plugin when mapping params.
1196
				// By default this is only the WordPress object's ID field.
1197
				// @see $wordpress->object_fields() method and object_sync_for_salesforce_wordpress_field_is_editable filter.
1198
				if ( isset( $fieldmap['wordpress_field']['editable'] ) && true !== (bool) $fieldmap['wordpress_field']['editable'] ) {
1199
					unset( $params[ $wordpress_field ] );
1200
					continue;
1201
				}
1202
1203
				switch ( $trigger ) {
1204
					case $this->sync_sf_create:
1205
						$params[ $wordpress_field ]['method_modify'] = $fieldmap['wordpress_field']['methods']['create'];
1206
						break;
1207
					case $this->sync_sf_update:
1208
						$params[ $wordpress_field ]['method_modify'] = $fieldmap['wordpress_field']['methods']['update'];
1209
						break;
1210
					case $this->sync_sf_delete:
1211
						$params[ $wordpress_field ]['method_modify'] = $fieldmap['wordpress_field']['methods']['delete'];
1212
						break;
1213
				}
1214
1215
				// always allow for the delete and read methods.
1216
				$params[ $wordpress_field ]['method_delete'] = $fieldmap['wordpress_field']['methods']['delete'];
1217
				$params[ $wordpress_field ]['method_read']   = $fieldmap['wordpress_field']['methods']['read'];
1218
1219
			} // End if() statement.
1220
		} // End foreach() loop.
1221
1222
		if ( true === $has_missing_required_salesforce_field ) {
1223
			update_option( $this->option_prefix . 'missing_required_data_id_' . $object[ $object_id_field ], true, false );
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

1223
			/** @scrutinizer ignore-call */ 
1224
   update_option( $this->option_prefix . 'missing_required_data_id_' . $object[ $object_id_field ], true, false );
Loading history...
1224
			return array();
1225
		}
1226
1227
		return $params;
1228
1229
	}
1230
1231
	/**
1232
	 * Prepare field map data for use
1233
	 *
1234
	 * @param array  $mappings Array of fieldmaps.
1235
	 * @param string $record_type Optional Salesforce record type to see if it is allowed or not.
1236
	 *
1237
	 * @return array $mappings Associative array of field maps ready to use
1238
	 */
1239
	private function prepare_fieldmap_data( $mappings, $record_type = '' ) {
1240
		foreach ( $mappings as $id => $mapping ) {
1241
			$mappings[ $id ]['salesforce_record_types_allowed'] = isset( $mapping['salesforce_record_types_allowed'] ) ? maybe_unserialize( $mapping['salesforce_record_types_allowed'] ) : array();
0 ignored issues
show
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

1241
			$mappings[ $id ]['salesforce_record_types_allowed'] = isset( $mapping['salesforce_record_types_allowed'] ) ? /** @scrutinizer ignore-call */ maybe_unserialize( $mapping['salesforce_record_types_allowed'] ) : array();
Loading history...
1242
			$mappings[ $id ]['fields']                          = isset( $mapping['fields'] ) ? maybe_unserialize( $mapping['fields'] ) : array();
1243
			$mappings[ $id ]['sync_triggers']                   = isset( $mapping['sync_triggers'] ) ? maybe_unserialize( $mapping['sync_triggers'] ) : array();
1244
			// format the sync triggers.
1245
			$sync_triggers                    = $this->maybe_upgrade_sync_triggers( $mappings[ $id ]['sync_triggers'], $mapping['version'], $mapping['id'] );
1246
			$mappings[ $id ]['sync_triggers'] = $sync_triggers;
1247
			if ( '' !== $record_type && ! in_array( $record_type, $mappings[ $id ]['salesforce_record_types_allowed'], true ) ) {
1248
				unset( $mappings[ $id ] );
1249
			}
1250
		}
1251
		return $mappings;
1252
	}
1253
1254
	/**
1255
	 * Format the sync trigger values for storage in the database.
1256
	 *
1257
	 * @param array  $sync_triggers Array of sync triggers.
1258
	 * @param string $mapping_version the database version when the fieldmmap was saved.
1259
	 * @param int    $mapping_id if the fieldmap already exists, this is the ID.
1260
	 *
1261
	 * @return array $sync_triggers possibly updated array of sync triggers.
1262
	 */
1263
	private function maybe_upgrade_sync_triggers( $sync_triggers, $mapping_version, $mapping_id = '' ) {
1264
		// in v2 of this plugin, we replaced the bit flags with strings to make them more legible.
1265
		if ( version_compare( $mapping_version, '2.0.0', '<' ) ) {
1266
			// check if the triggers stored in the database are up to date. if not, update them.
1267
			$intersect = array_intersect( $sync_triggers, array_merge( $this->wordpress_events, $this->salesforce_events ) );
1268
			if ( empty( $intersect ) ) {
1269
				$updated_sync_triggers = array();
1270
				foreach ( $sync_triggers as $key => $value ) {
1271
					if ( $value === (string) $this->sync_off_v1 ) {
1272
						$updated_sync_triggers[] = $this->sync_off;
1273
					}
1274
					if ( $value === (string) $this->sync_wordpress_create_v1 ) {
1275
						$updated_sync_triggers[] = $this->sync_wordpress_create;
1276
					}
1277
					if ( $value === (string) $this->sync_wordpress_update_v1 ) {
1278
						$updated_sync_triggers[] = $this->sync_wordpress_update;
1279
					}
1280
					if ( $value === (string) $this->sync_wordpress_delete_v1 ) {
1281
						$updated_sync_triggers[] = $this->sync_wordpress_delete;
1282
					}
1283
					if ( $value === (string) $this->sync_sf_create_v1 ) {
1284
						$updated_sync_triggers[] = $this->sync_sf_create;
1285
					}
1286
					if ( $value === (string) $this->sync_sf_update_v1 ) {
1287
						$updated_sync_triggers[] = $this->sync_sf_update;
1288
					}
1289
					if ( $value === (string) $this->sync_sf_delete_v1 ) {
1290
						$updated_sync_triggers[] = $this->sync_sf_delete;
1291
					}
1292
				}
1293
1294
				if ( '' !== $mapping_id ) {
1295
					// format the fieldmap update query for the database.
1296
					$data = array();
1297
					if ( ! empty( $updated_sync_triggers ) ) {
1298
						$data['sync_triggers'] = array();
1299
						foreach ( $updated_sync_triggers as $key => $value ) {
1300
							$updated_sync_triggers[ $key ] = esc_html( $updated_sync_triggers[ $key ] );
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

1300
							$updated_sync_triggers[ $key ] = /** @scrutinizer ignore-call */ esc_html( $updated_sync_triggers[ $key ] );
Loading history...
1301
						}
1302
						$data['sync_triggers'] = maybe_serialize( $updated_sync_triggers );
0 ignored issues
show
Bug introduced by
The function maybe_serialize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1302
						$data['sync_triggers'] = /** @scrutinizer ignore-call */ maybe_serialize( $updated_sync_triggers );
Loading history...
1303
						$data['version']       = get_option( $this->option_prefix . 'db_version', $this->version );
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

1303
						$data['version']       = /** @scrutinizer ignore-call */ get_option( $this->option_prefix . 'db_version', $this->version );
Loading history...
1304
						// update the sync triggers and version fieldmap in the database.
1305
						$update = $this->wpdb->update(
1306
							$this->fieldmap_table,
1307
							$data,
1308
							array(
1309
								'id' => $mapping_id,
1310
							)
1311
						);
1312
					}
1313
				}
1314
			}
1315
		}
1316
		// whether it was updated or not, this is the array of sync triggers.
1317
		return $sync_triggers;
1318
	}
1319
1320
	/**
1321
	 * Check object map table to see if there have been any failed object map create attempts
1322
	 *
1323
	 * @return array $errors Associative array of rows that failed to finish from either system
1324
	 */
1325
	public function get_failed_object_maps() {
1326
		$table                 = $this->object_map_table;
1327
		$errors                = array();
1328
		$items_per_page        = (int) get_option( $this->option_prefix . 'errors_per_page', 50 );
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

1328
		$items_per_page        = (int) /** @scrutinizer ignore-call */ get_option( $this->option_prefix . 'errors_per_page', 50 );
Loading history...
1329
		$current_error_page    = isset( $_GET['error_page'] ) ? (int) $_GET['error_page'] : 1;
1330
		$offset                = ( $current_error_page * $items_per_page ) - $items_per_page;
1331
		$all_errors            = $this->wpdb->get_results( "SELECT * FROM {$table} WHERE salesforce_id LIKE 'tmp_sf_%' OR wordpress_id LIKE 'tmp_wp_%' OR last_sync_status = 0 LIMIT {$offset}, {$items_per_page}", 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...
1332
		$errors_total          = $this->wpdb->get_var( "SELECT COUNT(`id`) FROM {$table} WHERE salesforce_id LIKE 'tmp_sf_%' OR wordpress_id LIKE 'tmp_wp_%' OR last_sync_status = 0" );
1333
		$errors['total_pages'] = ceil( $errors_total / $items_per_page );
1334
		$errors['pagination']  = paginate_links(
0 ignored issues
show
Bug introduced by
The function paginate_links was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1334
		$errors['pagination']  = /** @scrutinizer ignore-call */ paginate_links(
Loading history...
1335
			array(
1336
				'base'      => add_query_arg( 'error_page', '%#%' ),
0 ignored issues
show
Bug introduced by
The function add_query_arg was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1336
				'base'      => /** @scrutinizer ignore-call */ add_query_arg( 'error_page', '%#%' ),
Loading history...
1337
				'format'    => '',
1338
				'total'     => $errors['total_pages'],
1339
				'prev_text' => __( '&laquo;', '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

1339
				'prev_text' => /** @scrutinizer ignore-call */ __( '&laquo;', 'object-sync-for-salesforce' ),
Loading history...
1340
				'next_text' => __( '&raquo;', 'object-sync-for-salesforce' ),
1341
				'current'   => $current_error_page,
1342
			)
1343
		);
1344
		$errors['error_page']  = $current_error_page;
1345
		$errors['all_errors']  = $all_errors;
1346
		$errors['total']       = $errors_total;
1347
		return $errors;
1348
	}
1349
1350
	/**
1351
	 * Check object map table to see if there have been any failed object map create attempts
1352
	 *
1353
	 * @param int $id The ID of a desired mapping.
1354
	 * @return array $error Associative array of single row that failed to finish based on id
1355
	 */
1356
	public function get_failed_object_map( $id ) {
1357
		$table     = $this->object_map_table;
1358
		$error     = array();
1359
		$error_row = $this->wpdb->get_row( 'SELECT * FROM ' . $table . ' WHERE id = "' . $id . '"', 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...
1360
		if ( ! empty( $error_row ) ) {
1361
			$error = $error_row;
1362
		}
1363
		return $error;
1364
	}
1365
1366
}
1367