Passed
Pull Request — dev/2.3.2 (#218)
by Sudar
12:33
created

get_email_sent_svg()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 46
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 0
cp 0
crap 2
rs 9.1781
c 0
b 0
f 0
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 3
	}
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 2
 * Checks the Checkbox when values are present in a given array.
107 1
 *
108
 * Use this function in Checkbox fields.
109
 *
110 1
 * @since 2.1.0
111 1
 *
112 1
 * @param array  $values  List of all possible values.
113 1
 * @param string $current The current value to be checked.
114
 */
115
function checked_array( $values, $current ) {
116
	if ( ! is_array( $values ) ) {
117
		return;
118
	}
119
120
	if ( in_array( $current, $values ) ) {
121
		echo "checked='checked'";
122
	}
123
}
124
125
/**
126
 * Returns the Email failure SVG.
127
 *
128
 * @see   https://www.flaticon.com/free-icon/do-not-disturb-rounded-sign_61072
129
 * @since 2.4.0
130 1
 *
131
 * @return string
132
 */
133
function get_email_failed_svg() {
134 1
	return <<<EOT
135
<?xml version="1.0" encoding="iso-8859-1"?>
136
<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
137
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
138
<svg class="el_sent_status--failed" version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
139
	 width="15px" height="15px" viewBox="0 0 510 510" style="enable-background:new 0 0 510 510;" xml:space="preserve">
140
<g>
141
	<g id="do-not-disturb">
142
		<path d="M255,0C114.75,0,0,114.75,0,255s114.75,255,255,255s255-114.75,255-255S395.25,0,255,0z M51,255c0-112.2,91.8-204,204-204
143
			c45.9,0,89.25,15.3,124.95,43.35l-285.6,285.6C66.3,344.25,51,300.9,51,255z M255,459c-45.9,0-89.25-15.3-124.95-43.35
144
			L415.65,130.05C443.7,165.75,459,209.1,459,255C459,367.2,367.2,459,255,459z"/>
145
	</g>
146
</g>
147
<g>
148
</g>
149
<g>
150
</g>
151
<g>
152
</g>
153
<g>
154
</g>
155
<g>
156
</g>
157
<g>
158
</g>
159
<g>
160
</g>
161
<g>
162
</g>
163
<g>
164
</g>
165
<g>
166
</g>
167
<g>
168
</g>
169
<g>
170
</g>
171
<g>
172
</g>
173
<g>
174
</g>
175
<g>
176
</g>
177
</svg>
178
EOT;
179
}
180
181
/**
182
 * Returns the Email sent SVG.
183
 *
184
 * @see   https://www.flaticon.com/free-icon/tick-inside-circle_61222
185
 * @since 2.4.0
186
 *
187
 * @return string
188
 */
189
function get_email_sent_svg() {
190
	return <<<EOT
191
<?xml version="1.0" encoding="iso-8859-1"?>
192
<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
193
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
194
<svg class="el_sent_status--sent" version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
195
	 width="15px" height="15px" viewBox="0 0 510 510" style="enable-background:new 0 0 510 510;" xml:space="preserve">
196
<g>
197
	<g id="check-circle-outline">
198
		<path d="M150.45,206.55l-35.7,35.7L229.5,357l255-255l-35.7-35.7L229.5,285.6L150.45,206.55z M459,255c0,112.2-91.8,204-204,204
199
			S51,367.2,51,255S142.8,51,255,51c20.4,0,38.25,2.55,56.1,7.65l40.801-40.8C321.3,7.65,288.15,0,255,0C114.75,0,0,114.75,0,255
200
			s114.75,255,255,255s255-114.75,255-255H459z"/>
201
	</g>
202
</g>
203
<g>
204
</g>
205
<g>
206
</g>
207
<g>
208
</g>
209
<g>
210
</g>
211
<g>
212
</g>
213
<g>
214
</g>
215
<g>
216
</g>
217
<g>
218
</g>
219
<g>
220
</g>
221
<g>
222
</g>
223
<g>
224
</g>
225
<g>
226
</g>
227
<g>
228
</g>
229
<g>
230
</g>
231
<g>
232
</g>
233
</svg>
234
235
EOT;
236
237
}
238
239
/**
240
 * Gets the log row class by result code.
241
 *
242
 * @param int $result Mail sent status.
243
 *
244
 * @return string
245
 */
