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