Passed
Pull Request — dev/2.4.0 (#240)
by Sudar
17:08 queued 13:21
created

TableManager   F

Complexity

Total Complexity 68

Size/Duplication

Total Lines 550
Duplicated Lines 0 %

Test Coverage

Coverage 2.65%

Importance

Changes 25
Bugs 3 Features 0
Metric Value
wmc 68
eloc 218
c 25
b 3
f 0
dl 0
loc 550
ccs 6
cts 226
cp 0.0265
rs 2.96

19 Methods

Rating   Name   Duplication   Size   Complexity  
A insert_log() 0 5 1
A create_table_for_new_blog() 0 5 2
A get_log_table_name() 0 4 1
A delete_all_logs() 0 6 1
A delete_logs() 0 9 1
A get_logs_count() 0 6 1
A delete_table_from_deleted_blog() 0 4 1
A delete_logs_older_than() 0 8 1
A load() 0 7 1
A create_table_if_needed() 0 13 2
A fetch_log_items_by_id() 0 22 4
F fetch_log_items() 0 110 26
A on_activate() 0 13 4
B fetch_log_id_by_data() 0 47 11
A update_table_if_needed() 0 11 2
A get_create_table_query() 0 21 1
A validate_columns() 0 2 1
B query_log_items_by_column() 0 38 6
A mark_log_as_failed() 0 17 1

How to fix   Complexity   

Complex Class

Complex classes like TableManager 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 TableManager, and based on these observations, apply Extract Interface, too.

1
<?php namespace EmailLog\Core\DB;
2
3
/**
4
 * Handle installation and db table creation.
5
 */
6
use EmailLog\Core\Loadie;
7
use EmailLog\Util;
8
9
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
10
11
/**
12
 * Helper class to create table.
13
 *
14
 * @since 2.0.0
15
 */
16
class TableManager implements Loadie {
17
18
	/* Database table name */
19
	const LOG_TABLE_NAME = 'email_log';
20
21
	/* Database option name */
22
	const DB_OPTION_NAME = 'email-log-db';
23
24
	/* Database version */
25
	const DB_VERSION = '0.3';
26
27
	/**
28
	 * Setup hooks.
29
	 */
30
	public function load() {
31
		add_action( 'wpmu_new_blog', array( $this, 'create_table_for_new_blog' ) );
32
33
		add_filter( 'wpmu_drop_tables', array( $this, 'delete_table_from_deleted_blog' ) );
34
35
		// Do any DB upgrades.
36
		$this->update_table_if_needed();
37
	}
38
39
	/**
40
	 * On plugin activation, create table if needed.
41
	 *
42
	 * @param bool $network_wide True if the plugin was network activated.
43
	 */
44
	public function on_activate( $network_wide ) {
45
		if ( is_multisite() && $network_wide ) {
46
			// Note: if there are more than 10,000 blogs or
47
			// if `wp_is_large_network` filter is set, then this may fail.
48
			$sites = get_sites();
49
50
			foreach ( $sites as $site ) {
51
				switch_to_blog( $site->blog_id );
52
				$this->create_table_if_needed();
53
				restore_current_blog();
54
			}
55
		} else {
56
			$this->create_table_if_needed();
57
		}
58
	}
59
60
	/**
61
	 * Create email log table when a new blog is created.
62
	 *
63
	 * @param int $blog_id Blog Id.
64
	 */
65
	public function create_table_for_new_blog( $blog_id ) {
66
		if ( is_plugin_active_for_network( 'email-log/email-log.php' ) ) {
67
			switch_to_blog( $blog_id );
68
			$this->create_table_if_needed();
69
			restore_current_blog();
70
		}
71
	}
72
73
	/**
74
	 * Add email log table to the list of tables deleted when a blog is deleted.
75
	 *
76
	 * @param array $tables List of tables to be deleted.
77
	 *
78
	 * @return string[] $tables Modified list of tables to be deleted.
79
	 */
80 1
	public function delete_table_from_deleted_blog( $tables ) {
81 1
		$tables[] = $this->get_log_table_name();
82
83 1
		return $tables;
84
	}
85
86
	/**
87
	 * Get email log table name.
88
	 *
89
	 * @return string Email Log Table name.
90
	 */
91 2
	public function get_log_table_name() {
92 2
		global $wpdb;
93
94 2
		return $wpdb->prefix . self::LOG_TABLE_NAME;
95
	}
96
97
	/**
98
	 * Insert log data into DB.
99
	 *
100
	 * @param array $data Data to be inserted.
101
	 */
102
	public function insert_log( $data ) {
103
		global $wpdb;
104
105
		$table_name = $this->get_log_table_name();
106
		$wpdb->insert( $table_name, $data );
107
	}
108
109
	/**
110
	 * Delete log entries by ids.
111
	 *
112
	 * @param string $ids Comma separated list of log ids.
113
	 *
114
	 * @return false|int Number of log entries that got deleted. False on failure.
115
	 */
116
	public function delete_logs( $ids ) {
117
		global $wpdb;
118
119
		$table_name = $this->get_log_table_name();
120
121
		// Can't use wpdb->prepare for the below query. If used it results in this bug // https://github.com/sudar/email-log/issues/13.
122
		$ids = esc_sql( $ids );
123
124
		return $wpdb->query( "DELETE FROM {$table_name} where id IN ( {$ids} )" ); //@codingStandardsIgnoreLine
125
	}
126
127
	/**
128
	 * Delete all log entries.
129
	 *
130
	 * @return false|int Number of log entries that got deleted. False on failure.
131
	 */
132
	public function delete_all_logs() {
133
		global $wpdb;
134
135
		$table_name = $this->get_log_table_name();
136
137
		return $wpdb->query( "DELETE FROM {$table_name}" ); //@codingStandardsIgnoreLine
138
	}
139
140
	/**
141
	 * Deletes Email Logs older than the specified interval.
142
	 *
143
	 * @param int $interval_in_days No. of days beyond which logs are to be deleted.
144
	 *
145
	 * @return int $deleted_rows_count  Count of rows deleted.
146
	 */
147
	public function delete_logs_older_than( $interval_in_days ) {
148
		global $wpdb;
149
		$table_name = $this->get_log_table_name();
150
151
		$query              = $wpdb->prepare( "DELETE FROM {$table_name} WHERE sent_date < DATE_SUB( CURDATE(), INTERVAL %d DAY )", $interval_in_days );
152
		$deleted_rows_count = $wpdb->query( $query );
153
154
		return $deleted_rows_count;
155
	}
156
157
	/**
158
	 * Fetch log item by ID.
159
	 *
160
	 * @param array $ids             Optional. Array of IDs of the log items to be retrieved.
161
	 * @param array $additional_args {
162
	 *                               Optional. Array of additional args.
163
	 *
164
	 * @type string $date_column_format MySQL date column format. Refer
165
	 *
166
	 * @link  https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
167
	 * }
168
	 *
169
	 * @return array Log item(s).
170
	 */
171
	public function fetch_log_items_by_id( $ids = array(), $additional_args = array() ) {
172
		global $wpdb;
173
		$table_name = $this->get_log_table_name();
174
175
		$query = "SELECT * FROM {$table_name}";
176
177
		// When `date_column_format` exists, should replace the `$query` var.
178
		$date_column_format_key = 'date_column_format';
179
		if ( array_key_exists( $date_column_format_key, $additional_args ) && ! empty( $additional_args[ $date_column_format_key ] ) ) {
180
			$query = "SELECT DATE_FORMAT(sent_date, \"{$additional_args[ $date_column_format_key ]}\") as sent_date_custom, el.* FROM {$table_name} as el";
181
		}
182
183
		if ( ! empty( $ids ) ) {
184
			$ids = array_map( 'absint', $ids );
185
186
			// Can't use wpdb->prepare for the below query. If used it results in this bug https://github.com/sudar/email-log/issues/13.
187
			$ids_list = esc_sql( implode( ',', $ids ) );
188
189
			$query .= " where id IN ( {$ids_list} )";
190
		}
191
192
		return $wpdb->get_results( $query, 'ARRAY_A' ); //@codingStandardsIgnoreLine
193
	}
194
195
	/**
196
	 * Fetch log items.
197
	 *
198
	 * @since 2.3.0 Implemented Advanced Search. Search queries could look like the following.
199
	 *              Example:
200
	 *              id: 2
201
	 *              to: [email protected]
202
	 *
203
	 * @param array $request         Request object.
204
	 * @param int   $per_page        Entries per page.
205
	 * @param int   $current_page_no Current page no.
206
	 *
207
	 * @return array Log entries and total items count.
208
	 */
209
	public function fetch_log_items( $request, $per_page, $current_page_no ) {
210
		global $wpdb;
211
		$table_name = $this->get_log_table_name();
212
213
		$query       = 'SELECT * FROM ' . $table_name;
214
		$count_query = 'SELECT count(*) FROM ' . $table_name;
215
		$query_cond  = '';
216
217
		if ( isset( $request['s'] ) && is_string( $request['s'] ) && $request['s'] !== '' ) {
218
			$search_term = trim( esc_sql( $request['s'] ) );
0 ignored issues
show
Bug introduced by
It seems like esc_sql($request['s']) can also be of type array; however, parameter $str of trim() 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

218
			$search_term = trim( /** @scrutinizer ignore-type */ esc_sql( $request['s'] ) );
Loading history...
219
220
			if ( Util\is_advanced_search_term( $search_term ) ) {
0 ignored issues
show
Bug introduced by
The function is_advanced_search_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

220
			if ( /** @scrutinizer ignore-call */ Util\is_advanced_search_term( $search_term ) ) {
Loading history...
221
				$predicates = Util\get_advanced_search_term_predicates( $search_term );
0 ignored issues
show
Bug introduced by
The function get_advanced_search_term_predicates was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

221
				$predicates = /** @scrutinizer ignore-call */ Util\get_advanced_search_term_predicates( $search_term );
Loading history...
222
223
				foreach ( $predicates as $column => $email ) {
224
					switch ( $column ) {
225
						case 'id':
226
							$query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND ';
227
							$query_cond .= "id = '$email'";
228
							break;
229
						case 'to':
230
							$query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND ';
231
							$query_cond .= "to_email LIKE '%$email%'";
232
							break;
233
						case 'email':
234
							$query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND ';
235
							$query_cond .= ' ( '; /* Begin 1st */
236
							$query_cond .= " ( to_email LIKE '%$email%' OR subject LIKE '%$email%' ) "; /* Begin 2nd & End 2nd */
237
							$query_cond .= ' OR ';
238
							$query_cond .= ' ( '; /* Begin 3rd */
239
							$query_cond .= "headers <> ''";
240
							$query_cond .= ' AND ';
241
							$query_cond .= ' ( '; /* Begin 4th */
242
							$query_cond .= "headers REGEXP '[F|f]rom:.*$email' OR ";
243
							$query_cond .= "headers REGEXP '[CC|Cc|cc]:.*$email' OR ";
244
							$query_cond .= "headers REGEXP '[BCC|Bcc|bcc]:.*$email' OR ";
245
							$query_cond .= "headers REGEXP '[R|r]eply-[T|t]o:.*$email'";
246
							$query_cond .= ' ) '; /* End 4th */
247
							$query_cond .= ' ) '; /* End 3rd */
248
							$query_cond .= ' ) '; /* End 1st */
249
							break;
250
						case 'cc':
251
							$query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND ';
252
							$query_cond .= ' ( '; /* Begin 1st */
253
							$query_cond .= "headers <> ''";
254
							$query_cond .= ' AND ';
255
							$query_cond .= ' ( '; /* Begin 2nd */
256
							$query_cond .= "headers REGEXP '[CC|Cc|cc]:.*$email' ";
257
							$query_cond .= ' ) '; /* End 2nd */
258
							$query_cond .= ' ) '; /* End 1st */
259
							break;
260
						case 'bcc':
261
							$query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND ';
262
							$query_cond .= ' ( '; /* Begin 1st */
263
							$query_cond .= "headers <> ''";
264
							$query_cond .= ' AND ';
265
							$query_cond .= ' ( '; /* Begin 2nd */
266
							$query_cond .= "headers REGEXP '[BCC|Bcc|bcc]:.*$email' ";
267
							$query_cond .= ' ) '; /* End 2nd */
268
							$query_cond .= ' ) '; /* End 1st */
269
							break;
270
						case 'reply-to':
271
							$query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND ';
272
							$query_cond .= ' ( '; /* Begin 1st */
273
							$query_cond .= "headers <> ''";
274
							$query_cond .= ' AND ';
275
							$query_cond .= ' ( '; /* Begin 2nd */
276
							$query_cond .= "headers REGEXP '[R|r]eply-to:.*$email' ";
277
							$query_cond .= ' ) '; /* End 2nd */
278
							$query_cond .= ' ) '; /* End 1st */
279
							break;
280
					}
281
				}
282
			} else {
283
				$query_cond .= " WHERE ( to_email LIKE '%$search_term%' OR subject LIKE '%$search_term%' ) ";
284
			}
285
		}
286
287
		if ( isset( $request['d'] ) && $request['d'] !== '' ) {
288
			$search_date = trim( esc_sql( $request['d'] ) );
289
			if ( '' === $query_cond ) {
290
				$query_cond .= " WHERE sent_date BETWEEN '$search_date 00:00:00' AND '$search_date 23:59:59' ";
291
			} else {
292
				$query_cond .= " AND sent_date BETWEEN '$search_date 00:00:00' AND '$search_date 23:59:59' ";
293
			}
294
		}
295
296
		// Ordering parameters.
297
		$orderby = ! empty( $request['orderby'] ) ? esc_sql( $request['orderby'] ) : 'sent_date';
298
		$order   = ! empty( $request['order'] ) ? esc_sql( $request['order'] ) : 'DESC';
299
300
		if ( ! empty( $orderby ) & ! empty( $order ) ) {
0 ignored issues
show
Bug introduced by
Are you sure you want to use the bitwise & or did you mean &&?
Loading history...
301
			$query_cond .= ' ORDER BY ' . $orderby . ' ' . $order;
0 ignored issues
show
Bug introduced by
Are you sure $order of type array|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

301
			$query_cond .= ' ORDER BY ' . $orderby . ' ' . /** @scrutinizer ignore-type */ $order;
Loading history...
Bug introduced by
Are you sure $orderby of type array|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

301
			$query_cond .= ' ORDER BY ' . /** @scrutinizer ignore-type */ $orderby . ' ' . $order;
Loading history...
302
		}
303
304
		// Find total number of items.
305
		$count_query = $count_query . $query_cond;
306
		$total_items = $wpdb->get_var( $count_query );
307
308
		// Adjust the query to take pagination into account.
309
		if ( ! empty( $current_page_no ) && ! empty( $per_page ) ) {
310
			$offset     = ( $current_page_no - 1 ) * $per_page;
311
			$query_cond .= ' LIMIT ' . (int) $offset . ',' . (int) $per_page;
312
		}
313
314
		// Fetch the items.
315
		$query = $query . $query_cond;
316
		$items = $wpdb->get_results( $query );
317
318
		return array( $items, $total_items );
319
	}
320
321
	/**
322
	 * Create email log table.
323
	 *
324
	 * @global object $wpdb
325
	 */
326
	public function create_table_if_needed() {
327
		global $wpdb;
328
329
		$table_name = $this->get_log_table_name();
330
331
		if ( $wpdb->get_var( "show tables like '{$table_name}'" ) != $table_name ) {
332
333
			$sql = $this->get_create_table_query();
334
335
			require_once ABSPATH . 'wp-admin/includes/upgrade.php';
336
			dbDelta( $sql );
337
338
			add_option( self::DB_OPTION_NAME, self::DB_VERSION );
339
		}
340
	}
341
342
	/**
343
	 * Get the total number of email logs.
344
	 *
345
	 * @return int Total email log count
346
	 */
347
	public function get_logs_count() {
348
		global $wpdb;
349
350
		$query = 'SELECT count(*) FROM ' . $this->get_log_table_name();
351
352
		return $wpdb->get_var( $query );
353
	}
354
355
	/**
356
	 * Fetches the log id by item data.
357
	 *
358
	 * Use this method to get the log item id when the error instance only returns the log item data.
359
	 *
360
	 * @param array        $data Array of Email information {
361
	 * @type  array|string to
362
	 * @type  string       subject
363
	 * @type  string       message
364
	 * @type  array|string headers
365
	 * @type  array|string attachments
366
	 *                          }
367
	 *
368
	 * @return int Log item id.
369
	 */
370
	public function fetch_log_id_by_data( $data ) {
371
		if ( empty( $data ) || ! is_array( $data ) ) {
372
			return 0;
373
		}
374
375
		global $wpdb;
376
		$table_name = $this->get_log_table_name();
377
378
		$query      = "SELECT ID FROM {$table_name}";
379
		$query_cond = '';
380
		$where      = array();
381
382
		// Execute the following `if` conditions only when $data is array.
383
		if ( array_key_exists( 'to', $data ) ) {
384
			// Since the value is stored as CSV in DB, convert the values from error data to CSV to compare.
385
			$to_email = Util\stringify( $data['to'] );
0 ignored issues
show
Bug introduced by
The function stringify was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

385
			$to_email = /** @scrutinizer ignore-call */ Util\stringify( $data['to'] );
Loading history...
386
387
			$to_email = trim( esc_sql( $to_email ) );
0 ignored issues
show
Bug introduced by
It seems like esc_sql($to_email) can also be of type array; however, parameter $str of trim() 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

387
			$to_email = trim( /** @scrutinizer ignore-type */ esc_sql( $to_email ) );
Loading history...
388
			$where[]  = "to_email = '$to_email'";
389
		}
390
391
		if ( array_key_exists( 'subject', $data ) ) {
392
			$subject = trim( esc_sql( $data['subject'] ) );
393
			$where[] = "subject = '$subject'";
394
		}
395
396
		if ( array_key_exists( 'attachments', $data ) ) {
397
			if ( is_array( $data['attachments'] ) ) {
398
				$attachments = count( $data['attachments'] ) > 0 ? 'true' : 'false';
399
			} else {
400
				$attachments = empty( $data['attachments'] ) ? 'false' : 'true';
401
			}
402
			$attachments = trim( esc_sql( $attachments ) );
403
			$where[]     = "attachments = '$attachments'";
404
		}
405
406
		foreach ( $where as $index => $value ) {
407
			$query_cond .= 0 === $index ? ' WHERE ' : ' AND ';
408
			$query_cond .= $value;
409
		}
410
411
		// Get only the latest logged item when multiple rows match.
412
		$query_cond .= ' ORDER BY id DESC LIMIT 1';
413
414
		$query = $query . $query_cond;
415
416
		return absint( $wpdb->get_var( $query ) );
417
	}
418
419
	/**
420
	 * Sets email sent status and error message for the given log item when email fails.
421
	 *
422
	 * @param int    $log_item_id ID of the log item whose email sent status should be set to failed.
423
	 * @param string $message     Error message.
424
	 *
425
	 * @since 2.4.0 Include error message during update.
426
	 * @since 2.3.0
427
	 *
428
	 * @global \wpdb $wpdb
429
	 *
430
	 * @see  TableManager::get_log_table_name()
431
	 */
432
	public function mark_log_as_failed( $log_item_id, $message ) {
433
		global $wpdb;
434
		$table_name = $this->get_log_table_name();
435
436
		$wpdb->update(
437
			$table_name,
438
			array(
439
				'result'        => '0',
440
				'error_message' => $message,
441
			),
442
			array( 'ID' => $log_item_id ),
443
			array(
444
				'%d', // `result` format.
445
				'%s', // `error_message` format.
446
			),
447
			array(
448
				'%d', // `ID` format.
449
			)
450
		);
451
	}
452
453
	/**
454
	 * Updates the DB schema.
455
	 *
456
	 * Adds new columns to the Database as of v0.2.
457
	 *
458
	 * @since 2.3.0
459
	 */
460
	private function update_table_if_needed() {
461
		if ( get_option( self::DB_OPTION_NAME, false ) === self::DB_VERSION ) {
462
			return;
463
		}
464
465
		$sql = $this->get_create_table_query();
466
467
		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
468
		dbDelta( $sql );
469
470
		update_option( self::DB_OPTION_NAME, self::DB_VERSION );
471
	}
472
473
	/**
474
	 * Gets the Create Table query.
475
	 *
476
	 * @since 2.4.0 Added error_message column.
477
	 * @since 2.3.0
478
	 *
479
	 * @return string
480
	 */
481
	private function get_create_table_query() {
482
		global $wpdb;
483
		$table_name      = $this->get_log_table_name();
484
		$charset_collate = $wpdb->get_charset_collate();
485
486
		$sql = 'CREATE TABLE ' . $table_name . ' (
487
				id mediumint(9) NOT NULL AUTO_INCREMENT,
488
				to_email VARCHAR(500) NOT NULL,
489
				subject VARCHAR(500) NOT NULL,
490
				message TEXT NOT NULL,
491
				headers TEXT NOT NULL,
492
				attachments TEXT NOT NULL,
493
				sent_date timestamp NOT NULL,
494
				attachment_name VARCHAR(1000),
495
				ip_address VARCHAR(15),
496
				result TINYINT(1),
497
				error_message VARCHAR(1000),
498
				PRIMARY KEY  (id)
499
			) ' . $charset_collate . ';';
500
501
		return $sql;
502
	}
503
504
	/**
505
	 * Callback for the Array filter.
506
	 *
507
	 * @since 2.3.0
508
	 *
509
	 * @param string $column A column from the array Columns.
510
	 *
511
	 * @return bool
512
	 */
513
	private function validate_columns( $column ) {
514
		return in_array( $column, array( 'to' ), true );
515
	}
516
517
	/**
518
	 * Query log items by column.
519
	 *
520
	 * @since 2.3.0
521
	 *
522
	 * @param array $columns Key value pair based on which items should be retrieved.
523
	 *
524
	 * @uses \EmailLog\Core\DB\TableManager::validate_columns()
525
	 *
526
	 * @return array|object|null
527
	 */
528
	public function query_log_items_by_column( $columns ) {
529
		if ( ! is_array( $columns ) ) {
0 ignored issues
show
introduced by
The condition is_array($columns) is always true.
Loading history...
530
			return;
531
		}
532
533
		// Since we support PHP v5.2.4, we cannot use ARRAY_FILTER_USE_KEY
534
		// TODO: PHP v5.5: Once WordPress updates minimum PHP version to v5.5, start using ARRAY_FILTER_USE_KEY.
535
		$columns_keys = array_keys( $columns );
536
		if ( ! array_filter( $columns_keys, array( $this, 'validate_columns' ) ) ) {
537
			return;
538
		}
539
540
		global $wpdb;
541
542
		$table_name = $this->get_log_table_name();
543
		$query      = "SELECT id, sent_date, to_email, subject FROM {$table_name}";
544
		$query_cond = '';
545
		$where      = array();
546
547
		// Execute the following `if` conditions only when $data is array.
548
		if ( array_key_exists( 'to', $columns ) ) {
549
			// Since the value is stored as CSV in DB, convert the values from error data to CSV to compare.
550
			$to_email = Util\stringify( $columns['to'] );
0 ignored issues
show
Bug introduced by
The function stringify was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

550
			$to_email = /** @scrutinizer ignore-call */ Util\stringify( $columns['to'] );
Loading history...
551
552
			$to_email = trim( esc_sql( $to_email ) );
0 ignored issues
show
Bug introduced by
It seems like esc_sql($to_email) can also be of type array; however, parameter $str of trim() 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

552
			$to_email = trim( /** @scrutinizer ignore-type */ esc_sql( $to_email ) );
Loading history...
553
			$where[]  = "to_email = '$to_email'";
554
555
			foreach ( $where as $index => $value ) {
556
				$query_cond .= 0 === $index ? ' WHERE ' : ' AND ';
557
				$query_cond .= $value;
558
			}
559
560
			// Get only the latest logged item when multiple rows match.
561
			$query_cond .= ' ORDER BY id DESC';
562
563
			$query = $query . $query_cond;
564
565
			return $wpdb->get_results( $query );
566
		}
567
	}
568
}
569