Completed
Pull Request — master (#43)
by Sudar
04:50 queued 02:28
created

helper.php ➔ el_sanitize_email_with_name()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 12
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 20
rs 9.4285
1
<?php
2
/**
3
 * Email Log Helper functions.
4
 * Some of these functions would be used the addons.
5
 */
6
7
/**
8
 * Perform additional sanitation of emails.
9
 *
10
 * @since 1.9
11
 *
12
 * @param string $email    Email string to be sanitized.
13
 * @param bool   $multiple (Optional) Should multiple emails be allowed. True by default.
14
 *
15
 * @return string Sanitized email.
16
 */
17
function el_sanitize_email( $email, $multiple = true ) {
18
	$emails = explode( ',', $email );
19
	if ( ! $multiple ) {
20
		$emails = array_slice( $emails, 0, 1 );
21
	}
22
23
	$cleaned_emails = array_map( 'el_sanitize_email_with_name', $emails );
24
25
	return implode( ', ', $cleaned_emails );
26
}
27
28
/**
29
 * Sanitize email with name.
30
 *
31
 * @since 1.9
32
 *
33
 * @param $string $email Email string to be sanitized.
0 ignored issues
show
Documentation introduced by
The doc-type $string could not be parsed: Unknown type name "$string" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
Bug introduced by
There is no parameter named $email. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
34
 *
35
 * @return string Sanitized email.
36
 */
37
function el_sanitize_email_with_name( $string ) {
38
	$string = trim( $string );
39
40
	$bracket_pos = strpos( $string, '<' );
41
	if ( $bracket_pos !== false ) {
42
		// Text before the bracketed email is the name.
43
		if ( $bracket_pos > 0 ) {
44
			$name = substr( $string, 0, $bracket_pos );
45
			$name = str_replace( '"', '', $name );
46
			$name = trim( $name );
47
48
			$email = substr( $string, $bracket_pos + 1 );
49
			$email = str_replace( '>', '', $email );
50
51
			return sanitize_text_field( $name ) . ' <' . sanitize_email( $email ) . '>';
52
		}
53
	}
54
55
	return sanitize_email( $string );
56
}