SubscriberConfirmationEmailEndpoint.php
2 months ago
SubscribersBulkActionEndpoint.php
4 days ago
SubscribersListingEndpoint.php
2 weeks ago
index.php
2 months ago
SubscriberConfirmationEmailEndpoint.php
114 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\Subscribers\ConfirmationEmailMailer; |
| 15 | use MailPoet\Subscribers\SubscribersRepository; |
| 16 | use MailPoet\Validator\Builder; |
| 17 | use MailPoet\WP\Functions as WPFunctions; |
| 18 | |
| 19 | /** |
| 20 | * `POST /mailpoet/v1/subscribers/{id}/resend-confirmation-email` |
| 21 | * |
| 22 | * Replaces the legacy `MailPoet\API\JSON\v1\Subscribers::sendConfirmationEmail` |
| 23 | * call. Per-list confirmation settings are intentionally not resolved here; the |
| 24 | * global default is used to avoid ambiguity across multiple segments. |
| 25 | */ |
| 26 | class SubscriberConfirmationEmailEndpoint extends Endpoint { |
| 27 | /** @var SubscribersRepository */ |
| 28 | private $subscribersRepository; |
| 29 | |
| 30 | /** @var ConfirmationEmailMailer */ |
| 31 | private $confirmationEmailMailer; |
| 32 | |
| 33 | /** @var WPFunctions */ |
| 34 | private $wp; |
| 35 | |
| 36 | public function __construct( |
| 37 | SubscribersRepository $subscribersRepository, |
| 38 | ConfirmationEmailMailer $confirmationEmailMailer, |
| 39 | WPFunctions $wp |
| 40 | ) { |
| 41 | $this->subscribersRepository = $subscribersRepository; |
| 42 | $this->confirmationEmailMailer = $confirmationEmailMailer; |
| 43 | $this->wp = $wp; |
| 44 | } |
| 45 | |
| 46 | public function checkPermissions(): bool { |
| 47 | return $this->wp->currentUserCan(AccessControl::PERMISSION_MANAGE_SUBSCRIBERS); |
| 48 | } |
| 49 | |
| 50 | public function handle(Request $request): Response { |
| 51 | $idParam = $request->getParam('id'); |
| 52 | $id = is_numeric($idParam) ? (int)$idParam : 0; |
| 53 | $subscriber = $this->subscribersRepository->findOneById($id); |
| 54 | if (!$subscriber instanceof SubscriberEntity) { |
| 55 | throw new ApiException( |
| 56 | __('This subscriber does not exist.', 'mailpoet'), |
| 57 | 404, |
| 58 | 'mailpoet_subscribers_not_found' |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | try { |
| 63 | $result = $this->confirmationEmailMailer->sendAdminConfirmationEmail($subscriber); |
| 64 | } catch (\Exception $exception) { |
| 65 | throw new ApiException( |
| 66 | __('There was a problem with your sending method. Please check if your sending method is properly configured.', 'mailpoet'), |
| 67 | 500, |
| 68 | 'mailpoet_subscribers_sending_failed', |
| 69 | [], |
| 70 | $exception |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | if (($result['status'] ?? null) === 'sent') { |
| 75 | return new Response(['sent' => true]); |
| 76 | } |
| 77 | |
| 78 | $reason = $result['reason'] ?? null; |
| 79 | if ($reason === 'max_confirmations_reached') { |
| 80 | throw new ApiException( |
| 81 | __('The maximum number of confirmation emails has already been reached for this subscriber.', 'mailpoet'), |
| 82 | 400, |
| 83 | 'mailpoet_subscribers_max_confirmations_reached' |
| 84 | ); |
| 85 | } |
| 86 | if ($reason === 'recently_sent') { |
| 87 | throw new ApiException( |
| 88 | __('A confirmation email was sent recently. Please wait before resending it.', 'mailpoet'), |
| 89 | 400, |
| 90 | 'mailpoet_subscribers_recently_sent' |
| 91 | ); |
| 92 | } |
| 93 | if ($reason === 'confirmation_disabled') { |
| 94 | throw new ApiException( |
| 95 | __('Sign-up confirmation is disabled in your MailPoet settings. Please enable it to resend confirmation emails or update your subscriber\'s status manually.', 'mailpoet'), |
| 96 | 400, |
| 97 | 'mailpoet_subscribers_confirmation_disabled' |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | throw new ApiException( |
| 102 | __('There was a problem with your sending method. Please check if your sending method is properly configured.', 'mailpoet'), |
| 103 | 500, |
| 104 | 'mailpoet_subscribers_sending_failed' |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | public static function getRequestSchema(): array { |
| 109 | return [ |
| 110 | 'id' => Builder::integer()->required(), |
| 111 | ]; |
| 112 | } |
| 113 | } |
| 114 |