Data
2 years ago
CapabilitiesManager.php
2 months ago
Subscribers.php
2 months ago
index.php
3 years ago
Subscribers.php
192 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Util\License\Features; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Services\Bridge; |
| 9 | use MailPoet\Settings\SettingsController; |
| 10 | use MailPoet\Subscribers\SubscribersRepository; |
| 11 | use MailPoet\WP\Functions as WPFunctions; |
| 12 | |
| 13 | class Subscribers { |
| 14 | const SUBSCRIBERS_OLD_LIMIT = 2000; |
| 15 | const SUBSCRIBERS_NEW_LIMIT = 1000; |
| 16 | const NEW_LIMIT_DATE = '2019-11-00'; |
| 17 | const MSS_KEY_STATE = 'mta.mailpoet_api_key_state.state'; |
| 18 | const MSS_SUBSCRIBERS_LIMIT_SETTING_KEY = 'mta.mailpoet_api_key_state.data.site_active_subscriber_limit'; |
| 19 | const MSS_SUPPORT_SETTING_KEY = 'mta.mailpoet_api_key_state.data.support_tier'; |
| 20 | const PREMIUM_KEY_STATE = 'premium.premium_key_state.state'; |
| 21 | const PREMIUM_SUBSCRIBERS_LIMIT_SETTING_KEY = 'premium.premium_key_state.data.site_active_subscriber_limit'; |
| 22 | const MSS_EMAIL_VOLUME_LIMIT_SETTING_KEY = 'mta.mailpoet_api_key_state.data.email_volume_limit'; |
| 23 | const MSS_EMAILS_SENT_SETTING_KEY = 'mta.mailpoet_api_key_state.data.emails_sent'; |
| 24 | const PREMIUM_SUPPORT_SETTING_KEY = 'premium.premium_key_state.data.support_tier'; |
| 25 | const SUBSCRIBERS_COUNT_CACHE_KEY = 'mailpoet_subscribers_count'; |
| 26 | const SUBSCRIBERS_COUNT_CACHE_EXPIRATION_MINUTES = 60; |
| 27 | const SUBSCRIBERS_COUNT_CACHE_MIN_VALUE = 2000; |
| 28 | |
| 29 | /** @var SettingsController */ |
| 30 | private $settings; |
| 31 | |
| 32 | /** @var SubscribersRepository */ |
| 33 | private $subscribersRepository; |
| 34 | |
| 35 | /** @var WPFunctions */ |
| 36 | private $wp; |
| 37 | |
| 38 | public function __construct( |
| 39 | SettingsController $settings, |
| 40 | SubscribersRepository $subscribersRepository, |
| 41 | WPFunctions $wp |
| 42 | ) { |
| 43 | $this->settings = $settings; |
| 44 | $this->subscribersRepository = $subscribersRepository; |
| 45 | $this->wp = $wp; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Checks if the subscribers limit is reached |
| 50 | * @return bool True if subscribers limit reached or restriction set, false otherwise |
| 51 | */ |
| 52 | public function check(): bool { |
| 53 | $limit = $this->getSubscribersLimit(); |
| 54 | $subscribersCount = $this->getSubscribersCount(); |
| 55 | if ($limit && $subscribersCount > $limit) { |
| 56 | return true; |
| 57 | } |
| 58 | // We don't have data from MSS, or they might be outdated so we need to check accessibility restrictions |
| 59 | $mssStateData = $this->settings->get(Bridge::API_KEY_STATE_SETTING_NAME); |
| 60 | $restriction = $mssStateData['access_restriction'] ?? ''; |
| 61 | return $restriction === Bridge::KEY_ACCESS_SUBSCRIBERS_LIMIT; |
| 62 | } |
| 63 | |
| 64 | public function checkEmailVolumeLimitIsReached(): bool { |
| 65 | // We have data from MSS and we can determine based on the data |
| 66 | $emailVolumeLimit = $this->getEmailVolumeLimit(); |
| 67 | $emailsSent = $this->getEmailsSent(); |
| 68 | if ($emailVolumeLimit && $emailsSent > $emailVolumeLimit) { |
| 69 | return true; |
| 70 | } |
| 71 | // We don't have data from MSS, or they might be outdated so we need to check accessibility restrictions |
| 72 | $mssStateData = $this->settings->get(Bridge::API_KEY_STATE_SETTING_NAME); |
| 73 | $restriction = $mssStateData['access_restriction'] ?? ''; |
| 74 | return $restriction === Bridge::KEY_ACCESS_EMAIL_VOLUME_LIMIT; |
| 75 | } |
| 76 | |
| 77 | public function getSubscribersCount(): int { |
| 78 | $count = $this->wp->getTransient(self::SUBSCRIBERS_COUNT_CACHE_KEY); |
| 79 | if (is_numeric($count)) { |
| 80 | return (int)$count; |
| 81 | } |
| 82 | $count = $this->subscribersRepository->getTotalSubscribers(); |
| 83 | |
| 84 | // cache only when number of subscribers exceeds minimum value |
| 85 | if ($this->isSubscribersCountEnoughForCache($count)) { |
| 86 | $this->wp->setTransient(self::SUBSCRIBERS_COUNT_CACHE_KEY, $count, self::SUBSCRIBERS_COUNT_CACHE_EXPIRATION_MINUTES * 60); |
| 87 | } |
| 88 | return $count; |
| 89 | } |
| 90 | |
| 91 | public function getFreshSubscribersCount(): int { |
| 92 | return $this->subscribersRepository->getTotalSubscribers(); |
| 93 | } |
| 94 | |
| 95 | public function getSubscriberLimitForNotifications(): ?int { |
| 96 | $limit = $this->getSubscribersLimit(); |
| 97 | if (!is_numeric($limit)) { |
| 98 | return null; |
| 99 | } |
| 100 | |
| 101 | $limit = (int)$limit; |
| 102 | return $limit > 0 ? $limit : null; |
| 103 | } |
| 104 | |
| 105 | public function isSubscribersCountEnoughForCache(?int $count = null): bool { |
| 106 | if (is_null($count) && func_num_args() === 0) { |
| 107 | $count = $this->getSubscribersCount(); |
| 108 | } |
| 109 | return $count > self::SUBSCRIBERS_COUNT_CACHE_MIN_VALUE; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Returns true if key is valid or valid but underprivileged |
| 114 | * Do not use the method to check if key is valid for sending emails or premium |
| 115 | * This only means that the Bridge can authenticate the key. |
| 116 | * @return bool |
| 117 | */ |
| 118 | public function hasValidApiKey(): bool { |
| 119 | $mssState = $this->settings->get(self::MSS_KEY_STATE); |
| 120 | $premiumState = $this->settings->get(self::PREMIUM_KEY_STATE); |
| 121 | return $this->hasValidMssKey() |
| 122 | || $this->hasValidPremiumKey() |
| 123 | || $mssState === Bridge::KEY_VALID_UNDERPRIVILEGED |
| 124 | || $premiumState === Bridge::KEY_VALID_UNDERPRIVILEGED; |
| 125 | } |
| 126 | |
| 127 | public function getSubscribersLimit() { |
| 128 | if (!$this->hasValidApiKey()) { |
| 129 | return $this->getFreeSubscribersLimit(); |
| 130 | } |
| 131 | $mssState = $this->settings->get(self::MSS_KEY_STATE); |
| 132 | if (($this->hasValidMssKey() || $mssState === Bridge::KEY_VALID_UNDERPRIVILEGED) && $this->hasMssSubscribersLimit()) { |
| 133 | return $this->getMssSubscribersLimit(); |
| 134 | } |
| 135 | |
| 136 | $premiumState = $this->settings->get(self::PREMIUM_KEY_STATE); |
| 137 | if (($this->hasValidPremiumKey() || $premiumState === Bridge::KEY_VALID_UNDERPRIVILEGED) && $this->hasPremiumSubscribersLimit()) { |
| 138 | return $this->getPremiumSubscribersLimit(); |
| 139 | } |
| 140 | |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | public function getEmailVolumeLimit(): int { |
| 145 | return (int)$this->settings->get(self::MSS_EMAIL_VOLUME_LIMIT_SETTING_KEY); |
| 146 | } |
| 147 | |
| 148 | public function getEmailsSent(): int { |
| 149 | return (int)$this->settings->get(self::MSS_EMAILS_SENT_SETTING_KEY); |
| 150 | } |
| 151 | |
| 152 | public function hasValidMssKey() { |
| 153 | $state = $this->settings->get(self::MSS_KEY_STATE); |
| 154 | return $state === Bridge::KEY_VALID || $state === Bridge::KEY_EXPIRING; |
| 155 | } |
| 156 | |
| 157 | private function hasMssSubscribersLimit() { |
| 158 | return !empty($this->settings->get(self::MSS_SUBSCRIBERS_LIMIT_SETTING_KEY)); |
| 159 | } |
| 160 | |
| 161 | private function getMssSubscribersLimit() { |
| 162 | return (int)$this->settings->get(self::MSS_SUBSCRIBERS_LIMIT_SETTING_KEY); |
| 163 | } |
| 164 | |
| 165 | public function hasMssPremiumSupport() { |
| 166 | return $this->hasValidMssKey() && $this->settings->get(self::MSS_SUPPORT_SETTING_KEY) === 'premium'; |
| 167 | } |
| 168 | |
| 169 | public function hasValidPremiumKey() { |
| 170 | $state = $this->settings->get(self::PREMIUM_KEY_STATE); |
| 171 | return $state === Bridge::KEY_VALID || $state === Bridge::KEY_EXPIRING; |
| 172 | } |
| 173 | |
| 174 | private function hasPremiumSubscribersLimit() { |
| 175 | return !empty($this->settings->get(self::PREMIUM_SUBSCRIBERS_LIMIT_SETTING_KEY)); |
| 176 | } |
| 177 | |
| 178 | private function getPremiumSubscribersLimit() { |
| 179 | return (int)$this->settings->get(self::PREMIUM_SUBSCRIBERS_LIMIT_SETTING_KEY); |
| 180 | } |
| 181 | |
| 182 | public function hasPremiumSupport() { |
| 183 | return $this->hasValidPremiumKey() && $this->settings->get(self::PREMIUM_SUPPORT_SETTING_KEY) === 'premium'; |
| 184 | } |
| 185 | |
| 186 | private function getFreeSubscribersLimit() { |
| 187 | $installationTime = strtotime((string)$this->settings->get('installed_at')); |
| 188 | $oldUser = $installationTime < strtotime(self::NEW_LIMIT_DATE); |
| 189 | return $oldUser ? self::SUBSCRIBERS_OLD_LIMIT : self::SUBSCRIBERS_NEW_LIMIT; |
| 190 | } |
| 191 | } |
| 192 |