Completed
Push — master ( fec9e0...63ebd4 )
by Sudar
01:40
created

helper.php ➔ checked_array()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 12
rs 9.6666
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 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 string[] 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
 * Checks the Checkbox when values are present in a given array.
96
 *
97
 * Use this function in Checkbox fields.
98
 *
99
 * @since 2.1.0
100
 *
101
 * @param array $values   List of all possible values.
102
 * @param string $current The current value to be checked.
103
 */
104
function checked_array( $values, $current ) {
105
	if ( ! is_array( $values ) ) {
106
		return;
107
	}
108
109
	if ( in_array( $current, $values ) ) {
110
		echo "checked='checked'";
111
	}
112
}
113