← All changes
|
src/DonationForms/Actions/ConvertDonationAmountBlockToFieldsApi.php
+27
-9
4.16.3
→
4.17.0
View file →
| @@ -27,9 +27,12 @@ | ||
| 27 | 27 | class ConvertDonationAmountBlockToFieldsApi |
| 28 | 28 | { |
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | - * @since 4.10.0: Replaced generic 'currency' rule with custom CurrencyRule that uses GiveWP's currency list | |
| 31 | + * @since 4.16.8 Exempt admin-defined amounts from the custom amount minimum and maximum, and fall back | |
| 32 | + * to the lowest admin-defined amount when no custom amount minimum applies. | |
| 33 | + * @since 4.16.5 Set default value for the levelId hidden field. | |
| 34 | + * @since 4.10.0 Replaced generic 'currency' rule with custom CurrencyRule that uses GiveWP's currency list | |
| 32 | 35 | * @since 3.0.0 |
| 33 | 36 | * |
| 34 | 37 | * @throws EmptyNameException |
| 35 | 38 | * @throws NameCollisionException |
| @@ -37,8 +40,9 @@ | ||
| 37 | 40 | public function __invoke(DonationAmountBlockModel $block, string $currency): DonationAmount |
| 38 | 41 | { |
| 39 | 42 | $amountField = DonationAmount::make('donationAmount')->tap(function (Group $group) use ($block, $currency) { |
| 40 | 43 | $amountRules = ['required', 'numeric']; |
| 44 | + $exemptAmounts = $block->getAdminDefinedAmounts(); | |
| 41 | 45 | |
| 42 | 46 | if (!$block->isCustomAmountEnabled() && |
| 43 | 47 | $block->getPriceOption() === 'set') { |
| 44 | 48 | $size = $block->getSetPrice(); |
| @@ -43,18 +47,18 @@ | ||
| 43 | 47 | $block->getPriceOption() === 'set') { |
| 44 | 48 | $size = $block->getSetPrice(); |
| 45 | 49 | |
| 46 | 50 | $amountRules[] = new Size($size); |
| 47 | - } | |
| 51 | + } else { | |
| 52 | + $minimum = $block->getMinimumAmount(); | |
| 48 | 53 | |
| 49 | - if ($block->isCustomAmountEnabled()) { | |
| 50 | - if ($block->hasAttribute('customAmountMin')) { | |
| 51 | - $amountRules[] = new Min($block->getAttribute('customAmountMin')); | |
| 54 | + if ($minimum !== null) { | |
| 55 | + $amountRules[] = (new Min($minimum))->exemptAmounts(...$exemptAmounts); | |
| 52 | 56 | } |
| 57 | + } | |
| 53 | 58 | |
| 54 | - if ($block->hasAttribute('customAmountMax') && $block->getAttribute('customAmountMax') > 0) { | |
| 55 | - $amountRules[] = new Max($block->getAttribute('customAmountMax')); | |
| 56 | - } | |
| 59 | + if ($block->isCustomAmountEnabled() && $block->getCustomAmountMax() > 0) { | |
| 60 | + $amountRules[] = (new Max($block->getCustomAmountMax()))->exemptAmounts(...$exemptAmounts); | |
| 57 | 61 | } |
| 58 | 62 | |
| 59 | 63 | /** @var Amount $amountNode */ |
| 60 | 64 | $amountNode = $group->getNodeByName('amount'); |
| @@ -63,8 +67,9 @@ | ||
| 63 | 67 | ->allowCustomAmount($block->isCustomAmountEnabled()) |
| 64 | 68 | ->rules(...$amountRules); |
| 65 | 69 | |
| 66 | 70 | $priceOptions = $block->getPriceOption(); |
| 71 | + $defaultLevelId = ''; | |
| 67 | 72 | if ($priceOptions === 'multi') { |
| 68 | 73 | ['levels' => $levels, 'checked' => $checked] = $this->prepareLevelsArray($block); |
| 69 | 74 | |
| 70 | 75 | $amountNode |
| @@ -70,8 +75,15 @@ | ||
| 70 | 75 | $amountNode |
| 71 | 76 | ->allowLevels() |
| 72 | 77 | ->levels(...$levels) |
| 73 | 78 | ->defaultValue($checked); |
| 79 | + | |
| 80 | + foreach ($levels as $index => $level) { | |
| 81 | + if ($level['checked']) { | |
| 82 | + $defaultLevelId = (string)$index; | |
| 83 | + break; | |
| 84 | + } | |
| 85 | + } | |
| 74 | 86 | } else { |
| 75 | 87 | $amountNode |
| 76 | 88 | ->fixedAmountValue($block->getSetPrice()) |
| 77 | 89 | ->defaultValue($block->getSetPrice()); |
| @@ -76,8 +88,12 @@ | ||
| 76 | 88 | ->fixedAmountValue($block->getSetPrice()) |
| 77 | 89 | ->defaultValue($block->getSetPrice()); |
| 78 | 90 | } |
| 79 | 91 | |
| 92 | + /** @var Hidden $levelIdNode */ | |
| 93 | + $levelIdNode = $group->getNodeByName('levelId'); | |
| 94 | + $levelIdNode->defaultValue($defaultLevelId); | |
| 95 | + | |
| 80 | 96 | /** @var Hidden $currencyNode */ |
| 81 | 97 | $currencyNode = $group->getNodeByName('currency'); |
| 82 | 98 | $currencyNode |
| 83 | 99 | ->defaultValue($currency) |
| @@ -175,11 +191,12 @@ | ||
| 175 | 191 | |
| 176 | 192 | /** |
| 177 | 193 | * Prepares the options array to be used in the field. |
| 178 | 194 | * |
| 195 | + * @since 4.16.5 Add per-level "checked" flag. | |
| 179 | 196 | * @since 3.12.0 |
| 180 | 197 | * |
| 181 | - * @return array ['options' => ['label' => string, 'value' => string][], 'checked' => string] | |
| 198 | + * @return array ['levels' => ['label' => string, 'value' => string, 'checked' => bool][], 'checked' => string|null] | |
| 182 | 199 | */ |
| 183 | 200 | private function prepareLevelsArray(DonationAmountBlockModel $block): array |
| 184 | 201 | { |
| 185 | 202 | $checked = null; |
| @@ -193,8 +210,9 @@ | ||
| 193 | 210 | |
| 194 | 211 | return [ |
| 195 | 212 | 'value' => $item['value'] ?? '', |
| 196 | 213 | 'label' => $block->isDescriptionEnabled() ? $item['label'] : '', |
| 214 | + 'checked' => isset($item['checked']) && $item['checked'], | |
| 197 | 215 | ]; |
| 198 | 216 | }, |
| 199 | 217 | $block->getLevels() |
| 200 | 218 | ), |