246
function get_log_row_class_by_result_code( $result ) {
247
	$log_row_classes = array(
248
		0 => 'el_email_sent_status--failed',
249
		1 => 'el_email_sent_status--sent',
250
	);
251
	if ( empty( $result ) ) {
252
		return $log_row_classes[0];
253
	}
254
255
	$result = absint( $result );
256
	if ( array_key_exists( $result, $log_row_classes ) ) {
257
		return $log_row_classes[ $result ];
258
	}
259
260
	return $log_row_classes[0];
261
}
262
263
/**
264
 * Stringify arrays.
265
 *
266
 * If the parameter is an array, then return delimiter separated values of the array.
267
 * Otherwise return the parameter.
268
 *
269 1
 * @since 2.3.0
270 1
 * @since 2.3.2 Renamed name to `Stringify`.
271 1
 *
272 1
 * @param array|string $may_be_array The array whose values are to be converted to string.
273
 * @param string       $delimiter    Optional. Default is `,`.
274 1
 *
275
 * @return string Stringified value.
276
 */
277
function stringify( $may_be_array, $delimiter = ',' ) {
278
	if ( ! is_array( $may_be_array ) ) {
279
		return (string) $may_be_array;
280
	}
281
282
	return implode( $delimiter, $may_be_array );
283
}
284
285
/**
286
 * Gets the User defined Date time format.
287
 *
288
 * @used-by \EmailLog\Core\UI\Setting\CoreSetting
289
 * @used-by \EmailLog\Util\render_auto_delete_logs_next_run_schedule()
290 1
 *
291
 * @since   2.3.0
292 1
 *
293 1
 * @return string
294
 */
295 1
function get_user_defined_date_time_format() {
296
	return sprintf( '%1$s %2$s', get_option( 'date_format', 'Y-m-d' ), get_option( 'time_format', 'g:i a' ) );
0 ignored issues
show
Bug introduced by
It seems like get_option('date_format', 'Y-m-d') can also be of type false; however, parameter $args of sprintf() 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

296
	return sprintf( '%1$s %2$s', /** @scrutinizer ignore-type */ get_option( 'date_format', 'Y-m-d' ), get_option( 'time_format', 'g:i a' ) );
Loading history...
297
}
298 1
299
/**
300
 * Renders the next run auto delete logs schedule in Date and time format set within WordPress.
301
 *
302
 * @used-by \EmailLog\Addon\UI\Setting\DashboardWidget
303
 * @used-by \EmailLog\Core\UI\Component\AutoDeleteLogsSetting
304
 *
305
 * @since   2.3.0
306
 */
307
function render_auto_delete_logs_next_run_schedule() {
308
	?>
309
	<?php if ( wp_next_scheduled( 'el_scheduled_delete_logs' ) ) : ?>
310
		<p>
311
			<?php _e( 'Auto delete logs cron will be triggered next at', 'email-log' ); ?>:
312
			<?php $date_time_format = get_user_defined_date_time_format(); ?>
313 1
			<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>
0 ignored issues
show
Bug introduced by
It seems like wp_next_scheduled('el_scheduled_delete_logs') can also be of type false; however, parameter $timestamp of date() does only seem to accept 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

313
			<strong><?php echo get_date_from_gmt( date( 'Y-m-d H:i:s', /** @scrutinizer ignore-type */ wp_next_scheduled( 'el_scheduled_delete_logs' ) ), $date_time_format ); ?></strong>
Loading history...
314 1
		</p>
315 1
	<?php endif; ?>
316 1
	<?php
317 1
}
318 1
319 1
/**
320 1
 * Gets the value by key from the array.
321 1
 *
322 1
 * If the key isn't found, then null is returned.
323 1
 *
324 1
 * @since 2.3.0
325
 *
326
 * @param array  $array   The actual array.
327
 * @param string $key     The key whose value is to be retrieved.
328
 * @param string $default Optional.
329
 *
330
 * @return mixed|null
331
 */
