loader=Loader::$Instance; } public function GetEmailList(){ $repository=new EmailRepository(Loader::$Instance); return $repository->GetEmailList(); } /** * @param $EmailId * @param $order \WC_Order * @return bool */ public function SendEmail($EmailId, $order,$logMessage='',$logAddedByUser=false) { $orderId = $order != null ? $order->get_id() : 'null'; LogManager::LogDebug("[SendEmail] Starting send for email template #$EmailId, order #$orderId"); $repository=new EmailRepository($this->loader); $emailOptions=$repository->GetEmailOptions($EmailId); if($emailOptions==null||$order==null) { LogManager::LogDebug("[SendEmail] Aborted - ".($emailOptions==null?'email template not found':'order is null')); return false; } LogManager::LogDebug("[SendEmail] Loaded email template '".$emailOptions->Name."' (ID #$EmailId)"); $result=false; if($emailOptions->ContentOptions->SplitOrder) { LogManager::LogDebug("[SendEmail] Split order mode enabled, sending separate email per line item"); $splitOrder=new SplittedOrder($order->get_id()); $items = $splitOrder->GetAllItems(); LogManager::LogDebug("[SendEmail] Found ".count($items)." line item(s) to split"); foreach ($items as $currentItem) { $splitOrder->ClearShowedItems(); $splitOrder->ShowItem($currentItem->get_id()); LogManager::LogDebug("[SendEmail] Sending split email for line item #".$currentItem->get_id()); $result= $this->ExecuteSendingEmail($emailOptions,$splitOrder); if(!$result) { LogManager::LogError("[SendEmail] Split email failed for line item #".$currentItem->get_id().", aborting remaining items"); return false; } } }else{ $result= $this->ExecuteSendingEmail($emailOptions,$order); } if($result&&$logMessage!='') { $order->add_order_note(str_replace('[email_name]',$emailOptions->Name,$logMessage), false, $logAddedByUser); } LogManager::LogDebug("[SendEmail] Finished send for email template #$EmailId, order #$orderId - result: ".($result?'SUCCESS':'FAILED')); return $result; } public function RenderPreview($templateId,$orderId=null) { $emailRepository=new EmailRepository($this->loader); $options=$emailRepository->GetEmailOptions($templateId); if($orderId!=null) { $order=wc_get_order($orderId); if($order==null) throw new \Exception('Order not found'); $retriever=new OrderRetriever($this->loader,$order); }else $retriever=new TestOrderRetriever($this->loader); $emailRenderer=new EmailRenderer($this->loader,$options,$retriever); $emailRenderer->Initialize(); echo $emailRenderer->Render(); } public function GetEmailRenderedByTemplateId($emailId,$order) { $repository=new EmailRepository($this->loader); $emailOptions=$repository->GetEmailOptions($emailId); if($emailOptions==null||$order==null) return null; $retriever=new OrderRetriever($this->loader,$order); $emailRenderer= new EmailRenderer($this->loader,$emailOptions,$retriever); $emailRenderer->Initialize(); return $emailRenderer; } private function ExecuteSendingEmail($emailOptions,$order) { LogManager::LogDebug("[Execute] Rendering email template '".$emailOptions->Name."'"); $retriever=new OrderRetriever($this->loader,$order); $emailRenderer=new EmailRenderer($this->loader,$emailOptions,$retriever); $emailRenderer->Initialize(); $content= $emailRenderer->Render(); LogManager::LogDebug("[Execute] Email rendered successfully, content length: ".strlen($content)." bytes"); $attachments=[]; $attachments=apply_filters('aefwc-get-attachments',$attachments,$order,$emailOptions); if(count($attachments)>0) LogManager::LogDebug("[Execute] ".count($attachments)." attachment(s) found"); $toEmail=$emailRenderer->GetToEmail(); $subject=$emailRenderer->GetSubject(); LogManager::LogDebug("[Execute] To: '$toEmail', Subject: '$subject'"); if(count($emailRenderer->Options->HeaderOptions->To)>0&&$toEmail=='') { LogManager::LogError("[Execute] Recipient list configured but resolved to empty, aborting send"); return false; } $headers=$emailRenderer->GetHeaders(); LogManager::LogDebug("[Execute] Headers: ".implode(' | ',$headers)); LogManager::LogDebug("[Execute] Calling wp_mail..."); $result = \wp_mail($toEmail,$subject,strval($content),$headers,$attachments); if($result) LogManager::LogDebug("[Execute] wp_mail returned SUCCESS"); else LogManager::LogError("[Execute] wp_mail returned FAILURE for '$toEmail'"); return $result; } }