Verify that new email is unique when editing a subscriber
Throws an exception with information message if email already exists. [MAILPOET-4259]
This commit is contained in:
@ -2,7 +2,9 @@
|
||||
|
||||
namespace MailPoet\Subscribers;
|
||||
|
||||
use MailPoet\ConflictException;
|
||||
use MailPoet\CustomFields\CustomFieldsRepository;
|
||||
use MailPoet\Doctrine\Validator\ValidationException;
|
||||
use MailPoet\Entities\StatisticsUnsubscribeEntity;
|
||||
use MailPoet\Entities\SubscriberEntity;
|
||||
use MailPoet\Entities\SubscriberSegmentEntity;
|
||||
@ -69,8 +71,33 @@ class SubscriberSaveController {
|
||||
$this->wp = $wp;
|
||||
}
|
||||
|
||||
public function filterOutReservedColumns(array $subscriberData): array {
|
||||
$reservedColumns = [
|
||||
'id',
|
||||
'wp_user_id',
|
||||
'is_woocommerce_user',
|
||||
'status',
|
||||
'subscribed_ip',
|
||||
'confirmed_ip',
|
||||
'confirmed_at',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'deleted_at',
|
||||
'unconfirmed_data',
|
||||
];
|
||||
return array_diff_key(
|
||||
$subscriberData,
|
||||
array_flip($reservedColumns)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ConflictException
|
||||
* @throws ValidationException
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function save(array $data): SubscriberEntity {
|
||||
if (is_array($data) && !empty($data)) {
|
||||
if (!empty($data)) {
|
||||
$data = $this->wp->stripslashesDeep($data);
|
||||
}
|
||||
|
||||
@ -97,6 +124,10 @@ class SubscriberSaveController {
|
||||
);
|
||||
}
|
||||
|
||||
if (isset($data['email']) && $this->isNewEmail($data['email'], $oldSubscriber)) {
|
||||
$this->verifyEmailIsUnique($data['email']);
|
||||
}
|
||||
|
||||
$subscriber = $this->createOrUpdate($data, $oldSubscriber);
|
||||
|
||||
$this->updateCustomFields($data, $subscriber);
|
||||
@ -120,26 +151,6 @@ class SubscriberSaveController {
|
||||
return $subscriber;
|
||||
}
|
||||
|
||||
public function filterOutReservedColumns(array $subscriberData): array {
|
||||
$reservedColumns = [
|
||||
'id',
|
||||
'wp_user_id',
|
||||
'is_woocommerce_user',
|
||||
'status',
|
||||
'subscribed_ip',
|
||||
'confirmed_ip',
|
||||
'confirmed_at',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'deleted_at',
|
||||
'unconfirmed_data',
|
||||
];
|
||||
return array_diff_key(
|
||||
$subscriberData,
|
||||
array_flip($reservedColumns)
|
||||
);
|
||||
}
|
||||
|
||||
private function getNonDefaultSubscribedSegments(array $data): array {
|
||||
if (!isset($data['id']) || (int)$data['id'] <= 0) {
|
||||
return [];
|
||||
@ -175,6 +186,9 @@ class SubscriberSaveController {
|
||||
return array_diff($data['segments'], $oldSegmentIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function createOrUpdate(array $data, ?SubscriberEntity $subscriber): SubscriberEntity {
|
||||
if (!$subscriber) {
|
||||
$subscriber = $this->createSubscriber();
|
||||
@ -204,6 +218,22 @@ class SubscriberSaveController {
|
||||
return $subscriber;
|
||||
}
|
||||
|
||||
private function isNewEmail(string $email, ?SubscriberEntity $subscriber): bool {
|
||||
if ($subscriber && ($subscriber->getEmail() === $email)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ConflictException
|
||||
*/
|
||||
private function verifyEmailIsUnique(string $email): void {
|
||||
$existingSubscriber = $this->subscribersRepository->findOneBy(['email' => $email]);
|
||||
if ($existingSubscriber) {
|
||||
$exceptionMessage = __(sprintf("A subscriber with E-mail '%s' already exists.", $email), 'mailpoet');
|
||||
throw new ConflictException($exceptionMessage);
|
||||
}
|
||||
}
|
||||
|
||||
private function createSubscriber(): SubscriberEntity {
|
||||
$subscriber = new SubscriberEntity();
|
||||
$subscriber->setUnsubscribeToken($this->security->generateUnsubscribeTokenByEntity($subscriber));
|
||||
|
Reference in New Issue
Block a user