Allow multiple subjects of the same type
[MAILPOET-4465]
This commit is contained in:
@ -55,8 +55,8 @@ class TriggerHandler {
|
|||||||
|
|
||||||
// ensure subjects are registered and loadable
|
// ensure subjects are registered and loadable
|
||||||
$loadedSubjects = [];
|
$loadedSubjects = [];
|
||||||
foreach ($subjects as $key => $args) {
|
foreach ($subjects as $subject) {
|
||||||
$loadedSubjects[] = $this->subjectLoader->loadSubject($key, $args);
|
$loadedSubjects[] = $this->subjectLoader->loadSubject($subject['key'], $subject['args']);
|
||||||
}
|
}
|
||||||
|
|
||||||
$workflowRun = new WorkflowRun($workflow->getId(), $trigger->getKey(), $loadedSubjects);
|
$workflowRun = new WorkflowRun($workflow->getId(), $trigger->getKey(), $loadedSubjects);
|
||||||
|
@ -42,11 +42,9 @@ class WorkflowRunStorage {
|
|||||||
|
|
||||||
if ($result) {
|
if ($result) {
|
||||||
$data = (array)$result;
|
$data = (array)$result;
|
||||||
$subjects = [];
|
$data['subjects'] = array_map(function (array $subject) {
|
||||||
foreach (Json::decode($data['subjects']) as $key => $args) {
|
return $this->subjectLoader->loadSubject($subject['key'], $subject['args']);
|
||||||
$subjects[$key] = $this->subjectLoader->loadSubject($key, $args);
|
}, Json::decode($data['subjects']));
|
||||||
}
|
|
||||||
$data['subjects'] = $subjects;
|
|
||||||
return WorkflowRun::fromArray($data);
|
return WorkflowRun::fromArray($data);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -76,7 +76,14 @@ class WorkflowRun {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @return Subject[] */
|
/** @return Subject[] */
|
||||||
public function getSubjects(): array {
|
public function getSubjects(string $key = null): array {
|
||||||
|
if ($key) {
|
||||||
|
return array_values(
|
||||||
|
array_filter($this->subjects, function (Subject $subject) use ($key) {
|
||||||
|
return $subject->getKey() === $key;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
return $this->subjects;
|
return $this->subjects;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,10 +95,9 @@ class WorkflowRun {
|
|||||||
'created_at' => $this->createdAt->format(DateTimeImmutable::W3C),
|
'created_at' => $this->createdAt->format(DateTimeImmutable::W3C),
|
||||||
'updated_at' => $this->updatedAt->format(DateTimeImmutable::W3C),
|
'updated_at' => $this->updatedAt->format(DateTimeImmutable::W3C),
|
||||||
'subjects' => Json::encode(
|
'subjects' => Json::encode(
|
||||||
array_reduce($this->subjects, function (array $subjects, Subject $subject): array {
|
array_map(function (Subject $subject): array {
|
||||||
$subjects[$subject->getKey()] = $subject->pack();
|
return ['key' => $subject->getKey(), 'args' => $subject->pack()];
|
||||||
return $subjects;
|
}, $this->subjects)
|
||||||
}, [])
|
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ namespace MailPoet\Automation\Integrations\MailPoet\Actions;
|
|||||||
|
|
||||||
use MailPoet\Automation\Engine\Workflows\Action;
|
use MailPoet\Automation\Engine\Workflows\Action;
|
||||||
use MailPoet\Automation\Engine\Workflows\Step;
|
use MailPoet\Automation\Engine\Workflows\Step;
|
||||||
|
use MailPoet\Automation\Engine\Workflows\Subject;
|
||||||
use MailPoet\Automation\Engine\Workflows\Workflow;
|
use MailPoet\Automation\Engine\Workflows\Workflow;
|
||||||
use MailPoet\Automation\Engine\Workflows\WorkflowRun;
|
use MailPoet\Automation\Engine\Workflows\WorkflowRun;
|
||||||
use MailPoet\Automation\Integrations\MailPoet\Subjects\SegmentSubject;
|
use MailPoet\Automation\Integrations\MailPoet\Subjects\SegmentSubject;
|
||||||
@ -62,15 +63,19 @@ class SendWelcomeEmailAction implements Action {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$segmentSubject = $subjects['mailpoet:segment'] ?? null;
|
$segmentSubjects = array_filter($subjects, function (Subject $subject) {
|
||||||
$subscriberSubject = $subjects['mailpoet:subscriber'] ?? null;
|
return $subject->getKey() === SegmentSubject::KEY;
|
||||||
|
});
|
||||||
|
$subscriberSubjects = array_filter($subjects, function (Subject $subject) {
|
||||||
|
return $subject->getKey() === SubscriberSubject::KEY;
|
||||||
|
});
|
||||||
|
|
||||||
return $segmentSubject instanceof SegmentSubject && $subscriberSubject instanceof SubscriberSubject;
|
return count($segmentSubjects) === 1 && count($subscriberSubjects) === 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function run(Workflow $workflow, WorkflowRun $workflowRun, Step $step): void {
|
public function run(Workflow $workflow, WorkflowRun $workflowRun, Step $step): void {
|
||||||
$newsletter = $this->getWelcomeEmailForStep($step);
|
$newsletter = $this->getWelcomeEmailForStep($step);
|
||||||
$subscriberSubject = $workflowRun->getSubjects()['mailpoet:subscriber'] ?? null;
|
$subscriberSubject = $workflowRun->getSubjects('mailpoet:subscriber')[0] ?? null;
|
||||||
if (!$subscriberSubject instanceof SubscriberSubject) {
|
if (!$subscriberSubject instanceof SubscriberSubject) {
|
||||||
throw InvalidStateException::create()->withMessage('A mailpoet:subscriber subject is required.');
|
throw InvalidStateException::create()->withMessage('A mailpoet:subscriber subject is required.');
|
||||||
}
|
}
|
||||||
@ -86,7 +91,7 @@ class SendWelcomeEmailAction implements Action {
|
|||||||
throw InvalidStateException::create()->withMessage(sprintf("Cannot schedule a newsletter for subscriber ID '%s' because their status is '%s'.", $subscriber->getId(), $subscriber->getStatus()));
|
throw InvalidStateException::create()->withMessage(sprintf("Cannot schedule a newsletter for subscriber ID '%s' because their status is '%s'.", $subscriber->getId(), $subscriber->getStatus()));
|
||||||
}
|
}
|
||||||
|
|
||||||
$segmentSubject = $workflowRun->getSubjects()['mailpoet:segment'] ?? null;
|
$segmentSubject = $workflowRun->getSubjects('mailpoet:segment')[0] ?? null;
|
||||||
if (!$segmentSubject instanceof SegmentSubject) {
|
if (!$segmentSubject instanceof SegmentSubject) {
|
||||||
throw InvalidStateException::create()->withMessage('A mailpoet:segment subject is required.');
|
throw InvalidStateException::create()->withMessage('A mailpoet:segment subject is required.');
|
||||||
}
|
}
|
||||||
|
@ -41,8 +41,18 @@ class SegmentSubscribedTrigger implements Trigger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->wp->doAction(Hooks::TRIGGER, $this, [
|
$this->wp->doAction(Hooks::TRIGGER, $this, [
|
||||||
SegmentSubject::KEY => ['segment_id' => $segment->getId()],
|
[
|
||||||
SubscriberSubject::KEY => ['subscriber_id' => $subscriber->getId()],
|
'key' => SegmentSubject::KEY,
|
||||||
|
'args' => [
|
||||||
|
'segment_id' => $segment->getId(),
|
||||||
|
],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'key' => SubscriberSubject::KEY,
|
||||||
|
'args' => [
|
||||||
|
'subscriber_id' => $subscriber->getId(),
|
||||||
|
],
|
||||||
|
],
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user