PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.10.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.10.2
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
bit-form / includes / Core / Util / MailNotifier.php

MailNotifier.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.10.2, at includes/Core/Util/MailNotifier.php

168 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BitCode\BitForm\Core\Util;
4
5 use BitCode\BitForm\Core\Database\FormEntryMetaModel;
6 use BitCode\BitForm\Core\Messages\EmailTemplateHandler;
7 use BitCode\BitForm\Core\Messages\PdfTemplateHandler;
8 use BitCode\BitFormPro\Admin\AppSetting\Pdf;
9
10 final class MailNotifier
11 {
12 public static function notify($notifyDetails, $formID, $fieldValue, $entryID, $isDblOptin = false, $logId = '')
13 {
14 $emailTemplateHandler = new EmailTemplateHandler($formID);
15 $attachments = [];
16 $tempPdfLink = '';
17 if (!empty($notifyDetails->pdfId) && is_string($notifyDetails->pdfId)) {
18 $pdfTemplateID = json_decode($notifyDetails->pdfId)->id;
19 $pdfTemplateHandler = new PdfTemplateHandler($formID);
20
21 $pdfTemplate = $pdfTemplateHandler->getById($pdfTemplateID);
22 $pdfBody = FieldValueHandler::replaceFieldWithValue($pdfTemplate[0]->body, $fieldValue);
23
24 $pdfSetting = json_decode($pdfTemplate[0]->setting);
25
26 $path = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'pdf';
27 $fileName = 'bit-form-pdf-' . $formID . '-' . $entryID;
28
29 if (!is_dir($path)) {
30 mkdir($path, 0777, true);
31 }
32
33 if (class_exists('\BitCode\BitFormPro\Admin\AppSetting\Pdf')) {
34 $generatedPdf = Pdf::getInstance()->generator($pdfSetting, $pdfBody, $path, $entryID, 'F');
35
36 // $pdfFile = $path . DIRECTORY_SEPARATOR . $fileName . '.pdf';
37
38 if (!is_wp_error($generatedPdf) && file_exists($generatedPdf)) {
39 $attachments[] = $generatedPdf;
40 $tempPdfLink = $generatedPdf;
41 }
42 }
43 }
44
45 if (is_string($notifyDetails->id)) {
46 $mailTemplateID = json_decode($notifyDetails->id)->id;
47 $mailTemplate = $emailTemplateHandler->getATemplate($mailTemplateID);
48 if (!is_wp_error($mailTemplate)) {
49 $mailTo = FieldValueHandler::validateMailArry($notifyDetails->to, $fieldValue);
50 if (!empty($mailTo)) {
51 $from_name = '';
52 if (isset($notifyDetails->from_name) && !empty($notifyDetails->from_name)) {
53 $from_name = $notifyDetails->from_name;
54 }
55 (new MailConfig())->sendMail(['from_name' => $from_name]);
56 $mailSubject = FieldValueHandler::replaceFieldWithValue($mailTemplate[0]->sub, $fieldValue);
57 $mailBody = FieldValueHandler::replaceFieldWithValue($mailTemplate[0]->body, $fieldValue);
58
59 $mailHeaders = [
60 // "Content-Type: text/html; charset=UTF-8",
61 ];
62 if (!empty($notifyDetails->replyto)) {
63 $mailReplyTo = FieldValueHandler::validateMailArry($notifyDetails->replyto, $fieldValue);
64 if (is_array($mailReplyTo)) {
65 foreach ($mailReplyTo as $key => $emailAddress) {
66 $mailHeaders[] = 'Reply-To: ' . explode('@', $emailAddress)[0] . '<' . sanitize_email($emailAddress) . '>';
67 }
68 } else {
69 $mailHeaders[] = 'Reply-To: ' . explode('@', $mailReplyTo)[0] . '<' . sanitize_email($mailReplyTo) . '>';
70 }
71 }
72 $oldMailBody = $mailBody;
73 $data = [];
74 if ($isDblOptin && true === has_filter('bf_email_body_text')) {
75 $urlParams = $formID . '_' . $entryID . '_' . $logId;
76 $data = apply_filters('bf_email_body_text', $mailBody, $urlParams);
77 $mailBody = $data['mailbody'];
78 }
79
80 if (!empty($notifyDetails->bcc)) {
81 $mailBCC = FieldValueHandler::validateMailArry($notifyDetails->bcc, $fieldValue);
82 if (is_array($mailBCC)) {
83 foreach ($mailBCC as $key => $emailAddress) {
84 $mailHeaders[] = 'Bcc: ' . sanitize_email($emailAddress);
85 }
86 } else {
87 $mailHeaders[] = 'Bcc: ' . sanitize_email($mailBCC);
88 }
89 }
90 if (!empty($notifyDetails->cc)) {
91 $mailCC = FieldValueHandler::validateMailArry($notifyDetails->cc, $fieldValue);
92 if (is_array($mailCC)) {
93 foreach ($mailCC as $key => $emailAddress) {
94 $mailHeaders[] = 'Cc: ' . sanitize_email($emailAddress);
95 }
96 } else {
97 $mailHeaders[] = 'Cc: ' . sanitize_email($mailCC);
98 }
99 }
100 if (!empty($notifyDetails->from)) {
101 $mailFrom = FieldValueHandler::validateMailArry($notifyDetails->from, $fieldValue);
102 $fromName = !empty($notifyDetails->fromName) ? $notifyDetails->fromName : explode('@', $mailFrom[0])[0];
103 $mailHeaders[] = "FROM: $fromName " . '<' . sanitize_email($mailFrom[0]) . '>';
104 }
105
106 if (!empty($notifyDetails->attachment)) {
107 $files = $notifyDetails->attachment;
108 $fileBasePath = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR;
109 if (is_array($files)) {
110 foreach ($files as $file) {
111 if (isset($fieldValue[$file])) {
112 if (is_array($fieldValue[$file])) {
113 foreach ($fieldValue[$file] as $singleFile) {
114 if (\is_readable("{$fileBasePath}{$singleFile}")) {
115 $attachments[] = "{$fileBasePath}{$singleFile}";
116 }
117 }
118 } elseif (\is_readable("{$fileBasePath}{$fieldValue[$file]}")) {
119 $attachments[] = "{$fileBasePath}{$fieldValue[$file]}";
120 }
121 }
122 }
123 } elseif (isset($fieldValue[$files])) {
124 if (is_array($fieldValue[$files])) {
125 foreach ($fieldValue[$files] as $singleFile) {
126 if (\is_readable("{$fileBasePath}{$singleFile}")) {
127 $attachments[] = "{$fileBasePath}{$singleFile}";
128 }
129 }
130 } elseif (\is_readable("{$fileBasePath}{$fieldValue[$files]}")) {
131 $attachments[] = "{$fileBasePath}{$fieldValue[$files]}";
132 }
133 }
134 }
135 $mailBody = stripcslashes($mailBody);
136 $mailSubject = stripcslashes($mailSubject);
137 add_filter('wp_mail_content_type', [self::class, 'filterMailContentType']);
138 $status = wp_mail($mailTo, $mailSubject, $mailBody, $mailHeaders, $attachments);
139 if (!$status) {
140 $status = wp_mail($mailTo, $mailSubject, $mailBody, $mailHeaders);
141 }
142 if ($status && $isDblOptin && false !== strpos($oldMailBody, 'entry_confirmation_url')) {
143 $entryMeta = new FormEntryMetaModel();
144
145 $entryMeta->insert(
146 [
147 'bitforms_form_entry_id' => $entryID,
148 'meta_key' => 'entry_confirm_activation',
149 'meta_value' => $data['token']
150 ]
151 );
152 }
153 remove_filter('wp_mail_content_type', [self::class, 'filterMailContentType']);
154 }
155 }
156 }
157
158 if (!empty($tempPdfLink)) {
159 unlink($tempPdfLink);
160 }
161 }
162
163 public static function filterMailContentType()
164 {
165 return 'text/html; charset=UTF-8';
166 }
167 }
168