PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / V3.0.3
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder vV3.0.3
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 V3.0.3, at includes/Core/Util/MailNotifier.php

330 lines 14.0 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 if (!defined('ABSPATH')) {
6 exit;
7 }
8
9 use BitCode\BitForm\Admin\Form\Helpers;
10 use BitCode\BitForm\Core\Database\FormEntryMetaModel;
11 use BitCode\BitForm\Core\Form\FormManager;
12 use BitCode\BitForm\Core\Messages\EmailTemplateHandler;
13 use BitCode\BitForm\Core\Messages\PdfTemplateHandler;
14
15 final class MailNotifier
16 {
17 public static function notify($notifyDetails, $formID, $fieldValue, $entryID, $isDblOptin = false, $logId = '')
18 {
19 $apiResponse = new ApiResponse();
20 $formManager = FormManager::getInstance($formID);
21 $entryDetails = ['formId' => $formID, 'entryId' => $entryID, 'fieldValues' => $fieldValue];
22 $emailTemplateHandler = new EmailTemplateHandler($formID);
23 $attachments = [];
24 $tempPdfLink = '';
25 $pdfPassForEmail = '';
26 if (!empty($notifyDetails->pdfId) && is_string($notifyDetails->pdfId)) {
27 $pdfTemplateID = json_decode($notifyDetails->pdfId)->id;
28 $pdfTemplateHandler = new PdfTemplateHandler($formID);
29
30 $pdfTemplate = $pdfTemplateHandler->getById($pdfTemplateID);
31
32 $pdfSetting = json_decode($pdfTemplate[0]->setting);
33 // error_log('PDF Setting before pdfFileName: ' . print_r([$pdfSetting, $fieldValue], true));
34
35 $path = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'pdf';
36 // $fileName = 'bit-form-pdf-' . $formID . '-' . $entryID;
37
38 if (!is_dir($path)) {
39 wp_mkdir_p($path);
40 }
41
42 if (class_exists('\BitCode\BitFormPro\Admin\AppSetting\Pdf')) {
43 $serverPath = Helpers::getFullPathWithEncryptedEntryId($formID, $entryID);
44 $webPath = Helpers::getWebPathWithEncryptedEntryId($formID, $entryID);
45
46 if (isset($pdfSetting->password)) {
47 if (isset($pdfSetting->password->static) && $pdfSetting->password->static && !empty($pdfSetting->password->pass)) {
48 $pass = FieldValueHandler::replaceFieldWithValue($pdfSetting->password->pass, $fieldValue);
49 $pdfSetting->password->pass = $pass;
50 } elseif (isset($pdfSetting->password->dynamic)) {
51 $pass = Helpers::PDFPassHash($entryID);
52 $pdfSetting->password->pass = $pass;
53 }
54 }
55 if (isset($pdfSetting->pdfFileName)) {
56 $pdfSetting->pdfFileName = FieldValueHandler::replaceFieldWithValue($pdfSetting->pdfFileName, $fieldValue);
57 // allow developers to modify PDF filename
58 $pdfSetting->pdfFileName = apply_filters(
59 'bitform_filter_pdf_filename',
60 $pdfSetting->pdfFileName,
61 [
62 'form_id' => $formID,
63 'entry_id' => $entryID,
64 'field_values' => $fieldValue,
65 'pdf_setting' => $pdfSetting,
66 'template' => $pdfTemplate[0] ?? null,
67 ]
68 );
69 }
70
71 $fieldValue['entry_id'] = $entryID;
72
73 $pdfBody = FieldValueHandler::replaceFieldWithValue($pdfTemplate[0]->body, $fieldValue, $formID);
74 $pdfBody = FieldValueHandler::changeImagePathInHTMLString($pdfBody, $serverPath);
75 $pdfBody = FieldValueHandler::changeHrefPathInHTMLString($pdfBody, $webPath); // replace anchor tag href with constructed weburl
76
77 // allow developers to modify PDF body
78 $pdfBody = apply_filters(
79 'bitform_filter_pdf_body',
80 $pdfBody,
81 [
82 'form_id' => $formID,
83 'entry_id' => $entryID,
84 'field_values' => $fieldValue,
85 'pdf_setting' => $pdfSetting,
86 'template' => $pdfTemplate[0] ?? null,
87 ]
88 );
89
90 $generatedPdf = \BitCode\BitFormPro\Admin\AppSetting\Pdf::getInstance()->generator($pdfSetting, $pdfBody, $path, $entryID, 'F');
91
92 if (!is_wp_error($generatedPdf) && file_exists($generatedPdf)) {
93 $attachments[] = $generatedPdf;
94 $tempPdfLink = $generatedPdf;
95 $apiResponse->apiResponse($logId, '', ['type' => 'record', 'type_name' => 'pdf'], 'success', 'PDF successfully generated.', $entryDetails);
96 Log::debug_log([
97 'status' => 'success',
98 'code' => 'pdf_generated',
99 'message' => 'PDF successfully generated',
100 'inputDetails' => [
101 'notifyDetails' => $notifyDetails,
102 'formID' => $formID,
103 'entryID' => $entryID,
104 'isDblOptin' => $isDblOptin,
105 'logId' => $logId
106 ],
107 'responseDetails' => $generatedPdf
108 ]);
109 } else {
110 $apiResponse->apiResponse($logId, '', ['type' => 'record', 'type_name' => 'pdf'], 'errors', 'Error in generating PDF.', $entryDetails);
111 Log::debug_log([
112 'status' => 'error',
113 'code' => 'pdf_generation_error',
114 'message' => 'Error in generating PDF',
115 'inputDetails' => [
116 'notifyDetails' => $notifyDetails,
117 'formID' => $formID,
118 'entryID' => $entryID,
119 'isDblOptin' => $isDblOptin,
120 'logId' => $logId
121 ],
122 'responseDetails' => $generatedPdf->get_error_message()
123 ]);
124 }
125 }
126 }
127
128 if (is_string($notifyDetails->id)) {
129 $mailTemplateID = json_decode($notifyDetails->id)->id;
130 $mailTemplate = $emailTemplateHandler->getATemplate($mailTemplateID);
131 if (!is_wp_error($mailTemplate)) {
132 $mailTo = FieldValueHandler::validateMailArry($notifyDetails->to, $fieldValue);
133 if (!empty($mailTo)) {
134 $from_name = '';
135 if (isset($notifyDetails->from_name) && !empty($notifyDetails->from_name)) {
136 $from_name = $notifyDetails->from_name;
137 }
138 $mailHeaders = [
139 // 'Content-Type: text/html; charset=UTF-8',
140 // $embeddedMailHeader
141 ];
142 $from_mail = '';
143 if (!empty($notifyDetails->from)) {
144 $fromMail = FieldValueHandler::validateMailArry($notifyDetails->from, $fieldValue);
145 $headerFromName = !empty($notifyDetails->fromName) ? $notifyDetails->fromName : explode('@', $fromMail[0])[0];
146 $mailHeaders[] = "FROM: $headerFromName " . '<' . sanitize_email($fromMail[0]) . '>';
147 $from_mail = $fromMail[0];
148 }
149 (new MailConfig())->sendMail(['from_name' => $from_name, 'from_email' => $from_mail]);
150 $mailSubject = FieldValueHandler::replaceFieldWithValue($mailTemplate[0]->sub, $fieldValue, $formID);
151
152 // allow developers to modify email subject
153 $mailSubject = apply_filters(
154 'bitform_filter_email_subject',
155 $mailSubject,
156 [
157 'form_id' => $formID,
158 'entry_id' => $entryID,
159 'field_values' => $fieldValue,
160 'template' => $mailTemplate[0] ?? null,
161 'is_double_optin' => (bool) $isDblOptin,
162 ]
163 );
164
165 $mailBody = $mailTemplate[0]->body;
166 if (class_exists('\BitCode\BitFormPro\Admin\DownloadFile')) {
167 $downloadFile = new \BitCode\BitFormPro\Admin\DownloadFile();
168 $mailBody = $downloadFile->replacePdfShortCodeToLink($mailBody, $formID, $entryID);
169 $mailBody = $downloadFile->replaceShortCodeToPdfPassword($mailBody, $formID, $entryID);
170 }
171
172 $mailBody = FieldValueHandler::replaceFieldWithValue($mailBody, $fieldValue, $formID);
173 $webUrl = Helpers::getWebPathWithEncryptedEntryId($formID, $entryID);
174 $mailBody = FieldValueHandler::changeImagePathInHTMLString($mailBody, $webUrl);
175 $mailBody = FieldValueHandler::changeHrefPathInHTMLString($mailBody, $webUrl); // replace anchor tag href with constructed weburl
176
177 // allow developers to modify email body
178 $mailBody = apply_filters(
179 'bitform_filter_email_body',
180 $mailBody,
181 [
182 'form_id' => $formID,
183 'entry_id' => $entryID,
184 'field_values' => $fieldValue,
185 'template' => $mailTemplate[0] ?? null,
186 'is_double_optin' => (bool) $isDblOptin,
187 ]
188 );
189 if (!empty($notifyDetails->replyto)) {
190 $mailReplyTo = FieldValueHandler::validateMailArry($notifyDetails->replyto, $fieldValue);
191 if (is_array($mailReplyTo)) {
192 foreach ($mailReplyTo as $key => $emailAddress) {
193 $mailHeaders[] = 'Reply-To: ' . explode('@', $emailAddress)[0] . '<' . sanitize_email($emailAddress) . '>';
194 }
195 } else {
196 $mailHeaders[] = 'Reply-To: ' . explode('@', $mailReplyTo)[0] . '<' . sanitize_email($mailReplyTo) . '>';
197 }
198 }
199 $oldMailBody = $mailBody;
200 $data = [];
201 if ($isDblOptin && true === has_filter('bitform_email_body_text')) {
202 $urlParams = $formID . '_' . $entryID . '_' . $logId;
203 $data = apply_filters('bitform_email_body_text', $mailBody, $urlParams);
204 $mailBody = $data['mailbody'];
205 }
206
207 if (!empty($notifyDetails->bcc)) {
208 $mailBCC = FieldValueHandler::validateMailArry($notifyDetails->bcc, $fieldValue);
209 if (is_array($mailBCC)) {
210 foreach ($mailBCC as $key => $emailAddress) {
211 $mailHeaders[] = 'Bcc: ' . sanitize_email($emailAddress);
212 }
213 } else {
214 $mailHeaders[] = 'Bcc: ' . sanitize_email($mailBCC);
215 }
216 }
217 if (!empty($notifyDetails->cc)) {
218 $mailCC = FieldValueHandler::validateMailArry($notifyDetails->cc, $fieldValue);
219 if (is_array($mailCC)) {
220 foreach ($mailCC as $key => $emailAddress) {
221 $mailHeaders[] = 'Cc: ' . sanitize_email($emailAddress);
222 }
223 } else {
224 $mailHeaders[] = 'Cc: ' . sanitize_email($mailCC);
225 }
226 }
227 if (!empty($notifyDetails->attachment)) {
228 $fileFldKeys = $notifyDetails->attachment;
229 $fileBasePath = FileHandler::getEntriesFileUploadDir($formID, $entryID) . DIRECTORY_SEPARATOR;
230
231 // Normalize to array for consistent processing
232 $fileFldKeys = is_array($fileFldKeys) ? $fileFldKeys : [$fileFldKeys];
233
234 foreach ($fileFldKeys as $fldKey) {
235 $repeaterFieldKey = $formManager->isRepeatedField($fldKey);
236
237 if ($repeaterFieldKey) {
238 // Handle repeated file field
239 FileHandler::processRepeaterAttachment(
240 $repeaterFieldKey,
241 $fldKey,
242 $fieldValue,
243 $fileBasePath,
244 $attachments
245 );
246 } else {
247 // Handle regular file field
248 FileHandler::processRegularAttachment(
249 $fldKey,
250 $fieldValue,
251 $fileBasePath,
252 $attachments
253 );
254 }
255 }
256 }
257 $mailBody = stripcslashes($mailBody);
258 $mailSubject = stripcslashes($mailSubject);
259 add_filter('wp_mail_content_type', [self::class, 'filterMailContentType']);
260 $status = wp_mail($mailTo, $mailSubject, $mailBody, $mailHeaders, $attachments);
261
262 if (!$status) {
263 Log::debug_log([
264 'status' => 'error',
265 'code' => 'mail_not_sent',
266 'message' => 'Mail not sent',
267 'inputDetails' => [
268 'to' => $mailTo,
269 'subject' => $mailSubject,
270 'body' => $mailBody,
271 'headers' => $mailHeaders,
272 'attachments' => $attachments,
273 'notifyDetails' => $notifyDetails,
274 'formID' => $formID,
275 'entryID' => $entryID,
276 'isDblOptin' => $isDblOptin,
277 'logId' => $logId
278 ],
279 'responseDetails' => $status
280 ]);
281 $apiResponse->apiResponse($logId, '', ['type' => 'record', 'type_name' => 'smtp'], 'errors', 'Mail dose not send successfully', $entryDetails);
282 } else {
283 Log::debug_log([
284 'status' => 'success',
285 'code' => 'mail_sent',
286 'message' => 'Mail successfully sent',
287 'inputDetails' => [
288 'to' => $mailTo,
289 'subject' => $mailSubject,
290 'body' => $mailBody,
291 'headers' => $mailHeaders,
292 'attachments' => $attachments,
293 'notifyDetails' => $notifyDetails,
294 'formID' => $formID,
295 'entryID' => $entryID,
296 'isDblOptin' => $isDblOptin,
297 'logId' => $logId
298 ],
299 'responseDetails' => $status
300 ]);
301 $apiResponse->apiResponse($logId, '', ['type' => 'record', 'type_name' => 'smtp'], 'success', 'Mail successfully send.', $entryDetails);
302 }
303 if ($status && $isDblOptin && false !== strpos($oldMailBody, 'entry_confirmation_url')) {
304 $entryMeta = new FormEntryMetaModel();
305 $apiResponse->apiResponse($logId, '', ['type' => 'record', 'type_name' => 'smtp'], 'success', 'Mail successfully send.', $entryDetails);
306 // Form entry meta insert; meta_key/meta_value required to store dynamic field data per entry.
307 $entryMeta->insert(
308 [
309 'bitforms_form_entry_id' => $entryID,
310 'meta_key' => 'entry_confirm_activation',
311 'meta_value' => $data['token']
312 ]
313 );
314 }
315 remove_filter('wp_mail_content_type', [self::class, 'filterMailContentType']);
316 }
317 }
318 }
319
320 if (!empty($tempPdfLink)) {
321 wp_delete_file($tempPdfLink);
322 }
323 }
324
325 public static function filterMailContentType()
326 {
327 return 'text/html; charset=UTF-8';
328 }
329 }
330