Completed
Pull Request — master (#664)
by Devin
19:01
created

Give_Emails::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
ccs 0
cts 7
cp 0
crap 6
rs 9.4285
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 34
	public function __set( $key, $value ) {
97 34
		$this->$key = $value;
98 34
	}
99
100
	/**
101
	 * Get the email from name
102
	 *
103
	 * @since 1.0
104
	 */
105 34
	public function get_from_name() {
106 34
		if ( ! $this->from_name ) {
107
			$this->from_name = give_get_option( 'from_name', get_bloginfo( 'name' ) );
108
		}
109
110 34
		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 34
	public function get_from_address() {
119 34
		if ( ! $this->from_address ) {
120 1
			$this->from_address = give_get_option( 'from_email', get_option( 'admin_email' ) );
121 1
		}
122
123 34
		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 34
	public function get_content_type() {
132 34
		if ( ! $this->content_type && $this->html ) {
133 1
			$this->content_type = apply_filters( 'give_email_default_content_type', 'text/html', $this );
134 34
		} else if ( ! $this->html ) {
135
			$this->content_type = 'text/plain';
136
		}
137
138 34
		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 34
	public function get_headers() {
147 34
		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 34
		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 34
	public function get_template() {
176 34
		if ( ! $this->template ) {
177
			$this->template = give_get_option( 'email_template', 'default' );
178
		}
179
180 34
		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 34
	public function get_heading() {
189 34
		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 34
	public function parse_tags( $content ) {
200
		return $content;
201
	}
202
203
	/**
204 34
	 * Build the final email
205
	 *
206
	 * @since 1.0
207
	 */
208
	public function build_email( $message ) {
209
210
		if ( false === $this->html ) {
211
			return apply_filters( 'give_email_message', wp_strip_all_tags( $message ), $this );
212 34
		}
213
214 34
		$message = $this->text_to_html( $message );
215
216
		ob_start();
217
218 34
		give_get_template_part( 'emails/header', $this->get_template(), true );
219
220 34
		do_action( 'give_email_header', $this );
221
222 34
		if ( has_action( 'give_email_template_' . $this->get_template() ) ) {
223
			do_action( 'give_email_template_' . $this->get_template() );
224 34
		} else {
225
			give_get_template_part( 'emails/body', $this->get_template(), true );
226 34
		}
227
228
		do_action( 'give_email_body', $this );
229 34
230
		give_get_template_part( 'emails/footer', $this->get_template(), true );
231
232 34
		do_action( 'give_email_footer', $this );
233
234 34
		$body    = ob_get_clean();
235
		$message = str_replace( '{email}', $message, $body );
236 34
237
		return apply_filters( 'give_email_message', $message, $this );
238 34
	}
239 34
240
	/**
241 34
	 * 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
	public function send( $to, $subject, $message, $attachments = '' ) {
251
252
		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 34
255
			return false;
256 34
		}
257
258
		do_action( 'give_email_send_before', $this );
259
260
		$subject = $this->parse_tags( $subject );
261 34
		$message = $this->parse_tags( $message );
262
263 34
		$message = $this->build_email( $message );
264 34
265
		$attachments = apply_filters( 'give_email_attachments', $attachments, $this );
266 34
267
		$sent = wp_mail( $to, $subject, $message, $this->get_headers(), $attachments );
268 34
269
		do_action( 'give_email_send_after', $this );
270 34
271
		return $sent;
272 34
273
	}
274 34
275
	/**
276
	 * Add filters / actions before the email is sent
277
	 *
278
	 * @since 1.0
279
	 */
280
	public function send_before() {
281
		add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
282
		add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
283 34
		add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
284 34
	}
285 34
286 34
	/**
287 34
	 * Remove filters / actions after the email is sent
288
	 *
289
	 * @since 1.0
290
	 */
291
	public function send_after() {
292
		remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
293
		remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
294 34
		remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
295 34
296 34
		// Reset heading to an empty string
297 34
		$this->heading = '';
298
	}
299
300 34
	/**
301 34
	 * Converts text to formatted HTML. This is primarily for turning line breaks into <p> and <br/> tags.
302
	 *
303
	 * @since 1.0
304
	 */
305
	public function text_to_html( $message ) {
306
307
		if ( 'text/html' == $this->content_type || true === $this->html ) {
308 34
			$message = wpautop( $message );
309
		}
310 34
311 34
		return $message;
312 34
	}
313
314
}