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