PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.15.3
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.15.3
3.3.1 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 All 138 releases
← All changes | includes/Core/Util/MailNotifier.php +47 -287 3.1.02.15.3 View file →
@@ -1,127 +1,46 @@
1 1 <?php
2 2
3 3 namespace BitCode\BitForm\Core\Util;
4 4
5 -if (!defined('ABSPATH')) {
6 - exit;
7 -}
8 -
9 -use BitCode\BitForm\Admin\Form\Helpers;
10 5 use BitCode\BitForm\Core\Database\FormEntryMetaModel;
11 -use BitCode\BitForm\Core\Form\FormManager;
12 6 use BitCode\BitForm\Core\Messages\EmailTemplateHandler;
13 7 use BitCode\BitForm\Core\Messages\PdfTemplateHandler;
8 +use BitCode\BitFormPro\Admin\AppSetting\Pdf;
14 9
15 10 final class MailNotifier
16 11 {
17 12 public static function notify($notifyDetails, $formID, $fieldValue, $entryID, $isDblOptin = false, $logId = '')
18 13 {
19 - $apiResponse = new ApiResponse();
20 - $formManager = FormManager::getInstance($formID);
21 - $entryDetails = ['formId' => $formID, 'entryId' => $entryID, 'fieldValues' => $fieldValue];
22 14 $emailTemplateHandler = new EmailTemplateHandler($formID);
23 15 $attachments = [];
24 16 $tempPdfLink = '';
25 - $pdfPassForEmail = '';
26 17 if (!empty($notifyDetails->pdfId) && is_string($notifyDetails->pdfId)) {
27 18 $pdfTemplateID = json_decode($notifyDetails->pdfId)->id;
28 19 $pdfTemplateHandler = new PdfTemplateHandler($formID);
29 20
30 21 $pdfTemplate = $pdfTemplateHandler->getById($pdfTemplateID);
22 + $pdfBody = FieldValueHandler::replaceFieldWithValue($pdfTemplate[0]->body, $fieldValue);
31 23
32 24 $pdfSetting = json_decode($pdfTemplate[0]->setting);
33 - // error_log('PDF Setting before pdfFileName: ' . print_r([$pdfSetting, $fieldValue], true));
34 25
35 26 $path = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'pdf';
36 - // $fileName = 'bit-form-pdf-' . $formID . '-' . $entryID;
27 + $fileName = 'bit-form-pdf-' . $formID . '-' . $entryID;
37 28
38 29 if (!is_dir($path)) {
39 - wp_mkdir_p($path);
30 + mkdir($path, 0777, true);
40 31 }
41 32
42 33 if (class_exists('\BitCode\BitFormPro\Admin\AppSetting\Pdf')) {
43 - $serverPath = Helpers::getFullPathWithEncryptedEntryId($formID, $entryID);
44 - $webPath = Helpers::getWebPathWithEncryptedEntryId($formID, $entryID);
34 + $generatedPdf = Pdf::getInstance()->generator($pdfSetting, $pdfBody, $path, $entryID, 'F');
45 35
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 - }
36 + // $pdfFile = $path . DIRECTORY_SEPARATOR . $fileName . '.pdf';
70 37
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 38 if (!is_wp_error($generatedPdf) && file_exists($generatedPdf)) {
93 39 $attachments[] = $generatedPdf;
94 40 $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 41 } 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 - ]);
42 + Log::debug_log('Error in generating PDF: ' . $generatedPdf->get_error_message());
124 43 }
125 44 }
126 45 }
127 46
@@ -134,64 +53,15 @@
134 53 $from_name = '';
135 54 if (isset($notifyDetails->from_name) && !empty($notifyDetails->from_name)) {
136 55 $from_name = $notifyDetails->from_name;
137 56 }
57 + (new MailConfig())->sendMail(['from_name' => $from_name]);
58 + $mailSubject = FieldValueHandler::replaceFieldWithValue($mailTemplate[0]->sub, $fieldValue);
59 + $mailBody = FieldValueHandler::replaceFieldWithValue($mailTemplate[0]->body, $fieldValue);
60 +
138 61 $mailHeaders = [
139 - // 'Content-Type: text/html; charset=UTF-8',
140 - // $embeddedMailHeader
62 + // "Content-Type: text/html; charset=UTF-8",
141 63 ];
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 - // Signature images: embed inline (cid:) so they render for non-logged-in recipients.
174 - // Must run before changeImagePathInHTMLString so the src is still the raw filename.
175 - $cidMap = [];
176 - $sigBasePath = FileHandler::getEntriesFileUploadDir($formID, $entryID) . DIRECTORY_SEPARATOR;
177 - $mailBody = self::embedSignatureImages($mailBody, $formManager, $fieldValue, $sigBasePath, $cidMap);
178 - $webUrl = Helpers::getWebPathWithEncryptedEntryId($formID, $entryID);
179 - $mailBody = FieldValueHandler::changeImagePathInHTMLString($mailBody, $webUrl);
180 - $mailBody = FieldValueHandler::changeHrefPathInHTMLString($mailBody, $webUrl); // replace anchor tag href with constructed weburl
181 -
182 - // allow developers to modify email body
183 - $mailBody = apply_filters(
184 - 'bitform_filter_email_body',
185 - $mailBody,
186 - [
187 - 'form_id' => $formID,
188 - 'entry_id' => $entryID,
189 - 'field_values' => $fieldValue,
190 - 'template' => $mailTemplate[0] ?? null,
191 - 'is_double_optin' => (bool) $isDblOptin,
192 - ]
193 - );
194 64 if (!empty($notifyDetails->replyto)) {
195 65 $mailReplyTo = FieldValueHandler::validateMailArry($notifyDetails->replyto, $fieldValue);
196 66 if (is_array($mailReplyTo)) {
197 67 foreach ($mailReplyTo as $key => $emailAddress) {
@@ -202,11 +72,11 @@
202 72 }
203 73 }
204 74 $oldMailBody = $mailBody;
205 75 $data = [];
206 - if ($isDblOptin && true === has_filter('bitform_email_body_text')) {
76 + if ($isDblOptin && true === has_filter('bf_email_body_text')) {
207 77 $urlParams = $formID . '_' . $entryID . '_' . $logId;
208 - $data = apply_filters('bitform_email_body_text', $mailBody, $urlParams);
78 + $data = apply_filters('bf_email_body_text', $mailBody, $urlParams);
209 79 $mailBody = $data['mailbody'];
210 80 }
211 81
212 82 if (!empty($notifyDetails->bcc)) {
@@ -228,100 +98,53 @@
228 98 } else {
229 99 $mailHeaders[] = 'Cc: ' . sanitize_email($mailCC);
230 100 }
231 101 }
102 + if (!empty($notifyDetails->from)) {
103 + $mailFrom = FieldValueHandler::validateMailArry($notifyDetails->from, $fieldValue);
104 + $fromName = !empty($notifyDetails->fromName) ? $notifyDetails->fromName : explode('@', $mailFrom[0])[0];
105 + $mailHeaders[] = "FROM: $fromName " . '<' . sanitize_email($mailFrom[0]) . '>';
106 + }
107 +
232 108 if (!empty($notifyDetails->attachment)) {
233 - $fileFldKeys = $notifyDetails->attachment;
234 - $fileBasePath = FileHandler::getEntriesFileUploadDir($formID, $entryID) . DIRECTORY_SEPARATOR;
235 -
236 - // Normalize to array for consistent processing
237 - $fileFldKeys = is_array($fileFldKeys) ? $fileFldKeys : [$fileFldKeys];
238 -
239 - foreach ($fileFldKeys as $fldKey) {
240 - $repeaterFieldKey = $formManager->isRepeatedField($fldKey);
241 -
242 - if ($repeaterFieldKey) {
243 - // Handle repeated file field
244 - FileHandler::processRepeaterAttachment(
245 - $repeaterFieldKey,
246 - $fldKey,
247 - $fieldValue,
248 - $fileBasePath,
249 - $attachments
250 - );
251 - } else {
252 - // Handle regular file field
253 - FileHandler::processRegularAttachment(
254 - $fldKey,
255 - $fieldValue,
256 - $fileBasePath,
257 - $attachments
258 - );
109 + $files = $notifyDetails->attachment;
110 + $fileBasePath = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR;
111 + if (is_array($files)) {
112 + foreach ($files as $file) {
113 + if (isset($fieldValue[$file])) {
114 + if (is_array($fieldValue[$file])) {
115 + foreach ($fieldValue[$file] as $singleFile) {
116 + if (\is_readable("{$fileBasePath}{$singleFile}")) {
117 + $attachments[] = "{$fileBasePath}{$singleFile}";
118 + }
119 + }
120 + } elseif (\is_readable("{$fileBasePath}{$fieldValue[$file]}")) {
121 + $attachments[] = "{$fileBasePath}{$fieldValue[$file]}";
122 + }
123 + }
259 124 }
125 + } elseif (isset($fieldValue[$files])) {
126 + if (is_array($fieldValue[$files])) {
127 + foreach ($fieldValue[$files] as $singleFile) {
128 + if (\is_readable("{$fileBasePath}{$singleFile}")) {
129 + $attachments[] = "{$fileBasePath}{$singleFile}";
130 + }
131 + }
132 + } elseif (\is_readable("{$fileBasePath}{$fieldValue[$files]}")) {
133 + $attachments[] = "{$fileBasePath}{$fieldValue[$files]}";
134 + }
260 135 }
261 136 }
262 137 $mailBody = stripcslashes($mailBody);
263 138 $mailSubject = stripcslashes($mailSubject);
264 - $embedCb = static function ($phpmailer) use ($cidMap) {
265 - foreach ($cidMap as $cid => $info) {
266 - try {
267 - $phpmailer->addEmbeddedImage($info['path'], $cid, $info['name']);
268 - } catch (\Throwable $e) {
269 - Log::debug_log("[Signature Embed] failed for {$info['path']} - " . $e->getMessage());
270 - }
271 - }
272 - };
273 - if (!empty($cidMap)) {
274 - add_action('phpmailer_init', $embedCb);
275 - }
276 139 add_filter('wp_mail_content_type', [self::class, 'filterMailContentType']);
277 140 $status = wp_mail($mailTo, $mailSubject, $mailBody, $mailHeaders, $attachments);
278 -
279 141 if (!$status) {
280 - Log::debug_log([
281 - 'status' => 'error',
282 - 'code' => 'mail_not_sent',
283 - 'message' => 'Mail not sent',
284 - 'inputDetails' => [
285 - 'to' => $mailTo,
286 - 'subject' => $mailSubject,
287 - 'body' => $mailBody,
288 - 'headers' => $mailHeaders,
289 - 'attachments' => $attachments,
290 - 'notifyDetails' => $notifyDetails,
291 - 'formID' => $formID,
292 - 'entryID' => $entryID,
293 - 'isDblOptin' => $isDblOptin,
294 - 'logId' => $logId
295 - ],
296 - 'responseDetails' => $status
297 - ]);
298 - $apiResponse->apiResponse($logId, '', ['type' => 'record', 'type_name' => 'smtp'], 'errors', 'Mail dose not send successfully', $entryDetails);
299 - } else {
300 - Log::debug_log([
301 - 'status' => 'success',
302 - 'code' => 'mail_sent',
303 - 'message' => 'Mail successfully sent',
304 - 'inputDetails' => [
305 - 'to' => $mailTo,
306 - 'subject' => $mailSubject,
307 - 'body' => $mailBody,
308 - 'headers' => $mailHeaders,
309 - 'attachments' => $attachments,
310 - 'notifyDetails' => $notifyDetails,
311 - 'formID' => $formID,
312 - 'entryID' => $entryID,
313 - 'isDblOptin' => $isDblOptin,
314 - 'logId' => $logId
315 - ],
316 - 'responseDetails' => $status
317 - ]);
318 - $apiResponse->apiResponse($logId, '', ['type' => 'record', 'type_name' => 'smtp'], 'success', 'Mail successfully send.', $entryDetails);
142 + $status = wp_mail($mailTo, $mailSubject, $mailBody, $mailHeaders);
319 143 }
320 144 if ($status && $isDblOptin && false !== strpos($oldMailBody, 'entry_confirmation_url')) {
321 145 $entryMeta = new FormEntryMetaModel();
322 - $apiResponse->apiResponse($logId, '', ['type' => 'record', 'type_name' => 'smtp'], 'success', 'Mail successfully send.', $entryDetails);
323 - // Form entry meta insert; meta_key/meta_value required to store dynamic field data per entry.
146 +
324 147 $entryMeta->insert(
325 148 [
326 149 'bitforms_form_entry_id' => $entryID,
327 150 'meta_key' => 'entry_confirm_activation',
@@ -329,11 +152,8 @@
329 152 ]
330 153 );
331 154 }
332 155 remove_filter('wp_mail_content_type', [self::class, 'filterMailContentType']);
333 - if (!empty($cidMap)) {
334 - remove_action('phpmailer_init', $embedCb);
335 - }
336 156 }
337 157 }
338 158 }
339 159
@@ -344,66 +164,6 @@
344 164
345 165 public static function filterMailContentType()
346 166 {
347 167 return 'text/html; charset=UTF-8';
348 - }
349 -
350 - /**
351 - * Rewrite signature <img> tags to inline cid: references and collect the files
352 - * to embed. Only signature-field images that are local & readable are embedded;
353 - * external URLs and non-signature images are left untouched. When the form has
354 - * no signature (or none is in the body) $cidMap stays empty and nothing changes.
355 - */
356 - private static function embedSignatureImages($html, $formManager, $fieldValue, $baseDir, &$cidMap)
357 - {
358 - if (empty($html)) {
359 - return $html;
360 - }
361 -
362 - // Collect signature filenames for this entry.
363 - $sigFiles = [];
364 - foreach ($formManager->getFields() as $key => $detail) {
365 - if (!isset($detail['type']) || 'signature' !== $detail['type']) {
366 - continue;
367 - }
368 - $val = isset($fieldValue[$key]) ? $fieldValue[$key] : '';
369 - foreach ((array) $val as $fn) {
370 - $fn = is_string($fn) ? trim($fn) : '';
371 - if ('' !== $fn && 'signature-failed.png' !== $fn) {
372 - $sigFiles[basename($fn)] = true;
373 - }
374 - }
375 - }
376 - if (empty($sigFiles)) {
377 - return $html;
378 - }
379 -
380 - return preg_replace_callback(
381 - '/<img\s+[^>]*src=[\'"]([^\'"]+)[\'"][^>]*>/i',
382 - function ($m) use ($baseDir, $sigFiles, &$cidMap) {
383 - $src = $m[1];
384 - if (filter_var($src, FILTER_VALIDATE_URL)) {
385 - return $m[0]; // external URL, leave as-is
386 - }
387 - $name = basename($src);
388 - if (!isset($sigFiles[$name])) {
389 - return $m[0]; // not a signature image
390 - }
391 - $file = $baseDir . $name;
392 - if (!is_readable($file)) {
393 - return $m[0];
394 - }
395 - $cid = 'bfsig_' . md5($file);
396 - $cidMap[$cid] = ['path' => $file, 'name' => $name];
397 -
398 - // Replace only the src attribute value (leave alt untouched).
399 - return preg_replace(
400 - '/(src=[\'"])' . preg_quote($src, '/') . '([\'"])/i',
401 - '${1}cid:' . $cid . '${2}',
402 - $m[0],
403 - 1
404 - );
405 - },
406 - $html
407 - );
408 168 }
409 169 }