1
|
|
|
<?php namespace EmailLog\Util; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Email Log Helper functions. |
5
|
|
|
* Some of these functions would be used the addons. |
6
|
|
|
*/ |
7
|
|
|
defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Perform additional sanitation of emails. |
11
|
|
|
* |
12
|
|
|
* @since 1.9 |
13
|
|
|
* |
14
|
|
|
* @param string $email Email string to be sanitized. |
15
|
|
|
* @param bool $multiple (Optional) Should multiple emails be allowed. True by default. |
16
|
|
|
* |
17
|
|
|
* @return string Sanitized email. |
18
|
|
|
*/ |
19
|
|
|
function sanitize_email( $email, $multiple = true ) { |
20
|
13 |
|
$emails = explode( ',', $email ); |
21
|
13 |
|
if ( ! $multiple ) { |
22
|
3 |
|
$emails = array_slice( $emails, 0, 1 ); |
23
|
|
|
} |
24
|
|
|
|
25
|
13 |
|
$cleaned_emails = array_map( __NAMESPACE__ . '\\sanitize_email_with_name', $emails ); |
26
|
|
|
|
27
|
13 |
|
return implode( ', ', $cleaned_emails ); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Sanitize email with name. |
32
|
|
|
* |
33
|
|
|
* @since 1.9 |
34
|
|
|
* |
35
|
|
|
* @param string $string Email string to be sanitized. |
36
|
|
|
* |
37
|
|
|
* @return string Sanitized email. |
38
|
|
|
*/ |
39
|
|
|
function sanitize_email_with_name( $string ) { |
40
|
13 |
|
$string = trim( $string ); |
41
|
|
|
|
42
|
13 |
|
$bracket_pos = strpos( $string, '<' ); |
43
|
13 |
|
if ( false !== $bracket_pos ) { |
44
|
5 |
|
if ( $bracket_pos > 0 ) { |
45
|
5 |
|
$name = substr( $string, 0, $bracket_pos ); |
46
|
5 |
|
$name = trim( $name ); |
47
|
|
|
|
48
|
5 |
|
$email = substr( $string, $bracket_pos + 1 ); |
49
|
5 |
|
$email = str_replace( '>', '', $email ); |
50
|
|
|
|
51
|
5 |
|
return sanitize_text_field( $name ) . ' <' . \sanitize_email( $email ) . '>'; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
8 |
|
return \sanitize_email( $string ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Gets the columns to export logs. |
60
|
|
|
* |
61
|
|
|
* If the More Fields add-on is active, additional columns are returned. |
62
|
|
|
* |
63
|
|
|
* @since 2.0.0 |
64
|
|
|
* |
65
|
|
|
* @return string[] List of Columns to export. |
66
|
|
|
*/ |
67
|
|
|
function get_log_columns_to_export() { |
68
|
|
|
|
69
|
|
|
if ( is_plugin_active( 'email-log-more-fields/email-log-more-fields.php' ) ) { |
70
|
|
|
return array( |
71
|
|
|
'id', |
72
|
|
|
'sent_date', |
73
|
|
|
'to_email', |
74
|
|
|
'subject', |
75
|
|
|
'from', |
76
|
|
|
'cc', |
77
|
|
|
'bcc', |
78
|
|
|
'reply-to', |
79
|
|
|
'attachment', |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return array( 'id', 'sent_date', 'to_email', 'subject' ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Is it an admin request and not an ajax request. |
88
|
|
|
* |
89
|
|
|
* @since 2.1 |
90
|
|
|
* |
91
|
|
|
* @return bool True if admin non ajax request, False otherwise. |
92
|
|
|
*/ |
93
|
|
|
function is_admin_non_ajax_request() { |
94
|
|
|
if ( function_exists( 'wp_doing_ajax' ) && wp_doing_ajax() ) { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return is_admin(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Checks the Checkbox when values are present in a given array. |
107
|
|
|
* |
108
|
|
|
* Use this function in Checkbox fields. |
109
|
|
|
* |
110
|
|
|
* @since 2.1.0 |
111
|
|
|
* |
112
|
|
|
* @param array $values List of all possible values. |
113
|
|
|
* @param string $current The current value to be checked. |
114
|
|
|
*/ |
115
|
|
|
function checked_array( $values, $current ) { |
116
|
2 |
|
if ( ! is_array( $values ) ) { |
117
|
1 |
|
return; |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
if ( in_array( $current, $values, true ) ) { |
121
|
1 |
|
echo "checked='checked'"; |
122
|
|
|
} |
123
|
1 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Gets the log row class by result code. |
127
|
|
|
* |
128
|
|
|
* @param int $result Mail sent status. |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
function get_log_row_class_by_result_code( $result ) { |
133
|
|
|
$log_row_classes = array( |
134
|
|
|
0 => 'el_email_sent_status--failed', |
135
|
|
|
1 => 'el_email_sent_status--sent', |
136
|
|
|
); |
137
|
|
|
if ( empty( $result ) ) { |
138
|
|
|
return $log_row_classes[0]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$result = absint( $result ); |
142
|
|
|
if ( array_key_exists( $result, $log_row_classes ) ) { |
143
|
|
|
return $log_row_classes[ $result ]; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $log_row_classes[0]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Return failure icon. |
151
|
|
|
* |
152
|
|
|
* @since 2.3.2 |
153
|
|
|
* |
154
|
|
|
* @return string Failure icon markup. |
155
|
|
|
*/ |
156
|
|
|
function get_failure_icon() { |
157
|
|
|
return <<<EOT |
158
|
|
|
<span class="dashicons dashicons-dismiss"></span> |
159
|
|
|
EOT; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Return success icon. |
164
|
|
|
* |
165
|
|
|
* @since 2.3.2 |
166
|
|
|
* |
167
|
|
|
* @return string Success icon markup. |
168
|
|
|
*/ |
169
|
|
|
function get_success_icon() { |
170
|
|
|
return <<<EOT |
171
|
|
|
<span class="dashicons dashicons-yes-alt"></span> |
172
|
|
|
EOT; |
173
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Stringify arrays. |
178
|
|
|
* |
179
|
|
|
* If the parameter is an array, then return delimiter separated values of the array. |
180
|
|
|
* Otherwise return the parameter. |
181
|
|
|
* |
182
|
|
|
* @since 2.3.0 |
183
|
|
|
* @since 2.3.2 Renamed name to `Stringify`. |
184
|
|
|
* |
185
|
|
|
* @param array|string $may_be_array The array whose values are to be converted to string. |
186
|
|
|
* @param string $delimiter Optional. Default is `,`. |
187
|
|
|
* |
188
|
|
|
* @return string Stringified value. |
189
|
|
|
*/ |
190
|
|
|
function stringify( $may_be_array, $delimiter = ',' ) { |
191
|
1 |
|
if ( ! is_array( $may_be_array ) ) { |
192
|
|
|
return (string) $may_be_array; |
193
|
|
|
} |
194
|
|
|
|
195
|
1 |
|
return implode( $delimiter, $may_be_array ); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Gets the User defined Date time format. |
200
|
|
|
* |
201
|
|
|
* @used-by \EmailLog\Core\UI\Setting\CoreSetting |
202
|
|
|
* @used-by \EmailLog\Util\render_auto_delete_logs_next_run_schedule() |
203
|
|
|
* |
204
|
|
|
* @since 2.3.0 |
205
|
|
|
* |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
|
|
function get_user_defined_date_time_format() { |
209
|
|
|
return sprintf( '%1$s %2$s', get_option( 'date_format', 'Y-m-d' ), get_option( 'time_format', 'g:i a' ) ); |
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Renders the next run auto delete logs schedule in Date and time format set within WordPress. |
214
|
|
|
* |
215
|
|
|
* @used-by \EmailLog\Addon\UI\Setting\DashboardWidget |
216
|
|
|
* @used-by \EmailLog\Core\UI\Component\AutoDeleteLogsSetting |
217
|
|
|
* |
218
|
|
|
* @since 2.3.0 |
219
|
|
|
*/ |
220
|
|
|
function render_auto_delete_logs_next_run_schedule() { |
221
|
|
|
?> |
222
|
|
|
<?php if ( wp_next_scheduled( 'el_scheduled_delete_logs' ) ) : ?> |
223
|
|
|
<p> |
224
|
|
|
<?php _e( 'Auto delete logs cron will be triggered next at', 'email-log' ); ?>: |
225
|
|
|
<?php $date_time_format = get_user_defined_date_time_format(); ?> |
226
|
|
|
<strong><?php echo get_date_from_gmt( date( 'Y-m-d H:i:s', wp_next_scheduled( 'el_scheduled_delete_logs' ) ), $date_time_format ); ?></strong> |
|
|
|
|
227
|
|
|
</p> |
228
|
|
|
<?php endif; ?> |
229
|
|
|
<?php |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Gets the value by key from the array. |
234
|
|
|
* |
235
|
|
|
* If the key isn't found, then null is returned. |
236
|
|
|
* |
237
|
|
|
* @since 2.3.0 |
238
|
|
|
* |
239
|
|
|
* @param array $array The actual array. |
240
|
|
|
* @param string $key The key whose value is to be retrieved. |
241
|
|
|
* @param string $default Optional. |
242
|
|
|
* |
243
|
|
|
* @return mixed|null |
244
|
|
|
*/ |
245
|
|
|
function el_array_get( $array, $key, $default = null ) { |
246
|
|
|
return isset( $array[ $key ] ) ? $array[ $key ] : $default; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Returns TRUE if the given search term is Advanced Search Term. |
251
|
|
|
* |
252
|
|
|
* @param string $term Search Term. |
253
|
|
|
* |
254
|
|
|
* @return bool |
255
|
|
|
*/ |
256
|
|
|
function is_advanced_search_term( $term ) { |
257
|
|
|
if ( ! is_string( $term ) ) { |
|
|
|
|
258
|
|
|
return false; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
$predicates = get_advanced_search_term_predicates( $term ); |
262
|
|
|
|
263
|
|
|
return ! empty( $predicates ); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Gets the Search Term Predicates. |
268
|
|
|
* |
269
|
|
|
* Example: |
270
|
|
|
* |
271
|
|
|
* If $term = to:[email protected] then, |
272
|
|
|
* |
273
|
|
|
* the output would be |
274
|
|
|
* |
275
|
|
|
* $output = array( |
276
|
|
|
* 'to' => [email protected] |
277
|
|
|
* ) |
278
|
|
|
* |
279
|
|
|
* @since 2.3.0 |
280
|
|
|
* |
281
|
|
|
* @param string $term Search Term. |
282
|
|
|
* |
283
|
|
|
* @return array |
284
|
|
|
*/ |
285
|
|
|
function get_advanced_search_term_predicates( $term ) { |
286
|
|
|
if ( ! is_string( $term ) ) { |
|
|
|
|
287
|
|
|
return array(); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
$predicates = explode( ' ', $term ); |
291
|
|
|
$predicates_organized = array(); |
292
|
|
|
|
293
|
|
|
foreach ( $predicates as $predicate ) { |
294
|
|
|
$is_match = preg_match( '/(id|email|to|cc|bcc|reply-to):(.*)$/', $predicate, $matches ); |
295
|
|
|
if ( 1 === $is_match ) { |
296
|
|
|
$predicates_organized[ $matches[1] ] = $matches[2]; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
return $predicates_organized; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Gets the Advanced Search URL. |
305
|
|
|
* |
306
|
|
|
* @since 2.3.0 |
307
|
|
|
* |
308
|
|
|
* @return string |
309
|
|
|
*/ |
310
|
|
|
function get_advanced_search_url() { |
311
|
|
|
$admin_url = get_admin_url( null, 'admin.php?page=email-log' ); |
312
|
|
|
|
313
|
|
|
return add_query_arg( 'el_as', 1, $admin_url ); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Gets the Column labels to be used in LogList table. |
318
|
|
|
* |
319
|
|
|
* Deprecated. This is currently used by Email Log - Export Logs add-on v1.2.1 and will eventually be removed. |
320
|
|
|
* |
321
|
|
|
* @since 2.3.0 |
322
|
|
|
* @since 2.3.2 Deprecated. |
323
|
|
|
* |
324
|
|
|
* @param string $db_column Column ID. |
325
|
|
|
* |
326
|
|
|
* @return string Column label. |
327
|
|
|
*/ |
328
|
|
|
function get_column_label_by_db_column( $db_column ) { |
329
|
|
|
return get_column_label( $db_column ); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Get Column label based on column name. |
334
|
|
|
* |
335
|
|
|
* @since 2.3.2 |
336
|
|
|
* |
337
|
|
|
* @param string $column_name Column name. |
338
|
|
|
* |
339
|
|
|
* @return string Column label. |
340
|
|
|
*/ |
341
|
|
|
function get_column_label( $column_name ) { |
342
|
1 |
|
$labels = get_column_label_map(); |
343
|
|
|
|
344
|
1 |
|
if ( ! array_key_exists( $column_name, $labels ) ) { |
345
|
|
|
return $column_name; |
346
|
|
|
} |
347
|
|
|
|
348
|
1 |
|
return $labels[ $column_name ]; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Returns an array of Email Log columns. |
353
|
|
|
* |
354
|
|
|
* Keys are the column names in the DB. |
355
|
|
|
* This holds true except for CC, BCC & Reply To as they are put under one column `headers`. |
356
|
|
|
* |
357
|
|
|
* @since 2.3.2 |
358
|
|
|
* |
359
|
|
|
* @return array Key value pair of Email Log columns. |
360
|
|
|
*/ |
361
|
|
|
function get_column_label_map() { |
362
|
|
|
$labels = array( |
363
|
1 |
|
'id' => __( 'ID', 'email-log' ), |
364
|
1 |
|
'to_email' => __( 'To', 'email-log' ), |
365
|
1 |
|
'subject' => __( 'Subject', 'email-log' ), |
366
|
1 |
|
'message' => __( 'Message', 'email-log' ), |
367
|
1 |
|
'attachments' => __( 'Attachment', 'email-log' ), |
368
|
1 |
|
'sent_date' => __( 'Sent at', 'email-log' ), |
369
|
1 |
|
'from' => __( 'From', 'email-log' ), |
370
|
1 |
|
'cc' => __( 'CC', 'email-log' ), |
371
|
1 |
|
'bcc' => __( 'BCC', 'email-log' ), |
372
|
1 |
|
'reply_to' => __( 'Reply To', 'email-log' ), |
373
|
1 |
|
'result' => __( 'Sent Status', 'email-log' ), |
374
|
1 |
|
'ip_address' => __( 'IP Address', 'email-log' ), |
375
|
1 |
|
'result' => __( 'Sent Status', 'email-log' ), |
376
|
|
|
); |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Filters the Labels used through out the Email Log plugin. |
380
|
|
|
* |
381
|
|
|
* @since 2.3.2 |
382
|
|
|
* |
383
|
|
|
* @param array $labels List of DB Columns and its respective labels which are internationalized string. |
384
|
|
|
* Example: 'id' => __( 'ID', 'email-log' ), |
385
|
|
|
*/ |
386
|
1 |
|
return apply_filters( 'el_db_column_labels', $labels ); |
387
|
|
|
} |
388
|
|
|
|