API
3 years ago
Builder
2 months ago
Control
4 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
3 months ago
WordPress.php
2 months ago
index.php
3 years ago
Engine.php
108 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\API\API; |
| 9 | use MailPoet\Automation\Engine\Control\StepHandler; |
| 10 | use MailPoet\Automation\Engine\Control\TriggerHandler; |
| 11 | use MailPoet\Automation\Engine\Endpoints\Automations\AutomationsCreateFromTemplateEndpoint; |
| 12 | use MailPoet\Automation\Engine\Endpoints\Automations\AutomationsDeleteEndpoint; |
| 13 | use MailPoet\Automation\Engine\Endpoints\Automations\AutomationsDuplicateEndpoint; |
| 14 | use MailPoet\Automation\Engine\Endpoints\Automations\AutomationsGetEndpoint; |
| 15 | use MailPoet\Automation\Engine\Endpoints\Automations\AutomationsPutEndpoint; |
| 16 | use MailPoet\Automation\Engine\Endpoints\Automations\AutomationTemplateEmailPreviewEndpoint; |
| 17 | use MailPoet\Automation\Engine\Endpoints\Automations\AutomationTemplateGetEndpoint; |
| 18 | use MailPoet\Automation\Engine\Endpoints\Automations\AutomationTemplatesGetEndpoint; |
| 19 | use MailPoet\Automation\Engine\Endpoints\Automations\AutomationVersionsGetEndpoint; |
| 20 | use MailPoet\Automation\Engine\Storage\AutomationStorage; |
| 21 | use MailPoet\Automation\Integrations\Core\CoreIntegration; |
| 22 | use MailPoet\Automation\Integrations\WordPress\WordPressIntegration; |
| 23 | |
| 24 | class Engine { |
| 25 | const CAPABILITY_MANAGE_AUTOMATIONS = 'mailpoet_manage_automations'; |
| 26 | |
| 27 | /** @var API */ |
| 28 | private $api; |
| 29 | |
| 30 | /** @var CoreIntegration */ |
| 31 | private $coreIntegration; |
| 32 | |
| 33 | /** @var WordPressIntegration */ |
| 34 | private $wordPressIntegration; |
| 35 | |
| 36 | /** @var Registry */ |
| 37 | private $registry; |
| 38 | |
| 39 | /** @var StepHandler */ |
| 40 | private $stepHandler; |
| 41 | |
| 42 | /** @var TriggerHandler */ |
| 43 | private $triggerHandler; |
| 44 | |
| 45 | /** @var WordPress */ |
| 46 | private $wordPress; |
| 47 | |
| 48 | /** @var AutomationStorage */ |
| 49 | private $automationStorage; |
| 50 | |
| 51 | public function __construct( |
| 52 | API $api, |
| 53 | CoreIntegration $coreIntegration, |
| 54 | WordPressIntegration $wordPressIntegration, |
| 55 | Registry $registry, |
| 56 | StepHandler $stepHandler, |
| 57 | TriggerHandler $triggerHandler, |
| 58 | WordPress $wordPress, |
| 59 | AutomationStorage $automationStorage |
| 60 | ) { |
| 61 | $this->api = $api; |
| 62 | $this->coreIntegration = $coreIntegration; |
| 63 | $this->wordPressIntegration = $wordPressIntegration; |
| 64 | $this->registry = $registry; |
| 65 | $this->stepHandler = $stepHandler; |
| 66 | $this->triggerHandler = $triggerHandler; |
| 67 | $this->wordPress = $wordPress; |
| 68 | $this->automationStorage = $automationStorage; |
| 69 | } |
| 70 | |
| 71 | public function initialize(): void { |
| 72 | $this->registerApiRoutes(); |
| 73 | |
| 74 | $this->api->initialize(); |
| 75 | $this->stepHandler->initialize(); |
| 76 | $this->triggerHandler->initialize(); |
| 77 | |
| 78 | $this->coreIntegration->register($this->registry); |
| 79 | $this->wordPressIntegration->register($this->registry); |
| 80 | $this->wordPress->doAction(Hooks::INITIALIZE, [$this->registry]); |
| 81 | $this->registerActiveTriggerHooks(); |
| 82 | } |
| 83 | |
| 84 | private function registerApiRoutes(): void { |
| 85 | $this->wordPress->addAction(Hooks::API_INITIALIZE, function (API $api) { |
| 86 | $api->registerGetRoute('automations', AutomationsGetEndpoint::class); |
| 87 | $api->registerPutRoute('automations/(?P<id>\d+)', AutomationsPutEndpoint::class); |
| 88 | $api->registerGetRoute('automations/(?P<id>\d+)/versions', AutomationVersionsGetEndpoint::class); |
| 89 | $api->registerDeleteRoute('automations/(?P<id>\d+)', AutomationsDeleteEndpoint::class); |
| 90 | $api->registerPostRoute('automations/(?P<id>\d+)/duplicate', AutomationsDuplicateEndpoint::class); |
| 91 | $api->registerPostRoute('automations/create-from-template', AutomationsCreateFromTemplateEndpoint::class); |
| 92 | $api->registerGetRoute('automation-templates', AutomationTemplatesGetEndpoint::class); |
| 93 | $api->registerGetRoute('automation-template-email-preview', AutomationTemplateEmailPreviewEndpoint::class); |
| 94 | $api->registerGetRoute('automation-templates/(?P<slug>.+)', AutomationTemplateGetEndpoint::class); |
| 95 | }); |
| 96 | } |
| 97 | |
| 98 | private function registerActiveTriggerHooks(): void { |
| 99 | $triggerKeys = $this->automationStorage->getActiveTriggerKeys(); |
| 100 | foreach ($triggerKeys as $triggerKey) { |
| 101 | $instance = $this->registry->getTrigger($triggerKey); |
| 102 | if ($instance) { |
| 103 | $instance->registerHooks(); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 |