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