Completed
Push — master ( a5d7b8...eadf7b )
by Jonathan
06:36 queued 03:42
created

Object_Sync_Sf_Logging   F

Complexity

Total Complexity 66

Size/Duplication

Total Lines 588
Duplicated Lines 0 %

Importance

Changes 12
Bugs 1 Features 1
Metric Value
eloc 224
c 12
b 1
f 1
dl 0
loc 588
rs 3.12
wmc 66

22 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 2
A sortable_columns() 0 3 1
A set_log_visibility() 0 22 1
A type_column() 0 3 1
A type_column_content() 0 14 3
A init() 0 24 2
A set_log_slug() 0 5 2
A add_prune_interval() 0 14 1
B posts_filter() 0 13 7
B check_log_schedule() 0 25 6
A set_prune_option() 0 4 1
A add() 0 10 1
A get_schedule_frequency() 0 21 4
B restrict_log_posts() 0 27 6
A get_connected_logs() 0 33 4
A get_log_count() 0 28 4
A set_prune_age() 0 6 2
B setup() 0 27 10
A save_log_schedule() 0 11 5
A set_log_types() 0 3 1
A set_prune_args() 0 3 1
A get_logs() 0 6 1

How to fix   Complexity   

Complex Class

Complex classes like Object_Sync_Sf_Logging often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Object_Sync_Sf_Logging, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Class file for the Object_Sync_Sf_Logging class. Extend the WP_Logging class for the purposes of Object Sync for Salesforce.
4
 *
5
 * @file
6
 */
7
8
if ( ! class_exists( 'Object_Sync_Salesforce' ) ) {
9
	die();
10
}
11
12
/**
13
 * Log events based on plugin settings
14
 */
