CreateAutomationFromTemplateController.php
2 months ago
DeleteAutomationController.php
2 months ago
DuplicateAutomationController.php
2 months ago
UpdateAutomationController.php
2 months ago
UpdateStepsController.php
11 months ago
index.php
3 years ago
DuplicateAutomationController.php
124 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Engine\Builder; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\Automation; |
| 9 | use MailPoet\Automation\Engine\Data\NextStep; |
| 10 | use MailPoet\Automation\Engine\Data\Step; |
| 11 | use MailPoet\Automation\Engine\Exceptions; |
| 12 | use MailPoet\Automation\Engine\Exceptions\InvalidStateException; |
| 13 | use MailPoet\Automation\Engine\Hooks; |
| 14 | use MailPoet\Automation\Engine\Registry; |
| 15 | use MailPoet\Automation\Engine\Storage\AutomationStorage; |
| 16 | use MailPoet\Automation\Engine\WordPress; |
| 17 | use MailPoet\Util\Security; |
| 18 | |
| 19 | class DuplicateAutomationController { |
| 20 | /** @var WordPress */ |
| 21 | private $wordPress; |
| 22 | |
| 23 | /** @var AutomationStorage */ |
| 24 | private $automationStorage; |
| 25 | |
| 26 | /** @var Registry */ |
| 27 | private $registry; |
| 28 | |
| 29 | /** @var Hooks */ |
| 30 | private $hooks; |
| 31 | |
| 32 | public function __construct( |
| 33 | WordPress $wordPress, |
| 34 | AutomationStorage $automationStorage, |
| 35 | Registry $registry, |
| 36 | Hooks $hooks |
| 37 | ) { |
| 38 | $this->wordPress = $wordPress; |
| 39 | $this->automationStorage = $automationStorage; |
| 40 | $this->registry = $registry; |
| 41 | $this->hooks = $hooks; |
| 42 | } |
| 43 | |
| 44 | public function duplicateAutomation(int $id): Automation { |
| 45 | $automation = $this->automationStorage->getAutomation($id); |
| 46 | if (!$automation) { |
| 47 | throw Exceptions::automationNotFound($id); |
| 48 | } |
| 49 | |
| 50 | $duplicate = new Automation( |
| 51 | $this->getName($automation->getName()), |
| 52 | $this->getSteps($automation->getSteps()), |
| 53 | $this->wordPress->wpGetCurrentUser() |
| 54 | ); |
| 55 | $duplicate->setStatus(Automation::STATUS_DRAFT); |
| 56 | foreach ($automation->getAllMetas() as $key => $value) { |
| 57 | $duplicate->setMeta((string)$key, $value); |
| 58 | } |
| 59 | |
| 60 | $steps = $duplicate->getSteps(); |
| 61 | foreach ($steps as $id => $step) { |
| 62 | if ($step->getType() === 'action') { |
| 63 | $action = $this->registry->getAction($step->getKey()); |
| 64 | if ($action) { |
| 65 | $steps[$id] = $action->onDuplicate($step); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | $duplicate->setSteps($steps); |
| 70 | |
| 71 | $automationId = $this->automationStorage->createAutomation($duplicate); |
| 72 | $savedAutomation = $this->automationStorage->getAutomation($automationId); |
| 73 | if (!$savedAutomation) { |
| 74 | throw new InvalidStateException('Automation not found.'); |
| 75 | } |
| 76 | $this->hooks->doAutomationAfterDuplicate($savedAutomation, $automation); |
| 77 | return $savedAutomation; |
| 78 | } |
| 79 | |
| 80 | private function getName(string $name): string { |
| 81 | // translators: %s is the original automation name. |
| 82 | $newName = sprintf(__('Copy of %s', 'mailpoet'), $name); |
| 83 | $maxLength = $this->automationStorage->getNameColumnLength(); |
| 84 | if (strlen($newName) > $maxLength) { |
| 85 | $append = '…'; |
| 86 | return substr($newName, 0, $maxLength - strlen($append)) . $append; |
| 87 | } |
| 88 | return $newName; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @param Step[] $steps |
| 93 | * @return Step[] |
| 94 | */ |
| 95 | private function getSteps(array $steps): array { |
| 96 | $newIds = []; |
| 97 | foreach ($steps as $step) { |
| 98 | $id = $step->getId(); |
| 99 | $newIds[$id] = $id === 'root' ? 'root' : $this->getId(); |
| 100 | } |
| 101 | |
| 102 | $newSteps = []; |
| 103 | foreach ($steps as $step) { |
| 104 | $newId = $newIds[$step->getId()]; |
| 105 | $newSteps[$newId] = new Step( |
| 106 | $newId, |
| 107 | $step->getType(), |
| 108 | $step->getKey(), |
| 109 | $step->getArgs(), |
| 110 | array_map(function (NextStep $nextStep) use ($newIds): NextStep { |
| 111 | $nextStepId = $nextStep->getId(); |
| 112 | return new NextStep($nextStepId ? $newIds[$nextStepId] : null); |
| 113 | }, $step->getNextSteps()), |
| 114 | $step->getFilters() |
| 115 | ); |
| 116 | } |
| 117 | return $newSteps; |
| 118 | } |
| 119 | |
| 120 | private function getId(): string { |
| 121 | return Security::generateRandomString(16); |
| 122 | } |
| 123 | } |
| 124 |