NewsletterDuplicateEndpoint.php
1 month ago
NewsletterStatusEndpoint.php
1 month ago
NewslettersBulkActionEndpoint.php
1 month ago
NewslettersListingEndpoint.php
1 month ago
SendingStatusListingEndpoint.php
1 week ago
SendingStatusResendEndpoint.php
1 month ago
index.php
1 month ago
SendingStatusListingEndpoint.php
134 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\JSON\ResponseBuilders\ScheduledTaskSubscriberResponseBuilder; |
| 9 | use MailPoet\API\REST\AbstractListingEndpoint; |
| 10 | use MailPoet\API\REST\Request; |
| 11 | use MailPoet\API\REST\Response; |
| 12 | use MailPoet\Config\AccessControl; |
| 13 | use MailPoet\Cron\CronHelper; |
| 14 | use MailPoet\Listing\Handler as ListingHandler; |
| 15 | use MailPoet\Listing\ListingDefinition; |
| 16 | use MailPoet\Listing\ListingRepository; |
| 17 | use MailPoet\Newsletter\Sending\ScheduledTaskSubscribersListingRepository; |
| 18 | use MailPoet\Newsletter\Sending\SendingQueuesRepository; |
| 19 | use MailPoet\Settings\SettingsController; |
| 20 | use MailPoet\Validator\Builder; |
| 21 | use MailPoet\WP\Functions as WPFunctions; |
| 22 | |
| 23 | /** |
| 24 | * `GET /mailpoet/v1/newsletters/{id}/sending-status` |
| 25 | * |
| 26 | * Per-subscriber send status (Sent / Failed / Unprocessed) for a single |
| 27 | * newsletter. Replaces the legacy `sending_task_subscribers` JSON listing. |
| 28 | * The response shape stays 1:1 with the other DataViews-backed listings |
| 29 | * (`items`, `meta`, `filters`, `groups`) and additionally carries the |
| 30 | * mailer / cron envelope the page uses to surface sending notices. |
| 31 | */ |
| 32 | class SendingStatusListingEndpoint extends AbstractListingEndpoint { |
| 33 | /** @var ScheduledTaskSubscribersListingRepository */ |
| 34 | private $taskSubscribersListingRepository; |
| 35 | |
| 36 | /** @var ScheduledTaskSubscriberResponseBuilder */ |
| 37 | private $responseBuilder; |
| 38 | |
| 39 | /** @var SendingQueuesRepository */ |
| 40 | private $sendingQueuesRepository; |
| 41 | |
| 42 | /** @var SettingsController */ |
| 43 | private $settings; |
| 44 | |
| 45 | /** @var CronHelper */ |
| 46 | private $cronHelper; |
| 47 | |
| 48 | /** @var WPFunctions */ |
| 49 | private $wp; |
| 50 | |
| 51 | public function __construct( |
| 52 | ListingHandler $listingHandler, |
| 53 | ScheduledTaskSubscribersListingRepository $taskSubscribersListingRepository, |
| 54 | ScheduledTaskSubscriberResponseBuilder $responseBuilder, |
| 55 | SendingQueuesRepository $sendingQueuesRepository, |
| 56 | SettingsController $settings, |
| 57 | CronHelper $cronHelper, |
| 58 | WPFunctions $wp |
| 59 | ) { |
| 60 | parent::__construct($listingHandler); |
| 61 | $this->taskSubscribersListingRepository = $taskSubscribersListingRepository; |
| 62 | $this->responseBuilder = $responseBuilder; |
| 63 | $this->sendingQueuesRepository = $sendingQueuesRepository; |
| 64 | $this->settings = $settings; |
| 65 | $this->cronHelper = $cronHelper; |
| 66 | $this->wp = $wp; |
| 67 | } |
| 68 | |
| 69 | public function checkPermissions(): bool { |
| 70 | return $this->wp->currentUserCan(AccessControl::PERMISSION_MANAGE_EMAILS); |
| 71 | } |
| 72 | |
| 73 | public function handle(Request $request): Response { |
| 74 | /** @var Response $response */ |
| 75 | $response = parent::handle($request); |
| 76 | $payload = $response->get_data(); |
| 77 | if (is_array($payload) && isset($payload['data']) && is_array($payload['data'])) { |
| 78 | $payload['data']['mta_log'] = $this->settings->get('mta_log'); |
| 79 | $payload['data']['mta_method'] = $this->settings->get('mta.method'); |
| 80 | $payload['data']['cron_accessible'] = $this->cronHelper->isDaemonAccessible(); |
| 81 | $payload['data']['current_time'] = $this->wp->currentTime('mysql'); |
| 82 | $response->set_data($payload); |
| 83 | } |
| 84 | return $response; |
| 85 | } |
| 86 | |
| 87 | public static function getRequestSchema(): array { |
| 88 | return array_merge(parent::getRequestSchema(), [ |
| 89 | 'id' => Builder::integer()->required(), |
| 90 | ]); |
| 91 | } |
| 92 | |
| 93 | protected function getListingRepository(): ListingRepository { |
| 94 | return $this->taskSubscribersListingRepository; |
| 95 | } |
| 96 | |
| 97 | protected function buildItems(array $rows, ListingDefinition $definition): array { |
| 98 | $items = $this->responseBuilder->buildForListing($rows); |
| 99 | $taskIds = array_values(array_unique(array_filter(array_column($items, 'taskId')))); |
| 100 | $timezones = $this->sendingQueuesRepository->getGroupTimezonesByTaskIds($taskIds); |
| 101 | foreach ($items as &$item) { |
| 102 | $group = $timezones[$item['taskId']] ?? null; |
| 103 | $item['timezone'] = $group ? $group['timezone'] : null; |
| 104 | $item['timezoneFallbackUsed'] = $group ? $group['fallbackUsed'] : false; |
| 105 | } |
| 106 | return $items; |
| 107 | } |
| 108 | |
| 109 | protected function getDefaultSortBy(): string { |
| 110 | return 'failed'; |
| 111 | } |
| 112 | |
| 113 | protected function getDefaultSortOrder(): string { |
| 114 | return 'desc'; |
| 115 | } |
| 116 | |
| 117 | protected function getDefaultGroup(): ?string { |
| 118 | return 'all'; |
| 119 | } |
| 120 | |
| 121 | protected function getRequestParameters(Request $request): array { |
| 122 | $idParam = $request->getParam('id'); |
| 123 | $newsletterId = is_numeric($idParam) ? (int)$idParam : 0; |
| 124 | $taskIds = $newsletterId > 0 |
| 125 | ? $this->sendingQueuesRepository->getTaskIdsByNewsletterId($newsletterId) |
| 126 | : []; |
| 127 | // The repository filters task subscribers by `task IN (:taskIds)`. When the |
| 128 | // newsletter has no sending tasks (never sent, or the per-subscriber |
| 129 | // records were cleaned up) fall back to a sentinel that matches no rows so |
| 130 | // the listing returns empty instead of every subscriber across all emails. |
| 131 | return ['task_ids' => $taskIds ?: [0]]; |
| 132 | } |
| 133 | } |
| 134 |