| 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 = (new Pdf())->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 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
if (is_string($notifyDetails->id)) { |
| 46 |
$mailTemplateID = json_decode($notifyDetails->id)->id; |
| 47 |
$mailTemplate = $emailTemplateHandler->getATemplate($mailTemplateID); |
| 48 |
if (!is_wp_error($mailTemplate)) { |
| 49 |
$mailTo = FieldValueHandler::validateMailArry($notifyDetails->to, $fieldValue); |
| 50 |
if (!empty($mailTo)) { |
| 51 |
(new MailConfig())->sendMail(); |
| 52 |
$mailSubject = FieldValueHandler::replaceFieldWithValue($mailTemplate[0]->sub, $fieldValue); |
| 53 |
$mailBody = FieldValueHandler::replaceFieldWithValue($mailTemplate[0]->body, $fieldValue); |
| 54 |
|
| 55 |
$mailHeaders = [ |
| 56 |
// "Content-Type: text/html; charset=UTF-8", |
| 57 |
]; |
| 58 |
if (!empty($notifyDetails->replyto)) { |
| 59 |
$mailReplyTo = FieldValueHandler::validateMailArry($notifyDetails->replyto, $fieldValue); |
| 60 |
if (is_array($mailReplyTo)) { |
| 61 |
foreach ($mailReplyTo as $key => $emailAddress) { |
| 62 |
$mailHeaders[] = 'Reply-To: ' . explode('@', $emailAddress)[0] . '<' . sanitize_email($emailAddress) . '>'; |
| 63 |
} |
| 64 |
} else { |
| 65 |
$mailHeaders[] = 'Reply-To: ' . explode('@', $mailReplyTo)[0] . '<' . sanitize_email($mailReplyTo) . '>'; |
| 66 |
} |
| 67 |
} |
| 68 |
$oldMailBody = $mailBody; |
| 69 |
$data = []; |
| 70 |
if ($isDblOptin && true === has_filter('bf_email_body_text')) { |
| 71 |
$urlParams = $formID . '_' . $entryID . '_' . $logId; |
| 72 |
$data = apply_filters('bf_email_body_text', $mailBody, $urlParams); |
| 73 |
$mailBody = $data['mailbody']; |
| 74 |
} |
| 75 |
|
| 76 |
if (!empty($notifyDetails->bcc)) { |
| 77 |
$mailBCC = FieldValueHandler::validateMailArry($notifyDetails->bcc, $fieldValue); |
| 78 |
if (is_array($mailBCC)) { |
| 79 |
foreach ($mailBCC as $key => $emailAddress) { |
| 80 |
$mailHeaders[] = 'Bcc: ' . sanitize_email($emailAddress); |
| 81 |
} |
| 82 |
} else { |
| 83 |
$mailHeaders[] = 'Bcc: ' . sanitize_email($mailBCC); |
| 84 |
} |
| 85 |
} |
| 86 |
if (!empty($notifyDetails->cc)) { |
| 87 |
$mailCC = FieldValueHandler::validateMailArry($notifyDetails->cc, $fieldValue); |
| 88 |
if (is_array($mailCC)) { |
| 89 |
foreach ($mailCC as $key => $emailAddress) { |
| 90 |
$mailHeaders[] = 'Cc: ' . sanitize_email($emailAddress); |
| 91 |
} |
| 92 |
} else { |
| 93 |
$mailHeaders[] = 'Cc: ' . sanitize_email($mailCC); |
| 94 |
} |
| 95 |
} |
| 96 |
if (!empty($notifyDetails->from)) { |
| 97 |
$mailFrom = FieldValueHandler::validateMailArry($notifyDetails->from, $fieldValue); |
| 98 |
$fromName = !empty($notifyDetails->fromName) ? $notifyDetails->fromName : explode('@', $mailFrom[0])[0]; |
| 99 |
$mailHeaders[] = "FROM: $fromName " . '<' . sanitize_email($mailFrom[0]) . '>'; |
| 100 |
} |
| 101 |
|
| 102 |
if (!empty($notifyDetails->attachment)) { |
| 103 |
$files = $notifyDetails->attachment; |
| 104 |
$fileBasePath = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR; |
| 105 |
if (is_array($files)) { |
| 106 |
foreach ($files as $file) { |
| 107 |
if (isset($fieldValue[$file])) { |
| 108 |
if (is_array($fieldValue[$file])) { |
| 109 |
foreach ($fieldValue[$file] as $singleFile) { |
| 110 |
if (\is_readable("{$fileBasePath}{$singleFile}")) { |
| 111 |
$attachments[] = "{$fileBasePath}{$singleFile}"; |
| 112 |
} |
| 113 |
} |
| 114 |
} elseif (\is_readable("{$fileBasePath}{$fieldValue[$file]}")) { |
| 115 |
$attachments[] = "{$fileBasePath}{$fieldValue[$file]}"; |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
} elseif (isset($fieldValue[$files])) { |
| 120 |
if (is_array($fieldValue[$files])) { |
| 121 |
foreach ($fieldValue[$files] as $singleFile) { |
| 122 |
if (\is_readable("{$fileBasePath}{$singleFile}")) { |
| 123 |
$attachments[] = "{$fileBasePath}{$singleFile}"; |
| 124 |
} |
| 125 |
} |
| 126 |
} elseif (\is_readable("{$fileBasePath}{$fieldValue[$files]}")) { |
| 127 |
$attachments[] = "{$fileBasePath}{$fieldValue[$files]}"; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
$mailBody = stripcslashes($mailBody); |
| 132 |
$mailSubject = stripcslashes($mailSubject); |
| 133 |
add_filter('wp_mail_content_type', [self::class, 'filterMailContentType']); |
| 134 |
$status = wp_mail($mailTo, $mailSubject, $mailBody, $mailHeaders, $attachments); |
| 135 |
if (!$status) { |
| 136 |
$status = wp_mail($mailTo, $mailSubject, $mailBody, $mailHeaders); |
| 137 |
} |
| 138 |
if ($status && $isDblOptin && false !== strpos($oldMailBody, 'entry_confirmation_url')) { |
| 139 |
$entryMeta = new FormEntryMetaModel(); |
| 140 |
|
| 141 |
$entryMeta->insert( |
| 142 |
[ |
| 143 |
'bitforms_form_entry_id' => $entryID, |
| 144 |
'meta_key' => 'entry_confirm_activation', |
| 145 |
'meta_value' => $data['token'] |
| 146 |
] |
| 147 |
); |
| 148 |
} |
| 149 |
remove_filter('wp_mail_content_type', [self::class, 'filterMailContentType']); |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
if (!empty($tempPdfLink)) { |
| 155 |
unlink($tempPdfLink); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
public static function filterMailContentType() |
| 160 |
{ |
| 161 |
return 'text/html; charset=UTF-8'; |
| 162 |
} |
| 163 |
} |
| 164 |
|