Completed
Push — 224-feature/capture-error-mess... ( 42fb62...414860 )
by Maria Daniel Deepak
09:26 queued 07:04
created

get_masked_value()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 6
ccs 0
cts 0
cp 0
crap 2
rs 10
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
 *
131
 * @return string
132
 */
133
function get_email_failed_svg() {
134
	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 1
 * Returns Comma separated values of the given array elements.
265 1
 *
266 1
 * Use $delimiter param to join elements other than `,`.
267 1
 *
268 1
 * @since 2.3.0
269 1
 *
270 1
 * @param array|string $value     The array whose values are to be joined.
271 1
 * @param string       $delimiter Optional. Default is `,`.
272 1
 *
273 1
 * @return string
274 1
 */
275 1
function join_array_elements_with_delimiter( $value, $delimiter = ',' ) {
276 1
	if ( is_array( $value ) ) {
277
		return implode( $delimiter, $value );
278
	}
279
280
	return is_string( $value ) ? $value : '';
281
}
282
283
/**
284
 * Gets the User defined Date time format.
285
 *
286
 * @used-by \EmailLog\Core\UI\Setting\CoreSetting
287
 * @used-by \EmailLog\Util\render_auto_delete_logs_next_run_schedule()
288
 *
289
 * @since   2.3.0
290
 *
291
 * @return string
292 1
 */
293
function get_user_defined_date_time_format() {
294 1
	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

294
	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...
295 1
}
296
297
/**
298
 * Renders the next run auto delete logs schedule in Date and time format set within WordPress.
299
 *
300
 * @used-by \EmailLog\Addon\UI\Setting\DashboardWidget
301
 * @used-by \EmailLog\Core\UI\Component\AutoDeleteLogsSetting
302
 *
303
 * @since   2.3.0
304
 */
305
function render_auto_delete_logs_next_run_schedule() {
306
	?>
307
	<?php if ( wp_next_scheduled( 'el_scheduled_delete_logs' ) ) : ?>
308
		<p>
309
			<?php _e( 'Auto delete logs cron will be triggered next at', 'email-log' ); ?>:
310
			<?php $date_time_format = get_user_defined_date_time_format(); ?>
311
			<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

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