Add error notice for sending newsletter with deleted list

[MAILPOET-3418]
This commit is contained in:
Rostislav Wolny
2021-03-29 15:20:33 +02:00
committed by Veljko V
parent b3da3ec1bc
commit 5331d76c79
4 changed files with 73 additions and 5 deletions

View File

@ -32,6 +32,7 @@ class SendingQueue {
public $batchSize;
const BATCH_SIZE = 20;
const TASK_BATCH_SIZE = 5;
const EMAIL_WITH_INVALID_LIST_OPTION = 'mailpoet_email_with_invalid_list';
/** @var StatsNotificationsScheduler */
public $statsNotificationsScheduler;
@ -57,6 +58,9 @@ class SendingQueue {
/** @var SegmentsRepository */
private $segmentsRepository;
/** @var WPFunctions */
private $wp;
public function __construct(
SendingErrorHandler $errorHandler,
StatsNotificationsScheduler $statsNotificationsScheduler,
@ -65,6 +69,7 @@ class SendingQueue {
CronHelper $cronHelper,
SubscribersFinder $subscriberFinder,
SegmentsRepository $segmentsRepository,
WPFunctions $wp,
$mailerTask = false,
$newsletterTask = false
) {
@ -75,7 +80,7 @@ class SendingQueue {
$this->newsletterTask = ($newsletterTask) ? $newsletterTask : new NewsletterTask();
$this->segmentsRepository = $segmentsRepository;
$this->mailerMetaInfo = new MetaInfo;
$wp = new WPFunctions;
$this->wp = $wp;
$this->batchSize = $wp->applyFilters('mailpoet_cron_worker_sending_queue_batch_size', self::BATCH_SIZE);
$this->loggerFactory = $loggerFactory;
$this->newslettersRepository = $newslettersRepository;
@ -126,6 +131,7 @@ class SendingQueue {
);
$queue->status = ScheduledTaskEntity::STATUS_PAUSED;
$queue->save();
$this->wp->setTransient(self::EMAIL_WITH_INVALID_LIST_OPTION, $newsletter->subject);
continue;
}

View File

@ -0,0 +1,41 @@
<?php
namespace MailPoet\Util\Notices;
use MailPoet\Cron\Workers\SendingQueue\SendingQueue;
use MailPoet\WP\Functions as WPFunctions;
use MailPoet\WP\Notice;
class EmailWithInvalidListNotice {
const OPTION_NAME = SendingQueue::EMAIL_WITH_INVALID_LIST_OPTION;
/** @var WPFunctions */
private $wp;
public function __construct(WPFunctions $wp) {
$this->wp = $wp;
}
public function init($shouldDisplay) {
if (!$shouldDisplay || !$this->wp->getTransient(self::OPTION_NAME)) {
return;
}
return $this->display($this->wp->getTransient(self::OPTION_NAME));
}
public function disable() {
$this->wp->deleteTransient(self::OPTION_NAME);
}
private function display($newsletterSubject) {
$notice = sprintf(
__('You are sending “%s“ to the deleted list. To continue sending, please restore the list. Alternatively, delete the newsletter if you no longer want to keep sending it.', 'mailpoet'),
$this->wp->escHtml($newsletterSubject)
);
$extraClasses = 'mailpoet-dismissible-notice is-dismissible';
Notice::displayError($notice, $extraClasses, self::OPTION_NAME, true);
return $notice;
}
}

View File

@ -35,6 +35,9 @@ class PermanentNotices {
/** @var DeprecatedShortcodeNotice */
private $deprecatedShortcodeNotice;
/** @var EmailWithInvalidListNotice */
private $emailWithInvalidListNotice;
public function __construct(WPFunctions $wp) {
$this->wp = $wp;
$this->phpVersionWarnings = new PHPVersionWarnings();
@ -45,6 +48,7 @@ class PermanentNotices {
$this->blackFridayNotice = new BlackFridayNotice();
$this->headersAlreadySentNotice = new HeadersAlreadySentNotice(SettingsController::getInstance(), $wp);
$this->deprecatedShortcodeNotice = new DeprecatedShortcodeNotice();
$this->emailWithInvalidListNotice = new EmailWithInvalidListNotice($wp);
}
public function init() {
@ -82,6 +86,9 @@ class PermanentNotices {
$this->deprecatedShortcodeNotice->init(
Menu::isOnMailPoetAdminPage($excludeWizard)
);
$this->emailWithInvalidListNotice->init(
Menu::isOnMailPoetAdminPage($exclude = null, $pageId = 'mailpoet-newsletters')
);
}
public function ajaxDismissNoticeHandler() {
@ -105,6 +112,9 @@ class PermanentNotices {
case (DeprecatedShortcodeNotice::OPTION_NAME):
$this->deprecatedShortcodeNotice->disable();
break;
case (EmailWithInvalidListNotice::OPTION_NAME):
$this->emailWithInvalidListNotice->disable();
break;
}
}
}

View File

@ -74,6 +74,8 @@ class SendingQueueTest extends \MailPoetTest {
private $subscribersFinder;
/** @var SegmentsRepository */
private $segmentsRepository;
/** @var WPFunctions */
private $wp;
public function _before() {
parent::_before();
@ -82,6 +84,7 @@ class SendingQueueTest extends \MailPoetTest {
$this->settings = $this->diContainer->get(SettingsController::class);
$populator = $this->diContainer->get(Populator::class);
$populator->up();
$this->wp = $this->diContainer->get(WPFunctions::class);
$this->subscriber = Subscriber::create();
$this->subscriber->email = 'john@doe.com';
$this->subscriber->firstName = 'John';
@ -169,7 +172,8 @@ class SendingQueueTest extends \MailPoetTest {
Stub::makeEmpty(NewslettersRepository::class),
$this->cronHelper,
$this->subscribersFinder,
$this->segmentsRepository
$this->segmentsRepository,
$this->wp
);
try {
$sendingQueueWorker->process();
@ -193,6 +197,7 @@ class SendingQueueTest extends \MailPoetTest {
$this->cronHelper,
$this->subscribersFinder,
$this->segmentsRepository,
$this->wp,
Stub::make(
new MailerTask(),
[
@ -233,6 +238,7 @@ class SendingQueueTest extends \MailPoetTest {
$this->cronHelper,
$this->subscribersFinder,
$this->segmentsRepository,
$this->wp,
Stub::make(
new MailerTask(),
[
@ -271,7 +277,8 @@ class SendingQueueTest extends \MailPoetTest {
Stub::makeEmpty(NewslettersRepository::class),
$this->cronHelper,
$this->subscribersFinder,
$this->segmentsRepository
$this->segmentsRepository,
$this->wp
);
$sendingQueueWorker->process();
}
@ -751,6 +758,7 @@ class SendingQueueTest extends \MailPoetTest {
$this->cronHelper,
$this->subscribersFinder,
$this->segmentsRepository,
$this->wp,
Stub::make(
new MailerTask(),
[
@ -855,7 +863,7 @@ class SendingQueueTest extends \MailPoetTest {
}
public function testItPauseSendingTaskThatHasTrashedSegment() {
$newsletter = $this->createNewsletter(NewsletterEntity::TYPE_STANDARD, 'Subject', NewsletterEntity::STATUS_SENDING);
$newsletter = $this->createNewsletter(NewsletterEntity::TYPE_STANDARD, 'Subject With Trashed', NewsletterEntity::STATUS_SENDING);
$queue = $this->createQueueWithTaskAndSegment($newsletter, null, ['html' => 'Hello', 'text' => 'Hello']);
$segment = $this->createSegment('Segment test', SegmentEntity::TYPE_DEFAULT);
$segment->setDeletedAt(new \DateTime());
@ -871,10 +879,11 @@ class SendingQueueTest extends \MailPoetTest {
$this->entityManager->refresh($newsletter);
expect($task->getStatus())->equals(ScheduledTaskEntity::STATUS_PAUSED);
expect($newsletter->getStatus())->equals(NewsletterEntity::STATUS_SENDING);
expect($this->wp->getTransient(SendingQueueWorker::EMAIL_WITH_INVALID_LIST_OPTION))->equals('Subject With Trashed');
}
public function testItPauseSendingTaskThatHasDeletedSegment() {
$newsletter = $this->createNewsletter(NewsletterEntity::TYPE_STANDARD, 'Subject', NewsletterEntity::STATUS_SENDING);
$newsletter = $this->createNewsletter(NewsletterEntity::TYPE_STANDARD, 'Subject With Deleted', NewsletterEntity::STATUS_SENDING);
$queue = $this->createQueueWithTaskAndSegment($newsletter, null, ['html' => 'Hello', 'text' => 'Hello']);
$segment = $this->createSegment('Segment test', SegmentEntity::TYPE_DEFAULT);
$this->addSegmentToNewsletter($newsletter, $segment);
@ -892,6 +901,7 @@ class SendingQueueTest extends \MailPoetTest {
$this->entityManager->refresh($newsletter);
expect($task->getStatus())->equals(ScheduledTaskEntity::STATUS_PAUSED);
expect($newsletter->getStatus())->equals(NewsletterEntity::STATUS_SENDING);
expect($this->wp->getTransient(SendingQueueWorker::EMAIL_WITH_INVALID_LIST_OPTION))->equals('Subject With Deleted');
}
public function _after() {
@ -962,6 +972,7 @@ class SendingQueueTest extends \MailPoetTest {
$this->cronHelper,
$this->subscribersFinder,
$this->segmentsRepository,
$this->wp,
$mailerMock
);
}