PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 4.16.8
GiveWP – Donation Plugin and Fundraising Platform v4.16.8
4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 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 11 months ago class-give-emails.php 3 days ago functions.php 6 years ago template.php 4 years ago
class-give-emails.php
436 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 // __set() accepts any key, so keys without a declared property become dynamic ones. PHP 8.2 deprecates
33 // those, and the notice is printed mid-request, which breaks any redirect that follows.
34 // PHP 7.4 reads this line as a comment, so it is safe on the minimum supported version.
35 #[AllowDynamicProperties]
36 class Give_Emails {
37
38 /**
39 * Holds the from address.
40 *
41 * @since 1.0
42 */
43 private $from_address;
44
45 /**
46 * Holds the from name.
47 *
48 * @since 1.0
49 */
50 private $from_name;
51
52 /**
53 * Holds the email content type.
54 *
55 * @since 1.0
56 */
57 private $content_type;
58
59 /**
60 * Holds the email headers.
61 *
62 * @since 1.0
63 */
64 private $headers;
65
66 /**
67 * Whether to send email in HTML.
68 *
69 * @since 1.0
70 */
71 private $html = true;
72
73 /**
74 * The email template to use.
75 *
76 * @since 1.0
77 */
78 private $template;
79
80 /**
81 * The header text for the email.
82 *
83 * @since 1.0
84 */
85 private $heading = '';
86
87 /**
88 * Email template tags argument.
89 * This helps to decode email template tags,
90 *
91 * @since 1.0
92 */
93 public $tag_args = array();
94
95 /**
96 * Form ID
97 *
98 * @since 1.0
99 */
100 public $form_id = 0;
101
102 /**
103 * Get things going.
104 *
105 * @since 1.0
106 */
107 public function __construct() {
108
109 if ( 'none' === $this->get_template() ) {
110 $this->html = false;
111 }
112
113 add_action( 'give_email_send_before', array( $this, 'send_before' ) );
114 add_action( 'give_email_send_after', array( $this, 'send_after' ) );
115
116 }
117
118 /**
119 * Set a property.
120 *
121 * @since 1.0
122 *
123 * @param $key
124 * @param $value
125 */
126 public function __set( $key, $value ) {
127 $this->$key = $value;
128 }
129
130 /**
131 * Get the email from name.
132 *
133 * @since 1.0
134 */
135 public function get_from_name() {
136 if ( ! $this->from_name ) {
137 $this->from_name = give_get_option( 'from_name', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) );
138 }
139
140 return apply_filters( 'give_email_from_name', wp_specialchars_decode( $this->from_name ), $this );
141 }
142
143 /**
144 * Get the email from address.
145 *
146 * @since 1.0
147 */
148 public function get_from_address() {
149 if ( ! $this->from_address ) {
150 $this->from_address = give_get_option( 'from_email', get_option( 'admin_email' ) );
151 }
152
153 return apply_filters( 'give_email_from_address', $this->from_address, $this );
154 }
155
156 /**
157 * Get the email content type.
158 *
159 * @since 1.0
160 */
161 public function get_content_type() {
162 if ( ! $this->content_type ) {
163 $this->content_type = $this->html
164 ? apply_filters( 'give_email_default_content_type', 'text/html', $this )
165 : 'text/plain';
166 }
167
168 return apply_filters( 'give_email_content_type', $this->content_type, $this );
169 }
170
171 /**
172 * Get the email headers.
173 *
174 * @since 1.0
175 */
176 public function get_headers() {
177 if ( ! $this->headers ) {
178 $this->headers = "From: {$this->get_from_name()} <{$this->get_from_address()}>\r\n";
179 $this->headers .= "Reply-To: {$this->get_from_address()}\r\n";
180 $this->headers .= "Content-Type: {$this->get_content_type()}; charset=utf-8\r\n";
181 }
182
183 return apply_filters( 'give_email_headers', $this->headers, $this );
184 }
185
186 /**
187 * Retrieve email templates.
188 *
189 * @since 1.0
190 */
191 public function get_templates() {
192 $templates = array(
193 'default' => esc_html__( 'Default Template', 'give' ),
194 'none' => esc_html__( 'No template, plain text only', 'give' ),
195 );
196
197 return apply_filters( 'give_email_templates', $templates );
198 }
199
200 /**
201 * Get the enabled email template.
202 *
203 * @since 1.0
204 */
205 public function get_template() {
206 if ( ! $this->template ) {
207 $this->template = give_get_option( 'email_template', 'default' );
208 }
209
210 return apply_filters( 'give_email_template', $this->template );
211 }
212
213 /**
214 * Get the header text for the email.
215 *
216 * @since 1.0
217 */
218 public function get_heading() {
219 return apply_filters( 'give_email_heading', $this->heading );
220 }
221
222 /**
223 * Parse email template tags.
224 *
225 * @param $content
226 *
227 * @return mixed
228 */
229 public function parse_tags( $content ) {
230 return $content;
231 }
232
233 /**
234 * Build the final email.
235 *
236 * @since 1.0
237 *
238 * @param $message
239 *
240 * @return string
241 */
242 public function build_email( $message ) {
243
244 if ( false === $this->html ) {
245
246 // Added Replacement check to simply behaviour of anchor tags.
247 $pattern = '/<a.+?href\=(?:["|\'])(.+?)(?:["|\']).*?>(.+?)<\/a>/i';
248 $message = preg_replace_callback(
249 $pattern,
250 function ( $return ) {
251 if ( $return[1] !== $return[2] ) {
252 return "{$return[2]} ( {$return[1]} )";
253 }
254
255 return trailingslashit( $return[1] );
256 },
257 $message
258 );
259
260 return apply_filters( 'give_email_message', wp_strip_all_tags( $message ), $this );
261 }
262
263 $message = $this->text_to_html( $message );
264
265 $template = $this->get_template();
266
267 ob_start();
268
269 give_get_template_part( 'emails/header', $template, true );
270
271 /**
272 * Fires in the email head.
273 *
274 * @since 1.0
275 *
276 * @param Give_Emails $this The email object.
277 */
278 do_action( 'give_email_header', $this );
279
280 if ( has_action( 'give_email_template_' . $template ) ) {
281 /**
282 * Fires in a specific email template.
283 *
284 * @since 1.0
285 */
286 do_action( "give_email_template_{$template}" );
287 } else {
288 give_get_template_part( 'emails/body', $template, true );
289 }
290
291 /**
292 * Fires in the email body.
293 *
294 * @since 1.0
295 *
296 * @param Give_Emails $this The email object.
297 */
298 do_action( 'give_email_body', $this );
299
300 give_get_template_part( 'emails/footer', $template, true );
301
302 /**
303 * Fires in the email footer.
304 *
305 * @since 1.0
306 *
307 * @param Give_Emails $this The email object.
308 */
309 do_action( 'give_email_footer', $this );
310
311 $body = ob_get_clean();
312
313 // Email tag.
314 $message = str_replace( '{email}', $message, $body );
315
316 $header_img = Give_Email_Notification_Util::get_email_logo( $this->form_id );
317
318 if ( ! empty( $header_img ) ) {
319 $header_img = sprintf(
320 '<div id="template_header_image"><p style="margin-top:0;"><img style="max-width:450px;" src="%1$s" alt="%2$s" /></p></div>',
321 esc_url( $header_img ),
322 get_bloginfo( 'name' )
323 );
324 }
325
326 $message = str_replace( '{email_logo}', $header_img, $message );
327
328 return apply_filters( 'give_email_message', $message, $this );
329 }
330
331 /**
332 * Send the email.
333 *
334 * @param string $to The To address to send to.
335 * @param string $subject The subject line of the email to send.
336 * @param string $message The body of the email to send.
337 * @param string|array $attachments Attachments to the email in a format supported by wp_mail().
338 *
339 * @return bool
340 */
341 public function send( $to, $subject, $message, $attachments = '' ) {
342
343 if ( ! did_action( 'init' ) && ! did_action( 'admin_init' ) ) {
344 give_doing_it_wrong( __FUNCTION__, esc_html__( 'You cannot send email with Give_Emails until init/admin_init has been reached.', 'give' ) );
345
346 return false;
347 }
348
349 /**
350 * Fires before sending an email.
351 *
352 * @since 1.0
353 *
354 * @param Give_Emails $this The email object.
355 */
356 do_action( 'give_email_send_before', $this );
357
358 $subject = $this->parse_tags( $subject );
359 $message = $this->parse_tags( $message );
360
361 $message = $this->build_email( $message );
362
363 $attachments = apply_filters( 'give_email_attachments', $attachments, $this );
364
365 $sent = wp_mail( $to, $subject, $message, $this->get_headers(), $attachments );
366
367 /**
368 * Fires after sending an email.
369 *
370 * @since 1.0
371 *
372 * @param Give_Emails $this The email object.
373 */
374 do_action( 'give_email_send_after', $this );
375
376 return $sent;
377
378 }
379
380 /**
381 * Add filters / actions before the email is sent.
382 *
383 * @since 1.0
384 */
385 public function send_before() {
386 add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
387 add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
388 add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
389 }
390
391 /**
392 * Remove filters / actions after the email is sent.
393 *
394 * @since 1.0
395 */
396 public function send_after() {
397 remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
398 remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
399 remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
400
401 // Reset email related params.
402 $this->heading = '';
403 $this->from_name = '';
404 $this->from_address = '';
405 $this->form_id = 0;
406 }
407
408 /**
409 * Converts text to formatted HTML. This is primarily for turning line breaks into <p> and <br/> tags.
410 *
411 * @since 1.0
412 *
413 * @param string $message
414 *
415 * @return string
416 */
417 public function text_to_html( $message ) {
418 /**
419 * Filter the flag which decide to process email message with wpautop or not.
420 *
421 * @since 2.3.0
422 */
423 $disable_wpautop = apply_filters( 'give_email_message_disable_wpautop', false );
424
425 if (
426 ( 'text/html' == $this->content_type || true === $this->html )
427 && ! $disable_wpautop
428 ) {
429 $message = wpautop( $message );
430 }
431
432 return $message;
433 }
434
435 }
436