Trigger automation email sent hook for automation emails

[MAILPOET-4977]
This commit is contained in:
Jan Jakes
2024-04-25 15:24:55 +02:00
committed by Aschepikov
parent 36089cc2f4
commit 4c1b2e46c8
2 changed files with 34 additions and 0 deletions

View File

@@ -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();
}

View File

@@ -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);