API
3 years ago
Builder
2 months ago
Control
3 weeks ago
Data
2 months ago
Endpoints
1 month ago
Exceptions
1 year ago
Integration
11 months ago
Mappers
9 months ago
Storage
2 months ago
Templates
2 years ago
Utils
8 months ago
Validation
2 months ago
Engine.php
1 month ago
Exceptions.php
1 month ago
Hooks.php
2 months ago
Integration.php
4 years ago
Registry.php
2 months ago
WordPress.php
2 months ago
index.php
3 years ago
Registry.php
280 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Engine; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Control\RootStep; |
| 9 | use MailPoet\Automation\Engine\Data\AutomationTemplate; |
| 10 | use MailPoet\Automation\Engine\Data\AutomationTemplateCategory; |
| 11 | use MailPoet\Automation\Engine\Data\Field; |
| 12 | use MailPoet\Automation\Engine\Exceptions\InvalidStateException; |
| 13 | use MailPoet\Automation\Engine\Integration\Action; |
| 14 | use MailPoet\Automation\Engine\Integration\Filter; |
| 15 | use MailPoet\Automation\Engine\Integration\Payload; |
| 16 | use MailPoet\Automation\Engine\Integration\Step; |
| 17 | use MailPoet\Automation\Engine\Integration\Subject; |
| 18 | use MailPoet\Automation\Engine\Integration\SubjectTransformer; |
| 19 | use MailPoet\Automation\Engine\Integration\Trigger; |
| 20 | |
| 21 | class Registry { |
| 22 | /** @var array<string, AutomationTemplate> */ |
| 23 | private $templates; |
| 24 | |
| 25 | /** @var array<string, AutomationTemplateCategory> */ |
| 26 | private $templateCategories = []; |
| 27 | |
| 28 | /** @var array<string, Step> */ |
| 29 | private $steps = []; |
| 30 | |
| 31 | /** @var array<string, Subject<Payload>> */ |
| 32 | private $subjects = []; |
| 33 | |
| 34 | /** @var SubjectTransformer[] */ |
| 35 | private $subjectTransformers = []; |
| 36 | |
| 37 | /** @var array<string, Field>|null */ |
| 38 | private $fields = null; |
| 39 | |
| 40 | /** @var array<string, Filter> */ |
| 41 | private $filters = []; |
| 42 | |
| 43 | /** @var array<string, Trigger> */ |
| 44 | private $triggers = []; |
| 45 | |
| 46 | /** @var array<string, Action> */ |
| 47 | private $actions = []; |
| 48 | |
| 49 | /** @var array<string, callable> */ |
| 50 | private $contextFactories = []; |
| 51 | |
| 52 | /** @var WordPress */ |
| 53 | private $wordPress; |
| 54 | |
| 55 | public function __construct( |
| 56 | RootStep $rootStep, |
| 57 | WordPress $wordPress |
| 58 | ) { |
| 59 | $this->wordPress = $wordPress; |
| 60 | $this->steps[$rootStep->getKey()] = $rootStep; |
| 61 | } |
| 62 | |
| 63 | public function setupTemplateCategories(): void { |
| 64 | $this->templateCategories = [ |
| 65 | 'welcome' => new AutomationTemplateCategory('welcome', _x('Welcome', 'automation template category title', 'mailpoet')), |
| 66 | 'abandoned-cart' => new AutomationTemplateCategory('abandoned-cart', _x('Abandoned Cart', 'automation template category title', 'mailpoet')), |
| 67 | 'reengagement' => new AutomationTemplateCategory('reengagement', _x('Re-engagement', 'automation template category title', 'mailpoet')), |
| 68 | 'purchase' => new AutomationTemplateCategory('purchase', _x('Post-purchase', 'automation template category title', 'mailpoet')), |
| 69 | 'review' => new AutomationTemplateCategory('review', _x('Review', 'automation template category title', 'mailpoet')), |
| 70 | 'subscriptions' => new AutomationTemplateCategory('subscriptions', _x('Subscriptions', 'automation template category title', 'mailpoet')), |
| 71 | 'bookings' => new AutomationTemplateCategory('bookings', _x('Bookings', 'automation template category title', 'mailpoet')), |
| 72 | 'celebrations' => new AutomationTemplateCategory('celebrations', _x('Celebrations', 'automation template category title', 'mailpoet')), |
| 73 | ]; |
| 74 | } |
| 75 | |
| 76 | public function addTemplate(AutomationTemplate $template): void { |
| 77 | $category = $template->getCategory(); |
| 78 | $templateCategories = $this->getTemplateCategories(); |
| 79 | |
| 80 | if (!isset($templateCategories[$category])) { |
| 81 | throw InvalidStateException::create()->withMessage( |
| 82 | sprintf("Category '%s' was not registered", $category) |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | $this->templates[$template->getSlug()] = $template; |
| 87 | |
| 88 | // keep coming soon templates at the end |
| 89 | uasort( |
| 90 | $this->templates, |
| 91 | function (AutomationTemplate $a, AutomationTemplate $b): int { |
| 92 | if ($a->getType() === AutomationTemplate::TYPE_COMING_SOON) { |
| 93 | return 1; |
| 94 | } |
| 95 | if ($b->getType() === AutomationTemplate::TYPE_COMING_SOON) { |
| 96 | return -1; |
| 97 | } |
| 98 | return 0; |
| 99 | } |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | public function getTemplate(string $slug): ?AutomationTemplate { |
| 104 | return $this->getTemplates()[$slug] ?? null; |
| 105 | } |
| 106 | |
| 107 | /** @return array<string, AutomationTemplate> */ |
| 108 | public function getTemplates(?string $category = null): array { |
| 109 | return $category |
| 110 | ? array_filter( |
| 111 | $this->templates, |
| 112 | function(AutomationTemplate $template) use ($category): bool { |
| 113 | return $template->getCategory() === $category; |
| 114 | } |
| 115 | ) |
| 116 | : $this->templates; |
| 117 | } |
| 118 | |
| 119 | public function removeTemplate(string $slug): void { |
| 120 | unset($this->templates[$slug]); |
| 121 | } |
| 122 | |
| 123 | /** @return array<string, AutomationTemplateCategory> */ |
| 124 | public function getTemplateCategories(): array { |
| 125 | if (empty($this->templateCategories)) { |
| 126 | $this->setupTemplateCategories(); |
| 127 | } |
| 128 | return $this->templateCategories; |
| 129 | } |
| 130 | |
| 131 | /** @param Subject<Payload> $subject */ |
| 132 | public function addSubject(Subject $subject): void { |
| 133 | $key = $subject->getKey(); |
| 134 | if (isset($this->subjects[$key])) { |
| 135 | throw new \Exception(); // TODO |
| 136 | } |
| 137 | $this->subjects[$key] = $subject; |
| 138 | |
| 139 | // reset fields cache |
| 140 | $this->fields = null; |
| 141 | } |
| 142 | |
| 143 | /** @return Subject<Payload>|null */ |
| 144 | public function getSubject(string $key): ?Subject { |
| 145 | return $this->subjects[$key] ?? null; |
| 146 | } |
| 147 | |
| 148 | /** @return array<string, Subject<Payload>> */ |
| 149 | public function getSubjects(): array { |
| 150 | return $this->subjects; |
| 151 | } |
| 152 | |
| 153 | public function addSubjectTransformer(SubjectTransformer $transformer): void { |
| 154 | $this->subjectTransformers[] = $transformer; |
| 155 | } |
| 156 | |
| 157 | public function getSubjectTransformers(): array { |
| 158 | return $this->subjectTransformers; |
| 159 | } |
| 160 | |
| 161 | public function getField(string $key): ?Field { |
| 162 | return $this->getFields()[$key] ?? null; |
| 163 | } |
| 164 | |
| 165 | /** @return array<string, Field> */ |
| 166 | public function getFields(): array { |
| 167 | // add fields lazily (on the first call) |
| 168 | if ($this->fields === null) { |
| 169 | $this->fields = []; |
| 170 | foreach ($this->subjects as $subject) { |
| 171 | foreach ($subject->getFields() as $field) { |
| 172 | $this->addField($field); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | return $this->fields ?? []; |
| 177 | } |
| 178 | |
| 179 | public function addFilter(Filter $filter): void { |
| 180 | $fieldType = $filter->getFieldType(); |
| 181 | if (isset($this->filters[$fieldType])) { |
| 182 | throw new \Exception(); // TODO |
| 183 | } |
| 184 | $this->filters[$fieldType] = $filter; |
| 185 | } |
| 186 | |
| 187 | public function getFilter(string $fieldType): ?Filter { |
| 188 | return $this->filters[$fieldType] ?? null; |
| 189 | } |
| 190 | |
| 191 | /** @return array<string, Filter> */ |
| 192 | public function getFilters(): array { |
| 193 | return $this->filters; |
| 194 | } |
| 195 | |
| 196 | public function addStep(Step $step): void { |
| 197 | if ($step instanceof Trigger) { |
| 198 | $this->addTrigger($step); |
| 199 | } elseif ($step instanceof Action) { |
| 200 | $this->addAction($step); |
| 201 | } |
| 202 | |
| 203 | // TODO: allow adding any other step implementations? |
| 204 | } |
| 205 | |
| 206 | public function getStep(string $key): ?Step { |
| 207 | return $this->steps[$key] ?? null; |
| 208 | } |
| 209 | |
| 210 | /** @return array<string, Step> */ |
| 211 | public function getSteps(): array { |
| 212 | return $this->steps; |
| 213 | } |
| 214 | |
| 215 | public function addTrigger(Trigger $trigger): void { |
| 216 | $key = $trigger->getKey(); |
| 217 | if (isset($this->steps[$key]) || isset($this->triggers[$key])) { |
| 218 | throw new \Exception(); // TODO |
| 219 | } |
| 220 | $this->steps[$key] = $trigger; |
| 221 | $this->triggers[$key] = $trigger; |
| 222 | } |
| 223 | |
| 224 | public function getTrigger(string $key): ?Trigger { |
| 225 | return $this->triggers[$key] ?? null; |
| 226 | } |
| 227 | |
| 228 | /** @return array<string, Trigger> */ |
| 229 | public function getTriggers(): array { |
| 230 | return $this->triggers; |
| 231 | } |
| 232 | |
| 233 | public function addAction(Action $action): void { |
| 234 | $key = $action->getKey(); |
| 235 | if (isset($this->steps[$key]) || isset($this->actions[$key])) { |
| 236 | throw new \Exception(); // TODO |
| 237 | } |
| 238 | $this->steps[$key] = $action; |
| 239 | $this->actions[$key] = $action; |
| 240 | } |
| 241 | |
| 242 | public function getAction(string $key): ?Action { |
| 243 | return $this->actions[$key] ?? null; |
| 244 | } |
| 245 | |
| 246 | /** @return array<string, Action> */ |
| 247 | public function getActions(): array { |
| 248 | return $this->actions; |
| 249 | } |
| 250 | |
| 251 | public function addContextFactory(string $key, callable $factory): void { |
| 252 | $this->contextFactories[$key] = $factory; |
| 253 | } |
| 254 | |
| 255 | /** @return callable[] */ |
| 256 | public function getContextFactories(): array { |
| 257 | return $this->contextFactories; |
| 258 | } |
| 259 | |
| 260 | public function onBeforeAutomationSave(callable $callback, int $priority = 10): void { |
| 261 | $this->wordPress->addAction(Hooks::AUTOMATION_BEFORE_SAVE, $callback, $priority); |
| 262 | } |
| 263 | |
| 264 | public function onBeforeAutomationStepSave(callable $callback, ?string $key = null, int $priority = 10): void { |
| 265 | $keyPart = $key ? "/key=$key" : ''; |
| 266 | $this->wordPress->addAction(Hooks::AUTOMATION_STEP_BEFORE_SAVE . $keyPart, $callback, $priority, 2); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * This is used only internally. Fields are added lazily from subjects. |
| 271 | */ |
| 272 | private function addField(Field $field): void { |
| 273 | $key = $field->getKey(); |
| 274 | if (isset($this->fields[$key])) { |
| 275 | throw new \Exception(); // TODO |
| 276 | } |
| 277 | $this->fields[$key] = $field; |
| 278 | } |
| 279 | } |
| 280 |