Completed
Push — master ( 647547...13dcfc )
by Devin
17:56
created

Give_Emails::build_email()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3.0494

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 3
nop 1
dl 0
loc 31
ccs 14
cts 17
cp 0.8235
crap 3.0494
rs 8.8571
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 24 and the first side effect is on line 16.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Emails
4
 *
5
 * This class handles all emails sent through Give
6
 *
7
 * @package     Give
8
 * @subpackage  Classes/Emails
9
 * @copyright   Copyright (c) 2016, WordImpress
10
 * @license     http://opensource.org/licenses/gpl-2.1.php GNU Public License
11
 * @since       1.0
12
 */
13
14
// Exit if accessed directly
15
if ( ! defined( 'ABSPATH' ) ) {
16
	exit;
17
}
18
19
/**
20
 * Give_Emails Class
21
 *
22
 * @since 1.0
23
 */
24
class Give_Emails {
25
26
	/**
27
	 * Holds the from address
28
	 *
29
	 * @since 1.0
30
	 */
31
	private $from_address;
32
33
	/**
34
	 * Holds the from name
35
	 *
36
	 * @since 1.0
37
	 */
38
	private $from_name;
39
40
	/**
41
	 * Holds the email content type
42
	 *
43
	 * @since 1.0
44
	 */
45
	private $content_type;
46
47
	/**
48
	 * Holds the email headers
49
	 *
50
	 * @since 1.0
51
	 */
52
	private $headers;
53
54
	/**
55
	 * Whether to send email in HTML
56
	 *
57
	 * @since 1.0
58
	 */
59
	private $html = true;
60
61
	/**
62
	 * The email template to use
63
	 *
64
	 * @since 1.0
65
	 */
66
	private $template;
67
68
	/**
69
	 * The header text for the email
70
	 *
71
	 * @since  1.0
72
	 */
73
	private $heading = '';
74
75
	/**
76
	 * Get things going
77
	 *
78
	 * @since 1.0
79
	 */
80
	public function __construct() {
81
82
		if ( 'none' === $this->get_template() ) {
83
			$this->html = false;
84
		}
85
86
		add_action( 'give_email_send_before', array( $this, 'send_before' ) );
87
		add_action( 'give_email_send_after', array( $this, 'send_after' ) );
88
89
	}
90
91
	/**
92
	 * Set a property
93
	 *
94
	 * @since 1.0
95
	 */
96 42
	public function __set( $key, $value ) {
97 42
		$this->$key = $value;
98 42
	}
99
100
	/**
101
	 * Get the email from name
102
	 *
103
	 * @since 1.0
104
	 */
105 42
	public function get_from_name() {
106 42
		if ( ! $this->from_name ) {
107
			$this->from_name = give_get_option( 'from_name', get_bloginfo( 'name' ) );
108
		}
109
110 42
		return apply_filters( 'give_email_from_name', wp_specialchars_decode( $this->from_name ), $this );
111
	}
112
113
	/**
114
	 * Get the email from address
115
	 *
116
	 * @since 1.0
117
	 */
118 42
	public function get_from_address() {
119 42
		if ( ! $this->from_address ) {
120 1
			$this->from_address = give_get_option( 'from_email', get_option( 'admin_email' ) );
121 1
		}
122
123 42
		return apply_filters( 'give_email_from_address', $this->from_address, $this );
124
	}
125
126
	/**
127
	 * Get the email content type
128
	 *
129
	 * @since 1.0
130
	 */
131 42
	public function get_content_type() {
132 42
		if ( ! $this->content_type && $this->html ) {
133 1
			$this->content_type = apply_filters( 'give_email_default_content_type', 'text/html', $this );
134 42
		} else if ( ! $this->html ) {
135
			$this->content_type = 'text/plain';
136
		}
137
138 42
		return apply_filters( 'give_email_content_type', $this->content_type, $this );
139
	}
140
141
	/**
142
	 * Get the email headers
143
	 *
144
	 * @since 1.0
145
	 */
146 42
	public function get_headers() {
147 42
		if ( ! $this->headers ) {
148 1
			$this->headers = "From: {$this->get_from_name()} <{$this->get_from_address()}>\r\n";
149 1
			$this->headers .= "Reply-To: {$this->get_from_address()}\r\n";
150 1
			$this->headers .= "Content-Type: {$this->get_content_type()}; charset=utf-8\r\n";
151 1
		}
152
153 42
		return apply_filters( 'give_email_headers', $this->headers, $this );
154
	}
155
156
	/**
157
	 * Retrieve email templates
158
	 *
159
	 * @since 1.0
160
	 */
161
	public function get_templates() {
162
		$templates = array(
163
			'default' => __( 'Default Template', 'give' ),
164
			'none'    => __( 'No template, plain text only', 'give' )
165
		);
166
167
		return apply_filters( 'give_email_templates', $templates );
168
	}
169
170
	/**
171
	 * Get the enabled email template
172
	 *
173
	 * @since 1.0
174
	 */
175 42
	public function get_template() {
176 42
		if ( ! $this->template ) {
177
			$this->template = give_get_option( 'email_template', 'default' );
178
		}
179
180 42
		return apply_filters( 'give_email_template', $this->template );
181
	}
182
183
	/**
184
	 * Get the header text for the email
185
	 *
186
	 * @since 1.0
187
	 */
188 42
	public function get_heading() {
189 42
		return apply_filters( 'give_email_heading', $this->heading );
190
	}
191
192
	/**
193
	 * Parse email template tags
194
	 *
195
	 * @param $content
196
	 *
197
	 * @return mixed
198
	 */
199 42
	public function parse_tags( $content ) {
200 42
		return $content;
201
	}
202
203
	/**
204
	 * Build the final email
205
	 *
206
	 * @since 1.0
207
	 */
208 42
	public function build_email( $message ) {
209
210 42
		if ( false === $this->html ) {
211
			return apply_filters( 'give_email_message', wp_strip_all_tags( $message ), $this );
212
		}
213
214 42
		$message = $this->text_to_html( $message );
215
216 42
		ob_start();
217
218 42
		give_get_template_part( 'emails/header', $this->get_template(), true );
219
220 42
		do_action( 'give_email_header', $this );
221
222 42
		if ( has_action( 'give_email_template_' . $this->get_template() ) ) {
223
			do_action( 'give_email_template_' . $this->get_template() );
224
		} else {
225 42
			give_get_template_part( 'emails/body', $this->get_template(), true );
226
		}
227
228 42
		do_action( 'give_email_body', $this );
229
230 42
		give_get_template_part( 'emails/footer', $this->get_template(), true );
231
232 42
		do_action( 'give_email_footer', $this );
233
234 42
		$body    = ob_get_clean();
235 42
		$message = str_replace( '{email}', $message, $body );
236
237 42
		return apply_filters( 'give_email_message', $message, $this );
238
	}
239
240
	/**
241
	 * Send the email
242
	 *
243
	 * @param  string $to The To address to send to.
244
	 * @param  string $subject The subject line of the email to send.
245
	 * @param  string $message The body of the email to send.
246
	 * @param  string|array $attachments Attachments to the email in a format supported by wp_mail()
247
	 *
248
	 * @return bool
249
	 */
250 42
	public function send( $to, $subject, $message, $attachments = '' ) {
251
252 42
		if ( ! did_action( 'init' ) && ! did_action( 'admin_init' ) ) {
253
			_doing_it_wrong( __FUNCTION__, __( 'You cannot send email with Give_Emails until init/admin_init has been reached', 'give' ), null );
254
255
			return false;
256
		}
257
258 42
		do_action( 'give_email_send_before', $this );
259
260 42
		$subject = $this->parse_tags( $subject );
261 42
		$message = $this->parse_tags( $message );
262
263 42
		$message = $this->build_email( $message );
264
265 42
		$attachments = apply_filters( 'give_email_attachments', $attachments, $this );
266
267 42
		$sent = wp_mail( $to, $subject, $message, $this->get_headers(), $attachments );
268
269 42
		do_action( 'give_email_send_after', $this );
270
271 42
		return $sent;
272
273
	}
274
275
	/**
276
	 * Add filters / actions before the email is sent
277
	 *
278
	 * @since 1.0
279
	 */
280 42
	public function send_before() {
281 42
		add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
282 42
		add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
283 42
		add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
284 42
	}
285
286
	/**
287
	 * Remove filters / actions after the email is sent
288
	 *
289
	 * @since 1.0
290
	 */
291 42
	public function send_after() {
292 42
		remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
293 42
		remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
294 42
		remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
295
296
		// Reset heading to an empty string
297 42
		$this->heading = '';
298 42
	}
299
300
	/**
301
	 * Converts text to formatted HTML. This is primarily for turning line breaks into <p> and <br/> tags.
302
	 *
303
	 * @since 1.0
304
	 */
305 42
	public function text_to_html( $message ) {
306
307 42
		if ( 'text/html' == $this->content_type || true === $this->html ) {
308 42
			$message = wpautop( $message );
309 42
		}
310
311 42
		return $message;
312
	}
313
314
}