From 417b86929d092849db53fa6e81942034fb7cadef Mon Sep 17 00:00:00 2001 From: Josh Crawford Date: Fri, 15 Nov 2024 08:44:39 +1100 Subject: [PATCH] Fix PHP type issues for plugin settings, not working well with .env variables --- src/models/Settings.php | 59 ++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/src/models/Settings.php b/src/models/Settings.php index 72e35f2..ffdc464 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -2,6 +2,7 @@ namespace verbb\abandonedcart\models; use craft\base\Model; +use craft\behaviors\EnvAttributeParserBehavior; use craft\helpers\App; use craft\helpers\StringHelper; @@ -12,16 +13,16 @@ class Settings extends Model public string $pluginName = 'Abandoned Carts'; public ?string $passKey = null; - public ?int $restoreExpiryHours = 48; - public ?int $firstReminderDelay = 1; - public ?int $secondReminderDelay = 12; + public int|string|null $restoreExpiryHours = 48; + public int|string|null $firstReminderDelay = 1; + public int|string|null $secondReminderDelay = 12; public ?string $discountCode = null; public ?string $firstReminderTemplate = 'abandoned-cart/emails/first'; public ?string $secondReminderTemplate = 'abandoned-cart/emails/second'; public ?string $firstReminderSubject = 'You‘ve left some items in your cart'; public ?string $secondReminderSubject = 'Your items are still waiting - don‘t miss out'; public ?string $recoveryUrl = 'shop/cart'; - public bool $disableSecondReminder = false; + public bool|string|null $disableSecondReminder = false; public ?string $blacklist = null; @@ -45,17 +46,6 @@ public function init(): void parent::init(); } - public function defineRules(): array - { - $rules = parent::defineRules(); - $rules[] = [['pluginName', 'restoreExpiryHours', 'firstReminderDelay', 'secondReminderDelay', 'firstReminderTemplate', 'secondReminderTemplate', 'firstReminderSubject', 'secondReminderSubject', 'recoveryUrl', 'passKey'], 'required']; - $rules[] = ['restoreExpiryHours', 'integer', 'min' => 24, 'max' => '168']; // Atleast 24hrs - $rules[] = ['firstReminderDelay', 'integer', 'min' => 1, 'max' => 24]; // 1hr + - $rules[] = ['secondReminderDelay', 'integer', 'min' => 12, 'max' => 48]; // prevent spam - - return $rules; - } - public function getPassKey(): ?string { return App::parseEnv($this->passKey); @@ -120,4 +110,43 @@ public function getBlacklist(): ?string { return App::parseEnv($this->blacklist); } + + + // Protected Methods + // ========================================================================= + + protected function defineRules(): array + { + $rules = parent::defineRules(); + $rules[] = [['pluginName', 'restoreExpiryHours', 'firstReminderDelay', 'secondReminderDelay', 'firstReminderTemplate', 'secondReminderTemplate', 'firstReminderSubject', 'secondReminderSubject', 'recoveryUrl', 'passKey'], 'required']; + $rules[] = [['restoreExpiryHours'], 'integer', 'min' => 24, 'max' => '168']; // Atleast 24hrs + $rules[] = [['firstReminderDelay'], 'integer', 'min' => 1, 'max' => 24]; // 1hr + + $rules[] = [['secondReminderDelay'], 'integer', 'min' => 12, 'max' => 48]; // prevent spam + + return $rules; + } + + protected function defineBehaviors(): array + { + return [ + 'parser' => [ + 'class' => EnvAttributeParserBehavior::class, + 'attributes' => [ + 'passKey', + 'pluginName', + 'disableSecondReminder', + 'restoreExpiryHours', + 'firstReminderDelay', + 'secondReminderDelay', + 'firstReminderTemplate', + 'firstReminderSubject', + 'secondReminderTemplate', + 'secondReminderSubject', + 'discountCode', + 'recoveryUrl', + 'blacklist', + ], + ], + ]; + } }