Use step args for triggers
[MAILPOET-4629]
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace MailPoet\Automation\Engine\Control;
|
||||
|
||||
use MailPoet\Automation\Engine\Data\StepRunArgs;
|
||||
use MailPoet\Automation\Engine\Data\Subject;
|
||||
use MailPoet\Automation\Engine\Data\WorkflowRun;
|
||||
use MailPoet\Automation\Engine\Exceptions;
|
||||
@@ -60,11 +61,11 @@ class TriggerHandler {
|
||||
$entry->getPayload();
|
||||
}
|
||||
|
||||
if (!$trigger->isTriggeredBy($step->getArgs(), $subjectEntries)) {
|
||||
$workflowRun = new WorkflowRun($workflow->getId(), $workflow->getVersionId(), $trigger->getKey(), $subjects);
|
||||
if (!$trigger->isTriggeredBy(new StepRunArgs($workflow, $workflowRun, $step, $subjectEntries))) {
|
||||
return;
|
||||
}
|
||||
|
||||
$workflowRun = new WorkflowRun($workflow->getId(), $workflow->getVersionId(), $trigger->getKey(), $subjects);
|
||||
$workflowRunId = $this->workflowRunStorage->createWorkflowRun($workflowRun);
|
||||
$nextStep = $step->getNextSteps()[0] ?? null;
|
||||
$this->actionScheduler->enqueue(Hooks::WORKFLOW_STEP, [
|
||||
|
@@ -2,16 +2,10 @@
|
||||
|
||||
namespace MailPoet\Automation\Engine\Workflows;
|
||||
|
||||
use MailPoet\Automation\Engine\Data\SubjectEntry;
|
||||
use MailPoet\Automation\Engine\Data\StepRunArgs;
|
||||
|
||||
interface Trigger extends Step {
|
||||
public function registerHooks(): void;
|
||||
|
||||
/**
|
||||
* Validate if the specific context of a run meets the
|
||||
* settings of a given trigger.
|
||||
*
|
||||
* @param SubjectEntry[] $subjectEntries
|
||||
*/
|
||||
public function isTriggeredBy(array $args, array $subjectEntries): bool;
|
||||
public function isTriggeredBy(StepRunArgs $args): bool;
|
||||
}
|
||||
|
@@ -27,4 +27,8 @@ class SegmentPayload implements Payload {
|
||||
public function getName(): string {
|
||||
return $this->segment->getName();
|
||||
}
|
||||
|
||||
public function getType(): string {
|
||||
return $this->segment->getType();
|
||||
}
|
||||
}
|
||||
|
@@ -31,4 +31,12 @@ class SubscriberPayload implements Payload {
|
||||
public function getStatus(): string {
|
||||
return $this->subscriber->getStatus();
|
||||
}
|
||||
|
||||
public function isWpUser(): bool {
|
||||
return $this->subscriber->isWPUser();
|
||||
}
|
||||
|
||||
public function getWpUserId(): ?int {
|
||||
return $this->subscriber->getWpUserId();
|
||||
}
|
||||
}
|
||||
|
@@ -2,10 +2,11 @@
|
||||
|
||||
namespace MailPoet\Automation\Integrations\MailPoet\Triggers;
|
||||
|
||||
use MailPoet\Automation\Engine\Data\StepRunArgs;
|
||||
use MailPoet\Automation\Engine\Data\Subject;
|
||||
use MailPoet\Automation\Engine\Hooks;
|
||||
use MailPoet\Automation\Engine\Workflows\Subject;
|
||||
use MailPoet\Automation\Engine\Workflows\Trigger;
|
||||
use MailPoet\Automation\Integrations\MailPoet\Payloads\SegmentPayload;
|
||||
use MailPoet\Automation\Integrations\MailPoet\Subjects\SegmentSubject;
|
||||
use MailPoet\Automation\Integrations\MailPoet\Subjects\SubscriberSubject;
|
||||
use MailPoet\Entities\SubscriberSegmentEntity;
|
||||
@@ -64,26 +65,12 @@ class SomeoneSubscribesTrigger implements Trigger {
|
||||
]);
|
||||
}
|
||||
|
||||
public function isTriggeredBy(array $args, Subject ...$subjects): bool {
|
||||
public function isTriggeredBy(StepRunArgs $args): bool {
|
||||
$segmentId = $args->getSinglePayloadByClass(SegmentPayload::class)->getId();
|
||||
|
||||
$segment = null;
|
||||
foreach ($subjects as $subject) {
|
||||
if (!$subject instanceof SegmentSubject) {
|
||||
continue;
|
||||
}
|
||||
/**
|
||||
* @var SegmentSubject $subject
|
||||
*/
|
||||
$segment = $subject->getSegment();
|
||||
}
|
||||
|
||||
// Return true, when no segment list is defined (=any list) or the segment matches the definition.
|
||||
return (
|
||||
!$segment
|
||||
|| !isset($args['segment_ids'])
|
||||
|| !is_array($args['segment_ids'])
|
||||
|| !count($args['segment_ids'])
|
||||
|| in_array($segment->getId(), $args['segment_ids'], true)
|
||||
);
|
||||
// Triggers when no segment IDs defined (= any segment) or the current segment paylo.
|
||||
$triggerArgs = $args->getStep()->getArgs();
|
||||
$segmentIds = $triggerArgs['segment_ids'] ?? [];
|
||||
return !is_array($segmentIds) || !$segmentIds || in_array($segmentId, $segmentIds, true);
|
||||
}
|
||||
}
|
||||
|
@@ -2,9 +2,11 @@
|
||||
|
||||
namespace MailPoet\Automation\Integrations\MailPoet\Triggers;
|
||||
|
||||
use MailPoet\Automation\Engine\Data\StepRunArgs;
|
||||
use MailPoet\Automation\Engine\Hooks;
|
||||
use MailPoet\Automation\Engine\Workflows\Subject;
|
||||
use MailPoet\Automation\Engine\Workflows\Trigger;
|
||||
use MailPoet\Automation\Integrations\MailPoet\Payloads\SegmentPayload;
|
||||
use MailPoet\Automation\Integrations\MailPoet\Payloads\SubscriberPayload;
|
||||
use MailPoet\Automation\Integrations\MailPoet\Subjects\SegmentSubject;
|
||||
use MailPoet\Automation\Integrations\MailPoet\Subjects\SubscriberSubject;
|
||||
use MailPoet\Entities\SegmentEntity;
|
||||
@@ -62,47 +64,28 @@ class UserRegistrationTrigger implements Trigger {
|
||||
]);
|
||||
}
|
||||
|
||||
public function isTriggeredBy(array $args, Subject ...$subjects): bool {
|
||||
$segment = null;
|
||||
$subscriber = null;
|
||||
foreach ($subjects as $subject) {
|
||||
if ($subject instanceof SegmentSubject) {
|
||||
$segment = $subject->getSegment();
|
||||
}
|
||||
if ($subject instanceof SubscriberSubject) {
|
||||
$subscriber = $subject->getSubscriber();
|
||||
}
|
||||
}
|
||||
|
||||
if (!$segment || !$subscriber) {
|
||||
public function isTriggeredBy(StepRunArgs $args): bool {
|
||||
$segmentPayload = $args->getSinglePayloadByClass(SegmentPayload::class);
|
||||
if ($segmentPayload->getType() !== SegmentEntity::TYPE_WP_USERS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($segment->getType() !== SegmentEntity::TYPE_WP_USERS) {
|
||||
$subscriberPayload = $args->getSinglePayloadByClass(SubscriberPayload::class);
|
||||
if (!$subscriberPayload->isWPUser()) {
|
||||
return false;
|
||||
}
|
||||
if (!isset($args['roles']) || !is_array($args['roles']) || !count($args['roles'])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!$subscriber->isWPUser()) {
|
||||
return false;
|
||||
}
|
||||
$user = $this->wp->getUserBy('id', $subscriber->getWpUserId());
|
||||
$user = $this->wp->getUserBy('id', $subscriberPayload->getWpUserId());
|
||||
if (!$user) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($user->roles as $userRole) {
|
||||
if (in_array($userRole, $args['roles'], true)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
$triggerArgs = $args->getStep()->getArgs();
|
||||
$roles = $triggerArgs['roles'] ?? [];
|
||||
return !is_array($roles) || !$roles || count(array_intersect($user->roles, $roles)) > 0;
|
||||
}
|
||||
|
||||
private function getSegment(SubscriberEntity $subscriber): SegmentEntity {
|
||||
|
||||
$segments = $subscriber->getSubscriberSegments()->toArray();
|
||||
if (!$segments) {
|
||||
throw new InvalidStateException();
|
||||
|
Reference in New Issue
Block a user