Completed
Pull Request — master (#288)
by Jonathan
04:58 queued 02:09
created

Object_Sync_Sf_Logging   F

Complexity

Total Complexity 66

Size/Duplication

Total Lines 584
Duplicated Lines 0 %

Importance

Changes 11
Bugs 1 Features 1
Metric Value
eloc 222
c 11
b 1
f 1
dl 0
loc 584
rs 3.12
wmc 66

22 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 2
A add_prune_interval() 0 14 1
B posts_filter() 0 11 7
B check_log_schedule() 0 25 6
A set_prune_option() 0 4 1
A add() 0 10 1
A sortable_columns() 0 3 1
A get_schedule_frequency() 0 21 4
B restrict_log_posts() 0 27 6
A get_connected_logs() 0 33 4
A set_log_visibility() 0 22 1
A type_column() 0 3 1
A type_column_content() 0 14 3
A get_log_count() 0 28 4
A set_prune_age() 0 6 2
A init() 0 24 2
A set_log_slug() 0 5 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() && isset( $_GET['post_type'] ) && esc_attr( $_GET['post_type'] ) === $type && 'edit.php' === $pagenow && isset( $_GET[ $taxonomy ] ) && '' !== $_GET[ $taxonomy ] ) {
189
			$query->post_type = $type;
190
			$query->tax_query = array(
191
				array(
192
					'taxonomy' => $taxonomy,
193
					'field'    => 'slug',
194
					'terms'    => esc_attr( $_GET[ $taxonomy ] ),
195
				),
196
			);
197
		}
198
	}
199
200
	/**
201
	 * Add a filter form for the log admin so we can filter by wp_log_type taxonomy values
202
	 *
203
	 * @param object $query
204
	 * @return object $query
205
	 */
206
	public function restrict_log_posts() {
207
		$type     = 'wp_log';
208
		$taxonomy = 'wp_log_type';
209
		// only add filter to post type you want
210
		if ( isset( $_GET['post_type'] ) && esc_attr( $_GET['post_type'] ) === $type ) {
211
			// get wp_log_type
212
			$terms = get_terms(
213
				[
214
					'taxonomy'   => $taxonomy,
215
					'hide_empty' => true,
216
				]
217
			);
218
			?>
219
			<select name="wp_log_type">
220
				<option value=""><?php _e( 'All log types ', 'object-sync-for-salesforce' ); ?></option>
221
				<?php
222
				$current_log_type = isset( $_GET[ $taxonomy ] ) ? esc_attr( $_GET[ $taxonomy ] ) : '';
223
				foreach ( $terms as $key => $term ) {
224
					printf(
225
						'<option value="%s"%s>%s</option>',
226
						$term->slug,
227
						$term->slug == $current_log_type ? ' selected="selected"' : '',
228
						$term->name
229
					);
230
				}
231
				?>
232
			</select>
233
			<?php
234
		}
235
	}
236
237
	/**
238
	 * When the cron settings change, clear the relevant schedule
239
	 *
240
	 * @param mixed $old_value Previous option value
241
	 * @param mixed $new_value New option value
242
	 * @param string $option Name of option
243
	 */
244
	public function check_log_schedule( $old_value, $new_value, $option ) {
245
		$clear_schedule  = false;
246
		$schedule_unit   = get_option( $this->option_prefix . 'logs_how_often_unit', '' );
247
		$schedule_number = get_option( $this->option_prefix . 'logs_how_often_number', '' );
248
		if ( $this->option_prefix . 'logs_how_often_unit' === $option ) {
249
			$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

249
			$old_frequency = $this->get_schedule_frequency( $old_value, /** @scrutinizer ignore-type */ $schedule_number );
Loading history...
250
			$new_frequency = $this->get_schedule_frequency( $new_value, $schedule_number );
251
			$old_key       = $old_frequency['key'];
252
			$new_key       = $new_frequency['key'];
253
			if ( $old_key !== $new_key ) {
254
				$clear_schedule = true;
255
			}
256
		}
257
		if ( $this->option_prefix . 'logs_how_often_number' === $option ) {
258
			$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

258
			$old_frequency = $this->get_schedule_frequency( /** @scrutinizer ignore-type */ $schedule_unit, $old_value );
Loading history...
259
			$new_frequency = $this->get_schedule_frequency( $schedule_unit, $new_value );
260
			$old_key       = $old_frequency['key'];
261
			$new_key       = $new_frequency['key'];
262
			if ( $old_key !== $new_key ) {
263
				$clear_schedule = true;
264
			}
265
		}
266
		if ( true === $clear_schedule ) {
267
			wp_clear_scheduled_hook( $this->schedule_name );
268
			$this->save_log_schedule();
269
		}
270
	}
