TableManager::delete_all_logs()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

219
			$search_term = trim( /** @scrutinizer ignore-type */ esc_sql( $request['s'] ) );
Loading history...
220
221
			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

221
			if ( /** @scrutinizer ignore-call */ Util\is_advanced_search_term( $search_term ) ) {
Loading history...
222
				$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

222
				$predicates = /** @scrutinizer ignore-call */ Util\get_advanced_search_term_predicates( $search_term );
Loading history...
223
224
				foreach ( $predicates as $column => $email ) {
225
					switch ( $column ) {
226
						case 'id':
227
							$query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND ';
228
							$query_cond .= "id = '$email'";
229
							break;
230
						case 'to':
231
							$query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND ';
232
							$query_cond .= "to_email LIKE '%$email%'";
233
							break;
234
						case 'email':
235
							$query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND ';
236
							$query_cond .= ' ( '; /* Begin 1st */
237
							$query_cond .= " ( to_email LIKE '%$email%' OR subject LIKE '%$email%' ) "; /* Begin 2nd & End 2nd */
238
							$query_cond .= ' OR ';
239
							$query_cond .= ' ( '; /* Begin 3rd */
240
							$query_cond .= "headers <> ''";
241
							$query_cond .= ' AND ';
242
							$query_cond .= ' ( '; /* Begin 4th */
243
							$query_cond .= "headers REGEXP '[F|f]rom:.*$email' OR ";
244
							$query_cond .= "headers REGEXP '[CC|Cc|cc]:.*$email' OR ";
245
							$query_cond .= "headers REGEXP '[BCC|Bcc|bcc]:.*$email' OR ";
246
							$query_cond .= "headers REGEXP '[R|r]eply-[T|t]o:.*$email'";
247
							$query_cond .= ' ) '; /* End 4th */
248
							$query_cond .= ' ) '; /* End 3rd */
249
							$query_cond .= ' ) '; /* End 1st */
250
							break;
251
						case 'cc':
252
							$query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND ';
253
							$query_cond .= ' ( '; /* Begin 1st */
254
							$query_cond .= "headers <> ''";
255
							$query_cond .= ' AND ';
256
							$query_cond .= ' ( '; /* Begin 2nd */
257
							$query_cond .= "headers REGEXP '[CC|Cc|cc]:.*$email' ";
258
							$query_cond .= ' ) '; /* End 2nd */
259
							$query_cond .= ' ) '; /* End 1st */
260
							break;
261
						case 'bcc':
262
							$query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND ';
263
							$query_cond .= ' ( '; /* Begin 1st */
264
							$query_cond .= "headers <> ''";
265
							$query_cond .= ' AND ';
266
							$query_cond .= ' ( '; /* Begin 2nd */
267
							$query_cond .= "headers REGEXP '[BCC|Bcc|bcc]:.*$email' ";
268
							$query_cond .= ' ) '; /* End 2nd */
269
							$query_cond .= ' ) '; /* End 1st */
270
							break;
271
						case 'reply-to':
272
							$query_cond .= empty( $query_cond ) ? ' WHERE ' : ' AND ';
273
							$query_cond .= ' ( '; /* Begin 1st */
274
							$query_cond .= "headers <> ''";
275
							$query_cond .= ' AND ';
276
							$query_cond .= ' ( '; /* Begin 2nd */
277
							$query_cond .= "headers REGEXP '[R|r]eply-to:.*$email' ";
278
							$query_cond .= ' ) '; /* End 2nd */
279
							$query_cond .= ' ) '; /* End 1st */
280
							break;
281
					}
282
				}
283
			} else {
284
				$query_cond .= " WHERE ( to_email LIKE '%$search_term%' OR subject LIKE '%$search_term%' ) ";
285
			}
286
		}
287
288
		if ( isset( $request['d'] ) && $request['d'] !== '' ) {
289
			$search_date = trim( esc_sql( $request['d'] ) );
290
			if ( '' === $query_cond ) {
291
				$query_cond .= " WHERE sent_date BETWEEN '$search_date 00:00:00' AND '$search_date 23:59:59' ";
292
			} else {
293
				$query_cond .= " AND sent_date BETWEEN '$search_date 00:00:00' AND '$search_date 23:59:59' ";
294
			}
295
		}
296
297
		// Ordering parameters.
298
		$order_by = 'sent_date';
299
		$order    = 'DESC';
300
301
		$allowed_order_by = [
302
			'sent_date',
303
			'to_email',
304
			'subject',
305
		];
