|
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
|
|
|
use function EmailLog\Util\el_array_get; |
|
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
|
|
|
* The user meta key in which the starred emails of a user are stored. |
|
30
|
|
|
* |
|
31
|
|
|
* @since 2.5.0 |
|
32
|
|
|
*/ |
|
33
|
|
|
const STARRED_LOGS_META_KEY = 'email-log-starred-logs'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Setup hooks. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function load() { |
|
39
|
|
|
add_action( 'wpmu_new_blog', array( $this, 'create_table_for_new_blog' ) ); |
|
40
|
|
|
|
|
41
|
|
|
add_filter( 'wpmu_drop_tables', array( $this, 'delete_table_from_deleted_blog' ) ); |
|
42
|
|
|
|
|
43
|
|
|
// Do any DB upgrades. |
|
44
|
|
|
$this->update_table_if_needed(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* On plugin activation, create table if needed. |
|
49
|
|
|
* |
|
50
|
|
|
* @param bool $network_wide True if the plugin was network activated. |
|
51
|
|
|
*/ |
|
52
|
|
|
public function on_activate( $network_wide ) { |
|
53
|
|
|
if ( is_multisite() && $network_wide ) { |
|
54
|
|
|
// Note: if there are more than 10,000 blogs or |
|
55
|
|
|
// if `wp_is_large_network` filter is set, then this may fail. |
|
56
|
|
|
$sites = get_sites(); |
|
57
|
|
|
|
|
58
|
|
|
foreach ( $sites as $site ) { |
|
59
|
|
|
switch_to_blog( $site->blog_id ); |
|
60
|
|
|
$this->create_table_if_needed(); |
|
61
|
|
|
restore_current_blog(); |
|
62
|
|
|
} |
|
63
|
|
|
} else { |
|
64
|
|
|
$this->create_table_if_needed(); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Create email log table when a new blog is created. |
|
70
|
|
|
* |
|
71
|
|
|
* @param int $blog_id Blog Id. |
|
72
|
|
|
*/ |
|
73
|
|
|
public function create_table_for_new_blog( $blog_id ) { |
|
74
|
|
|
if ( is_plugin_active_for_network( 'email-log/email-log.php' ) ) { |
|
75
|
|
|
switch_to_blog( $blog_id ); |
|
76
|
|
|
$this->create_table_if_needed(); |
|
77
|
|
|
restore_current_blog(); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Add email log table to the list of tables deleted when a blog is deleted. |
|
83
|
|
|
* |
|
84
|
|
|
* @param array $tables List of tables to be deleted. |
|
85
|
|
|
* |
|
86
|
|
|
* @return string[] $tables Modified list of tables to be deleted. |
|
87
|
|
|
*/ |
|
88
|
1 |
|
public function delete_table_from_deleted_blog( $tables ) { |
|
89
|
1 |
|
$tables[] = $this->get_log_table_name(); |
|
90
|
|
|
|
|
91
|
1 |
|
return $tables; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Get email log table name. |
|
96
|
|
|
* |
|
97
|
|
|
* @return string Email Log Table name. |
|
98
|
|
|
*/ |
|
99
|
2 |
|
public function get_log_table_name() { |
|
100
|
2 |
|
global $wpdb; |
|
101
|
|
|
|
|
102
|
2 |
|
return $wpdb->prefix . self::LOG_TABLE_NAME; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Insert log data into DB. |
|
107
|
|
|
* |
|
108
|
|
|
* @param array $data Data to be inserted. |
|
109
|
|
|
*/ |
|
110
|
|
|
public function insert_log( $data ) { |
|
111
|
|
|
global $wpdb; |
|
112
|
|
|
|
|
113
|
|
|
$table_name = $this->get_log_table_name(); |
|
114
|
|
|
$wpdb->insert( $table_name, $data ); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Delete log entries by ids. |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $ids Comma separated list of log ids. |
|
121
|
|
|
* |
|
122
|
|
|
* @return false|int Number of log entries that got deleted. False on failure. |
|
123
|
|
|
*/ |
|
124
|
|
|
public function delete_logs( $ids ) { |
|
125
|
|
|
global $wpdb; |
|
126
|
|
|
|
|
127
|
|
|
$table_name = $this->get_log_table_name(); |
|
128
|
|
|
|
|
129
|
|
|
// Can't use wpdb->prepare for the below query. If used it results in this bug // https://github.com/sudar/email-log/issues/13. |
|
130
|
|
|
$ids = esc_sql( $ids ); |
|
131
|
|
|
|
|
132
|
|
|
return $wpdb->query( "DELETE FROM {$table_name} where id IN ( {$ids} )" ); //@codingStandardsIgnoreLine |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Delete all log entries. |
|
137
|
|
|
* |
|
138
|
|
|
* @return false|int Number of log entries that got deleted. False on failure. |
|
139
|
|
|
*/ |
|
140
|
|
|
public function delete_all_logs() { |
|
141
|
|
|
global $wpdb; |
|
142
|
|
|
|
|
143
|
|
|
$table_name = $this->get_log_table_name(); |
|
144
|
|
|
|
|
145
|
|
|
return $wpdb->query( "DELETE FROM {$table_name}" ); //@codingStandardsIgnoreLine |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Deletes Email Logs older than the specified interval. |
|
150
|
|
|
* |
|
151
|
|
|
* @param int $interval_in_days No. of days beyond which logs are to be deleted. |
|
152
|
|
|
* |
|
153
|
|
|
* @return int $deleted_rows_count Count of rows deleted. |
|
154
|
|
|
*/ |
|
155
|
|
|
public function delete_logs_older_than( $interval_in_days ) { |
|
156
|
|
|
global $wpdb; |
|
157
|
|
|
$table_name = $this->get_log_table_name(); |
|
158
|
|
|
|
|
159
|
|
|
$query = $wpdb->prepare( "DELETE FROM {$table_name} WHERE sent_date < DATE_SUB( CURDATE(), INTERVAL %d DAY )", $interval_in_days ); |
|
160
|
|
|
$deleted_rows_count = $wpdb->query( $query ); |
|
161
|
|
|
|
|
162
|
|
|
return $deleted_rows_count; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Fetch log item by ID. |
|
167
|
|
|
* |
|
168
|
|
|
* @param array $ids Optional. Array of IDs of the log items to be retrieved. |
|
169
|
|
|
* @param array $additional_args { |
|
170
|
|
|
* Optional. Array of additional args. |
|
171
|
|
|
* |
|
172
|
|
|
* @type string $date_column_format MySQL date column format. Refer |
|
173
|
|
|
* |
|
174
|
|
|
* @link https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format |
|
175
|
|
|
* @type int $current_page_no Current Page number. |
|
176
|
|
|
* @type int $per_page Per Page count. |
|
177
|
|
|
* } |
|
178
|
|
|
* |
|
179
|
|
|
* @return array Log item(s). |
|
180
|
|
|
*/ |
|
181
|
|
|
public function fetch_log_items_by_id( $ids = array(), $additional_args = array() ) { |
|
182
|
|
|
global $wpdb; |
|
183
|
|
|
$table_name = $this->get_log_table_name(); |
|
184
|
|
|
$current_page_no = el_array_get( $additional_args, 'current_page_no', false ); |
|
|
|
|
|
|
185
|
|
|
$per_page = el_array_get( $additional_args, 'per_page', false ); |
|
186
|
|
|
|
|
187
|
|
|
$query = "SELECT * FROM {$table_name}"; |
|
188
|
|
|
|
|
189
|
|
|
// When `date_column_format` exists, should replace the `$query` var. |
|
190
|
|
|
$date_column_format_key = 'date_column_format'; |
|
191
|
|
|
if ( array_key_exists( $date_column_format_key, $additional_args ) && ! empty( $additional_args[ $date_column_format_key ] ) ) { |
|
192
|
|
|
$query = "SELECT DATE_FORMAT(sent_date, \"{$additional_args[ $date_column_format_key ]}\") as sent_date_custom, el.* FROM {$table_name} as el"; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
if ( ! empty( $ids ) ) { |
|
196
|
|
|
$ids = array_map( 'absint', $ids ); |
|
197
|
|
|
|
|
198
|
|
|
// Can't use wpdb->prepare for the below query. If used it results in this bug https://github.com/sudar/email-log/issues/13. |
|
199
|
|
|
$ids_list = esc_sql( implode( ',', $ids ) ); |
|
200
|
|
|
|
|
201
|
|
|
if ( isset( $additional_args['exclude_starred'] ) && true === $additional_args['exclude_starred'] ) { |
|
202
|
|
|
$query .= " where id NOT IN ( {$ids_list} )"; |
|
203
|
|
|
} else { |
|
204
|
|
|
$query .= " where id IN ( {$ids_list} )"; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
$query .= $this->build_query_condition( $_GET, true ); |
|
209
|
|
|
|
|
210
|
|
|
// Adjust the query to take pagination into account. |
|
211
|
|
|
if ( ! empty( $current_page_no ) && ! empty( $per_page ) ) { |
|
212
|
|
|
$offset = ( $current_page_no - 1 ) * $per_page; |
|
213
|
|
|
$query .= ' LIMIT ' . (int) $offset . ',' . (int) $per_page; |
|
214
|
|
|
} |
|
215
|
|
|
if ( isset( $additional_args['output_type'] ) && in_array( $additional_args['output_type'], [ OBJECT, OBJECT_K, ARRAY_A, ARRAY_N ], true ) ) { |
|
216
|
|
|
return $wpdb->get_results( $query, $additional_args['output_type'] ); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
return $wpdb->get_results( $query, 'ARRAY_A' ); //@codingStandardsIgnoreLine |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Fetch log items. |
|
224
|
|
|
* |
|
225
|
|
|
* @since 2.3.0 Implemented Advanced Search. Search queries could look like the following. |
|
226
|
|
|
* Example: |
|
227
|
|
|
* id: 2 |
|
228
|
|
|
* to: [email protected] |
|
229
|
|
|
* @since 2.5.0 Return only fetched log items and not total count. |
|
230
|
|
|
* |
|
231
|
|
|
* @param array $request Request object. |
|
232
|
|
|
* @param int $per_page Entries per page. |
|
233
|
|
|
* @param int $current_page_no Current page no. |
|
234
|
|
|
* |
|
235
|
|
|
* @return array Log entries. |
|
236
|
|
|
*/ |
|
237
|
|
|
public function fetch_log_items( $request, $per_page, $current_page_no ) { |
|
238
|
|
|
global $wpdb; |
|
239
|
|
|
$table_name = $this->get_log_table_name(); |
|
240
|
|
|
|
|
241
|
|
|
$query = 'SELECT * FROM ' . $table_name; |
|
242
|
|
|
|
|
243
|
|
|
$query_cond = $this->build_query_condition( $request ); |
|
244
|
|
|
|
|
245
|
|
|
// Adjust the query to take pagination into account. |
|
246
|
|
|
if ( ! empty( $current_page_no ) && ! empty( $per_page ) ) { |
|
247
|
|
|
$offset = ( $current_page_no - 1 ) * $per_page; |
|
248
|
|
|
$query_cond .= ' LIMIT ' . (int) $offset . ',' . (int) $per_page; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
$query .= $query_cond; |
|
252
|
|
|
|
|
253
|
|
|
return $wpdb->get_results( $query ); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Builds query condition based on supplied parameters. Currently handles search and sorting. |
|
258
|
|
|
* |
|
259
|
|
|
* @param array $request Request object. |
|
260
|
|
|
* @param bool $where_clause True if where clause is present, False otherwise. |
|
261
|
|
|
* |
|
262
|
|
|
* @since 2.5.0 |
|
263
|
|
|
*/ |
|
264
|
|
|
public function build_query_condition( $request, $where_clause = false ) { |
|
265
|
|
|
$query_cond = ''; |
|
266
|
|
|
if ( isset( $request['s'] ) && is_string( $request['s'] ) && $request['s'] !== '' ) { |
|
267
|
|
|
$search_term = trim( esc_sql( $request['s'] ) ); |
|
|
|
|
|
|
268
|
|
|
|
|
269
|
|
|
if ( Util\is_advanced_search_term( $search_term ) ) { |
|
|
|
|
|
|
270
|
|
|
$predicates = Util\get_advanced_search_term_predicates( $search_term ); |
|
|
|
|
|
|
271
|
|
|
|
|
272
|
|
|
foreach ( $predicates as $column => $email ) { |
|
273
|
|
|
switch ( $column ) { |
|
274
|
|
|
case 'id': |
|
275
|
|
|
$query_cond .= empty( $query_cond ) && ! $where_clause ? ' WHERE ' : ' AND '; |
|
276
|
|
|
$query_cond .= "id = '$email'"; |
|
277
|
|
|
break; |
|
278
|
|
|
case 'to': |
|
279
|
|
|
$query_cond .= empty( $query_cond ) && ! $where_clause ? ' WHERE ' : ' AND '; |
|
280
|
|
|
$query_cond .= "to_email LIKE '%$email%'"; |
|
281
|
|
|
break; |
|
282
|
|
|
case 'email': |
|
283
|
|
|
$query_cond .= empty( $query_cond ) && ! $where_clause ? ' WHERE ' : ' AND '; |
|
284
|
|
|
$query_cond .= ' ( '; /* Begin 1st */ |
|
285
|
|
|
$query_cond .= " ( to_email LIKE '%$email%' OR subject LIKE '%$email%' ) "; /* Begin 2nd & End 2nd */ |
|
286
|
|
|
$query_cond .= ' OR '; |
|
287
|
|
|
$query_cond .= ' ( '; /* Begin 3rd */ |
|
288
|
|
|
$query_cond .= "headers <> ''"; |
|
289
|
|
|
$query_cond .= ' AND '; |
|
290
|
|
|
$query_cond .= ' ( '; /* Begin 4th */ |
|
291
|
|
|
$query_cond .= "headers REGEXP '[F|f]rom:.*$email' OR "; |
|
292
|
|
|
$query_cond .= "headers REGEXP '[CC|Cc|cc]:.*$email' OR "; |
|
293
|
|
|
$query_cond .= "headers REGEXP '[BCC|Bcc|bcc]:.*$email' OR "; |
|
294
|
|
|
$query_cond .= "headers REGEXP '[R|r]eply-[T|t]o:.*$email'"; |
|
295
|
|
|
$query_cond .= ' ) '; /* End 4th */ |
|
296
|
|
|
$query_cond .= ' ) '; /* End 3rd */ |
|
297
|
|
|
$query_cond .= ' ) '; /* End 1st */ |
|
298
|
|
|
break; |
|
299
|
|
|
case 'cc': |
|
300
|
|
|
$query_cond .= empty( $query_cond ) && ! $where_clause ? ' WHERE ' : ' AND '; |
|
301
|
|
|
$query_cond .= ' ( '; /* Begin 1st */ |
|
302
|
|
|
$query_cond .= "headers <> ''"; |
|
303
|
|
|
$query_cond .= ' AND '; |
|
304
|
|
|
$query_cond .= ' ( '; /* Begin 2nd */ |
|
305
|
|
|
$query_cond .= "headers REGEXP '[CC|Cc|cc]:.*$email' "; |
|
306
|
|
|
$query_cond .= ' ) '; /* End 2nd */ |
|
307
|
|
|
$query_cond .= ' ) '; /* End 1st */ |
|
308
|
|
|
break; |
|
309
|
|
|
case 'bcc': |
|
310
|
|
|
$query_cond .= empty( $query_cond ) && ! $where_clause ? ' WHERE ' : ' AND '; |
|
311
|
|
|
$query_cond .= ' ( '; /* Begin 1st */ |
|
312
|
|
|
$query_cond .= "headers <> ''"; |
|
313
|
|
|
$query_cond .= ' AND '; |
|
314
|
|
|
$query_cond .= ' ( '; /* Begin 2nd */ |
|
315
|
|
|
$query_cond .= "headers REGEXP '[BCC|Bcc|bcc]:.*$email' "; |
|
316
|
|
|
$query_cond .= ' ) '; /* End 2nd */ |
|
317
|
|
|
$query_cond .= ' ) '; /* End 1st */ |
|
318
|
|
|
break; |
|
319
|
|
|
case 'reply-to': |
|
320
|
|
|
$query_cond .= empty( $query_cond ) && ! $where_clause ? ' WHERE ' : ' AND '; |
|
321
|
|
|
$query_cond .= ' ( '; /* Begin 1st */ |
|
322
|
|
|
$query_cond .= "headers <> ''"; |
|
323
|
|
|
$query_cond .= ' AND '; |
|
324
|
|
|
$query_cond .= ' ( '; /* Begin 2nd */ |
|
325
|
|
|
$query_cond .= "headers REGEXP '[R|r]eply-to:.*$email' "; |
|
326
|
|
|
$query_cond .= ' ) '; /* End 2nd */ |
|
327
|
|
|
$query_cond .= ' ) '; /* End 1st */ |
|
328
|
|
|
break; |
|
329
|
|
|
} |
|
330
|
|
|
} |
|
331
|
|
|
} else { |
|
332
|
|
|
if ( $where_clause ) { |
|
333
|
|
|
$query_cond .= " AND ( to_email LIKE '%$search_term%' OR subject LIKE '%$search_term%' ) "; |
|
334
|
|
|
} else { |
|
335
|
|
|
$query_cond .= " WHERE ( to_email LIKE '%$search_term%' OR subject LIKE '%$search_term%' ) "; |
|
336
|
|
|
} |
|
337
|
|
|
} |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
if ( isset( $request['d'] ) && $request['d'] !== '' ) { |
|
341
|
|
|
$search_date = trim( esc_sql( $request['d'] ) ); |
|
342
|
|
|
if ( empty( $query_cond ) && ! $where_clause ) { |
|
343
|
|
|
$query_cond .= " WHERE sent_date BETWEEN '$search_date 00:00:00' AND '$search_date 23:59:59' "; |
|
344
|
|
|
} else { |
|
345
|
|
|
$query_cond .= " AND sent_date BETWEEN '$search_date 00:00:00' AND '$search_date 23:59:59' "; |
|
346
|
|
|
} |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
if ( isset( $request['el_log_list_type'] ) ) { |
|
350
|
|
|
if ( 'sent' === $request['el_log_list_type'] ) { |
|
351
|
|
|
$query_cond .= ' WHERE result = 1'; |
|
352
|
|
|
} |
|
353
|
|
|
if ( 'failed' === $request['el_log_list_type'] ) { |
|
354
|
|
|
$query_cond .= ' WHERE result = 0'; |
|
355
|
|
|
} |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
// Ordering parameters. |
|
359
|
|
|
$orderby = ! empty( $request['orderby'] ) ? esc_sql( $request['orderby'] ) : 'sent_date'; |
|
360
|
|
|
$order = ! empty( $request['order'] ) ? esc_sql( $request['order'] ) : 'DESC'; |
|
361
|
|
|
|
|
362
|
|
|
if ( ! empty( $orderby ) & ! empty( $order ) ) { |
|
|
|
|
|
|
363
|
|
|
$query_cond .= ' ORDER BY ' . $orderby . ' ' . $order; |
|
|
|
|
|
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
|
|
return $query_cond; |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
/** |
|
370
|
|
|
* Create email log table. |
|
371
|
|
|
* |
|
372
|
|
|
* @global object $wpdb |
|
373
|
|
|
*/ |
|
374
|
|
|
public function create_table_if_needed() { |
|
375
|
|
|
global $wpdb; |
|
376
|
|
|
|
|
377
|
|
|
$table_name = $this->get_log_table_name(); |
|
378
|
|
|
|
|
379
|
|
|
if ( $wpdb->get_var( "show tables like '{$table_name}'" ) != $table_name ) { |
|
380
|
|
|
|
|
381
|
|
|
$sql = $this->get_create_table_query(); |
|
382
|
|
|
|
|
383
|
|
|
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
|
384
|
|
|
dbDelta( $sql ); |
|
385
|
|
|
|
|
386
|
|
|
add_option( self::DB_OPTION_NAME, self::DB_VERSION ); |
|
387
|
|
|
} |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
/** |
|
391
|
|
|
* Get the total number of email logs. |
|
392
|
|
|
* |
|
393
|
|
|
* @return int Total email log count |
|
394
|
|
|
*/ |
|
395
|
|
|
public function get_logs_count() { |
|
396
|
|
|
global $wpdb; |
|
397
|
|
|
|
|
398
|
|
|
$query = 'SELECT count(*) FROM ' . $this->get_log_table_name(); |
|
399
|
|
|
|
|
400
|
|
|
return $wpdb->get_var( $query ); |
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
/** |
|
404
|
|
|
* Get the total number of email logs in the result after search or filtering. |
|
405
|
|
|
* |
|
406
|
|
|
* @param array $request Request object. |
|
407
|
|
|
* |
|
408
|
|
|
* @return int Total email log count in the result. |
|
409
|
|
|
* |
|
410
|
|
|
* @since 2.5.0 |
|
411
|
|
|
*/ |
|
412
|
|
|
public function get_result_logs_count( $request ) { |
|
413
|
|
|
global $wpdb; |
|
414
|
|
|
|
|
415
|
|
|
$query = 'SELECT count(*) FROM ' . $this->get_log_table_name(); |
|
416
|
|
|
|
|
417
|
|
|
$query_condition = $this->build_query_condition( $request ); |
|
418
|
|
|
|
|
419
|
|
|
$query .= $query_condition; |
|
420
|
|
|
|
|
421
|
|
|
return $wpdb->get_var( $query ); |
|
422
|
|
|
} |
|
423
|
|
|
|
|
424
|
|
|
/** |
|
425
|
|
|
* Fetches the log id by item data. |
|
426
|
|
|
* |
|
427
|
|
|
* Use this method to get the log item id when the error instance only returns the log item data. |
|
428
|
|
|
* |
|
429
|
|
|
* @param array $data Array of Email information { |
|
430
|
|
|
* @type array|string to |
|
431
|
|
|
* @type string subject |
|
432
|
|
|
* @type string message |
|
433
|
|
|
* @type array|string headers |
|
434
|
|
|
* @type array|string attachments |
|
435
|
|
|
* } |
|
436
|
|
|
* |
|
437
|
|
|
* @return int Log item id. |
|
438
|
|
|
*/ |
|
439
|
|
|
public function fetch_log_id_by_data( $data ) { |
|
440
|
|
|
if ( empty( $data ) || ! is_array( $data ) ) { |
|
441
|
|
|
return 0; |
|
442
|
|
|
} |
|
443
|
|
|
|
|
444
|
|
|
global $wpdb; |
|
445
|
|
|
$table_name = $this->get_log_table_name(); |
|
446
|
|
|
|
|
447
|
|
|
$query = "SELECT ID FROM {$table_name}"; |
|
448
|
|
|
$query_cond = ''; |
|
449
|
|
|
$where = array(); |
|
450
|
|
|
|
|
451
|
|
|
// Execute the following `if` conditions only when $data is array. |
|
452
|
|
|
if ( array_key_exists( 'to', $data ) ) { |
|
453
|
|
|
// Since the value is stored as CSV in DB, convert the values from error data to CSV to compare. |
|
454
|
|
|
$to_email = Util\stringify( $data['to'] ); |
|
|
|
|
|
|
455
|
|
|
|
|
456
|
|
|
$to_email = trim( esc_sql( $to_email ) ); |
|
|
|
|
|
|
457
|
|
|
$where[] = "to_email = '$to_email'"; |
|
458
|
|
|
} |
|
459
|
|
|
|
|
460
|
|
|
if ( array_key_exists( 'subject', $data ) ) { |
|
461
|
|
|
$subject = trim( esc_sql( $data['subject'] ) ); |
|
462
|
|
|
$where[] = "subject = '$subject'"; |
|
463
|
|
|
} |
|
464
|
|
|
|
|
465
|
|
|
if ( array_key_exists( 'attachments', $data ) ) { |
|
466
|
|
|
if ( is_array( $data['attachments'] ) ) { |
|
467
|
|
|
$attachments = count( $data['attachments'] ) > 0 ? 'true' : 'false'; |
|
468
|
|
|
} else { |
|
469
|
|
|
$attachments = empty( $data['attachments'] ) ? 'false' : 'true'; |
|
470
|
|
|
} |
|
471
|
|
|
$attachments = trim( esc_sql( $attachments ) ); |
|
472
|
|
|
$where[] = "attachments = '$attachments'"; |
|
473
|
|
|
} |
|
474
|
|
|
|
|
475
|
|
|
foreach ( $where as $index => $value ) { |
|
476
|
|
|
$query_cond .= 0 === $index ? ' WHERE ' : ' AND '; |
|
477
|
|
|
$query_cond .= $value; |
|
478
|
|
|
} |
|
479
|
|
|
|
|
480
|
|
|
// Get only the latest logged item when multiple rows match. |
|
481
|
|
|
$query_cond .= ' ORDER BY id DESC LIMIT 1'; |
|
482
|
|
|
|
|
483
|
|
|
$query = $query . $query_cond; |
|
484
|
|
|
|
|
485
|
|
|
return absint( $wpdb->get_var( $query ) ); |
|
486
|
|
|
} |
|
487
|
|
|
|
|
488
|
|
|
/** |
|
489
|
|
|
* Get the list of starred log items for a user. |
|
490
|
|
|
* |
|
491
|
|
|
* @since 2.5.0 |
|
492
|
|
|
* |
|
493
|
|
|
* @param int|null $user_id User id. If empty, then current user id is used. |
|
494
|
|
|
* |
|
495
|
|
|
* @return array Starred log list items. |
|
496
|
|
|
*/ |
|
497
|
|
|
public function get_starred_log_item_ids( $user_id = null ) { |
|
498
|
|
|
if ( empty( $user_id ) ) { |
|
499
|
|
|
$user_id = get_current_user_id(); |
|
500
|
|
|
} |
|
501
|
|
|
|
|
502
|
|
|
$starred_log_item_ids = get_user_meta( |
|
503
|
|
|
$user_id, |
|
504
|
|
|
self::STARRED_LOGS_META_KEY, |
|
505
|
|
|
true |
|
506
|
|
|
); |
|
507
|
|
|
|
|
508
|
|
|
if ( empty( $starred_log_item_ids ) || ! is_array( $starred_log_item_ids ) ) { |
|
509
|
|
|
return []; |
|
510
|
|
|
} |
|
511
|
|
|
|
|
512
|
|
|
return $starred_log_item_ids; |
|
513
|
|
|
} |
|
514
|
|
|
|
|
515
|
|
|
/** |
|
516
|
|
|
* Star (or Unstar) an email log id. |
|
517
|
|
|
* |
|
518
|
|
|
* @since 2.5.0 |
|
519
|
|
|
* |
|
520
|
|
|
* @param int $log_id Log id. |
|
521
|
|
|
* @param bool $un_star Whether to unstar an email or star it. Default false. |
|
522
|
|
|
* @param int|null $user_id User id. Default null. Current user id is used if not specified. |
|
523
|
|
|
* |
|
524
|
|
|
* @return bool Whether the update was successful. |
|
525
|
|
|
*/ |
|
526
|
|
|
public function star_log_item( $log_id, $un_star = false, $user_id = null ) { |
|
527
|
|
|
if ( empty( $user_id ) ) { |
|
528
|
|
|
$user_id = get_current_user_id(); |
|
529
|
|
|
} |
|
530
|
|
|
|
|
531
|
|
|
$starred_log_ids = $this->get_starred_log_item_ids( $user_id ); |
|
532
|
|
|
|
|
533
|
|
|
if ( $un_star ) { |
|
534
|
|
|
$key = array_search( $log_id, $starred_log_ids, true ); |
|
535
|
|
|
unset( $starred_log_ids[ $key ] ); |
|
536
|
|
|
} else { |
|
537
|
|
|
$starred_log_ids = array_merge( $starred_log_ids, array( $log_id ) ); |
|
538
|
|
|
} |
|
539
|
|
|
|
|
540
|
|
|
return update_user_meta( |
|
541
|
|
|
$user_id, |
|
542
|
|
|
self::STARRED_LOGS_META_KEY, |
|
543
|
|
|
$starred_log_ids |
|
544
|
|
|
); |
|
545
|
|
|
} |
|
546
|
|
|
|
|
547
|
|
|
/** |
|
548
|
|
|
* Sets email sent status and error message for the given log item when email fails. |
|
549
|
|
|
* |
|
550
|
|
|
* @param int $log_item_id ID of the log item whose email sent status should be set to failed. |
|
551
|
|
|
* @param string $message Error message. |
|
552
|
|
|
* |
|
553
|
|
|
* @since 2.4.0 Include error message during update. |
|
554
|
|
|
* @since 2.3.0 |
|
555
|
|
|
* |
|
556
|
|
|
* @global \wpdb $wpdb |
|
557
|
|
|
* |
|
558
|
|
|
* @see TableManager::get_log_table_name() |
|
559
|
|
|
*/ |
|
560
|
|
|
public function mark_log_as_failed( $log_item_id, $message ) { |
|
561
|
|
|
global $wpdb; |
|
562
|
|
|
$table_name = $this->get_log_table_name(); |
|
563
|
|
|
|
|
564
|
|
|
$wpdb->update( |
|
565
|
|
|
$table_name, |
|
566
|
|
|
array( |
|
567
|
|
|
'result' => '0', |
|
568
|
|
|
'error_message' => $message, |
|
569
|
|
|
), |
|
570
|
|
|
array( 'ID' => $log_item_id ), |
|
571
|
|
|
array( |
|
572
|
|
|
'%d', // `result` format. |
|
573
|
|
|
'%s', // `error_message` format. |
|
574
|
|
|
), |
|
575
|
|
|
array( |
|
576
|
|
|
'%d', // `ID` format. |
|
577
|
|
|
) |
|
578
|
|
|
); |
|
579
|
|
|
} |
|
580
|
|
|
|
|
581
|
|
|
/** |
|
582
|
|
|
* Updates the DB schema. |
|
583
|
|
|
* |
|
584
|
|
|
* Adds new columns to the Database as of v0.2. |
|
585
|
|
|
* |
|
586
|
|
|
* @since 2.3.0 |
|
587
|
|
|
*/ |
|
588
|
|
|
private function update_table_if_needed() { |
|
589
|
|
|
if ( get_option( self::DB_OPTION_NAME, false ) === self::DB_VERSION ) { |
|
590
|
|
|
return; |
|
591
|
|
|
} |
|
592
|
|
|
|
|
593
|
|
|
$sql = $this->get_create_table_query(); |
|
594
|
|
|
|
|
595
|
|
|
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
|
596
|
|
|
dbDelta( $sql ); |
|
597
|
|
|
|
|
598
|
|
|
update_option( self::DB_OPTION_NAME, self::DB_VERSION ); |
|
599
|
|
|
} |
|
600
|
|
|
|
|
601
|
|
|
/** |
|
602
|
|
|
* Gets the Create Table query. |
|
603
|
|
|
* |
|
604
|
|
|
* @since 2.4.0 Added error_message column. |
|
605
|
|
|
* @since 2.3.0 |
|
606
|
|
|
* |
|
607
|
|
|
* @return string |
|
608
|
|
|
*/ |
|
609
|
|
|
private function get_create_table_query() { |
|
610
|
|
|
global $wpdb; |
|
611
|
|
|
$table_name = $this->get_log_table_name(); |
|
612
|
|
|
$charset_collate = $wpdb->get_charset_collate(); |
|
613
|
|
|
|
|
614
|
|
|
$sql = 'CREATE TABLE ' . $table_name . ' ( |
|
615
|
|
|
id mediumint(9) NOT NULL AUTO_INCREMENT, |
|
616
|
|
|
to_email VARCHAR(500) NOT NULL, |
|
617
|
|
|
subject VARCHAR(500) NOT NULL, |
|
618
|
|
|
message TEXT NOT NULL, |
|
619
|
|
|
headers TEXT NOT NULL, |
|
620
|
|
|
attachments TEXT NOT NULL, |
|
621
|
|
|
sent_date timestamp NOT NULL, |
|
622
|
|
|
attachment_name VARCHAR(1000), |
|
623
|
|
|
ip_address VARCHAR(15), |
|
624
|
|
|
result TINYINT(1), |
|
625
|
|
|
error_message VARCHAR(1000), |
|
626
|
|
|
PRIMARY KEY (id) |
|
627
|
|
|
) ' . $charset_collate . ';'; |
|
628
|
|
|
|
|
629
|
|
|
return $sql; |
|
630
|
|
|
} |
|
631
|
|
|
|
|
632
|
|
|
/** |
|
633
|
|
|
* Callback for the Array filter. |
|
634
|
|
|
* |
|
635
|
|
|
* @since 2.3.0 |
|
636
|
|
|
* |
|
637
|
|
|
* @param string $column A column from the array Columns. |
|
638
|
|
|
* |
|
639
|
|
|
* @return bool |
|
640
|
|
|
*/ |
|
641
|
|
|
private function validate_columns( $column ) { |
|
642
|
|
|
return in_array( $column, array( 'to' ), true ); |
|
643
|
|
|
} |
|
644
|
|
|
|
|
645
|
|
|
/** |
|
646
|
|
|
* Query log items by column. |
|
647
|
|
|
* |
|
648
|
|
|
* @since 2.3.0 |
|
649
|
|
|
* |
|
650
|
|
|
* @param array $columns Key value pair based on which items should be retrieved. |
|
651
|
|
|
* |
|
652
|
|
|
* @uses \EmailLog\Core\DB\TableManager::validate_columns() |
|
653
|
|
|
* |
|
654
|
|
|
* @return array|object|null |
|
655
|
|
|
*/ |
|
656
|
|
|
public function query_log_items_by_column( $columns ) { |
|
657
|
|
|
if ( ! is_array( $columns ) ) { |
|
|
|
|
|
|
658
|
|
|
return; |
|
659
|
|
|
} |
|
660
|
|
|
|
|
661
|
|
|
// Since we support PHP v5.2.4, we cannot use ARRAY_FILTER_USE_KEY |
|
662
|
|
|
// TODO: PHP v5.5: Once WordPress updates minimum PHP version to v5.5, start using ARRAY_FILTER_USE_KEY. |
|
663
|
|
|
$columns_keys = array_keys( $columns ); |
|
664
|
|
|
if ( ! array_filter( $columns_keys, array( $this, 'validate_columns' ) ) ) { |
|
665
|
|
|
return; |
|
666
|
|
|
} |
|
667
|
|
|
|
|
668
|
|
|
global $wpdb; |
|
669
|
|
|
|
|
670
|
|
|
$table_name = $this->get_log_table_name(); |
|
671
|
|
|
$query = "SELECT id, sent_date, to_email, subject FROM {$table_name}"; |
|
672
|
|
|
$query_cond = ''; |
|
673
|
|
|
$where = array(); |
|
674
|
|
|
|
|
675
|
|
|
// Execute the following `if` conditions only when $data is array. |
|
676
|
|
|
if ( array_key_exists( 'to', $columns ) ) { |
|
677
|
|
|
// Since the value is stored as CSV in DB, convert the values from error data to CSV to compare. |
|
678
|
|
|
$to_email = Util\stringify( $columns['to'] ); |
|
|
|
|
|
|
679
|
|
|
|
|
680
|
|
|
$to_email = trim( esc_sql( $to_email ) ); |
|
|
|
|
|
|
681
|
|
|
$where[] = "to_email = '$to_email'"; |
|
682
|
|
|
|
|
683
|
|
|
foreach ( $where as $index => $value ) { |
|
684
|
|
|
$query_cond .= 0 === $index ? ' WHERE ' : ' AND '; |
|
685
|
|
|
$query_cond .= $value; |
|
686
|
|
|
} |
|
687
|
|
|
|
|
688
|
|
|
// Get only the latest logged item when multiple rows match. |
|
689
|
|
|
$query_cond .= ' ORDER BY id DESC'; |
|
690
|
|
|
|
|
691
|
|
|
$query = $query . $query_cond; |
|
692
|
|
|
|
|
693
|
|
|
return $wpdb->get_results( $query ); |
|
694
|
|
|
} |
|
695
|
|
|
} |
|
696
|
|
|
} |
|
697
|
|
|
|