271
272
	/**
273
	 * Save a cron schedule
274
	 *
275
	 */
276
	public function save_log_schedule() {
277
		global $pagenow;
278
		if ( ( 'options.php' !== $pagenow ) && ( ! isset( $_GET['page'] ) || $this->slug . '-admin' !== $_GET['page'] ) ) {
279
			return;
280
		}
281
		$schedule_unit   = get_option( $this->option_prefix . 'logs_how_often_unit', '' );
282
		$schedule_number = get_option( $this->option_prefix . 'logs_how_often_number', '' );
283
		$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

283
		$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

283
		$frequency       = $this->get_schedule_frequency( /** @scrutinizer ignore-type */ $schedule_unit, $schedule_number );
Loading history...
284
		$key             = $frequency['key'];
285
		if ( ! wp_next_scheduled( $this->schedule_name ) ) {
286
			wp_schedule_event( time(), $key, $this->schedule_name );
287
		}
288
	}
289
290
	/**
291
	 * Add interval to wp schedules based on admin settings
292
	 *
293
	 * @param array $schedules An array of scheduled cron items.
294
	 * @return array $frequency
295
	 */
296
	public function add_prune_interval( $schedules ) {
297
298
		$schedule_unit   = get_option( $this->option_prefix . 'logs_how_often_unit', '' );
299
		$schedule_number = get_option( $this->option_prefix . 'logs_how_often_number', '' );
300
		$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

300
		$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

300
		$frequency       = $this->get_schedule_frequency( /** @scrutinizer ignore-type */ $schedule_unit, $schedule_number );
Loading history...
301
		$key             = $frequency['key'];
302
		$seconds         = $frequency['seconds'];
303
304
		$schedules[ $key ] = array(
305
			'interval' => $seconds * $schedule_number,
306
			'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

306
			'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

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

376
		$value = /** @scrutinizer ignore-type */ get_option( $this->option_prefix . 'logs_how_old', '' ) . ' ago';
Loading history...
377
		if ( '' !== $value ) {
378
			return $value;
379
		} else {
380
			return $how_old;
381
		}
382
	}
383
384
	/**
385
	 * Set arguments for only getting the Salesforce logs
386
	 *
387
	 * @param array $args Argument array for get_posts determining what posts are eligible for pruning.
388
	 * @return array $args
389
	 */
390
	public function set_prune_args( $args ) {
391
		$args['wp_log_type'] = 'salesforce';
392
		return $args;
393
	}
394
395
	/**
396
	 * Setup new log entry
397
	 *
398
	 * Check and see if we should log anything, and if so, send it to add()
399
	 *
400
	 * @access      public
401
	 * @since       1.0
402
	 *
403
	 * @param       string|array $title_or_params A log post title, or the full array of parameters
404
	 * @param       string $message The log message.
405
	 * @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...
406
	 * @param       int $parent The parent WordPress object.
407
	 * @param       string $status The log status.
408
	 *
409
	 * @uses        self::add()
410
	 * @see         Object_Sync_Sf_Mapping::__construct()    the location of the bitmasks that define the logging triggers.
411
	 *
412
	 * @return      void
413
	 */
414
	public function setup( $title_or_params, $message = '', $trigger = 0, $parent = 0, $status = '' ) {
415
416
		if ( is_array( $title_or_params ) ) {
417
			$title   = $title_or_params['title'];
418
			$message = $title_or_params['message'];
419
			$trigger = $title_or_params['trigger'];
420
			$parent  = $title_or_params['parent'];
421
			$status  = $title_or_params['status'];
422
		} else {
423
			$title = $title_or_params;
424
		}
425
426
		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

426
		if ( ! is_array( maybe_unserialize( /** @scrutinizer ignore-type */ $this->statuses_to_log ) ) ) {
Loading history...
427
			if ( $status === $this->statuses_to_log ) {
428
				$this->add( $title, $message, $parent );
429
			} else {
430
				return;
431
			}
432
		}
433
434
		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

434
		if ( true === filter_var( $this->enabled, FILTER_VALIDATE_BOOLEAN ) && in_array( $status, /** @scrutinizer ignore-type */ maybe_unserialize( $this->statuses_to_log ), true ) ) {
Loading history...
435
			$triggers_to_log = get_option( $this->option_prefix . 'triggers_to_log', array() );
436
			// 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()
437
			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

437
			if ( in_array( $trigger, /** @scrutinizer ignore-type */ maybe_unserialize( $triggers_to_log ) ) || 0 === $trigger ) {
Loading history...
438
				$this->add( $title, $message, $parent );
439
			} 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

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