15
class Object_Sync_Sf_Logging extends WP_Logging {
16
17
	protected $wpdb;
18
	protected $version;
19
	protected $slug;
20
	protected $option_prefix;
21
22
	public $enabled;
23
	public $statuses_to_log;
24
25
	private $schedule_name;
26
27
28
	/**
29
	 * Constructor which sets content type and pruning for logs
30
	 *
31
	 * @param object $wpdb An instance of the wpdb class.
32
	 * @param string $version The version of this plugin.
33
	 * @param string $slug The plugin slug
34
	 * @param string $option_prefix The plugin's option prefix
35
	 * @throws \Exception
36
	 */
37
	public function __construct( $wpdb, $version, $slug = '', $option_prefix = '' ) {
38
		$this->wpdb          = $wpdb;
39
		$this->version       = $version;
40
		$this->slug          = $slug;
41
		$this->option_prefix = isset( $option_prefix ) ? $option_prefix : 'object_sync_for_salesforce_';
42
43
		$this->enabled         = get_option( $this->option_prefix . 'enable_logging', false );
44
		$this->statuses_to_log = get_option( $this->option_prefix . 'statuses_to_log', array() );
45
46
		$this->schedule_name = 'wp_logging_prune_routine';
47
48
		$this->capability = 'configure_salesforce';
0 ignored issues
show
Bug Best Practice introduced by
The property capability does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
49
50
		$this->init();
51
52
	}
53
54
	/**
55
	 * Start. This creates a schedule for pruning logs, and also the custom content type
56
	 *
57
	 * @throws \Exception
58
	 */
59
	private function init() {
60
		if ( true === filter_var( $this->enabled, FILTER_VALIDATE_BOOLEAN ) ) {
61
			add_filter( 'cron_schedules', array( $this, 'add_prune_interval' ) );
62
			add_filter( 'wp_log_types', array( $this, 'set_log_types' ), 10, 1 );
63
			add_filter( 'wp_logging_should_we_prune', array( $this, 'set_prune_option' ), 10, 1 );
64
			add_filter( 'wp_logging_prune_when', array( $this, 'set_prune_age' ), 10, 1 );
65
			add_filter( 'wp_logging_prune_query_args', array( $this, 'set_prune_args' ), 10, 1 );
66
			add_filter( 'wp_logging_post_type_args', array( $this, 'set_log_visibility' ), 10, 1 );
67
			add_filter( 'pre_wp_unique_post_slug', array( $this, 'set_log_slug' ), 10, 5 );
68
69
			// add a sortable Type column to the posts admin
70
			add_filter( 'manage_edit-wp_log_columns', array( $this, 'type_column' ), 10, 1 );
71
			add_filter( 'manage_edit-wp_log_sortable_columns', array( $this, 'sortable_columns' ), 10, 1 );
72
			add_action( 'manage_wp_log_posts_custom_column', array( $this, 'type_column_content' ), 10, 2 );
73
74
			// filter the log posts admin by log type
75
			add_filter( 'parse_query', array( $this, 'posts_filter' ), 10, 1 );
76
			add_action( 'restrict_manage_posts', array( $this, 'restrict_log_posts' ) );
77
78
			// when the schedule might change
79
			add_action( 'update_option_' . $this->option_prefix . 'logs_how_often_unit', array( $this, 'check_log_schedule' ), 10, 3 );
80
			add_action( 'update_option_' . $this->option_prefix . 'logs_how_often_number', array( $this, 'check_log_schedule' ), 10, 3 );
81
82
			$this->save_log_schedule();
83
		}
84
	}
85
86
	/**
87
	 * Set visibility for the post type
88
	 *
89
	 * @param array $log_args The post arguments
90
	 * @return array $log_args
91
	 */
92
	public function set_log_visibility( $log_args ) {
93
		// set public to true overrides the WP_DEBUG setting that is the default on the class
94
		// capabilities makes it so (currently) only admin users can see the log posts in their admin view
95
		// note: a public value of true is required to show Logs as a nav menu item on the admin.
96
		// however, if we don't set exclude_from_search to true and publicly_queryable to false, logs *can* appear in search results
97
		$log_args['public']              = true;
98
		$log_args['publicly_queryable']  = false;
99
		$log_args['exclude_from_search'] = true;
100
		$log_args['capabilities']        = array(
101
			'edit_post'          => $this->capability,
102
			'read_post'          => $this->capability,
103
			'delete_post'        => $this->capability,
104
			'edit_posts'         => $this->capability,
105
			'edit_others_posts'  => $this->capability,
106
			'delete_posts'       => $this->capability,
107
			'publish_posts'      => $this->capability,
108
			'read_private_posts' => $this->capability,
109
		);
110
111
		$log_args = apply_filters( $this->option_prefix . 'logging_post_type_args', $log_args );
112
113
		return $log_args;
114
	}
115
116
	/**
117
	 * Create a (probably unique) post name for logs in a more performant manner than wp_unique_post_slug().
118
	 *
119
	 * @param string $override_slug Short-circuit return value.
120
	 * @param string $slug The desired slug (post_name).
121
	 * @param int $post_ID The post ID
122
	 * @param string $post_status The post status
123
	 * @param string $post_type The post type
124
	 * @return string
125
	 */
126
	public function set_log_slug( $override_slug, $slug, $post_ID, $post_status, $post_type ) {
127
		if ( 'wp_log' === $post_type ) {
128
			$override_slug = uniqid( $post_type . '-', true ) . '-' . wp_generate_password( 32, false );
129
		}
130
		return $override_slug;
131
	}
132
133
	/**
134
	 * Add a Type column to the posts admin for this post type
135
	 *
136
	 * @param array $columns
137
	 * @return array $columns
138
	 */
139
	public function type_column( $columns ) {
140
		$columns['type'] = __( 'Type', 'object-sync-for-salesforce' );
141
		return $columns;
142
	}
143
144
	/**
145
	 * Make the Type column in the posts admin for this post type sortable
146
	 *
147
	 * @param array $columns
148
	 * @return array $columns
149
	 */
150
	public function sortable_columns( $columns ) {
151
		$columns['type'] = 'type';
152
		return $columns;
153
	}
154
155
	/**
156
	 * Add the content for the Type column in the posts admin for this post type
157
	 *
158
	 * @param string $column_name
159
	 * @param int $post_id
160
	 */
161
	public function type_column_content( $column_name, $post_id ) {
162
		if ( 'type' != $column_name ) {
163
			return;
164
		}
165
		// get wp_log_type
166
		$terms = wp_get_post_terms(
167
			$post_id,
168
			'wp_log_type',
169
			array(
170
				'fields' => 'names',
171
			)
172
		);
173
		if ( is_array( $terms ) ) {
0 ignored issues
show
introduced by
The condition is_array($terms) is always false.
Loading history...
174
			echo esc_attr( $terms[0] );
175
		}
176
	}
177
178
	/**
179
	 * Filter log posts by the taxonomy from the dropdown when a value is present
180
	 *
181
	 * @param object $query
182
	 * @return object $query
183
	 */
184
	public function posts_filter( $query ) {
185
		global $pagenow;
186
		$type     = 'wp_log';
187
		$taxonomy = 'wp_log_type';
188
		if ( is_admin() && 'edit.php' === $pagenow ) {
189
			if ( isset( $_GET['post_type'] ) && esc_attr( $_GET['post_type'] ) === $type ) {
190
				if ( isset( $_GET[ $taxonomy ] ) && '' !== $_GET[ $taxonomy ] ) {
191
					$query->post_type = $type;
192
					$query->tax_query = array(
193
						array(
194
							'taxonomy' => $taxonomy,
195
							'field'    => 'slug',
196
							'terms'    => esc_attr( $_GET[ $taxonomy ] ),
197
						),
198
					);
199
				}
200
			}
201
		}
202
	}
203
204
	/**
205
	 * Add a filter form for the log admin so we can filter by wp_log_type taxonomy values
206
	 *
207
	 * @param object $query
208
	 * @return object $query
209
	 */
210
	public function restrict_log_posts() {
211
		$type     = 'wp_log';
212
		$taxonomy = 'wp_log_type';
213
		// only add filter to post type you want
214
		if ( isset( $_GET['post_type'] ) && esc_attr( $_GET['post_type'] ) === $type ) {
215
			// get wp_log_type
216
			$terms = get_terms(
217
				[
218
					'taxonomy'   => $taxonomy,
219
					'hide_empty' => true,
220
				]
221
			);
222
			?>
223
			<select name="wp_log_type">
224
				<option value=""><?php _e( 'All log types ', 'object-sync-for-salesforce' ); ?></option>
225
				<?php
226
				$current_log_type = isset( $_GET[ $taxonomy ] ) ? esc_attr( $_GET[ $taxonomy ] ) : '';
227
				foreach ( $terms as $key => $term ) {
228
					printf(
229
						'<option value="%s"%s>%s</option>',
230
						$term->slug,
231
						$term->slug == $current_log_type ? ' selected="selected"' : '',
232
						$term->name
233
					);
234
				}
235
				?>
236
			</select>
237
			<?php
238
		}
239
	}
240
241
	/**
242
	 * When the cron settings change, clear the relevant schedule
243
	 *
244
	 * @param mixed $old_value Previous option value
245
	 * @param mixed $new_value New option value
246
	 * @param string $option Name of option
247
	 */
248
	public function check_log_schedule( $old_value, $new_value, $option ) {
249
		$clear_schedule  = false;
250
		$schedule_unit   = get_option( $this->option_prefix . 'logs_how_often_unit', '' );
251
		$schedule_number = get_option( $this->option_prefix . 'logs_how_often_number', '' );
252
		if ( $this->option_prefix . 'logs_how_often_unit' === $option ) {
253
			$old_frequency = $this->get_schedule_frequency( $old_value, $schedule_number );
0 ignored issues
show
Bug introduced by
It seems like $schedule_number can also be of type false and string; however, parameter $number of Object_Sync_Sf_Logging::get_schedule_frequency() does only seem to accept double|integer, maybe add an additional type check? ( Ignorable by Annotation )

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

253
			$old_frequency = $this->get_schedule_frequency( $old_value, /** @scrutinizer ignore-type */ $schedule_number );
Loading history...
254
			$new_frequency = $this->get_schedule_frequency( $new_value, $schedule_number );
255
			$old_key       = $old_frequency['key'];
256
			$new_key       = $new_frequency['key'];
257
			if ( $old_key !== $new_key ) {
258
				$clear_schedule = true;
259
			}
260
		}
261
		if ( $this->option_prefix . 'logs_how_often_number' === $option ) {
262
			$old_frequency = $this->get_schedule_frequency( $schedule_unit, $old_value );
0 ignored issues
show
Bug introduced by
It seems like $schedule_unit can also be of type false; however, parameter $unit of Object_Sync_Sf_Logging::get_schedule_frequency() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

262
			$old_frequency = $this->get_schedule_frequency( /** @scrutinizer ignore-type */ $schedule_unit, $old_value );
Loading history...
263
			$new_frequency = $this->get_schedule_frequency( $schedule_unit, $new_value );
264
			$old_key       = $old_frequency['key'];
265
			$new_key       = $new_frequency['key'];
266
			if ( $old_key !== $new_key ) {
267
				$clear_schedule = true;
268
			}
269
		}
270
		if ( true === $clear_schedule ) {
271
			wp_clear_scheduled_hook( $this->schedule_name );
272
			$this->save_log_schedule();
273
		}
274
	}
275
276
	/**
277
	 * Save a cron schedule
278
	 *
279
	 */
280
	public function save_log_schedule() {
281
		global $pagenow;
282
		if ( ( 'options.php' !== $pagenow ) && ( ! isset( $_GET['page'] ) || $this->slug . '-admin' !== $_GET['page'] ) ) {
283
			return;
284
		}
285
		$schedule_unit   = get_option( $this->option_prefix . 'logs_how_often_unit', '' );
286
		$schedule_number = get_option( $this->option_prefix . 'logs_how_often_number', '' );
287
		$frequency       = $this->get_schedule_frequency( $schedule_unit, $schedule_number );
0 ignored issues
show
Bug introduced by
It seems like $schedule_number can also be of type false and string; however, parameter $number of Object_Sync_Sf_Logging::get_schedule_frequency() does only seem to accept double|integer, maybe add an additional type check? ( Ignorable by Annotation )

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

287
		$frequency       = $this->get_schedule_frequency( $schedule_unit, /** @scrutinizer ignore-type */ $schedule_number );
Loading history...
Bug introduced by
It seems like $schedule_unit can also be of type false; however, parameter $unit of Object_Sync_Sf_Logging::get_schedule_frequency() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

287
		$frequency       = $this->get_schedule_frequency( /** @scrutinizer ignore-type */ $schedule_unit, $schedule_number );
Loading history...
288
		$key             = $frequency['key'];
289
		if ( ! wp_next_scheduled( $this->schedule_name ) ) {
290
			wp_schedule_event( time(), $key, $this->schedule_name );
291
		}
292
	}
293
294
	/**
295
	 * Add interval to wp schedules based on admin settings
296
	 *
297
	 * @param array $schedules An array of scheduled cron items.
298
	 * @return array $frequency
299
	 */
300
	public function add_prune_interval( $schedules ) {
301
302
		$schedule_unit   = get_option( $this->option_prefix . 'logs_how_often_unit', '' );
303
		$schedule_number = get_option( $this->option_prefix . 'logs_how_often_number', '' );
304
		$frequency       = $this->get_schedule_frequency( $schedule_unit, $schedule_number );
0 ignored issues
show
Bug introduced by
It seems like $schedule_unit can also be of type false; however, parameter $unit of Object_Sync_Sf_Logging::get_schedule_frequency() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

304
		$frequency       = $this->get_schedule_frequency( /** @scrutinizer ignore-type */ $schedule_unit, $schedule_number );
Loading history...
Bug introduced by
It seems like $schedule_number can also be of type false and string; however, parameter $number of Object_Sync_Sf_Logging::get_schedule_frequency() does only seem to accept double|integer, maybe add an additional type check? ( Ignorable by Annotation )

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

304
		$frequency       = $this->get_schedule_frequency( $schedule_unit, /** @scrutinizer ignore-type */ $schedule_number );
Loading history...
305
		$key             = $frequency['key'];
306
		$seconds         = $frequency['seconds'];
307
308
		$schedules[ $key ] = array(
309
			'interval' => $seconds * $schedule_number,
310
			'display'  => 'Every ' . $schedule_number . ' ' . $schedule_unit,
0 ignored issues
show
Bug introduced by
Are you sure $schedule_unit of type false|mixed|string can be used in concatenation? ( Ignorable by Annotation )

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

310
			'display'  => 'Every ' . $schedule_number . ' ' . /** @scrutinizer ignore-type */ $schedule_unit,
Loading history...
Bug introduced by
Are you sure $schedule_number of type false|mixed|string can be used in concatenation? ( Ignorable by Annotation )

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

310
			'display'  => 'Every ' . /** @scrutinizer ignore-type */ $schedule_number . ' ' . $schedule_unit,
Loading history...
311
		);
312
313
		return $schedules;
314
315
	}
316
317
	/**
318
	 * Convert the schedule frequency from the admin settings into an array
319
	 * interval must be in seconds for the class to use it
320
	 *
321
	 * @param string $unit A unit of time.
322
	 * @param number $number The number of those units.
323
	 * @return array
324
	 */
325
	public function get_schedule_frequency( $unit, $number ) {
326
327
		switch ( $unit ) {
328
			case 'minutes':
329
				$seconds = 60;
330
				break;
331
			case 'hours':
332
				$seconds = 3600;
333
				break;
334
			case 'days':
335
				$seconds = 86400;
336
				break;
337
			default:
338
				$seconds = 0;
339
		}
340
341
		$key = $unit . '_' . $number;
342
343
		return array(
344
			'key'     => $key,
345
			'seconds' => $seconds,
346
		);
347
348
	}
349
350
	/**
351
	 * Set terms for Salesforce logs
352
	 *
353
	 * @param array $terms An array of string log types in the WP_Logging class.
354
	 * @return array $terms
355
	 */
356
	public function set_log_types( $terms ) {
357
		$terms[] = 'salesforce';
358
		return $terms;
359
	}
360
361
	/**
362
	 * Should logs be pruned at all?
363
	 *
364
	 * @param string $should_we_prune Whether to prune old log items.
365
	 * @return string $should_we_prune Whether to prune old log items.
366
	 */
367
	public function set_prune_option( $should_we_prune ) {
368
		$should_we_prune = get_option( $this->option_prefix . 'prune_logs', $should_we_prune );
369
		$should_we_prune = filter_var( $should_we_prune, FILTER_VALIDATE_BOOLEAN );
370
		return $should_we_prune;
371
	}
372
373
	/**
374
	 * Set how often to prune the Salesforce logs
375
	 *
376
	 * @param string $how_old How old the oldest non-pruned log items should be allowed to be.
377
	 * @return string $how_old
378
	 */
379
	public function set_prune_age( $how_old ) {
380
		$value = get_option( $this->option_prefix . 'logs_how_old', '' ) . ' ago';
0 ignored issues
show
Bug introduced by
Are you sure get_option($this->option...x . 'logs_how_old', '') of type false|mixed|string can be used in concatenation? ( Ignorable by Annotation )

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

380
		$value = /** @scrutinizer ignore-type */ get_option( $this->option_prefix . 'logs_how_old', '' ) . ' ago';
Loading history...
381
		if ( '' !== $value ) {
382
			return $value;
383
		} else {
384
			return $how_old;
385
		}
386
	}
387
388
	/**
389
	 * Set arguments for only getting the Salesforce logs
390
	 *
391
	 * @param array $args Argument array for get_posts determining what posts are eligible for pruning.
392
	 * @return array $args
393
	 */
394
	public function set_prune_args( $args ) {
395
		$args['wp_log_type'] = 'salesforce';
396
		return $args;
397
	}
398
399
	/**
400
	 * Setup new log entry
401
	 *
402
	 * Check and see if we should log anything, and if so, send it to add()
403
	 *
404
	 * @access      public
405
	 * @since       1.0
406
	 *
407
	 * @param       string|array $title_or_params A log post title, or the full array of parameters
408
	 * @param       string $message The log message.
409
	 * @param       string|0 $trigger The type of log triggered. Usually one of: debug, notice, warning, error.
0 ignored issues
show
Documentation Bug introduced by
The doc comment string|0 at position 2 could not be parsed: Unknown type name '0' at position 2 in string|0.
Loading history...
410
	 * @param       int $parent The parent WordPress object.
411
	 * @param       string $status The log status.
412
	 *
413
	 * @uses        self::add()
414
	 * @see         Object_Sync_Sf_Mapping::__construct()    the location of the bitmasks that define the logging triggers.
415
	 *
416
	 * @return      void
417
	 */
418
	public function setup( $title_or_params, $message = '', $trigger = 0, $parent = 0, $status = '' ) {
419
420
		if ( is_array( $title_or_params ) ) {
421
			$title   = $title_or_params['title'];
422
			$message = $title_or_params['message'];
423
			$trigger = $title_or_params['trigger'];
424
			$parent  = $title_or_params['parent'];
425
			$status  = $title_or_params['status'];
426
		} else {
427
			$title = $title_or_params;
428
		}
429
430
		if ( ! is_array( maybe_unserialize( $this->statuses_to_log ) ) ) {
0 ignored issues
show
Bug introduced by
It seems like $this->statuses_to_log can also be of type array; however, parameter $original of maybe_unserialize() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

430
		if ( ! is_array( maybe_unserialize( /** @scrutinizer ignore-type */ $this->statuses_to_log ) ) ) {
Loading history...
431
			if ( $status === $this->statuses_to_log ) {
432
				$this->add( $title, $message, $parent );
433
			} else {
434
				return;
435
			}
436
		}
437
438
		if ( true === filter_var( $this->enabled, FILTER_VALIDATE_BOOLEAN ) && in_array( $status, maybe_unserialize( $this->statuses_to_log ), true ) ) {
0 ignored issues
show
Bug introduced by
It seems like maybe_unserialize($this->statuses_to_log) can also be of type boolean; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

438
		if ( true === filter_var( $this->enabled, FILTER_VALIDATE_BOOLEAN ) && in_array( $status, /** @scrutinizer ignore-type */ maybe_unserialize( $this->statuses_to_log ), true ) ) {
Loading history...
439
			$triggers_to_log = get_option( $this->option_prefix . 'triggers_to_log', array() );
440
			// if we force strict on this in_array, it fails because the mapping triggers are bit operators, as indicated in Object_Sync_Sf_Mapping class's method __construct()
441
			if ( in_array( $trigger, maybe_unserialize( $triggers_to_log ) ) || 0 === $trigger ) {
0 ignored issues
show
Bug introduced by
It seems like maybe_unserialize($triggers_to_log) can also be of type false; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

441
			if ( in_array( $trigger, /** @scrutinizer ignore-type */ maybe_unserialize( $triggers_to_log ) ) || 0 === $trigger ) {
Loading history...
442
				$this->add( $title, $message, $parent );
443
			} elseif ( is_array( $trigger ) && array_intersect( $trigger, maybe_unserialize( $triggers_to_log ) ) ) {
0 ignored issues
show
Bug introduced by
It seems like maybe_unserialize($triggers_to_log) can also be of type false; however, parameter $array2 of array_intersect() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

443
			} elseif ( is_array( $trigger ) && array_intersect( $trigger, /** @scrutinizer ignore-type */ maybe_unserialize( $triggers_to_log ) ) ) {
Loading history...
444
				$this->add( $title, $message, $parent );
445
			}
446
		}
447
	}
448
449
	/**
450
	 * Create new log entry
451
	 *
452
	 * This is just a simple and fast way to log something. Use self::insert_log()
453
	 * if you need to store custom meta data
454
	 *
455
	 * @access      public
456
	 * @since       1.0
457
	 *
458
	 * @param       string $title A log post title.
459
	 *
460
	 * @uses        self::insert_log()
461
	 * @param       string $message The log message.
462
	 * @param       int $parent The parent WordPress object.
463
	 * @param       string $type The type of log message; defaults to 'salesforce'.
464
	 *
465
	 * @return      int The ID of the new log entry
466
	 */
467
	public static function add( $title = '', $message = '', $parent = 0, $type = 'salesforce' ) {
468
469
		$log_data = array(
470
			'post_title'   => esc_html( $title ),
471
			'post_content' => wp_kses_post( $message ),
472
			'post_parent'  => absint( $parent ),
473
			'log_type'     => esc_attr( $type ),
474
		);
475
476
		return self::insert_log( $log_data );
477
478
	}
479
480
481
	/**
482
	 * Easily retrieves log items for a particular object ID
483
	 *
484
	 * @access      private
485
	 * @since       1.0
486
	 *
487
	 * @param       int $object_id A WordPress object ID.
488
	 * @param       string $type The type of log item; defaults to 'salesforce' because that's the type of logs we create.
489
	 * @param       int $paged Which page of results do we want?
490
	 *
491
	 * @uses        self::get_connected_logs()
492
	 *
493
	 * @return      array
494
	 */
495
	public static function get_logs( $object_id = 0, $type = 'salesforce', $paged = null ) {
496
		return self::get_connected_logs(
497
			array(
498
				'post_parent' => (int) $object_id,
499
				'paged'       => (int) $paged,
500
				'log_type'    => (string) $type,
501
			)
502
		);
503
	}
504
505
506
	/**
507
	 * Retrieve all connected logs
508
	 *
509
	 * Used for retrieving logs related to particular items, such as a specific purchase.
510
	 *
511
	 * @access  private
512
	 * @since   1.0
513
	 *
514
	 * @param   Array $args An array of arguments for get_posts().
515
	 *
516
	 * @uses    wp_parse_args()
517
	 * @uses    get_posts()
518
	 * @uses    get_query_var()
519
	 * @uses    self::valid_type()
520
	 *
521
	 * @return  array / false
522
	 */
523
	public static function get_connected_logs( $args = array() ) {
524
525
		$defaults = array(
526
			'post_parent'    => 0,
527
			'post_type'      => 'wp_log',
528
			'posts_per_page' => 10,
529
			'post_status'    => 'publish',
530
			'paged'          => get_query_var( 'paged' ),
531
			'log_type'       => 'salesforce',
532
		);
533
534
		$query_args = wp_parse_args( $args, $defaults );
535
536
		if ( $query_args['log_type'] && self::valid_type( $query_args['log_type'] ) ) {
537
538
			$query_args['tax_query'] = array(
539
				array(
540
					'taxonomy' => 'wp_log_type',
541
					'field'    => 'slug',
542
					'terms'    => $query_args['log_type'],
543
				),
544
			);
545
546
		}
547
548
		$logs = get_posts( $query_args );
549
550
		if ( $logs ) {
551
			return $logs;
552
		}
553
554
		// no logs found.
555
		return false;
556
557
	}
558
559
560
	/**
561
	 * Retrieves number of log entries connected to particular object ID
562
	 *
563
	 * @access  private
564
	 * @since   1.0
565
	 *
566
	 * @param       int $object_id A WordPress object ID.
567
	 * @param       string $type The type of log item; defaults to 'salesforce' because that's the type of logs we create.
568
	 * @param       Array $meta_query A WordPress meta query, parseable by WP_Meta_Query.
569
	 *
570
	 * @uses    WP_Query()
571
	 * @uses    self::valid_type()
572
	 *
573
	 * @return  int
574
	 */
575
	public static function get_log_count( $object_id = 0, $type = 'salesforce', $meta_query = null ) {
576
577
		$query_args = array(
578
			'post_parent'    => (int) $object_id,
579
			'post_type'      => 'wp_log',
580
			'posts_per_page' => 100,
581
			'post_status'    => 'publish',
582
		);
583
584
		if ( ! empty( $type ) && self::valid_type( $type ) ) {
585
586
			$query_args['tax_query'] = array(
587
				array(
588
					'taxonomy' => 'wp_log_type',
589
					'field'    => 'slug',
590
					'terms'    => sanitize_key( $type ),
591
				),
592
			);
593
594
		}
595
596
		if ( ! empty( $meta_query ) ) {
597
			$query_args['meta_query'] = $meta_query;
598
		}
599
600
		$logs = new WP_Query( $query_args );
601
602
		return (int) $logs->post_count;
603
604
	}
605
606
}
607