Add hook and basic handler for automation triggers

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]
This commit is contained in:
Jan Jakes
2022-03-07 11:59:54 +01:00
committed by Veljko V
parent 933ee0c8c8
commit a191c691f5
6 changed files with 80 additions and 0 deletions

View File

@@ -3,6 +3,7 @@
namespace MailPoet\Automation\Engine\Storage;
use MailPoet\Automation\Engine\Exceptions;
use MailPoet\Automation\Engine\Workflows\Trigger;
use MailPoet\Automation\Engine\Workflows\Workflow;
use wpdb;
@@ -45,4 +46,20 @@ class WorkflowStorage {
}
return array_unique($triggerKeys);
}
/** @return Workflow[] */
public function getActiveWorkflowsByTrigger(Trigger $trigger): array {
$query = strval(
$this->wpdb->prepare(
"SELECT * FROM $this->table WHERE status = %s AND trigger_keys LIKE %s",
Workflow::STATUS_ACTIVE,
'%' . $this->wpdb->esc_like($trigger->getKey()) . '%'
)
);
$data = $this->wpdb->get_results($query, ARRAY_A);
return array_map(function (array $workflowData) {
return Workflow::fromArray($workflowData);
}, (array)$data);
}
}