332
function el_array_get( $array, $key, $default = null ) {
333
	return isset( $array[ $key ] ) ? $array[ $key ] : $default;
334
}
335
336
/**
337
 * Returns TRUE if the given search term is Advanced Search Term.
338
 *
339
 * @param string $term Search Term.
340
 *
341
 * @return bool
342
 */
343
function is_advanced_search_term( $term ) {
344
	if ( ! is_string( $term ) ) {
0 ignored issues
show
introduced by
The condition is_string($term) is always true.
Loading history...
345
		return false;
346
	}
347
348
	$predicates = get_advanced_search_term_predicates( $term );
349
350
	return ! empty( $predicates );
351
}
352
353
/**
354
 * Gets the Search Term Predicates.
355
 *
356
 * Example:
357
 *
358
 * If $term = to:[email protected] then,
359
 *
360
 * the output would be
361
 *
362
 * $output = array(
363
 *      'to' => [email protected]
364
 * )
365
 *
366
 * @since 2.3.0
367
 *
368
 * @param string $term Search Term.
369
 *
370
 * @return array
371
 */
372
function get_advanced_search_term_predicates( $term ) {
373
	if ( ! is_string( $term ) ) {
0 ignored issues
show
introduced by
The condition is_string($term) is always true.
Loading history...
374
		return array();
375
	}
376
377
	$predicates           = explode( ' ', $term );
378
	$predicates_organized = array();
379
380
	foreach ( $predicates as $predicate ) {
381
		$is_match = preg_match( '/(id|email|to|cc|bcc|reply-to):(.*)$/', $predicate, $matches );
382
		if ( 1 === $is_match ) {
383
			$predicates_organized[ $matches[1] ] = $matches[2];
384
		}
385
	}
386
387
	return $predicates_organized;
388
}
389
390
/**
391
 * Gets the Advanced Search URL.
392
 *
393
 * @since 2.3.0
394
 *
395
 * @return string
396
 */
397
function get_advanced_search_url() {
398
	$admin_url = get_admin_url( null, 'admin.php?page=email-log' );
399
400
	return add_query_arg( 'el_as', 1, $admin_url );
401
}
402
403
/**
404
 * Gets the Column labels to be used in LogList table.
405
 *
406
 * @since 2.3.2
407
 * @since 2.3.0
408
 *
409
 * @param string $db_column
410
 *
411
 * @return string
412
 */
413
function get_column_label_by_db_column( $db_column ) {
414
	// Standard column labels are on the right.
415
	// $mapping[ $non_standard_key ] => $standard_key
416
	$mapping = array(
417
		'to'          => 'to_email', // EmailLog\Core\UI\ListTable::get_columns() uses `to`.
418
		'reply-to'    => 'reply_to',
419
		'attachment'  => 'attachments',
420
		'sent_status' => 'result',
421
	);
422
423
	$labels = get_email_log_columns();
424
425
	/**
426
	 * Filters the Labels used through out the Email Log plugin.
427
	 *
428
	 * @since 2.3.0
429
	 *
430
	 * @param array $labels {
431
	 *                      List of DB Columns and its respective labels.
432
	 *
433
	 *                      Example:
434
	 *                      'id'          => __( 'ID', 'email-log' ),
435
	 *
436
	 * @type string $key    DB Column or any key for which a Label would be required. Accepts a internationalized string as Label.
437
	 *              }
438
	 */
439
	$labels = apply_filters( 'el_db_column_labels', $labels );
440
441
	if ( array_key_exists( $db_column, $labels ) ) {
442
		return $labels[ $db_column ];
443
	} elseif ( array_key_exists( $db_column, $mapping ) ) {
444
		$label_key = $mapping[ $db_column ];
445
446
		return $labels[ $label_key ];
447
	}
448
449
	return $db_column;
450
}
451
452
/**
453
 * Returns an array of Email Log columns.
454
 *
455
 * Keys are the column names in the DB.
456
 * This holds true except for CC, BCC & Reply To as they are put under one column `headers`.
457
 *
458
 * @since 2.3.2
459
 *
460
 * @return array Key value pair of Email Log columns.
461
 */
