Passed
Push — master ( 258987...abdd42 )
by Brian
11:44 queued 05:35
created

GetPaid_Notification_Email_Sender   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 62
c 1
b 0
f 0
dl 0
loc 200
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A get_from_address() 0 9 2
A before_sending() 0 7 1
A get_headers() 0 7 1
A get_reply_to() 0 9 2
A get_from_name() 0 9 2
A default_from_address() 0 11 2
A after_sending() 0 7 1
A get_content_type() 0 2 1
A send() 0 50 2
A ensure_email_content() 0 3 1
1
<?php
2
/**
3
 * Contains the notification email sending class.
4
 *
5
 */
6
7
defined( 'ABSPATH' ) || exit;
8
9
/**
10
 * This function is responsible for sending emails.
11
 *
12
 */
13
class GetPaid_Notification_Email_Sender {
14
15
    /**
16
	 * Whether or not we should inline CSS into the email.
17
	 */
18
	public $inline_css = true;
19
20
    /**
21
	 * The wp_mail() data.
22
	 */
23
    public $wp_mail_data = null;
24
25
    /**
26
	 * Sends a new email.
27
     * 
28
     * @param string|array $to The recipients email or an array of recipient emails.
29
     * @param string $subject The email's subject.
30
     * @param string $email The email body.
31
     * @param array $attachments The email attachments.
32
     * 
33
     * @return bool
34
	 */
35
	public function send( $to, $subject, $email, $attachments = array() ) {
36
37
		/*
38
		 * Allow to filter data on per-email basis.
39
		 */
40
		$data = apply_filters(
41
			'getpaid_email_data',
42
			array(
43
				'to'          => array_filter( wpinv_parse_list( $to ) ),
44
				'subject'     => $subject,
45
				'email'       => $email,
46
				'headers'     => $this->get_headers(),
47
				'attachments' => $attachments,
48
			),
49
			$this
50
		);
51
52
        // Remove slashes.
53
        $data               = (array) wp_unslash( $data );
54
55
        // Cache it.
56
		$this->wp_mail_data = $data;
57
58
		// Attach our own hooks.
59
		$this->before_sending();
60
61
		// Prepare the sending function.
62
		$sending_function = apply_filters( 'getpaid_email_email_sending_function', 'wp_mail' );
63
        $result           = false;
64
65
        foreach ( $this->wp_mail_data['to'] as $to ) {
0 ignored issues
show
Bug introduced by
The expression $this->wp_mail_data['to'] of type string is not traversable.
Loading history...
introduced by
$to is overwriting one of the parameters of this function.
Loading history...
66
67
            // Send the actual email.
68
            $result = call_user_func(
69
                $sending_function,
70
                $to,
71
                html_entity_decode( $data['subject'], ENT_QUOTES, get_bloginfo( 'charset' ) ),
72
                $data['email'],
73
                $data['headers'],
74
                $data['attachments']
75
            );
76
77
        }
78
79
		// Remove our hooks.
80
		$this->after_sending();		
81
82
		$this->wp_mail_data = null;
83
84
		return $result;
85
    }
86
    
87
    /**
88
	 * Retrieves email headers.
89
	 */
90
	public function get_headers() {
91
92
		$name       = $this->get_from_name();
93
		$reply_to   = $this->get_reply_to();
94
		$headers    = array( "Reply-To:$name <$reply_to>" );
95
96
		return apply_filters( 'getpaid_email_headers',  $headers, $this );
97
98
	}
99
100
    /**
101
	 * Fires before an email is sent
102
	 *
103
	 * @since 1.0.0
104
	 */
105
	public function before_sending() {
106
107
        do_action( 'getpaid_before_send_email', $this );
108
		add_filter( 'wp_mail_from', array( $this, 'get_from_address' ), 1000 );
109
		add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ), 1000 );
110
		add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ), 1000 );
