SomeoneSubscribesTrigger.php
3 months ago
SomeoneUnsubscribesTrigger.php
3 months ago
UserRegistrationTrigger.php
3 months ago
index.php
3 years ago
SomeoneUnsubscribesTrigger.php
108 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\MailPoet\Triggers; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\Automation; |
| 9 | use MailPoet\Automation\Engine\Data\StepRunArgs; |
| 10 | use MailPoet\Automation\Engine\Data\StepValidationArgs; |
| 11 | use MailPoet\Automation\Engine\Data\Subject; |
| 12 | use MailPoet\Automation\Engine\Exceptions; |
| 13 | use MailPoet\Automation\Engine\Hooks; |
| 14 | use MailPoet\Automation\Engine\Integration\Trigger; |
| 15 | use MailPoet\Automation\Integrations\MailPoet\Actions\SendEmailAction; |
| 16 | use MailPoet\Automation\Integrations\MailPoet\Payloads\SubscriberPayload; |
| 17 | use MailPoet\Automation\Integrations\MailPoet\Subjects\SubscriberSubject; |
| 18 | use MailPoet\Entities\SubscriberEntity; |
| 19 | use MailPoet\Subscribers\SubscribersRepository; |
| 20 | use MailPoet\Validator\Builder; |
| 21 | use MailPoet\Validator\Schema\ObjectSchema; |
| 22 | use MailPoet\WP\Functions as WPFunctions; |
| 23 | |
| 24 | class SomeoneUnsubscribesTrigger implements Trigger { |
| 25 | const KEY = 'mailpoet:someone-unsubscribes'; |
| 26 | const RULE_ID = 'unsubscribes-trigger-no-send-email'; |
| 27 | |
| 28 | /** @var WPFunctions */ |
| 29 | private $wp; |
| 30 | |
| 31 | /** @var SubscribersRepository */ |
| 32 | private $subscribersRepository; |
| 33 | |
| 34 | public function __construct( |
| 35 | WPFunctions $wp, |
| 36 | SubscribersRepository $subscribersRepository |
| 37 | ) { |
| 38 | $this->wp = $wp; |
| 39 | $this->subscribersRepository = $subscribersRepository; |
| 40 | } |
| 41 | |
| 42 | public function getKey(): string { |
| 43 | return self::KEY; |
| 44 | } |
| 45 | |
| 46 | public function getName(): string { |
| 47 | // translators: automation trigger title |
| 48 | return __('Email subscriber unsubscribed', 'mailpoet'); |
| 49 | } |
| 50 | |
| 51 | public function getArgsSchema(): ObjectSchema { |
| 52 | return Builder::object(); |
| 53 | } |
| 54 | |
| 55 | public function getSubjectKeys(): array { |
| 56 | return [ |
| 57 | SubscriberSubject::KEY, |
| 58 | ]; |
| 59 | } |
| 60 | |
| 61 | public function validate(StepValidationArgs $args): void { |
| 62 | } |
| 63 | |
| 64 | public function validateAutomation(Automation $automation): void { |
| 65 | if (!$automation->needsFullValidation()) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | $hasUnsubscribeTrigger = false; |
| 70 | $hasSendEmailAction = false; |
| 71 | foreach ($automation->getSteps() as $step) { |
| 72 | if ($step->getKey() === self::KEY) { |
| 73 | $hasUnsubscribeTrigger = true; |
| 74 | } |
| 75 | if ($step->getKey() === SendEmailAction::KEY) { |
| 76 | $hasSendEmailAction = true; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if ($hasUnsubscribeTrigger && $hasSendEmailAction) { |
| 81 | throw Exceptions::automationStructureNotValid( |
| 82 | __('The "Email subscriber unsubscribed" trigger cannot be used together with a "Send email" action. Remove the "Send email" action to activate this automation.', 'mailpoet'), |
| 83 | self::RULE_ID |
| 84 | ); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | public function registerHooks(): void { |
| 89 | $this->wp->addAction(SubscriberEntity::HOOK_SUBSCRIBER_STATUS_CHANGED, [$this, 'handleStatusChange']); |
| 90 | } |
| 91 | |
| 92 | public function handleStatusChange(int $subscriberId): void { |
| 93 | $subscriber = $this->subscribersRepository->findOneById($subscriberId); |
| 94 | if (!$subscriber || $subscriber->getStatus() !== SubscriberEntity::STATUS_UNSUBSCRIBED) { |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | $this->wp->doAction(Hooks::TRIGGER, $this, [ |
| 99 | new Subject(SubscriberSubject::KEY, ['subscriber_id' => $subscriber->getId()]), |
| 100 | ]); |
| 101 | } |
| 102 | |
| 103 | public function isTriggeredBy(StepRunArgs $args): bool { |
| 104 | $subscriberPayload = $args->getSinglePayloadByClass(SubscriberPayload::class); |
| 105 | return $subscriberPayload->getStatus() === SubscriberEntity::STATUS_UNSUBSCRIBED; |
| 106 | } |
| 107 | } |
| 108 |