From 4c1b2e46c81c751c78a26afd1ec49a1d65fb866e Mon Sep 17 00:00:00 2001 From: Jan Jakes Date: Thu, 25 Apr 2024 15:24:55 +0200 Subject: [PATCH] Trigger automation email sent hook for automation emails [MAILPOET-4977] --- .../Cron/Workers/SendingQueue/SendingQueue.php | 18 ++++++++++++++++++ .../Workers/SendingQueue/SendingQueueTest.php | 16 ++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/mailpoet/lib/Cron/Workers/SendingQueue/SendingQueue.php b/mailpoet/lib/Cron/Workers/SendingQueue/SendingQueue.php index 8a19565a7c..1cb7fb66ae 100644 --- a/mailpoet/lib/Cron/Workers/SendingQueue/SendingQueue.php +++ b/mailpoet/lib/Cron/Workers/SendingQueue/SendingQueue.php @@ -541,12 +541,30 @@ class SendingQueue { // log statistics $this->statisticsNewslettersRepository->createMultiple($statistics); + // update the sent count $this->mailerTask->updateSentCount(); + // enforce execution limits if queue is still being processed if ($task->getStatus() !== ScheduledTaskEntity::STATUS_COMPLETED) { $this->enforceSendingAndExecutionLimits($timer); } + + // trigger automation email sent hook for automation emails + if ( + $task->getStatus() === ScheduledTaskEntity::STATUS_COMPLETED + && isset($task->getMeta()['automation']) + ) { + try { + $this->wp->doAction('mailpoet_automation_email_sent', $task->getMeta()['automation']); + } catch (Throwable $e) { + $this->loggerFactory->getLogger(LoggerFactory::TOPIC_NEWSLETTERS)->error( + 'Error while executing "mailpoet_automation_email_sent action" hook', + ['task_id' => $task->getId(), 'error' => $e->getMessage()] + ); + } + } + $this->throttlingHandler->processSuccess(); } diff --git a/mailpoet/tests/integration/Cron/Workers/SendingQueue/SendingQueueTest.php b/mailpoet/tests/integration/Cron/Workers/SendingQueue/SendingQueueTest.php index f9f31cec6b..980e11305b 100644 --- a/mailpoet/tests/integration/Cron/Workers/SendingQueue/SendingQueueTest.php +++ b/mailpoet/tests/integration/Cron/Workers/SendingQueue/SendingQueueTest.php @@ -1424,6 +1424,22 @@ class SendingQueueTest extends \MailPoetTest { verify($this->entityManager->find(NewsletterEntity::class, $newsletter->getId()))->null(); } + public function testItTriggersAutomationEmailSentHook(): void { + $hookTriggered = false; + $hook = function() use (&$hookTriggered) { + $hookTriggered = true; + }; + $this->wp->addAction('mailpoet_automation_email_sent', $hook, 1); + + $this->scheduledTask->setMeta(['automation' => ['id' => 1, 'run_id' => 1, 'step_id' => 'abc', 'run_number' => 1]]); + $this->entityManager->flush(); + + $sendingQueueWorker = $this->getSendingQueueWorker(); + $sendingQueueWorker->process(); + + $this->assertSame(true, $hookTriggered); + } + private function createNewsletter(string $type, $subject, string $status = NewsletterEntity::STATUS_DRAFT): NewsletterEntity { $newsletter = new NewsletterEntity(); $newsletter->setType($type);