NewsletterDuplicateEndpoint.php
2 months ago
NewsletterStatusEndpoint.php
2 months ago
NewslettersBulkActionEndpoint.php
2 months ago
NewslettersListingEndpoint.php
2 months ago
SendingStatusListingEndpoint.php
3 weeks ago
SendingStatusResendEndpoint.php
2 months ago
index.php
2 months ago
SendingStatusResendEndpoint.php
125 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Newsletter\RestApi\Endpoints; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\API\REST\ApiException; |
| 9 | use MailPoet\API\REST\Endpoint; |
| 10 | use MailPoet\API\REST\Request; |
| 11 | use MailPoet\API\REST\Response; |
| 12 | use MailPoet\Config\AccessControl; |
| 13 | use MailPoet\Entities\NewsletterEntity; |
| 14 | use MailPoet\Newsletter\Sending\ScheduledTaskSubscribersRepository; |
| 15 | use MailPoet\Newsletter\Sending\SendingQueuesRepository; |
| 16 | use MailPoet\Validator\Builder; |
| 17 | use MailPoet\WP\Functions as WPFunctions; |
| 18 | |
| 19 | /** |
| 20 | * `POST /mailpoet/v1/newsletters/{id}/sending-status/resend` |
| 21 | * |
| 22 | * Resets a failed task subscriber back to unprocessed so the next cron run |
| 23 | * retries the send. Replaces the legacy `sending_task_subscribers` `resend` |
| 24 | * JSON action. |
| 25 | */ |
| 26 | class SendingStatusResendEndpoint extends Endpoint { |
| 27 | /** @var ScheduledTaskSubscribersRepository */ |
| 28 | private $scheduledTaskSubscribersRepository; |
| 29 | |
| 30 | /** @var SendingQueuesRepository */ |
| 31 | private $sendingQueuesRepository; |
| 32 | |
| 33 | /** @var WPFunctions */ |
| 34 | private $wp; |
| 35 | |
| 36 | public function __construct( |
| 37 | ScheduledTaskSubscribersRepository $scheduledTaskSubscribersRepository, |
| 38 | SendingQueuesRepository $sendingQueuesRepository, |
| 39 | WPFunctions $wp |
| 40 | ) { |
| 41 | $this->scheduledTaskSubscribersRepository = $scheduledTaskSubscribersRepository; |
| 42 | $this->sendingQueuesRepository = $sendingQueuesRepository; |
| 43 | $this->wp = $wp; |
| 44 | } |
| 45 | |
| 46 | public function checkPermissions(): bool { |
| 47 | return $this->wp->currentUserCan(AccessControl::PERMISSION_MANAGE_EMAILS); |
| 48 | } |
| 49 | |
| 50 | public function handle(Request $request): Response { |
| 51 | $newsletterIdParam = $request->getParam('id'); |
| 52 | $taskIdParam = $request->getParam('task_id'); |
| 53 | $subscriberIdParam = $request->getParam('subscriber_id'); |
| 54 | $newsletterId = is_numeric($newsletterIdParam) ? (int)$newsletterIdParam : 0; |
| 55 | $taskId = is_numeric($taskIdParam) ? (int)$taskIdParam : 0; |
| 56 | $subscriberId = is_numeric($subscriberIdParam) ? (int)$subscriberIdParam : 0; |
| 57 | |
| 58 | $taskSubscriber = $this->scheduledTaskSubscribersRepository->findOneBy([ |
| 59 | 'task' => $taskId, |
| 60 | 'subscriber' => $subscriberId, |
| 61 | 'failed' => 1, |
| 62 | ]); |
| 63 | $sendingQueue = $this->sendingQueuesRepository->findOneBy(['task' => $taskId]); |
| 64 | |
| 65 | if ( |
| 66 | !$taskSubscriber |
| 67 | || !$taskSubscriber->getTask() |
| 68 | || !$sendingQueue |
| 69 | ) { |
| 70 | throw new ApiException( |
| 71 | __('Failed sending task not found!', 'mailpoet'), |
| 72 | 404, |
| 73 | 'mailpoet_sending_status_task_not_found' |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | $newsletter = $sendingQueue->getNewsletter(); |
| 78 | if (!$newsletter) { |
| 79 | throw new ApiException( |
| 80 | __('Newsletter not found!', 'mailpoet'), |
| 81 | 404, |
| 82 | 'mailpoet_sending_status_newsletter_not_found' |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | // The task is addressed through `/newsletters/{id}/sending-status/resend`, |
| 87 | // so it must belong to the newsletter named in the route. Without this the |
| 88 | // body's `task_id` alone would decide which email is acted on, regardless |
| 89 | // of the URL. |
| 90 | if ((int)$newsletter->getId() !== $newsletterId) { |
| 91 | throw new ApiException( |
| 92 | __('Failed sending task not found!', 'mailpoet'), |
| 93 | 404, |
| 94 | 'mailpoet_sending_status_task_not_found' |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | if ($newsletter->canBeSetActive() && $newsletter->getStatus() !== NewsletterEntity::STATUS_ACTIVE) { |
| 99 | throw new ApiException( |
| 100 | // translators: This error occurs when resending a failed email message to a recipient and the associated email definition (e.g., a welcome email, an automation email) is inactive. |
| 101 | __('Failed to resend! The email is not active. Please activate it first.', 'mailpoet'), |
| 102 | 400, |
| 103 | 'mailpoet_sending_status_email_not_active' |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | $taskSubscriber->resetToUnprocessed(); |
| 108 | $taskSubscriber->getTask()->setStatus(null); |
| 109 | if (!$newsletter->canBeSetActive()) { |
| 110 | $newsletter->setStatus(NewsletterEntity::STATUS_SENDING); |
| 111 | } |
| 112 | $this->scheduledTaskSubscribersRepository->flush(); |
| 113 | |
| 114 | return new Response([]); |
| 115 | } |
| 116 | |
| 117 | public static function getRequestSchema(): array { |
| 118 | return [ |
| 119 | 'id' => Builder::integer()->required(), |
| 120 | 'task_id' => Builder::integer()->required(), |
| 121 | 'subscriber_id' => Builder::integer()->required(), |
| 122 | ]; |
| 123 | } |
| 124 | } |
| 125 |