111
		add_filter( 'wp_mail', array( $this, 'ensure_email_content' ), 1000000 );
112
113
	}
114
115
    /**
116
	 * Returns the from name.
117
	 */
118
	public function get_from_name() {
119
120
        $from_name = wpinv_get_option( 'email_from_name', get_bloginfo( 'name' ) );
121
122
		if ( empty( $from_name ) ) {
123
			$from_name =  get_bloginfo( 'name' );
124
        }
125
126
		return wp_specialchars_decode( $from_name, ENT_QUOTES );
0 ignored issues
show
Bug introduced by
It seems like $from_name can also be of type array; however, parameter $string of wp_specialchars_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

126
		return wp_specialchars_decode( /** @scrutinizer ignore-type */ $from_name, ENT_QUOTES );
Loading history...
127
    }
128
129
    /**
130
	 * Returns the from email.
131
	 */
132
	public function get_from_address() {
133
134
        $from_address = wpinv_get_option( 'email_from', $this->default_from_address() );
135
136
		if ( ! is_email( $from_address ) ) {
0 ignored issues
show
Bug introduced by
$from_address of type array is incompatible with the type string expected by parameter $email of is_email(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

136
		if ( ! is_email( /** @scrutinizer ignore-type */ $from_address ) ) {
Loading history...
137
			$from_address =  $this->default_from_address();
138
        }
139
        
140
        return $from_address;
141
142
    }
143
144
    /**
145
	 * The default emails from address.
146
	 * 
147
	 * Defaults to wordpress@$sitename
148
	 * Some hosts will block outgoing mail from this address if it doesn't exist,
149
	 * but there's no easy alternative. Defaulting to admin_email might appear to be
150
	 * another option, but some hosts may refuse to relay mail from an unknown domain.
151
	 *
152
	 */
153
	public function default_from_address() {
154
155
		// Get the site domain and get rid of www.
156
		$sitename = strtolower( $_SERVER['SERVER_NAME'] );
157
		if ( substr( $sitename, 0, 4 ) == 'www.' ) {
158
			$sitename = substr( $sitename, 4 );
159
		}
160
161
		$from_email = 'wordpress@' . $sitename;
162
163
		return apply_filters( 'getpaid_default_from_address', $from_email );
164
165
    }
166
    
167
    /**
168
	 * Get the email reply-to.
169
	 *
170
	 *
171
	 * @return string The email reply-to address.
172
	 */
173
	public function get_reply_to() {
174
175
		$reply_to = wpinv_get_admin_email();
176
177
		if ( ! is_email( $reply_to ) ) {
0 ignored issues
show
Bug introduced by
It seems like $reply_to can also be of type array; however, parameter $email of is_email() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

177
		if ( ! is_email( /** @scrutinizer ignore-type */ $reply_to ) ) {
Loading history...
178
			$reply_to =  get_option( 'admin_email' );
179
		}
180
181
		return $reply_to;
182
    }
183
    
184
    /**
185
	 * Get the email content type.
186
	 *
187
	 */
188
	public function get_content_type() {
189
		return apply_filters( 'getpaid_email_content_type', 'text/html', $this );
190
    }
191
    
192
    /**
193
	 * Ensures that our email messages are not messed up by template plugins.
194
	 *
195
	 * @return array wp_mail_data.
196
	 */
197
	public function ensure_email_content( $args ) {
198
		$args['message'] = $this->wp_mail_data['email'];
199
		return $args;
200
    }
201
    
202
    /**
203
	 * A little house keeping after an email is sent.
204
	 *
205
 	 */
206
	public function after_sending() {
207
208
        do_action( 'after_noptin_sends_email', $this->wp_mail_data );
209
		remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ), 1000 );
210
		remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ), 1000 );
211
		remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ), 1000 );
212
		remove_filter( 'wp_mail', array( $this, 'ensure_email_content' ), 1000000 );
213
214
	}
215
216
}
217