PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.7.4
GiveWP – Donation Plugin and Fundraising Platform v2.7.4
4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / emails / class-give-emails.php
give / includes / emails Last commit date
actions.php 6 years ago class-give-email-tags.php 6 years ago class-give-emails.php 6 years ago functions.php 6 years ago template.php 6 years ago
class-give-emails.php
432 lines
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, GiveWP
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 * Email template tags argument.
85 * This helps to decode email template tags,
86 *
87 * @since 1.0
88 */
89 public $tag_args = array();
90
91 /**
92 * Form ID
93 *
94 * @since 1.0
95 */
96 public $form_id = 0;
97
98 /**
99 * Get things going.
100 *
101 * @since 1.0
102 */
103 public function __construct() {
104
105 if ( 'none' === $this->get_template() ) {
106 $this->html = false;
107 }
108
109 add_action( 'give_email_send_before', array( $this, 'send_before' ) );
110 add_action( 'give_email_send_after', array( $this, 'send_after' ) );
111
112 }
113
114 /**
115 * Set a property.
116 *
117 * @since 1.0
118 *
119 * @param $key
120 * @param $value
121 */
122 public function __set( $key, $value ) {
123 $this->$key = $value;
124 }
125
126 /**
127 * Get the email from name.
128 *
129 * @since 1.0
130 */
131 public function get_from_name() {
132 if ( ! $this->from_name ) {
133 $this->from_name = give_get_option( 'from_name', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) );
134 }
135
136 return apply_filters( 'give_email_from_name', wp_specialchars_decode( $this->from_name ), $this );
137 }
138
139 /**
140 * Get the email from address.
141 *
142 * @since 1.0
143 */
144 public function get_from_address() {
145 if ( ! $this->from_address ) {
146 $this->from_address = give_get_option( 'from_email', get_option( 'admin_email' ) );
147 }
148
149 return apply_filters( 'give_email_from_address', $this->from_address, $this );
150 }
151
152 /**
153 * Get the email content type.
154 *
155 * @since 1.0
156 */
157 public function get_content_type() {
158 if ( ! $this->content_type ) {
159 $this->content_type = $this->html
160 ? apply_filters( 'give_email_default_content_type', 'text/html', $this )
161 : 'text/plain';
162 }
163
164 return apply_filters( 'give_email_content_type', $this->content_type, $this );
165 }
166
167 /**
168 * Get the email headers.
169 *
170 * @since 1.0
171 */
172 public function get_headers() {
173 if ( ! $this->headers ) {
174 $this->headers = "From: {$this->get_from_name()} <{$this->get_from_address()}>\r\n";
175 $this->headers .= "Reply-To: {$this->get_from_address()}\r\n";
176 $this->headers .= "Content-Type: {$this->get_content_type()}; charset=utf-8\r\n";
177 }
178
179 return apply_filters( 'give_email_headers', $this->headers, $this );
180 }
181
182 /**
183 * Retrieve email templates.
184 *
185 * @since 1.0
186 */
187 public function get_templates() {
188 $templates = array(
189 'default' => esc_html__( 'Default Template', 'give' ),
190 'none' => esc_html__( 'No template, plain text only', 'give' ),
191 );
192
193 return apply_filters( 'give_email_templates', $templates );
194 }
195
196 /**
197 * Get the enabled email template.
198 *
199 * @since 1.0
200 */
201 public function get_template() {
202 if ( ! $this->template ) {
203 $this->template = give_get_option( 'email_template', 'default' );
204 }
205
206 return apply_filters( 'give_email_template', $this->template );
207 }
208
209 /**
210 * Get the header text for the email.
211 *
212 * @since 1.0
213 */
214 public function get_heading() {
215 return apply_filters( 'give_email_heading', $this->heading );
216 }
217
218 /**
219 * Parse email template tags.
220 *
221 * @param $content
222 *
223 * @return mixed
224 */
225 public function parse_tags( $content ) {
226 return $content;
227 }
228
229 /**
230 * Build the final email.
231 *
232 * @since 1.0
233 *
234 * @param $message
235 *
236 * @return string
237 */
238 public function build_email( $message ) {
239
240 if ( false === $this->html ) {
241
242 // Added Replacement check to simply behaviour of anchor tags.
243 $pattern = '/<a.+?href\=(?:["|\'])(.+?)(?:["|\']).*?>(.+?)<\/a>/i';
244 $message = preg_replace_callback(
245 $pattern,
246 function ( $return ) {
247 if ( $return[1] !== $return[2] ) {
248 return "{$return[2]} ( {$return[1]} )";
249 }
250
251 return trailingslashit( $return[1] );
252 },
253 $message
254 );
255
256 return apply_filters( 'give_email_message', wp_strip_all_tags( $message ), $this );
257 }
258
259 $message = $this->text_to_html( $message );
260
261 $template = $this->get_template();
262
263 ob_start();
264
265 give_get_template_part( 'emails/header', $template, true );
266
267 /**
268 * Fires in the email head.
269 *
270 * @since 1.0
271 *
272 * @param Give_Emails $this The email object.
273 */
274 do_action( 'give_email_header', $this );
275
276 if ( has_action( 'give_email_template_' . $template ) ) {
277 /**
278 * Fires in a specific email template.
279 *
280 * @since 1.0
281 */
282 do_action( "give_email_template_{$template}" );
283 } else {
284 give_get_template_part( 'emails/body', $template, true );
285 }
286
287 /**
288 * Fires in the email body.
289 *
290 * @since 1.0
291 *
292 * @param Give_Emails $this The email object.
293 */
294 do_action( 'give_email_body', $this );
295
296 give_get_template_part( 'emails/footer', $template, true );
297
298 /**
299 * Fires in the email footer.
300 *
301 * @since 1.0
302 *
303 * @param Give_Emails $this The email object.
304 */
305 do_action( 'give_email_footer', $this );
306
307 $body = ob_get_clean();
308
309 // Email tag.
310 $message = str_replace( '{email}', $message, $body );
311
312 $header_img = Give_Email_Notification_Util::get_email_logo( $this->form_id );
313
314 if ( ! empty( $header_img ) ) {
315 $header_img = sprintf(
316 '<div id="template_header_image"><p style="margin-top:0;"><img style="max-width:450px;" src="%1$s" alt="%2$s" /></p></div>',
317 esc_url( $header_img ),
318 get_bloginfo( 'name' )
319 );
320 }
321
322 $message = str_replace( '{email_logo}', $header_img, $message );
323
324 return apply_filters( 'give_email_message', $message, $this );
325 }
326
327 /**
328 * Send the email.
329 *
330 * @param string $to The To address to send to.
331 * @param string $subject The subject line of the email to send.
332 * @param string $message The body of the email to send.
333 * @param string|array $attachments Attachments to the email in a format supported by wp_mail().
334 *
335 * @return bool
336 */
337 public function send( $to, $subject, $message, $attachments = '' ) {
338
339 if ( ! did_action( 'init' ) && ! did_action( 'admin_init' ) ) {
340 give_doing_it_wrong( __FUNCTION__, esc_html__( 'You cannot send email with Give_Emails until init/admin_init has been reached.', 'give' ) );
341
342 return false;
343 }
344
345 /**
346 * Fires before sending an email.
347 *
348 * @since 1.0
349 *
350 * @param Give_Emails $this The email object.
351 */
352 do_action( 'give_email_send_before', $this );
353
354 $subject = $this->parse_tags( $subject );
355 $message = $this->parse_tags( $message );
356
357 $message = $this->build_email( $message );
358
359 $attachments = apply_filters( 'give_email_attachments', $attachments, $this );
360
361 $sent = wp_mail( $to, $subject, $message, $this->get_headers(), $attachments );
362
363 /**
364 * Fires after sending an email.
365 *
366 * @since 1.0
367 *
368 * @param Give_Emails $this The email object.
369 */
370 do_action( 'give_email_send_after', $this );
371
372 return $sent;
373
374 }
375
376 /**
377 * Add filters / actions before the email is sent.
378 *
379 * @since 1.0
380 */
381 public function send_before() {
382 add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
383 add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
384 add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
385 }
386
387 /**
388 * Remove filters / actions after the email is sent.
389 *
390 * @since 1.0
391 */
392 public function send_after() {
393 remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
394 remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
395 remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
396
397 // Reset email related params.
398 $this->heading = '';
399 $this->from_name = '';
400 $this->from_address = '';
401 $this->form_id = 0;
402 }
403
404 /**
405 * Converts text to formatted HTML. This is primarily for turning line breaks into <p> and <br/> tags.
406 *
407 * @since 1.0
408 *
409 * @param string $message
410 *
411 * @return string
412 */
413 public function text_to_html( $message ) {
414 /**
415 * Filter the flag which decide to process email message with wpautop or not.
416 *
417 * @since 2.3.0
418 */
419 $disable_wpautop = apply_filters( 'give_email_message_disable_wpautop', false );
420
421 if (
422 ( 'text/html' == $this->content_type || true === $this->html )
423 && ! $disable_wpautop
424 ) {
425 $message = wpautop( $message );
426 }
427
428 return $message;
429 }
430
431 }
432