ConfirmationEmailTemplate
1 month ago
ImportExport
2 weeks ago
RestApi
3 days ago
Statistics
1 month ago
BulkActionController.php
3 days ago
BulkActionException.php
1 month ago
BulkConfirmationEmailResender.php
2 months ago
ConfirmationEmailCustomizer.php
2 months ago
ConfirmationEmailMailer.php
2 months ago
ConfirmationEmailResolver.php
2 months ago
EngagementDataBackfiller.php
2 months ago
InactiveSubscribersController.php
4 days ago
LinkTokens.php
2 months ago
NewSubscriberNotificationMailer.php
2 months ago
RequiredCustomFieldValidator.php
2 months ago
SegmentsCountRecalculator.php
2 weeks ago
Source.php
2 months ago
SubscriberActions.php
2 months ago
SubscriberCustomFieldRepository.php
3 years ago
SubscriberIPsRepository.php
2 years ago
SubscriberLimitNotificationEvaluator.php
2 months ago
SubscriberLimitNotificationMailer.php
2 months ago
SubscriberLimitNotificationScheduler.php
2 months ago
SubscriberListingRepository.php
2 weeks ago
SubscriberPersonalDataEraser.php
2 months ago
SubscriberSaveController.php
3 days ago
SubscriberSegmentRepository.php
2 weeks ago
SubscriberSubscribeController.php
2 weeks ago
SubscriberTagRepository.php
4 years ago
SubscribersCountsController.php
2 weeks ago
SubscribersEmailCountsController.php
2 weeks ago
SubscribersRepository.php
3 days ago
TrackingConsentController.php
4 days ago
index.php
3 years ago
SubscriberLimitNotificationMailer.php
98 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Subscribers; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Config\Renderer; |
| 9 | use MailPoet\Config\ServicesChecker; |
| 10 | use MailPoet\Mailer\MailerFactory; |
| 11 | use MailPoet\Mailer\MetaInfo; |
| 12 | use MailPoet\WP\Functions as WPFunctions; |
| 13 | |
| 14 | class SubscriberLimitNotificationMailer { |
| 15 | |
| 16 | /** @var Renderer */ |
| 17 | private $renderer; |
| 18 | |
| 19 | /** @var WPFunctions */ |
| 20 | private $wp; |
| 21 | |
| 22 | /** @var MailerFactory */ |
| 23 | private $mailerFactory; |
| 24 | |
| 25 | /** @var MetaInfo */ |
| 26 | private $mailerMetaInfo; |
| 27 | |
| 28 | /** @var ServicesChecker */ |
| 29 | private $servicesChecker; |
| 30 | |
| 31 | public function __construct( |
| 32 | Renderer $renderer, |
| 33 | WPFunctions $wp, |
| 34 | MailerFactory $mailerFactory, |
| 35 | MetaInfo $mailerMetaInfo, |
| 36 | ServicesChecker $servicesChecker |
| 37 | ) { |
| 38 | $this->renderer = $renderer; |
| 39 | $this->wp = $wp; |
| 40 | $this->mailerFactory = $mailerFactory; |
| 41 | $this->mailerMetaInfo = $mailerMetaInfo; |
| 42 | $this->servicesChecker = $servicesChecker; |
| 43 | } |
| 44 | |
| 45 | public function send(int $threshold, int $count, int $limit, bool $hasValidApiKey): bool { |
| 46 | $recipient = $this->getRecipient(); |
| 47 | if ($recipient === null) { |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | $context = [ |
| 52 | 'count' => $count, |
| 53 | 'limit' => $limit, |
| 54 | 'threshold' => $threshold, |
| 55 | 'hasValidApiKey' => $hasValidApiKey, |
| 56 | 'link_upgrade' => $this->getUpgradeLink($limit, $hasValidApiKey), |
| 57 | ]; |
| 58 | |
| 59 | $newsletter = [ |
| 60 | // translators: %d is the subscriber limit threshold percentage. |
| 61 | 'subject' => sprintf(__('Your MailPoet subscriber list is at %d%% of its limit', 'mailpoet'), $threshold), |
| 62 | 'body' => [ |
| 63 | 'html' => $this->renderer->render('emails/subscriberLimitThresholdNotification.html', $context), |
| 64 | 'text' => $this->renderer->render('emails/subscriberLimitThresholdNotification.txt', $context), |
| 65 | ], |
| 66 | ]; |
| 67 | |
| 68 | try { |
| 69 | $result = $this->mailerFactory->getDefaultMailer()->send($newsletter, $recipient, [ |
| 70 | 'meta' => $this->mailerMetaInfo->getSubscriberLimitNotificationMetaInfo(), |
| 71 | ]); |
| 72 | } catch (\Exception $e) { |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | return (bool)($result['response'] ?? false); |
| 77 | } |
| 78 | |
| 79 | private function getRecipient(): ?string { |
| 80 | $recipient = $this->wp->sanitizeEmail((string)$this->wp->getOption('admin_email')); |
| 81 | if ($recipient === '' || !$this->wp->isEmail($recipient)) { |
| 82 | return null; |
| 83 | } |
| 84 | return $recipient; |
| 85 | } |
| 86 | |
| 87 | private function getUpgradeLink(int $limit, bool $hasValidApiKey): string { |
| 88 | if ($hasValidApiKey) { |
| 89 | $partialApiKey = $this->servicesChecker->generatePartialApiKey(); |
| 90 | if ($partialApiKey !== '') { |
| 91 | return 'https://account.mailpoet.com/orders/upgrade/' . $partialApiKey; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return 'https://account.mailpoet.com/?s=' . ($limit + 1); |
| 96 | } |
| 97 | } |
| 98 |