Actions
1 month ago
Analytics
1 month ago
Fields
2 months ago
Hooks
1 month ago
Payloads
2 years ago
SubjectTransformers
2 months ago
Subjects
5 months ago
Templates
1 month ago
Triggers
2 months ago
ContextFactory.php
2 months ago
MailPoetIntegration.php
2 months ago
index.php
3 years ago
MailPoetIntegration.php
171 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\MailPoet; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Hooks; |
| 9 | use MailPoet\Automation\Engine\Integration; |
| 10 | use MailPoet\Automation\Engine\Registry; |
| 11 | use MailPoet\Automation\Engine\WordPress; |
| 12 | use MailPoet\Automation\Integrations\MailPoet\Actions\SendEmailAction; |
| 13 | use MailPoet\Automation\Integrations\MailPoet\Actions\SendLatestNewsletterAction; |
| 14 | use MailPoet\Automation\Integrations\MailPoet\Analytics\Analytics; |
| 15 | use MailPoet\Automation\Integrations\MailPoet\Hooks\AutomationEditorLoadingHooks; |
| 16 | use MailPoet\Automation\Integrations\MailPoet\Hooks\CreateAutomationRunHook; |
| 17 | use MailPoet\Automation\Integrations\MailPoet\Subjects\NewsletterLinkSubject; |
| 18 | use MailPoet\Automation\Integrations\MailPoet\Subjects\SegmentSubject; |
| 19 | use MailPoet\Automation\Integrations\MailPoet\Subjects\SubscriberSubject; |
| 20 | use MailPoet\Automation\Integrations\MailPoet\SubjectTransformers\CommentSubjectToSubscriberSubjectTransformer; |
| 21 | use MailPoet\Automation\Integrations\MailPoet\SubjectTransformers\CustomerSubjectToSubscriberSubjectTransformer; |
| 22 | use MailPoet\Automation\Integrations\MailPoet\SubjectTransformers\OrderSubjectToSegmentSubjectTransformer; |
| 23 | use MailPoet\Automation\Integrations\MailPoet\SubjectTransformers\OrderSubjectToSubscriberSubjectTransformer; |
| 24 | use MailPoet\Automation\Integrations\MailPoet\SubjectTransformers\SubscriberSubjectToWordPressUserSubjectTransformer; |
| 25 | use MailPoet\Automation\Integrations\MailPoet\Templates\TemplatesFactory; |
| 26 | use MailPoet\Automation\Integrations\MailPoet\Triggers\SomeoneSubscribesTrigger; |
| 27 | use MailPoet\Automation\Integrations\MailPoet\Triggers\SomeoneUnsubscribesTrigger; |
| 28 | use MailPoet\Automation\Integrations\MailPoet\Triggers\UserRegistrationTrigger; |
| 29 | |
| 30 | class MailPoetIntegration implements Integration { |
| 31 | /** @var ContextFactory */ |
| 32 | private $contextFactory; |
| 33 | |
| 34 | /** @var SegmentSubject */ |
| 35 | private $segmentSubject; |
| 36 | |
| 37 | /** @var SubscriberSubject */ |
| 38 | private $subscriberSubject; |
| 39 | |
| 40 | /** @var NewsletterLinkSubject */ |
| 41 | private $emailLinkSubject; |
| 42 | |
| 43 | /** @var SomeoneSubscribesTrigger */ |
| 44 | private $someoneSubscribesTrigger; |
| 45 | |
| 46 | /** @var SomeoneUnsubscribesTrigger */ |
| 47 | private $someoneUnsubscribesTrigger; |
| 48 | |
| 49 | /** @var UserRegistrationTrigger */ |
| 50 | private $userRegistrationTrigger; |
| 51 | |
| 52 | /** @var SendEmailAction */ |
| 53 | private $sendEmailAction; |
| 54 | |
| 55 | /** @var SendLatestNewsletterAction */ |
| 56 | private $sendLatestNewsletterAction; |
| 57 | |
| 58 | /** @var AutomationEditorLoadingHooks */ |
| 59 | private $automationEditorLoadingHooks; |
| 60 | |
| 61 | /** @var CreateAutomationRunHook */ |
| 62 | private $createAutomationRunHook; |
| 63 | |
| 64 | /** @var OrderSubjectToSubscriberSubjectTransformer */ |
| 65 | private $orderToSubscriberTransformer; |
| 66 | |
| 67 | /** @var OrderSubjectToSegmentSubjectTransformer */ |
| 68 | private $orderToSegmentTransformer; |
| 69 | |
| 70 | /** @var SubscriberSubjectToWordPressUserSubjectTransformer */ |
| 71 | private $subscriberToWordPressUserTransformer; |
| 72 | |
| 73 | /** @var CommentSubjectToSubscriberSubjectTransformer */ |
| 74 | private $commentToSubscriberTransformer; |
| 75 | |
| 76 | /** @var CustomerSubjectToSubscriberSubjectTransformer */ |
| 77 | private $customerToSubscriberTransformer; |
| 78 | |
| 79 | /** @var TemplatesFactory */ |
| 80 | private $templatesFactory; |
| 81 | |
| 82 | /** @var Analytics */ |
| 83 | private $registerAnalytics; |
| 84 | |
| 85 | /** @var WordPress */ |
| 86 | private $wordPress; |
| 87 | |
| 88 | public function __construct( |
| 89 | ContextFactory $contextFactory, |
| 90 | SegmentSubject $segmentSubject, |
| 91 | SubscriberSubject $subscriberSubject, |
| 92 | NewsletterLinkSubject $emailLinkSubject, |
| 93 | OrderSubjectToSubscriberSubjectTransformer $orderToSubscriberTransformer, |
| 94 | OrderSubjectToSegmentSubjectTransformer $orderToSegmentTransformer, |
| 95 | SubscriberSubjectToWordPressUserSubjectTransformer $subscriberToWordPressUserTransformer, |
| 96 | CommentSubjectToSubscriberSubjectTransformer $commentToSubscriberTransformer, |
| 97 | CustomerSubjectToSubscriberSubjectTransformer $customerToSubscriberTransformer, |
| 98 | SomeoneSubscribesTrigger $someoneSubscribesTrigger, |
| 99 | SomeoneUnsubscribesTrigger $someoneUnsubscribesTrigger, |
| 100 | UserRegistrationTrigger $userRegistrationTrigger, |
| 101 | SendEmailAction $sendEmailAction, |
| 102 | SendLatestNewsletterAction $sendLatestNewsletterAction, |
| 103 | AutomationEditorLoadingHooks $automationEditorLoadingHooks, |
| 104 | CreateAutomationRunHook $createAutomationRunHook, |
| 105 | TemplatesFactory $templatesFactory, |
| 106 | Analytics $registerAnalytics, |
| 107 | WordPress $wordPress |
| 108 | ) { |
| 109 | $this->contextFactory = $contextFactory; |
| 110 | $this->segmentSubject = $segmentSubject; |
| 111 | $this->subscriberSubject = $subscriberSubject; |
| 112 | $this->emailLinkSubject = $emailLinkSubject; |
| 113 | $this->orderToSubscriberTransformer = $orderToSubscriberTransformer; |
| 114 | $this->orderToSegmentTransformer = $orderToSegmentTransformer; |
| 115 | $this->subscriberToWordPressUserTransformer = $subscriberToWordPressUserTransformer; |
| 116 | $this->commentToSubscriberTransformer = $commentToSubscriberTransformer; |
| 117 | $this->customerToSubscriberTransformer = $customerToSubscriberTransformer; |
| 118 | $this->someoneSubscribesTrigger = $someoneSubscribesTrigger; |
| 119 | $this->someoneUnsubscribesTrigger = $someoneUnsubscribesTrigger; |
| 120 | $this->userRegistrationTrigger = $userRegistrationTrigger; |
| 121 | $this->sendEmailAction = $sendEmailAction; |
| 122 | $this->sendLatestNewsletterAction = $sendLatestNewsletterAction; |
| 123 | $this->automationEditorLoadingHooks = $automationEditorLoadingHooks; |
| 124 | $this->createAutomationRunHook = $createAutomationRunHook; |
| 125 | $this->templatesFactory = $templatesFactory; |
| 126 | $this->registerAnalytics = $registerAnalytics; |
| 127 | $this->wordPress = $wordPress; |
| 128 | } |
| 129 | |
| 130 | public function register(Registry $registry): void { |
| 131 | $registry->addContextFactory('mailpoet', function () { |
| 132 | return $this->contextFactory->getContextData(); |
| 133 | }); |
| 134 | |
| 135 | $registry->addSubject($this->segmentSubject); |
| 136 | $registry->addSubject($this->subscriberSubject); |
| 137 | $registry->addSubject($this->emailLinkSubject); |
| 138 | $registry->addTrigger($this->someoneSubscribesTrigger); |
| 139 | $registry->addTrigger($this->someoneUnsubscribesTrigger); |
| 140 | $registry->addTrigger($this->userRegistrationTrigger); |
| 141 | $registry->addAction($this->sendEmailAction); |
| 142 | $registry->addAction($this->sendLatestNewsletterAction); |
| 143 | $registry->addSubjectTransformer($this->orderToSubscriberTransformer); |
| 144 | $registry->addSubjectTransformer($this->orderToSegmentTransformer); |
| 145 | $registry->addSubjectTransformer($this->subscriberToWordPressUserTransformer); |
| 146 | $registry->addSubjectTransformer($this->commentToSubscriberTransformer); |
| 147 | $registry->addSubjectTransformer($this->customerToSubscriberTransformer); |
| 148 | |
| 149 | foreach ($this->templatesFactory->createTemplates() as $template) { |
| 150 | $registry->addTemplate($template); |
| 151 | } |
| 152 | |
| 153 | // sync step args (subject, preheader, etc.) to email settings |
| 154 | $registry->onBeforeAutomationStepSave( |
| 155 | [$this->sendEmailAction, 'saveEmailSettings'], |
| 156 | $this->sendEmailAction->getKey() |
| 157 | ); |
| 158 | |
| 159 | // execute send email step progress when email is sent |
| 160 | $this->wordPress->addAction('mailpoet_automation_email_sent', [$this->sendEmailAction, 'handleEmailSent']); |
| 161 | |
| 162 | // forbid combining the unsubscribe trigger with a "Send email" action |
| 163 | $this->wordPress->addAction(Hooks::AUTOMATION_BEFORE_SAVE, [$this->someoneUnsubscribesTrigger, 'validateAutomation']); |
| 164 | |
| 165 | $this->automationEditorLoadingHooks->init(); |
| 166 | $this->createAutomationRunHook->init(); |
| 167 | |
| 168 | $this->registerAnalytics->register(); |
| 169 | } |
| 170 | } |
| 171 |