diff --git a/lib/Util/Notices/PermanentNotices.php b/lib/Util/Notices/PermanentNotices.php index fe55e7c1d4..01b0119a33 100644 --- a/lib/Util/Notices/PermanentNotices.php +++ b/lib/Util/Notices/PermanentNotices.php @@ -20,6 +20,9 @@ class PermanentNotices { /** @var UnauthorizedEmailNotice */ private $unauthorized_emails_notice; + /** @var UnauthorizedEmailInNewslettersNotice */ + private $unauthorized_emails_in_newsletters_notice; + /** @var WPFunctions */ private $wp; @@ -29,6 +32,7 @@ class PermanentNotices { $this->after_migration_notice = new AfterMigrationNotice(); $this->discounts_announcement = new DiscountsAnnouncement(); $this->unauthorized_emails_notice = new UnauthorizedEmailNotice(new SettingsController, $this->wp); + $this->unauthorized_emails_in_newsletters_notice = new UnauthorizedEmailInNewslettersNotice(new SettingsController, $this->wp); } public function init() { @@ -47,6 +51,9 @@ class PermanentNotices { $this->unauthorized_emails_notice->init( Menu::isOnMailPoetAdminPage($exclude = ['mailpoet-welcome-wizard']) ); + $this->unauthorized_emails_in_newsletters_notice->init( + Menu::isOnMailPoetAdminPage($exclude = null, $page_id = 'mailpoet-newsletters') + ); $this->discounts_announcement->init( empty($_GET['page']) && $this->wp->isAdmin() diff --git a/lib/Util/Notices/UnauthorizedEmailInNewslettersNotice.php b/lib/Util/Notices/UnauthorizedEmailInNewslettersNotice.php new file mode 100644 index 0000000000..4d0aaf2d1c --- /dev/null +++ b/lib/Util/Notices/UnauthorizedEmailInNewslettersNotice.php @@ -0,0 +1,85 @@ +settings = $settings; + $this->wp = $wp; + } + + function init($should_display) { + $validation_error = $this->settings->get(AuthorizedEmailsController::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING); + if ($should_display && isset($validation_error['invalid_senders_in_newsletters'])) { + return $this->display($validation_error); + } + } + + function display($validation_error) { + $message = $this->getMessageText(); + $message .= $this->getNewslettersLinks($validation_error); + $message .= $this->getAuthorizationLink($validation_error); + $message .= $this->getResumeSendingButton(); + // Use Mailer log errors display system to display this notice + $mailer_log = MailerLog::setError(MailerLog::getMailerLog(), MailerError::OPERATION_AUTHORIZATION, $message); + MailerLog::updateMailerLog($mailer_log); + } + + private function getMessageText() { + $message = $this->wp->__('Your automatic emails has been paused, because some email addresses hasn’t been authorized yet.'); + return "
$message
"; + } + + private function getNewslettersLinks($validation_error) { + $links = ''; + foreach ($validation_error['invalid_senders_in_newsletters'] as $error) { + $link_text = $this->wp->_x('Update the from address of %subject', '%subject will be replaced by a newsletter subject'); + $link_text = str_replace('%subject', EscapeHelper::escapeHtmlText($error['subject']), $link_text); + $link_url = $this->wp->adminUrl('admin.php?page=' . Menu::MAIN_PAGE_SLUG . '#/send/' . $error['newsletter_id']); + $link = Helpers::replaceLinkTags("[link]{$link_text}[/link]", $link_url, ['target' => '_blank']); + $links .= "$link
"; + } + return $links; + } + + private function getAuthorizationLink($validation_error) { + $emails = array_unique(array_column($validation_error['invalid_senders_in_newsletters'], 'sender_address')); + if (count($emails) > 1) { + $authorize_link = $this->wp->_x('Authorize %email1 and %email2', 'Link for user to authorize their email address'); + $authorize_link = str_replace('%email2', EscapeHelper::escapeHtmlText(array_pop($emails)), $authorize_link); + $authorize_link = str_replace('%email1', EscapeHelper::escapeHtmlText(implode(', ', $emails)), $authorize_link); + } else { + $authorize_link = $this->wp->_x('Authorize %email', 'Link for user to authorize their email address'); + $authorize_link = str_replace('%email', EscapeHelper::escapeHtmlText($emails[0]), $authorize_link); + } + + $authorize_link = Helpers::replaceLinkTags("[link]{$authorize_link}[/link]", 'https://account.mailpoet.com/authorization', ['target' => '_blank']); + $html = '' . $this->wp->_x('OR', 'User has to choose between two options') . '
'; + $html .= "$authorize_link
"; + return $html; + } + + private function getResumeSendingButton() { + $button = ''; + return "$button
"; + } +}