AbstractAutomationEmbed.php
5 months ago
Automation.php
2 years ago
AutomationAnalytics.php
2 months ago
AutomationEditor.php
2 months ago
AutomationFlowEmbed.php
6 months ago
AutomationPreviewEmbed.php
2 months ago
AutomationTemplates.php
2 years ago
CustomFields.php
2 months ago
DynamicSegments.php
2 months ago
ExperimentalFeatures.php
3 years ago
FormEditor.php
1 week ago
Forms.php
2 months ago
Help.php
2 months ago
Homepage.php
2 years ago
Landingpage.php
2 years ago
Logs.php
1 month ago
NewsletterEditor.php
2 months ago
Newsletters.php
2 months ago
Settings.php
5 months ago
StaticSegments.php
2 months ago
Subscribers.php
1 week ago
SubscribersExport.php
3 years ago
SubscribersImport.php
2 months ago
Tags.php
2 months ago
Upgrade.php
1 year ago
WelcomeWizard.php
2 months ago
WooCommerceSetup.php
2 months ago
index.php
3 years ago
AutomationEditor.php
177 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\AdminPages\Pages; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\AdminPages\AssetsController; |
| 9 | use MailPoet\AdminPages\PageRenderer; |
| 10 | use MailPoet\Automation\Engine\Control\SubjectTransformerHandler; |
| 11 | use MailPoet\Automation\Engine\Data\Automation; |
| 12 | use MailPoet\Automation\Engine\Data\Field; |
| 13 | use MailPoet\Automation\Engine\Hooks; |
| 14 | use MailPoet\Automation\Engine\Integration\Trigger; |
| 15 | use MailPoet\Automation\Engine\Mappers\AutomationMapper; |
| 16 | use MailPoet\Automation\Engine\Registry; |
| 17 | use MailPoet\Automation\Engine\Storage\AutomationStorage; |
| 18 | use MailPoet\WP\Functions as WPFunctions; |
| 19 | use MailPoet\WP\Notice as WPNotice; |
| 20 | use MailPoet\WPCOM\DotcomHelperFunctions; |
| 21 | |
| 22 | class AutomationEditor { |
| 23 | /** @var AssetsController */ |
| 24 | private $assetsController; |
| 25 | |
| 26 | /** @var AutomationMapper */ |
| 27 | private $automationMapper; |
| 28 | |
| 29 | /** @var AutomationStorage */ |
| 30 | private $automationStorage; |
| 31 | |
| 32 | /** @var PageRenderer */ |
| 33 | private $pageRenderer; |
| 34 | |
| 35 | /** @var Registry */ |
| 36 | private $registry; |
| 37 | |
| 38 | /** @var WPFunctions */ |
| 39 | private $wp; |
| 40 | |
| 41 | /** @var SubjectTransformerHandler */ |
| 42 | private $subjectTransformerHandler; |
| 43 | |
| 44 | /** @var DotcomHelperFunctions */ |
| 45 | private $dotcomHelperFunctions; |
| 46 | |
| 47 | public function __construct( |
| 48 | AssetsController $assetsController, |
| 49 | AutomationMapper $automationMapper, |
| 50 | AutomationStorage $automationStorage, |
| 51 | PageRenderer $pageRenderer, |
| 52 | Registry $registry, |
| 53 | WPFunctions $wp, |
| 54 | SubjectTransformerHandler $subjectTransformerHandler, |
| 55 | DotcomHelperFunctions $dotcomHelperFunctions |
| 56 | ) { |
| 57 | $this->assetsController = $assetsController; |
| 58 | $this->automationMapper = $automationMapper; |
| 59 | $this->automationStorage = $automationStorage; |
| 60 | $this->pageRenderer = $pageRenderer; |
| 61 | $this->registry = $registry; |
| 62 | $this->wp = $wp; |
| 63 | $this->subjectTransformerHandler = $subjectTransformerHandler; |
| 64 | $this->dotcomHelperFunctions = $dotcomHelperFunctions; |
| 65 | } |
| 66 | |
| 67 | public function render() { |
| 68 | $this->assetsController->setupAutomationEditorDependencies(); |
| 69 | |
| 70 | $id = isset($_GET['id']) && is_numeric($_GET['id']) ? (int)$_GET['id'] : null; |
| 71 | |
| 72 | $this->wp->doAction(Hooks::EDITOR_BEFORE_LOAD, (int)$id); |
| 73 | |
| 74 | $automation = $id ? $this->automationStorage->getAutomation($id) : null; |
| 75 | if (!$automation) { |
| 76 | $notice = new WPNotice( |
| 77 | WPNotice::TYPE_ERROR, |
| 78 | __('Automation not found.', 'mailpoet') |
| 79 | ); |
| 80 | $notice->displayWPNotice(); |
| 81 | $this->pageRenderer->displayPage('blank.html'); |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | if ($automation->getStatus() === Automation::STATUS_TRASH) { |
| 86 | $this->wp->wpSafeRedirect($this->wp->adminUrl('admin.php?page=mailpoet-automation&status=trash¬ice=had-been-deleted')); |
| 87 | exit(); |
| 88 | } |
| 89 | |
| 90 | $this->pageRenderer->displayPage('automation/editor.html', [ |
| 91 | 'registry' => $this->buildRegistry(), |
| 92 | 'context' => $this->buildContext(), |
| 93 | 'automation' => $this->automationMapper->buildAutomation($automation), |
| 94 | 'locale_full' => $this->wp->getLocale(), |
| 95 | 'api' => [ |
| 96 | 'root' => rtrim($this->wp->escUrlRaw($this->wp->restUrl()), '/'), |
| 97 | 'nonce' => $this->wp->wpCreateNonce('wp_rest'), |
| 98 | ], |
| 99 | 'jsonapi' => [ |
| 100 | 'root' => rtrim($this->wp->escUrlRaw(admin_url('admin-ajax.php')), '/'), |
| 101 | ], |
| 102 | ]); |
| 103 | } |
| 104 | |
| 105 | private function buildRegistry(): array { |
| 106 | $steps = []; |
| 107 | foreach ($this->registry->getSteps() as $key => $step) { |
| 108 | $steps[$key] = [ |
| 109 | 'key' => $step->getKey(), |
| 110 | 'name' => $step->getName(), |
| 111 | 'subject_keys' => $step instanceof Trigger ? $this->subjectTransformerHandler->getSubjectKeysForTrigger($step) : $step->getSubjectKeys(), |
| 112 | 'args_schema' => $step->getArgsSchema()->toArray(), |
| 113 | ]; |
| 114 | } |
| 115 | |
| 116 | $subjects = []; |
| 117 | foreach ($this->registry->getSubjects() as $key => $subject) { |
| 118 | $subjectFields = $subject->getFields(); |
| 119 | usort($subjectFields, function (Field $a, Field $b) { |
| 120 | return $a->getName() <=> $b->getName(); |
| 121 | }); |
| 122 | |
| 123 | $subjects[$key] = [ |
| 124 | 'key' => $subject->getKey(), |
| 125 | 'name' => $subject->getName(), |
| 126 | 'args_schema' => $subject->getArgsSchema()->toArray(), |
| 127 | 'field_keys' => array_map(function ($field) { |
| 128 | return $field->getKey(); |
| 129 | }, $subjectFields), |
| 130 | ]; |
| 131 | } |
| 132 | |
| 133 | $fields = []; |
| 134 | foreach ($this->registry->getFields() as $key => $field) { |
| 135 | $fields[$key] = [ |
| 136 | 'key' => $field->getKey(), |
| 137 | 'type' => $field->getType(), |
| 138 | 'name' => $field->getName(), |
| 139 | 'args' => $field->getArgs(), |
| 140 | ]; |
| 141 | } |
| 142 | |
| 143 | $filters = []; |
| 144 | foreach ($this->registry->getFilters() as $fieldType => $filter) { |
| 145 | $conditions = []; |
| 146 | foreach ($filter->getConditions() as $key => $label) { |
| 147 | $conditions[] = [ |
| 148 | 'key' => $key, |
| 149 | 'label' => $label, |
| 150 | ]; |
| 151 | } |
| 152 | $filters[$fieldType] = [ |
| 153 | 'field_type' => $filter->getFieldType(), |
| 154 | 'conditions' => $conditions, |
| 155 | ]; |
| 156 | } |
| 157 | |
| 158 | return [ |
| 159 | 'steps' => $steps, |
| 160 | 'subjects' => $subjects, |
| 161 | 'fields' => $fields, |
| 162 | 'filters' => $filters, |
| 163 | ]; |
| 164 | } |
| 165 | |
| 166 | private function buildContext(): array { |
| 167 | $data = []; |
| 168 | foreach ($this->registry->getContextFactories() as $key => $factory) { |
| 169 | $data[$key] = $factory(); |
| 170 | } |
| 171 | |
| 172 | $data['is_garden'] = $this->dotcomHelperFunctions->isGarden(); |
| 173 | |
| 174 | return $data; |
| 175 | } |
| 176 | } |
| 177 |