diff --git a/assets/js/src/newsletters/listings/utils.jsx b/assets/js/src/newsletters/listings/utils.jsx index 776b1b2b42..00e793a637 100644 --- a/assets/js/src/newsletters/listings/utils.jsx +++ b/assets/js/src/newsletters/listings/utils.jsx @@ -38,13 +38,13 @@ export const addStatsCTAAction = (actions) => { export const checkMailerStatus = (state) => { if (state.meta.mta_log.error && state.meta.mta_log.error.operation === 'authorization') { MailPoet.Notice.hide('mailpoet_notice_being_sent'); + if (state.meta.mta_log.error.error_message.indexOf('mailpoet-js-button-resume-sending') >= 0) { + jQuery('.mailpoet-js-error-unauthorized-emails-notice').hide(); // prevent duplicate notices + } MailPoet.Notice.error( state.meta.mta_log.error.error_message, { static: true, id: 'mailpoet_authorization_error' } ); - jQuery('.js-button-resume-sending').on('click', () => { - jQuery('[data-id="mailpoet_authorization_error"]').slideUp(); - }); } }; diff --git a/lib/Mailer/Methods/ErrorMappers/MailPoetMapper.php b/lib/Mailer/Methods/ErrorMappers/MailPoetMapper.php index a5706fcd2b..51aa2b4eda 100644 --- a/lib/Mailer/Methods/ErrorMappers/MailPoetMapper.php +++ b/lib/Mailer/Methods/ErrorMappers/MailPoetMapper.php @@ -8,6 +8,7 @@ use MailPoet\Mailer\MailerError; use MailPoet\Mailer\SubscriberError; use MailPoet\Services\Bridge\API; use MailPoet\Util\Helpers; +use MailPoet\Util\Notices\UnauthorizedEmailNotice; use MailPoet\WP\Functions as WPFunctions; class MailPoetMapper { @@ -93,21 +94,10 @@ class MailPoetMapper { } private function getUnauthorizedEmailMessage($sender) { - $email = $sender ? $sender['from_email'] : null; - $message = '

'; - $message .= sprintf(WPFunctions::get()->__('The MailPoet Sending Service did not send your latest email because the address %s is not yet authorized.', 'mailpoet'), '' . ( $email ?: WPFunctions::get()->__('Unknown address') ) . '' ); - $message .= '

'; - $message .= Helpers::replaceLinkTags( - WPFunctions::get()->__('[link]Authorize your email in your account now.[/link]', 'mailpoet'), - 'https://account.mailpoet.com/authorization', - [ - 'class' => 'button button-primary', - 'target' => '_blank', - 'rel' => 'noopener noreferrer', - ] - ); - $message .= '   '; - $message .= '

'; + $email = $sender ? $sender['from_email'] : WPFunctions::get()->__('Unknown address'); + $validationError = ['invalid_sender_address' => $email]; + $notice = new UnauthorizedEmailNotice(WPFunctions::get(), null); + $message = $notice->getMessage($validationError); return $message; } diff --git a/lib/Util/Notices/PermanentNotices.php b/lib/Util/Notices/PermanentNotices.php index 2db167f743..ab55fe8220 100644 --- a/lib/Util/Notices/PermanentNotices.php +++ b/lib/Util/Notices/PermanentNotices.php @@ -44,7 +44,7 @@ class PermanentNotices { $this->wp = $wp; $this->phpVersionWarnings = new PHPVersionWarnings(); $this->afterMigrationNotice = new AfterMigrationNotice(); - $this->unauthorizedEmailsNotice = new UnauthorizedEmailNotice(SettingsController::getInstance(), $wp); + $this->unauthorizedEmailsNotice = new UnauthorizedEmailNotice($wp, SettingsController::getInstance()); $this->unauthorizedEmailsInNewslettersNotice = new UnauthorizedEmailInNewslettersNotice(SettingsController::getInstance(), $wp); $this->inactiveSubscribersNotice = new InactiveSubscribersNotice(SettingsController::getInstance(), $wp); $this->blackFridayNotice = new BlackFridayNotice(); diff --git a/lib/Util/Notices/UnauthorizedEmailNotice.php b/lib/Util/Notices/UnauthorizedEmailNotice.php index 1709128daa..0e8c3b8581 100644 --- a/lib/Util/Notices/UnauthorizedEmailNotice.php +++ b/lib/Util/Notices/UnauthorizedEmailNotice.php @@ -12,21 +12,24 @@ class UnauthorizedEmailNotice { const OPTION_NAME = 'unauthorized-email-addresses-notice'; - /** @var SettingsController */ + /** @var SettingsController|null */ private $settings; /** @var WPFunctions */ private $wp; public function __construct( - SettingsController $settings, - WPFunctions $wp + WPFunctions $wp, + SettingsController $settings = null ) { $this->settings = $settings; $this->wp = $wp; } public function init($shouldDisplay) { + if (!$this->settings instanceof SettingsController) { + throw new \Exception('This method can only be called if SettingsController is provided'); + } $validationError = $this->settings->get(AuthorizedEmailsController::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING); if ($shouldDisplay && isset($validationError['invalid_sender_address'])) { return $this->display($validationError); @@ -34,6 +37,12 @@ class UnauthorizedEmailNotice { } public function display($validationError) { + $message = $this->getMessage($validationError); + $extraClasses = 'mailpoet-js-error-unauthorized-emails-notice'; + Notice::displayError($message, $extraClasses, self::OPTION_NAME, false, false); + } + + public function getMessage($validationError) { $message = $this->getMessageText($validationError); $message .= sprintf( '

%s   %s   %s

', @@ -41,8 +50,7 @@ class UnauthorizedEmailNotice { $this->getDifferentEmailButton(), $this->getResumeSendingButton($validationError) ); - $extraClasses = 'mailpoet-js-error-unauthorized-emails-notice'; - Notice::displayError($message, $extraClasses, self::OPTION_NAME, false, false); + return $message; } private function getMessageText($validationError) {