Completed
Push — 124-feature/notify-admins-on-e... ( 2e956d...ebe3a5 )
by Maria Daniel Deepak
06:33
created

join_array_elements_with_delimiter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 6
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 9
	$emails = explode( ',', $email );
21 9
	if ( ! $multiple ) {
22 2
		$emails = array_slice( $emails, 0, 1 );
23 2
	}
24
25 9
	$cleaned_emails = array_map( __NAMESPACE__ . '\\sanitize_email_with_name', $emails );
26
27 9
	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 9
	$string = trim( $string );
41
42 9
	$bracket_pos = strpos( $string, '<' );
43 9
	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 4
	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( 'id', 'sent_date', 'to_email', 'subject', 'from', 'cc', 'bcc', 'reply-to', 'attachment' );
71
	}
72
73
	return array( 'id', 'sent_date', 'to_email', 'subject' );
74
}
75
76
/**
77
 * Is it an admin request and not an ajax request.
78
 *
79
 * @since 2.1
80
 *
81
 * @return bool True if admin non ajax request, False otherwise.
82
 */
83
function is_admin_non_ajax_request() {
84
	if ( function_exists( 'wp_doing_ajax' ) && wp_doing_ajax() ) {
85
		return false;
86
	}
87
88
	if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
89
		return false;
90
	}
91
92
	return is_admin();
93
}
94
95
/**
96
 * Checks the Checkbox when values are present in a given array.
97
 *
98
 * Use this function in Checkbox fields.
99
 *
100
 * @since 2.1.0
101
 *
102
 * @param array  $values  List of all possible values.
103
 * @param string $current The current value to be checked.
104
 */
105
function checked_array( $values, $current ) {
106
	if ( ! is_array( $values ) ) {
0 ignored issues
show
introduced by
The condition is_array($values) is always true.
Loading history...
107
		return;
108
	}
109
110
	if ( in_array( $current, $values ) ) {
111
		echo "checked='checked'";
112
	}
113
}
114
115
/**
116
 * Returns Comma separated values of the given array elements.
117
 *
118
 * Use $delimiter param to join elements other than `,`.
119
 *
120
 * @since 2.3.0
121
 *
122
 * @param array|string $value     The array whose values are to be joined.
123
 * @param string       $delimiter Optional. Default is `,`.
124
 *
125
 * @return string
126
 */
127
function join_array_elements_with_delimiter( $value, $delimiter = ',' ) {
128
	if ( is_array( $value ) ) {
129
		return implode( $delimiter, $value );
130
	}
131
	return $value;
132
}