This commit is contained in:
emmymayo
2025-02-05 23:15:46 +01:00
commit 7269c99357
16995 changed files with 3389680 additions and 0 deletions
@@ -0,0 +1 @@
<?php
@@ -0,0 +1,121 @@
<?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
namespace MailPoet\API\MP\v1;
if (!defined('ABSPATH')) exit;
use MailPoet\Config\Changelog;
/**
* API used by other plugins
* Do not add bodies of methods into this class. Use other classes. See CustomFields or Subscribers.
* This class is under refactor, and we are going to move most of the remaining implementations from here.
*/
class API {
/** @var CustomFields */
private $customFields;
/** @var Segments */
private $segments;
/** @var Subscribers */
private $subscribers;
/** @var Changelog */
private $changelog;
public function __construct(
CustomFields $customFields,
Segments $segments,
Subscribers $subscribers,
Changelog $changelog
) {
$this->customFields = $customFields;
$this->segments = $segments;
$this->subscribers = $subscribers;
$this->changelog = $changelog;
}
public function getSubscriberFields() {
return $this->customFields->getSubscriberFields();
}
public function addSubscriberField(array $data = []) {
try {
return $this->customFields->addSubscriberField($data);
} catch (\InvalidArgumentException $e) {
throw new APIException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* @throws APIException
*/
public function subscribeToList($subscriberId, $listId, $options = []): array {
return $this->subscribeToLists($subscriberId, [$listId], $options);
}
/**
* @throws APIException
*/
public function subscribeToLists($subscriberId, array $listIds, $options = []) {
return $this->subscribers->subscribeToLists($subscriberId, $listIds, $options);
}
public function unsubscribeFromList($subscriberId, $listId) {
return $this->unsubscribeFromLists($subscriberId, [$listId]);
}
public function unsubscribeFromLists($subscriberId, array $listIds) {
return $this->subscribers->unsubscribeFromLists($subscriberId, $listIds);
}
public function unsubscribe($subscriberIdOrEmail) {
return $this->subscribers->unsubscribe($subscriberIdOrEmail);
}
public function getLists(): array {
return $this->segments->getAll();
}
public function addSubscriber(array $subscriber, $listIds = [], $options = []): array {
return $this->subscribers->addSubscriber($subscriber, $listIds, $options);
}
public function updateSubscriber($subscriberIdOrEmail, array $subscriber): array {
return $this->subscribers->updateSubscriber($subscriberIdOrEmail, $subscriber);
}
public function addList(array $list) {
return $this->segments->addList($list);
}
public function deleteList(string $listId): bool {
return $this->segments->deleteList($listId);
}
public function updateList(array $list): array {
return $this->segments->updateList($list);
}
public function getSubscriber($subscriberEmail) {
return $this->subscribers->getSubscriber($subscriberEmail);
}
public function getSubscribers(array $filter = [], int $limit = 50, int $offset = 0): array {
return $this->subscribers->getSubscribers($filter, $limit, $offset);
}
public function getSubscribersCount(array $filter = []): int {
return $this->subscribers->getSubscribersCount($filter);
}
public function isSetupComplete() {
return !(
$this->changelog->shouldShowWelcomeWizard()
|| $this->changelog->shouldShowWooCommerceListImportPage()
|| $this->changelog->shouldShowRevenueTrackingPermissionPage()
);
}
}
@@ -0,0 +1,31 @@
<?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
namespace MailPoet\API\MP\v1;
if (!defined('ABSPATH')) exit;
class APIException extends \Exception {
const FAILED_TO_SAVE_SUBSCRIBER_FIELD = 1;
const SEGMENT_REQUIRED = 3;
const SUBSCRIBER_NOT_EXISTS = 4;
const LIST_NOT_EXISTS = 5;
const SUBSCRIBING_TO_WP_LIST_NOT_ALLOWED = 6;
const SUBSCRIBING_TO_WC_LIST_NOT_ALLOWED = 7;
const SUBSCRIBING_TO_LIST_NOT_ALLOWED = 8;
const CONFIRMATION_FAILED_TO_SEND = 10;
const EMAIL_ADDRESS_REQUIRED = 11;
const SUBSCRIBER_EXISTS = 12;
const FAILED_TO_SAVE_SUBSCRIBER = 13;
const LIST_NAME_REQUIRED = 14;
const LIST_EXISTS = 15;
const FAILED_TO_SAVE_LIST = 16;
const WELCOME_FAILED_TO_SEND = 17;
const LIST_ID_REQUIRED = 18;
const FAILED_TO_UPDATE_LIST = 19;
const LIST_USED_IN_EMAIL = 20;
const LIST_USED_IN_FORM = 21;
const FAILED_TO_DELETE_LIST = 22;
const LIST_TYPE_IS_NOT_SUPPORTED = 23;
const SUBSCRIBER_ALREADY_UNSUBSCRIBED = 24;
}
@@ -0,0 +1,81 @@
<?php declare(strict_types = 1);
namespace MailPoet\API\MP\v1;
if (!defined('ABSPATH')) exit;
use MailPoet\CustomFields\ApiDataSanitizer;
use MailPoet\CustomFields\CustomFieldsRepository;
class CustomFields {
/** @var ApiDataSanitizer */
private $customFieldsDataSanitizer;
/** @var CustomFieldsRepository */
private $customFieldsRepository;
public function __construct(
ApiDataSanitizer $customFieldsDataSanitizer,
CustomFieldsRepository $customFieldsRepository
) {
$this->customFieldsDataSanitizer = $customFieldsDataSanitizer;
$this->customFieldsRepository = $customFieldsRepository;
}
public function getSubscriberFields(): array {
$data = [
[
'id' => 'email',
'name' => __('Email', 'mailpoet'),
'type' => 'text',
'params' => [
'required' => '1',
],
],
[
'id' => 'first_name',
'name' => __('First name', 'mailpoet'),
'type' => 'text',
'params' => [
'required' => '',
],
],
[
'id' => 'last_name',
'name' => __('Last name', 'mailpoet'),
'type' => 'text',
'params' => [
'required' => '',
],
],
];
$customFields = $this->customFieldsRepository->findAll();
foreach ($customFields as $customField) {
$result = [
'id' => 'cf_' . $customField->getId(),
'name' => $customField->getName(),
'type' => $customField->getType(),
'params' => $customField->getParams(),
];
$data[] = $result;
}
return $data;
}
public function addSubscriberField(array $data = []): array {
try {
$customField = $this->customFieldsRepository->createOrUpdate($this->customFieldsDataSanitizer->sanitize($data));
} catch (\Exception $e) {
throw new APIException('Failed to save a new subscriber field ' . $e->getMessage(), APIException::FAILED_TO_SAVE_SUBSCRIBER_FIELD);
}
return [
'id' => 'cf_' . $customField->getId(),
'name' => $customField->getName(),
'type' => $customField->getType(),
'params' => $customField->getParams(),
];
}
}
@@ -0,0 +1,208 @@
<?php declare(strict_types = 1);
namespace MailPoet\API\MP\v1;
if (!defined('ABSPATH')) exit;
use MailPoet\Entities\SegmentEntity;
use MailPoet\Form\FormsRepository;
use MailPoet\Newsletter\Segment\NewsletterSegmentRepository;
use MailPoet\Segments\SegmentsRepository;
class Segments {
private const DATE_FORMAT = 'Y-m-d H:i:s';
/** @var NewsletterSegmentRepository */
private $newsletterSegmentRepository;
/** @var FormsRepository */
private $formsRepository;
/** @var SegmentsRepository */
private $segmentsRepository;
public function __construct (
NewsletterSegmentRepository $newsletterSegmentRepository,
FormsRepository $formsRepository,
SegmentsRepository $segmentsRepository
) {
$this->newsletterSegmentRepository = $newsletterSegmentRepository;
$this->formsRepository = $formsRepository;
$this->segmentsRepository = $segmentsRepository;
}
public function getAll(): array {
$segments = $this->segmentsRepository->findBy(['type' => SegmentEntity::TYPE_DEFAULT], ['id' => 'asc']);
$result = [];
foreach ($segments as $segment) {
$result[] = $this->buildItem($segment);
}
return $result;
}
public function addList(array $data): array {
$this->validateSegmentName($data);
try {
$name = isset($data['name']) ? sanitize_text_field($data['name']) : '';
$description = isset($data['description']) ? sanitize_textarea_field($data['description']) : '';
$segment = $this->segmentsRepository->createOrUpdate($name, $description);
} catch (\Exception $e) {
throw new APIException(
__('The list couldnt be created in the database', 'mailpoet'),
APIException::FAILED_TO_SAVE_LIST
);
}
return $this->buildItem($segment);
}
public function updateList(array $data): array {
// firstly validation on list id
$this->validateSegmentId((string)($data['id'] ?? ''));
// secondly validation on list name
$this->validateSegmentName($data);
// update is supported only for default segment type
$this->validateSegmentType((string)$data['id']);
$name = isset($data['name']) ? sanitize_text_field($data['name']) : '';
$description = isset($data['description']) ? sanitize_textarea_field($data['description']) : '';
try {
$segment = $this->segmentsRepository->createOrUpdate(
$name,
$description,
SegmentEntity::TYPE_DEFAULT,
[],
(int)$data['id']
);
} catch (\Exception $e) {
throw new APIException(
__('The list couldnt be updated in the database', 'mailpoet'),
APIException::FAILED_TO_UPDATE_LIST
);
}
return $this->buildItem($segment);
}
public function deleteList(string $listId): bool {
$this->validateSegmentId($listId);
// delete is supported only for default segment type
$this->validateSegmentType($listId);
$activelyUsedNewslettersSubjects = $this->newsletterSegmentRepository->getSubjectsOfActivelyUsedEmailsForSegments([$listId]);
if (isset($activelyUsedNewslettersSubjects[$listId])) {
throw new APIException(
str_replace(
'%1$s',
"'" . join("', '", $activelyUsedNewslettersSubjects[$listId]) . "'",
// translators: %1$s is a comma-seperated list of emails for which the segment is used.
_x('List cannot be deleted because its used for %1$s email', 'Alert shown when trying to delete segment, which is assigned to any automatic emails.', 'mailpoet')
),
APIException::LIST_USED_IN_EMAIL
);
}
$activelyUsedFormNames = $this->formsRepository->getNamesOfFormsForSegments();
if (isset($activelyUsedFormNames[$listId])) {
throw new APIException(
str_replace(
'%1$s',
"'" . join("', '", $activelyUsedFormNames[$listId]) . "'",
// translators: %1$s is a comma-seperated list of forms for which the segment is used.
_nx(
'List cannot be deleted because its used for %1$s form',
'List cannot be deleted because its used for %1$s forms',
count($activelyUsedFormNames[$listId]),
'Alert shown when trying to delete segment, when it is assigned to a form.',
'mailpoet'
)
),
APIException::LIST_USED_IN_FORM
);
}
try {
$this->segmentsRepository->bulkDelete([$listId]);
return true;
} catch (\Exception $e) {
throw new APIException(
__('The list couldnt be deleted from the database', 'mailpoet'),
APIException::FAILED_TO_DELETE_LIST
);
}
}
private function validateSegmentId(string $segmentId): void {
if (empty($segmentId)) {
throw new APIException(
__('List id is required.', 'mailpoet'),
APIException::LIST_ID_REQUIRED
);
}
if (!$this->segmentsRepository->findOneById($segmentId)) {
throw new APIException(
__('The list does not exist.', 'mailpoet'),
APIException::LIST_NOT_EXISTS
);
}
}
/**
* Throws an exception when the segment's name is invalid
* @return void
*/
private function validateSegmentName(array $data): void {
if (empty($data['name'])) {
throw new APIException(
__('List name is required.', 'mailpoet'),
APIException::LIST_NAME_REQUIRED
);
}
$segmentId = isset($data['id']) ? (int)$data['id'] : null;
if (!$this->segmentsRepository->isNameUnique($data['name'], $segmentId)) {
throw new APIException(
__('This list already exists.', 'mailpoet'),
APIException::LIST_EXISTS
);
}
}
private function validateSegmentType(string $segmentId): void {
$segment = $this->segmentsRepository->findOneById($segmentId);
if ($segment && $segment->getType() !== SegmentEntity::TYPE_DEFAULT) {
throw new APIException(
str_replace(
'%1$s',
"'" . $segment->getType() . "'",
// translators: %1$s is an invalid segment type.
__('List of the type %1$s is not supported for this action.', 'mailpoet')
),
APIException::LIST_TYPE_IS_NOT_SUPPORTED
);
}
}
/**
* @param SegmentEntity $segment
* @return array
*/
private function buildItem(SegmentEntity $segment): array {
return [
'id' => (string)$segment->getId(), // (string) for BC
'name' => $segment->getName(),
'type' => $segment->getType(),
'description' => $segment->getDescription(),
'created_at' => ($createdAt = $segment->getCreatedAt()) ? $createdAt->format(self::DATE_FORMAT) : null,
'updated_at' => $segment->getUpdatedAt()->format(self::DATE_FORMAT),
'deleted_at' => ($deletedAt = $segment->getDeletedAt()) ? $deletedAt->format(self::DATE_FORMAT) : null,
];
}
}
@@ -0,0 +1,512 @@
<?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
namespace MailPoet\API\MP\v1;
if (!defined('ABSPATH')) exit;
use MailPoet\API\JSON\ResponseBuilders\SubscribersResponseBuilder;
use MailPoet\Entities\SegmentEntity;
use MailPoet\Entities\StatisticsUnsubscribeEntity;
use MailPoet\Entities\SubscriberEntity;
use MailPoet\Listing\ListingDefinition;
use MailPoet\Newsletter\Scheduler\WelcomeScheduler;
use MailPoet\Segments\SegmentsRepository;
use MailPoet\Settings\SettingsController;
use MailPoet\Statistics\Track\Unsubscribes;
use MailPoet\Subscribers\ConfirmationEmailMailer;
use MailPoet\Subscribers\NewSubscriberNotificationMailer;
use MailPoet\Subscribers\RequiredCustomFieldValidator;
use MailPoet\Subscribers\Source;
use MailPoet\Subscribers\SubscriberListingRepository;
use MailPoet\Subscribers\SubscriberSaveController;
use MailPoet\Subscribers\SubscriberSegmentRepository;
use MailPoet\Subscribers\SubscribersRepository;
use MailPoet\Util\Helpers;
use MailPoet\WP\Functions as WPFunctions;
use MailPoetVendor\Carbon\Carbon;
class Subscribers {
const CONTEXT_SUBSCRIBE = 'subscribe';
const CONTEXT_UNSUBSCRIBE = 'unsubscribe';
/** @var SettingsController */
private $settings;
/** @var SubscribersRepository */
private $subscribersRepository;
/** @var SegmentsRepository */
private $segmentsRepository;
/** @var SubscriberSegmentRepository */
private $subscribersSegmentRepository;
/** @var ConfirmationEmailMailer */
private $confirmationEmailMailer;
/** @var WelcomeScheduler */
private $welcomeScheduler;
/** @var SubscribersResponseBuilder */
private $subscribersResponseBuilder;
/** @var NewSubscriberNotificationMailer */
private $newSubscriberNotificationMailer;
/** @var SubscriberSaveController */
private $subscriberSaveController;
/** @var RequiredCustomFieldValidator */
private $requiredCustomFieldsValidator;
/** @var WPFunctions */
private $wp;
/** @var SubscriberListingRepository */
private $subscriberListingRepository;
/** @var Unsubscribes */
private $unsubscribesTracker;
public function __construct (
ConfirmationEmailMailer $confirmationEmailMailer,
NewSubscriberNotificationMailer $newSubscriberNotificationMailer,
SegmentsRepository $segmentsRepository,
SettingsController $settings,
SubscriberSegmentRepository $subscriberSegmentRepository,
SubscribersRepository $subscribersRepository,
SubscriberSaveController $subscriberSaveController,
SubscribersResponseBuilder $subscribersResponseBuilder,
WelcomeScheduler $welcomeScheduler,
RequiredCustomFieldValidator $requiredCustomFieldsValidator,
SubscriberListingRepository $subscriberListingRepository,
WPFunctions $wp,
Unsubscribes $unsubscribesTracker
) {
$this->confirmationEmailMailer = $confirmationEmailMailer;
$this->newSubscriberNotificationMailer = $newSubscriberNotificationMailer;
$this->segmentsRepository = $segmentsRepository;
$this->settings = $settings;
$this->subscribersSegmentRepository = $subscriberSegmentRepository;
$this->subscribersRepository = $subscribersRepository;
$this->subscriberSaveController = $subscriberSaveController;
$this->subscribersResponseBuilder = $subscribersResponseBuilder;
$this->welcomeScheduler = $welcomeScheduler;
$this->requiredCustomFieldsValidator = $requiredCustomFieldsValidator;
$this->wp = $wp;
$this->subscriberListingRepository = $subscriberListingRepository;
$this->unsubscribesTracker = $unsubscribesTracker;
}
public function getSubscriber($subscriberIdOrEmail): array {
$subscriber = $this->findSubscriber($subscriberIdOrEmail);
return $this->subscribersResponseBuilder->build($subscriber);
}
public function addSubscriber(array $data, array $listIds = [], array $options = []): array {
$sendConfirmationEmail = !(isset($options['send_confirmation_email']) && $options['send_confirmation_email'] === false);
$scheduleWelcomeEmail = !(isset($options['schedule_welcome_email']) && $options['schedule_welcome_email'] === false);
$skipSubscriberNotification = (isset($options['skip_subscriber_notification']) && $options['skip_subscriber_notification'] === true);
// throw exception when subscriber email is missing
if (empty($data['email'])) {
throw new APIException(
__('Subscriber email address is required.', 'mailpoet'),
APIException::EMAIL_ADDRESS_REQUIRED
);
}
// throw exception when subscriber already exists
if ($this->subscribersRepository->findOneBy(['email' => $data['email']])) {
throw new APIException(
__('This subscriber already exists.', 'mailpoet'),
APIException::SUBSCRIBER_EXISTS
);
}
[$defaultFields, $customFields] = $this->extractCustomFieldsFromFromSubscriberData($data);
$this->requiredCustomFieldsValidator->validate($customFields);
// filter out all incoming data that we don't want to change, like status ...
$defaultFields = array_intersect_key($defaultFields, array_flip(['email', 'first_name', 'last_name', 'subscribed_ip']));
if (empty($defaultFields['subscribed_ip'])) {
$defaultFields['subscribed_ip'] = Helpers::getIP();
}
$defaultFields['source'] = Source::API;
try {
$subscriberEntity = $this->subscriberSaveController->createOrUpdate($defaultFields, null);
} catch (\Exception $e) {
throw new APIException(
// translators: %s is an error message.
sprintf(__('Failed to add subscriber: %s', 'mailpoet'), $e->getMessage()),
APIException::FAILED_TO_SAVE_SUBSCRIBER
);
}
try {
$this->subscriberSaveController->updateCustomFields($customFields, $subscriberEntity);
} catch (\Exception $e) {
throw new APIException(
// translators: %s is an error message
sprintf(__('Failed to save subscriber custom fields: %s', 'mailpoet'), $e->getMessage()),
APIException::FAILED_TO_SAVE_SUBSCRIBER
);
}
// subscribe to segments and optionally: 1) send confirmation email, 2) schedule welcome email(s)
if (!empty($listIds)) {
$this->subscribeToLists($subscriberEntity->getId(), $listIds, [
'send_confirmation_email' => $sendConfirmationEmail,
'schedule_welcome_email' => $scheduleWelcomeEmail,
'skip_subscriber_notification' => $skipSubscriberNotification,
]);
}
return $this->subscribersResponseBuilder->build($subscriberEntity);
}
public function updateSubscriber($subscriberIdOrEmail, array $data): array {
$this->checkSubscriberParam($subscriberIdOrEmail);
$subscriber = $this->findSubscriber($subscriberIdOrEmail);
[$defaultFields, $customFields] = $this->extractCustomFieldsFromFromSubscriberData($data);
$this->requiredCustomFieldsValidator->validate($customFields);
// filter out all incoming data that we don't want to change, like status ...
$defaultFields = array_intersect_key($defaultFields, array_flip(['email', 'first_name', 'last_name', 'subscribed_ip']));
if ($subscriber->getWpUserId() !== null) {
unset($defaultFields['email']);
unset($defaultFields['first_name']);
unset($defaultFields['last_name']);
};
if (empty($defaultFields['subscribed_ip'])) {
$defaultFields['subscribed_ip'] = Helpers::getIP();
}
$defaultFields['source'] = Source::API;
try {
$subscriberEntity = $this->subscriberSaveController->createOrUpdate($defaultFields, $subscriber);
} catch (\Exception $e) {
throw new APIException(
// translators: %s is an error message.
sprintf(__('Failed to update subscriber: %s', 'mailpoet'), $e->getMessage()),
APIException::FAILED_TO_SAVE_SUBSCRIBER
);
}
try {
$this->subscriberSaveController->updateCustomFields($customFields, $subscriberEntity);
} catch (\Exception $e) {
throw new APIException(
// translators: %s is an error message
sprintf(__('Failed to save subscriber custom fields: %s', 'mailpoet'), $e->getMessage()),
APIException::FAILED_TO_SAVE_SUBSCRIBER
);
}
return $this->subscribersResponseBuilder->build($subscriberEntity);
}
/**
* @throws APIException
*/
public function subscribeToLists(
$subscriberId,
array $listIds,
array $options = []
): array {
$scheduleWelcomeEmail = !((isset($options['schedule_welcome_email']) && $options['schedule_welcome_email'] === false));
$sendConfirmationEmail = !((isset($options['send_confirmation_email']) && $options['send_confirmation_email'] === false));
$skipSubscriberNotification = isset($options['skip_subscriber_notification']) && $options['skip_subscriber_notification'] === true;
$signupConfirmationEnabled = (bool)$this->settings->get('signup_confirmation.enabled');
$this->checkSubscriberAndListParams($subscriberId, $listIds);
$subscriber = $this->findSubscriber($subscriberId);
$foundSegments = $this->getAndValidateSegments($listIds, self::CONTEXT_SUBSCRIBE);
// restore trashed subscriber
if ($subscriber->getDeletedAt()) {
$subscriber->setDeletedAt(null);
}
$this->subscribersSegmentRepository->subscribeToSegments($subscriber, $foundSegments);
// set status depending on signup confirmation setting
if ($subscriber->getStatus() !== SubscriberEntity::STATUS_SUBSCRIBED) {
if ($signupConfirmationEnabled === true) {
$subscriber->setStatus(SubscriberEntity::STATUS_UNCONFIRMED);
} else {
$subscriber->setStatus(SubscriberEntity::STATUS_SUBSCRIBED);
}
try {
$this->subscribersRepository->flush();
} catch (\Exception $e) {
throw new APIException(
// translators: %s is the error message
sprintf(__('Failed to save a status of a subscriber : %s', 'mailpoet'), $e->getMessage()),
APIException::FAILED_TO_SAVE_SUBSCRIBER
);
}
// when global status changes to subscribed, fire subscribed hook for all subscribed segments
/** @var SubscriberEntity $subscriber - From some reason PHPStan evaluates $subscriber->getStatus() as mixed */
if ($subscriber->getStatus() === SubscriberEntity::STATUS_SUBSCRIBED) {
$subscriberSegments = $subscriber->getSubscriberSegments();
foreach ($subscriberSegments as $subscriberSegment) {
if ($subscriberSegment->getStatus() === SubscriberEntity::STATUS_SUBSCRIBED) {
$this->wp->doAction('mailpoet_segment_subscribed', $subscriberSegment);
}
}
}
}
// schedule welcome email
$foundSegmentsIds = array_map(
function(SegmentEntity $segment) {
return $segment->getId();
},
$foundSegments
);
if ($scheduleWelcomeEmail && $subscriber->getStatus() === SubscriberEntity::STATUS_SUBSCRIBED) {
$this->_scheduleWelcomeNotification($subscriber, $foundSegmentsIds);
}
// send confirmation email
if ($sendConfirmationEmail) {
$this->_sendConfirmationEmail($subscriber);
}
if (!$skipSubscriberNotification && ($subscriber->getStatus() === SubscriberEntity::STATUS_SUBSCRIBED)) {
$this->newSubscriberNotificationMailer->send($subscriber, $this->segmentsRepository->findBy(['id' => $foundSegmentsIds]));
}
$this->subscribersRepository->refresh($subscriber);
return $this->subscribersResponseBuilder->build($subscriber);
}
public function unsubscribe($subscriberIdOrEmail): array {
$this->checkSubscriberParam($subscriberIdOrEmail);
$subscriber = $this->findSubscriber($subscriberIdOrEmail);
if ($subscriber->getStatus() === SubscriberEntity::STATUS_UNSUBSCRIBED) {
throw new APIException(__('This subscriber is already unsubscribed.', 'mailpoet'), APIException::SUBSCRIBER_ALREADY_UNSUBSCRIBED);
}
$this->unsubscribesTracker->track(
(int)$subscriber->getId(),
StatisticsUnsubscribeEntity::SOURCE_MP_API
);
$subscriber->setStatus(SubscriberEntity::STATUS_UNSUBSCRIBED);
$this->subscribersRepository->persist($subscriber);
$this->subscribersRepository->flush();
$this->subscribersSegmentRepository->unsubscribeFromSegments($subscriber);
return $this->subscribersResponseBuilder->build($subscriber);
}
public function unsubscribeFromLists($subscriberIdOrEmail, array $listIds): array {
$this->checkSubscriberAndListParams($subscriberIdOrEmail, $listIds);
$subscriber = $this->findSubscriber($subscriberIdOrEmail);
$foundSegments = $this->getAndValidateSegments($listIds, self::CONTEXT_UNSUBSCRIBE);
$this->subscribersSegmentRepository->unsubscribeFromSegments($subscriber, $foundSegments);
return $this->subscribersResponseBuilder->build($subscriber);
}
public function getSubscribers(array $filter, int $limit, int $offset): array {
$listingDefinition = $this->buildListingDefinition($filter, $limit, $offset);
$subscribers = $this->subscriberListingRepository->getData($listingDefinition);
$result = [];
foreach ($subscribers as $subscriber) {
$result[] = $this->subscribersResponseBuilder->build($subscriber);
}
return $result;
}
public function getSubscribersCount(array $filter): int {
$listingDefinition = $this->buildListingDefinition($filter);
return $this->subscriberListingRepository->getCount($listingDefinition);
}
/**
* @param array $filter {
* Filters to retrieve subscribers.
*
* @type string $status One of values: subscribed, unconfirmed, unsubscribed, inactive, bounced
* @type int $listId id of a list or dynamic segment
* @type \DateTime|int $minUpdatedAt DateTime object or timestamp of last update of subscriber.
* }
*/
private function buildListingDefinition(array $filter, int $limit = 50, int $offset = 0): ListingDefinition {
$group = isset($filter['status']) && is_string($filter['status']) ? $filter['status'] : null;
$listingFilters = [];
// Set filtering by listId
if (isset($filter['listId']) && is_int($filter['listId'])) {
$listingFilters['segment'] = $filter['listId'];
}
// Set filtering by minimal updatedAt
if (isset($filter['minUpdatedAt'])) {
if ($filter['minUpdatedAt'] instanceof \DateTime) {
$listingFilters['minUpdatedAt'] = $filter['minUpdatedAt'];
} elseif (is_int($filter['minUpdatedAt'])) {
$listingFilters['minUpdatedAt'] = Carbon::createFromTimestamp($filter['minUpdatedAt']);
}
}
return new ListingDefinition($group, $listingFilters, null, [], 'id', 'asc', $offset, $limit);
}
/**
* @throws APIException
*/
protected function _scheduleWelcomeNotification(SubscriberEntity $subscriber, array $segments) {
try {
$this->welcomeScheduler->scheduleSubscriberWelcomeNotification($subscriber->getId(), $segments);
} catch (\Throwable $e) {
throw new APIException(
// translators: %s is an error message
sprintf(__('Subscriber added, but welcome email failed to send: %s', 'mailpoet'), $e->getMessage()),
APIException::WELCOME_FAILED_TO_SEND
);
}
}
/**
* @throws APIException
*/
protected function _sendConfirmationEmail(SubscriberEntity $subscriberEntity) {
try {
$this->confirmationEmailMailer->sendConfirmationEmailOnce($subscriberEntity);
} catch (\Exception $e) {
throw new APIException(
// translators: %s is the error message
sprintf(__('Subscriber added to lists, but confirmation email failed to send: %s', 'mailpoet'), strtolower($e->getMessage())),
APIException::CONFIRMATION_FAILED_TO_SEND
);
}
}
/**
* @throws APIException
*/
private function checkSubscriberAndListParams($subscriberIdOrEmail, array $listIds): void {
if (empty($listIds)) {
throw new APIException(__('At least one segment ID is required.', 'mailpoet'), APIException::SEGMENT_REQUIRED);
}
$this->checkSubscriberParam($subscriberIdOrEmail);
}
/**
* @throws APIException
*/
private function checkSubscriberParam($subscriberIdOrEmail): void {
if (empty($subscriberIdOrEmail)) {
throw new APIException(__('A subscriber is required.', 'mailpoet'), APIException::SUBSCRIBER_NOT_EXISTS);
}
}
/**
* @throws APIException
*/
private function findSubscriber($subscriberIdOrEmail): SubscriberEntity {
// throw exception when subscriber does not exist
$subscriber = null;
if (is_int($subscriberIdOrEmail) || (string)(int)$subscriberIdOrEmail === $subscriberIdOrEmail) {
$subscriber = $this->subscribersRepository->findOneById($subscriberIdOrEmail);
} else if (strlen(trim($subscriberIdOrEmail)) > 0) {
$subscriber = $this->subscribersRepository->findOneBy(['email' => $subscriberIdOrEmail]);
}
if (!$subscriber) {
throw new APIException(__('This subscriber does not exist.', 'mailpoet'), APIException::SUBSCRIBER_NOT_EXISTS);
}
return $subscriber;
}
/**
* @return SegmentEntity[]
* @throws APIException
*/
private function getAndValidateSegments(array $listIds, string $context): array {
// throw exception when none of the segments exist
$foundSegments = $this->segmentsRepository->findBy(['id' => $listIds]);
if (!$foundSegments) {
$exception = _n('This list does not exist.', 'These lists do not exist.', count($listIds), 'mailpoet');
throw new APIException($exception, APIException::LIST_NOT_EXISTS);
}
// throw exception when trying to subscribe to WP Users or WooCommerce Customers segments
$foundSegmentsIds = [];
foreach ($foundSegments as $foundSegment) {
if ($foundSegment->getType() === SegmentEntity::TYPE_WP_USERS) {
if ($context === self::CONTEXT_SUBSCRIBE) {
// translators: %d is the ID of the segment
$message = __("Can't subscribe to a WordPress Users list with ID '%d'.", 'mailpoet');
} else {
// translators: %d is the ID of the segment
$message = __("Can't unsubscribe from a WordPress Users list with ID '%d'.", 'mailpoet');
}
throw new APIException(sprintf($message, $foundSegment->getId()), APIException::SUBSCRIBING_TO_WP_LIST_NOT_ALLOWED);
}
if ($foundSegment->getType() === SegmentEntity::TYPE_WC_USERS) {
if ($context === self::CONTEXT_SUBSCRIBE) {
// translators: %d is the ID of the segment
$message = __("Can't subscribe to a WooCommerce Customers list with ID '%d'.", 'mailpoet');
} else {
// translators: %d is the ID of the segment
$message = __("Can't unsubscribe from a WooCommerce Customers list with ID '%d'.", 'mailpoet');
}
throw new APIException(sprintf($message, $foundSegment->getId()), APIException::SUBSCRIBING_TO_WC_LIST_NOT_ALLOWED);
}
if ($foundSegment->getType() !== SegmentEntity::TYPE_DEFAULT) {
if ($context === self::CONTEXT_SUBSCRIBE) {
// translators: %d is the ID of the segment
$message = __("Can't subscribe to a list with ID '%d'.", 'mailpoet');
} else {
// translators: %d is the ID of the segment
$message = __("Can't unsubscribe from a list with ID '%d'.", 'mailpoet');
}
throw new APIException(sprintf($message, $foundSegment->getId()), APIException::SUBSCRIBING_TO_LIST_NOT_ALLOWED);
}
$foundSegmentsIds[] = $foundSegment->getId();
}
// throw an exception when one or more segments do not exist
if (count($foundSegmentsIds) !== count($listIds)) {
$missingIds = array_values(array_diff($listIds, $foundSegmentsIds));
$exception = sprintf(
// translators: %s is the count of lists
_n("List with ID '%s' does not exist.", "Lists with IDs '%s' do not exist.", count($missingIds), 'mailpoet'),
implode(', ', $missingIds)
);
throw new APIException(sprintf($exception, implode(', ', $missingIds)), APIException::LIST_NOT_EXISTS);
}
return $foundSegments;
}
/**
* Splits subscriber data into two arrays with basic data (index 0) and custom fields data (index 1)
* @return array<int, array>
*/
private function extractCustomFieldsFromFromSubscriberData($data): array {
$customFields = [];
foreach ($data as $key => $value) {
if (strpos($key, 'cf_') === 0) {
$customFields[$key] = $value;
unset($data[$key]);
}
}
return [$data, $customFields];
}
}
@@ -0,0 +1 @@
<?php