|
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 https://opensource.org/licenses/gpl-license 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
|
|
|
* @property $from_address |
|
23
|
|
|
* @property $from_name |
|
24
|
|
|
* @property $content_type |
|
25
|
|
|
* @property $headers |
|
26
|
|
|
* @property $html |
|
27
|
|
|
* @property $template |
|
28
|
|
|
* @property $heading |
|
29
|
|
|
* |
|
30
|
|
|
* @since 1.0 |
|
31
|
|
|
*/ |
|
32
|
|
|
class Give_Emails { |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Holds the from address. |
|
36
|
|
|
* |
|
37
|
|
|
* @since 1.0 |
|
38
|
|
|
*/ |
|
39
|
|
|
private $from_address; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Holds the from name. |
|
43
|
|
|
* |
|
44
|
|
|
* @since 1.0 |
|
45
|
|
|
*/ |
|
46
|
|
|
private $from_name; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Holds the email content type. |
|
50
|
|
|
* |
|
51
|
|
|
* @since 1.0 |
|
52
|
|
|
*/ |
|
53
|
|
|
private $content_type; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Holds the email headers. |
|
57
|
|
|
* |
|
58
|
|
|
* @since 1.0 |
|
59
|
|
|
*/ |
|
60
|
|
|
private $headers; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Whether to send email in HTML. |
|
64
|
|
|
* |
|
65
|
|
|
* @since 1.0 |
|
66
|
|
|
*/ |
|
67
|
|
|
private $html = true; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* The email template to use. |
|
71
|
|
|
* |
|
72
|
|
|
* @since 1.0 |
|
73
|
|
|
*/ |
|
74
|
|
|
private $template; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* The header text for the email. |
|
78
|
|
|
* |
|
79
|
|
|
* @since 1.0 |
|
80
|
|
|
*/ |
|
81
|
|
|
private $heading = ''; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get things going. |
|
85
|
|
|
* |
|
86
|
|
|
* @since 1.0 |
|
87
|
|
|
*/ |
|
88
|
|
|
public function __construct() { |
|
89
|
|
|
|
|
90
|
|
|
if ( 'none' === $this->get_template() ) { |
|
91
|
|
|
$this->html = false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
add_action( 'give_email_send_before', array( $this, 'send_before' ) ); |
|
95
|
|
|
add_action( 'give_email_send_after', array( $this, 'send_after' ) ); |
|
96
|
42 |
|
|
|
97
|
42 |
|
} |
|
98
|
42 |
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Set a property. |
|
101
|
|
|
* |
|
102
|
|
|
* @since 1.0 |
|
103
|
|
|
* |
|
104
|
|
|
* @param $key |
|
105
|
42 |
|
* @param $value |
|
106
|
42 |
|
*/ |
|
107
|
|
|
public function __set( $key, $value ) { |
|
108
|
|
|
$this->$key = $value; |
|
109
|
|
|
} |
|
110
|
42 |
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get the email from name. |
|
113
|
|
|
* |
|
114
|
|
|
* @since 1.0 |
|
115
|
|
|
*/ |
|
116
|
|
|
public function get_from_name() { |
|
117
|
|
|
if ( ! $this->from_name ) { |
|
118
|
42 |
|
$this->from_name = give_get_option( 'from_name', get_bloginfo( 'name' ) ); |
|
119
|
42 |
|
} |
|
120
|
1 |
|
|
|
121
|
1 |
|
return apply_filters( 'give_email_from_name', wp_specialchars_decode( $this->from_name ), $this ); |
|
122
|
|
|
} |
|
123
|
42 |
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Get the email from address. |
|
126
|
|
|
* |
|
127
|
|
|
* @since 1.0 |
|
128
|
|
|
*/ |
|
129
|
|
|
public function get_from_address() { |
|
130
|
|
|
if ( ! $this->from_address ) { |
|
131
|
42 |
|
$this->from_address = give_get_option( 'from_email', get_option( 'admin_email' ) ); |
|
132
|
42 |
|
} |
|
133
|
1 |
|
|
|
134
|
42 |
|
return apply_filters( 'give_email_from_address', $this->from_address, $this ); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
42 |
|
* Get the email content type. |
|
139
|
|
|
* |
|
140
|
|
|
* @since 1.0 |
|
141
|
|
|
*/ |
|
142
|
|
|
public function get_content_type() { |
|
143
|
|
|
if ( ! $this->content_type && $this->html ) { |
|
144
|
|
|
$this->content_type = apply_filters( 'give_email_default_content_type', 'text/html', $this ); |
|
145
|
|
|
} else if ( ! $this->html ) { |
|
146
|
42 |
|
$this->content_type = 'text/plain'; |
|
147
|
42 |
|
} |
|
148
|
1 |
|
|
|
149
|
1 |
|
return apply_filters( 'give_email_content_type', $this->content_type, $this ); |
|
150
|
1 |
|
} |
|
151
|
1 |
|
|
|
152
|
|
|
/** |
|
153
|
42 |
|
* Get the email headers. |
|
154
|
|
|
* |
|
155
|
|
|
* @since 1.0 |
|
156
|
|
|
*/ |
|
157
|
|
|
public function get_headers() { |
|
158
|
|
|
if ( ! $this->headers ) { |
|
159
|
|
|
$this->headers = "From: {$this->get_from_name()} <{$this->get_from_address()}>\r\n"; |
|
160
|
|
|
$this->headers .= "Reply-To: {$this->get_from_address()}\r\n"; |
|
161
|
|
|
$this->headers .= "Content-Type: {$this->get_content_type()}; charset=utf-8\r\n"; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
return apply_filters( 'give_email_headers', $this->headers, $this ); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Retrieve email templates. |
|
169
|
|
|
* |
|
170
|
|
|
* @since 1.0 |
|
171
|
|
|
*/ |
|
172
|
|
|
public function get_templates() { |
|
173
|
|
|
$templates = array( |
|
174
|
|
|
'default' => esc_html__( 'Default Template', 'give' ), |
|
175
|
42 |
|
'none' => esc_html__( 'No template, plain text only', 'give' ) |
|
176
|
42 |
|
); |
|
177
|
|
|
|
|
178
|
|
|
return apply_filters( 'give_email_templates', $templates ); |
|
179
|
|
|
} |
|
180
|
42 |
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Get the enabled email template. |
|
183
|
|
|
* |
|
184
|
|
|
* @since 1.0 |
|
185
|
|
|
*/ |
|
186
|
|
|
public function get_template() { |
|
187
|
|
|
if ( ! $this->template ) { |
|
188
|
42 |
|
$this->template = give_get_option( 'email_template', 'default' ); |
|
189
|
42 |
|
} |
|
190
|
|
|
|
|
191
|
|
|
return apply_filters( 'give_email_template', $this->template ); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Get the header text for the email. |
|
196
|
|
|
* |
|
197
|
|
|
* @since 1.0 |
|
198
|
|
|
*/ |
|
199
|
42 |
|
public function get_heading() { |
|
200
|
42 |
|
return apply_filters( 'give_email_heading', $this->heading ); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Parse email template tags. |
|
205
|
|
|
* |
|
206
|
|
|
* @param $content |
|
207
|
|
|
* |
|
208
|
42 |
|
* @return mixed |
|
209
|
|
|
*/ |
|
210
|
42 |
|
public function parse_tags( $content ) { |
|
211
|
|
|
return $content; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
42 |
|
/** |
|
215
|
|
|
* Build the final email. |
|
216
|
42 |
|
* |
|
217
|
|
|
* @since 1.0 |
|
218
|
42 |
|
* |
|
219
|
|
|
* @param $message |
|
220
|
42 |
|
* |
|
221
|
|
|
* @return string |
|
222
|
42 |
|
*/ |
|
223
|
|
|
public function build_email( $message ) { |
|
224
|
|
|
|
|
225
|
42 |
|
if ( false === $this->html ) { |
|
226
|
|
|
|
|
227
|
|
|
// Added Replacement check to simply behaviour of anchor tags. |
|
228
|
42 |
|
$pattern = '/<a.+?href\=(?:["|\'])(.+?)(?:["|\']).*?>(.+?)<\/a>/i'; |
|
229
|
|
|
$message = preg_replace_callback( |
|
230
|
42 |
|
$pattern, |
|
231
|
|
|
function( $return ) { |
|
232
|
42 |
|
if ( $return[1] !== $return[2] ) { |
|
233
|
|
|
return "{$return[2]} ( {$return[1]} )"; |
|
234
|
42 |
|
} |
|
235
|
42 |
|
return trailingslashit( $return[1] ); |
|
236
|
|
|
}, |
|
237
|
42 |
|
$message |
|
238
|
|
|
); |
|
239
|
|
|
|
|
240
|
|
|
return apply_filters( 'give_email_message', wp_strip_all_tags( $message ), $this ); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
$message = $this->text_to_html( $message ); |
|
244
|
|
|
|
|
245
|
|
|
$template = $this->get_template(); |
|
246
|
|
|
|
|
247
|
|
|
ob_start(); |
|
248
|
|
|
|
|
249
|
|
|
give_get_template_part( 'emails/header', $template, true ); |
|
250
|
42 |
|
|
|
251
|
|
|
/** |
|
252
|
42 |
|
* Fires in the email head. |
|
253
|
|
|
* |
|
254
|
|
|
* @since 1.0 |
|
255
|
|
|
* |
|
256
|
|
|
* @param Give_Emails $this The email object. |
|
257
|
|
|
*/ |
|
258
|
42 |
|
do_action( 'give_email_header', $this ); |
|
259
|
|
|
|
|
260
|
42 |
|
if ( has_action( 'give_email_template_' . $template ) ) { |
|
261
|
42 |
|
/** |
|
262
|
|
|
* Fires in a specific email template. |
|
263
|
42 |
|
* |
|
264
|
|
|
* @since 1.0 |
|
265
|
42 |
|
*/ |
|
266
|
|
|
do_action( "give_email_template_{$template}" ); |
|
267
|
42 |
|
} else { |
|
268
|
|
|
give_get_template_part( 'emails/body', $template, true ); |
|
269
|
42 |
|
} |
|
270
|
|
|
|
|
271
|
42 |
|
/** |
|
272
|
|
|
* Fires in the email body. |
|
273
|
|
|
* |
|
274
|
|
|
* @since 1.0 |
|
275
|
|
|
* |
|
276
|
|
|
* @param Give_Emails $this The email object. |
|
277
|
|
|
*/ |
|
278
|
|
|
do_action( 'give_email_body', $this ); |
|
279
|
|
|
|
|
280
|
42 |
|
give_get_template_part( 'emails/footer', $template, true ); |
|
281
|
42 |
|
|
|
282
|
42 |
|
/** |
|
283
|
42 |
|
* Fires in the email footer. |
|
284
|
42 |
|
* |
|
285
|
|
|
* @since 1.0 |
|
286
|
|
|
* |
|
287
|
|
|
* @param Give_Emails $this The email object. |
|
288
|
|
|
*/ |
|
289
|
|
|
do_action( 'give_email_footer', $this ); |
|
290
|
|
|
|
|
291
|
42 |
|
$body = ob_get_clean(); |
|
292
|
42 |
|
$message = str_replace( '{email}', $message, $body ); |
|
293
|
42 |
|
|
|
294
|
42 |
|
return apply_filters( 'give_email_message', $message, $this ); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
42 |
|
/** |
|
298
|
42 |
|
* Send the email. |
|
299
|
|
|
* |
|
300
|
|
|
* @param string $to The To address to send to. |
|
301
|
|
|
* @param string $subject The subject line of the email to send. |
|
302
|
|
|
* @param string $message The body of the email to send. |
|
303
|
|
|
* @param string|array $attachments Attachments to the email in a format supported by wp_mail(). |
|
304
|
|
|
* |
|
305
|
42 |
|
* @return bool |
|
306
|
|
|
*/ |
|
307
|
42 |
|
public function send( $to, $subject, $message, $attachments = '' ) { |
|
308
|
42 |
|
|
|
309
|
42 |
|
if ( ! did_action( 'init' ) && ! did_action( 'admin_init' ) ) { |
|
310
|
|
|
give_doing_it_wrong( __FUNCTION__, esc_html__( 'You cannot send email with Give_Emails until init/admin_init has been reached.', 'give' ), null ); |
|
311
|
42 |
|
|
|
312
|
|
|
return false; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* Fires before sending an email. |
|
317
|
|
|
* |
|
318
|
|
|
* @since 1.0 |
|
319
|
|
|
* |
|
320
|
|
|
* @param Give_Emails $this The email object. |
|
321
|
|
|
*/ |
|
322
|
|
|
do_action( 'give_email_send_before', $this ); |
|
323
|
|
|
|
|
324
|
|
|
$subject = $this->parse_tags( $subject ); |
|
325
|
|
|
$message = $this->parse_tags( $message ); |
|
326
|
|
|
|
|
327
|
|
|
$message = $this->build_email( $message ); |
|
328
|
|
|
|
|
329
|
|
|
$attachments = apply_filters( 'give_email_attachments', $attachments, $this ); |
|
330
|
|
|
|
|
331
|
|
|
$sent = wp_mail( $to, $subject, $message, $this->get_headers(), $attachments ); |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* Fires after sending an email. |
|
335
|
|
|
* |
|
336
|
|
|
* @since 1.0 |
|
337
|
|
|
* |
|
338
|
|
|
* @param Give_Emails $this The email object. |
|
339
|
|
|
*/ |
|
340
|
|
|
do_action( 'give_email_send_after', $this ); |
|
341
|
|
|
|
|
342
|
|
|
return $sent; |
|
343
|
|
|
|
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
/** |
|
347
|
|
|
* Add filters / actions before the email is sent. |
|
348
|
|
|
* |
|
349
|
|
|
* @since 1.0 |
|
350
|
|
|
*/ |
|
351
|
|
|
public function send_before() { |
|
352
|
|
|
add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) ); |
|
353
|
|
|
add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) ); |
|
354
|
|
|
add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) ); |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* Remove filters / actions after the email is sent. |
|
359
|
|
|
* |
|
360
|
|
|
* @since 1.0 |
|
361
|
|
|
*/ |
|
362
|
|
|
public function send_after() { |
|
363
|
|
|
remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) ); |
|
364
|
|
|
remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) ); |
|
365
|
|
|
remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) ); |
|
366
|
|
|
|
|
367
|
|
|
// Reset heading to an empty string |
|
368
|
|
|
$this->heading = ''; |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
|
|
/** |
|
372
|
|
|
* Converts text to formatted HTML. This is primarily for turning line breaks into <p> and <br/> tags. |
|
373
|
|
|
* |
|
374
|
|
|
* @since 1.0 |
|
375
|
|
|
*/ |
|
376
|
|
|
public function text_to_html( $message ) { |
|
377
|
|
|
|
|
378
|
|
|
if ( 'text/html' == $this->content_type || true === $this->html ) { |
|
379
|
|
|
$message = wpautop( $message ); |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
return $message; |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
} |