Editor
2 months ago
Embed
1 month ago
Links
2 months ago
Listing
9 months ago
Options
2 years ago
Preview
1 month ago
Renderer
5 days ago
RestApi
1 week ago
Scheduler
5 days ago
Segment
1 year ago
Sending
1 week ago
Sharing
1 month ago
Shortcodes
4 days ago
Statistics
1 week ago
ViewInBrowser
1 month ago
ApiDataSanitizer.php
3 years ago
AutomatedLatestContent.php
2 months ago
AutomaticEmailsRepository.php
3 years ago
BlockPostQuery.php
2 months ago
BulkActionController.php
2 months ago
BulkActionException.php
2 months ago
DynamicProducts.php
11 months ago
NewsletterCoupon.php
2 months ago
NewsletterDeleteController.php
2 years ago
NewsletterHtmlSanitizer.php
2 years ago
NewsletterPostsRepository.php
2 years ago
NewsletterResendController.php
5 days ago
NewsletterSaveController.php
1 month ago
NewsletterValidator.php
1 year ago
NewslettersRepository.php
2 weeks ago
StatusController.php
2 months ago
Url.php
2 months ago
index.php
3 years ago
StatusController.php
139 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Newsletter; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\NewsletterEntity; |
| 9 | use MailPoet\Entities\NewsletterOptionFieldEntity; |
| 10 | use MailPoet\Entities\ScheduledTaskEntity; |
| 11 | use MailPoet\Entities\SendingQueueEntity; |
| 12 | use MailPoet\InvalidStateException; |
| 13 | use MailPoet\Newsletter\Scheduler\PostNotificationScheduler; |
| 14 | use MailPoet\Newsletter\Scheduler\Scheduler; |
| 15 | use MailPoet\Services\AuthorizedEmailsController; |
| 16 | use MailPoet\Util\License\Features\Subscribers as SubscribersFeature; |
| 17 | use MailPoetVendor\Carbon\Carbon; |
| 18 | |
| 19 | /** |
| 20 | * Centralized newsletter status-toggle (active/draft) flow used by the REST |
| 21 | * endpoint at `PUT mailpoet/v1/newsletters/{id}/status`. |
| 22 | * |
| 23 | * Lifts the status change, paused-queue resume, post-notification reschedule, |
| 24 | * and authorized-sender gating into one place so the REST endpoint can stay a |
| 25 | * thin HTTP adapter. |
| 26 | */ |
| 27 | class StatusController { |
| 28 | /** @var NewslettersRepository */ |
| 29 | private $newslettersRepository; |
| 30 | |
| 31 | /** @var SubscribersFeature */ |
| 32 | private $subscribersFeature; |
| 33 | |
| 34 | /** @var AuthorizedEmailsController */ |
| 35 | private $authorizedEmailsController; |
| 36 | |
| 37 | /** @var NewsletterValidator */ |
| 38 | private $newsletterValidator; |
| 39 | |
| 40 | /** @var Scheduler */ |
| 41 | private $scheduler; |
| 42 | |
| 43 | /** @var PostNotificationScheduler */ |
| 44 | private $postNotificationScheduler; |
| 45 | |
| 46 | public function __construct( |
| 47 | NewslettersRepository $newslettersRepository, |
| 48 | SubscribersFeature $subscribersFeature, |
| 49 | AuthorizedEmailsController $authorizedEmailsController, |
| 50 | NewsletterValidator $newsletterValidator, |
| 51 | Scheduler $scheduler, |
| 52 | PostNotificationScheduler $postNotificationScheduler |
| 53 | ) { |
| 54 | $this->newslettersRepository = $newslettersRepository; |
| 55 | $this->subscribersFeature = $subscribersFeature; |
| 56 | $this->authorizedEmailsController = $authorizedEmailsController; |
| 57 | $this->newsletterValidator = $newsletterValidator; |
| 58 | $this->scheduler = $scheduler; |
| 59 | $this->postNotificationScheduler = $postNotificationScheduler; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @throws BulkActionException |
| 64 | */ |
| 65 | public function setStatus(NewsletterEntity $newsletter, string $status): NewsletterEntity { |
| 66 | if ($status === NewsletterEntity::STATUS_ACTIVE && $this->subscribersFeature->check()) { |
| 67 | throw new BulkActionException( |
| 68 | __('Subscribers limit reached.', 'mailpoet'), |
| 69 | 'mailpoet_newsletters_subscribers_limit', |
| 70 | 403 |
| 71 | ); |
| 72 | } |
| 73 | if ($status === NewsletterEntity::STATUS_ACTIVE && !$this->authorizedEmailsController->isSenderAddressValid($newsletter)) { |
| 74 | throw new BulkActionException( |
| 75 | __('The sender address is not an authorized sender domain.', 'mailpoet'), |
| 76 | 'mailpoet_newsletters_unauthorized_sender', |
| 77 | 403 |
| 78 | ); |
| 79 | } |
| 80 | if ($status === NewsletterEntity::STATUS_ACTIVE) { |
| 81 | $validationError = $this->newsletterValidator->validate($newsletter); |
| 82 | if ($validationError !== null) { |
| 83 | throw new BulkActionException( |
| 84 | $validationError, |
| 85 | 'mailpoet_newsletters_validation_failed', |
| 86 | 403 |
| 87 | ); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | $this->newslettersRepository->prefetchOptions([$newsletter]); |
| 92 | $newsletter->setStatus($status); |
| 93 | |
| 94 | if ($newsletter->getStatus() === NewsletterEntity::STATUS_ACTIVE) { |
| 95 | // Unpause any tasks that were halted by an earlier deactivation so a |
| 96 | // toggle-off/on cycle resumes rather than orphaning the queue. |
| 97 | foreach ($newsletter->getUnfinishedQueues() as $queue) { |
| 98 | $task = $queue->getTask(); |
| 99 | if ($task && $task->getStatus() === ScheduledTaskEntity::STATUS_PAUSED) { |
| 100 | $task->setStatus(ScheduledTaskEntity::STATUS_SCHEDULED); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Past-due post notifications need their next run date computed and a |
| 106 | // new sending task scheduled; otherwise re-activation would silently |
| 107 | // miss the next interval. |
| 108 | if ($newsletter->getType() === NewsletterEntity::TYPE_NOTIFICATION && $status === NewsletterEntity::STATUS_ACTIVE) { |
| 109 | $scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE); |
| 110 | if ($scheduleOption === null) { |
| 111 | throw new BulkActionException( |
| 112 | __('This email has incorrect state.', 'mailpoet'), |
| 113 | 'mailpoet_newsletters_missing_schedule', |
| 114 | 400 |
| 115 | ); |
| 116 | } |
| 117 | $nextRunDate = $this->scheduler->getNextRunDate($scheduleOption->getValue()); |
| 118 | foreach ($newsletter->getQueues() as $queue) { |
| 119 | $task = $queue->getTask(); |
| 120 | if ( |
| 121 | $task |
| 122 | && $task->getScheduledAt() <= Carbon::now()->millisecond(0) |
| 123 | && $task->getStatus() === SendingQueueEntity::STATUS_SCHEDULED |
| 124 | ) { |
| 125 | $parsedDate = $nextRunDate ? Carbon::createFromFormat('Y-m-d H:i:s', $nextRunDate) : null; |
| 126 | if ($parsedDate === false) { |
| 127 | throw InvalidStateException::create()->withMessage('Invalid next run date generated'); |
| 128 | } |
| 129 | $task->setScheduledAt($parsedDate); |
| 130 | } |
| 131 | } |
| 132 | $this->postNotificationScheduler->createPostNotificationSendingTask($newsletter); |
| 133 | } |
| 134 | |
| 135 | $this->newslettersRepository->flush(); |
| 136 | return $newsletter; |
| 137 | } |
| 138 | } |
| 139 |