PluginProbe ʕ •ᴥ•ʔ
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI / 1.6.3
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI v1.6.3
3.5.2 3.5.1 3.5.0 3.4.8 3.4.7 3.4.6 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5.1 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.10 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.6.1 1.6.7 1.7.0 1.7.0.1 1.7.0.2 1.7.0.3 1.7.1 1.7.2 1.7.2.1 1.7.2.2 1.7.3 1.7.4 1.7.5 1.7.5.1 1.7.5.2 1.7.6 1.7.7 1.7.7.1 1.7.7.2 1.7.8 1.7.9 1.8.0 1.8.0.1 1.8.1 1.8.2 1.8.2.1 1.8.2.2 1.8.2.3 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9.0 1.9.0.1 1.9.1 1.9.2 1.9.3 1.9.4 1.9.4.1 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.0.1 2.0.1 2.0.2 2.0.3 2.0.3.1 2.0.4 2.0.4.1 2.0.5 2.0.6 2.0.7 2.0.8 2.0.8.1 2.0.9 3.0.0 3.0.0.1 3.0.1 3.0.2 3.0.3 3.0.3.1 3.0.4 3.0.4.1 3.0.4.2 3.0.5 3.0.5.1 3.0.5.2 3.0.6 3.0.6.1 3.0.7.1 3.0.8 3.0.8.1 3.0.9 3.0.9.1 3.0.9.2 3.0.9.3 3.0.9.4 3.0.9.5 3.1.0 3.1.1 3.1.2 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.3.0 3.4.0 3.4.1 3.4.2 3.4.2.1 3.4.3 3.4.4 3.4.5 trunk 1.0 1.0.1 1.0.2 1.0.3
everest-forms / includes / class-evf-emails.php
everest-forms / includes Last commit date
abstracts 6 years ago admin 6 years ago export 6 years ago fields 6 years ago interfaces 8 years ago libraries 7 years ago log-handlers 6 years ago shortcodes 6 years ago templates 6 years ago class-everest-forms.php 6 years ago class-evf-ajax.php 6 years ago class-evf-autoloader.php 7 years ago class-evf-background-updater.php 7 years ago class-evf-cache-helper.php 6 years ago class-evf-deprecated-action-hooks.php 6 years ago class-evf-deprecated-filter-hooks.php 7 years ago class-evf-emails.php 6 years ago class-evf-fields.php 6 years ago class-evf-form-block.php 6 years ago class-evf-form-handler.php 6 years ago class-evf-form-task.php 6 years ago class-evf-forms-features.php 6 years ago class-evf-frontend-scripts.php 6 years ago class-evf-install.php 6 years ago class-evf-integrations.php 7 years ago class-evf-log-levels.php 8 years ago class-evf-logger.php 6 years ago class-evf-post-types.php 6 years ago class-evf-privacy.php 6 years ago class-evf-session-handler.php 7 years ago class-evf-shortcodes.php 7 years ago class-evf-smart-tags.php 6 years ago class-evf-template-loader.php 6 years ago class-evf-validation.php 6 years ago evf-conditional-functions.php 6 years ago evf-core-functions.php 6 years ago evf-deprecated-functions.php 6 years ago evf-entry-functions.php 6 years ago evf-formatting-functions.php 6 years ago evf-notice-functions.php 6 years ago evf-template-functions.php 6 years ago evf-template-hooks.php 7 years ago evf-update-functions.php 6 years ago
class-evf-emails.php
575 lines
1 <?php
2 /**
3 * This class handles all (notification) emails sent by Everest Forms.
4 *
5 * Heavily influenced by the great AffiliateWP plugin by Pippin Williamson.
6 * https://github.com/AffiliateWP/AffiliateWP/blob/master/includes/emails/class-affwp-emails.php
7 *
8 * @package EverestForms\Classes\Emails
9 * @version 1.2.0
10 * @since 1.0.0
11 */
12
13 defined( 'ABSPATH' ) || exit;
14
15 /**
16 * Email class.
17 */
18 class EVF_Emails {
19
20 /**
21 * Holds the from address.
22 *
23 * @var string
24 */
25 private $from_address;
26
27 /**
28 * Holds the from name.
29 *
30 * @var string
31 */
32 private $from_name;
33
34 /**
35 * Holds the reply-to address.
36 *
37 * @var string
38 */
39 private $reply_to = false;
40
41 /**
42 * Holds the carbon copy addresses.
43 *
44 * @var string
45 */
46 private $cc = false;
47
48 /**
49 * Holds the blind carbon copy addresses.
50 *
51 * @var string
52 */
53 private $bcc = false;
54
55 /**
56 * Holds the email content type.
57 *
58 * @var string
59 */
60 private $content_type;
61
62 /**
63 * Holds the email headers.
64 *
65 * @var string
66 */
67 private $headers;
68
69 /**
70 * Holds the email attachments.
71 *
72 * @var string
73 */
74 public $attachments = '';
75
76 /**
77 * Whether to send email in HTML.
78 *
79 * @var bool
80 */
81 private $html = true;
82
83 /**
84 * The email template to use.
85 *
86 * @var string
87 */
88 private $template;
89
90 /**
91 * Form data.
92 *
93 * @var array
94 */
95 public $form_data = array();
96
97 /**
98 * Fields, formatted, and sanitized.
99 *
100 * @var array
101 */
102 public $fields = array();
103
104 /**
105 * Entry ID.
106 *
107 * @var int
108 */
109 public $entry_id = '';
110
111 /**
112 * Constructor.
113 */
114 public function __construct() {
115 if ( 'none' === $this->get_template() ) {
116 $this->html = false;
117 }
118
119 // Hooks.
120 add_action( 'everest_forms_email_send_before', array( $this, 'send_before' ) );
121 add_action( 'everest_forms_email_send_after', array( $this, 'send_after' ) );
122 }
123
124 /**
125 * Set a property.
126 *
127 * @param string $key Object property key.
128 * @param mixed $value Object property value.
129 */
130 public function __set( $key, $value ) {
131 $this->$key = $value;
132 }
133
134 /**
135 * Get the email from name.
136 *
137 * @return string The email from name.
138 */
139 public function get_from_name() {
140 if ( ! empty( $this->from_name ) ) {
141 $this->from_name = $this->process_tag( $this->from_name );
142 } else {
143 $this->from_name = get_bloginfo( 'name' );
144 }
145
146 return apply_filters( 'everest_forms_email_from_name', wp_specialchars_decode( $this->from_name ), $this );
147 }
148
149 /**
150 * Get the email from address.
151 *
152 * @return string The email from address.
153 */
154 public function get_from_address() {
155 if ( ! empty( $this->from_address ) ) {
156 $this->from_address = $this->process_tag( $this->from_address );
157 } else {
158 $this->from_address = get_option( 'admin_email' );
159 }
160
161 return apply_filters( 'everest_forms_email_from_address', $this->from_address, $this );
162 }
163
164 /**
165 * Get the email reply-to.
166 *
167 * @return string The email reply-to address.
168 */
169 public function get_reply_to() {
170 if ( ! empty( $this->reply_to ) ) {
171 $this->reply_to = $this->process_tag( $this->reply_to );
172
173 if ( ! is_email( $this->reply_to ) ) {
174 $this->reply_to = false;
175 }
176 }
177
178 return apply_filters( 'everest_forms_email_reply_to', $this->reply_to, $this );
179 }
180
181 /**
182 * Get the email carbon copy addresses.
183 *
184 * @return string The email carbon copy addresses.
185 */
186 public function get_cc() {
187 if ( ! empty( $this->cc ) ) {
188 $this->cc = $this->process_tag( $this->cc );
189 $addresses = array_map( 'trim', explode( ',', $this->cc ) );
190
191 foreach ( $addresses as $key => $address ) {
192 if ( ! is_email( $address ) ) {
193 unset( $addresses[ $key ] );
194 }
195 }
196
197 $this->cc = implode( ',', $addresses );
198 }
199
200 return apply_filters( 'everest_forms_email_cc', $this->cc, $this );
201 }
202
203 /**
204 * Get the email blind carbon copy addresses.
205 *
206 * @return string The email blind carbon copy addresses.
207 */
208 public function get_bcc() {
209 if ( ! empty( $this->bcc ) ) {
210 $this->bcc = $this->process_tag( $this->bcc );
211 $addresses = array_map( 'trim', explode( ',', $this->bcc ) );
212
213 foreach ( $addresses as $key => $address ) {
214 if ( ! is_email( $address ) ) {
215 unset( $addresses[ $key ] );
216 }
217 }
218
219 $this->bcc = implode( ',', $addresses );
220 }
221
222 return apply_filters( 'everest_forms_email_bcc', $this->bcc, $this );
223 }
224
225 /**
226 * Get the email content type.
227 *
228 * @return string The email content type.
229 */
230 public function get_content_type() {
231 if ( ! $this->content_type && $this->html ) {
232 $this->content_type = apply_filters( 'everest_forms_email_default_content_type', 'text/html', $this );
233 } elseif ( ! $this->html ) {
234 $this->content_type = 'text/plain';
235 }
236
237 return apply_filters( 'everest_forms_email_content_type', $this->content_type, $this );
238 }
239
240 /**
241 * Get the email headers.
242 *
243 * @return string The email headers.
244 */
245 public function get_headers() {
246 if ( ! $this->headers ) {
247 $this->headers = "From: {$this->get_from_name()} <{$this->get_from_address()}>\r\n";
248 if ( $this->get_reply_to() ) {
249 $this->headers .= "Reply-To: {$this->get_reply_to()}\r\n";
250 }
251 if ( $this->get_cc() ) {
252 $this->headers .= "Cc: {$this->get_cc()}\r\n";
253 }
254 if ( $this->get_bcc() ) {
255 $this->headers .= "Bcc: {$this->get_bcc()}\r\n";
256 }
257 $this->headers .= "Content-Type: {$this->get_content_type()}; charset=utf-8\r\n";
258 }
259
260 return apply_filters( 'everest_forms_email_headers', $this->headers, $this );
261 }
262
263 /**
264 * Build the email.
265 *
266 * @param string $message The email message.
267 * @return string
268 */
269 public function build_email( $message ) {
270 if ( false === $this->html ) {
271 $message = $this->process_tag( $message, false, true );
272 $message = str_replace( '{all_fields}', $this->everest_forms_html_field_value( false ), $message );
273
274 return apply_filters( 'everest_forms_email_message', $message, $this );
275 }
276
277 ob_start();
278
279 evf_get_template( 'emails/header-' . $this->get_template() . '.php' );
280
281 // Hooks into the email header.
282 do_action( 'everest_forms_email_header', $this );
283
284 evf_get_template( 'emails/body-' . $this->get_template() . '.php' );
285
286 // Hooks into the email body.
287 do_action( 'everest_forms_email_body', $this );
288
289 evf_get_template( 'emails/footer-' . $this->get_template() . '.php' );
290
291 // Hooks into the email footer.
292 do_action( 'everest_forms_email_footer', $this );
293
294 $message = $this->process_tag( $message, false );
295 $message = nl2br( $message );
296
297 $body = ob_get_clean();
298 $message = str_replace( '{email}', $message, $body );
299 $message = str_replace( '{all_fields}', $this->everest_forms_html_field_value( true ), $message );
300 $message = make_clickable( $message );
301
302 return apply_filters( 'everest_forms_email_message', $message, $this );
303 }
304
305 /**
306 * Send the email.
307 *
308 * @param string $to The To address.
309 * @param string $subject The subject line of the email.
310 * @param string $message The body of the email.
311 * @param array $attachments Attachments to the email.
312 *
313 * @return bool
314 */
315 public function send( $to, $subject, $message, $attachments = '' ) {
316 if ( ! did_action( 'init' ) && ! did_action( 'admin_init' ) ) {
317 evf_doing_it_wrong( __FUNCTION__, __( 'You cannot send emails with EVF_Emails until init/admin_init has been reached', 'everest-forms' ), null );
318 return false;
319 }
320
321 // Don't send anything if emails have been disabled.
322 if ( $this->is_email_disabled() ) {
323 return false;
324 }
325
326 // Don't send if email address is invalid.
327 if ( ! is_email( $to ) ) {
328 return false;
329 }
330
331 // Hooks before email is sent.
332 do_action( 'everest_forms_email_send_before', $this );
333
334 $message = $this->build_email( $message );
335 $this->attachments = apply_filters( 'everest_forms_email_attachments', $this->attachments, $this );
336 $subject = evf_decode_string( $this->process_tag( $subject ) );
337
338 // Let's do this.
339 $sent = wp_mail( $to, $subject, $message, $this->get_headers(), $this->attachments );
340
341 // Hooks after the email is sent.
342 do_action( 'everest_forms_email_send_after', $this );
343
344 return $sent;
345 }
346
347 /**
348 * Add filters/actions before the email is sent.
349 */
350 public function send_before() {
351 add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
352 add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
353 add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
354 }
355
356 /**
357 * Remove filters/actions after the email is sent.
358 */
359 public function send_after() {
360 remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
361 remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
362 remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
363 }
364
365 /**
366 * Converts text formatted HTML. This is primarily for turning line breaks
367 * into <p> and <br/> tags.
368 *
369 * @param string $message Text to convert.
370 * @return string
371 */
372 public function text_to_html( $message ) {
373 if ( 'text/html' === $this->content_type || true === $this->html ) {
374 $message = wpautop( $message );
375 }
376
377 return $message;
378 }
379
380 /**
381 * Processes a smart tag.
382 *
383 * @param string $string String that may contain tags.
384 * @param bool $sanitize Toggle to maybe sanitize.
385 * @param bool $linebreaks Toggle to process linebreaks.
386 *
387 * @return string
388 */
389 public function process_tag( $string = '', $sanitize = true, $linebreaks = false ) {
390 $tag = apply_filters( 'everest_forms_process_smart_tags', $string, $this->form_data, $this->fields, $this->entry_id );
391 $tag = evf_decode_string( $tag );
392
393 if ( $sanitize ) {
394 if ( $linebreaks ) {
395 $tag = evf_sanitize_textarea_field( $tag );
396 } else {
397 $tag = sanitize_text_field( $tag );
398 }
399 }
400
401 return $tag;
402 }
403
404 /**
405 * Process the all fields smart tag if present.
406 *
407 * @param bool $html Toggle to use HTML or plaintext.
408 * @return string
409 */
410 public function everest_forms_html_field_value( $html = true ) {
411 if ( empty( $this->fields ) ) {
412 return '';
413 }
414
415 // Make sure we have an entry id.
416 if ( ! empty( $this->entry_id ) ) {
417 $this->form_data['entry_id'] = (int) $this->entry_id;
418 }
419
420 $message = '';
421
422 if ( $html ) {
423 /*
424 * HTML emails.
425 */
426 ob_start();
427
428 // Hooks into the email field.
429 do_action( 'everest_forms_email_field', $this );
430
431 evf_get_template( 'emails/field-' . $this->get_template() . '.php' );
432
433 $field_template = ob_get_clean();
434
435 $x = 1;
436 foreach ( $this->fields as $field ) {
437
438 if (
439 ! apply_filters( 'everest_forms_email_display_empty_fields', false ) &&
440 ( empty( $field['value'] ) && '0' !== $field['value'] )
441 ) {
442 continue;
443 }
444
445 $field_val = empty( $field['value'] ) && '0' !== $field['value'] ? '<em>' . __( '(empty)', 'everest-forms' ) . '</em>' : $field['value'];
446 $field_name = isset( $field_val['name'] ) ? $field_val['name'] : $field['name'];
447 $field_label = ! empty( $field_val['label'] ) ? $field_val['label'] : $field_val;
448 $field_type = $field['type'];
449
450 // If empty label is provided for choice field, don't store their data nor send email.
451 if ( in_array( $field_type, array( 'radio', 'payment-multiple' ), true ) ) {
452 if ( isset( $field_val['label'] ) && '' === $field_val['label'] ) {
453 continue;
454 }
455 } elseif ( in_array( $field_type, array( 'checkbox', 'payment-checkbox' ), true ) ) {
456 if ( isset( $field_val['label'] ) && ( empty( $field_val['label'] ) || '' === current( $field_val['label'] ) ) ) {
457 continue;
458 }
459 }
460
461 if ( isset( $field['value'], $field['value_raw'] ) && is_string( $field['value'] ) && in_array( $field_type, array( 'image-upload', 'file-upload' ), true ) ) {
462 $field['value'] = $field;
463 }
464
465 if ( isset( $field_val['type'] ) && in_array( $field['type'], array( 'image-upload', 'file-upload', 'rating' ), true ) ) {
466 if ( 'rating' === $field_val['type'] ) {
467 $value = ! empty( $field_val['value'] ) ? $field_val['value'] : 0;
468 $number_of_stars = ! empty( $field_val['number_of_rating'] ) ? $field_val['number_of_rating'] : 5;
469 $field_val = $value . '/' . $number_of_stars;
470 } else {
471 $field_val = empty( $field_val['file_url'] ) ? '<em>' . __( '(empty)', 'everest-forms' ) . '</em>' : $field_val;
472 }
473 }
474
475 if ( 'rating' !== $field_type ) {
476 if ( is_array( $field_label ) ) {
477 $field_html = array();
478 foreach ( $field_label as $meta_val ) {
479 $field_html[] = esc_html( $meta_val );
480 }
481 $field_val = implode( ', ', $field_html );
482 } else {
483 $field_val = esc_html( $field_label );
484 }
485 }
486
487 if ( empty( $field_name ) ) {
488 $field_name = sprintf(
489 /* translators: %d - field ID. */
490 esc_html__( 'Field ID #%d', 'everest-forms' ),
491 absint( $field['id'] )
492 );
493 }
494
495 $field_item = $field_template;
496 if ( 1 === $x ) {
497 $field_item = str_replace( 'border-top:1px solid #dddddd;', '', $field_item );
498 }
499
500 $field_item = str_replace( '{field_name}', $field_name, $field_item );
501 $field_value = apply_filters( 'everest_forms_html_field_value', evf_decode_string( $field_val ), $field['value'], $this->form_data, 'email-html' );
502 $field_item = str_replace( '{field_value}', $field_value, $field_item );
503
504 $message .= wpautop( $field_item );
505 $x ++;
506 }
507 } else {
508 /*
509 * Plain Text emails.
510 */
511 foreach ( $this->fields as $field ) {
512 if ( ! apply_filters( 'everest_forms_email_display_empty_fields', false ) && ( empty( $field['value'] ) && '0' !== $field['value'] ) ) {
513 continue;
514 }
515
516 $field_val = empty( $field['value'] ) && '0' !== $field['value'] ? esc_html__( '(empty)', 'everest-forms' ) : $field['value'];
517 $field_name = $field['name'];
518
519 if ( is_array( $field_val ) ) {
520 $field_html = array();
521
522 foreach ( $field_val as $meta_val ) {
523 $field_html[] = $meta_val;
524 }
525
526 $field_val = implode( ', ', $field_html );
527 }
528
529 if ( empty( $field_name ) ) {
530 $field_name = sprintf(
531 /* translators: %d - field ID. */
532 esc_html__( 'Field ID #%d', 'everest-forms' ),
533 absint( $field['id'] )
534 );
535 }
536
537 $message .= '--- ' . evf_decode_string( $field_name ) . " ---\r\n\r\n";
538 $field_value = evf_decode_string( $field_val ) . "\r\n\r\n";
539 $message .= apply_filters( 'everest_forms_plaintext_field_value', $field_value, $field['value'], $this->form_data, 'email-plain' );
540 }
541 }
542
543 if ( empty( $message ) ) {
544 $empty_message = esc_html__( 'An empty form was submitted.', 'everest-forms' );
545 $message = $html ? wpautop( $empty_message ) : $empty_message;
546 }
547
548 return $message;
549 }
550
551 /**
552 * Email kill switch if needed.
553 *
554 * @return bool
555 */
556 public function is_email_disabled() {
557 return (bool) apply_filters( 'everest_forms_disable_all_emails', false, $this );
558 }
559
560 /**
561 * Get the enabled email template.
562 *
563 * @todo Email template.
564 *
565 * @return string When filtering return 'none' to switch to text/plain email.
566 */
567 public function get_template() {
568 if ( ! $this->template ) {
569 $this->template = get_option( 'everest_forms_email_template', 'default' );
570 }
571
572 return apply_filters( 'everest_forms_email_template', $this->template );
573 }
574 }
575