mailpoet
/
lib
/
Automation
/
Integrations
/
MailPoet
/
Analytics
/
Controller
/
OverviewStatisticsController.php
AutomationTimeSpanController.php
2 months ago
OverviewStatisticsController.php
2 months ago
StepStatisticController.php
2 months ago
index.php
3 years ago
OverviewStatisticsController.php
127 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\MailPoet\Analytics\Controller; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\Automation; |
| 9 | use MailPoet\Automation\Integrations\MailPoet\Analytics\Entities\QueryWithCompare; |
| 10 | use MailPoet\Entities\StatisticsClickEntity; |
| 11 | use MailPoet\Entities\StatisticsOpenEntity; |
| 12 | use MailPoet\Newsletter\NewslettersRepository; |
| 13 | use MailPoet\Newsletter\Statistics\NewsletterStatisticsRepository; |
| 14 | use MailPoet\Newsletter\Statistics\WooCommerceRevenue; |
| 15 | use MailPoet\Newsletter\Url as NewsletterUrl; |
| 16 | |
| 17 | class OverviewStatisticsController { |
| 18 | /** @var NewslettersRepository */ |
| 19 | private $newslettersRepository; |
| 20 | |
| 21 | /** @var NewsletterStatisticsRepository */ |
| 22 | private $newsletterStatisticsRepository; |
| 23 | |
| 24 | /** @var NewsletterUrl */ |
| 25 | private $newsletterUrl; |
| 26 | |
| 27 | /** @var AutomationTimeSpanController */ |
| 28 | private $automationTimeSpanController; |
| 29 | |
| 30 | public function __construct( |
| 31 | NewslettersRepository $newslettersRepository, |
| 32 | NewsletterStatisticsRepository $newsletterStatisticsRepository, |
| 33 | NewsletterUrl $newsletterUrl, |
| 34 | AutomationTimeSpanController $automationTimeSpanController |
| 35 | ) { |
| 36 | $this->newslettersRepository = $newslettersRepository; |
| 37 | $this->newsletterStatisticsRepository = $newsletterStatisticsRepository; |
| 38 | $this->newsletterUrl = $newsletterUrl; |
| 39 | $this->automationTimeSpanController = $automationTimeSpanController; |
| 40 | } |
| 41 | |
| 42 | public function getStatisticsForAutomation(Automation $automation, QueryWithCompare $query): array { |
| 43 | $currentEmails = $this->automationTimeSpanController->getAutomationEmailsInTimeSpan($automation, $query->getAfter(), $query->getBefore(), $query->getVersionId()); |
| 44 | $previousEmails = $this->automationTimeSpanController->getAutomationEmailsInTimeSpan($automation, $query->getCompareWithAfter(), $query->getCompareWithBefore(), $query->getVersionId()); |
| 45 | $data = [ |
| 46 | 'sent' => ['current' => 0, 'previous' => 0], |
| 47 | 'opened' => ['current' => 0, 'previous' => 0], |
| 48 | 'clicked' => ['current' => 0, 'previous' => 0], |
| 49 | 'orders' => ['current' => 0, 'previous' => 0], |
| 50 | 'unsubscribed' => ['current' => 0, 'previous' => 0], |
| 51 | 'revenue' => ['current' => 0, 'previous' => 0], |
| 52 | 'emails' => [], |
| 53 | ]; |
| 54 | if (!$currentEmails) { |
| 55 | return $data; |
| 56 | } |
| 57 | |
| 58 | $requiredData = [ |
| 59 | 'totals', |
| 60 | StatisticsClickEntity::class, |
| 61 | StatisticsOpenEntity::class, |
| 62 | WooCommerceRevenue::class, |
| 63 | ]; |
| 64 | |
| 65 | $currentStatistics = $this->newsletterStatisticsRepository->getBatchStatistics( |
| 66 | $currentEmails, |
| 67 | $query->getAfter(), |
| 68 | $query->getBefore(), |
| 69 | $requiredData |
| 70 | ); |
| 71 | foreach ($currentStatistics as $newsletterId => $statistic) { |
| 72 | $data['sent']['current'] += $statistic->getTotalSentCount(); |
| 73 | $data['opened']['current'] += $statistic->getOpenCount(); |
| 74 | $data['clicked']['current'] += $statistic->getClickCount(); |
| 75 | $data['unsubscribed']['current'] += $statistic->getUnsubscribeCount(); |
| 76 | $data['orders']['current'] += $statistic->getWooCommerceRevenue() ? $statistic->getWooCommerceRevenue()->getOrdersCount() : 0; |
| 77 | $data['revenue']['current'] += $statistic->getWooCommerceRevenue() ? $statistic->getWooCommerceRevenue()->getValue() : 0; |
| 78 | $newsletter = $this->newslettersRepository->findOneById($newsletterId); |
| 79 | $data['emails'][$newsletterId]['id'] = $newsletterId; |
| 80 | $data['emails'][$newsletterId]['name'] = $newsletter ? $newsletter->getSubject() : ''; |
| 81 | $data['emails'][$newsletterId]['sent']['current'] = $statistic->getTotalSentCount(); |
| 82 | $data['emails'][$newsletterId]['sent']['previous'] = 0; |
| 83 | $data['emails'][$newsletterId]['opened'] = $statistic->getOpenCount(); |
| 84 | $data['emails'][$newsletterId]['clicked'] = $statistic->getClickCount(); |
| 85 | $data['emails'][$newsletterId]['unsubscribed'] = $statistic->getUnsubscribeCount(); |
| 86 | $data['emails'][$newsletterId]['orders'] = $statistic->getWooCommerceRevenue() ? $statistic->getWooCommerceRevenue()->getOrdersCount() : 0; |
| 87 | $data['emails'][$newsletterId]['revenue'] = $statistic->getWooCommerceRevenue() ? $statistic->getWooCommerceRevenue()->getValue() : 0; |
| 88 | $data['emails'][$newsletterId]['previewUrl'] = $newsletter ? $this->newsletterUrl->getViewInBrowserUrl($newsletter) : ''; |
| 89 | $data['emails'][$newsletterId]['order'] = count($data['emails']); |
| 90 | $data['emails'][$newsletterId]['wpPostId'] = $newsletter ? $newsletter->getWpPostId() : 0; |
| 91 | } |
| 92 | |
| 93 | $previousStatistics = $this->newsletterStatisticsRepository->getBatchStatistics( |
| 94 | $previousEmails, |
| 95 | $query->getCompareWithAfter(), |
| 96 | $query->getCompareWithBefore(), |
| 97 | $requiredData |
| 98 | ); |
| 99 | |
| 100 | foreach ($previousStatistics as $newsletterId => $statistic) { |
| 101 | $data['sent']['previous'] += $statistic->getTotalSentCount(); |
| 102 | $data['opened']['previous'] += $statistic->getOpenCount(); |
| 103 | $data['clicked']['previous'] += $statistic->getClickCount(); |
| 104 | $data['unsubscribed']['previous'] += $statistic->getUnsubscribeCount(); |
| 105 | $data['orders']['previous'] += $statistic->getWooCommerceRevenue() ? $statistic->getWooCommerceRevenue()->getOrdersCount() : 0; |
| 106 | $data['revenue']['previous'] += $statistic->getWooCommerceRevenue() ? $statistic->getWooCommerceRevenue()->getValue() : 0; |
| 107 | if (isset($data['emails'][$newsletterId])) { |
| 108 | $data['emails'][$newsletterId]['sent']['previous'] = $statistic->getTotalSentCount(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Filter emails by search if provided. |
| 113 | $search = $query->getSearch(); |
| 114 | if ($search) { |
| 115 | $data['emails'] = array_filter($data['emails'], function ($email) use ($search) { |
| 116 | return stripos($email['name'], $search) !== false; |
| 117 | }); |
| 118 | } |
| 119 | |
| 120 | usort($data['emails'], function ($a, $b) { |
| 121 | return $a['order'] <=> $b['order']; |
| 122 | }); |
| 123 | |
| 124 | return $data; |
| 125 | } |
| 126 | } |
| 127 |