NewsletterDuplicateEndpoint.php
2 months ago
NewsletterStatusEndpoint.php
2 months ago
NewslettersBulkActionEndpoint.php
2 months ago
NewslettersListingEndpoint.php
2 months ago
SendingStatusListingEndpoint.php
1 month ago
SendingStatusResendEndpoint.php
2 months ago
index.php
2 months ago
NewslettersListingEndpoint.php
139 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\NewslettersResponseBuilder; |
| 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\Entities\NewsletterEntity; |
| 15 | use MailPoet\Listing\Handler as ListingHandler; |
| 16 | use MailPoet\Listing\ListingDefinition; |
| 17 | use MailPoet\Listing\ListingRepository; |
| 18 | use MailPoet\Newsletter\Listing\NewsletterListingRepository; |
| 19 | use MailPoet\Settings\SettingsController; |
| 20 | use MailPoet\Validator\Builder; |
| 21 | use MailPoet\WP\Functions as WPFunctions; |
| 22 | |
| 23 | /** |
| 24 | * `GET /mailpoet/v1/newsletters` |
| 25 | * |
| 26 | * Each email "tab" in the admin UI (Standard, Post Notifications, Notification |
| 27 | * History, Re-engagement) is the same underlying listing scoped by the `type` |
| 28 | * query parameter; the repository inspects the listing definition's `params` |
| 29 | * to apply the correct WHERE on `newsletters.type`. The response shape stays |
| 30 | * 1:1 with the legacy JSON endpoint at the top level (`items`, `meta`, |
| 31 | * `filters`, `groups`) and additionally carries the mailer / cron envelope |
| 32 | * fields the listing page used to read from the legacy meta. |
| 33 | */ |
| 34 | class NewslettersListingEndpoint extends AbstractListingEndpoint { |
| 35 | public const SUPPORTED_TYPES = [ |
| 36 | NewsletterEntity::TYPE_STANDARD, |
| 37 | NewsletterEntity::TYPE_NOTIFICATION, |
| 38 | NewsletterEntity::TYPE_NOTIFICATION_HISTORY, |
| 39 | NewsletterEntity::TYPE_RE_ENGAGEMENT, |
| 40 | NewsletterEntity::TYPE_WELCOME, |
| 41 | NewsletterEntity::TYPE_AUTOMATIC, |
| 42 | ]; |
| 43 | |
| 44 | /** @var NewsletterListingRepository */ |
| 45 | private $newsletterListingRepository; |
| 46 | |
| 47 | /** @var NewslettersResponseBuilder */ |
| 48 | private $newslettersResponseBuilder; |
| 49 | |
| 50 | /** @var SettingsController */ |
| 51 | private $settings; |
| 52 | |
| 53 | /** @var CronHelper */ |
| 54 | private $cronHelper; |
| 55 | |
| 56 | /** @var WPFunctions */ |
| 57 | private $wp; |
| 58 | |
| 59 | public function __construct( |
| 60 | ListingHandler $listingHandler, |
| 61 | NewsletterListingRepository $newsletterListingRepository, |
| 62 | NewslettersResponseBuilder $newslettersResponseBuilder, |
| 63 | SettingsController $settings, |
| 64 | CronHelper $cronHelper, |
| 65 | WPFunctions $wp |
| 66 | ) { |
| 67 | parent::__construct($listingHandler); |
| 68 | $this->newsletterListingRepository = $newsletterListingRepository; |
| 69 | $this->newslettersResponseBuilder = $newslettersResponseBuilder; |
| 70 | $this->settings = $settings; |
| 71 | $this->cronHelper = $cronHelper; |
| 72 | $this->wp = $wp; |
| 73 | } |
| 74 | |
| 75 | public function checkPermissions(): bool { |
| 76 | return $this->wp->currentUserCan(AccessControl::PERMISSION_MANAGE_EMAILS); |
| 77 | } |
| 78 | |
| 79 | public function handle(Request $request): Response { |
| 80 | /** @var Response $response */ |
| 81 | $response = parent::handle($request); |
| 82 | $payload = $response->get_data(); |
| 83 | if (is_array($payload) && isset($payload['data']) && is_array($payload['data'])) { |
| 84 | $payload['data']['mta_log'] = $this->settings->get('mta_log'); |
| 85 | $payload['data']['mta_method'] = $this->settings->get('mta.method'); |
| 86 | $payload['data']['cron_accessible'] = $this->cronHelper->isDaemonAccessible(); |
| 87 | $payload['data']['current_time'] = $this->wp->currentTime('mysql'); |
| 88 | $response->set_data($payload); |
| 89 | } |
| 90 | return $response; |
| 91 | } |
| 92 | |
| 93 | public static function getRequestSchema(): array { |
| 94 | return array_merge(parent::getRequestSchema(), [ |
| 95 | 'type' => Builder::string(), |
| 96 | 'parent_id' => Builder::integer(), |
| 97 | ]); |
| 98 | } |
| 99 | |
| 100 | protected function getListingRepository(): ListingRepository { |
| 101 | return $this->newsletterListingRepository; |
| 102 | } |
| 103 | |
| 104 | protected function buildItems(array $rows, ListingDefinition $definition): array { |
| 105 | $items = $this->newslettersResponseBuilder->buildForListing($rows); |
| 106 | $filteredItems = []; |
| 107 | foreach ($items as $item) { |
| 108 | $filtered = $this->wp->applyFilters('mailpoet_api_newsletters_listing_item', $item); |
| 109 | $filteredItems[] = is_array($filtered) ? $filtered : $item; |
| 110 | } |
| 111 | return $filteredItems; |
| 112 | } |
| 113 | |
| 114 | protected function getDefaultSortBy(): string { |
| 115 | return 'updated_at'; |
| 116 | } |
| 117 | |
| 118 | protected function getDefaultSortOrder(): string { |
| 119 | return 'desc'; |
| 120 | } |
| 121 | |
| 122 | protected function getDefaultGroup(): ?string { |
| 123 | return 'all'; |
| 124 | } |
| 125 | |
| 126 | protected function getRequestParameters(Request $request): array { |
| 127 | $params = []; |
| 128 | $typeParam = $request->getParam('type'); |
| 129 | if (is_string($typeParam) && in_array($typeParam, self::SUPPORTED_TYPES, true)) { |
| 130 | $params['type'] = $typeParam; |
| 131 | } |
| 132 | $parentIdParam = $request->getParam('parent_id'); |
| 133 | if (is_numeric($parentIdParam)) { |
| 134 | $params['parentId'] = (int)$parentIdParam; |
| 135 | } |
| 136 | return $params; |
| 137 | } |
| 138 | } |
| 139 |