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
AutomationAnalytics.php
118 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\Mappers\AutomationMapper; |
| 11 | use MailPoet\Automation\Engine\Registry; |
| 12 | use MailPoet\Automation\Engine\Storage\AutomationStorage; |
| 13 | use MailPoet\WP\Functions as WPFunctions; |
| 14 | use MailPoet\WP\Notice as WPNotice; |
| 15 | |
| 16 | class AutomationAnalytics { |
| 17 | |
| 18 | /** @var AssetsController */ |
| 19 | private $assetsController; |
| 20 | |
| 21 | /** @var PageRenderer */ |
| 22 | private $pageRenderer; |
| 23 | |
| 24 | /** @var AutomationStorage */ |
| 25 | private $automationStorage; |
| 26 | |
| 27 | /** @var AutomationMapper */ |
| 28 | private $automationMapper; |
| 29 | |
| 30 | /** @var Registry */ |
| 31 | private $registry; |
| 32 | |
| 33 | /** @var WPFunctions */ |
| 34 | private $wp; |
| 35 | |
| 36 | public function __construct( |
| 37 | AssetsController $assetsController, |
| 38 | PageRenderer $pageRenderer, |
| 39 | AutomationStorage $automationStorage, |
| 40 | AutomationMapper $automationMapper, |
| 41 | Registry $registry, |
| 42 | WPFunctions $wp |
| 43 | ) { |
| 44 | $this->assetsController = $assetsController; |
| 45 | $this->pageRenderer = $pageRenderer; |
| 46 | $this->automationStorage = $automationStorage; |
| 47 | $this->automationMapper = $automationMapper; |
| 48 | $this->registry = $registry; |
| 49 | $this->wp = $wp; |
| 50 | } |
| 51 | |
| 52 | public function render() { |
| 53 | $this->assetsController->setupAutomationAnalyticsDependencies(); |
| 54 | |
| 55 | $id = isset($_GET['id']) && is_numeric($_GET['id']) ? (int)$_GET['id'] : null; |
| 56 | $automation = $id ? $this->automationStorage->getAutomation($id) : null; |
| 57 | if (!$automation) { |
| 58 | $notice = new WPNotice( |
| 59 | WPNotice::TYPE_ERROR, |
| 60 | __('Automation not found.', 'mailpoet') |
| 61 | ); |
| 62 | $notice->displayWPNotice(); |
| 63 | $this->pageRenderer->displayPage('blank.html'); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | $this->pageRenderer->displayPage('automation/analytics.html', [ |
| 68 | 'registry' => $this->buildRegistry(), |
| 69 | 'context' => $this->buildContext(), |
| 70 | 'automation' => $this->automationMapper->buildAutomation($automation), |
| 71 | 'locale_full' => $this->wp->getLocale(), |
| 72 | 'api' => [ |
| 73 | 'root' => rtrim($this->wp->escUrlRaw($this->wp->restUrl()), '/'), |
| 74 | 'nonce' => $this->wp->wpCreateNonce('wp_rest'), |
| 75 | ], |
| 76 | 'jsonapi' => [ |
| 77 | 'root' => rtrim($this->wp->escUrlRaw(admin_url('admin-ajax.php')), '/'), |
| 78 | ], |
| 79 | ]); |
| 80 | } |
| 81 | |
| 82 | private function buildRegistry(): array { |
| 83 | $steps = []; |
| 84 | foreach ($this->registry->getSteps() as $key => $step) { |
| 85 | $steps[$key] = [ |
| 86 | 'key' => $step->getKey(), |
| 87 | 'name' => $step->getName(), |
| 88 | 'args_schema' => $step->getArgsSchema()->toArray(), |
| 89 | ]; |
| 90 | } |
| 91 | |
| 92 | $subjects = []; |
| 93 | foreach ($this->registry->getSubjects() as $key => $subject) { |
| 94 | $subjects[$key] = [ |
| 95 | 'key' => $subject->getKey(), |
| 96 | 'name' => $subject->getName(), |
| 97 | 'args_schema' => $subject->getArgsSchema()->toArray(), |
| 98 | 'field_keys' => array_map(function ($field) { |
| 99 | return $field->getKey(); |
| 100 | }, $subject->getFields()), |
| 101 | ]; |
| 102 | } |
| 103 | |
| 104 | return [ |
| 105 | 'steps' => $steps, |
| 106 | 'subjects' => $subjects, |
| 107 | ]; |
| 108 | } |
| 109 | |
| 110 | private function buildContext(): array { |
| 111 | $data = []; |
| 112 | foreach ($this->registry->getContextFactories() as $key => $factory) { |
| 113 | $data[$key] = $factory(); |
| 114 | } |
| 115 | return $data; |
| 116 | } |
| 117 | } |
| 118 |