diff --git a/CHANGELOG.md b/CHANGELOG.md index e5411bd..b140c86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,14 @@ - Fixed a styling issue with the element’s `.proxy-element-card` meta card footer. +## 1.2.0.2 - 2024-11-08 + +- Fixed a PHP error that could occur when saving a new user. + +## 1.2.0.1 - 2024-11-07 + +- Fixed an infinite loop that could occur when querying for a user. + ## 1.2.0 - 2024-11-06 > [!NOTE] diff --git a/src/Plugin.php b/src/Plugin.php index 857f8b8..b926024 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -575,15 +575,15 @@ function(DefineMenuItemsEvent $event) { private function handleUserElementChanges(): void { // if email address got changed - update stripe - Event::on(UserRecord::class, UserRecord::EVENT_BEFORE_UPDATE, function(ModelEvent $event) { - $userRecord = $event->sender; + Event::on(User::class, User::EVENT_BEFORE_SAVE, function(ModelEvent $event) { /** @var User|StripeCustomerBehavior $user */ - $user = Craft::$app->getUsers()->getUserById($userRecord->id); + $user = $event->sender; + $userRecord = UserRecord::findOne($user->id); $settings = $this->getSettings(); - if ($user->isCredentialed && $settings['syncChangedUserEmailsToStripe']) { - $oldEmail = $userRecord->getOldAttribute('email'); - $newEmail = $userRecord->getAttribute('email'); - if ($oldEmail != $newEmail) { + if ($userRecord && $user->isCredentialed && $settings['syncChangedUserEmailsToStripe']) { + $oldEmail = $userRecord->getAttribute('email'); + $newEmail = $user->email; + if ($oldEmail && $newEmail && ($oldEmail != $newEmail)) { $customers = $user->getStripeCustomers(); if ($customers->isNotEmpty()) { $client = $this->getApi()->getClient(); @@ -671,7 +671,6 @@ public function getCpNavItem(): ?array ]; } - return $ret; }