diff --git a/mailpoet/lib/Mailer/Methods/ErrorMappers/MailPoetMapper.php b/mailpoet/lib/Mailer/Methods/ErrorMappers/MailPoetMapper.php index 48fc5008a9..d865285227 100644 --- a/mailpoet/lib/Mailer/Methods/ErrorMappers/MailPoetMapper.php +++ b/mailpoet/lib/Mailer/Methods/ErrorMappers/MailPoetMapper.php @@ -193,12 +193,21 @@ class MailPoetMapper { $partialApiKey = $this->servicesChecker->generatePartialApiKey(); $emailVolumeLimit = $this->subscribersFeature->getEmailVolumeLimit(); $date = Carbon::now()->startOfMonth()->addMonth(); - $message = sprintf( + if ($emailVolumeLimit) { + $message = sprintf( // translators: %1$s is email volume limit and %2$s the date when you can resume sending. - __('You have sent more emails this month than your MailPoet plan includes (%1$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 %2$s.', 'mailpoet'), - $emailVolumeLimit, - $this->wp->dateI18n(get_option('date_format'), $date->getTimestamp()) - ); + __('You have sent more emails this month than your MailPoet plan includes (%1$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 %2$s.', 'mailpoet'), + $emailVolumeLimit, + $this->wp->dateI18n($this->wp->getOption('date_format'), $date->getTimestamp()) + ); + } else { + $message = sprintf( + // translators: %1$s the date when you can resume sending. + __('You have sent more emails this month than your MailPoet plan includes, 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 %1$s.', 'mailpoet'), + $this->wp->dateI18n($this->wp->getOption('date_format'), $date->getTimestamp()) + ); + } + $message = Helpers::replaceLinkTags( $message, "https://account.mailpoet.com/orders/upgrade/{$partialApiKey}", diff --git a/mailpoet/tests/unit/Mailer/Methods/ErrorMappers/MailPoetMapperTest.php b/mailpoet/tests/unit/Mailer/Methods/ErrorMappers/MailPoetMapperTest.php index 95307870e5..327ad9f79d 100644 --- a/mailpoet/tests/unit/Mailer/Methods/ErrorMappers/MailPoetMapperTest.php +++ b/mailpoet/tests/unit/Mailer/Methods/ErrorMappers/MailPoetMapperTest.php @@ -213,4 +213,56 @@ class MailPoetMapperTest extends \MailPoetUnitTest { expect($error->getLevel())->equals(MailerError::LEVEL_HARD); expect($error->getMessage())->equals('Email service is temporarily not available, please try again in a few minutes.'); } + + public function testGetErrorEmailVolumeLimitWithAndWithoutKnownLimit(): void { + $apiResult = [ + 'code' => API::RESPONSE_CODE_CAN_NOT_SEND, + 'status' => API::SENDING_STATUS_SEND_ERROR, + 'message' => API::ERROR_MESSAGE_EMAIL_VOLUME_LIMIT_REACHED, + 'error' => API::ERROR_MESSAGE_EMAIL_VOLUME_LIMIT_REACHED, + ]; + + $wpFunctions = Stub::make(new WPFunctions, [ + '_x' => function ($value) { + return $value; + }, + 'getOption' => '', + 'dateI18n' => '2023-01-31', + ]); + + $subscribersWithLimit = Stub::make(Subscribers::class, [ + 'getEmailVolumeLimit' => 1000, + ]); + + $subscribersWithoutKnownLimit = Stub::make(Subscribers::class, [ + 'getEmailVolumeLimit' => 0, + ]); + + $serviceChecker = Stub::make(ServicesChecker::class, [ + 'generatePartialApiKey' => 'abc', + ]); + + // Check email volume error when the limit is known + $this->mapper = new MailPoetMapper( + $serviceChecker, + $subscribersWithLimit, + $wpFunctions + ); + $error = $this->mapper->getErrorForResult($apiResult, $this->subscribers); + expect($error)->isInstanceOf(MailerError::class); + expect($error->getOperation())->equals(MailerError::OPERATION_EMAIL_LIMIT_REACHED); + expect($error->getLevel())->equals(MailerError::LEVEL_HARD); + expect($error->getMessage())->stringContainsString('You have sent more emails this month than your MailPoet plan includes (1000),'); + expect($error->getMessage())->stringContainsString('wait until sending is automatically resumed on 2023-01-31'); + + // Check email volume error when the limit is unknown + $this->mapper = new MailPoetMapper( + $serviceChecker, + $subscribersWithoutKnownLimit, + $wpFunctions + ); + $error = $this->mapper->getErrorForResult($apiResult, $this->subscribers); + expect($error->getMessage())->stringContainsString('You have sent more emails this month than your MailPoet plan includes,'); + expect($error->getMessage())->stringContainsString('wait until sending is automatically resumed on 2023-01-31'); + } }