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
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.15.3, at includes/Core/Util/MailNotifier.php

170 lines 7.1 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 } else {
42 Log::debug_log('Error in generating PDF: ' . $generatedPdf->get_error_message());
43 }
44 }
45 }
46
47 if (is_string($notifyDetails->id)) {
48 $mailTemplateID = json_decode($notifyDetails->id)->id;
49 $mailTemplate = $emailTemplateHandler->getATemplate($mailTemplateID);
50 if (!is_wp_error($mailTemplate)) {
51 $mailTo = FieldValueHandler::validateMailArry($notifyDetails->to, $fieldValue);
52 if (!empty($mailTo)) {
53 $from_name = '';
54 if (isset($notifyDetails->from_name) && !empty($notifyDetails->from_name)) {
55 $from_name = $notifyDetails->from_name;
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
61 $mailHeaders = [
62 // "Content-Type: text/html; charset=UTF-8",
63 ];
64 if (!empty($notifyDetails->replyto)) {
65 $mailReplyTo = FieldValueHandler::validateMailArry($notifyDetails->replyto, $fieldValue);
66 if (is_array($mailReplyTo)) {
67 foreach ($mailReplyTo as $key => $emailAddress) {
68 $mailHeaders[] = 'Reply-To: ' . explode('@', $emailAddress)[0] . '<' . sanitize_email($emailAddress) . '>';
69 }
70 } else {
71 $mailHeaders[] = 'Reply-To: ' . explode('@', $mailReplyTo)[0] . '<' . sanitize_email($mailReplyTo) . '>';
72 }
73 }
74 $oldMailBody = $mailBody;
75 $data = [];
76 if ($isDblOptin && true === has_filter('bf_email_body_text')) {
77 $urlParams = $formID . '_' . $entryID . '_' . $logId;
78 $data = apply_filters('bf_email_body_text', $mailBody, $urlParams);
79 $mailBody = $data['mailbody'];
80 }
81
82 if (!empty($notifyDetails->bcc)) {
83 $mailBCC = FieldValueHandler::validateMailArry($notifyDetails->bcc, $fieldValue);
84 if (is_array($mailBCC)) {
85 foreach ($mailBCC as $key => $emailAddress) {
86 $mailHeaders[] = 'Bcc: ' . sanitize_email($emailAddress);
87 }
88 } else {
89 $mailHeaders[] = 'Bcc: ' . sanitize_email($mailBCC);
90 }
91 }
92 if (!empty($notifyDetails->cc)) {
93 $mailCC = FieldValueHandler::validateMailArry($notifyDetails->cc, $fieldValue);
94 if (is_array($mailCC)) {
95 foreach ($mailCC as $key => $emailAddress) {
96 $mailHeaders[] = 'Cc: ' . sanitize_email($emailAddress);
97 }
98 } else {
99 $mailHeaders[] = 'Cc: ' . sanitize_email($mailCC);
100 }
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
108 if (!empty($notifyDetails->attachment)) {
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 }
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 }
135 }
136 }
137 $mailBody = stripcslashes($mailBody);
138 $mailSubject = stripcslashes($mailSubject);
139 add_filter('wp_mail_content_type', [self::class, 'filterMailContentType']);
140 $status = wp_mail($mailTo, $mailSubject, $mailBody, $mailHeaders, $attachments);
141 if (!$status) {
142 $status = wp_mail($mailTo, $mailSubject, $mailBody, $mailHeaders);
143 }
144 if ($status && $isDblOptin && false !== strpos($oldMailBody, 'entry_confirmation_url')) {
145 $entryMeta = new FormEntryMetaModel();
146
147 $entryMeta->insert(
148 [
149 'bitforms_form_entry_id' => $entryID,
150 'meta_key' => 'entry_confirm_activation',
151 'meta_value' => $data['token']
152 ]
153 );
154 }
155 remove_filter('wp_mail_content_type', [self::class, 'filterMailContentType']);
156 }
157 }
158 }
159
160 if (!empty($tempPdfLink)) {
161 wp_delete_file($tempPdfLink);
162 }
163 }
164
165 public static function filterMailContentType()
166 {
167 return 'text/html; charset=UTF-8';
168 }
169 }
170