462
function get_email_log_columns() {
463
	return array(
464
		'id'          => __( 'ID', 'email-log' ),
465
		'sent_date'   => __( 'Sent at', 'email-log' ),
466
		'to_email'    => __( 'To', 'email-log' ),
467
		'subject'     => __( 'Subject', 'email-log' ),
468
		'message'     => __( 'Message', 'email-log' ),
469
		'from'        => __( 'From', 'email-log' ),
470
		'cc'          => __( 'CC', 'email-log' ),
471
		'bcc'         => __( 'BCC', 'email-log' ),
472
		'attachments' => __( 'Attachment', 'email-log' ),
473
		'ip_address'  => __( 'IP Address', 'email-log' ),
474
		'reply_to'    => __( 'Reply To', 'email-log' ),
475
		'result'      => __( 'Sent Status', 'email-log' ),
476
	);
477
}
478
479
/**
480
 * Abstract of the core logic behind masking.
481
 *
482
 * @since 2.3.2
483
 *
484
 * @param string $value     Content.
485
 * @param string $mask_char Mask character.
486
 * @param int    $percent   The higher the percent, the more masking character on the email.
487
 *
488
 * @return string
489
 */
490
function get_masked_value( $value, $mask_char, $percent ) {
491
	$len        = strlen( $value );
492
	$mask_count = (int) floor( $len * $percent / 100 );
493
	$offset     = (int) floor( ( $len - $mask_count ) / 2 );
494
495
	return substr( $value, 0, $offset ) . str_repeat( $mask_char, $mask_count ) . substr( $value, $mask_count + $offset );
496
}
497
498
/**
499
 * Masks Email address.
500
 *
501
 * @see   http://www.webhostingtalk.com/showthread.php?t=1014672
502
 * @since 2.3.2
503
 *
504
 * @uses  get_masked_value()
505
 *
506
 * @param string $email     Email to be masked.
507
 * @param string $mask_char Mask character.
508
 * @param int    $percent   The higher the percent, the more masking character on the email.
509
 *
510
 * @return string
511
 */
512
function mask_email( $email, $mask_char = '*', $percent = 50 ) {
513
	if ( ! is_email( $email ) ) {
514
		return $email;
515
	}
516
517
	list( $user, $domain ) = preg_split( '/@/', $email );
518
519
	return sprintf(
520
		'%1$s@%2$s',
521
		get_masked_value( $user, $mask_char, $percent ),
522
		get_masked_value( $domain, $mask_char, $percent )
523
	);
524
}
525
526
/**
527
 * Mask Content fields.
528
 *
529
 * Content fields can be Subject or Email message.
530
 *
531
 * @since 2.3.2
532
 *
533
 * @uses  get_masked_value()
534
 *
535
 * @param string $content   The actual content.
536
 * @param string $mask_char Mask character.
537
 * @param int    $percent   The higher the percent, the more masking character on the email.
538
 *
539
 * @return string
540
 */
541
function mask_content( $content, $mask_char = '*', $percent = 80 ) {
542
	$content = wp_strip_all_tags( $content );
543
544
	return get_masked_value( $content, $mask_char, $percent );
545
}
546