MPMarketingChannel.php
2 years ago
MPMarketingChannelController.php
2 years ago
MPMarketingChannelDataController.php
2 years ago
index.php
2 years ago
MPMarketingChannelDataController.php
228 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\WooCommerce\MultichannelMarketing; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\Automation; |
| 9 | use MailPoet\Automation\Engine\Storage\AutomationStorage; |
| 10 | use MailPoet\Automation\Integrations\MailPoet\Analytics\Controller\OverviewStatisticsController; |
| 11 | use MailPoet\Automation\Integrations\MailPoet\Analytics\Entities\QueryWithCompare; |
| 12 | use MailPoet\Config\Menu; |
| 13 | use MailPoet\Newsletter\NewslettersRepository; |
| 14 | use MailPoet\Newsletter\Statistics\NewsletterStatisticsRepository; |
| 15 | use MailPoet\Newsletter\Statistics\WooCommerceRevenue; |
| 16 | use MailPoet\Services\AuthorizedEmailsController; |
| 17 | use MailPoet\Services\Bridge; |
| 18 | use MailPoet\Settings\SettingsController; |
| 19 | use MailPoet\Util\CdnAssetUrl; |
| 20 | use MailPoet\WooCommerce\Helper; |
| 21 | use MailPoetVendor\Carbon\Carbon; |
| 22 | |
| 23 | /** |
| 24 | * Created to pass data to the `MPMarketingChannel`. |
| 25 | * |
| 26 | * We create an instance of this class with the MailPoet DI ContainerInterface |
| 27 | * |
| 28 | * This provides the class access to other MailPoet services, |
| 29 | * preventing us from overloading the `MPMarketingChannelController` class with imports and duplicating efforts |
| 30 | */ |
| 31 | class MPMarketingChannelDataController { |
| 32 | |
| 33 | /** @var CdnAssetUrl */ |
| 34 | private $cdnAssetUrl; |
| 35 | |
| 36 | /** |
| 37 | * @var SettingsController |
| 38 | */ |
| 39 | private $settings; |
| 40 | |
| 41 | /** |
| 42 | * @var Bridge |
| 43 | */ |
| 44 | private $bridge; |
| 45 | |
| 46 | /** |
| 47 | * @var NewslettersRepository |
| 48 | */ |
| 49 | private $newsletterRepository; |
| 50 | |
| 51 | /** |
| 52 | * @var Helper |
| 53 | */ |
| 54 | private $woocommerceHelper; |
| 55 | |
| 56 | /** |
| 57 | * @var AutomationStorage |
| 58 | */ |
| 59 | private $automationStorage; |
| 60 | |
| 61 | /** |
| 62 | * @var NewsletterStatisticsRepository |
| 63 | */ |
| 64 | private $newsletterStatisticsRepository; |
| 65 | |
| 66 | /** |
| 67 | * @var OverviewStatisticsController |
| 68 | */ |
| 69 | private $overviewStatisticsController; |
| 70 | |
| 71 | public function __construct( |
| 72 | CdnAssetUrl $cdnAssetUrl, |
| 73 | SettingsController $settings, |
| 74 | Bridge $bridge, |
| 75 | NewslettersRepository $newsletterRepository, |
| 76 | Helper $woocommerceHelper, |
| 77 | AutomationStorage $automationStorage, |
| 78 | NewsletterStatisticsRepository $newsletterStatisticsRepository, |
| 79 | OverviewStatisticsController $overviewStatisticsController |
| 80 | ) { |
| 81 | $this->cdnAssetUrl = $cdnAssetUrl; |
| 82 | $this->settings = $settings; |
| 83 | $this->bridge = $bridge; |
| 84 | $this->newsletterRepository = $newsletterRepository; |
| 85 | $this->automationStorage = $automationStorage; |
| 86 | $this->woocommerceHelper = $woocommerceHelper; |
| 87 | $this->newsletterStatisticsRepository = $newsletterStatisticsRepository; |
| 88 | $this->overviewStatisticsController = $overviewStatisticsController; |
| 89 | } |
| 90 | |
| 91 | public function getIconUrl(): string { |
| 92 | return $this->cdnAssetUrl->generateCdnUrl('icon-white-123x128.png'); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Whether the task is completed. |
| 97 | * If the setting 'version' is not null it means the welcome wizard |
| 98 | * was already completed so we mark this task as completed as well. |
| 99 | */ |
| 100 | public function isMPSetupComplete(): bool { |
| 101 | $version = $this->settings->get('version'); |
| 102 | |
| 103 | return $version !== null; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Is MSS Enabled? |
| 108 | * @return bool |
| 109 | */ |
| 110 | public function isMailPoetSendingServiceEnabled(): bool { |
| 111 | return $this->bridge->isMailpoetSendingServiceEnabled(); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Check for error status. It's null by default when there isn't an error |
| 116 | * @return mixed |
| 117 | */ |
| 118 | public function getMailPoetSendingStatus() { |
| 119 | return $this->settings->get('mta_log.status'); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Get the number of errors available |
| 124 | * Mostly likely sending errors |
| 125 | * @return int |
| 126 | */ |
| 127 | public function getErrorCount(): int { |
| 128 | $error = $this->settings->get('mta_log.error'); |
| 129 | |
| 130 | $count = 0; |
| 131 | |
| 132 | if (!empty($error)) { |
| 133 | $count++; |
| 134 | } |
| 135 | |
| 136 | $validationError = $this->settings->get(AuthorizedEmailsController::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING); |
| 137 | |
| 138 | if ($validationError && isset($validationError['invalid_sender_address'])) { |
| 139 | $count++; |
| 140 | } |
| 141 | |
| 142 | return $count; |
| 143 | } |
| 144 | |
| 145 | public function getStandardNewsletterList($campaignType): array { |
| 146 | return $this->getNewsletterTypeLists( |
| 147 | // fetch the most recently sent post-notification history newsletters limited to ten |
| 148 | $this->newsletterRepository->getStandardNewsletterListWithMultipleStatuses(10), |
| 149 | $campaignType |
| 150 | ); |
| 151 | } |
| 152 | |
| 153 | public function getPostNotificationNewsletters($campaignType): array { |
| 154 | return $this->getNewsletterTypeLists( |
| 155 | // fetch the most recently sent post-notification history newsletters limited to ten |
| 156 | $this->newsletterRepository->getNotificationHistoryItems(10), |
| 157 | $campaignType |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | public function getAutomations($campaignType): array { |
| 162 | $result = []; |
| 163 | |
| 164 | // Fetch Automation stats within the last 90 days |
| 165 | $primaryAfter = new \DateTimeImmutable((string)Carbon::now()->subDays(90)->toISOString()); |
| 166 | $primaryBefore = new \DateTimeImmutable((string)Carbon::now()->toISOString()); |
| 167 | $now = new \DateTimeImmutable(''); |
| 168 | |
| 169 | $query = new QueryWithCompare($primaryAfter, $primaryBefore, $now, $now); |
| 170 | $userCurrency = $this->woocommerceHelper->getWoocommerceCurrency(); |
| 171 | |
| 172 | foreach ($this->automationStorage->getAutomations([Automation::STATUS_ACTIVE]) as $automation) { |
| 173 | $automationId = (string)$automation->getId(); |
| 174 | |
| 175 | $automationStatistics = $this->overviewStatisticsController->getStatisticsForAutomation($automation, $query); |
| 176 | |
| 177 | $result[] = [ |
| 178 | 'id' => $automationId, |
| 179 | 'name' => $automation->getName(), |
| 180 | 'campaignType' => $campaignType, |
| 181 | 'url' => admin_url('admin.php?page=' . Menu::AUTOMATION_ANALYTICS_PAGE_SLUG . '&id=' . $automationId), |
| 182 | 'price' => [ |
| 183 | 'amount' => isset($automationStatistics['revenue']['current']) ? $this->formatPrice($automationStatistics['revenue']['current']) : 0, |
| 184 | 'currency' => $userCurrency, |
| 185 | ], |
| 186 | ]; |
| 187 | } |
| 188 | |
| 189 | return $result; |
| 190 | } |
| 191 | |
| 192 | private function getNewsletterTypeLists($allNewsletters, $campaignType): array { |
| 193 | $result = []; |
| 194 | |
| 195 | $userCurrency = $this->woocommerceHelper->getWoocommerceCurrency(); |
| 196 | |
| 197 | // fetch the most recent newsletters limited to ten |
| 198 | foreach ($allNewsletters as $newsletter) { |
| 199 | $newsLetterId = (string)$newsletter->getId(); |
| 200 | |
| 201 | /** @var ?WooCommerceRevenue $wooRevenue */ |
| 202 | $wooRevenue = $this->newsletterStatisticsRepository->getWooCommerceRevenue($newsletter); |
| 203 | |
| 204 | $result[] = [ |
| 205 | 'id' => $newsLetterId, |
| 206 | 'name' => $newsletter->getSubject(), |
| 207 | 'campaignType' => $campaignType, |
| 208 | 'url' => admin_url('admin.php?page=' . Menu::EMAILS_PAGE_SLUG . '/#/stats/' . $newsLetterId), |
| 209 | 'price' => [ |
| 210 | 'amount' => $wooRevenue ? $this->formatPrice($wooRevenue->getValue()) : 0, |
| 211 | 'currency' => $userCurrency, |
| 212 | ], |
| 213 | ]; |
| 214 | } |
| 215 | |
| 216 | return $result; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Format amount to 2 dp |
| 221 | * @param string|int|float $amount |
| 222 | * @return string |
| 223 | */ |
| 224 | private function formatPrice($amount): string { |
| 225 | return number_format((float)$amount, 2, '.', ''); |
| 226 | } |
| 227 | } |
| 228 |