This commit is contained in:
emmymayo
2025-02-05 23:15:46 +01:00
commit 7269c99357
16995 changed files with 3389680 additions and 0 deletions
@@ -0,0 +1,50 @@
<?php declare(strict_types = 1);
namespace MailPoet\Automation\Engine\Builder;
if (!defined('ABSPATH')) exit;
use MailPoet\Automation\Engine\Data\Automation;
use MailPoet\Automation\Engine\Exceptions;
use MailPoet\Automation\Engine\Exceptions\InvalidStateException;
use MailPoet\Automation\Engine\Registry;
use MailPoet\Automation\Engine\Storage\AutomationStorage;
use MailPoet\Automation\Engine\Validation\AutomationValidator;
class CreateAutomationFromTemplateController {
/** @var AutomationStorage */
private $storage;
/** @var AutomationValidator */
private $automationValidator;
/** @var Registry */
private $registry;
public function __construct(
AutomationStorage $storage,
AutomationValidator $automationValidator,
Registry $registry
) {
$this->storage = $storage;
$this->automationValidator = $automationValidator;
$this->registry = $registry;
}
public function createAutomation(string $slug): Automation {
$template = $this->registry->getTemplate($slug);
if (!$template) {
throw Exceptions::automationTemplateNotFound($slug);
}
$automation = $template->createAutomation();
$this->automationValidator->validate($automation);
$automationId = $this->storage->createAutomation($automation);
$savedAutomation = $this->storage->getAutomation($automationId);
if (!$savedAutomation) {
throw new InvalidStateException('Automation not found.');
}
return $savedAutomation;
}
}
@@ -0,0 +1,35 @@
<?php declare(strict_types = 1);
namespace MailPoet\Automation\Engine\Builder;
if (!defined('ABSPATH')) exit;
use MailPoet\Automation\Engine\Data\Automation;
use MailPoet\Automation\Engine\Exceptions;
use MailPoet\Automation\Engine\Storage\AutomationStorage;
class DeleteAutomationController {
/** @var AutomationStorage */
private $automationStorage;
public function __construct(
AutomationStorage $automationStorage
) {
$this->automationStorage = $automationStorage;
}
public function deleteAutomation(int $id): Automation {
$automation = $this->automationStorage->getAutomation($id);
if (!$automation) {
throw Exceptions::automationNotFound($id);
}
if ($automation->getStatus() !== Automation::STATUS_TRASH) {
throw Exceptions::automationNotTrashed($id);
}
$this->automationStorage->deleteAutomation($automation);
return $automation;
}
}
@@ -0,0 +1,95 @@
<?php declare(strict_types = 1);
namespace MailPoet\Automation\Engine\Builder;
if (!defined('ABSPATH')) exit;
use MailPoet\Automation\Engine\Data\Automation;
use MailPoet\Automation\Engine\Data\NextStep;
use MailPoet\Automation\Engine\Data\Step;
use MailPoet\Automation\Engine\Exceptions;
use MailPoet\Automation\Engine\Exceptions\InvalidStateException;
use MailPoet\Automation\Engine\Storage\AutomationStorage;
use MailPoet\Automation\Engine\WordPress;
use MailPoet\Util\Security;
class DuplicateAutomationController {
/** @var WordPress */
private $wordPress;
/** @var AutomationStorage */
private $automationStorage;
public function __construct(
WordPress $wordPress,
AutomationStorage $automationStorage
) {
$this->wordPress = $wordPress;
$this->automationStorage = $automationStorage;
}
public function duplicateAutomation(int $id): Automation {
$automation = $this->automationStorage->getAutomation($id);
if (!$automation) {
throw Exceptions::automationNotFound($id);
}
$duplicate = new Automation(
$this->getName($automation->getName()),
$this->getSteps($automation->getSteps()),
$this->wordPress->wpGetCurrentUser()
);
$duplicate->setStatus(Automation::STATUS_DRAFT);
$automationId = $this->automationStorage->createAutomation($duplicate);
$savedAutomation = $this->automationStorage->getAutomation($automationId);
if (!$savedAutomation) {
throw new InvalidStateException('Automation not found.');
}
return $savedAutomation;
}
private function getName(string $name): string {
// translators: %s is the original automation name.
$newName = sprintf(__('Copy of %s', 'mailpoet'), $name);
$maxLength = $this->automationStorage->getNameColumnLength();
if (strlen($newName) > $maxLength) {
$append = '…';
return substr($newName, 0, $maxLength - strlen($append)) . $append;
}
return $newName;
}
/**
* @param Step[] $steps
* @return Step[]
*/
private function getSteps(array $steps): array {
$newIds = [];
foreach ($steps as $step) {
$id = $step->getId();
$newIds[$id] = $id === 'root' ? 'root' : $this->getId();
}
$newSteps = [];
foreach ($steps as $step) {
$newId = $newIds[$step->getId()];
$newSteps[$newId] = new Step(
$newId,
$step->getType(),
$step->getKey(),
$step->getArgs(),
array_map(function (NextStep $nextStep) use ($newIds): NextStep {
$nextStepId = $nextStep->getId();
return new NextStep($nextStepId ? $newIds[$nextStepId] : null);
}, $step->getNextSteps())
);
}
return $newSteps;
}
private function getId(): string {
return Security::generateRandomString(16);
}
}
@@ -0,0 +1,184 @@
<?php declare(strict_types = 1);
namespace MailPoet\Automation\Engine\Builder;
if (!defined('ABSPATH')) exit;
use ActionScheduler_Store;
use MailPoet\Automation\Engine\Control\ActionScheduler;
use MailPoet\Automation\Engine\Data\Automation;
use MailPoet\Automation\Engine\Data\AutomationRun;
use MailPoet\Automation\Engine\Data\Step;
use MailPoet\Automation\Engine\Exceptions;
use MailPoet\Automation\Engine\Exceptions\UnexpectedValueException;
use MailPoet\Automation\Engine\Hooks;
use MailPoet\Automation\Engine\Storage\AutomationRunStorage;
use MailPoet\Automation\Engine\Storage\AutomationStatisticsStorage;
use MailPoet\Automation\Engine\Storage\AutomationStorage;
use MailPoet\Automation\Engine\Validation\AutomationValidator;
class UpdateAutomationController {
/** @var Hooks */
private $hooks;
/** @var AutomationStorage */
private $storage;
/** @var AutomationStatisticsStorage */
private $statisticsStorage;
/** @var AutomationValidator */
private $automationValidator;
/** @var UpdateStepsController */
private $updateStepsController;
private AutomationRunStorage $automationRunStorage;
private ActionScheduler $actionScheduler;
public function __construct(
Hooks $hooks,
AutomationStorage $storage,
AutomationStatisticsStorage $statisticsStorage,
AutomationValidator $automationValidator,
AutomationRunStorage $automationRunStorage,
ActionScheduler $actionScheduler,
UpdateStepsController $updateStepsController
) {
$this->hooks = $hooks;
$this->storage = $storage;
$this->statisticsStorage = $statisticsStorage;
$this->automationValidator = $automationValidator;
$this->updateStepsController = $updateStepsController;
$this->automationRunStorage = $automationRunStorage;
$this->actionScheduler = $actionScheduler;
}
public function updateAutomation(int $id, array $data): Automation {
$automation = $this->storage->getAutomation($id);
if (!$automation) {
throw Exceptions::automationNotFound($id);
}
$this->validateIfAutomationCanBeUpdated($automation, $data);
if (array_key_exists('name', $data)) {
$automation->setName($data['name']);
}
$originalStatus = $automation->getStatus();
if (array_key_exists('status', $data)) {
$this->checkAutomationStatus($data['status']);
$automation->setStatus($data['status']);
}
if (array_key_exists('steps', $data)) {
$this->validateAutomationSteps($automation, $data['steps']);
$this->updateStepsController->updateSteps($automation, $data['steps']);
foreach ($automation->getSteps() as $step) {
$this->hooks->doAutomationStepBeforeSave($step, $automation);
$this->hooks->doAutomationStepByKeyBeforeSave($step, $automation);
}
}
if (($automation->getStatus() === Automation::STATUS_DRAFT) && ($originalStatus === Automation::STATUS_ACTIVE)) {
$this->unscheduleAutomationRuns($automation);
}
if (array_key_exists('meta', $data)) {
$automation->deleteAllMetas();
foreach ($data['meta'] as $key => $value) {
$automation->setMeta($key, $value);
}
}
$this->hooks->doAutomationBeforeSave($automation);
$this->automationValidator->validate($automation);
$this->storage->updateAutomation($automation);
$automation = $this->storage->getAutomation($id);
if (!$automation) {
throw Exceptions::automationNotFound($id);
}
return $automation;
}
/**
* This is a temporary validation, see MAILPOET-4744
*/
private function validateIfAutomationCanBeUpdated(Automation $automation, array $data): void {
if (
!in_array(
$automation->getStatus(),
[
Automation::STATUS_ACTIVE,
Automation::STATUS_DEACTIVATING,
],
true
)
) {
return;
}
$statistics = $this->statisticsStorage->getAutomationStats($automation->getId());
if ($statistics->getInProgress() === 0) {
return;
}
if (!isset($data['status']) || $data['status'] === $automation->getStatus()) {
throw Exceptions::automationHasActiveRuns($automation->getId());
}
}
private function checkAutomationStatus(string $status): void {
if (!in_array($status, Automation::STATUS_ALL, true)) {
// translators: %s is the status.
throw UnexpectedValueException::create()->withMessage(sprintf(__('Invalid status: %s', 'mailpoet'), $status));
}
}
protected function validateAutomationSteps(Automation $automation, array $steps): void {
$existingSteps = $automation->getSteps();
if (count($steps) !== count($existingSteps)) {
throw Exceptions::automationStructureModificationNotSupported();
}
foreach ($steps as $id => $data) {
$existingStep = $existingSteps[$id] ?? null;
if (!$existingStep || !$this->stepChanged(Step::fromArray($data), $existingStep)) {
throw Exceptions::automationStructureModificationNotSupported();
}
}
}
private function stepChanged(Step $a, Step $b): bool {
$aData = $a->toArray();
$bData = $b->toArray();
unset($aData['args']);
unset($bData['args']);
return $aData === $bData;
}
private function unscheduleAutomationRuns(Automation $automation): void {
$runIds = [];
$runs = $this->automationRunStorage->getAutomationRunsForAutomation($automation);
foreach ($runs as $run) {
if ($run->getStatus() === AutomationRun::STATUS_RUNNING) {
$this->automationRunStorage->updateStatus($run->getId(), AutomationRun::STATUS_CANCELLED);
}
$runIds[$run->getId()] = $run;
}
$actions = $this->actionScheduler->getScheduledActions(['hook' => Hooks::AUTOMATION_STEP, 'status' => ActionScheduler_Store::STATUS_PENDING]);
foreach ($actions as $action) {
$args = $action->get_args();
$automationArgs = reset($args);
if (isset($automationArgs['automation_run_id']) && isset($runIds[$automationArgs['automation_run_id']])) {
$this->actionScheduler->unscheduleAction(Hooks::AUTOMATION_STEP, $args);
}
}
}
}
@@ -0,0 +1,41 @@
<?php declare(strict_types = 1);
namespace MailPoet\Automation\Engine\Builder;
if (!defined('ABSPATH')) exit;
use MailPoet\Automation\Engine\Data\Automation;
use MailPoet\Automation\Engine\Data\Step;
use MailPoet\Automation\Engine\Exceptions;
use MailPoet\Automation\Engine\Registry;
class UpdateStepsController {
/** @var Registry */
private $registry;
public function __construct(
Registry $registry
) {
$this->registry = $registry;
}
public function updateSteps(Automation $automation, array $data): Automation {
$steps = [];
foreach ($data as $index => $stepData) {
$step = $this->processStep($stepData, $automation->getStep($stepData['id']));
$steps[$index] = $step;
}
$automation->setSteps($steps);
return $automation;
}
private function processStep(array $data, ?Step $existingStep): Step {
$key = $data['key'];
$step = $this->registry->getStep($key);
if (!$step && $existingStep && $data !== $existingStep->toArray()) {
throw Exceptions::automationStepNotFound($key);
}
return Step::fromArray($data);
}
}
@@ -0,0 +1 @@
<?php