Execute send email step progress when email is sent

[MAILPOET-4977]
This commit is contained in:
Jan Jakes
2024-04-25 15:26:29 +02:00
committed by Aschepikov
parent 4c1b2e46c8
commit fdc0468c64
2 changed files with 41 additions and 1 deletions

View File

@@ -3,6 +3,7 @@
namespace MailPoet\Automation\Integrations\MailPoet\Actions;
use MailPoet\AutomaticEmails\WooCommerce\Events\AbandonedCart;
use MailPoet\Automation\Engine\Control\AutomationController;
use MailPoet\Automation\Engine\Control\StepRunController;
use MailPoet\Automation\Engine\Data\Automation;
use MailPoet\Automation\Engine\Data\Step;
@@ -49,6 +50,9 @@ class SendEmailAction implements Action {
'woocommerce-subscriptions:trial-started',
];
/** @var AutomationController */
private $automationController;
/** @var SettingsController */
private $settings;
@@ -71,6 +75,7 @@ class SendEmailAction implements Action {
private $newsletterOptionFieldsRepository;
public function __construct(
AutomationController $automationController,
SettingsController $settings,
NewslettersRepository $newslettersRepository,
SubscriberSegmentRepository $subscriberSegmentRepository,
@@ -79,6 +84,7 @@ class SendEmailAction implements Action {
NewsletterOptionsRepository $newsletterOptionsRepository,
NewsletterOptionFieldsRepository $newsletterOptionFieldsRepository
) {
$this->automationController = $automationController;
$this->settings = $settings;
$this->newslettersRepository = $newslettersRepository;
$this->subscriberSegmentRepository = $subscriberSegmentRepository;
@@ -179,6 +185,31 @@ class SendEmailAction implements Action {
$controller->scheduleProgress(time() + MONTH_IN_SECONDS);
}
/** @param mixed $data */
public function handleEmailSent($data): void {
if (!is_array($data)) {
throw InvalidStateException::create()->withMessage(
sprintf('Invalid automation step data. Array expected, got: %s', gettype($data))
);
}
$runId = $data['run_id'] ?? null;
if (!is_int($runId)) {
throw InvalidStateException::create()->withMessage(
sprintf("Invalid automation step data. Expected 'run_id' to be an integer, got: %s", gettype($runId))
);
}
$stepId = $data['step_id'] ?? null;
if (!is_string($stepId)) {
throw InvalidStateException::create()->withMessage(
sprintf("Invalid automation step data. Expected 'step_id' to be a string, got: %s", gettype($runId))
);
}
$this->automationController->enqueueProgress($runId, $stepId);
}
private function checkSendingStatus(NewsletterEntity $newsletter, SubscriberEntity $subscriber): void {
$scheduledTaskSubscriber = $this->automationEmailScheduler->getScheduledTaskSubscriber($newsletter, $subscriber);
if (!$scheduledTaskSubscriber) {

View File

@@ -4,6 +4,7 @@ namespace MailPoet\Automation\Integrations\MailPoet;
use MailPoet\Automation\Engine\Integration;
use MailPoet\Automation\Engine\Registry;
use MailPoet\Automation\Engine\WordPress;
use MailPoet\Automation\Integrations\MailPoet\Actions\SendEmailAction;
use MailPoet\Automation\Integrations\MailPoet\Analytics\Analytics;
use MailPoet\Automation\Integrations\MailPoet\Hooks\AutomationEditorLoadingHooks;
@@ -65,6 +66,9 @@ class MailPoetIntegration implements Integration {
/** @var Analytics */
private $registerAnalytics;
/** @var WordPress */
private $wordPress;
public function __construct(
ContextFactory $contextFactory,
SegmentSubject $segmentSubject,
@@ -80,7 +84,8 @@ class MailPoetIntegration implements Integration {
AutomationEditorLoadingHooks $automationEditorLoadingHooks,
CreateAutomationRunHook $createAutomationRunHook,
TemplatesFactory $templatesFactory,
Analytics $registerAnalytics
Analytics $registerAnalytics,
WordPress $wordPress
) {
$this->contextFactory = $contextFactory;
$this->segmentSubject = $segmentSubject;
@@ -97,6 +102,7 @@ class MailPoetIntegration implements Integration {
$this->createAutomationRunHook = $createAutomationRunHook;
$this->templatesFactory = $templatesFactory;
$this->registerAnalytics = $registerAnalytics;
$this->wordPress = $wordPress;
}
public function register(Registry $registry): void {
@@ -125,6 +131,9 @@ class MailPoetIntegration implements Integration {
$this->sendEmailAction->getKey()
);
// execute send email step progress when email is sent
$this->wordPress->addAction('mailpoet_automation_email_sent', [$this->sendEmailAction, 'handleEmailSent']);
$this->automationEditorLoadingHooks->init();
$this->createAutomationRunHook->init();