SubscriberConfirmationEmailEndpoint.php
2 months ago
SubscribersBulkActionEndpoint.php
4 days ago
SubscribersListingEndpoint.php
2 weeks ago
index.php
2 months ago
SubscribersBulkActionEndpoint.php
266 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Subscribers\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\SubscriberEntity; |
| 14 | use MailPoet\Listing\Handler as ListingHandler; |
| 15 | use MailPoet\Listing\ListingDefinition; |
| 16 | use MailPoet\Subscribers\BulkActionController; |
| 17 | use MailPoet\Subscribers\BulkActionException; |
| 18 | use MailPoet\Subscribers\BulkConfirmationEmailResender; |
| 19 | use MailPoet\Validator\Builder; |
| 20 | use MailPoet\WP\Functions as WPFunctions; |
| 21 | |
| 22 | class SubscribersBulkActionEndpoint extends Endpoint { |
| 23 | public const ACTION_RESEND_CONFIRMATION_EMAILS = 'resendConfirmationEmails'; |
| 24 | |
| 25 | private const VALID_SELECT_ALL_GROUPS = [ |
| 26 | 'all', |
| 27 | SubscriberEntity::STATUS_SUBSCRIBED, |
| 28 | SubscriberEntity::STATUS_UNCONFIRMED, |
| 29 | SubscriberEntity::STATUS_UNSUBSCRIBED, |
| 30 | SubscriberEntity::STATUS_INACTIVE, |
| 31 | SubscriberEntity::STATUS_BOUNCED, |
| 32 | 'trash', |
| 33 | ]; |
| 34 | |
| 35 | private const TRASH_ONLY_ACTIONS = [ |
| 36 | BulkActionController::ACTION_DELETE, |
| 37 | BulkActionController::ACTION_RESTORE, |
| 38 | ]; |
| 39 | |
| 40 | private const NON_TRASH_ACTIONS = [ |
| 41 | BulkActionController::ACTION_TRASH, |
| 42 | BulkActionController::ACTION_UNSUBSCRIBE, |
| 43 | BulkActionController::ACTION_MOVE_TO_LIST, |
| 44 | BulkActionController::ACTION_ADD_TO_LIST, |
| 45 | BulkActionController::ACTION_REMOVE_FROM_LIST, |
| 46 | BulkActionController::ACTION_REMOVE_FROM_ALL_LISTS, |
| 47 | BulkActionController::ACTION_ADD_TAG, |
| 48 | BulkActionController::ACTION_REMOVE_TAG, |
| 49 | ]; |
| 50 | |
| 51 | /** @var ListingHandler */ |
| 52 | private $listingHandler; |
| 53 | |
| 54 | /** @var BulkActionController */ |
| 55 | private $bulkActionController; |
| 56 | |
| 57 | /** @var BulkConfirmationEmailResender */ |
| 58 | private $bulkConfirmationEmailResender; |
| 59 | |
| 60 | /** @var WPFunctions */ |
| 61 | private $wp; |
| 62 | |
| 63 | public function __construct( |
| 64 | ListingHandler $listingHandler, |
| 65 | BulkActionController $bulkActionController, |
| 66 | BulkConfirmationEmailResender $bulkConfirmationEmailResender, |
| 67 | WPFunctions $wp |
| 68 | ) { |
| 69 | $this->listingHandler = $listingHandler; |
| 70 | $this->bulkActionController = $bulkActionController; |
| 71 | $this->bulkConfirmationEmailResender = $bulkConfirmationEmailResender; |
| 72 | $this->wp = $wp; |
| 73 | } |
| 74 | |
| 75 | public function checkPermissions(): bool { |
| 76 | return $this->wp->currentUserCan(AccessControl::PERMISSION_MANAGE_SUBSCRIBERS); |
| 77 | } |
| 78 | |
| 79 | public function handle(Request $request): Response { |
| 80 | $actionParam = $request->getParam('action'); |
| 81 | $action = is_string($actionParam) ? $actionParam : ''; |
| 82 | $definition = $this->buildDefinition($request); |
| 83 | |
| 84 | if ($action === self::ACTION_RESEND_CONFIRMATION_EMAILS) { |
| 85 | return $this->handleResendConfirmation($request, $definition); |
| 86 | } |
| 87 | |
| 88 | $selectAll = $request->getParam('select_all') === true; |
| 89 | $selectionParam = $request->getParam('selection'); |
| 90 | $hasSelection = is_array($selectionParam) && $selectionParam !== []; |
| 91 | if (!$hasSelection && !$selectAll) { |
| 92 | throw new ApiException( |
| 93 | __('No subscribers selected.', 'mailpoet'), |
| 94 | 400, |
| 95 | 'mailpoet_subscribers_no_selection' |
| 96 | ); |
| 97 | } |
| 98 | if ($selectAll) { |
| 99 | $this->validateSelectAllScope($action, $definition); |
| 100 | } |
| 101 | |
| 102 | $data = [ |
| 103 | 'trigger_automations' => $request->getParam('trigger_automations') === true, |
| 104 | ]; |
| 105 | $segmentIdParam = $request->getParam('segment_id'); |
| 106 | if (is_numeric($segmentIdParam)) { |
| 107 | $data['segment_id'] = (int)$segmentIdParam; |
| 108 | } |
| 109 | $tagIdParam = $request->getParam('tag_id'); |
| 110 | if (is_numeric($tagIdParam)) { |
| 111 | $data['tag_id'] = (int)$tagIdParam; |
| 112 | } |
| 113 | |
| 114 | try { |
| 115 | $result = $this->bulkActionController->execute($action, $definition, $data); |
| 116 | } catch (BulkActionException $exception) { |
| 117 | throw new ApiException( |
| 118 | $exception->getMessage(), |
| 119 | $exception->getStatusCode(), |
| 120 | $exception->getErrorCode() |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | return new Response([ |
| 125 | 'action' => $action, |
| 126 | 'count' => $result['count'], |
| 127 | 'kept' => $result['kept'] ?? 0, |
| 128 | 'segment' => $result['segment'] ?? null, |
| 129 | 'tag' => $result['tag'] ?? null, |
| 130 | ]); |
| 131 | } |
| 132 | |
| 133 | public static function getRequestSchema(): array { |
| 134 | return [ |
| 135 | 'action' => Builder::string()->required(), |
| 136 | 'selection' => Builder::array(Builder::integer()), |
| 137 | 'select_all' => Builder::boolean(), |
| 138 | 'group' => Builder::string(), |
| 139 | 'search' => Builder::string(), |
| 140 | 'filter' => Builder::object(), |
| 141 | 'segment_id' => Builder::integer(), |
| 142 | 'tag_id' => Builder::integer(), |
| 143 | 'trigger_automations' => Builder::boolean(), |
| 144 | ]; |
| 145 | } |
| 146 | |
| 147 | private function handleResendConfirmation(Request $request, ListingDefinition $definition): Response { |
| 148 | if (!$this->bulkConfirmationEmailResender->canCurrentUserResend()) { |
| 149 | throw new ApiException( |
| 150 | __('You do not have permission to resend confirmation emails.', 'mailpoet'), |
| 151 | 403, |
| 152 | 'mailpoet_subscribers_resend_forbidden' |
| 153 | ); |
| 154 | } |
| 155 | if ($definition->getGroup() !== SubscriberEntity::STATUS_UNCONFIRMED) { |
| 156 | throw new ApiException( |
| 157 | __('Confirmation emails can be resent in bulk only from the Unconfirmed subscribers view.', 'mailpoet'), |
| 158 | 400, |
| 159 | 'mailpoet_subscribers_invalid_group' |
| 160 | ); |
| 161 | } |
| 162 | if (!$this->bulkConfirmationEmailResender->isSignupConfirmationEnabled()) { |
| 163 | throw new ApiException( |
| 164 | $this->bulkConfirmationEmailResender->getConfirmationDisabledMessage(), |
| 165 | 400, |
| 166 | 'mailpoet_subscribers_confirmation_disabled' |
| 167 | ); |
| 168 | } |
| 169 | $selectAll = $request->getParam('select_all') === true; |
| 170 | $selection = $request->getParam('selection'); |
| 171 | $hasSelection = is_array($selection) && $selection !== []; |
| 172 | // Resend runs before the main guard, so apply the same explicit-intent |
| 173 | // rule here: without a selection and without select_all, an omitted |
| 174 | // listing selection would otherwise target every matching subscriber. |
| 175 | if (!$hasSelection && !$selectAll) { |
| 176 | throw new ApiException( |
| 177 | __('No subscribers selected.', 'mailpoet'), |
| 178 | 400, |
| 179 | 'mailpoet_subscribers_no_selection' |
| 180 | ); |
| 181 | } |
| 182 | // BulkConfirmationEmailResender::queue() inspects $requestData['listing'] |
| 183 | // to detect whether the caller provided an explicit selection (so that |
| 184 | // empty selection at the listing scope can target every matching |
| 185 | // subscriber). Rebuild that shape from the flat REST schema. |
| 186 | $listing = [ |
| 187 | 'group' => $request->getParam('group'), |
| 188 | 'search' => $request->getParam('search'), |
| 189 | 'filter' => $request->getParam('filter'), |
| 190 | ]; |
| 191 | if (!$selectAll && is_array($selection)) { |
| 192 | $listing['selection'] = $this->toIntList($selection); |
| 193 | } |
| 194 | $queueResult = $this->bulkConfirmationEmailResender->queue($definition, ['listing' => $listing]); |
| 195 | |
| 196 | return new Response([ |
| 197 | 'action' => self::ACTION_RESEND_CONFIRMATION_EMAILS, |
| 198 | 'count' => $queueResult['queued_count'], |
| 199 | 'segment' => null, |
| 200 | 'tag' => null, |
| 201 | 'queue' => $queueResult, |
| 202 | ]); |
| 203 | } |
| 204 | |
| 205 | private function buildDefinition(Request $request): ListingDefinition { |
| 206 | $filter = $request->getParam('filter'); |
| 207 | $selection = $request->getParam('selection'); |
| 208 | $selectAll = $request->getParam('select_all') === true; |
| 209 | $searchParam = $request->getParam('search'); |
| 210 | $groupParam = $request->getParam('group'); |
| 211 | |
| 212 | return $this->listingHandler->getListingDefinition([ |
| 213 | 'offset' => 0, |
| 214 | 'limit' => 0, |
| 215 | 'sort_by' => 'id', |
| 216 | 'sort_order' => 'desc', |
| 217 | 'search' => is_string($searchParam) ? $searchParam : null, |
| 218 | 'group' => is_string($groupParam) ? $groupParam : null, |
| 219 | 'filter' => is_array($filter) ? $filter : [], |
| 220 | 'selection' => !$selectAll && is_array($selection) ? $this->toIntList($selection) : [], |
| 221 | 'params' => [], |
| 222 | ]); |
| 223 | } |
| 224 | |
| 225 | private function validateSelectAllScope(string $action, ListingDefinition $definition): void { |
| 226 | $group = $definition->getGroup(); |
| 227 | if (!is_string($group) || !in_array($group, self::VALID_SELECT_ALL_GROUPS, true)) { |
| 228 | throw new ApiException( |
| 229 | __('Select all requires a valid subscriber view.', 'mailpoet'), |
| 230 | 400, |
| 231 | 'mailpoet_subscribers_invalid_select_all_group' |
| 232 | ); |
| 233 | } |
| 234 | |
| 235 | if (in_array($action, self::TRASH_ONLY_ACTIONS, true) && $group !== 'trash') { |
| 236 | throw new ApiException( |
| 237 | __('This bulk action can only be applied from the Trash view.', 'mailpoet'), |
| 238 | 400, |
| 239 | 'mailpoet_subscribers_invalid_select_all_scope' |
| 240 | ); |
| 241 | } |
| 242 | |
| 243 | if (in_array($action, self::NON_TRASH_ACTIONS, true) && $group === 'trash') { |
| 244 | throw new ApiException( |
| 245 | __('This bulk action cannot be applied from the Trash view.', 'mailpoet'), |
| 246 | 400, |
| 247 | 'mailpoet_subscribers_invalid_select_all_scope' |
| 248 | ); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * @param array<mixed> $values |
| 254 | * @return int[] |
| 255 | */ |
| 256 | private function toIntList(array $values): array { |
| 257 | $ints = []; |
| 258 | foreach ($values as $value) { |
| 259 | if (is_scalar($value)) { |
| 260 | $ints[] = (int)$value; |
| 261 | } |
| 262 | } |
| 263 | return $ints; |
| 264 | } |
| 265 | } |
| 266 |