Add handling new type of error for email volume limit

[MAILPOET-4047]
This commit is contained in:
Jan Lysý
2022-02-23 11:02:31 +01:00
committed by Veljko V
parent f527a4ae2b
commit 69769697ab
5 changed files with 56 additions and 2 deletions

View File

@@ -3,13 +3,16 @@
namespace MailPoet\Mailer\Methods\ErrorMappers;
use InvalidArgumentException;
use MailPoet\Config\ServicesChecker;
use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\MailerError;
use MailPoet\Mailer\SubscriberError;
use MailPoet\Services\Bridge\API;
use MailPoet\Util\Helpers;
use MailPoet\Util\License\Features\Subscribers as SubscribersFeature;
use MailPoet\Util\Notices\UnauthorizedEmailNotice;
use MailPoet\WP\Functions as WPFunctions;
use MailPoetVendor\Carbon\Carbon;
class MailPoetMapper {
use BlacklistErrorMapperTrait;
@@ -19,6 +22,25 @@ class MailPoetMapper {
const TEMPORARY_UNAVAILABLE_RETRY_INTERVAL = 300; // seconds
/** @var ServicesChecker */
private $servicesChecker;
/** @var SubscribersFeature */
private $subscribersFeature;
/** @var WPFunctions */
private $wp;
public function __construct(
ServicesChecker $servicesChecker,
SubscribersFeature $subscribers,
WPFunctions $wp
) {
$this->servicesChecker = $servicesChecker;
$this->subscribersFeature = $subscribers;
$this->wp = $wp;
}
public function getInvalidApiKeyError() {
return new MailerError(
MailerError::OPERATION_SEND,
@@ -60,6 +82,9 @@ class MailPoetMapper {
if ($result['message'] === MailerError::MESSAGE_EMAIL_INSUFFICIENT_PRIVILEGES) {
$operation = MailerError::OPERATION_INSUFFICIENT_PRIVILEGES;
$message = $this->getInsufficientPrivilegesMessage();
} elseif ($result['message'] === MailerError::MESSAGE_EMAIL_VOLUME_LIMIT_REACHED) {
$operation = MailerError::OPERATION_EMAIL_LIMIT_REACHED;
$message = $this->getEmailVolumeLimitReachedMessage();
} elseif ($result['message'] === MailerError::MESSAGE_EMAIL_NOT_AUTHORIZED) {
$operation = MailerError::OPERATION_AUTHORIZATION;
$message = $this->getUnauthorizedEmailMessage($sender);
@@ -96,7 +121,7 @@ class MailPoetMapper {
private function getUnauthorizedEmailMessage($sender) {
$email = $sender ? $sender['from_email'] : __('Unknown address', 'mailpoet');
$validationError = ['invalid_sender_address' => $email];
$notice = new UnauthorizedEmailNotice(WPFunctions::get(), null);
$notice = new UnauthorizedEmailNotice($this->wp, null);
$message = $notice->getMessage($validationError);
return $message;
}
@@ -148,4 +173,25 @@ class MailPoetMapper {
return "{$message}<br/>";
}
private function getEmailVolumeLimitReachedMessage(): string {
$partialApiKey = $this->servicesChecker->generatePartialApiKey();
$emailVolumeLimit = $this->subscribersFeature->getEmailVolumeLimit();
$date = Carbon::now()->startOfMonth()->addMonth();
$message = sprintf(
__('You have sent more emails this month than your MailPoet plan includes (%s), and sending has been temporarily paused. To continue sending with MailPoet Sending Service please [link]upgrade your plan[/link], or wait until sending is automatically resumed on <b>%s</b>.', 'mailpoet'),
$emailVolumeLimit,
$this->wp->dateI18n(get_option('date_format'), $date->getTimestamp())
);
$message = Helpers::replaceLinkTags(
$message,
"https://account.mailpoet.com/orders/upgrade/{$partialApiKey}",
[
'target' => '_blank',
'rel' => 'noopener noreferrer',
]
);
return "{$message}<br/>";
}
}