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

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