Object_Sync_Sf_WordPress::post_create()   F
last analyzed

Complexity

Conditions 13
Paths 720

Size

Total Lines 74
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 13
eloc 45
c 1
b 1
f 0
nc 720
nop 3
dl 0
loc 74
rs 2.8388

How to fix   Long Method    Complexity   

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
 * Work with the WordPress data. This class can make read and write calls to the WordPress database, and also cache the responses.
4
 *
5
 * @class   Object_Sync_Sf_WordPress
6
 * @package Object_Sync_Salesforce
7
 */
8
9
defined( 'ABSPATH' ) || exit;
10
11
/**
12
 * Object_Sync_Sf_WordPress class.
13
 */
14
class Object_Sync_Sf_WordPress {
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
	 * Object_Sync_Sf_Mapping class
60
	 *
61
	 * @var object
62
	 */
63
	public $mappings;
64
65
	/**
66
	 * Supported WordPress objects
67
	 *
68
	 * @var array
69
	 */
70
	public $wordpress_objects;
71
72
	/**
73
	 * Method call options
74
	 *
75
	 * @var array
76
	 */
77
	public $options;
78
79
	/**
80
	 * Object_Sync_Sf_WordPress_Transient class
81
	 *
82
	 * @var object
83
	 */
84
	public $sfwp_transients;
85
86
	/**
87
	 * Whether the plugin is in debug mode
88
	 *
89
	 * @var bool
90
	 */
91
	public $debug;
92
93
	/**
94
	 * Constructor for WordPress class
95
	 */
96
	public function __construct() {
97
		$this->version       = object_sync_for_salesforce()->version;
98
		$this->file          = object_sync_for_salesforce()->file;
99
		$this->wpdb          = object_sync_for_salesforce()->wpdb;
100
		$this->slug          = object_sync_for_salesforce()->slug;
101
		$this->option_prefix = object_sync_for_salesforce()->option_prefix;
102
103
		$this->logging  = object_sync_for_salesforce()->logging;
104
		$this->mappings = object_sync_for_salesforce()->mappings;
105
106
		add_action(
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

106
		/** @scrutinizer ignore-call */ 
107
  add_action(
Loading history...
107
			'admin_init',
108
			function() {
109
				$this->wordpress_objects = $this->get_object_types();
110
			}
111
		);
112
113
		$this->options = array(
114
			'cache'            => true,
115
			'cache_expiration' => $this->cache_expiration( 'wordpress_data_cache', 86400 ),
116
			'type'             => 'read',
117
		);
118
119
		$this->sfwp_transients = new Object_Sync_Sf_WordPress_Transient( 'sfwp_transients' );
120
121
		// use the option value for whether we're in debug mode.
122
		$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

122
		$this->debug = filter_var( /** @scrutinizer ignore-call */ get_option( $this->option_prefix . 'debug_mode', false ), FILTER_VALIDATE_BOOLEAN );
Loading history...
123
124
	}
125
126
	/**
127
	 * Get WordPress object types
128
	 *
129
	 * @return array $wordpress_objects
130
	 */
131
	public function get_object_types() {
132
		/*
133
		 * Allow developers to specify, especially non-post, content types that should be included or ignored.
134
		 * Here's an example of filters to add/remove types:
135
		 *
136
			add_filter( 'object_sync_for_salesforce_add_more_wordpress_types', 'add_more_types', 10, 1 );
137
			function add_more_types( $wordpress_object_types ) {
138
				$wordpress_object_types[] = 'foo'; // this will add to the existing types.
139
				return $wordpress_object_types;
140
			}
141
142
			add_filter( 'object_sync_for_salesforce_remove_wordpress_types', 'wordpress_object_types', 10, 1 );
143
			function remove_types( $types_to_remove ) {
144
				$types_to_remove[] = 'revision'; // this adds to the array of types to ignore
145
				return $types_to_remove;
146
			}
147
		*/
148
149
		// this should include the available object types and send them to the hook.
150
		$wordpress_types_not_posts_include = array( 'user', 'comment', 'category', 'tag' );
151
		$wordpress_objects                 = array_merge( get_post_types(), $wordpress_types_not_posts_include );
0 ignored issues
show
Bug introduced by
The function get_post_types was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

151
		$wordpress_objects                 = array_merge( /** @scrutinizer ignore-call */ get_post_types(), $wordpress_types_not_posts_include );
Loading history...
152
		// this should be all the objects.
153
		$wordpress_objects = apply_filters( $this->option_prefix . 'add_more_wordpress_types', $wordpress_objects );
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

153
		$wordpress_objects = /** @scrutinizer ignore-call */ apply_filters( $this->option_prefix . 'add_more_wordpress_types', $wordpress_objects );
Loading history...
154
155
		// by default, only remove the revision, log, and scheduled-action types that we use in this plugin.
156
		$types_to_remove = apply_filters( $this->option_prefix . 'remove_wordpress_types', array( 'wp_log', 'scheduled-action', 'revision' ) );
157
158
		// if the hook filters out any types, remove them from the visible list.
159
		if ( ! empty( $types_to_remove ) ) {
160
			$wordpress_objects = array_diff( $wordpress_objects, $types_to_remove );
161
		}
162
163
		sort( $wordpress_objects );
164
		return $wordpress_objects;
165
	}
166
167
	/**
168
	 * Get WordPress table structure for an object
169
	 *
170
	 * @param string $object_type The type of object.
171
	 * @return array $object_table_structure The table structure.
172
	 */
173
	public function get_wordpress_table_structure( $object_type ) {
174
		if ( 'attachment' === $object_type ) {
175
			$object_table_structure = array(
176
				'object_name'     => 'post',
177
				'content_methods' => array(
178
					'create' => 'wp_insert_attachment',
179
					'read'   => 'get_posts',
180
					'update' => 'wp_insert_attachment',
181
					'delete' => 'wp_delete_attachment',
182
					'match'  => 'get_posts',
183
				),
184
				'meta_methods'    => array(
185
					'create' => 'wp_generate_attachment_metadata',
186
					'read'   => 'wp_get_attachment_metadata',
187
					'update' => 'wp_update_attachment_metadata',
188
					'delete' => '',
189
					'match'  => 'WP_Query',
190
				),
191
				'content_table'   => $this->wpdb->prefix . 'posts',
192
				'id_field'        => 'ID',
193
				'meta_table'      => $this->wpdb->prefix . 'postmeta',
194
				'meta_join_field' => 'post_id',
195
				'where'           => 'AND ' . $this->wpdb->prefix . 'posts.post_type = "' . $object_type . '"',
196
				'ignore_keys'     => array(),
197
			);
198
		} elseif ( 'user' === $object_type ) {
199
			// User meta fields need to use update_user_meta for create as well, otherwise it'll just get created twice because apparently when the post is created it's already there.
200
201
			$user_meta_methods = array(
202
				'create' => 'update_user_meta',
203
				'read'   => 'get_user_meta',
204
				'update' => 'update_user_meta',
205
				'delete' => 'delete_user_meta',
206
			);
207
208
			$object_table_structure = array(
209
				'object_name'     => 'user',
210
				'content_methods' => array(
211
					'create' => 'wp_insert_user',
212
					'read'   => 'get_user_by',
213
					'update' => 'wp_update_user',
214
					'delete' => 'wp_delete_user',
215
					'match'  => 'get_user_by',
216
				),
217
				'meta_methods'    => $user_meta_methods,
218
				'content_table'   => $this->wpdb->prefix . 'users',
219
				'id_field'        => 'ID',
220
				'meta_table'      => $this->wpdb->prefix . 'usermeta',
221
				'meta_join_field' => 'user_id',
222
				'where'           => '',
223
				'ignore_keys'     => array( // Keep it simple and avoid security risks.
224
					'user_pass',
225
					'user_activation_key',
226
					'session_tokens',
227
				),
228
			);
229
			// Check for Multisite installation. Sitewide User table uses base site prefix across all sites.
230
			if ( is_multisite() ) {
0 ignored issues
show
Bug introduced by
The function is_multisite was not found. Maybe you did not declare it correctly or list all 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
			if ( /** @scrutinizer ignore-call */ is_multisite() ) {
Loading history...
231
				$object_table_structure['content_table'] = $this->wpdb->base_prefix . 'users';
232
				$object_table_structure['meta_table']    = $this->wpdb->base_prefix . 'usermeta';
233
			}
234
		} elseif ( 'post' === $object_type ) {
235
			$object_table_structure = array(
236
				'object_name'     => 'post',
237
				'content_methods' => array(
238
					'create' => 'wp_insert_post',
239
					'read'   => 'get_posts',
240
					'update' => 'wp_update_post',
241
					'delete' => 'wp_delete_post',
242
					'match'  => 'get_posts',
243
				),
244
				'meta_methods'    => array(
245
					'create' => 'add_post_meta',
246
					'read'   => 'get_post_meta',
247
					'update' => 'update_post_meta',
248
					'delete' => 'delete_post_meta',
249
					'match'  => 'WP_Query',
250
				),
251
				'content_table'   => $this->wpdb->prefix . 'posts',
252
				'id_field'        => 'ID',
253
				'meta_table'      => $this->wpdb->prefix . 'postmeta',
254
				'meta_join_field' => 'post_id',
255
				'where'           => 'AND ' . $this->wpdb->prefix . 'posts.post_type = "' . $object_type . '"',
256
				'ignore_keys'     => array(),
257
			);
258
		} elseif ( 'category' === $object_type || 'tag' === $object_type || 'post_tag' === $object_type ) {
259
			// I am unsure why post_tag wasn't here for so long, but i figure it probably needs to be there.
260
			$object_table_structure = array(
261
				'object_name'     => 'term',
262
				'content_methods' => array(
263
					'create' => 'wp_insert_term',
264
					'read'   => 'get_term_by',
265
					'update' => 'wp_update_term',
266
					'delete' => 'wp_delete_term',
267
					'match'  => 'get_term_by',
268
				),
269
				'meta_methods'    => array(
270
					'create' => 'add_term_meta',
271
					'read'   => 'get_term_meta',
272
					'update' => 'update_term_meta',
273
					'delete' => 'delete_metadata',
274
					'match'  => 'WP_Term_Query',
275
				),
276
				'content_table'   => $this->wpdb->prefix . 'terms',
277
				'id_field'        => 'term_id',
278
				'meta_table'      => array( $this->wpdb->prefix . 'termmeta', $this->wpdb->prefix . 'term_taxonomy' ),
279
				'meta_join_field' => 'term_id',
280
				'where'           => '',
281
				'ignore_keys'     => array(),
282
			);
283
		} elseif ( 'comment' === $object_type ) {
284
			$object_table_structure = array(
285
				'object_name'     => 'comment',
286
				'content_methods' => array(
287
					'create' => 'wp_new_comment',
288
					'read'   => 'get_comments',
289
					'update' => 'wp_update_comment',
290
					'delete' => 'wp_delete_comment',
291
					'match'  => 'get_comments',
292
				),
293
				'meta_methods'    => array(
294
					'create' => 'add_comment_meta',
295
					'read'   => 'get_comment_meta',
296
					'update' => 'update_comment_meta',
297
					'delete' => 'delete_comment_metadata',
298
					'match'  => 'WP_Comment_Query',
299
				),
300
				'content_table'   => $this->wpdb->prefix . 'comments',
301
				'id_field'        => 'comment_ID',
302
				'meta_table'      => $this->wpdb->prefix . 'commentmeta',
303
				'meta_join_field' => 'comment_id',
304
				'where'           => '',
305
				'ignore_keys'     => array(),
306
			);
307
		} else { // This is for custom post types.
308
			$object_table_structure = array(
309
				'object_name'     => 'post',
310
				'content_methods' => array(
311
					'create' => 'wp_insert_post',
312
					'read'   => 'get_posts',
313
					'update' => 'wp_update_post',
314
					'delete' => 'wp_delete_post',
315
					'match'  => 'get_posts',
316
				),
317
				'meta_methods'    => array(
318
					'create' => 'add_post_meta',
319
					'read'   => 'get_post_meta',
320
					'update' => 'update_post_meta',
321
					'delete' => 'delete_post_meta',
322
					'match'  => 'WP_Query',
323
				),
324
				'content_table'   => $this->wpdb->prefix . 'posts',
325
				'id_field'        => 'ID',
326
				'meta_table'      => $this->wpdb->prefix . 'postmeta',
327
				'meta_join_field' => 'post_id',
328
				'where'           => 'AND ' . $this->wpdb->prefix . 'posts.post_type = "' . $object_type . '"',
329
				'ignore_keys'     => array(),
330
			);
331
		} // End if() statement.
332
333
		/**
334
		 * Filter the resolved WordPress table structure for an object type.
335
		 *
336
		 * This is a catch-all filter that allows overriding or extending the
337
		 * table structure for any object type (core, custom post types, or
338
		 * custom taxonomies), while keeping backwards compatibility.
339
		 *
340
		 * @since 2.2.13
341
		 *
342
		 * @param array  $object_table_structure The resolved table structure.
343
		 * @param string $object_type            The requested object type.
344
		 */
345
		$object_table_structure = apply_filters(
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

345
		$object_table_structure = /** @scrutinizer ignore-call */ apply_filters(
Loading history...
346
			$this->option_prefix . 'wordpress_table_structure',
347
			$object_table_structure,
348
			$object_type
349
		);
350
351
		return $object_table_structure;
352
	}
353
354
	/**
355
	 * Get WordPress fields for an object
356
	 *
357
	 * @param string $wordpress_object The type of WordPress object.
358
	 * @param string $id_field The field of that object that corresponds with its ID in the database.
359
	 * @return array $object_fields
360
	 */
361
	public function get_wordpress_object_fields( $wordpress_object, $id_field = 'ID' ) {
362
363
		$object_table_structure = $this->get_wordpress_table_structure( $wordpress_object );
364
365
		$meta_table      = $object_table_structure['meta_table'];
366
		$meta_methods    = maybe_unserialize( $object_table_structure['meta_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

366
		$meta_methods    = /** @scrutinizer ignore-call */ maybe_unserialize( $object_table_structure['meta_methods'] );
Loading history...
367
		$content_table   = $object_table_structure['content_table'];
368
		$content_methods = maybe_unserialize( $object_table_structure['content_methods'] );
369
		$id_field        = $object_table_structure['id_field'];
370
		$object_name     = $object_table_structure['object_name'];
371
		$where           = $object_table_structure['where'];
372
		$ignore_keys     = $object_table_structure['ignore_keys'];
373
374
		$object_fields = array();
375
376
		// Try to find the object fields in cache before acquiring it from other source.
377
		if ( true === $this->options['cache'] && 'write' !== $this->options['cache'] ) {
378
			$cached = $this->cache_get( $wordpress_object, array( 'data', 'meta' ) );
379
			if ( is_array( $cached ) ) {
380
				$object_fields['data']       = $cached;
381
				$object_fields['from_cache'] = true;
382
				$object_fields['cached']     = true;
383
			} else {
384
				$object_fields['data'] = $this->object_fields( $object_name, $id_field, $content_table, $content_methods, $meta_table, $meta_methods, $where, $ignore_keys );
385
				if ( ! empty( $object_fields['data'] ) ) {
386
					$object_fields['cached'] = $this->cache_set( $wordpress_object, array( 'data', 'meta' ), $object_fields['data'], $this->options['cache_expiration'] );
387
				} else {
388
					$object_fields['cached'] = false;
389
				}
390
				$object_fields['from_cache'] = false;
391
			}
392
		} else {
393
			$object_fields['data']       = $this->object_fields( $object_name, $id_field, $content_table, $content_methods, $meta_table, $meta_methods, $where, $ignore_keys );
394
			$object_fields['from_cache'] = false;
395
			$object_fields['cached']     = false;
396
		}
397
398
		/*
399
		 * Developers can use this hook to change the WordPress object field array.
400
		 * The returned $object_fields needs to be an array like is described earlier in this method:
401
		 *     $object_fields = array( 'data' => array(), 'from_cache' => bool, 'cached' => bool );
402
		 * This is useful for custom objects that do not use the normal metadata table structure.
403
		 */
404
		$object_fields = apply_filters( $this->option_prefix . 'wordpress_object_fields', $object_fields, $wordpress_object );
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

404
		$object_fields = /** @scrutinizer ignore-call */ apply_filters( $this->option_prefix . 'wordpress_object_fields', $object_fields, $wordpress_object );
Loading history...
405
406
		return $object_fields['data'];
407
408
	}
409
410
	/**
411
	 * Get WordPress data based on what object it is
412
	 *
413
	 * @param string $object_type The type of object.
414
	 * @param int    $object_id The ID of the object.
415
	 * @param bool   $is_deleted Whether the WordPress object has been deleted.
416
	 * @return array $wordpress_object
417
	 */
418
	public function get_wordpress_object_data( $object_type, $object_id, $is_deleted = false ) {
419
		$wordpress_object       = array();
420
		$object_table_structure = $this->get_wordpress_table_structure( $object_type );
421
422
		$meta_table      = $object_table_structure['meta_table'];
423
		$meta_methods    = maybe_unserialize( $object_table_structure['meta_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

423
		$meta_methods    = /** @scrutinizer ignore-call */ maybe_unserialize( $object_table_structure['meta_methods'] );
Loading history...
424
		$content_table   = $object_table_structure['content_table'];
425
		$content_methods = maybe_unserialize( $object_table_structure['content_methods'] );
426
		$id_field        = $object_table_structure['id_field'];
427
		$object_name     = $object_table_structure['object_name'];
428
		$where           = $object_table_structure['where'];
429
		$ignore_keys     = $object_table_structure['ignore_keys'];
430
431
		if ( true === $is_deleted ) {
432
			$wordpress_object              = array();
433
			$wordpress_object[ $id_field ] = $object_id;
434
			return $wordpress_object;
435
		}
436
437
		if ( 'user' === $object_type ) {
438
			$data = get_userdata( $object_id );
0 ignored issues
show
Bug introduced by
The function get_userdata was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

438
			$data = /** @scrutinizer ignore-call */ get_userdata( $object_id );
Loading history...
439
		} elseif ( 'post' === $object_type || 'attachment' === $object_type ) {
440
			$data = get_post( $object_id );
0 ignored issues
show
Bug introduced by
The function get_post was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

440
			$data = /** @scrutinizer ignore-call */ get_post( $object_id );
Loading history...
441
		} elseif ( 'category' === $object_type || 'tag' === $object_type || 'post_tag' === $object_type ) {
442
			$data = get_term( $object_id );
0 ignored issues
show
Bug introduced by
The function get_term was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

442
			$data = /** @scrutinizer ignore-call */ get_term( $object_id );
Loading history...
443
		} elseif ( 'comment' === $object_type ) {
444
			$data = get_comment( $object_id );
0 ignored issues
show
Bug introduced by
The function get_comment was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

444
			$data = /** @scrutinizer ignore-call */ get_comment( $object_id );
Loading history...
445
		} else { // This is for custom post types.
446
			$data = get_post( $object_id );
447
		}
448
449
		if ( ! is_object( $data ) ) {
450
			return $wordpress_object;
451
		}
452
453
		$fields = $this->get_wordpress_object_fields( $object_type );
454
		foreach ( $fields as $key => $value ) {
455
			$field                      = $value['key'];
456
			$wordpress_object[ $field ] = $data->{$field};
457
		}
458
459
		/*
460
		 * Allow developers to change the WordPress object, including any formatting that needs to happen to the data.
461
		 * The returned $wordpress_object needs to be an array like described above.
462
		 * This is useful for custom objects, hidden fields, or custom formatting.
463
		 * Here's an example of filters to add/modify data:
464
		 *
465
			add_filter( 'object_sync_for_salesforce_wordpress_object_data', 'modify_data', 10, 2 );
466
			function modify_data( $wordpress_object, $object_type ) {
467
				$wordpress_object['field_a'] = 'i am a field value that salesforce wants to store but WordPress does not care about';
468
				// Add field values to specific WordPress objects such as 'post', 'page', 'user', a Custom Post Type, etc.
469
				if ( 'user' === $object_type ) {
470
					$wordpress_object['field_b'] = 'i am a field value that salesforce wants to store but WordPress does not care about';
471
				}
472
				return $wordpress_object;
473
			}
474
		*/
475
476
		$wordpress_object = apply_filters( $this->option_prefix . 'wordpress_object_data', $wordpress_object, $object_type );
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

476
		$wordpress_object = /** @scrutinizer ignore-call */ apply_filters( $this->option_prefix . 'wordpress_object_data', $wordpress_object, $object_type );
Loading history...
477
478
		return $wordpress_object;
479
480
	}
481
482
	/**
483
	 * Check to see if this API call exists in the cache
484
	 * if it does, return the transient for that key
485
	 *
486
	 * @param string $url The API call we'd like to make.
487
	 * @param array  $args The arguents of the API call.
488
	 * @return $this->sfwp_transients->get $cachekey
0 ignored issues
show
Documentation Bug introduced by
The doc comment $this->sfwp_transients->get at position 0 could not be parsed: Unknown type name '$this-' at position 0 in $this->sfwp_transients->get.
Loading history...
489
	 */
490
	public function cache_get( $url, $args ) {
491
		if ( is_array( $args ) ) {
0 ignored issues
show
introduced by
The condition is_array($args) is always true.
Loading history...
492
			$args[] = $url;
493
			array_multisort( $args );
494
		} else {
495
			$args .= $url;
496
		}
497
498
		$cachekey = md5( wp_json_encode( $args ) );
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

498
		$cachekey = md5( /** @scrutinizer ignore-call */ wp_json_encode( $args ) );
Loading history...
499
		return $this->sfwp_transients->get( $cachekey );
500
	}
501
502
	/**
503
	 * Create a cache entry for the current result, with the url and args as the key
504
	 *
505
	 * @param string $url The API query URL.
506
	 * @param array  $args The arguments passed on the API query.
507
	 * @param array  $data The data received.
508
	 * @param string $cache_expiration How long to keep the cache result around for.
509
	 * @return Bool whether or not the value was set
510
	 * @link https://wordpress.stackexchange.com/questions/174330/transient-storage-location-database-xcache-w3total-cache
511
	 */
512
	public function cache_set( $url, $args, $data, $cache_expiration = '' ) {
513
		if ( is_array( $args ) ) {
0 ignored issues
show
introduced by
The condition is_array($args) is always true.
Loading history...
514
			$args[] = $url;
515
			array_multisort( $args );
516
		} else {
517
			$args .= $url;
518
		}
519
		$cachekey = md5( wp_json_encode( $args ) );
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

519
		$cachekey = md5( /** @scrutinizer ignore-call */ wp_json_encode( $args ) );
Loading history...
520
		// Cache_expiration is how long it should be stored in the cache.
521
		// If we didn't give a custom one, use the default.
522
		if ( '' === $cache_expiration ) {
523
			$cache_expiration = $this->options['cache_expiration'];
524
		}
525
		return $this->sfwp_transients->set( $cachekey, $data, $cache_expiration );
526
	}
527
528
	/**
529
	 * If there is a WordPress setting for how long to keep this specific cache, return it and set the object property
530
	 * Otherwise, return seconds in 24 hours
531
	 *
532
	 * @param string $option_key The cache item to keep around.
533
	 * @param int    $expire The default time after which to expire the cache.
534
	 * @return The cache expiration saved in the database.
535
	 */
536
	public function cache_expiration( $option_key, $expire ) {
537
		$cache_expiration = get_option( $option_key, $expire );
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

537
		$cache_expiration = /** @scrutinizer ignore-call */ get_option( $option_key, $expire );
Loading history...
538
		return $cache_expiration;
539
	}
540
541
	/**
542
	 * Get all the fields for an object
543
	 * The important thing here is returning the fields as an array:
544
	 * $all_fields = array( 'key' => 'key name', 'table' => 'table name', 'methods' => array( 'create' => '', 'read' => '', 'update' => '', 'delete' => '' ) );
545
	 * if there's a better way to do this than the mess of queries below, we should switch to that when we can
546
	 * we just need to make sure we get all applicable fields for the object itself, as well as its meta fields
547
	 *
548
	 * @param string $object_name THe name of the object type.
549
	 * @param string $id_field The database filed that contains its ID.
550
	 * @param string $content_table The table that normally contains such objects.
551
	 * @param array  $content_methods Unused, but included as part of the return.
552
	 * @param string $meta_table The table where meta values for this object type are contained.
553
	 * @param array  $meta_methods Unused, but included as part of the return.
554
	 * @param string $where SQL query.
555
	 * @param array  $ignore_keys Fields to ignore from the database.
556
	 * @return array $all_fields The fields for the object.
557
	 */
558
	private function object_fields( $object_name, $id_field, $content_table, $content_methods, $meta_table, $meta_methods, $where, $ignore_keys = array() ) {
559
		// These two queries load all the fields from the specified object unless they have been specified as ignore fields.
560
		// They also load the fields that are meta_keys from the specified object's meta table.
561
		// Maybe a box for a custom query, since custom fields get done in so many ways.
562
		// Eventually this would be the kind of thing we could use fields api for, if it ever gets done.
563
		$data_fields      = $this->wpdb->get_col( "DESC {$content_table}", 0 );
564
		$data_field_types = $this->wpdb->get_col( "DESC {$content_table}", 1 ); // get the database field types.
565
566
		if ( is_array( $meta_table ) ) {
0 ignored issues
show
introduced by
The condition is_array($meta_table) is always false.
Loading history...
567
			$tax_table  = $meta_table[1];
568
			$meta_table = $meta_table[0];
569
		}
570
		$select_meta = '
571
		SELECT DISTINCT ' . $meta_table . '.meta_key
572
		FROM ' . $content_table . '
573
		LEFT JOIN ' . $meta_table . '
574
		ON ' . $content_table . '.' . $id_field . ' = ' . $meta_table . '.' . $object_name . '_id
575
		WHERE ' . $meta_table . '.meta_key != ""
576
		' . $where . '
577
		';
578
		$meta_fields = $this->wpdb->get_results( $select_meta );
579
		$all_fields  = array();
580
581
		// by default, WordPress fields are editable except for an object ID field.
582
		// use the filter below to change this for any given field.
583
		// if a field is not editable, it will show in the fieldmap screen with a lock, and will be removed when saving data into WordPress.
584
585
		/* // phpcs:ignore Squiz.PHP.CommentedOutCode.Found
586
		add_filter( 'object_sync_for_salesforce_wordpress_field_is_editable', 'wordpress_field_is_editable', 10, 2 );
587
		function wordpress_field_is_editable( $editable, $field_name ) {
588
			if ( 'ID' === $field_name ) {
589
				$editable = true;
590
			}
591
			return $editable;
592
		}
593
		*/
594
595
		foreach ( $data_fields as $key => $value ) {
596
			if ( ! in_array( $value, (array) $ignore_keys, true ) ) {
597
				$editable = true;
598
				if ( $value === $id_field ) {
599
					$editable = false;
600
				}
601
				$editable     = apply_filters( $this->option_prefix . 'wordpress_field_is_editable', $editable, $value );
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

601
				$editable     = /** @scrutinizer ignore-call */ apply_filters( $this->option_prefix . 'wordpress_field_is_editable', $editable, $value );
Loading history...
602
				$all_fields[] = array(
603
					'key'      => $value,
604
					'table'    => $content_table,
605
					'methods'  => serialize( $content_methods ),
606
					'type'     => $data_field_types[ $key ],
607
					'editable' => $editable,
608
				);
609
			}
610
		}
611
612
		foreach ( $meta_fields as $key => $value ) {
613
			if ( ! in_array( $value->meta_key, (array) $ignore_keys, true ) ) {
614
				$editable = true;
615
				if ( $value === $id_field ) {
616
					$editable = false;
617
				}
618
				$editable     = apply_filters( $this->option_prefix . 'wordpress_field_is_editable', $editable, $value->meta_key );
619
				$all_fields[] = array(
620
					'key'      => $value->meta_key,
621
					'table'    => $meta_table,
622
					'methods'  => serialize( $meta_methods ),
623
					'editable' => $editable,
624
				);
625
			}
626
		}
627
628
		if ( 'term' === $object_name ) {
629
			$taxonomy = $this->wpdb->get_col( "DESC {$tax_table}", 0 );
630
			foreach ( $taxonomy as $key => $value ) {
631
				$exists = array_search( $value, array_column( $all_fields, 'key' ), true );
632
				if ( 0 !== $exists ) {
633
					$editable = true;
634
					if ( $value === $id_field ) {
635
						$editable = false;
636
					}
637
					$editable     = apply_filters( $this->option_prefix . 'wordpress_field_is_editable', $editable, $value );
638
					$all_fields[] = array(
639
						'key'      => $value,
640
						'table'    => $tax_table,
641
						'methods'  => serialize( $content_methods ),
642
						'editable' => $editable,
643
					);
644
				}
645
			}
646
		}
647
648
		return $all_fields;
649
650
	}
651
652
	/**
653
	 * Create a new object of a given type.
654
	 *
655
	 * @param string $name Object type name, E.g., user, post, comment.
656
	 * @param array  $params Values of the fields to set for the object.
657
	 *
658
	 * @return array
659
	 *   data:
660
	 *     id : 123,
661
	 *     success : true
662
	 *   errors : [ ],
663
	 *   from_cache:
664
	 *   cached:
665
	 *   is_redo:
666
	 *
667
	 * part of CRUD for WordPress objects
668
	 */
669
	public function object_create( $name, $params ) {
670
671
		$structure = $this->get_wordpress_table_structure( $name );
672
		$id_field  = $structure['id_field'];
673
674
		switch ( $name ) {
675
			case 'user':
676
				$result = $this->user_create( $params, $id_field );
677
				break;
678
			case 'post':
679
				$result = $this->post_create( $params, $id_field );
680
				break;
681
			case 'attachment':
682
				$result = $this->attachment_create( $params, $id_field );
683
				break;
684
			case 'category':
685
			case 'tag':
686
			case 'post_tag':
687
				$result = $this->term_create( $params, $name, $id_field );
688
				break;
689
			case 'comment':
690
				$result = $this->comment_create( $params, $id_field );
691
				break;
692
			default:
693
				/*
694
				 * Developers can use this hook to create objects with their own methods.
695
				 * The returned $result needs to be an array like this.
696
				 * $id_field should be the database key; i.e. 'ID' and the value should be the new item's id, or whatever WordPress returns from the method (sometimes this can be an error)
697
				 * $success should be a boolean value
698
					$result = array( 'data' => array( $id_field => $post_id, 'success' => $success ), 'errors' => $errors );
699
				 * use hook like: add_filter( 'object_sync_for_salesforce_create_custom_wordpress_item', add_object, 10, 1 );
700
				 * the one param is: array( 'name' => objecttype, 'params' => array_of_params, 'id_field' => idfield )
701
				 */
702
				// Check to see if someone is calling the filter, and apply it if so.
703
				if ( ! has_filter( $this->option_prefix . 'create_custom_wordpress_item' ) ) {
0 ignored issues
show
Bug introduced by
The function has_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

703
				if ( ! /** @scrutinizer ignore-call */ has_filter( $this->option_prefix . 'create_custom_wordpress_item' ) ) {
Loading history...
704
					$result = $this->post_create( $params, $id_field, $name );
705
				} else {
706
					$result = apply_filters(
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

706
					$result = /** @scrutinizer ignore-call */ apply_filters(
Loading history...
707
						$this->option_prefix . 'create_custom_wordpress_item',
708
						array(
709
							'params'   => $params,
710
							'name'     => $name,
711
							'id_field' => $id_field,
712
						)
713
					);
714
				}
715
				break;
716
		} // End switch() method.
717
718
		return $result;
719
	}
720
721
	/**
722
	 * Create new records or update existing records.
723
	 *
724
	 * The new records or updated records are based on the value of the specified
725
	 * field.  If the value is not unique, REST API returns a 300 response with
726
	 * the list of matching records.
727
	 *
728
	 * @param string $name Object type name, E.g., user, post, comment.
729
	 * @param string $key The field to check if this record should be created or updated.
730
	 * @param string $value The value for this record of the field specified for $key.
731
	 * @param array  $methods What WordPress methods do we use to get the data, if there are any. otherwise, maybe will have to do a wpdb query.
732
	 * @param array  $params Values of the fields to set for the object.
733
	 * @param bool   $pull_to_drafts Whether to save to WordPress drafts when pulling from Salesforce.
734
	 * @param bool   $check_only Allows this method to only check for matching records, instead of making any data changes.
735
	 *
736
	 * @return array
737
	 *   data:
738
	 *     "id" : 123,
739
	 *     "success" : true
740
	 *   "errors" : [ ],
741
	 *   from_cache:
742
	 *   cached:
743
	 *   is_redo:
744
	 *
745
	 * part of CRUD for WordPress objects
746
	 */
747
	public function object_upsert( $name, $key, $value, $methods, $params, $pull_to_drafts = false, $check_only = false ) {
748
749
		$structure = $this->get_wordpress_table_structure( $name );
750
		$id_field  = $structure['id_field'];
751
752
		// If key is set, remove from $params to avoid SQL errors.
753
		if ( isset( $params[ $key ] ) ) {
754
			unset( $params[ $key ] );
755
		}
756
757
		// Allow developers to change both the key and value by which objects should be matched.
758
		$key   = apply_filters( $this->option_prefix . 'modify_upsert_key', $key );
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

758
		$key   = /** @scrutinizer ignore-call */ apply_filters( $this->option_prefix . 'modify_upsert_key', $key );
Loading history...
759
		$value = apply_filters( $this->option_prefix . 'modify_upsert_value', $value );
760
761
		switch ( $name ) {
762
			case 'user':
763
				$result = $this->user_upsert( $key, $value, $methods, $params, $id_field, $pull_to_drafts, $check_only );
764
				break;
765
			case 'post':
766
				$result = $this->post_upsert( $key, $value, $methods, $params, $id_field, $pull_to_drafts, $name, $check_only );
767
				break;
768
			case 'attachment':
769
				$result = $this->attachment_upsert( $key, $value, $methods, $params, $id_field, $check_only );
770
				break;
771
			case 'category':
772
			case 'tag':
773
			case 'post_tag':
774
				$result = $this->term_upsert( $key, $value, $methods, $params, $name, $id_field, $check_only );
775
				break;
776
			case 'comment':
777
				$result = $this->comment_upsert( $key, $value, $methods, $params, $id_field, $pull_to_drafts, $check_only );
778
				break;
779
			default:
780
				/*
781
				 * Developers can use this hook to upsert objects with their own methods.
782
				 * The returned $result needs to be an array like this:
783
				 * $id_field should be the database key; i.e. 'ID' and the value should be the item's id, or whatever WordPress returns from the method (sometimes this can be an error)
784
				 * $success should be a boolean value
785
				 *     $result = array( 'data' => array( $id_field => $post_id, 'success' => $success ), 'errors' => $errors );
786
				 * Use hook like this:
787
				 *     add_filter( 'object_sync_for_salesforce_upsert_custom_wordpress_item', upsert_object, 10, 1 );
788
				 * The one param is:
789
				 *     array( 'key' => key, 'value' => value, 'methods' => methods, 'params' => array_of_params, 'id_field' => idfield, 'pull_to_drafts' => pulltodrafts, 'name' => name, 'check_only' => $check_only )
790
				*/
791
				// Check to see if someone is calling the filter, and apply it if so.
792
				if ( ! has_filter( $this->option_prefix . 'upsert_custom_wordpress_item' ) ) {
0 ignored issues
show
Bug introduced by
The function has_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

792
				if ( ! /** @scrutinizer ignore-call */ has_filter( $this->option_prefix . 'upsert_custom_wordpress_item' ) ) {
Loading history...
793
					$result = $this->post_upsert( $key, $value, $methods, $params, $id_field, $pull_to_drafts, $name, $check_only );
794
				} else {
795
					$result = apply_filters(
796
						$this->option_prefix . 'upsert_custom_wordpress_item',
797
						array(
798
							'key'            => $key,
799
							'value'          => $value,
800
							'methods'        => $methods,
801
							'params'         => $params,
802
							'id_field'       => $id_field,
803
							'pull_to_drafts' => $pull_to_drafts,
804
							'name'           => $name,
805
							'check_only'     => $check_only,
806
						)
807
					);
808
				}
809
				break;
810
		} // End switch() method.
811
812
		return $result;
813
	}
814
815
	/**
816
	 * Update an existing object. Part of CRUD for WordPress objects
817
	 *
818
	 * @param string $name Object type name, E.g., user, post, comment.
819
	 * @param int    $id WordPress id of the object.
820
	 * @param array  $params Values of the fields to set for the object.
821
	 * @param array  $mapping_object is the object map connecting the records.
822
	 * @return array
823
	 *   data:
824
	 *     success: 1
825
	 *   "errors" : [ ],
826
	 *   from_cache:
827
	 *   cached:
828
	 *   is_redo:
829
	 */
830
	public function object_update( $name, $id, $params, $mapping_object = array() ) {
831
832
		$structure = $this->get_wordpress_table_structure( $name );
833
		$id_field  = $structure['id_field'];
834
835
		switch ( $name ) {
836
			case 'user':
837
				// User id does not come through by default, but we need it to pass to wp method.
838
				$result = $this->user_update( $id, $params, $id_field );
839
				break;
840
			case 'post':
841
				$result = $this->post_update( $id, $params, $id_field );
842
				break;
843
			case 'attachment':
844
				$result = $this->attachment_update( $id, $params, $id_field );
845
				break;
846
			case 'category':
847
			case 'tag':
848
			case 'post_tag':
849
				$result = $this->term_update( $id, $params, $name, $id_field );
850
				break;
851
			case 'comment':
852
				$result = $this->comment_update( $id, $params, $id_field );
853
				break;
854
			default:
855
				/*
856
				 * Developers can use this hook to update objects with their own methods.
857
				 * The returned $result needs to be an array like this:
858
				 *     $id_field should be the database key; i.e. 'ID' and the value should be the updated item's id, or whatever WordPress returns from the method (sometimes this can be an error)
859
				 * $success should be a boolean value
860
				 *     $result = array( 'data' => array( $id_field => $post_id, 'success' => $success ), 'errors' => $errors );
861
				 * Use hook like this:
862
				 *     add_filter( 'object_sync_for_salesforce_update_custom_wordpress_item', update_object, 10, 1 );
863
				 * The one param is:
864
				 *     array( 'id' => id, 'params' => array_of_params, 'name' => objecttype, 'id_field' => idfield )
865
				 */
866
				// Check to see if someone is calling the filter, and apply it if so.
867
				if ( ! has_filter( $this->option_prefix . 'update_custom_wordpress_item' ) ) {
0 ignored issues
show
Bug introduced by
The function has_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

867
				if ( ! /** @scrutinizer ignore-call */ has_filter( $this->option_prefix . 'update_custom_wordpress_item' ) ) {
Loading history...
868
					$result = $this->post_update( $id, $params, $id_field, $name );
869
				} else {
870
					$result = apply_filters(
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

870
					$result = /** @scrutinizer ignore-call */ apply_filters(
Loading history...
871
						$this->option_prefix . 'update_custom_wordpress_item',
872
						array(
873
							'id'       => $id,
874
							'params'   => $params,
875
							'name'     => $name,
876
							'id_field' => $id_field,
877
						)
878
					);
879
				}
880
				break;
881
		} // End switch() method.
882
883
		return $result;
884
	}
885
886
	/**
887
	 * Delete a WordPress object.
888
	 *
889
	 * @param string $name Object type name, E.g., user, post, comment.
890
	 * @param int    $id WordPress id of the object.
891
	 *
892
	 * @return array
893
	 *   data:
894
	 *     success: 1
895
	 *   "errors" : [ ],
896
	 *
897
	 * part of CRUD for WordPress objects
898
	 */
899
	public function object_delete( $name, $id ) {
900
		$structure = $this->get_wordpress_table_structure( $name );
901
		$id_field  = $structure['id_field'];
902
903
		switch ( $name ) {
904
			case 'user':
905
				$success = $this->user_delete( $id );
906
				break;
907
			case 'post':
908
				$success = $this->post_delete( $id );
909
				break;
910
			case 'attachment':
911
				$success = $this->attachment_delete( $id );
912
				break;
913
			case 'category':
914
			case 'tag':
915
			case 'post_tag':
916
				$success = $this->term_delete( $id, $name );
917
				break;
918
			case 'comment':
919
				$success = $this->comment_delete( $id );
920
				break;
921
			default:
922
				/*
923
				 * Developers can use this hook to delete objects with their own methods.
924
				 * The returned $success is an object of the correct type, or a FALSE
925
				 * Use hook like:
926
				 *     add_filter( 'object_sync_for_salesforce_delete_custom_wordpress_item', delete_object, 10, 1 );
927
				 * The one param is:
928
				 *     array( 'id' => id, 'name' => objecttype )
929
				 */
930
				// Check to see if someone is calling the filter, and apply it if so.
931
				if ( ! has_filter( $this->option_prefix . 'delete_custom_wordpress_item' ) ) {
0 ignored issues
show
Bug introduced by
The function has_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

931
				if ( ! /** @scrutinizer ignore-call */ has_filter( $this->option_prefix . 'delete_custom_wordpress_item' ) ) {
Loading history...
932
					$success = $this->post_delete( $id );
933
				} else {
934
					$success = apply_filters(
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

934
					$success = /** @scrutinizer ignore-call */ apply_filters(
Loading history...
935
						$this->option_prefix . 'delete_custom_wordpress_item',
936
						array(
937
							'id'   => $id,
938
							'name' => $name,
939
						)
940
					);
941
				}
942
943
				$success = $this->post_delete( $id );
944
				break;
945
		} // End switch() method.
946
947
		$result = array(
948
			'data'   => array(
949
				'success' => $success,
950
			),
951
			'errors' => array(),
952
		);
953
		return $result;
954
	}
955
956
	/**
957
	 * Create a new WordPress user.
958
	 *
959
	 * @param array  $params array of user data params.
960
	 * @param string $id_field The column in the DB that holdes the user ID.
961
	 *
962
	 * @return array
963
	 *   data:
964
	 *     ID : 123,
965
	 *     success: 1
966
	 *   "errors" : [ ],
967
	 */
968
	private function user_create( $params, $id_field = 'ID' ) {
969
970
		// Allow username to be email address or username.
971
		// The username could be autogenerated before this point for the sake of URLs.
972
		$username      = $params['user_email']['value'];
973
		$email_address = $params['user_email']['value'];
974
		if ( isset( $params['user_login']['value'] ) ) { // User_login is used by username_exists.
975
			$username = $params['user_login']['value'];
976
		} else {
977
			$params['user_login'] = array(
978
				'value'         => $username,
979
				'method_modify' => 'wp_insert_user',
980
				'method_read'   => 'get_user_by',
981
			);
982
		}
983
984
		// This is a new user.
985
		if ( false === username_exists( $username ) ) {
0 ignored issues
show
Bug introduced by
The function username_exists was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

985
		if ( false === /** @scrutinizer ignore-call */ username_exists( $username ) ) {
Loading history...
986
987
			// Create the user
988
			// WordPress sends a password reset link so this password doesn't get used, but it does exist in the database, which is helpful to prevent access before the user uses their password reset email.
989
			$params['user_pass'] = array(
990
				'value'         => wp_generate_password( 12, false ),
0 ignored issues
show
Bug introduced by
The function wp_generate_password was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

990
				'value'         => /** @scrutinizer ignore-call */ wp_generate_password( 12, false ),
Loading history...
991
				'method_modify' => 'wp_insert_user',
992
				'method_read'   => 'get_user_by',
993
			);
994
			// Load all params with a method_modify of the object structure's content_method into $content.
995
			$content   = array();
996
			$structure = $this->get_wordpress_table_structure( 'user' );
997
			foreach ( $params as $key => $value ) {
998
				if ( in_array( $value['method_modify'], $structure['content_methods'], true ) ) {
999
					$content[ $key ] = $value['value'];
1000
					unset( $params[ $key ] );
1001
				}
1002
			}
1003
1004
			$user_id = wp_insert_user( $content );
0 ignored issues
show
Bug introduced by
The function wp_insert_user was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1004
			$user_id = /** @scrutinizer ignore-call */ wp_insert_user( $content );
Loading history...
1005
1006
			if ( is_wp_error( $user_id ) ) {
0 ignored issues
show
Bug introduced by
The function is_wp_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1006
			if ( /** @scrutinizer ignore-call */ is_wp_error( $user_id ) ) {
Loading history...
1007
				$success = false;
1008
				$errors  = $user_id;
1009
			} else {
1010
				$meta_result = $this->create_wp_meta( $params, $user_id, 'user' );
1011
				$success     = $meta_result['success'];
1012
				$errors      = $meta_result['errors'];
1013
1014
				// Developers can use this hook to set any other user data - permissions, etc.
1015
				do_action( $this->option_prefix . 'set_more_user_data', $user_id, $params, 'create' );
0 ignored issues
show
Bug introduced by
The function do_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

1015
				/** @scrutinizer ignore-call */ 
1016
    do_action( $this->option_prefix . 'set_more_user_data', $user_id, $params, 'create' );
Loading history...
1016
1017
				// Send notification of new user.
1018
				// todo: Figure out what permissions ought to get notifications for this and make sure it works the right way. In the meantime, allow developers to prevent the notification from being sent.
1019
				if ( apply_filters( $this->option_prefix . 'send_new_user_notification', true, $user_id, $params ) ) {
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

1019
				if ( /** @scrutinizer ignore-call */ apply_filters( $this->option_prefix . 'send_new_user_notification', true, $user_id, $params ) ) {
Loading history...
1020
					wp_new_user_notification( $user_id, null, 'both' );
0 ignored issues
show
Bug introduced by
The function wp_new_user_notification was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1020
					/** @scrutinizer ignore-call */ 
1021
     wp_new_user_notification( $user_id, null, 'both' );
Loading history...
1021
				}
1022
			}
1023
		} else {
1024
			$user_id = username_exists( $username );
1025
		} // End if() statement.
1026
1027
		if ( is_wp_error( $user_id ) ) {
1028
			$success = false;
1029
			$errors  = $user_id;
1030
		} else {
1031
			$success = true;
1032
			$errors  = array();
1033
		}
1034
1035
		$result = array(
1036
			'data'   => array(
1037
				$id_field => $user_id,
1038
				'success' => $success,
1039
			),
1040
			'errors' => $errors,
1041
		);
1042
1043
		return $result;
1044
	}
1045
1046
	/**
1047
	 * Create a new WordPress user or update it if a match is found.
1048
	 *
1049
	 * @param string $key What key we are looking at for possible matches.
1050
	 * @param string $value What value we are looking at for possible matches.
1051
	 * @param array  $methods What WordPress methods do we use to get the data, if there are any. otherwise, maybe will have to do a wpdb query.
1052
	 * @param array  $params Array of user data params. This is generated by Object_Sync_Sf_Mapping::map_params().
1053
	 * @param string $id_field Optional string of what the ID field is, if it is ever not ID.
1054
	 * @param bool   $pull_to_drafts Whether to save to WordPress drafts when pulling from Salesforce.
1055
	 * @param bool   $check_only Allows this method to only check for matching records, instead of making any data changes.
1056
	 *
1057
	 * @return array
1058
	 *   data:
1059
	 *     ID : 123,
1060
	 *     success: 1
1061
	 *   "errors" : [ ],
1062
	 */
1063
	private function user_upsert( $key, $value, $methods, $params, $id_field = 'ID', $pull_to_drafts = false, $check_only = false ) {
1064
1065
		// If the key is user_email, we need to make it just email because that is how the WordPress method reads it.
1066
		$method = isset( $methods['method_match'] ) ? $methods['method_match'] : '';
1067
		if ( '' !== $method ) {
1068
			// These methods should give us the user object if we are matching for one.
1069
			// if we are trying to match to a meta field, the method is an object.
1070
			if ( class_exists( $method ) ) {
1071
				$args        = array(
1072
					'meta_query' => array(
1073
						array(
1074
							'key'   => $key,
1075
							'value' => $value,
1076
						),
1077
					),
1078
				);
1079
				$match_query = new $method( $args );
1080
				// the WP_User_Query object has a get_results method that we use.
1081
				$users = $match_query->get_results();
1082
				if ( ! empty( $users ) ) {
1083
					$user = $users[0];
1084
				}
1085
			} else {
1086
				$user = $method( str_replace( 'user_', '', $key ), $value );
1087
			}
1088
1089
			if ( isset( $user ) && isset( $user->{$id_field} ) ) {
1090
				// User does exist after checking the matching value. we want its id.
1091
				$user_id = $user->{$id_field};
1092
1093
				if ( true === $check_only ) {
1094
					// We are just checking to see if there is a match.
1095
					return $user_id;
1096
				}
1097
1098
				// On the prematch fields, we specify the method_update param.
1099
				if ( isset( $methods['method_update'] ) ) {
1100
					$method = $methods['method_update'];
1101
				} else {
1102
					$method = $methods['method_modify'];
1103
				}
1104
				$params[ $key ] = array(
1105
					'value'         => $value,
1106
					'method_modify' => $method,
1107
					'method_read'   => $methods['method_read'],
1108
				);
1109
			} elseif ( false === $check_only ) {
1110
				// User does not exist after checking the matching value. create it.
1111
				// On the prematch fields, we specify the method_create param.
1112
				if ( isset( $methods['method_create'] ) ) {
1113
					$method = $methods['method_create'];
1114
				} else {
1115
					$method = $methods['method_modify'];
1116
				}
1117
				$params[ $key ] = array(
1118
					'value'         => $value,
1119
					'method_modify' => $method,
1120
					'method_read'   => $methods['method_read'],
1121
				);
1122
				$result         = $this->user_create( $params );
1123
				return $result;
1124
			} else {
1125
				// Check only is true but there's not a user yet.
1126
				return null;
1127
			} // End if() statement.
1128
		} else {
1129
			// There is no method by which to check the user. we can check other ways here.
1130
			$params[ $key ] = array(
1131
				'value'         => $value,
1132
				'method_modify' => $methods['method_modify'],
1133
				'method_read'   => $methods['method_read'],
1134
			);
1135
1136
			// Allow username to be email address or username.
1137
			// The username could be autogenerated before this point for the sake of URLs.
1138
			if ( isset( $params['user_email']['value'] ) ) {
1139
				$username      = $params['user_email']['value'];
1140
				$email_address = $params['user_email']['value'];
1141
			}
1142
			if ( isset( $params['user_login']['value'] ) ) { // user_login is used by username_exists.
1143
				$username = $params['user_login']['value'];
1144
			}
1145
1146
			$existing_id = username_exists( $username ); // Returns an id if there is a result.
0 ignored issues
show
Bug introduced by
The function username_exists was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1146
			$existing_id = /** @scrutinizer ignore-call */ username_exists( $username ); // Returns an id if there is a result.
Loading history...
1147
1148
			// User does not exist after more checking. we want to create it.
1149
			if ( false === $existing_id && false === $check_only ) {
1150
				$result = $this->user_create( $params );
1151
				return $result;
1152
			} elseif ( true === $check_only ) {
1153
				// We are just checking to see if there is a match.
1154
				return $existing_id;
1155
			} else {
1156
				// User does exist based on username, and we aren't doing a check only. we want to update the wp user here.
1157
				$user_id = $existing_id;
1158
			}
1159
		} // End if() statement.
1160
1161
		if ( isset( $user_id ) ) {
1162
			foreach ( $params as $key => $value ) {
1163
				$params[ $key ]['method_modify'] = $methods['method_update'];
1164
			}
1165
			$result = $this->user_update( $user_id, $params );
1166
			return $result;
1167
		}
1168
1169
		// Create log entry for lack of a user id.
1170
		$status = 'error';
1171
		$title  = sprintf(
1172
			// translators: placeholders are: 1) the log status.
1173
			esc_html__( '%1$s: Users: Tried to run user_upsert, and ended up without a user id', '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

1173
			/** @scrutinizer ignore-call */ 
1174
   esc_html__( '%1$s: Users: Tried to run user_upsert, and ended up without a user id', 'object-sync-for-salesforce' ),
Loading history...
1174
			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

1174
			ucfirst( /** @scrutinizer ignore-call */ esc_attr( $status ) )
Loading history...
1175
		);
1176
		$this->logging->setup(
1177
			// todo: can we get any more specific about this?
1178
			$title,
1179
			'',
1180
			0,
1181
			0,
1182
			$status
1183
		);
1184
1185
	}
1186
1187
	/**
1188
	 * Update a WordPress user.
1189
	 *
1190
	 * @param string $user_id The ID for the user to be updated. This value needs to be in the array that is sent to wp_update_user.
1191
	 * @param array  $params Array of user data params.
1192
	 * @param string $id_field Optional string of what the ID field is, if it is ever not ID.
1193
	 *
1194
	 * @return array
1195
	 *   data:
1196
	 *     success: 1
1197
	 *   "errors" : [ ],
1198
	 */
1199
	private function user_update( $user_id, $params, $id_field = 'ID' ) {
1200
		$content              = array();
1201
		$content[ $id_field ] = $user_id;
1202
		foreach ( $params as $key => $value ) {
1203
1204
			// if the update value for email already exists on another user, don't fail this update; keep the user's email address.
1205
			if ( 'user_email' === $key && email_exists( $value['value'] ) ) {
0 ignored issues
show
Bug introduced by
The function email_exists was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1205
			if ( 'user_email' === $key && /** @scrutinizer ignore-call */ email_exists( $value['value'] ) ) {
Loading history...
1206
				unset( $params[ $key ] );
1207
				continue;
1208
			}
1209
1210
			// if the update value for login already exists on another user, don't fail this update; keep the user's login.
1211
			if ( 'user_login' === $key && username_exists( $value['value'] ) ) {
0 ignored issues
show
Bug introduced by
The function username_exists was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1211
			if ( 'user_login' === $key && /** @scrutinizer ignore-call */ username_exists( $value['value'] ) ) {
Loading history...
1212
				unset( $params[ $key ] );
1213
				continue;
1214
			}
1215
1216
			if ( 'wp_update_user' === $value['method_modify'] ) {
1217
				$content[ $key ] = $value['value'];
1218
				unset( $params[ $key ] );
1219
			}
1220
		}
1221
1222
		$user_id = wp_update_user( $content );
0 ignored issues
show
Bug introduced by
The function wp_update_user was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1222
		$user_id = /** @scrutinizer ignore-call */ wp_update_user( $content );
Loading history...
1223
1224
		if ( is_wp_error( $user_id ) ) {
0 ignored issues
show
Bug introduced by
The function is_wp_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1224
		if ( /** @scrutinizer ignore-call */ is_wp_error( $user_id ) ) {
Loading history...
1225
			$success = false;
1226
			$errors  = $user_id;
1227
		} else {
1228
			$meta_result = $this->update_wp_meta( $params, $user_id, 'user' );
1229
			$success     = $meta_result['success'];
1230
			$errors      = $meta_result['errors'];
1231
			// Developers can use this hook to set any other user data - permissions, etc.
1232
			do_action( $this->option_prefix . 'set_more_user_data', $user_id, $params, 'update' );
0 ignored issues
show
Bug introduced by
The function do_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

1232
			/** @scrutinizer ignore-call */ 
1233
   do_action( $this->option_prefix . 'set_more_user_data', $user_id, $params, 'update' );
Loading history...
1233
		} // End if() statement.
1234
1235
		$result = array(
1236
			'data'   => array(
1237
				$id_field => $user_id,
1238
				'success' => $success,
1239
			),
1240
			'errors' => $errors,
1241
		);
1242
		return $result;
1243
	}
1244
1245
	/**
1246
	 * Delete a WordPress user.
1247
	 *
1248
	 * @param int $id User ID.
1249
	 * @param int $reassign If we should reassign any posts to other users. We don't change this from NULL anywhere in this plugin.
1250
	 *
1251
	 * @return boolean true if successful
1252
	 */
1253
	private function user_delete( $id, $reassign = null ) {
1254
		// According to https://developer.wordpress.org/reference/functions/wp_delete_user/ we have to include user.php first; otherwise it throws undefined error.
1255
		require_once ABSPATH . 'wp-admin/includes/user.php';
0 ignored issues
show
Bug introduced by
The constant ABSPATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
1256
		$result = wp_delete_user( $id, $reassign );
0 ignored issues
show
Bug introduced by
The function wp_delete_user was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1256
		$result = /** @scrutinizer ignore-call */ wp_delete_user( $id, $reassign );
Loading history...
1257
		return $result;
1258
	}
1259
1260
	/**
1261
	 * Create a new WordPress post.
1262
	 *
1263
	 * @param array  $params Array of post data params.
1264
	 * @param string $id_field Optional string of what the ID field is, if it is ever not ID.
1265
	 * @param string $post_type Optional string for custom post type, if applicable.
1266
	 *
1267
	 * @return array
1268
	 *   data:
1269
	 *     ID : 123,
1270
	 *     success: 1
1271
	 *   "errors" : [ ],
1272
	 */
1273
	private function post_create( $params, $id_field = 'ID', $post_type = 'post' ) {
1274
		// Load all params with a method_modify of the object structure's content_method into $content.
1275
		$content   = array();
1276
		$structure = $this->get_wordpress_table_structure( $post_type );
1277
		foreach ( $params as $key => $value ) {
1278
			if ( in_array( $value['method_modify'], $structure['content_methods'], true ) ) {
1279
				$content[ $key ] = $value['value'];
1280
				unset( $params[ $key ] );
1281
			}
1282
		}
1283
1284
		if ( '' !== $post_type ) {
1285
			$content['post_type'] = $post_type;
1286
		}
1287
1288
		// WordPress post creation will fail with an object of 0 if there is no title or content
1289
		// I think we should allow this to happen and not make users' data decisions, so
1290
		// if we're receiving nothing for either of these, create a blank one so it doesn't fail
1291
		// here we have to use $content because $params has already been unset.
1292
		if ( ! isset( $content['post_title'] ) ) {
1293
			$content['post_title'] = ' ';
1294
		}
1295
		if ( ! isset( $content['post_content'] ) ) {
1296
			$content['post_content'] = ' ';
1297
		}
1298
1299
		if ( 'tribe_events' === $content['post_type'] && function_exists( 'tribe_create_event' ) ) {
1300
			// borrowing some code from https://github.com/tacjtg/rhp-tribe-events/blob/master/rhp-tribe-events.php.
1301
			if ( isset( $params['_EventStartDate'] ) ) {
1302
				$content = $this->append_tec_event_dates( $params['_EventStartDate']['value'], 'start', $content );
1303
				unset( $params['_EventStartDate'] );
1304
			}
1305
			if ( isset( $params['_EventEndDate'] ) ) {
1306
				$content = $this->append_tec_event_dates( $params['_EventEndDate']['value'], 'end', $content );
1307
				unset( $params['_EventEndDate'] );
1308
			}
1309
			$post_id = tribe_create_event( $content );
1310
		} else {
1311
			$post_id = wp_insert_post( $content, true ); // return an error instead of a 0 id.
0 ignored issues
show
Bug introduced by
The function wp_insert_post was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1311
			$post_id = /** @scrutinizer ignore-call */ wp_insert_post( $content, true ); // return an error instead of a 0 id.
Loading history...
1312
		}
1313
1314
		if ( is_wp_error( $post_id ) ) {
0 ignored issues
show
Bug introduced by
The function is_wp_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1314
		if ( /** @scrutinizer ignore-call */ is_wp_error( $post_id ) ) {
Loading history...
1315
			$success = false;
1316
			$errors  = $post_id;
1317
		} else {
1318
			// If it's a custom record type, fix the methods.
1319
			if ( isset( $params['RecordTypeId']['value'] ) ) {
1320
				$params['RecordTypeId']['method_modify'] = 'update_post_meta';
1321
				$params['RecordTypeId']['method_read']   = 'get_post_meta';
1322
			}
1323
			$meta_result = $this->create_wp_meta( $params, $post_id, 'post' );
1324
			$success     = $meta_result['success'];
1325
			$errors      = $meta_result['errors'];
1326
			// Developers can use this hook to set any other post data.
1327
			do_action( $this->option_prefix . 'set_more_post_data', $post_id, $params, 'create' );
0 ignored issues
show
Bug introduced by
The function do_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

1327
			/** @scrutinizer ignore-call */ 
1328
   do_action( $this->option_prefix . 'set_more_post_data', $post_id, $params, 'create' );
Loading history...
1328
		} // End if() statement.
1329
1330
		if ( is_wp_error( $post_id ) ) {
1331
			$success = false;
1332
			$errors  = $post_id;
1333
		} else {
1334
			$success = true;
1335
			$errors  = array();
1336
		}
1337
1338
		$result = array(
1339
			'data'   => array(
1340
				$id_field => $post_id,
1341
				'success' => $success,
1342
			),
1343
			'errors' => $errors,
1344
		);
1345
1346
		return $result;
1347
	}
1348
1349
	/**
1350
	 * Create a new WordPress post or update it if a match is found.
1351
	 *
1352
	 * @param string $key What key we are looking at for possible matches.
1353
	 * @param string $value What value we are looking at for possible matches.
1354
	 * @param array  $methods What WordPress methods do we use to get the data, if there are any. otherwise, maybe will have to do a wpdb query.
1355
	 * @param array  $params Array of post data params.
1356
	 * @param string $id_field optional string of what the ID field is, if it is ever not ID.
1357
	 * @param bool   $pull_to_drafts Whether to save to WordPress drafts when pulling from Salesforce.
1358
	 * @param string $post_type Optional string for custom post type, if applicable.
1359
	 * @param bool   $check_only Allows this method to only check for matching records, instead of making any data changes.
1360
	 *
1361
	 * @return array
1362
	 *   data:
1363
	 *     ID : 123,
1364
	 *     success: 1
1365
	 *   "errors" : [ ],
1366
	 */
1367
	private function post_upsert( $key, $value, $methods, $params, $id_field = 'ID', $pull_to_drafts = false, $post_type = 'post', $check_only = false ) {
1368
1369
		$method = isset( $methods['method_match'] ) ? $methods['method_match'] : '';
1370
		if ( '' !== $method ) {
1371
			// By default, posts use get_posts as the method. args can be like this.
1372
			// The args don't really make sense, and are inconsistently documented.
1373
			// These methods should give us the post object.
1374
			$args = array();
1375
			if ( 'post_title' === $key ) {
1376
				$params['post_title'] = array(
1377
					'value'         => $value,
1378
					'method_modify' => $method,
1379
					'method_read'   => $methods['method_read'],
1380
				);
1381
				$args['name']         = sanitize_title( $value );
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

1381
				$args['name']         = /** @scrutinizer ignore-call */ sanitize_title( $value );
Loading history...
1382
			} else {
1383
				$args[ $key ] = $value;
1384
			}
1385
			$args['post_type'] = $post_type;
1386
			$post_statuses     = array( 'publish' );
1387
1388
			if ( true === filter_var( $pull_to_drafts, FILTER_VALIDATE_BOOLEAN ) ) {
1389
				$post_statuses[] = 'draft';
1390
			}
1391
			$args['post_status'] = $post_statuses;
1392
1393
			// if we are trying to match to a meta field, the method is an object.
1394
			if ( class_exists( $method ) ) {
1395
				unset( $args[ $key ] );
1396
				$args['meta_query'] = array(
1397
					array(
1398
						'key'   => $key,
1399
						'value' => $value,
1400
					),
1401
				);
1402
				$match_query        = new $method( $args );
1403
				// the WP_Query object has a get_posts method that we use.
1404
				$posts = $match_query->get_posts();
1405
			} else {
1406
				$posts = $method( $args );
1407
			}
1408
1409
			if ( isset( $posts ) && isset( $posts[0]->{$id_field} ) ) {
1410
				// Post does exist after checking the matching value. We want its id.
1411
				$post_id = $posts[0]->{$id_field};
1412
1413
				if ( true === $check_only ) {
1414
					// We are just checking to see if there is a match.
1415
					return $post_id;
1416
				}
1417
1418
				// On the prematch fields, we specify the method_update param.
1419
				if ( isset( $methods['method_update'] ) ) {
1420
					$method = $methods['method_update'];
1421
				} else {
1422
					$method = $methods['method_modify'];
1423
				}
1424
				$params[ $key ] = array(
1425
					'value'         => $value,
1426
					'method_modify' => $method,
1427
					'method_read'   => $methods['method_read'],
1428
				);
1429
			} elseif ( false === $check_only ) {
1430
				// Post does not exist after checking the matching value. create it.
1431
				// On the prematch fields, we specify the method_create param.
1432
				if ( isset( $methods['method_create'] ) ) {
1433
					$method = $methods['method_create'];
1434
				} else {
1435
					$method = $methods['method_modify'];
1436
				}
1437
				$params[ $key ] = array(
1438
					'value'         => $value,
1439
					'method_modify' => $method,
1440
					'method_read'   => $methods['method_read'],
1441
				);
1442
				$result         = $this->post_create( $params, $id_field, $post_type );
1443
				return $result;
1444
			} else {
1445
				// Check only is true but there's not a post yet.
1446
				return null;
1447
			} // End if() statement.
1448
		} else {
1449
			// There is no method by which to check the post. we can check other ways here.
1450
			$params[ $key ] = array(
1451
				'value'         => $value,
1452
				'method_modify' => $methods['method_modify'],
1453
				'method_read'   => $methods['method_read'],
1454
			);
1455
1456
			// If we have a title, use it to check for existing post.
1457
			if ( isset( $params['post_title']['value'] ) ) {
1458
				$title = $params['post_title']['value'];
1459
			}
1460
1461
			// If we have content, use it to check for existing post.
1462
			if ( isset( $params['post_content']['value'] ) ) {
1463
				$content = $params['post_content']['value'];
1464
			} else {
1465
				$content = '';
1466
			}
1467
1468
			// If we have a date, use it to check for existing post.
1469
			if ( isset( $params['post_date']['value'] ) ) {
1470
				$date = $params['post_date']['value'];
1471
			} else {
1472
				$date = '';
1473
			}
1474
1475
			$existing_id = post_exists( $title, $content, $date ); // Returns an id if there is a result. Returns 0 if not.
0 ignored issues
show
Bug introduced by
The function post_exists was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1475
			$existing_id = /** @scrutinizer ignore-call */ post_exists( $title, $content, $date ); // Returns an id if there is a result. Returns 0 if not.
Loading history...
1476
1477
			// Post does not exist after more checking. maybe we want to create it.
1478
			if ( 0 === $existing_id && false === $check_only ) {
1479
				$result = $this->post_create( $params, $id_field, $post_type );
1480
				return $result;
1481
			} elseif ( true === $check_only ) {
1482
				// We are just checking to see if there is a match.
1483
				return $existing_id;
1484
			} else {
1485
				// Post does exist based on fields, and we aren't doing a check only. we want to update the wp post here.
1486
				$post_id = $existing_id;
1487
			}
1488
1489
			return $result;
1490
1491
		} // End if() statement.
1492
1493
		if ( isset( $post_id ) ) {
1494
			foreach ( $params as $key => $value ) {
1495
				$params[ $key ]['method_modify'] = $methods['method_update'];
1496
			}
1497
			$result = $this->post_update( $post_id, $params, $id_field, $post_type );
1498
			return $result;
1499
		}
1500
		// Create log entry for lack of a post id.
1501
		$status = 'error';
1502
		$title  = sprintf(
1503
			// translators: placeholders are: 1) the log status.
1504
			esc_html__( '%1$s: Posts: Tried to run post_upsert, and ended up without a post id', '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

1504
			/** @scrutinizer ignore-call */ 
1505
   esc_html__( '%1$s: Posts: Tried to run post_upsert, and ended up without a post id', 'object-sync-for-salesforce' ),
Loading history...
1505
			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

1505
			ucfirst( /** @scrutinizer ignore-call */ esc_attr( $status ) )
Loading history...
1506
		);
1507
		$this->logging->setup(
1508
			// todo: can we be more explicit here about what post upsert failed?
1509
			$title,
1510
			'',
1511
			0,
1512
			0,
1513
			$status
1514
		);
1515
1516
	}
1517
1518
	/**
1519
	 * Update a WordPress post.
1520
	 *
1521
	 * @param string $post_id The ID for the post to be updated. This value needs to be in the array that is sent to wp_update_post.
1522
	 * @param array  $params Array of post data params.
1523
	 * @param string $id_field Optional string of what the ID field is, if it is ever not ID.
1524
	 * @param string $post_type Optional string for custom post type, if applicable.
1525
	 *
1526
	 * @return array
1527
	 *   data:
1528
	 *     success: 1
1529
	 *   "errors" : [ ],
1530
	 */
1531
	private function post_update( $post_id, $params, $id_field = 'ID', $post_type = '' ) {
1532
		$content              = array();
1533
		$content[ $id_field ] = $post_id;
1534
		foreach ( $params as $key => $value ) {
1535
			if ( 'wp_update_post' === $value['method_modify'] ) {
1536
				$content[ $key ] = $value['value'];
1537
				unset( $params[ $key ] );
1538
			}
1539
		}
1540
1541
		if ( '' !== $post_type ) {
1542
			$content['post_type'] = $post_type;
1543
		}
1544
1545
		$post_id = wp_update_post( $content, true ); // return an error instead of a 0 id.
0 ignored issues
show
Bug introduced by
The function wp_update_post was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1545
		$post_id = /** @scrutinizer ignore-call */ wp_update_post( $content, true ); // return an error instead of a 0 id.
Loading history...
1546
1547
		if ( is_wp_error( $post_id ) ) {
0 ignored issues
show
Bug introduced by
The function is_wp_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1547
		if ( /** @scrutinizer ignore-call */ is_wp_error( $post_id ) ) {
Loading history...
1548
			$success = false;
1549
			$errors  = $post_id;
1550
		} else {
1551
			// If it's a custom record type, fix the methods.
1552
			if ( isset( $params['RecordTypeId']['value'] ) ) {
1553
				$params['RecordTypeId']['method_modify'] = 'update_post_meta';
1554
				$params['RecordTypeId']['method_read']   = 'get_post_meta';
1555
			}
1556
			$meta_result = $this->update_wp_meta( $params, $post_id, 'post' );
1557
			$success     = $meta_result['success'];
1558
			$errors      = $meta_result['errors'];
1559
			// Developers can use this hook to set any other post data.
1560
			do_action( $this->option_prefix . 'set_more_post_data', $post_id, $params, 'update' );
0 ignored issues
show
Bug introduced by
The function do_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

1560
			/** @scrutinizer ignore-call */ 
1561
   do_action( $this->option_prefix . 'set_more_post_data', $post_id, $params, 'update' );
Loading history...
1561
		} // End if() statement.
1562
1563
		$result = array(
1564
			'data'   => array(
1565
				$id_field => $post_id,
1566
				'success' => $success,
1567
			),
1568
			'errors' => $errors,
1569
		);
1570
		return $result;
1571
	}
1572
1573
	/**
1574
	 * Delete a WordPress post.
1575
	 *
1576
	 * @param int  $id Post ID.
1577
	 * @param bool $force_delete If we should bypass the trash. We don't change this from FALSE anywhere in this plugin.
1578
	 *
1579
	 * @return mixed post object if successful, false if failed
1580
	 */
1581
	private function post_delete( $id, $force_delete = false ) {
1582
		$result = wp_delete_post( $id, $force_delete );
0 ignored issues
show
Bug introduced by
The function wp_delete_post was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1582
		$result = /** @scrutinizer ignore-call */ wp_delete_post( $id, $force_delete );
Loading history...
1583
		return $result;
1584
	}
1585
1586
	/**
1587
	 * Create a new WordPress attachment.
1588
	 *
1589
	 * @param array  $params Array of attachment data params.
1590
	 * @param string $id_field Optional string of what the ID field is, if it is ever not ID.
1591
	 *
1592
	 * @return array
1593
	 *   data:
1594
	 *     ID : 123,
1595
	 *     success: 1
1596
	 *   "errors" : [ ],
1597
	 */
1598
	private function attachment_create( $params, $id_field = 'ID' ) {
1599
		// Load all params with a method_modify of the object structure's content_method into $content.
1600
		$content   = array();
1601
		$structure = $this->get_wordpress_table_structure( 'attachment' );
1602
		// WP requires post_title, post_content (can be empty), post_status, and post_mime_type to create an attachment.
1603
		foreach ( $params as $key => $value ) {
1604
			if ( in_array( $value['method_modify'], $structure['content_methods'], true ) ) {
1605
				$content[ $key ] = $value['value'];
1606
				unset( $params[ $key ] );
1607
			}
1608
		}
1609
1610
		// Developers can use this hook to pass filename and parent data for the attachment.
1611
		$params = apply_filters( $this->option_prefix . 'set_initial_attachment_data', $params );
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

1611
		$params = /** @scrutinizer ignore-call */ apply_filters( $this->option_prefix . 'set_initial_attachment_data', $params );
Loading history...
1612
1613
		if ( isset( $params['filename']['value'] ) ) {
1614
			$filename = $params['filename']['value'];
1615
		} else {
1616
			$filename = false;
1617
		}
1618
1619
		if ( isset( $params['parent']['value'] ) ) {
1620
			$parent = $params['parent']['value'];
1621
		} else {
1622
			$parent = 0;
1623
		}
1624
1625
		$attachment_id = wp_insert_attachment( $content, $filename, $parent );
0 ignored issues
show
Bug introduced by
The function wp_insert_attachment was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1625
		$attachment_id = /** @scrutinizer ignore-call */ wp_insert_attachment( $content, $filename, $parent );
Loading history...
1626
1627
		if ( is_wp_error( $attachment_id ) ) {
0 ignored issues
show
Bug introduced by
The function is_wp_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1627
		if ( /** @scrutinizer ignore-call */ is_wp_error( $attachment_id ) ) {
Loading history...
1628
			$success = false;
1629
			$errors  = $attachment_id;
1630
		} else {
1631
			$success = true;
1632
			$errors  = array();
1633
1634
			if ( false !== $filename ) {
1635
				// According to https://codex.wordpress.org/Function_Reference/wp_insert_attachment we need this file.
1636
				require_once ABSPATH . 'wp-admin/includes/image.php';
0 ignored issues
show
Bug introduced by
The constant ABSPATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
1637
				// Generate metadata for the attachment.
1638
				$attach_data = wp_generate_attachment_metadata( $attachment_id, $filename );
0 ignored issues
show
Bug introduced by
The function wp_generate_attachment_metadata was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1638
				$attach_data = /** @scrutinizer ignore-call */ wp_generate_attachment_metadata( $attachment_id, $filename );
Loading history...
1639
				wp_update_attachment_metadata( $attachment_id, $attach_data );
0 ignored issues
show
Bug introduced by
The function wp_update_attachment_metadata was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1639
				/** @scrutinizer ignore-call */ 
1640
    wp_update_attachment_metadata( $attachment_id, $attach_data );
Loading history...
1640
			}
1641
1642
			if ( 0 !== $parent ) {
1643
				set_post_thumbnail( $parent_post_id, $attachment_id );
0 ignored issues
show
Bug introduced by
The function set_post_thumbnail was not found. Maybe you did not declare it correctly or list all 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
				/** @scrutinizer ignore-call */ 
1644
    set_post_thumbnail( $parent_post_id, $attachment_id );
Loading history...
1644
			}
1645
1646
			// Developers can use this hook to set any other attachment data.
1647
			do_action( $this->option_prefix . 'set_more_attachment_data', $attachment_id, $params, 'create' );
0 ignored issues
show
Bug introduced by
The function do_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

1647
			/** @scrutinizer ignore-call */ 
1648
   do_action( $this->option_prefix . 'set_more_attachment_data', $attachment_id, $params, 'create' );
Loading history...
1648
1649
		}
1650
1651
		$result = array(
1652
			'data'   => array(
1653
				$id_field => $attachment_id,
1654
				'success' => $success,
1655
			),
1656
			'errors' => $errors,
1657
		);
1658
1659
		return $result;
1660
1661
	}
1662
1663
	/**
1664
	 * Create a new WordPress attachment or update it if a match is found.
1665
	 *
1666
	 * @param string $key What key we are looking at for possible matches.
1667
	 * @param string $value What value we are looking at for possible matches.
1668
	 * @param array  $methods What WordPress methods do we use to get the data, if there are any. otherwise, maybe will have to do a wpdb query.
1669
	 * @param array  $params Array of attachment data params.
1670
	 * @param string $id_field Optional string of what the ID field is, if it is ever not ID.
1671
	 * @param bool   $check_only Allows this method to only check for matching records, instead of making any data changes.
1672
	 *
1673
	 * @return array
1674
	 *   data:
1675
	 *     ID : 123,
1676
	 *     success: 1
1677
	 *   "errors" : [ ],
1678
	 */
1679
	private function attachment_upsert( $key, $value, $methods, $params, $id_field = 'ID', $check_only = false ) {
1680
1681
		$method = isset( $methods['method_match'] ) ? $methods['method_match'] : '';
1682
		if ( '' !== $method ) {
1683
			// Get_posts is more helpful here, so that is the method attachment uses for 'read'.
1684
			// By default, posts use get_posts as the method. args can be like this.
1685
			// The args don't really make sense, and are inconsistently documented.
1686
			// These methods should give us the post object.
1687
			$args = array();
1688
			if ( 'post_title' === $key ) {
1689
				$params['post_title'] = array(
1690
					'value'         => $value,
1691
					'method_modify' => $method,
1692
					'method_read'   => $methods['method_read'],
1693
				);
1694
				$args['name']         = sanitize_title( $value );
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

1694
				$args['name']         = /** @scrutinizer ignore-call */ sanitize_title( $value );
Loading history...
1695
			} else {
1696
				$args[ $key ] = $value;
1697
			}
1698
			$args['post_type'] = 'attachment';
1699
1700
			// if we are trying to match to a meta field, the method is an object.
1701
			if ( class_exists( $method ) ) {
1702
				unset( $args[ $key ] );
1703
				$args['meta_query'] = array(
1704
					array(
1705
						'key'   => $key,
1706
						'value' => $value,
1707
					),
1708
				);
1709
				$match_query        = new $method( $args );
1710
				// the WP_Query object has a get_posts method that we use.
1711
				$posts = $match_query->get_posts();
1712
			} else {
1713
				$posts = $method( $args );
1714
			}
1715
1716
			if ( isset( $posts ) && isset( $posts[0]->{$id_field} ) ) {
1717
				// Attachment does exist after checking the matching value. we want its id.
1718
				$attachment_id = $posts[0]->{$id_field};
1719
1720
				if ( true === $check_only ) {
1721
					// We are just checking to see if there is a match.
1722
					return $attachment_id;
1723
				}
1724
1725
				// On the prematch fields, we specify the method_update param.
1726
				if ( isset( $methods['method_update'] ) ) {
1727
					$method = $methods['method_update'];
1728
				} else {
1729
					$method = $methods['method_modify'];
1730
				}
1731
				$params[ $key ] = array(
1732
					'value'         => $value,
1733
					'method_modify' => $method,
1734
					'method_read'   => $methods['method_read'],
1735
				);
1736
			} elseif ( false === $check_only ) {
1737
				// Attachment does not exist after checking the matching value. create it.
1738
				// On the prematch fields, we specify the method_create param.
1739
				if ( isset( $methods['method_create'] ) ) {
1740
					$method = $methods['method_create'];
1741
				} else {
1742
					$method = $methods['method_modify'];
1743
				}
1744
				$params[ $key ] = array(
1745
					'value'         => $value,
1746
					'method_modify' => $method,
1747
					'method_read'   => $methods['method_read'],
1748
				);
1749
				$result         = $this->attachment_create( $params );
1750
				return $result;
1751
			} else {
1752
				// Check only is true but there's not an attachment yet.
1753
				return null;
1754
			} // End if() statement.
1755
		} else {
1756
			// There is no method by which to check the post. we can check other ways here.
1757
			$params[ $key ] = array(
1758
				'value'         => $value,
1759
				'method_modify' => $methods['method_modify'],
1760
				'method_read'   => $methods['method_read'],
1761
			);
1762
1763
			// If we have a title, use it to check for existing post.
1764
			if ( isset( $params['post_title']['value'] ) ) {
1765
				$title = $params['post_title']['value'];
1766
			}
1767
1768
			// If we have content, use it to check for existing post.
1769
			if ( isset( $params['post_content']['value'] ) ) {
1770
				$content = $params['post_content']['value'];
1771
			} else {
1772
				$content = '';
1773
			}
1774
1775
			// If we have a date, use it to check for existing post.
1776
			if ( isset( $params['post_date']['value'] ) ) {
1777
				$date = $params['post_date']['value'];
1778
			} else {
1779
				$date = '';
1780
			}
1781
1782
			$existing_id = post_exists( $title, $content, $date ); // Returns an id if there is a result. Returns 0 if not.
0 ignored issues
show
Bug introduced by
The function post_exists was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1782
			$existing_id = /** @scrutinizer ignore-call */ post_exists( $title, $content, $date ); // Returns an id if there is a result. Returns 0 if not.
Loading history...
1783
1784
			// Attachment does not exist after more checking. maybe we want to create it.
1785
			if ( 0 === $existing_id && false === $check_only ) {
1786
				$result = $this->attachment_create( $params );
1787
				return $result;
1788
			} elseif ( true === $check_only ) {
1789
				// We are just checking to see if there is a match.
1790
				return $existing_id;
1791
			} else {
1792
				// Attachment does exist based on fields, and we aren't doing a check only. we want to update the wp attachment here.
1793
				$attachment_id = $existing_id;
1794
			}
1795
1796
			return $result;
1797
1798
		} // End if() statement.
1799
1800
		if ( isset( $attachment_id ) ) {
1801
			foreach ( $params as $key => $value ) {
1802
				$params[ $key ]['method_modify'] = $methods['method_update'];
1803
			}
1804
			$result = $this->attachment_update( $attachment_id, $params );
1805
			return $result;
1806
		}
1807
1808
		// Create log entry for lack of an attachment id.
1809
		$status = 'error';
1810
		$title  = sprintf(
1811
			// translators: placeholders are: 1) the log status.
1812
			esc_html__( '%1$s: Attachments: Tried to run attachment_upsert, and ended up without an attachment id', '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

1812
			/** @scrutinizer ignore-call */ 
1813
   esc_html__( '%1$s: Attachments: Tried to run attachment_upsert, and ended up without an attachment id', 'object-sync-for-salesforce' ),
Loading history...
1813
			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

1813
			ucfirst( /** @scrutinizer ignore-call */ esc_attr( $status ) )
Loading history...
1814
		);
1815
		$this->logging->setup(
1816
			$title,
1817
			'',
1818
			0,
1819
			0,
1820
			$status
1821
		);
1822
	}
1823
1824
	/**
1825
	 * Update a WordPress attachment.
1826
	 *
1827
	 * @param string $attachment_id The ID for the attachment to be updated. This value needs to be in the array that is sent to the update methods.
1828
	 * @param array  $params Array of attachment data params.
1829
	 * @param string $id_field Optional string of what the ID field is, if it is ever not ID.
1830
	 *
1831
	 * @return array
1832
	 *   data:
1833
	 *     success: 1
1834
	 *   "errors" : [ ],
1835
	 *
1836
	 * Note: this method uses wp_insert_attachment for core content fields as there isn't a corresponding method for updating these rows
1837
	 * it does use wp_update_attachment_metadata for the meta fields, though.
1838
	 * Developers should use hooks to change this, if it does not meet their needs.
1839
	 */
1840
	private function attachment_update( $attachment_id, $params, $id_field = 'ID' ) {
1841
		$content              = array();
1842
		$content[ $id_field ] = $attachment_id;
1843
		foreach ( $params as $key => $value ) {
1844
			if ( 'wp_insert_attachment' === $value['method_modify'] ) { // Should also be insert attachment maybe.
1845
				$content[ $key ] = $value['value'];
1846
				unset( $params[ $key ] );
1847
			}
1848
		}
1849
1850
		if ( isset( $params['filename']['value'] ) ) {
1851
			$filename = $params['filename']['value'];
1852
		} else {
1853
			$filename = false;
1854
		}
1855
1856
		if ( isset( $params['parent']['value'] ) ) {
1857
			$parent = $params['parent']['value'];
1858
		} else {
1859
			$parent = 0;
1860
		}
1861
1862
		$attachment_id = wp_insert_attachment( $content, $filename, $parent );
0 ignored issues
show
Bug introduced by
The function wp_insert_attachment was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1862
		$attachment_id = /** @scrutinizer ignore-call */ wp_insert_attachment( $content, $filename, $parent );
Loading history...
1863
1864
		if ( is_wp_error( $attachment_id ) ) {
0 ignored issues
show
Bug introduced by
The function is_wp_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1864
		if ( /** @scrutinizer ignore-call */ is_wp_error( $attachment_id ) ) {
Loading history...
1865
			$success = false;
1866
			$errors  = $attachment_id;
1867
		} else {
1868
			$success = true;
1869
			$errors  = array();
1870
1871
			if ( false !== $filename ) {
1872
				// According to https://codex.wordpress.org/Function_Reference/wp_insert_attachment we need this file.
1873
				require_once ABSPATH . 'wp-admin/includes/image.php';
0 ignored issues
show
Bug introduced by
The constant ABSPATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
1874
				// Generate metadata for the attachment.
1875
				$attach_data = wp_generate_attachment_metadata( $attachment_id, $filename );
0 ignored issues
show
Bug introduced by
The function wp_generate_attachment_metadata was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1875
				$attach_data = /** @scrutinizer ignore-call */ wp_generate_attachment_metadata( $attachment_id, $filename );
Loading history...
1876
			}
1877
1878
			// Put the data from salesforce into the meta array.
1879
			$attach_new_data = array();
1880
			foreach ( $params as $key => $value ) {
1881
				$method                  = $value['method_modify'];
1882
				$attach_new_data[ $key ] = $value['value'];
1883
			}
1884
1885
			if ( isset( $attach_data ) ) {
1886
				$attach_data = array_merge( $attach_data, $attach_new_data );
1887
			} else {
1888
				$attach_data = $attach_new_data;
1889
			}
1890
1891
			$meta_updated = wp_update_attachment_metadata( $attachment_id, $attach_data );
0 ignored issues
show
Bug introduced by
The function wp_update_attachment_metadata was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1891
			$meta_updated = /** @scrutinizer ignore-call */ wp_update_attachment_metadata( $attachment_id, $attach_data );
Loading history...
1892
1893
			if ( false === $meta_updated ) {
1894
				$success  = false;
1895
				$errors[] = array(
1896
					'key'   => $key,
1897
					'value' => $value,
1898
				);
1899
			}
1900
1901
			if ( 0 !== $parent ) {
1902
				set_post_thumbnail( $parent_post_id, $attachment_id );
0 ignored issues
show
Bug introduced by
The function set_post_thumbnail was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1902
				/** @scrutinizer ignore-call */ 
1903
    set_post_thumbnail( $parent_post_id, $attachment_id );
Loading history...
1903
			}
1904
1905
			// Developers can use this hook to set any other attachment data.
1906
			do_action( $this->option_prefix . 'set_more_attachment_data', $attachment_id, $params, 'update' );
0 ignored issues
show
Bug introduced by
The function do_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

1906
			/** @scrutinizer ignore-call */ 
1907
   do_action( $this->option_prefix . 'set_more_attachment_data', $attachment_id, $params, 'update' );
Loading history...
1907
1908
		} // End if() statement.
1909
1910
		$result = array(
1911
			'data'   => array(
1912
				$id_field => $attachment_id,
1913
				'success' => $success,
1914
			),
1915
			'errors' => $errors,
1916
		);
1917
		return $result;
1918
	}
1919
1920
	/**
1921
	 * Delete a WordPress attachment.
1922
	 *
1923
	 * @param int  $id Attachment ID.
1924
	 * @param bool $force_delete If we should bypass the trash. We don't change this from FALSE anywhere in this plugin.
1925
	 *
1926
	 * @return mixed
1927
	 *   attachment object if successful, false if failed
1928
	 */
1929
	private function attachment_delete( $id, $force_delete = false ) {
1930
		$result = wp_delete_attachment( $id, $force_delete );
0 ignored issues
show
Bug introduced by
The function wp_delete_attachment was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1930
		$result = /** @scrutinizer ignore-call */ wp_delete_attachment( $id, $force_delete );
Loading history...
1931
		return $result;
1932
	}
1933
1934
	/**
1935
	 * Create a new WordPress term.
1936
	 *
1937
	 * @param array  $params Array of term data params.
1938
	 * @param string $taxonomy The taxonomy to which to add the term. this is required.
1939
	 * @param string $id_field Optional string of what the ID field is, if it is ever not ID.
1940
	 *
1941
	 * @return array
1942
	 *   data:
1943
	 *     ID : 123,
1944
	 *     success: 1
1945
	 *   "errors" : [ ],
1946
	 */
1947
	private function term_create( $params, $taxonomy, $id_field = 'ID' ) {
1948
		if ( 'tag' === $taxonomy ) {
1949
			$taxonomy = 'post_tag';
1950
		}
1951
		// Load all params with a method_modify of the object structure's content_method into $content.
1952
		$content   = array();
1953
		$structure = $this->get_wordpress_table_structure( $taxonomy );
1954
		$args      = array();
1955
		foreach ( $params as $key => $value ) {
1956
			if ( 'name' === $key ) {
1957
				$name = $value['value'];
1958
				unset( $params[ $key ] );
1959
			}
1960
			if ( in_array( $value['method_modify'], $structure['content_methods'], true ) && 'name' !== $key ) {
1961
				$args[ $key ] = $value['value'];
1962
				unset( $params[ $key ] );
1963
			}
1964
		}
1965
		if ( isset( $name ) ) {
1966
			$term = wp_insert_term( $name, $taxonomy, $args );
0 ignored issues
show
Bug introduced by
The function wp_insert_term was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1966
			$term = /** @scrutinizer ignore-call */ wp_insert_term( $name, $taxonomy, $args );
Loading history...
1967
		}
1968
1969
		if ( is_wp_error( $term ) ) {
0 ignored issues
show
Bug introduced by
The function is_wp_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

1969
		if ( /** @scrutinizer ignore-call */ is_wp_error( $term ) ) {
Loading history...
1970
			$success = false;
1971
			$errors  = $term;
1972
		} else {
1973
			$term_id     = $term[ "$id_field" ];
1974
			$meta_result = $this->create_wp_meta( $params, $term_id, 'term' );
1975
			$success     = $meta_result['success'];
1976
			$errors      = $meta_result['errors'];
1977
			// Developers can use this hook to set any other term data.
1978
			do_action( $this->option_prefix . 'set_more_term_data', $term_id, $params, 'create' );
0 ignored issues
show
Bug introduced by
The function do_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

1978
			/** @scrutinizer ignore-call */ 
1979
   do_action( $this->option_prefix . 'set_more_term_data', $term_id, $params, 'create' );
Loading history...
1979
		} // End if() statement.
1980
1981
		if ( is_wp_error( $term ) ) {
1982
			$success = false;
1983
			$errors  = $term;
1984
		} else {
1985
			$success = true;
1986
			$errors  = array();
1987
		}
1988
1989
		$result = array(
1990
			'data'   => array(
1991
				$id_field => $term_id,
1992
				'success' => $success,
1993
			),
1994
			'errors' => $errors,
1995
		);
1996
1997
		return $result;
1998
1999
	}
2000
2001
	/**
2002
	 * Create a new WordPress term or update it if a match is found.
2003
	 *
2004
	 * @param string $key What key we are looking at for possible matches.
2005
	 * @param string $value What value we are looking at for possible matches.
2006
	 * @param array  $methods What WordPress methods do we use to get the data, if there are any. otherwise, maybe will have to do a wpdb query.
2007
	 * @param array  $params Array of term data params.
2008
	 * @param string $taxonomy The taxonomy to which to add the term. this is required..
2009
	 * @param string $id_field Optional string of what the ID field is, if it is ever not ID.
2010
	 * @param bool   $pull_to_drafts Whether to save to WordPress drafts when pulling from Salesforce.
2011
	 * @param bool   $check_only Allows this method to only check for matching records, instead of making any data changes.
2012
	 *
2013
	 * @return array
2014
	 *   data:
2015
	 *     ID : 123,
2016
	 *     success: 1
2017
	 *   "errors" : [ ],
2018
	 */
2019
	private function term_upsert( $key, $value, $methods, $params, $taxonomy, $id_field = 'ID', $pull_to_drafts = false, $check_only = false ) {
2020
		if ( 'tag' === $taxonomy ) {
2021
			$taxonomy = 'post_tag';
2022
		}
2023
		$method = isset( $methods['method_match'] ) ? $methods['method_match'] : '';
2024
		if ( '' !== $method ) {
2025
			// These methods should give us the term object if we are matching for one.
2026
			// If we are trying to match to a meta field, the method is an object.
2027
			if ( class_exists( $method ) ) {
2028
				$args        = array(
2029
					'taxonomy'   => $taxonomy,
2030
					'meta_key'   => $key,
2031
					'meta_value' => $value,
2032
				);
2033
				$match_query = new $method( $args );
2034
				// the WP_Term_Query object has a get_terms method that we use.
2035
				$terms = $match_query->get_terms();
2036
				if ( ! empty( $terms ) ) {
2037
					$term = $terms[0];
2038
				}
2039
			} else {
2040
				$term = $method( $key, $value, $taxonomy ); // We need to put the taxonomy in there probably.
2041
			}
2042
2043
			if ( isset( $term ) && isset( $term->{$id_field} ) ) {
2044
				// Term does exist after checking the matching value. we want its id.
2045
				$term_id = $term->{$id_field};
2046
2047
				if ( true === $check_only ) {
2048
					// We are just checking to see if there is a match.
2049
					return $term_id;
2050
				}
2051
2052
				// On the prematch fields, we specify the method_update param.
2053
				if ( isset( $methods['method_update'] ) ) {
2054
					$method = $methods['method_update'];
2055
				} else {
2056
					$method = $methods['method_modify'];
2057
				}
2058
				$params[ $key ] = array(
2059
					'value'         => $value,
2060
					'method_modify' => $method,
2061
					'method_read'   => $methods['method_read'],
2062
				);
2063
			} elseif ( false === $check_only ) {
2064
				// Term does not exist after checking the matching value. Create it.
2065
				// On the prematch fields, we specify the method_create param.
2066
				if ( isset( $methods['method_create'] ) ) {
2067
					$method = $methods['method_create'];
2068
				} else {
2069
					$method = $methods['method_modify'];
2070
				}
2071
				$params[ $key ] = array(
2072
					'value'         => $value,
2073
					'method_modify' => $method,
2074
					'method_read'   => $methods['method_read'],
2075
				);
2076
				$result         = $this->term_create( $params, $taxonomy, $id_field );
2077
				return $result;
2078
			} else {
2079
				// Check only is true but there's not a term yet.
2080
				return null;
2081
			} // End if() statement.
2082
		} else {
2083
			// There is no method by which to check the term. we can check other ways here.
2084
			$params[ $key ] = array(
2085
				'value'         => $value,
2086
				'method_modify' => $methods['method_modify'],
2087
				'method_read'   => $methods['method_read'],
2088
			);
2089
2090
			if ( isset( $params['name']['value'] ) ) {
2091
				$term = $params['name']['value'];
2092
			}
2093
2094
			if ( isset( $params['parent']['value'] ) ) {
2095
				$parent = $params['parent']['value'];
2096
			} else {
2097
				$parent = 0;
2098
			}
2099
2100
			// Returns an id if there is a result. Returns null if it does not exist.
2101
			// wpcom_vip_term_exists is cached, and therefore preferred.
2102
			if ( function_exists( 'wpcom_vip_term_exists' ) ) {
2103
				$existing_id = wpcom_vip_term_exists( $term, $taxonomy, $parent );
2104
			} else {
2105
				$existing_id = term_exists( $term, $taxonomy, $parent );
0 ignored issues
show
Bug introduced by
The function term_exists was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

2105
				$existing_id = /** @scrutinizer ignore-call */ term_exists( $term, $taxonomy, $parent );
Loading history...
2106
			}
2107
2108
			// Term does not exist after more checking. maybe we want to create it.
2109
			if ( null === $existing_id && false === $check_only ) {
2110
				$result = $this->term_create( $params, $taxonomy, $id_field );
2111
				return $result;
2112
			} elseif ( true === $check_only ) {
2113
				// We are just checking to see if there is a match.
2114
				return $existing_id;
2115
			} else {
2116
				// Term does exist based on criteria, and we aren't doing a check only. we want to update the wp term here.
2117
				$term_id = $existing_id;
2118
			}
2119
		} // End if() statement.
2120
2121
		if ( isset( $term_id ) ) {
2122
			foreach ( $params as $key => $value ) {
2123
				$params[ $key ]['method_modify'] = $methods['method_update'];
2124
			}
2125
			$result = $this->term_update( $term_id, $params, $taxonomy, $id_field );
2126
			return $result;
2127
		}
2128
		// Create log entry for lack of a term id.
2129
		$status = 'error';
2130
		$title  = sprintf(
2131
			// translators: placeholders are: 1) the log status.
2132
			esc_html__( '%1$s: Terms: Tried to run term_upsert, and ended up without a term id', '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

2132
			/** @scrutinizer ignore-call */ 
2133
   esc_html__( '%1$s: Terms: Tried to run term_upsert, and ended up without a term id', 'object-sync-for-salesforce' ),
Loading history...
2133
			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

2133
			ucfirst( /** @scrutinizer ignore-call */ esc_attr( $status ) )
Loading history...
2134
		);
2135
		$this->logging->setup(
2136
			$title,
2137
			'',
2138
			0,
2139
			0,
2140
			$status
2141
		);
2142
2143
	}
2144
2145
	/**
2146
	 * Update a WordPress term.
2147
	 *
2148
	 * @param int    $term_id The ID for the term to be updated. This value needs to be in the array that is sent to wp_update_term.
2149
	 * @param array  $params Array of term data params.
2150
	 * @param string $taxonomy The taxonomy to which to add the term. this is required.
2151
	 * @param string $id_field Optional string of what the ID field is, if it is ever not ID.
2152
	 *
2153
	 * @return array
2154
	 *   data:
2155
	 *     success: 1
2156
	 *   "errors" : [ ],
2157
	 */
2158
	private function term_update( $term_id, $params, $taxonomy, $id_field = 'ID' ) {
2159
		if ( 'tag' === $taxonomy ) {
2160
			$taxonomy = 'post_tag';
2161
		}
2162
		$args = array();
2163
		foreach ( $params as $key => $value ) {
2164
			if ( 'wp_update_term' === $value['method_modify'] ) {
2165
				$args[ $key ] = $value['value'];
2166
				unset( $params[ $key ] );
2167
			}
2168
		}
2169
		$term = wp_update_term( $term_id, $taxonomy, $args );
0 ignored issues
show
Bug introduced by
The function wp_update_term was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

2169
		$term = /** @scrutinizer ignore-call */ wp_update_term( $term_id, $taxonomy, $args );
Loading history...
2170
2171
		if ( is_wp_error( $term ) ) {
0 ignored issues
show
Bug introduced by
The function is_wp_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

2171
		if ( /** @scrutinizer ignore-call */ is_wp_error( $term ) ) {
Loading history...
2172
			$success = false;
2173
			$errors  = $term;
2174
		} else {
2175
			$term_id     = $term[ "$id_field" ];
2176
			$meta_result = $this->update_wp_meta( $params, $term_id, 'term' );
2177
			$success     = $meta_result['success'];
2178
			$errors      = $meta_result['errors'];
2179
			// Developers can use this hook to set any other term data.
2180
			do_action( $this->option_prefix . 'set_more_term_data', $term_id, $params, 'update' );
0 ignored issues
show
Bug introduced by
The function do_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

2180
			/** @scrutinizer ignore-call */ 
2181
   do_action( $this->option_prefix . 'set_more_term_data', $term_id, $params, 'update' );
Loading history...
2181
		} // End if() statement.
2182
2183
		if ( is_wp_error( $term ) ) {
2184
			$success = false;
2185
			$errors  = $term;
2186
		} else {
2187
			$success = true;
2188
			$errors  = array();
2189
		}
2190
2191
		$result = array(
2192
			'data'   => array(
2193
				$id_field => $term_id,
2194
				'success' => $success,
2195
			),
2196
			'errors' => $errors,
2197
		);
2198
2199
		return $result;
2200
2201
	}
2202
2203
	/**
2204
	 * Delete a WordPress term.
2205
	 *
2206
	 * @param int    $term_id The ID for the term to be updated. This value needs to be in the array that is sent to wp_update_term.
2207
	 * @param string $taxonomy The taxonomy from which to delete the term. this is required.
2208
	 *
2209
	 * @return bool True if successful, false if failed.
2210
	 */
2211
	private function term_delete( $term_id, $taxonomy ) {
2212
		if ( 'tag' === $taxonomy ) {
2213
			$taxonomy = 'post_tag';
2214
		}
2215
		$result = wp_delete_term( $term_id, $taxonomy );
0 ignored issues
show
Bug introduced by
The function wp_delete_term was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

2215
		$result = /** @scrutinizer ignore-call */ wp_delete_term( $term_id, $taxonomy );
Loading history...
2216
		return $result;
2217
	}
2218
2219
	/**
2220
	 * Create a new WordPress comment.
2221
	 *
2222
	 * @param array  $params Array of comment data params.
2223
	 * @param string $id_field Optional string of what the ID field is, if it is ever not comment_ID.
2224
	 *
2225
	 * @return array
2226
	 *   data:
2227
	 *     ID : 123,
2228
	 *     success: 1
2229
	 *   "errors" : [ ],
2230
	 */
2231
	private function comment_create( $params, $id_field = 'comment_ID' ) {
2232
		// Load all params with a method_modify of the object structure's content_method into $content.
2233
		$content   = array();
2234
		$structure = $this->get_wordpress_table_structure( 'comment' );
2235
		foreach ( $params as $key => $value ) {
2236
			if ( in_array( $value['method_modify'], $structure['content_methods'], true ) ) {
2237
				$content[ $key ] = $value['value'];
2238
				unset( $params[ $key ] );
2239
			}
2240
		}
2241
2242
		// Fields that are required for comments, even if they are empty values.
2243
		if ( ! isset( $content['comment_author'] ) ) {
2244
			$content['comment_author'] = '';
2245
		}
2246
		if ( ! isset( $content['comment_author_IP'] ) ) {
2247
			$content['comment_author_IP'] = '';
2248
		}
2249
		if ( ! isset( $content['comment_author_email'] ) ) {
2250
			$content['comment_author_email'] = '';
2251
		}
2252
		if ( ! isset( $content['comment_author_url'] ) ) {
2253
			$content['comment_author_url'] = '';
2254
		}
2255
		if ( ! isset( $content['comment_type'] ) ) {
2256
			$content['comment_type'] = '';
2257
		}
2258
2259
		$comment_id = wp_new_comment( $content );
0 ignored issues
show
Bug introduced by
The function wp_new_comment was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

2259
		$comment_id = /** @scrutinizer ignore-call */ wp_new_comment( $content );
Loading history...
2260
2261
		if ( is_wp_error( $comment_id ) ) {
0 ignored issues
show
Bug introduced by
The function is_wp_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

2261
		if ( /** @scrutinizer ignore-call */ is_wp_error( $comment_id ) ) {
Loading history...
2262
			$success = false;
2263
			$errors  = $comment_id;
2264
		} else {
2265
			$meta_result = $this->create_wp_meta( $params, $comment_id, 'comment' );
2266
			$success     = $meta_result['success'];
2267
			$errors      = $meta_result['errors'];
2268
			// Developers can use this hook to set any other comment data.
2269
			do_action( $this->option_prefix . 'set_more_comment_data', $comment_id, $params, 'create' );
0 ignored issues
show
Bug introduced by
The function do_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

2269
			/** @scrutinizer ignore-call */ 
2270
   do_action( $this->option_prefix . 'set_more_comment_data', $comment_id, $params, 'create' );
Loading history...
2270
		} // End if() statement.
2271
2272
		if ( is_wp_error( $comment_id ) ) {
2273
			$success = false;
2274
			$errors  = $comment_id;
2275
		} else {
2276
			$success = true;
2277
			$errors  = array();
2278
		}
2279
2280
		$result = array(
2281
			'data'   => array(
2282
				$id_field => $comment_id,
2283
				'success' => $success,
2284
			),
2285
			'errors' => $errors,
2286
		);
2287
2288
		return $result;
2289
2290
	}
2291
2292
	/**
2293
	 * Create a new WordPress comment or update it if a match is found.
2294
	 *
2295
	 * @param string $key What key we are looking at for possible matches.
2296
	 * @param string $value What value we are looking at for possible matches.
2297
	 * @param array  $methods What WordPress methods do we use to get the data, if there are any. otherwise, maybe will have to do a wpdb query.
2298
	 * @param array  $params Array of comment data params.
2299
	 * @param string $id_field Optional string of what the ID field is, if it is ever not comment_ID.
2300
	 * @param bool   $pull_to_drafts Whether to save to WordPress drafts when pulling from Salesforce.
2301
	 * @param bool   $check_only Allows this method to only check for matching records, instead of making any data changes.
2302
	 *
2303
	 * @return array
2304
	 *   data:
2305
	 *     ID : 123,
2306
	 *     success: 1
2307
	 *   "errors" : [ ],
2308
	 */
2309
	private function comment_upsert( $key, $value, $methods, $params, $id_field = 'comment_ID', $pull_to_drafts = false, $check_only = false ) {
2310
		$method = isset( $methods['method_match'] ) ? $methods['method_match'] : '';
2311
		if ( 'get_comment' === $method ) {
2312
			$method = 'get_comments';
2313
		}
2314
		if ( '' !== $method ) {
2315
2316
			// These methods should give us the comment object if we are matching for one.
2317
			// If we are trying to match to a meta field, the method is an object.
2318
			if ( class_exists( $method ) ) {
2319
				$args        = array(
2320
					'meta_query' => array(
2321
						array(
2322
							'key'   => $key,
2323
							'value' => $value,
2324
						),
2325
					),
2326
				);
2327
				$match_query = new $method( $args );
2328
				// the WP_Comment_Query object has a get_comments method that we use.
2329
				$comments = $match_query->get_comments();
2330
				if ( ! empty( $comments ) ) {
2331
					$comment = $users[0];
2332
				}
2333
			} else {
2334
				$match = array();
2335
				if ( 'comment_author' === $key ) {
2336
					$match['author__in'] = array( $value );
2337
				} else {
2338
					$key           = str_replace( 'comment_', '', $key );
2339
					$match[ $key ] = $value;
2340
				}
2341
				$comments = $method( $match );
2342
			}
2343
2344
			if ( 1 === count( $comments ) && isset( $comments ) && isset( $comments[0]->{$id_field} ) ) {
2345
				$comment = $comments[0];
2346
				// Comment does exist after checking the matching value. we want its id.
2347
				$comment_id = $comment->{$id_field};
2348
2349
				if ( true === $check_only ) {
2350
					// We are just checking to see if there is a match.
2351
					return $comment_id;
2352
				}
2353
2354
				// On the prematch fields, we specify the method_update param.
2355
				if ( isset( $methods['method_update'] ) ) {
2356
					$method = $methods['method_update'];
2357
				} else {
2358
					$method = $methods['method_modify'];
2359
				}
2360
				$params[ $key ] = array(
2361
					'value'         => $value,
2362
					'method_modify' => $method,
2363
					'method_read'   => $methods['method_read'],
2364
				);
2365
			} elseif ( count( $comments ) > 1 ) {
2366
				$status = 'error';
2367
				// Create log entry for multiple matches.
2368
				$this->logging->setup(
2369
					sprintf(
2370
						// translators: %1$s is the log status. %2$s is a number. %3$s is a key. %4$s is the value of that key. %5$s is a var_export'd array of comments.
2371
						esc_html__( '%1$s: Comments: there are %2$s comment matches for the Salesforce key %3$s with the value of %4$s. Here they are: %5$s', '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

2371
						/** @scrutinizer ignore-call */ 
2372
      esc_html__( '%1$s: Comments: there are %2$s comment matches for the Salesforce key %3$s with the value of %4$s. Here they are: %5$s', 'object-sync-for-salesforce' ),
Loading history...
2372
						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

2372
						ucfirst( /** @scrutinizer ignore-call */ esc_attr( $status ) ),
Loading history...
2373
						absint( count( $comments ) ),
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

2373
						/** @scrutinizer ignore-call */ 
2374
      absint( count( $comments ) ),
Loading history...
2374
						esc_html( $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

2374
						/** @scrutinizer ignore-call */ 
2375
      esc_html( $key ),
Loading history...
2375
						esc_html( $value ),
2376
						esc_html( var_export( $comments ) ) // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
0 ignored issues
show
Bug introduced by
Are you sure the usage of var_export($comments) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
2377
					),
2378
					'',
2379
					0,
2380
					0,
2381
					$status
2382
				);
2383
			} elseif ( false === $check_only ) {
2384
				// Comment does not exist after checking the matching value. Create it.
2385
				// On the prematch fields, we specify the method_create param.
2386
				if ( isset( $methods['method_create'] ) ) {
2387
					$method = $methods['method_create'];
2388
				} else {
2389
					$method = $methods['method_modify'];
2390
				}
2391
				$params[ $key ] = array(
2392
					'value'         => $value,
2393
					'method_modify' => $method,
2394
					'method_read'   => $methods['method_read'],
2395
				);
2396
				$result         = $this->comment_create( $params, $id_field );
2397
				return $result;
2398
			} else {
2399
				// Check only is true but there's not a comment yet.
2400
				return null;
2401
			} // End if() statement.
2402
		} else {
2403
			// There is no method by which to check the comment. We can check other ways here.
2404
			$params[ $key ] = array(
2405
				'value'         => $value,
2406
				'method_modify' => $methods['method_modify'],
2407
				'method_read'   => $methods['method_read'],
2408
			);
2409
2410
			if ( isset( $params['comment_author']['value'] ) ) {
2411
				$comment_author = $params['comment_author']['value'];
2412
			}
2413
2414
			if ( isset( $params['comment_date']['value'] ) ) {
2415
				$comment_date = $params['comment_date']['value'];
2416
			}
2417
2418
			if ( isset( $params['timezone']['value'] ) ) {
2419
				$timezone = $params['timezone']['value'];
2420
			} else {
2421
				$timezone = 'blog';
2422
			}
2423
2424
			$existing_id = comment_exists( $comment_author, $comment_date, $timezone ); // Returns an id if there is a result. Uses $wpdb->get_var, so it returns null if there is no value.
0 ignored issues
show
Bug introduced by
The function comment_exists was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

2424
			$existing_id = /** @scrutinizer ignore-call */ comment_exists( $comment_author, $comment_date, $timezone ); // Returns an id if there is a result. Uses $wpdb->get_var, so it returns null if there is no value.
Loading history...
2425
2426
			// Comment does not exist after more checking. We want to create it.
2427
			if ( null === $existing_id && false === $check_only ) {
2428
				$result = $this->comment_create( $params, $id_field );
2429
				return $result;
2430
			} elseif ( true === $check_only ) {
2431
				// We are just checking to see if there is a match.
2432
				return $existing_id;
2433
			} else {
2434
				// Comment does exist based on username, and we aren't doing a check only. we want to update the wp user here.
2435
				$comment_id = $existing_id;
2436
			}
2437
		} // End if() that sets up the parameters in the $params array.
2438
2439
		if ( isset( $comment_id ) ) {
2440
			foreach ( $params as $key => $value ) {
2441
				$params[ $key ]['method_modify'] = $methods['method_update'];
2442
			}
2443
			$result = $this->comment_update( $comment_id, $params, $id_field );
2444
			return $result;
2445
		}
2446
2447
		// Create log entry for lack of a comment id.
2448
		$status = 'error';
2449
		$title  = sprintf(
2450
			// translators: placeholders are: 1) the log status.
2451
			esc_html__( '%1$s: Comments: Tried to run comment_upsert, and ended up without a comment id', 'object-sync-for-salesforce' ),
2452
			ucfirst( esc_attr( $status ) )
2453
		);
2454
		$this->logging->setup(
2455
			$title,
2456
			'',
2457
			0,
2458
			0,
2459
			$status
2460
		);
2461
2462
	}
2463
2464
	/**
2465
	 * Update a WordPress comment.
2466
	 *
2467
	 * @param int    $comment_id The ID for the comment to be updated. This value needs to be in the array that is sent to wp_update_comment.
2468
	 * @param array  $params Array of comment data params.
2469
	 * @param string $id_field Optional string of what the ID field is, if it is ever not ID.
2470
	 *
2471
	 * @return array
2472
	 *   data:
2473
	 *     success: 1
2474
	 *   "errors" : [ ],
2475
	 */
2476
	private function comment_update( $comment_id, $params, $id_field = 'comment_ID' ) {
2477
		$content              = array();
2478
		$content[ $id_field ] = $comment_id;
2479
		foreach ( $params as $key => $value ) {
2480
			if ( 'wp_update_comment' === $value['method_modify'] ) {
2481
				$content[ $key ] = $value['value'];
2482
				unset( $params[ $key ] );
2483
			}
2484
		}
2485
2486
		$updated = wp_update_comment( $content );
0 ignored issues
show
Bug introduced by
The function wp_update_comment was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

2486
		$updated = /** @scrutinizer ignore-call */ wp_update_comment( $content );
Loading history...
2487
2488
		if ( 0 === $updated ) {
2489
			$success = false;
2490
			$errors  = $updated;
2491
		} else {
2492
			$meta_result = $this->update_wp_meta( $params, $comment_id, 'comment' );
2493
			$success     = $meta_result['success'];
2494
			$errors      = $meta_result['errors'];
2495
			// Developers can use this hook to set any other comment data.
2496
			do_action( $this->option_prefix . 'set_more_comment_data', $comment_id, $params, 'update' );
0 ignored issues
show
Bug introduced by
The function do_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

2496
			/** @scrutinizer ignore-call */ 
2497
   do_action( $this->option_prefix . 'set_more_comment_data', $comment_id, $params, 'update' );
Loading history...
2497
		} // End if() statement.
2498
2499
		if ( is_wp_error( $updated ) ) {
0 ignored issues
show
Bug introduced by
The function is_wp_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

2499
		if ( /** @scrutinizer ignore-call */ is_wp_error( $updated ) ) {
Loading history...
2500
			$success = false;
2501
			$errors  = $updated;
2502
		} else {
2503
			$success = true;
2504
			$errors  = array();
2505
		}
2506
2507
		$result = array(
2508
			'data'   => array(
2509
				$id_field => $comment_id,
2510
				'success' => $success,
2511
			),
2512
			'errors' => $errors,
2513
		);
2514
2515
		return $result;
2516
2517
	}
2518
2519
	/**
2520
	 * Delete a WordPress comment.
2521
	 *
2522
	 * @param int  $id Comment ID.
2523
	 * @param bool $force_delete If we should bypass the trash. We don't change this from FALSE anywhere in this plugin.
2524
	 *
2525
	 * @return boolean true if successful, false if failed.
2526
	 */
2527
	private function comment_delete( $id, $force_delete = false ) {
2528
		$result = wp_delete_comment( $id, $force_delete );
0 ignored issues
show
Bug introduced by
The function wp_delete_comment was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

2528
		$result = /** @scrutinizer ignore-call */ wp_delete_comment( $id, $force_delete );
Loading history...
2529
		return $result;
2530
	}
2531
2532
	/**
2533
	 * Standard method for creating meta values
2534
	 * This works for users, posts, terms, and comments. It does not work for attachments.
2535
	 *
2536
	 * @param array        $params the values to be saved.
2537
	 * @param int|wp_error $parent_object_id the WordPress object ID that this metadata is associated with. It shouldn't ever end up here as an error, but it's worth documenting.
0 ignored issues
show
Bug introduced by
The type wp_error 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...
2538
	 * @param string       $parent_object_type the WordPress object type.
2539
	 *
2540
	 * @return array $meta_result contains the success flag and the array of errors
2541
	 */
2542
	private function create_wp_meta( $params, $parent_object_id, $parent_object_type ) {
2543
		$success = true;
2544
		$errors  = array();
2545
		if ( ! is_wp_error( $parent_object_id ) && is_array( $params ) && ! empty( $params ) ) {
0 ignored issues
show
Bug introduced by
The function is_wp_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

2545
		if ( ! /** @scrutinizer ignore-call */ is_wp_error( $parent_object_id ) && is_array( $params ) && ! empty( $params ) ) {
Loading history...
2546
			foreach ( $params as $key => $value ) {
2547
2548
				// if the value is empty, skip it.
2549
				if ( '' === $value['value'] ) {
2550
					continue;
2551
				}
2552
2553
				$modify = $value['method_modify'];
2554
				// Todo: we could provide a way for passing the values in a custom order here.
2555
				$meta_id = $modify( $parent_object_id, $key, $value['value'] );
2556
				if ( false === $meta_id ) {
2557
					$success  = false;
2558
					$errors[] = array(
2559
						'message' => sprintf(
2560
							// translators: 1) is the WordPress object type, 2) is the method that should be used to save the value.
2561
							esc_html__( 'Tried to add %1$s meta with method %2$s.', '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

2561
							/** @scrutinizer ignore-call */ 
2562
       esc_html__( 'Tried to add %1$s meta with method %2$s.', 'object-sync-for-salesforce' ),
Loading history...
2562
							esc_attr( $parent_object_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

2562
							/** @scrutinizer ignore-call */ 
2563
       esc_attr( $parent_object_type ),
Loading history...
2563
							esc_html( $method )
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

2563
							/** @scrutinizer ignore-call */ 
2564
       esc_html( $method )
Loading history...
2564
						),
2565
						'key'     => $key,
2566
						'value'   => $value,
2567
					);
2568
				}
2569
			} // End foreach.
2570
		}
2571
		$meta_result = array(
2572
			'success' => $success,
2573
			'errors'  => $errors,
2574
		);
2575
		return $meta_result;
2576
	}
2577
2578
	/**
2579
	 * Standard method for updating meta values
2580
	 * This works for users, posts, terms, and comments. It does not work for attachments.
2581
	 *
2582
	 * @param array        $params the values to be saved.
2583
	 * @param int|wp_error $parent_object_id the WordPress object ID that this metadata is associated with. It shouldn't ever end up here as an error, but it's worth documenting.
2584
	 * @param string       $parent_object_type the WordPress object type.
2585
	 *
2586
	 * @return array $meta_result contains the success flag, the changed flag, and the array of errors
2587
	 */
2588
	private function update_wp_meta( $params, $parent_object_id, $parent_object_type ) {
2589
		$success = true;
2590
		$changed = false;
2591
		$errors  = array();
2592
		if ( ! is_wp_error( $parent_object_id ) && is_array( $params ) && ! empty( $params ) ) {
0 ignored issues
show
Bug introduced by
The function is_wp_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

2592
		if ( ! /** @scrutinizer ignore-call */ is_wp_error( $parent_object_id ) && is_array( $params ) && ! empty( $params ) ) {
Loading history...
2593
			$changed = true;
2594
			foreach ( $params as $key => $value ) {
2595
				$modify = $value['method_modify'];
2596
2597
				// if the value is empty, use the delete method to modify it.
2598
				if ( '' === $value['value'] ) {
2599
					$modify = isset( $value['method_delete'] ) ? $value['method_delete'] : $value['method_modify'];
2600
				}
2601
2602
				$read = $value['method_read'];
2603
				// todo: we could provide a way for passing the values in a custom order here.
2604
				$meta_id = $modify( $parent_object_id, $key, $value['value'] );
2605
				if ( false === $meta_id ) {
2606
					$changed = false;
2607
					// Check and make sure the stored value matches $value['value'], otherwise it's an error.
2608
					// In some cases, such as picklists, WordPress is dealing with an array that came from Salesforce at this point, so we need to serialize the value before assuming it's an error.
2609
2610
					if ( is_array( $value['value'] ) ) {
2611
						$new_value = maybe_serialize( $value['value'] );
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

2611
						$new_value = /** @scrutinizer ignore-call */ maybe_serialize( $value['value'] );
Loading history...
2612
					} else {
2613
						$new_value = (string) $value['value'];
2614
					}
2615
2616
					if ( is_array( $read( $parent_object_id, $key, true ) ) ) {
2617
						$stored_value = maybe_serialize( $read( $parent_object_id, $key, true ) );
2618
					} else {
2619
						$stored_value = (string) $read( $parent_object_id, $key, true );
2620
					}
2621
2622
					if ( $stored_value !== $new_value ) {
2623
						$errors[] = array(
2624
							'message' => sprintf(
2625
								// Translators: 1) is the WordPress object type, 2) is the key of the meta field, 3) is the method that should be used to update the value, 4) is the already stored value, 5) is the new value the plugin tried to save.
2626
								esc_html__( 'Unable to update %1$s meta key %2$s with method %3$s. The stored value is %4$s and the new value should be %5$s.', '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

2626
								/** @scrutinizer ignore-call */ 
2627
        esc_html__( 'Unable to update %1$s meta key %2$s with method %3$s. The stored value is %4$s and the new value should be %5$s.', 'object-sync-for-salesforce' ),
Loading history...
2627
								esc_attr( $parent_object_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

2627
								/** @scrutinizer ignore-call */ 
2628
        esc_attr( $parent_object_type ),
Loading history...
2628
								esc_attr( $key ),
2629
								esc_attr( $modify ),
2630
								wp_kses_post( $stored_value ),
0 ignored issues
show
Bug introduced by
The function wp_kses_post was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

2630
								/** @scrutinizer ignore-call */ 
2631
        wp_kses_post( $stored_value ),
Loading history...
2631
								wp_kses_post( $new_value )
2632
							),
2633
						);
2634
					}
2635
				}
2636
			} // End foreach.
2637
		}
2638
		$meta_result = array(
2639
			'success' => $success,
2640
			'changed' => $changed,
2641
			'errors'  => $errors,
2642
		);
2643
		return $meta_result;
2644
	}
2645
2646
	/**
2647
	 * Generate date formats for The Event Calendar plugin
2648
	 *
2649
	 * @param string $date the string value of the date from Salesforce.
2650
	 * @param string $type this should be start or end.
2651
	 * @param array  $content the other mapped params.
2652
	 *
2653
	 * @return array $content
2654
	 */
2655
	private function append_tec_event_dates( $date, $type, $content ) {
2656
		if ( ( 'start' === $type || 'end' === $type ) && class_exists( 'Tribe__Date_Utils' ) ) {
2657
			$dates                                      = array();
2658
			$date_type                                  = ucfirst( $type );
2659
			$timestamp                                  = strtotime( $date );
2660
			$dates[ 'Event' . $date_type . 'Date' ]     = gmdate( Tribe__Date_Utils::DBDATEFORMAT, $timestamp );
0 ignored issues
show
Bug introduced by
The type Tribe__Date_Utils 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...
2661
			$dates[ 'Event' . $date_type . 'Hour' ]     = gmdate( Tribe__Date_Utils::HOURFORMAT, $timestamp );
2662
			$dates[ 'Event' . $date_type . 'Minute' ]   = gmdate( Tribe__Date_Utils::MINUTEFORMAT, $timestamp );
2663
			$dates[ 'Event' . $date_type . 'Meridian' ] = gmdate( Tribe__Date_Utils::MERIDIANFORMAT, $timestamp );
2664
			$content                                    = $content + $dates;
2665
		}
2666
		return $content;
2667
	}
2668
2669
	/**
2670
	 * Generate an edit link for the WordPress record.
2671
	 *
2672
	 * @param string $object_type the type of WordPress object.
2673
	 * @param int    $wordpress_id the ID of the WordPress object.
2674
	 *
2675
	 * @return string $edit_link
2676
	 */
2677
	public function object_edit_link( $object_type, $wordpress_id ) {
2678
		$edit_link = '';
2679
		if ( 'user' === $object_type ) {
2680
			$edit_link = get_edit_user_link( $wordpress_id );
0 ignored issues
show
Bug introduced by
The function get_edit_user_link was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

2680
			$edit_link = /** @scrutinizer ignore-call */ get_edit_user_link( $wordpress_id );
Loading history...
2681
		} elseif ( 'post' === $object_type || 'page' === $object_type || 'attachment' === $object_type ) {
2682
			$edit_link = get_edit_post_link( $wordpress_id );
0 ignored issues
show
Bug introduced by
The function get_edit_post_link was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

2682
			$edit_link = /** @scrutinizer ignore-call */ get_edit_post_link( $wordpress_id );
Loading history...
2683
		} elseif ( 'category' === $object_type || 'tag' === $object_type || 'post_tag' === $object_type ) {
2684
			$edit_link = get_edit_term_link( $wordpress_id );
0 ignored issues
show
Bug introduced by
The function get_edit_term_link was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

2684
			$edit_link = /** @scrutinizer ignore-call */ get_edit_term_link( $wordpress_id );
Loading history...
2685
		} elseif ( 'comment' === $object_type ) {
2686
			$edit_link = get_edit_comment_link( $wordpress_id );
0 ignored issues
show
Bug introduced by
The function get_edit_comment_link was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

2686
			$edit_link = /** @scrutinizer ignore-call */ get_edit_comment_link( $wordpress_id );
Loading history...
2687
		} else { // This is for custom post types.
2688
			$edit_link = get_edit_post_link( $wordpress_id );
2689
		} // End if() statement.
2690
		return $edit_link;
2691
	}
2692
2693
	/**
2694
	 * Generate a delete link for the WordPress record.
2695
	 *
2696
	 * @param string $object_type the type of WordPress object.
2697
	 * @param int    $wordpress_id the ID of the WordPress object.
2698
	 *
2699
	 * @return string $delete_link
2700
	 */
2701
	public function object_delete_link( $object_type, $wordpress_id ) {
2702
		$delete_link = '';
2703
		if ( 'post' === $object_type || 'page' === $object_type || 'attachment' === $object_type ) {
2704
			$delete_link = get_delete_post_link( $wordpress_id );
0 ignored issues
show
Bug introduced by
The function get_delete_post_link was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

2704
			$delete_link = /** @scrutinizer ignore-call */ get_delete_post_link( $wordpress_id );
Loading history...
2705
		} else { // This is for custom post types.
2706
			$delete_link = get_delete_post_link( $wordpress_id );
2707
		} // End if() statement.
2708
		return $delete_link;
2709
	}
2710
2711
}
2712