306
307
		$sanitized_order_by = ( ! empty( $request['orderby'] ) ) ? sanitize_text_field( $request['orderby'] ) : '';
308
		if ( ! empty( $sanitized_order_by ) && in_array( $sanitized_order_by, $allowed_order_by, true ) ) {
309
			$order_by = $sanitized_order_by;
310
		}
311
312
		if ( ! empty( $request['order'] ) && 'asc' === strtolower( sanitize_text_field( $request['order'] ) ) ) {
313
			$order = 'ASC';
314
		}
315
316
		if ( ! empty( $order_by ) & ! empty( $order ) ) {
0 ignored issues
show
Bug introduced by
Are you sure you want to use the bitwise & or did you mean &&?
Loading history...
317
			$query_cond .= ' ORDER BY ' . $order_by . ' ' . $order;
318
		}
319
320
		// Find total number of items.
321
		$count_query = $count_query . $query_cond;
322
		$total_items = $wpdb->get_var( $count_query );
323
324
		// Adjust the query to take pagination into account.
325
		if ( ! empty( $current_page_no ) && ! empty( $per_page ) ) {
326
			$offset     = ( $current_page_no - 1 ) * $per_page;
327
			$query_cond .= ' LIMIT ' . (int) $offset . ',' . (int) $per_page;
328
		}
329
330
		// Fetch the items.
331
		$query = $query . $query_cond;
332
		$items = $wpdb->get_results( $query );
333
334
		return array( $items, $total_items );
335
	}
336
337
	/**
338
	 * Create email log table.
339
	 *
340
	 * @global object $wpdb
341
	 */
342
	public function create_table_if_needed() {
343
		global $wpdb;
344
345
		$table_name = $this->get_log_table_name();
346
347
		if ( $wpdb->get_var( "show tables like '{$table_name}'" ) != $table_name ) {
348
349
			$sql = $this->get_create_table_query();
350
351
			require_once ABSPATH . 'wp-admin/includes/upgrade.php';
352
			dbDelta( $sql );
353
354
			add_option( self::DB_OPTION_NAME, self::DB_VERSION );
355
		}
356
	}
357
358
	/**
359
	 * Get the total number of email logs.
360
	 *
361
	 * @return int Total email log count
362
	 */
363
	public function get_logs_count() {
364
		global $wpdb;
365
366
		$query = 'SELECT count(*) FROM ' . $this->get_log_table_name();
367
368
		return $wpdb->get_var( $query );
369
	}
370
371
	/**
372
	 * Fetches the log id by item data.
373
	 *
374
	 * Use this method to get the log item id when the error instance only returns the log item data.
375
	 *
376
	 * @param array        $data Array of Email information {
377
	 * @type  array|string to
378
	 * @type  string       subject
379
	 * @type  string       message
380
	 * @type  array|string headers
381
	 * @type  array|string attachments
382
	 *                          }
383
	 *
384
	 * @return int Log item id.
385
	 */
386
	public function fetch_log_id_by_data( $data ) {
387
		if ( empty( $data ) || ! is_array( $data ) ) {
388
			return 0;
389
		}
390
391
		global $wpdb;
392
		$table_name = $this->get_log_table_name();
393
394
		$query      = "SELECT ID FROM {$table_name}";
395
		$query_cond = '';
396
		$where      = array();
397
398
		// Execute the following `if` conditions only when $data is array.
399
		if ( array_key_exists( 'to', $data ) ) {
400
			// Since the value is stored as CSV in DB, convert the values from error data to CSV to compare.
401
			$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

401
			$to_email = /** @scrutinizer ignore-call */ Util\stringify( $data['to'] );
Loading history...
402
403
			$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 $string 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

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

566
			$to_email = /** @scrutinizer ignore-call */ Util\stringify( $columns['to'] );
Loading history...
567
568
			$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 $string 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

568
			$to_email = trim( /** @scrutinizer ignore-type */ esc_sql( $to_email ) );
Loading history...
569
			$where[]  = "to_email = '$to_email'";
570
571
			foreach ( $where as $index => $value ) {
572
				$query_cond .= 0 === $index ? ' WHERE ' : ' AND ';
573
				$query_cond .= $value;
574
			}
575
576
			// Get only the latest logged item when multiple rows match.
577
			$query_cond .= ' ORDER BY id DESC';
578
579
			$query = $query . $query_cond;
580
581
			return $wpdb->get_results( $query );
582
		}
583
	}
584
}
585