StoreBackwardsCompatibleFormMeta.php
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonationForms\Actions; |
| 4 | |
| 5 | use Give\DonationForms\Models\DonationForm; |
| 6 | use Give\DonationForms\V2\ValueObjects\DonationFormMetaKeys; |
| 7 | use Give\DonationForms\ValueObjects\GoalType; |
| 8 | use Give\Donations\ValueObjects\DonationType; |
| 9 | use Give\Framework\FieldsAPI\Amount; |
| 10 | use Give\Framework\FieldsAPI\DonationAmount; |
| 11 | use Give\Framework\FieldsAPI\Field; |
| 12 | |
| 13 | class StoreBackwardsCompatibleFormMeta |
| 14 | { |
| 15 | /** |
| 16 | * @since 3.0.0 |
| 17 | */ |
| 18 | public function __invoke(DonationForm $donationForm) |
| 19 | { |
| 20 | $this->storeDonationLevels($donationForm); |
| 21 | $this->storeDonationGoal($donationForm); |
| 22 | $this->storeRecurringMetaKeys($donationForm); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @since 3.12.0 Update to store donation levels with descriptions |
| 27 | * @since 3.0.0 update with dynamic values from amount field |
| 28 | * @since 3.0.0 |
| 29 | */ |
| 30 | public function storeDonationLevels(DonationForm $donationForm) |
| 31 | { |
| 32 | /** @var Amount $amountField */ |
| 33 | $amountField = $donationForm->schema()->getNodeByName('amount'); |
| 34 | |
| 35 | if (!$amountField) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | $donationLevels = $amountField->getLevels(); |
| 40 | |
| 41 | if ($donationLevels) { |
| 42 | $donationLevels = array_map(static function ($donationLevel, $index) { |
| 43 | return [ |
| 44 | '_give_id' => [ |
| 45 | 'level_id' => $index, |
| 46 | ], |
| 47 | '_give_amount' => $donationLevel['value'], |
| 48 | '_give_text' => $donationLevel['label'] ?? '', |
| 49 | ]; |
| 50 | }, $donationLevels, array_keys($donationLevels)); |
| 51 | |
| 52 | $this->saveSingleFormMeta($donationForm->id, DonationFormMetaKeys::PRICE_OPTION, 'multi'); |
| 53 | $this->saveSingleFormMeta($donationForm->id, DonationFormMetaKeys::DONATION_LEVELS, $donationLevels); |
| 54 | } else { |
| 55 | $this->saveSingleFormMeta($donationForm->id, DonationFormMetaKeys::PRICE_OPTION, 'set'); |
| 56 | $this->saveSingleFormMeta( |
| 57 | $donationForm->id, |
| 58 | DonationFormMetaKeys::SET_PRICE, |
| 59 | $amountField->getFixedAmountValue() |
| 60 | ); |
| 61 | $this->saveSingleFormMeta($donationForm->id, DonationFormMetaKeys::DONATION_LEVELS, []); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @since 3.0.0 |
| 67 | */ |
| 68 | public function storeDonationGoal(DonationForm $donationForm) |
| 69 | { |
| 70 | $this->saveSingleFormMeta( |
| 71 | $donationForm->id, |
| 72 | DonationFormMetaKeys::GOAL_OPTION, |
| 73 | $donationForm->settings->enableDonationGoal ? 'enabled' : 'disabled' |
| 74 | ); |
| 75 | |
| 76 | $goalType = $donationForm->settings->goalType; |
| 77 | |
| 78 | if (!$goalType) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | $legacyGoalType = $this->getLegacyGoalTypeValue($goalType); |
| 83 | |
| 84 | $this->saveSingleFormMeta($donationForm->id, '_give_goal_format', $legacyGoalType); |
| 85 | |
| 86 | $metaLookup = [ |
| 87 | 'donation' => '_give_number_of_donation_goal', |
| 88 | 'donors' => '_give_number_of_donor_goal', |
| 89 | 'amount' => '_give_set_goal', |
| 90 | ]; |
| 91 | |
| 92 | $goalAmount = ('amount' === $legacyGoalType) ? give_sanitize_amount_for_db( |
| 93 | $donationForm->settings->goalAmount |
| 94 | ) : $donationForm->settings->goalAmount; |
| 95 | |
| 96 | $this->saveSingleFormMeta($donationForm->id, $metaLookup[$legacyGoalType], $goalAmount); |
| 97 | |
| 98 | if ($goalType->isOneOf( |
| 99 | GoalType::SUBSCRIPTIONS(), |
| 100 | GoalType::DONORS_FROM_SUBSCRIPTIONS(), |
| 101 | GoalType::AMOUNT_FROM_SUBSCRIPTIONS() |
| 102 | )) { |
| 103 | $this->saveSingleFormMeta($donationForm->id, '_give_recurring_goal_format', 1); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | protected function getLegacyGoalTypeValue(GoalType $goalType): string |
| 108 | { |
| 109 | switch ($goalType): |
| 110 | case GoalType::DONATIONS(): |
| 111 | // Legacy uses "donation" instead of "donations". |
| 112 | return 'donation'; |
| 113 | case GoalType::DONORS(): |
| 114 | return 'donors'; |
| 115 | default: |
| 116 | return 'amount'; |
| 117 | endswitch; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @since 3.0.0 |
| 122 | */ |
| 123 | protected function saveSingleFormMeta($formId, $metaKey, $metaValue) |
| 124 | { |
| 125 | give()->form_meta->update_meta($formId, $metaKey, $metaValue); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @since 3.0.0 |
| 130 | */ |
| 131 | protected function storeRecurringMetaKeys(DonationForm $donationForm) |
| 132 | { |
| 133 | $donationFormSchema = $donationForm->schema(); |
| 134 | |
| 135 | /** @var Field|null $donationTypeField */ |
| 136 | $donationTypeField = $donationFormSchema->getNodeByName('donationType'); |
| 137 | $donationType = !$donationTypeField ? DonationType::SINGLE() : new DonationType( |
| 138 | $donationTypeField->getDefaultValue() |
| 139 | ); |
| 140 | |
| 141 | if (!$donationType->isSubscription()) { |
| 142 | $this->saveSingleFormMeta( |
| 143 | $donationForm->id, |
| 144 | '_give_recurring', |
| 145 | 'no' |
| 146 | ); |
| 147 | |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | /** @var DonationAmount $donationAmountField */ |
| 152 | $donationAmountField = $donationFormSchema->getNodeByName('donationAmount'); |
| 153 | |
| 154 | /** @var Field $subscriptionPeriodField */ |
| 155 | $subscriptionPeriodField = $donationFormSchema->getNodeByName('subscriptionPeriod'); |
| 156 | |
| 157 | /** @var Field $subscriptionFrequencyField */ |
| 158 | $subscriptionFrequencyField = $donationFormSchema->getNodeByName('subscriptionFrequency'); |
| 159 | |
| 160 | /** @var Field $subscriptionInstallmentsField */ |
| 161 | $subscriptionInstallmentsField = $donationFormSchema->getNodeByName('subscriptionInstallments'); |
| 162 | |
| 163 | $this->saveSingleFormMeta( |
| 164 | $donationForm->id, |
| 165 | '_give_recurring', |
| 166 | $donationAmountField->subscriptionDetailsAreFixed ? 'yes_admin' : 'yes' |
| 167 | ); |
| 168 | |
| 169 | // period |
| 170 | $this->saveSingleFormMeta( |
| 171 | $donationForm->id, |
| 172 | '_give_period', |
| 173 | $subscriptionPeriodField->getDefaultValue() |
| 174 | ); |
| 175 | |
| 176 | // frequency |
| 177 | $this->saveSingleFormMeta( |
| 178 | $donationForm->id, |
| 179 | '_give_period_interval', |
| 180 | $subscriptionFrequencyField->getDefaultValue() |
| 181 | ); |
| 182 | |
| 183 | // times |
| 184 | $this->saveSingleFormMeta( |
| 185 | $donationForm->id, |
| 186 | '_give_times', |
| 187 | $subscriptionInstallmentsField->getDefaultValue() |
| 188 | ); |
| 189 | } |
| 190 | } |
| 191 |