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
AutomationTemplates.php
135 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 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\Data\AutomationTemplate; |
| 11 | use MailPoet\Automation\Engine\Data\AutomationTemplateCategory; |
| 12 | use MailPoet\Automation\Engine\Registry; |
| 13 | use MailPoet\WP\Functions as WPFunctions; |
| 14 | |
| 15 | class AutomationTemplates { |
| 16 | /** @var AssetsController */ |
| 17 | private $assetsController; |
| 18 | |
| 19 | /** @var PageRenderer */ |
| 20 | private $pageRenderer; |
| 21 | |
| 22 | /** @var Registry */ |
| 23 | private $registry; |
| 24 | |
| 25 | /** @var WPFunctions */ |
| 26 | private $wp; |
| 27 | |
| 28 | public function __construct( |
| 29 | AssetsController $assetsController, |
| 30 | PageRenderer $pageRenderer, |
| 31 | Registry $registry, |
| 32 | WPFunctions $wp |
| 33 | ) { |
| 34 | $this->assetsController = $assetsController; |
| 35 | $this->pageRenderer = $pageRenderer; |
| 36 | $this->registry = $registry; |
| 37 | $this->wp = $wp; |
| 38 | } |
| 39 | |
| 40 | public function render() { |
| 41 | $this->assetsController->setupAutomationTemplatesDependencies(); |
| 42 | |
| 43 | $this->pageRenderer->displayPage( |
| 44 | 'automation/templates.html', |
| 45 | [ |
| 46 | 'locale_full' => $this->wp->getLocale(), |
| 47 | 'api' => [ |
| 48 | 'root' => rtrim($this->wp->escUrlRaw($this->wp->restUrl()), '/'), |
| 49 | 'nonce' => $this->wp->wpCreateNonce('wp_rest'), |
| 50 | ], |
| 51 | 'templates' => array_map( |
| 52 | function(AutomationTemplate $template): array { |
| 53 | return $template->toArray(); |
| 54 | }, |
| 55 | array_values($this->registry->getTemplates()) |
| 56 | ), |
| 57 | 'template_categories' => array_map( |
| 58 | function (AutomationTemplateCategory $category): array { |
| 59 | return [ |
| 60 | 'slug' => $category->getSlug(), |
| 61 | 'name' => $category->getName(), |
| 62 | ]; |
| 63 | }, |
| 64 | array_values($this->registry->getTemplateCategories()) |
| 65 | ), |
| 66 | 'registry' => $this->buildRegistry(), |
| 67 | 'context' => $this->buildContext(), |
| 68 | ] |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | private function buildRegistry(): array { |
| 73 | $steps = []; |
| 74 | foreach ($this->registry->getSteps() as $key => $step) { |
| 75 | $steps[$key] = [ |
| 76 | 'key' => $step->getKey(), |
| 77 | 'name' => $step->getName(), |
| 78 | 'args_schema' => $step->getArgsSchema()->toArray(), |
| 79 | ]; |
| 80 | } |
| 81 | |
| 82 | $subjects = []; |
| 83 | foreach ($this->registry->getSubjects() as $key => $subject) { |
| 84 | $subjects[$key] = [ |
| 85 | 'key' => $subject->getKey(), |
| 86 | 'name' => $subject->getName(), |
| 87 | 'args_schema' => $subject->getArgsSchema()->toArray(), |
| 88 | 'field_keys' => array_map(function ($field) { |
| 89 | return $field->getKey(); |
| 90 | }, $subject->getFields()), |
| 91 | ]; |
| 92 | } |
| 93 | |
| 94 | $fields = []; |
| 95 | foreach ($this->registry->getFields() as $key => $field) { |
| 96 | $fields[$key] = [ |
| 97 | 'key' => $field->getKey(), |
| 98 | 'type' => $field->getType(), |
| 99 | 'name' => $field->getName(), |
| 100 | 'args' => $field->getArgs(), |
| 101 | ]; |
| 102 | } |
| 103 | |
| 104 | $filters = []; |
| 105 | foreach ($this->registry->getFilters() as $fieldType => $filter) { |
| 106 | $conditions = []; |
| 107 | foreach ($filter->getConditions() as $key => $label) { |
| 108 | $conditions[] = [ |
| 109 | 'key' => $key, |
| 110 | 'label' => $label, |
| 111 | ]; |
| 112 | } |
| 113 | $filters[$fieldType] = [ |
| 114 | 'field_type' => $filter->getFieldType(), |
| 115 | 'conditions' => $conditions, |
| 116 | ]; |
| 117 | } |
| 118 | |
| 119 | return [ |
| 120 | 'steps' => $steps, |
| 121 | 'subjects' => $subjects, |
| 122 | 'fields' => $fields, |
| 123 | 'filters' => $filters, |
| 124 | ]; |
| 125 | } |
| 126 | |
| 127 | private function buildContext(): array { |
| 128 | $data = []; |
| 129 | foreach ($this->registry->getContextFactories() as $key => $factory) { |
| 130 | $data[$key] = $factory(); |
| 131 | } |
| 132 | return $data; |
| 133 | } |
| 134 | } |
| 135 |