Completed
Pull Request — dev/2.1.0 (#102)
by Maria Daniel Deepak
06:54
created

helper.php ➔ can_current_user_email_log()   B

Complexity

Conditions 9
Paths 5

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 14
nc 5
nop 0
dl 0
loc 20
ccs 0
cts 16
cp 0
crap 90
rs 7.756
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
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 8
	$emails = explode( ',', $email );
20 8
	if ( ! $multiple ) {
21 2
		$emails = array_slice( $emails, 0, 1 );
22 2
	}
23
24 8
	$cleaned_emails = array_map( __NAMESPACE__ . '\\sanitize_email_with_name', $emails );
25
26 8
	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 8
	$string = trim( $string );
40
41 8
	$bracket_pos = strpos( $string, '<' );
42 8
	if ( false !== $bracket_pos ) {
43
		// Text before the bracketed email is the name.
44 4
		if ( $bracket_pos > 0 ) {
45 4
			$name = substr( $string, 0, $bracket_pos );
46 4
			$name = str_replace( '"', '', $name );
47 4
			$name = trim( $name );
48
49 4
			$email = substr( $string, $bracket_pos + 1 );
50 4
			$email = str_replace( '>', '', $email );
51
52 4
			return sanitize_text_field( $name ) . ' <' . \sanitize_email( $email ) . '>';
53
		}
54
	}
55
56 4
	return \sanitize_email( $string );
57
}
58
59
/**
60
 * Gets the columns to export logs.
61
 *
62
 * If the More Fields add-on is active, additional columns are returned.
63
 *
64
 * @since 2.0.0
65
 *
66
 * @return array List of Columns to export.
67
 */
68
function get_log_columns_to_export() {
69
70
	if ( is_plugin_active( 'email-log-more-fields/email-log-more-fields.php' ) ) {
71
		return array( 'id', 'sent_date', 'to_email', 'subject', 'from', 'cc', 'bcc', 'reply-to', 'attachment' );
72
	}
73
74
	return array( 'id', 'sent_date', 'to_email', 'subject' );
75
}
76
77
/**
78
 * Returns TRUE if the User is Administrator or the User's role is allowed in Plugin's settings page.
79
 *
80
 * @since 2.1.0
81
 *
82
 * @return bool
83
 */
84
function can_current_user_email_log() {
85
	$return_value = false;
86
	$option       = get_option( 'el_email_log' );
87
88
	if ( current_user_can( 'administrator' ) ) {
89
		$return_value = true;
90
	} elseif ( ! is_admin() && ! current_user_can( 'administrator' ) ) {
91
		if ( $option && is_array( $option ) && array_key_exists( 'allowed_user_roles', $option ) ) {
92
			$user               = wp_get_current_user();
93
			$allowed_user_roles = $option['allowed_user_roles'];
94
			$allowed_user_roles = array_map( 'strtolower', $allowed_user_roles );
95
			$matched_role       = array_intersect( (array) $user->roles, $allowed_user_roles );
96
			if ( is_array( $matched_role ) && ! empty( $matched_role ) ) {
97
				$return_value = true;
98
			}
99
		}
100
	}
101
102
	return $return_value;
103
}