ConfirmationEmailTemplate
1 month ago
ImportExport
2 weeks ago
RestApi
4 days ago
Statistics
1 month ago
BulkActionController.php
4 days ago
BulkActionException.php
2 months 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
5 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
4 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
4 days ago
TrackingConsentController.php
5 days ago
index.php
3 years ago
SubscriberLimitNotificationEvaluator.php
109 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Subscribers; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Settings\SettingsController; |
| 9 | use MailPoet\Util\License\Features\Subscribers as SubscribersFeature; |
| 10 | use MailPoet\WP\Functions as WPFunctions; |
| 11 | |
| 12 | class SubscriberLimitNotificationEvaluator { |
| 13 | public const SETTINGS_KEY = 'subscriber_limit_threshold_notifications'; |
| 14 | private const THRESHOLDS = [95, 99]; |
| 15 | |
| 16 | /** @var SettingsController */ |
| 17 | private $settings; |
| 18 | |
| 19 | /** @var SubscribersFeature */ |
| 20 | private $subscribersFeature; |
| 21 | |
| 22 | /** @var SubscriberLimitNotificationMailer */ |
| 23 | private $mailer; |
| 24 | |
| 25 | /** @var WPFunctions */ |
| 26 | private $wp; |
| 27 | |
| 28 | public function __construct( |
| 29 | SettingsController $settings, |
| 30 | SubscribersFeature $subscribersFeature, |
| 31 | SubscriberLimitNotificationMailer $mailer, |
| 32 | WPFunctions $wp |
| 33 | ) { |
| 34 | $this->settings = $settings; |
| 35 | $this->subscribersFeature = $subscribersFeature; |
| 36 | $this->mailer = $mailer; |
| 37 | $this->wp = $wp; |
| 38 | } |
| 39 | |
| 40 | public function evaluate(): void { |
| 41 | $storedState = $this->settings->fetch(self::SETTINGS_KEY, []); |
| 42 | $state = $this->normalizeState($storedState); |
| 43 | $limit = $this->subscribersFeature->getSubscriberLimitForNotifications(); |
| 44 | |
| 45 | if ($limit === null) { |
| 46 | if ($storedState) { |
| 47 | $this->settings->set(self::SETTINGS_KEY, []); |
| 48 | } |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | if (($state['limit'] ?? null) !== $limit) { |
| 53 | $state = [ |
| 54 | 'limit' => $limit, |
| 55 | 'thresholds' => [], |
| 56 | ]; |
| 57 | } |
| 58 | |
| 59 | $count = $this->subscribersFeature->getFreshSubscribersCount(); |
| 60 | $stateChanged = false; |
| 61 | |
| 62 | foreach (self::THRESHOLDS as $threshold) { |
| 63 | $thresholdKey = (string)$threshold; |
| 64 | $thresholdCount = (int)ceil($limit * $threshold / 100); |
| 65 | |
| 66 | if ($count < $thresholdCount) { |
| 67 | if (isset($state['thresholds'][$thresholdKey])) { |
| 68 | unset($state['thresholds'][$thresholdKey]); |
| 69 | $stateChanged = true; |
| 70 | } |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | if (isset($state['thresholds'][$thresholdKey]['sent_at'])) { |
| 75 | continue; |
| 76 | } |
| 77 | |
| 78 | if (!$this->mailer->send($threshold, $count, $limit, $this->subscribersFeature->hasValidApiKey())) { |
| 79 | break; |
| 80 | } |
| 81 | |
| 82 | $state['thresholds'][$thresholdKey] = [ |
| 83 | 'sent_at' => $this->wp->currentTime('mysql', true), |
| 84 | 'count_at_send' => $count, |
| 85 | ]; |
| 86 | $this->settings->set(self::SETTINGS_KEY, $state); |
| 87 | $stateChanged = false; |
| 88 | } |
| 89 | |
| 90 | if ($stateChanged || $this->settings->get(self::SETTINGS_KEY, []) !== $state) { |
| 91 | $this->settings->set(self::SETTINGS_KEY, $state); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | private function normalizeState($state): array { |
| 96 | if (!is_array($state)) { |
| 97 | return []; |
| 98 | } |
| 99 | |
| 100 | $limit = isset($state['limit']) && is_numeric($state['limit']) ? (int)$state['limit'] : null; |
| 101 | $thresholds = isset($state['thresholds']) && is_array($state['thresholds']) ? $state['thresholds'] : []; |
| 102 | |
| 103 | return [ |
| 104 | 'limit' => $limit, |
| 105 | 'thresholds' => $thresholds, |
| 106 | ]; |
| 107 | } |
| 108 | } |
| 109 |