Нет описания

ServicesChecker.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace MailPoet\Config;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\DI\ContainerWrapper;
  5. use MailPoet\Services\Bridge;
  6. use MailPoet\Settings\SettingsController;
  7. use MailPoet\Util\Helpers;
  8. use MailPoet\Util\License\Features\Subscribers as SubscribersFeature;
  9. use MailPoet\Util\License\License;
  10. use MailPoet\WP\DateTime;
  11. use MailPoet\WP\Functions as WPFunctions;
  12. use MailPoet\WP\Notice as WPNotice;
  13. class ServicesChecker {
  14. /** @var SettingsController */
  15. private $settings;
  16. /** @var SubscribersFeature */
  17. private $subscribersFeature;
  18. public function __construct() {
  19. $this->settings = SettingsController::getInstance();
  20. $this->subscribersFeature = ContainerWrapper::getInstance()->get(SubscribersFeature::class);
  21. }
  22. public function isMailPoetAPIKeyValid($displayErrorNotice = true, $forceCheck = false) {
  23. if (!$forceCheck && !Bridge::isMPSendingServiceEnabled()) {
  24. return null;
  25. }
  26. $mssKeySpecified = Bridge::isMSSKeySpecified();
  27. $mssKey = $this->settings->get(Bridge::API_KEY_STATE_SETTING_NAME);
  28. if (!$mssKeySpecified
  29. || empty($mssKey['state'])
  30. || $mssKey['state'] == Bridge::KEY_INVALID
  31. ) {
  32. if ($displayErrorNotice) {
  33. $error = '<h3>' . __('All sending is currently paused!', 'mailpoet') . '</h3>';
  34. $error .= '<p>' . __('Your key to send with MailPoet is invalid.', 'mailpoet') . '</p>';
  35. $error .= '<p><a '
  36. . ' href="https://account.mailpoet.com?s=' . ($this->subscribersFeature->getSubscribersCount() + 1) . '"'
  37. . ' class="button button-primary" '
  38. . ' target="_blank"'
  39. . '>' . __('Purchase a key', 'mailpoet') . '</a></p>';
  40. WPNotice::displayError($error, '', '', false, false);
  41. }
  42. return false;
  43. } elseif ($mssKey['state'] == Bridge::KEY_EXPIRING
  44. && !empty($mssKey['data']['expire_at'])
  45. ) {
  46. if ($displayErrorNotice) {
  47. $dateTime = new DateTime();
  48. $date = $dateTime->formatDate(strtotime($mssKey['data']['expire_at']));
  49. $error = Helpers::replaceLinkTags(
  50. WPFunctions::get()->__("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'),
  51. 'https://account.mailpoet.com?s=' . $this->subscribersFeature->getSubscribersCount(),
  52. ['target' => '_blank']
  53. );
  54. $error = sprintf($error, $date);
  55. WPNotice::displayWarning($error);
  56. }
  57. return true;
  58. } elseif ($mssKey['state'] == Bridge::KEY_VALID) {
  59. return true;
  60. }
  61. return false;
  62. }
  63. public function isPremiumKeyValid($displayErrorNotice = true) {
  64. $premiumKeySpecified = Bridge::isPremiumKeySpecified();
  65. $premiumPluginActive = License::getLicense();
  66. $premiumKey = $this->settings->get(Bridge::PREMIUM_KEY_STATE_SETTING_NAME);
  67. if (!$premiumPluginActive) {
  68. $displayErrorNotice = false;
  69. }
  70. if (!$premiumKeySpecified
  71. || empty($premiumKey['state'])
  72. || $premiumKey['state'] === Bridge::KEY_INVALID
  73. || $premiumKey['state'] === Bridge::KEY_ALREADY_USED
  74. ) {
  75. if ($displayErrorNotice) {
  76. $errorString = WPFunctions::get()->__('[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');
  77. $error = Helpers::replaceLinkTags(
  78. $errorString,
  79. 'admin.php?page=mailpoet-settings#premium',
  80. [],
  81. 'link1'
  82. );
  83. $error = Helpers::replaceLinkTags(
  84. $error,
  85. 'admin.php?page=mailpoet-premium',
  86. [],
  87. 'link2'
  88. );
  89. WPNotice::displayWarning($error);
  90. }
  91. return false;
  92. } elseif ($premiumKey['state'] === Bridge::KEY_EXPIRING
  93. && !empty($premiumKey['data']['expire_at'])
  94. ) {
  95. if ($displayErrorNotice) {
  96. $dateTime = new DateTime();
  97. $date = $dateTime->formatDate(strtotime($premiumKey['data']['expire_at']));
  98. $error = Helpers::replaceLinkTags(
  99. WPFunctions::get()->__("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'),
  100. 'https://account.mailpoet.com',
  101. ['target' => '_blank']
  102. );
  103. $error = sprintf($error, $date);
  104. WPNotice::displayWarning($error);
  105. }
  106. return true;
  107. } elseif ($premiumKey['state'] === Bridge::KEY_VALID) {
  108. return true;
  109. }
  110. return false;
  111. }
  112. public function isMailPoetAPIKeyPendingApproval(): bool {
  113. $mssActive = Bridge::isMPSendingServiceEnabled();
  114. $mssKeyValid = $this->isMailPoetAPIKeyValid();
  115. $isApproved = $this->settings->get('mta.mailpoet_api_key_state.data.is_approved');
  116. $mssKeyPendingApproval = $isApproved === false || $isApproved === 'false'; // API unfortunately saves this as a string
  117. return $mssActive && $mssKeyValid && $mssKeyPendingApproval;
  118. }
  119. }