AbstractSegmentsListingEndpoint.php
1 month ago
DynamicSegmentsBulkActionEndpoint.php
2 months ago
DynamicSegmentsListingEndpoint.php
1 month ago
SegmentRequestValidationTrait.php
2 months ago
SegmentsBulkActionEndpoint.php
2 months ago
SegmentsEndpoint.php
2 months ago
SegmentsListingEndpoint.php
1 month ago
index.php
2 months ago
DynamicSegmentsBulkActionEndpoint.php
235 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Segments\RestApi\Endpoints; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\API\REST\ApiException; |
| 9 | use MailPoet\API\REST\Request; |
| 10 | use MailPoet\API\REST\Response; |
| 11 | use MailPoet\Entities\SegmentEntity; |
| 12 | use MailPoet\Listing\Handler as ListingHandler; |
| 13 | use MailPoet\Newsletter\Segment\NewsletterSegmentRepository; |
| 14 | use MailPoet\Segments\DynamicSegments\DynamicSegmentsListingRepository; |
| 15 | use MailPoet\Segments\SegmentsRepository; |
| 16 | use MailPoet\Validator\Builder; |
| 17 | |
| 18 | class DynamicSegmentsBulkActionEndpoint extends SegmentsEndpoint { |
| 19 | private const ACTION_TRASH = 'trash'; |
| 20 | private const ACTION_RESTORE = 'restore'; |
| 21 | private const ACTION_DELETE = 'delete'; |
| 22 | |
| 23 | private const SUPPORTED_ACTIONS = [ |
| 24 | self::ACTION_TRASH, |
| 25 | self::ACTION_RESTORE, |
| 26 | self::ACTION_DELETE, |
| 27 | ]; |
| 28 | |
| 29 | /** @var SegmentsRepository */ |
| 30 | private $segmentsRepository; |
| 31 | |
| 32 | /** @var ListingHandler */ |
| 33 | private $listingHandler; |
| 34 | |
| 35 | /** @var DynamicSegmentsListingRepository */ |
| 36 | private $dynamicSegmentsListingRepository; |
| 37 | |
| 38 | /** @var NewsletterSegmentRepository */ |
| 39 | private $newsletterSegmentRepository; |
| 40 | |
| 41 | public function __construct( |
| 42 | ListingHandler $listingHandler, |
| 43 | DynamicSegmentsListingRepository $dynamicSegmentsListingRepository, |
| 44 | SegmentsRepository $segmentsRepository, |
| 45 | NewsletterSegmentRepository $newsletterSegmentRepository |
| 46 | ) { |
| 47 | $this->listingHandler = $listingHandler; |
| 48 | $this->dynamicSegmentsListingRepository = $dynamicSegmentsListingRepository; |
| 49 | $this->segmentsRepository = $segmentsRepository; |
| 50 | $this->newsletterSegmentRepository = $newsletterSegmentRepository; |
| 51 | } |
| 52 | |
| 53 | public function handle(Request $request): Response { |
| 54 | $action = $this->validateAction($request); |
| 55 | $this->validateGroup(is_string($request->getParam('group')) ? (string)$request->getParam('group') : null); |
| 56 | $orderParam = $request->getParam('order') ?? $request->getParam('sort_order'); |
| 57 | $this->validateOrder(is_string($orderParam) ? (string)$orderParam : null, 'desc'); |
| 58 | $this->validatePage($request->getParam('page')); |
| 59 | $this->validateOffset($request->getParam('offset')); |
| 60 | $this->validatePerPage($request->getParam('per_page') ?? $request->getParam('limit'), 25); |
| 61 | $orderbyParam = $request->getParam('orderby') ?? $request->getParam('sort_by'); |
| 62 | $orderby = is_string($orderbyParam) && $orderbyParam !== '' |
| 63 | ? (string)$orderbyParam |
| 64 | : 'updated_at'; |
| 65 | $allowedSortFields = ['name', 'created_at', 'updated_at']; |
| 66 | if (!in_array($orderby, $allowedSortFields, true)) { |
| 67 | throw new ApiException( |
| 68 | sprintf( |
| 69 | // translators: %s is the list of supported sort fields. |
| 70 | __('Unsupported sort field. Allowed values are: %s.', 'mailpoet'), |
| 71 | implode(', ', $allowedSortFields) |
| 72 | ), |
| 73 | 400, |
| 74 | 'mailpoet_segments_invalid_orderby' |
| 75 | ); |
| 76 | } |
| 77 | $ids = $this->getSelectedIds($request, $orderby, is_string($orderParam) ? strtolower($orderParam) : 'desc'); |
| 78 | $this->validateDynamicSelection($ids); |
| 79 | if ($action === self::ACTION_DELETE) { |
| 80 | $this->validateDeletionSelection($ids); |
| 81 | } |
| 82 | |
| 83 | $result = [ |
| 84 | 'updated' => 0, |
| 85 | 'deleted' => 0, |
| 86 | 'skipped' => 0, |
| 87 | 'errors' => [], |
| 88 | ]; |
| 89 | |
| 90 | if ($action === self::ACTION_TRASH) { |
| 91 | $this->trashSegments($ids, $result); |
| 92 | } elseif ($action === self::ACTION_RESTORE) { |
| 93 | $result['updated'] = $this->segmentsRepository->bulkRestore($ids, SegmentEntity::TYPE_DYNAMIC); |
| 94 | } else { |
| 95 | $result['deleted'] = $this->segmentsRepository->bulkDelete($ids, SegmentEntity::TYPE_DYNAMIC); |
| 96 | } |
| 97 | |
| 98 | return new Response($result); |
| 99 | } |
| 100 | |
| 101 | public static function getRequestSchema(): array { |
| 102 | return [ |
| 103 | 'action' => Builder::string()->required(), |
| 104 | 'ids' => Builder::array(Builder::integer()), |
| 105 | 'select_all' => Builder::boolean(), |
| 106 | 'group' => Builder::string(), |
| 107 | 'search' => Builder::string(), |
| 108 | 'page' => Builder::integer(), |
| 109 | 'per_page' => Builder::integer(), |
| 110 | 'limit' => Builder::integer(), |
| 111 | 'offset' => Builder::integer(), |
| 112 | 'orderby' => Builder::string(), |
| 113 | 'order' => Builder::string(), |
| 114 | 'sort_by' => Builder::string(), |
| 115 | 'sort_order' => Builder::string(), |
| 116 | ]; |
| 117 | } |
| 118 | |
| 119 | private function validateAction(Request $request): string { |
| 120 | $action = is_string($request->getParam('action')) ? (string)$request->getParam('action') : ''; |
| 121 | if (!in_array($action, self::SUPPORTED_ACTIONS, true)) { |
| 122 | throw new ApiException( |
| 123 | sprintf( |
| 124 | // translators: %s is the list of supported bulk actions. |
| 125 | __('Unsupported bulk action. Allowed values are: %s.', 'mailpoet'), |
| 126 | implode(', ', self::SUPPORTED_ACTIONS) |
| 127 | ), |
| 128 | 400, |
| 129 | 'mailpoet_dynamic_segments_invalid_bulk_action' |
| 130 | ); |
| 131 | } |
| 132 | return $action; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @return int[] |
| 137 | */ |
| 138 | private function getSelectedIds(Request $request, string $sortBy, string $sortOrder): array { |
| 139 | if ($request->getParam('select_all') !== true) { |
| 140 | return $this->validateIds($request->getParam('ids')); |
| 141 | } |
| 142 | |
| 143 | $definition = $this->listingHandler->getListingDefinition([ |
| 144 | 'group' => $this->validateGroup(is_string($request->getParam('group')) ? (string)$request->getParam('group') : null), |
| 145 | 'search' => is_string($request->getParam('search')) ? (string)$request->getParam('search') : null, |
| 146 | 'sort_by' => $sortBy, |
| 147 | 'sort_order' => $sortOrder, |
| 148 | 'params' => ['segments'], |
| 149 | ]); |
| 150 | $ids = $this->dynamicSegmentsListingRepository->getActionableIds($definition); |
| 151 | $ids = array_map('intval', $ids); |
| 152 | if ($ids === []) { |
| 153 | throw new ApiException( |
| 154 | __('At least one segment id is required.', 'mailpoet'), |
| 155 | 400, |
| 156 | 'mailpoet_segments_ids_required' |
| 157 | ); |
| 158 | } |
| 159 | return $ids; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * @param int[] $ids |
| 164 | * @param array{updated:int,deleted:int,skipped:int,errors:array<int, array{id:int|null,message:string}>} $result |
| 165 | */ |
| 166 | private function trashSegments(array $ids, array &$result): void { |
| 167 | $newsletterBlockers = $this->newsletterSegmentRepository->getSubjectsOfActivelyUsedEmailsForSegments($ids); |
| 168 | $trashIds = []; |
| 169 | foreach ($ids as $id) { |
| 170 | if (isset($newsletterBlockers[$id])) { |
| 171 | $segment = $this->segmentsRepository->findOneById($id); |
| 172 | $this->skip($result, $id, sprintf( |
| 173 | // translators: %1$s is the name of the segment, %2$s is a comma-separated list of emails for which the segment is used. |
| 174 | _x('Segment \'%1$s\' cannot be deleted because it’s used for \'%2$s\' email', 'Alert shown when trying to delete segment, which is assigned to any automatic emails.', 'mailpoet'), |
| 175 | $segment instanceof SegmentEntity ? $segment->getName() : (string)$id, |
| 176 | join("', '", $newsletterBlockers[$id]) |
| 177 | )); |
| 178 | continue; |
| 179 | } |
| 180 | $trashIds[] = $id; |
| 181 | } |
| 182 | $result['updated'] = $this->segmentsRepository->bulkTrash($trashIds, SegmentEntity::TYPE_DYNAMIC); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * @param int[] $ids |
| 187 | */ |
| 188 | private function validateDynamicSelection(array $ids): void { |
| 189 | foreach ($ids as $id) { |
| 190 | $segment = $this->segmentsRepository->findOneById($id); |
| 191 | if (!$segment instanceof SegmentEntity) { |
| 192 | throw new ApiException( |
| 193 | __('One or more selected dynamic segments were not found.', 'mailpoet'), |
| 194 | 400, |
| 195 | 'mailpoet_dynamic_segments_not_found' |
| 196 | ); |
| 197 | } |
| 198 | if ($segment->getType() !== SegmentEntity::TYPE_DYNAMIC) { |
| 199 | throw new ApiException( |
| 200 | __('This endpoint only supports dynamic segments.', 'mailpoet'), |
| 201 | 400, |
| 202 | 'mailpoet_dynamic_segments_invalid_type' |
| 203 | ); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * @param int[] $ids |
| 210 | */ |
| 211 | private function validateDeletionSelection(array $ids): void { |
| 212 | foreach ($ids as $id) { |
| 213 | $segment = $this->segmentsRepository->findOneById($id); |
| 214 | if ($segment instanceof SegmentEntity && $segment->getDeletedAt() === null) { |
| 215 | throw new ApiException( |
| 216 | __('Only dynamic segments in the trash can be permanently deleted.', 'mailpoet'), |
| 217 | 400, |
| 218 | 'mailpoet_dynamic_segments_delete_requires_trash' |
| 219 | ); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * @param array{updated:int,deleted:int,skipped:int,errors:array<int, array{id:int|null,message:string}>} $result |
| 226 | */ |
| 227 | private function skip(array &$result, ?int $id, string $message): void { |
| 228 | $result['skipped']++; |
| 229 | $result['errors'][] = [ |
| 230 | 'id' => $id, |
| 231 | 'message' => $message, |
| 232 | ]; |
| 233 | } |
| 234 | } |
| 235 |