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
Automation.php
141 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\AutomaticEmails\AutomaticEmails; |
| 11 | use MailPoet\Automation\Engine\Data\AutomationTemplate; |
| 12 | use MailPoet\Automation\Engine\Data\AutomationTemplateCategory; |
| 13 | use MailPoet\Automation\Engine\Registry; |
| 14 | use MailPoet\Automation\Engine\Storage\AutomationStorage; |
| 15 | use MailPoet\Entities\NewsletterEntity; |
| 16 | use MailPoet\Newsletter\NewslettersRepository; |
| 17 | use MailPoet\Segments\SegmentsSimpleListRepository; |
| 18 | use MailPoet\Settings\UserFlagsController; |
| 19 | use MailPoet\WP\Functions as WPFunctions; |
| 20 | |
| 21 | class Automation { |
| 22 | /** @var AssetsController */ |
| 23 | private $assetsController; |
| 24 | |
| 25 | private AutomaticEmails $automaticEmails; |
| 26 | |
| 27 | /** @var PageRenderer */ |
| 28 | private $pageRenderer; |
| 29 | |
| 30 | /** @var WPFunctions */ |
| 31 | private $wp; |
| 32 | |
| 33 | /** @var AutomationStorage */ |
| 34 | private $automationStorage; |
| 35 | |
| 36 | /** @var Registry */ |
| 37 | private $registry; |
| 38 | |
| 39 | private NewslettersRepository $newslettersRepository; |
| 40 | |
| 41 | private SegmentsSimpleListRepository $segmentsListRepository; |
| 42 | |
| 43 | private UserFlagsController $userFlagsController; |
| 44 | |
| 45 | public function __construct( |
| 46 | AssetsController $assetsController, |
| 47 | AutomaticEmails $automaticEmails, |
| 48 | PageRenderer $pageRenderer, |
| 49 | WPFunctions $wp, |
| 50 | AutomationStorage $automationStorage, |
| 51 | Registry $registry, |
| 52 | NewslettersRepository $newslettersRepository, |
| 53 | SegmentsSimpleListRepository $segmentsListRepository, |
| 54 | UserFlagsController $userFlagsController |
| 55 | ) { |
| 56 | $this->assetsController = $assetsController; |
| 57 | $this->automaticEmails = $automaticEmails; |
| 58 | $this->pageRenderer = $pageRenderer; |
| 59 | $this->wp = $wp; |
| 60 | $this->automationStorage = $automationStorage; |
| 61 | $this->registry = $registry; |
| 62 | $this->newslettersRepository = $newslettersRepository; |
| 63 | $this->segmentsListRepository = $segmentsListRepository; |
| 64 | $this->userFlagsController = $userFlagsController; |
| 65 | } |
| 66 | |
| 67 | public function render() { |
| 68 | global $wp_roles; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 69 | |
| 70 | $this->assetsController->setupAutomationListingDependencies(); |
| 71 | $this->pageRenderer->displayPage('automation.html', [ |
| 72 | 'locale_full' => $this->wp->getLocale(), |
| 73 | 'api' => [ |
| 74 | 'root' => rtrim($this->wp->escUrlRaw($this->wp->restUrl()), '/'), |
| 75 | 'nonce' => $this->wp->wpCreateNonce('wp_rest'), |
| 76 | ], |
| 77 | 'automationCount' => $this->automationStorage->getAutomationCount(), |
| 78 | 'legacyAutomationCount' => $this->newslettersRepository->countBy([ |
| 79 | 'type' => [NewsletterEntity::TYPE_WELCOME, NewsletterEntity::TYPE_AUTOMATIC], |
| 80 | ]), |
| 81 | 'templates' => array_map( |
| 82 | function(AutomationTemplate $template): array { |
| 83 | return $template->toArray(); |
| 84 | }, |
| 85 | array_values($this->registry->getTemplates()) |
| 86 | ), |
| 87 | 'template_categories' => array_map( |
| 88 | function (AutomationTemplateCategory $category): array { |
| 89 | return [ |
| 90 | 'slug' => $category->getSlug(), |
| 91 | 'name' => $category->getName(), |
| 92 | ]; |
| 93 | }, |
| 94 | array_values($this->registry->getTemplateCategories()) |
| 95 | ), |
| 96 | 'registry' => $this->buildRegistry(), |
| 97 | 'context' => $this->buildContext(), |
| 98 | 'segments' => $this->segmentsListRepository->getListWithSubscribedSubscribersCounts(), |
| 99 | 'roles' => $wp_roles->get_names() + ['mailpoet_all' => __('In any WordPress role', 'mailpoet')], |
| 100 | 'automatic_emails' => $this->automaticEmails->getAutomaticEmails(), |
| 101 | 'legacy_automations_notice_dismissed' => (bool)$this->userFlagsController->get('legacy_automations_notice_dismissed'), |
| 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 | 'args_schema' => $step->getArgsSchema()->toArray(), |
| 112 | ]; |
| 113 | } |
| 114 | |
| 115 | $subjects = []; |
| 116 | foreach ($this->registry->getSubjects() as $key => $subject) { |
| 117 | $subjects[$key] = [ |
| 118 | 'key' => $subject->getKey(), |
| 119 | 'name' => $subject->getName(), |
| 120 | 'args_schema' => $subject->getArgsSchema()->toArray(), |
| 121 | 'field_keys' => array_map(function ($field) { |
| 122 | return $field->getKey(); |
| 123 | }, $subject->getFields()), |
| 124 | ]; |
| 125 | } |
| 126 | |
| 127 | return [ |
| 128 | 'steps' => $steps, |
| 129 | 'subjects' => $subjects, |
| 130 | ]; |
| 131 | } |
| 132 | |
| 133 | private function buildContext(): array { |
| 134 | $data = []; |
| 135 | foreach ($this->registry->getContextFactories() as $key => $factory) { |
| 136 | $data[$key] = $factory(); |
| 137 | } |
| 138 | return $data; |
| 139 | } |
| 140 | } |
| 141 |