-
Notifications
You must be signed in to change notification settings - Fork 8
/
helper.php
119 lines (101 loc) · 2.93 KB
/
helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php namespace EmailLog\Util;
/**
* Email Log Helper functions.
* Some of these functions would be used the addons.
*/
/**
* Perform additional sanitation of emails.
*
* @since 1.9
*
* @param string $email Email string to be sanitized.
* @param bool $multiple (Optional) Should multiple emails be allowed. True by default.
*
* @return string Sanitized email.
*/
function sanitize_email( $email, $multiple = true ) {
$emails = explode( ',', $email );
if ( ! $multiple ) {
$emails = array_slice( $emails, 0, 1 );
}
$cleaned_emails = array_map( __NAMESPACE__ . '\\sanitize_email_with_name', $emails );
return implode( ', ', $cleaned_emails );
}
/**
* Sanitize email with name.
*
* @since 1.9
*
* @param string $string Email string to be sanitized.
*
* @return string Sanitized email.
*/
function sanitize_email_with_name( $string ) {
$string = trim( $string );
$bracket_pos = strpos( $string, '<' );
if ( false !== $bracket_pos ) {
// Text before the bracketed email is the name.
if ( $bracket_pos > 0 ) {
$name = substr( $string, 0, $bracket_pos );
$name = str_replace( '"', '', $name );
$name = trim( $name );
$email = substr( $string, $bracket_pos + 1 );
$email = str_replace( '>', '', $email );
return sanitize_text_field( $name ) . ' <' . \sanitize_email( $email ) . '>';
}
}
return \sanitize_email( $string );
}
/**
* Gets the columns to export logs.
*
* If the More Fields add-on is active, additional columns are returned.
*
* @since 2.0.0
*
* @return array List of Columns to export.
*/
function get_log_columns_to_export() {
if ( is_plugin_active( 'email-log-more-fields/email-log-more-fields.php' ) ) {
return array( 'id', 'sent_date', 'to_email', 'subject', 'from', 'cc', 'bcc', 'reply-to', 'attachment' );
}
return array( 'id', 'sent_date', 'to_email', 'subject' );
}
/**
* Returns TRUE if the User is Administrator or the User's role is allowed in Plugin's settings page.
*
* @since 2.1.0
*
* @return bool
*/
function can_current_user_view_email_log() {
$option = get_option( 'el_email_log_core' );
if ( current_user_can( 'administrator' ) ) {
return true;
}
$user = wp_get_current_user();
$allowed_user_roles = $option['allowed_user_roles'];
$allowed_user_roles = array_map( 'strtolower', $allowed_user_roles );
$matched_role = array_intersect( (array) $user->roles, $allowed_user_roles );
if ( is_array( $option ) &&
array_key_exists( 'allowed_user_roles', $option ) &&
is_array( $matched_role ) && ! empty( $matched_role ) ) {
return true;
}
return false;
}
/**
* Checks the Checkbox when values are present in a given array.
*
* Use this function in Checkbox fields.
*
* @since 2.1.0
*
* @param array $values List of all possible values.
* @param string $current The current value to be checked.
*/
function checked_array( $values, $current ) {
if ( in_array( $current, $values ) ) {
echo "checked='checked'";
}
}