For now trigger keys are stored in the workflow as a JSON array. This is not optimal in case someone has many workflows but as workflows are user-created it's unlinkely someone will have thousands of them. We can consider adding a workflow_triggers table as well. [MAILPOET-4136]
60 lines
1.5 KiB
PHP
60 lines
1.5 KiB
PHP
<?php declare(strict_types = 1);
|
|
|
|
namespace MailPoet\Automation\Engine;
|
|
|
|
use MailPoet\Automation\Engine\API\API;
|
|
use MailPoet\Automation\Engine\Control\TriggerHandler;
|
|
use MailPoet\Automation\Engine\Storage\WorkflowStorage;
|
|
|
|
class Engine {
|
|
/** @var API */
|
|
private $api;
|
|
|
|
/** @var Registry */
|
|
private $registry;
|
|
|
|
/** @var TriggerHandler */
|
|
private $triggerHandler;
|
|
|
|
/** @var WordPress */
|
|
private $wordPress;
|
|
|
|
/** @var WorkflowStorage */
|
|
private $workflowStorage;
|
|
|
|
public function __construct(
|
|
API $api,
|
|
Registry $registry,
|
|
TriggerHandler $triggerHandler,
|
|
WordPress $wordPress,
|
|
WorkflowStorage $workflowStorage
|
|
) {
|
|
$this->api = $api;
|
|
$this->registry = $registry;
|
|
$this->triggerHandler = $triggerHandler;
|
|
$this->wordPress = $wordPress;
|
|
$this->workflowStorage = $workflowStorage;
|
|
}
|
|
|
|
public function initialize(): void {
|
|
// register Action Scheduler (when behind feature flag, do it only on initialization)
|
|
require_once __DIR__ . '/../../../vendor/woocommerce/action-scheduler/action-scheduler.php';
|
|
|
|
$this->api->initialize();
|
|
$this->triggerHandler->initialize();
|
|
|
|
$this->wordPress->doAction(Hooks::INITIALIZE, [$this->registry]);
|
|
$this->registerActiveTriggerHooks();
|
|
}
|
|
|
|
private function registerActiveTriggerHooks(): void {
|
|
$triggerKeys = $this->workflowStorage->getActiveTriggerKeys();
|
|
foreach ($triggerKeys as $triggerKey) {
|
|
$instance = $this->registry->getTrigger($triggerKey);
|
|
if ($instance) {
|
|
$instance->registerHooks();
|
|
}
|
|
}
|
|
}
|
|
}
|