PopulatorData
2 months ago
AccessControl.php
2 years ago
Activator.php
2 months ago
AssetsLoader.php
2 weeks ago
Capabilities.php
2 months ago
Changelog.php
2 months ago
DeactivationPoll.php
3 years ago
DeferredAdminNotices.php
2 months ago
Env.php
6 months ago
Hooks.php
4 weeks ago
HooksWooCommerce.php
1 month ago
Initializer.php
4 weeks ago
Installer.php
10 months ago
Localizer.php
3 years ago
Menu.php
1 month ago
PersonalDataErasers.php
1 month ago
PersonalDataExporters.php
1 month ago
PluginActivatedHook.php
3 years ago
Populator.php
2 weeks ago
PrivacyPolicy.php
1 month ago
Renderer.php
1 year ago
RendererFactory.php
3 years ago
RequirementsChecker.php
2 months ago
Router.php
2 months ago
ServicesChecker.php
3 years ago
Shortcodes.php
1 month ago
SilentUpgraderSkin.php
3 years ago
SubscriberChangesNotifier.php
2 months ago
TranslationUpdater.php
3 months ago
TwigEnvironment.php
1 year ago
TwigFileSystemCache.php
3 years ago
Updater.php
4 days ago
index.php
3 years ago
ServicesChecker.php
199 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Config; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\DI\ContainerWrapper; |
| 9 | use MailPoet\Services\Bridge; |
| 10 | use MailPoet\Settings\SettingsController; |
| 11 | use MailPoet\Util\Helpers; |
| 12 | use MailPoet\Util\License\Features\Subscribers as SubscribersFeature; |
| 13 | use MailPoet\Util\License\License; |
| 14 | use MailPoet\WP\DateTime; |
| 15 | use MailPoet\WP\Notice as WPNotice; |
| 16 | |
| 17 | class ServicesChecker { |
| 18 | |
| 19 | /** @var SettingsController */ |
| 20 | private $settings; |
| 21 | |
| 22 | /** @var SubscribersFeature */ |
| 23 | private $subscribersFeature; |
| 24 | |
| 25 | public function __construct() { |
| 26 | $this->settings = SettingsController::getInstance(); |
| 27 | $this->subscribersFeature = ContainerWrapper::getInstance()->get(SubscribersFeature::class); |
| 28 | } |
| 29 | |
| 30 | public function isPremiumPluginActive() { |
| 31 | return License::getLicense() ? true : false; |
| 32 | } |
| 33 | |
| 34 | public function isMailPoetAPIKeyValid($displayErrorNotice = true, $forceCheck = false) { |
| 35 | if (!$forceCheck && !Bridge::isMPSendingServiceEnabled()) { |
| 36 | return null; |
| 37 | } |
| 38 | |
| 39 | $mssKeySpecified = Bridge::isMSSKeySpecified(); |
| 40 | $mssKey = $this->settings->get(Bridge::API_KEY_STATE_SETTING_NAME); |
| 41 | |
| 42 | if ( |
| 43 | !$mssKeySpecified |
| 44 | || empty($mssKey['state']) |
| 45 | || $mssKey['state'] == Bridge::KEY_INVALID |
| 46 | ) { |
| 47 | if ($displayErrorNotice) { |
| 48 | $error = '<h3>' . __('All sending is currently paused!', 'mailpoet') . '</h3>'; |
| 49 | $error .= '<p>' . __('Your key to send with MailPoet is invalid.', 'mailpoet') . '</p>'; |
| 50 | $error .= '<p><a ' |
| 51 | . ' href="https://account.mailpoet.com?s=' . ($this->subscribersFeature->getSubscribersCount() + 1) . '"' |
| 52 | . ' class="button button-primary" ' |
| 53 | . ' target="_blank"' |
| 54 | . '>' . __('Purchase a key', 'mailpoet') . '</a></p>'; |
| 55 | |
| 56 | WPNotice::displayError($error, '', '', false, false); |
| 57 | } |
| 58 | return false; |
| 59 | } elseif ( |
| 60 | $mssKey['state'] == Bridge::KEY_EXPIRING |
| 61 | && !empty($mssKey['data']['expire_at']) |
| 62 | ) { |
| 63 | if ($displayErrorNotice) { |
| 64 | $dateTime = new DateTime(); |
| 65 | $date = $dateTime->formatDate(strtotime($mssKey['data']['expire_at'])); |
| 66 | $error = Helpers::replaceLinkTags( |
| 67 | // translators: %s is a date. |
| 68 | __("Your newsletters are awesome! Don't forget to [link]upgrade your MailPoet email plan[/link] by %s to keep sending them to your subscribers.", 'mailpoet'), |
| 69 | 'https://account.mailpoet.com?s=' . $this->subscribersFeature->getSubscribersCount(), |
| 70 | ['target' => '_blank'] |
| 71 | ); |
| 72 | $error = sprintf($error, $date); |
| 73 | WPNotice::displayWarning($error); |
| 74 | } |
| 75 | return true; |
| 76 | } elseif ($mssKey['state'] == Bridge::KEY_VALID) { |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | public function isPremiumKeyValid($displayErrorNotice = true) { |
| 84 | $premiumKeySpecified = Bridge::isPremiumKeySpecified(); |
| 85 | $premiumPluginActive = License::getLicense(); |
| 86 | $premiumKey = $this->settings->get(Bridge::PREMIUM_KEY_STATE_SETTING_NAME); |
| 87 | |
| 88 | if (!$premiumPluginActive) { |
| 89 | $displayErrorNotice = false; |
| 90 | } |
| 91 | |
| 92 | if ( |
| 93 | !$premiumKeySpecified |
| 94 | || empty($premiumKey['state']) |
| 95 | || $premiumKey['state'] === Bridge::KEY_INVALID |
| 96 | || $premiumKey['state'] === Bridge::KEY_ALREADY_USED |
| 97 | ) { |
| 98 | if ($displayErrorNotice) { |
| 99 | $errorString = __('[link1]Register[/link1] your copy of the MailPoet Premium plugin to receive access to automatic upgrades and support. Need a license key? [link2]Purchase one now.[/link2]', 'mailpoet'); |
| 100 | $error = Helpers::replaceLinkTags( |
| 101 | $errorString, |
| 102 | 'admin.php?page=mailpoet-settings#premium', |
| 103 | [], |
| 104 | 'link1' |
| 105 | ); |
| 106 | $error = Helpers::replaceLinkTags( |
| 107 | $error, |
| 108 | 'admin.php?page=mailpoet-upgrade', |
| 109 | [], |
| 110 | 'link2' |
| 111 | ); |
| 112 | WPNotice::displayWarning($error); |
| 113 | } |
| 114 | return false; |
| 115 | } elseif ( |
| 116 | $premiumKey['state'] === Bridge::KEY_EXPIRING |
| 117 | && !empty($premiumKey['data']['expire_at']) |
| 118 | ) { |
| 119 | if ($displayErrorNotice) { |
| 120 | $dateTime = new DateTime(); |
| 121 | $date = $dateTime->formatDate(strtotime($premiumKey['data']['expire_at'])); |
| 122 | $error = Helpers::replaceLinkTags( |
| 123 | // translators: %s is a date. |
| 124 | __("Your License Key for MailPoet is expiring! Don't forget to [link]renew your license[/link] by %s to keep enjoying automatic updates and Premium support.", 'mailpoet'), |
| 125 | 'https://account.mailpoet.com', |
| 126 | ['target' => '_blank'] |
| 127 | ); |
| 128 | $error = sprintf($error, $date); |
| 129 | WPNotice::displayWarning($error); |
| 130 | } |
| 131 | return true; |
| 132 | } elseif ($premiumKey['state'] === Bridge::KEY_VALID) { |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | public function isBundledSubscription(): bool { |
| 140 | $subscriptionType = $this->settings->get(Bridge::SUBSCRIPTION_TYPE_SETTING_NAME); |
| 141 | return $subscriptionType === Bridge::WPCOM_BUNDLE_SUBSCRIPTION_TYPE; |
| 142 | } |
| 143 | |
| 144 | public function isMailPoetAPIKeyPendingApproval(): bool { |
| 145 | $mssActive = Bridge::isMPSendingServiceEnabled(); |
| 146 | $mssKeyValid = $this->isMailPoetAPIKeyValid(); |
| 147 | $isApproved = $this->settings->get('mta.mailpoet_api_key_state.data.is_approved'); |
| 148 | $mssKeyPendingApproval = $isApproved === false || $isApproved === 'false'; // API unfortunately saves this as a string |
| 149 | return $mssActive && $mssKeyValid && $mssKeyPendingApproval; |
| 150 | } |
| 151 | |
| 152 | public function isUserActivelyPaying(): bool { |
| 153 | $isPremiumKeyValid = $this->isPremiumKeyValid(false); |
| 154 | |
| 155 | $mssActive = Bridge::isMPSendingServiceEnabled(); |
| 156 | $isMssKeyValid = $this->isMailPoetAPIKeyValid(false); |
| 157 | |
| 158 | if (!$mssActive || ($isPremiumKeyValid && !$isMssKeyValid)) { |
| 159 | return $this->subscribersFeature->hasPremiumSupport(); |
| 160 | } else { |
| 161 | return $this->subscribersFeature->hasMssPremiumSupport(); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Return a key when it can be used for account administration purposes (stats report, auth. addresses or domains administration) |
| 167 | * Key can be used when it is valid for MSS or Premium, but also when it is valid but has no privileges for MSS or Premium (API returns 403). |
| 168 | */ |
| 169 | public function getValidAccountKey(): ?string { |
| 170 | if ($this->isMailPoetAPIKeyValid(false, true)) { |
| 171 | return $this->settings->get(Bridge::API_KEY_SETTING_NAME); |
| 172 | } |
| 173 | $mssKeyState = $this->settings->get(Bridge::API_KEY_STATE_SETTING_NAME); |
| 174 | if (($mssKeyState['state'] ?? null) === Bridge::KEY_VALID_UNDERPRIVILEGED) { |
| 175 | return $this->settings->get(Bridge::API_KEY_SETTING_NAME); |
| 176 | } |
| 177 | |
| 178 | if ($this->isPremiumKeyValid(false)) { |
| 179 | return $this->settings->get(Bridge::PREMIUM_KEY_SETTING_NAME); |
| 180 | } |
| 181 | $premiumKeyState = $this->settings->get(Bridge::PREMIUM_KEY_STATE_SETTING_NAME); |
| 182 | if (($premiumKeyState['state'] ?? null) === Bridge::KEY_VALID_UNDERPRIVILEGED) { |
| 183 | return $this->settings->get(Bridge::PREMIUM_KEY_SETTING_NAME); |
| 184 | } |
| 185 | |
| 186 | return null; |
| 187 | } |
| 188 | |
| 189 | public function generatePartialApiKey(): string { |
| 190 | $key = (string)($this->getValidAccountKey()); |
| 191 | if ($key) { |
| 192 | $halfKeyLength = (int)(strlen($key) / 2); |
| 193 | |
| 194 | return substr($key, 0, $halfKeyLength); |
| 195 | } |
| 196 | return ''; |
| 197 | } |
| 198 | } |
| 199 |