Data
2 years ago
CapabilitiesManager.php
2 months ago
Subscribers.php
2 months ago
index.php
3 years ago
CapabilitiesManager.php
172 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Util\License\Features; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Config\ServicesChecker; |
| 9 | use MailPoet\Settings\SettingsController; |
| 10 | use MailPoet\Util\License\Features\Data\Capability; |
| 11 | |
| 12 | class CapabilitiesManager { |
| 13 | // Settings mapping |
| 14 | const MSS_TIER_SETTING_KEY = 'mta.mailpoet_api_key_state.data.tier'; |
| 15 | const MSS_MAILPOET_LOGO_IN_EMAILS_SETTING_KEY = 'mta.mailpoet_api_key_state.data.mailpoet_logo_in_emails'; |
| 16 | const MSS_DETAILED_ANALYTICS_SETTING_KEY = 'mta.mailpoet_api_key_state.data.detailed_analytics'; |
| 17 | const MSS_AUTOMATION_STEPS_SETTING_KEY = 'mta.mailpoet_api_key_state.data.automation_steps'; |
| 18 | const MSS_SEGMENT_FILTERS_SETTING_KEY = 'mta.mailpoet_api_key_state.data.segment_filters'; |
| 19 | const MSS_SEND_BY_TIMEZONE_SETTING_KEY = 'mta.mailpoet_api_key_state.data.send_by_timezone'; |
| 20 | // Product capabilities mapping |
| 21 | const MIN_TIER_LOGO_NOT_REQUIRED = 1; |
| 22 | const MIN_TIER_ANALYTICS_ENABLED = 1; |
| 23 | const MIN_TIER_NO_UPGRADE_PAGE = 2; |
| 24 | const MIN_TIER_UNLIMITED_AUTOMATION_STEPS = 2; |
| 25 | const MIN_TIER_UNLIMITED_SEGMENT_FILTERS = 2; |
| 26 | const MIN_TIER_SEND_BY_TIMEZONE_ENABLED = 1; |
| 27 | |
| 28 | private SettingsController $settings; |
| 29 | private ServicesChecker $servicesChecker; |
| 30 | private Subscribers $subscribersFeature; |
| 31 | private ?int $tier; |
| 32 | private bool $isKeyValid = false; |
| 33 | |
| 34 | /** @var array<string,Capability>|null */ |
| 35 | private ?array $capabilities = null; |
| 36 | |
| 37 | public function __construct( |
| 38 | SettingsController $settings, |
| 39 | ServicesChecker $servicesChecker, |
| 40 | Subscribers $subscribersFeature |
| 41 | ) { |
| 42 | $this->settings = $settings; |
| 43 | $this->servicesChecker = $servicesChecker; |
| 44 | $this->subscribersFeature = $subscribersFeature; |
| 45 | } |
| 46 | |
| 47 | public function getTier(): ?int { |
| 48 | $tier = $this->settings->get(self::MSS_TIER_SETTING_KEY); |
| 49 | return isset($tier) ? (int)$tier : null; |
| 50 | } |
| 51 | |
| 52 | private function isMailpoetLogoInEmailsRequired(): bool { |
| 53 | $mailpoetLogoInEmails = $this->settings->get(self::MSS_MAILPOET_LOGO_IN_EMAILS_SETTING_KEY); |
| 54 | |
| 55 | if (!isset($this->tier) && !isset($mailpoetLogoInEmails)) { |
| 56 | return !$this->servicesChecker->isUserActivelyPaying(); // Backward compatibility |
| 57 | } |
| 58 | |
| 59 | if (!$this->isKeyValid) { |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | // Allow for less restrictive individual capability to take precedence over tier |
| 64 | if (isset($mailpoetLogoInEmails) && (bool)$mailpoetLogoInEmails === false) { |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | return !isset($this->tier) || $this->tier < self::MIN_TIER_LOGO_NOT_REQUIRED; |
| 69 | } |
| 70 | |
| 71 | private function isDetailedAnalyticsEnabled(): bool { |
| 72 | // Preconditions |
| 73 | if (!$this->subscribersFeature->hasValidPremiumKey() || $this->subscribersFeature->check() || !$this->servicesChecker->isPremiumPluginActive()) { |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | $detailedAnalytics = $this->settings->get(self::MSS_DETAILED_ANALYTICS_SETTING_KEY); |
| 78 | |
| 79 | if (!isset($this->tier) && !isset($detailedAnalytics)) { |
| 80 | return true; // Backward compatibility is true when preconditions have been met |
| 81 | } |
| 82 | |
| 83 | // Allow for less restrictive individual capability to take precedence |
| 84 | if (isset($detailedAnalytics) && (bool)$detailedAnalytics === true) { |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | return (isset($this->tier) && $this->tier >= self::MIN_TIER_ANALYTICS_ENABLED); |
| 89 | } |
| 90 | |
| 91 | private function isSendByTimezoneEnabled(): bool { |
| 92 | if (!$this->subscribersFeature->hasValidPremiumKey() || $this->subscribersFeature->check() || !$this->servicesChecker->isPremiumPluginActive()) { |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | $sendByTimezone = $this->settings->get(self::MSS_SEND_BY_TIMEZONE_SETTING_KEY); |
| 97 | |
| 98 | if (!isset($this->tier) && !isset($sendByTimezone)) { |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | if (isset($sendByTimezone) && (bool)$sendByTimezone === true) { |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | return (isset($this->tier) && $this->tier >= self::MIN_TIER_SEND_BY_TIMEZONE_ENABLED); |
| 107 | } |
| 108 | |
| 109 | private function getLimit(string $settingKey, int $minTierForUnlimited): int { |
| 110 | $capabilityValue = $this->settings->get($settingKey); |
| 111 | |
| 112 | if (!isset($this->tier) && !isset($capabilityValue)) { |
| 113 | return 0; // Backward compatibility |
| 114 | } |
| 115 | |
| 116 | $limitFromTier = isset($this->tier) && $this->tier >= $minTierForUnlimited ? 0 : 1; // 0 is unlimited |
| 117 | |
| 118 | if ($limitFromTier === 0) { |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | // Allow for less restrictive individual capability to take precedence |
| 123 | return (isset($capabilityValue) && ((int)$capabilityValue === 0 || (int)$capabilityValue > $limitFromTier)) ? (int)$capabilityValue : $limitFromTier; |
| 124 | } |
| 125 | |
| 126 | private function getAutomationStepsLimit(): int { |
| 127 | return $this->getLimit(self::MSS_AUTOMATION_STEPS_SETTING_KEY, self::MIN_TIER_UNLIMITED_AUTOMATION_STEPS); |
| 128 | } |
| 129 | |
| 130 | private function getSegmentFiltersLimit(): int { |
| 131 | return $this->getLimit(self::MSS_SEGMENT_FILTERS_SETTING_KEY, self::MIN_TIER_UNLIMITED_SEGMENT_FILTERS); |
| 132 | } |
| 133 | |
| 134 | public function initCapabilities(): void { |
| 135 | $this->tier = $this->getTier(); |
| 136 | $isPremiumKeyValid = $this->servicesChecker->isPremiumKeyValid(false); |
| 137 | $this->isKeyValid = $isPremiumKeyValid || $this->servicesChecker->isMailPoetAPIKeyValid(false); |
| 138 | $automationSteps = $this->getAutomationStepsLimit(); |
| 139 | $segmentFilters = $this->getSegmentFiltersLimit(); |
| 140 | $this->capabilities = [ |
| 141 | 'mailpoetLogoInEmails' => new Capability('mailpoetLogoInEmails', Capability::TYPE_BOOLEAN, $this->isMailpoetLogoInEmailsRequired()), |
| 142 | 'detailedAnalytics' => new Capability('detailedAnalytics', Capability::TYPE_BOOLEAN, !$this->isDetailedAnalyticsEnabled()), |
| 143 | 'sendByTimezone' => new Capability('sendByTimezone', Capability::TYPE_BOOLEAN, !$this->isSendByTimezoneEnabled()), |
| 144 | 'automationSteps' => new Capability('automationSteps', Capability::TYPE_NUMBER, $automationSteps > 0, $automationSteps), |
| 145 | 'segmentFilters' => new Capability('segmentFilters', Capability::TYPE_NUMBER, $segmentFilters > 0, $segmentFilters), |
| 146 | ]; |
| 147 | } |
| 148 | |
| 149 | /** @return array<string,Capability> */ |
| 150 | public function getCapabilities(): array { |
| 151 | if ($this->capabilities === null) { |
| 152 | $this->initCapabilities(); |
| 153 | } |
| 154 | return $this->capabilities ?? []; |
| 155 | } |
| 156 | |
| 157 | public function getCapability(string $name): ?Capability { |
| 158 | return $this->getCapabilities()[$name] ?? null; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Returns true if there is no valid premium key or the product tier can be upgraded |
| 163 | */ |
| 164 | public function showUpgradePage(): bool { |
| 165 | $tier = $this->getTier(); |
| 166 | if (!$this->subscribersFeature->hasValidPremiumKey() || (isset($tier) && $tier < self::MIN_TIER_NO_UPGRADE_PAGE)) { |
| 167 | return true; |
| 168 | } |
| 169 | return false; |
| 170 | } |
| 171 | } |
| 172 |