1
|
|
|
<?php |
|
|
|
|
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' => esc_html__( 'Default Template', 'give' ), |
164
|
|
|
'none' => esc_html__( '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 |
|
$template = $this->get_template(); |
217
|
|
|
|
218
|
42 |
|
ob_start(); |
219
|
|
|
|
220
|
42 |
|
give_get_template_part( 'emails/header', $template, true ); |
221
|
|
|
|
222
|
42 |
|
/** |
223
|
|
|
* Fires in the email head. |
224
|
|
|
* |
225
|
42 |
|
* @since 1.0 |
226
|
|
|
* |
227
|
|
|
* @param Give_Emails $this The email object. |
228
|
42 |
|
*/ |
229
|
|
|
do_action( 'give_email_header', $this ); |
230
|
42 |
|
|
231
|
|
|
if ( has_action( 'give_email_template_' . $template ) ) { |
232
|
42 |
|
/** |
233
|
|
|
* Fires in a specific email template. |
234
|
42 |
|
* |
235
|
42 |
|
* @since 1.0 |
236
|
|
|
*/ |
237
|
42 |
|
do_action( "give_email_template_{$template}" ); |
238
|
|
|
} else { |
239
|
|
|
give_get_template_part( 'emails/body', $template, true ); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Fires in the email body. |
244
|
|
|
* |
245
|
|
|
* @since 1.0 |
246
|
|
|
* |
247
|
|
|
* @param Give_Emails $this The email object. |
248
|
|
|
*/ |
249
|
|
|
do_action( 'give_email_body', $this ); |
250
|
42 |
|
|
251
|
|
|
give_get_template_part( 'emails/footer', $template, true ); |
252
|
42 |
|
|
253
|
|
|
/** |
254
|
|
|
* Fires in the email footer. |
255
|
|
|
* |
256
|
|
|
* @since 1.0 |
257
|
|
|
* |
258
|
42 |
|
* @param Give_Emails $this The email object. |
259
|
|
|
*/ |
260
|
42 |
|
do_action( 'give_email_footer', $this ); |
261
|
42 |
|
|
262
|
|
|
$body = ob_get_clean(); |
263
|
42 |
|
$message = str_replace( '{email}', $message, $body ); |
264
|
|
|
|
265
|
42 |
|
return apply_filters( 'give_email_message', $message, $this ); |
266
|
|
|
} |
267
|
42 |
|
|
268
|
|
|
/** |
269
|
42 |
|
* Send the email. |
270
|
|
|
* |
271
|
42 |
|
* @param string $to The To address to send to. |
272
|
|
|
* @param string $subject The subject line of the email to send. |
273
|
|
|
* @param string $message The body of the email to send. |
274
|
|
|
* @param string|array $attachments Attachments to the email in a format supported by wp_mail(). |
275
|
|
|
* |
276
|
|
|
* @return bool |
277
|
|
|
*/ |
278
|
|
|
public function send( $to, $subject, $message, $attachments = '' ) { |
279
|
|
|
|
280
|
42 |
|
if ( ! did_action( 'init' ) && ! did_action( 'admin_init' ) ) { |
281
|
42 |
|
_doing_it_wrong( __FUNCTION__, esc_html__( 'You cannot send email with Give_Emails until init/admin_init has been reached.', 'give' ), null ); |
282
|
42 |
|
|
283
|
42 |
|
return false; |
284
|
42 |
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Fires before sending an email. |
288
|
|
|
* |
289
|
|
|
* @since 1.0 |
290
|
|
|
* |
291
|
42 |
|
* @param Give_Emails $this The email object. |
292
|
42 |
|
*/ |
293
|
42 |
|
do_action( 'give_email_send_before', $this ); |
294
|
42 |
|
|
295
|
|
|
$subject = $this->parse_tags( $subject ); |
296
|
|
|
$message = $this->parse_tags( $message ); |
297
|
42 |
|
|
298
|
42 |
|
$message = $this->build_email( $message ); |
299
|
|
|
|
300
|
|
|
$attachments = apply_filters( 'give_email_attachments', $attachments, $this ); |
301
|
|
|
|
302
|
|
|
$sent = wp_mail( $to, $subject, $message, $this->get_headers(), $attachments ); |
303
|
|
|
|
304
|
|
|
/** |
305
|
42 |
|
* Fires after sending an email. |
306
|
|
|
* |
307
|
42 |
|
* @since 1.0 |
308
|
42 |
|
* |
309
|
42 |
|
* @param Give_Emails $this The email object. |
310
|
|
|
*/ |
311
|
42 |
|
do_action( 'give_email_send_after', $this ); |
312
|
|
|
|
313
|
|
|
return $sent; |
314
|
|
|
|
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Add filters / actions before the email is sent. |
319
|
|
|
* |
320
|
|
|
* @since 1.0 |
321
|
|
|
*/ |
322
|
|
|
public function send_before() { |
323
|
|
|
add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) ); |
324
|
|
|
add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) ); |
325
|
|
|
add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) ); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Remove filters / actions after the email is sent. |
330
|
|
|
* |
331
|
|
|
* @since 1.0 |
332
|
|
|
*/ |
333
|
|
|
public function send_after() { |
334
|
|
|
remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) ); |
335
|
|
|
remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) ); |
336
|
|
|
remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) ); |
337
|
|
|
|
338
|
|
|
// Reset heading to an empty string |
339
|
|
|
$this->heading = ''; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Converts text to formatted HTML. This is primarily for turning line breaks into <p> and <br/> tags. |
344
|
|
|
* |
345
|
|
|
* @since 1.0 |
346
|
|
|
*/ |
347
|
|
|
public function text_to_html( $message ) { |
348
|
|
|
|
349
|
|
|
if ( 'text/html' == $this->content_type || true === $this->html ) { |
350
|
|
|
$message = wpautop( $message ); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
return $message; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
} |
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.