| @@ -9,9 +9,8 @@ | ||
| 9 | 9 | use BitCode\BitForm\Admin\Form\Helpers; |
| 10 | 10 | use BitCode\BitForm\Core\Database\FormEntryMetaModel; |
| 11 | 11 | use BitCode\BitForm\Core\Form\FormManager; |
| 12 | 12 | use BitCode\BitForm\Core\Messages\EmailTemplateHandler; |
| 13 | -use BitCode\BitForm\Core\Messages\PdfTemplateHandler; | |
| 14 | 13 | |
| 15 | 14 | final class MailNotifier |
| 16 | 15 | { |
| 17 | 16 | public static function notify($notifyDetails, $formID, $fieldValue, $entryID, $isDblOptin = false, $logId = '') |
| @@ -20,117 +19,20 @@ | ||
| 20 | 19 | $formManager = FormManager::getInstance($formID); |
| 21 | 20 | $entryDetails = ['formId' => $formID, 'entryId' => $entryID, 'fieldValues' => $fieldValue]; |
| 22 | 21 | $emailTemplateHandler = new EmailTemplateHandler($formID); |
| 23 | 22 | $attachments = []; |
| 24 | - $tempPdfLink = ''; | |
| 25 | - $pdfPassForEmail = ''; | |
| 26 | - if (!empty($notifyDetails->pdfId) && is_string($notifyDetails->pdfId)) { | |
| 27 | - $pdfTemplateID = json_decode($notifyDetails->pdfId)->id; | |
| 28 | - $pdfTemplateHandler = new PdfTemplateHandler($formID); | |
| 29 | - | |
| 30 | - $pdfTemplate = $pdfTemplateHandler->getById($pdfTemplateID); | |
| 31 | - | |
| 32 | - $pdfSetting = json_decode($pdfTemplate[0]->setting); | |
| 33 | - // error_log('PDF Setting before pdfFileName: ' . print_r([$pdfSetting, $fieldValue], true)); | |
| 34 | - | |
| 35 | - $path = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'pdf'; | |
| 36 | - // $fileName = 'bit-form-pdf-' . $formID . '-' . $entryID; | |
| 37 | - | |
| 38 | - if (!is_dir($path)) { | |
| 39 | - wp_mkdir_p($path); | |
| 40 | - } | |
| 41 | - | |
| 42 | - if (class_exists('\BitCode\BitFormPro\Admin\AppSetting\Pdf')) { | |
| 43 | - $serverPath = Helpers::getFullPathWithEncryptedEntryId($formID, $entryID); | |
| 44 | - $webPath = Helpers::getWebPathWithEncryptedEntryId($formID, $entryID); | |
| 45 | - | |
| 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 | - } | |
| 70 | - | |
| 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 | - if (!is_wp_error($generatedPdf) && file_exists($generatedPdf)) { | |
| 93 | - $attachments[] = $generatedPdf; | |
| 94 | - $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 | - } 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 | - ]); | |
| 124 | - } | |
| 125 | - } | |
| 126 | - } | |
| 127 | - | |
| 23 | + $tempPdfLinks = []; | |
| 128 | 24 | if (is_string($notifyDetails->id)) { |
| 129 | - $mailTemplateID = json_decode($notifyDetails->id)->id; | |
| 25 | + $mailTemplateID = Utilities::jsonObj($notifyDetails->id)->id ?? null; | |
| 130 | 26 | $mailTemplate = $emailTemplateHandler->getATemplate($mailTemplateID); |
| 131 | 27 | if (!is_wp_error($mailTemplate)) { |
| 28 | + // Honor template enable/disable: a disabled email template is never sent. | |
| 29 | + if (isset($mailTemplate[0]->status) && empty($mailTemplate[0]->status)) { | |
| 30 | + return; | |
| 31 | + } | |
| 132 | 32 | $mailTo = FieldValueHandler::validateMailArry($notifyDetails->to, $fieldValue); |
| 33 | + // Conditional email routing (Pro): add recipients resolved from value-based rules in the template config. | |
| 34 | + $mailTo = apply_filters('bitform_email_conditional_routing', $mailTo, $mailTemplate[0], $fieldValue, $formID); | |
| 133 | 35 | if (!empty($mailTo)) { |
| 134 | 36 | $from_name = ''; |
| 135 | 37 | if (isset($notifyDetails->from_name) && !empty($notifyDetails->from_name)) { |
| 136 | 38 | $from_name = $notifyDetails->from_name; |
| @@ -141,14 +43,22 @@ | ||
| 141 | 43 | ]; |
| 142 | 44 | $from_mail = ''; |
| 143 | 45 | if (!empty($notifyDetails->from)) { |
| 144 | 46 | $fromMail = FieldValueHandler::validateMailArry($notifyDetails->from, $fieldValue); |
| 145 | - $headerFromName = !empty($notifyDetails->fromName) ? $notifyDetails->fromName : explode('@', $fromMail[0])[0]; | |
| 47 | + $headerFromName = !empty($notifyDetails->from_name) ? $notifyDetails->from_name : explode('@', $fromMail[0])[0]; | |
| 146 | 48 | $mailHeaders[] = "FROM: $headerFromName " . '<' . sanitize_email($fromMail[0]) . '>'; |
| 147 | 49 | $from_mail = $fromMail[0]; |
| 148 | 50 | } |
| 149 | 51 | (new MailConfig())->sendMail(['from_name' => $from_name, 'from_email' => $from_mail]); |
| 150 | - $mailSubject = FieldValueHandler::replaceFieldWithValue($mailTemplate[0]->sub, $fieldValue, $formID); | |
| 52 | + // Translate before smart-tag replacement. One language per submission, | |
| 53 | + // shared by the admin and submitter emails. | |
| 54 | + $mailSubjectTemplate = (string) apply_filters( | |
| 55 | + 'bitform_translate_form_string', | |
| 56 | + (string) $mailTemplate[0]->sub, | |
| 57 | + 'mail-sub-' . $mailTemplateID, | |
| 58 | + $formID | |
| 59 | + ); | |
| 60 | + $mailSubject = FieldValueHandler::replaceFieldWithValue($mailSubjectTemplate, $fieldValue, $formID); | |
| 151 | 61 | |
| 152 | 62 | // allow developers to modify email subject |
| 153 | 63 | $mailSubject = apply_filters( |
| 154 | 64 | 'bitform_filter_email_subject', |
| @@ -161,9 +71,14 @@ | ||
| 161 | 71 | 'is_double_optin' => (bool) $isDblOptin, |
| 162 | 72 | ] |
| 163 | 73 | ); |
| 164 | 74 | |
| 165 | - $mailBody = $mailTemplate[0]->body; | |
| 75 | + $mailBody = (string) apply_filters( | |
| 76 | + 'bitform_translate_form_string', | |
| 77 | + (string) $mailTemplate[0]->body, | |
| 78 | + 'mail-body-' . $mailTemplateID, | |
| 79 | + $formID | |
| 80 | + ); | |
| 166 | 81 | if (class_exists('\BitCode\BitFormPro\Admin\DownloadFile')) { |
| 167 | 82 | $downloadFile = new \BitCode\BitFormPro\Admin\DownloadFile(); |
| 168 | 83 | $mailBody = $downloadFile->replacePdfShortCodeToLink($mailBody, $formID, $entryID); |
| 169 | 84 | $mailBody = $downloadFile->replaceShortCodeToPdfPassword($mailBody, $formID, $entryID); |
| @@ -228,8 +143,19 @@ | ||
| 228 | 143 | } else { |
| 229 | 144 | $mailHeaders[] = 'Cc: ' . sanitize_email($mailCC); |
| 230 | 145 | } |
| 231 | 146 | } |
| 147 | + // PDF generation is a Pro feature; Pro hooks this filter and returns the generated | |
| 148 | + // file path(s) — an array (one per configured template), or a single path string | |
| 149 | + // from Pro versions that predate multi-PDF. Generated here (not before the | |
| 150 | + // template/recipient checks) so disabled templates and empty recipients never | |
| 151 | + // generate PDFs — and never leak temp files. | |
| 152 | + $tempPdfLinks = apply_filters('bitform_generate_pdf_attachment', [], $notifyDetails, $entryDetails, $logId); | |
| 153 | + $tempPdfLinks = is_array($tempPdfLinks) ? $tempPdfLinks : [$tempPdfLinks]; | |
| 154 | + $tempPdfLinks = array_values(array_unique(array_filter($tempPdfLinks, static function ($pdfPath) { | |
| 155 | + return !empty($pdfPath) && is_string($pdfPath) && file_exists($pdfPath); | |
| 156 | + }))); | |
| 157 | + $attachments = array_merge($attachments, $tempPdfLinks); | |
| 232 | 158 | if (!empty($notifyDetails->attachment)) { |
| 233 | 159 | $fileFldKeys = $notifyDetails->attachment; |
| 234 | 160 | $fileBasePath = FileHandler::getEntriesFileUploadDir($formID, $entryID) . DIRECTORY_SEPARATOR; |
| 235 | 161 | |
| @@ -258,8 +184,18 @@ | ||
| 258 | 184 | ); |
| 259 | 185 | } |
| 260 | 186 | } |
| 261 | 187 | } |
| 188 | + // WordPress Media Library attachments (stored as attachment IDs in the template config). | |
| 189 | + if (!empty($notifyDetails->mediaAttachment)) { | |
| 190 | + $mediaIds = is_array($notifyDetails->mediaAttachment) ? $notifyDetails->mediaAttachment : [$notifyDetails->mediaAttachment]; | |
| 191 | + foreach ($mediaIds as $mediaId) { | |
| 192 | + $mediaPath = get_attached_file(absint($mediaId)); | |
| 193 | + if ($mediaPath && file_exists($mediaPath)) { | |
| 194 | + $attachments[] = $mediaPath; | |
| 195 | + } | |
| 196 | + } | |
| 197 | + } | |
| 262 | 198 | $mailBody = stripcslashes($mailBody); |
| 263 | 199 | $mailSubject = stripcslashes($mailSubject); |
| 264 | 200 | $embedCb = static function ($phpmailer) use ($cidMap) { |
| 265 | 201 | foreach ($cidMap as $cid => $info) { |
| @@ -273,8 +209,15 @@ | ||
| 273 | 209 | if (!empty($cidMap)) { |
| 274 | 210 | add_action('phpmailer_init', $embedCb); |
| 275 | 211 | } |
| 276 | 212 | add_filter('wp_mail_content_type', [self::class, 'filterMailContentType']); |
| 213 | + $fromNameCb = null; | |
| 214 | + if (!empty($from_name)) { | |
| 215 | + $fromNameCb = static function () use ($from_name) { | |
| 216 | + return $from_name; | |
| 217 | + }; | |
| 218 | + add_filter('wp_mail_from_name', $fromNameCb); | |
| 219 | + } | |
| 277 | 220 | $status = wp_mail($mailTo, $mailSubject, $mailBody, $mailHeaders, $attachments); |
| 278 | 221 | |
| 279 | 222 | if (!$status) { |
| 280 | 223 | Log::debug_log([ |
| @@ -329,8 +272,11 @@ | ||
| 329 | 272 | ] |
| 330 | 273 | ); |
| 331 | 274 | } |
| 332 | 275 | remove_filter('wp_mail_content_type', [self::class, 'filterMailContentType']); |
| 276 | + if (null !== $fromNameCb) { | |
| 277 | + remove_filter('wp_mail_from_name', $fromNameCb); | |
| 278 | + } | |
| 333 | 279 | if (!empty($cidMap)) { |
| 334 | 280 | remove_action('phpmailer_init', $embedCb); |
| 335 | 281 | } |
| 336 | 282 | } |
| @@ -336,9 +282,9 @@ | ||
| 336 | 282 | } |
| 337 | 283 | } |
| 338 | 284 | } |
| 339 | 285 | |
| 340 | - if (!empty($tempPdfLink)) { | |
| 286 | + foreach ($tempPdfLinks as $tempPdfLink) { | |
| 341 | 287 | wp_delete_file($tempPdfLink); |
| 342 | 288 | } |
| 343 | 289 | } |
| 344 | 290 | |