Skip to content

Commit

Permalink
Improves subscriber list add/remove logic
Browse files Browse the repository at this point in the history
  • Loading branch information
BenParizek committed Mar 30, 2024
1 parent 95bb363 commit 9e1be9f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG/CHANGELOG-MAILER.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

### Fixed

- Improves native field logic so layouts aren’t initialized before subclass is checked
- Improved behavior when adding and removing non-credentialed users from Subscriber List Audience Types
- Improved native field logic so layouts aren’t initialized before subclass is checked
- Fixed logic when checking if a subscriber is subscribed by email

## 4.1.8 - 2024-03-09

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public function isSubscribed(mixed $identifier): bool
$criteria['userId'] = $identifier->id;
} elseif (is_numeric($identifier)) {
$criteria['userId'] = $identifier;
} elseif (!filter_var($identifier, FILTER_VALIDATE_EMAIL)) {
} elseif (filter_var($identifier, FILTER_VALIDATE_EMAIL)) {
$user = User::find()
->email($identifier)
->one();
Expand Down
5 changes: 5 additions & 0 deletions src/mailer/controllers/SubscriberListsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

class SubscriberListsController extends Controller
{
protected array|bool|int $allowAnonymous = [
'add',
'remove'
];

/**
* Adds a User to a Subscriber List
*/
Expand Down
13 changes: 12 additions & 1 deletion src/mailer/subscriberlists/SubscriberLists.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function add(SubscriptionRecord $subscription): bool
$transaction = Craft::$app->getDb()->beginTransaction();

try {
$user = null;

// Prefer User ID over email
if ($subscription->userId) {
Expand All @@ -34,7 +35,7 @@ public function add(SubscriptionRecord $subscription): bool
}
}

if (!$subscription->validate()) {
if (!$user || !$subscription->validate()) {
return false;
}

Expand All @@ -45,6 +46,9 @@ public function add(SubscriptionRecord $subscription): bool

if (!$subscriptionExists) {
$subscription->save();

// Resave user to ensure Element Indexes are updated when refreshed
Craft::$app->elements->saveElement($user, false);
}

$transaction->commit();
Expand All @@ -59,6 +63,8 @@ public function add(SubscriptionRecord $subscription): bool

public function remove(SubscriptionRecord $subscription): bool
{
$user = null;

if ($subscription->userId) {
$user = Craft::$app->getUsers()->getUserById($subscription->userId);
if (!$user) {
Expand All @@ -85,6 +91,11 @@ public function remove(SubscriptionRecord $subscription): bool
'userId' => $subscription->userId,
])?->delete();

if ($user) {
// Resave user to ensure Element Indexes are updated when refreshed
Craft::$app->elements->saveElement($user, false);
}

return true;
}

Expand Down

0 comments on commit 9e1be9f

Please sign in to comment.