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 +152 -92 1.5.22.15.3 View file →
@@ -1,109 +1,169 @@
1 1 <?php
2 2
3 3 namespace BitCode\BitForm\Core\Util;
4 4
5 -use BitCode\BitForm\Core\Util\MailConfig;
6 -use BitCode\BitForm\Core\Util\FieldValueHandler;
5 +use BitCode\BitForm\Core\Database\FormEntryMetaModel;
7 6 use BitCode\BitForm\Core\Messages\EmailTemplateHandler;
7 +use BitCode\BitForm\Core\Messages\PdfTemplateHandler;
8 +use BitCode\BitFormPro\Admin\AppSetting\Pdf;
8 9
9 10 final class MailNotifier
10 11 {
11 - public static function notify($notifyDetails, $formID, $fieldValue, $entryID)
12 - {
13 - $emailTemplateHandler = new EmailTemplateHandler($formID);
14 - if (is_string($notifyDetails->id)) {
15 - $mailTemplateID = json_decode($notifyDetails->id)->id;
16 - $mailTemplate = $emailTemplateHandler->getATemplate($mailTemplateID);
17 - if (!is_wp_error($mailTemplate)) {
18 - $mailTo = FieldValueHandler::validateMailArry($notifyDetails->to, $fieldValue);
19 - if (!empty($mailTo)) {
20 - (new MailConfig())->sendMail();
21 - $mailSubject = FieldValueHandler::replaceFieldWithValue($mailTemplate[0]->sub, $fieldValue);
22 - $mailBody = FieldValueHandler::replaceFieldWithValue($mailTemplate[0]->body, $fieldValue);
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);
23 20
24 - $mailHeaders = array(
25 - // "Content-Type: text/html; charset=UTF-8",
26 - );
27 - if (!empty($notifyDetails->replyto)) {
28 - $mailReplyTo = FieldValueHandler::validateMailArry($notifyDetails->replyto, $fieldValue);
29 - if (is_array($mailReplyTo)) {
30 - foreach ($mailReplyTo as $key => $emailAddress) {
31 - $mailHeaders[] = "Reply-To: " . explode('@', $emailAddress)[0] . "<" . sanitize_email($emailAddress) . ">";
32 - }
33 - } else {
34 - $mailHeaders[] = "Reply-To: " . explode('@', $mailReplyTo)[0] . "<" . sanitize_email($mailReplyTo) . ">";
35 - }
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 + }
36 119 }
37 - if (!empty($notifyDetails->bcc)) {
38 - $mailBCC = FieldValueHandler::validateMailArry($notifyDetails->bcc, $fieldValue);
39 - if (is_array($mailBCC)) {
40 - foreach ($mailBCC as $key => $emailAddress) {
41 - $mailHeaders[] = "Bcc: " . sanitize_email($emailAddress);
42 - }
43 - } else {
44 - $mailHeaders[] = "Bcc: " . sanitize_email($mailBCC);
45 - }
46 - }
47 - if (!empty($notifyDetails->cc)) {
48 - $mailCC = FieldValueHandler::validateMailArry($notifyDetails->cc, $fieldValue);
49 - if (is_array($mailCC)) {
50 - foreach ($mailCC as $key => $emailAddress) {
51 - $mailHeaders[] = "Cc: " . sanitize_email($emailAddress);
52 - }
53 - } else {
54 - $mailHeaders[] = "Cc: " . sanitize_email($mailCC);
55 - }
56 - }
57 - if (!empty($notifyDetails->from)) {
58 - $mailFrom = FieldValueHandler::validateMailArry($notifyDetails->from, $fieldValue);
59 - $fromName = !empty($notifyDetails->fromName) ? $notifyDetails->fromName : explode('@', $mailFrom[0])[0];
60 - $mailHeaders[] = "FROM: $fromName " . "<" . sanitize_email($mailFrom[0]) . ">";
61 - }
62 - $attachments = [];
63 - if (!empty($notifyDetails->attachment)) {
64 - $files = $notifyDetails->attachment;
65 - $fileBasePath = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR;
66 - if (is_array($files)) {
67 - foreach ($files as $file) {
68 - if (isset($fieldValue[$file])) {
69 - if (is_array($fieldValue[$file])) {
70 - foreach ($fieldValue[$file] as $singleFile) {
71 - if (\is_readable("{$fileBasePath}{$singleFile}")) {
72 - $attachments[] = "{$fileBasePath}{$singleFile}";
73 - }
74 - }
75 - } elseif (\is_readable("{$fileBasePath}{$fieldValue[$file]}")) {
76 - $attachments[] = "{$fileBasePath}{$fieldValue[$file]}";
77 - }
78 - }
79 - }
80 - } else if (isset($fieldValue[$files])) {
81 - if (is_array($fieldValue[$files])) {
82 - foreach ($fieldValue[$files] as $singleFile) {
83 - if (\is_readable("{$fileBasePath}{$singleFile}")) {
84 - $attachments[] = "{$fileBasePath}{$singleFile}";
85 - }
86 - }
87 - } elseif (\is_readable("{$fileBasePath}{$fieldValue[$files]}")) {
88 - $attachments[] = "{$fileBasePath}{$fieldValue[$files]}";
89 - }
90 - }
91 - }
92 - $mailBody = stripcslashes($mailBody);
93 - $mailSubject = stripcslashes($mailSubject);
94 - add_filter('wp_mail_content_type', [self::class, 'filterMailContentType']);
95 - $status = wp_mail($mailTo, $mailSubject, $mailBody, $mailHeaders, $attachments);
96 - if (!$status) {
97 - wp_mail($mailTo, $mailSubject, $mailBody, $mailHeaders);
98 - }
99 - remove_filter('wp_mail_content_type', [self::class, 'filterMailContentType']);
120 + } elseif (\is_readable("{$fileBasePath}{$fieldValue[$file]}")) {
121 + $attachments[] = "{$fileBasePath}{$fieldValue[$file]}";
122 + }
100 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 + }
101 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']);
102 156 }
157 + }
103 158 }
104 159
105 - public static function filterMailContentType()
106 - {
107 - return 'text/html; charset=UTF-8';
160 + if (!empty($tempPdfLink)) {
161 + wp_delete_file($tempPdfLink);
108 162 }
163 + }
164 +
165 + public static function filterMailContentType()
166 + {
167 + return 'text/html; charset=UTF-8';
168 + }
109 169 }