PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
← All changes | src/DonationForms/V2/DataTransferObjects/DonationFormQueryData.php +105 -9 2.30.04.16.9 View file →
@@ -2,12 +2,17 @@
2 2
3 3 namespace Give\DonationForms\V2\DataTransferObjects;
4 4
5 5 use DateTime;
6 +use Give\Campaigns\Models\Campaign;
7 +use Give\DonationForms\Properties\FormSettings;
8 +use Give\DonationForms\Properties\GoalSettings;
6 9 use Give\DonationForms\V2\Models\DonationForm;
7 10 use Give\DonationForms\V2\Properties\DonationFormLevel;
8 -use Give\DonationForms\V2\ValueObjects\DonationFormMetaKeys;
11 +use Give\DonationForms\V2\ValueObjects\DonationFormMetaKeys as LegacyDonationFormMetaKeys;
9 12 use Give\DonationForms\V2\ValueObjects\DonationFormStatus;
13 +use Give\DonationForms\ValueObjects\DonationFormMetaKeys;
14 +use Give\DonationForms\ValueObjects\GoalType;
10 15 use Give\Framework\Support\Facades\DateTime\Temporal;
11 16 use Give\Framework\Support\ValueObjects\Money;
12 17
13 18 /**
@@ -12,9 +17,10 @@
12 17
13 18 /**
14 19 * Class DonationFormQueryData
15 20 *
16 - * @since 2.24.0
21 + * @since 4.3.0 add GoalSettings
22 + * @since 2.24.0
17 23 */
18 24 final class DonationFormQueryData
19 25 {
20 26
@@ -55,8 +61,23 @@
55 61 */
56 62 public $status;
57 63
58 64 /**
65 + * @since 4.3.0
66 + */
67 + public GoalSettings $goalSettings;
68 +
69 + /**
70 + * @since 4.3.0
71 + */
72 + public bool $usesFormBuilder;
73 +
74 + /**
75 + * @since 4.3.0
76 + */
77 + public int $campaignId;
78 +
79 + /**
59 80 * Convert data from donation form object to DonationForm Model
60 81 *
61 82 * @since 2.24.0
62 83 *
@@ -65,19 +86,23 @@
65 86 * @return DonationFormQueryData
66 87 */
67 88 public static function fromObject($object): DonationFormQueryData
68 89 {
69 - $self = new static();
90 + $self = new DonationFormQueryData();
70 91
92 + $self->campaignId = 0;
71 93 $self->id = (int)$object->id;
72 94 $self->title = $object->title;
73 95 $self->levels = $self->getDonationFormLevels($object);
74 - $self->goalOption = ($object->{DonationFormMetaKeys::GOAL_OPTION()->getKeyAsCamelCase()} === 'enabled');
96 + $self->goalOption = ($object->{LegacyDonationFormMetaKeys::GOAL_OPTION()->getKeyAsCamelCase()} === 'enabled');
75 97 $self->createdAt = Temporal::toDateTime($object->createdAt);
76 98 $self->updatedAt = Temporal::toDateTime($object->updatedAt);
77 - $self->totalAmountDonated = Money::fromDecimal($object->{DonationFormMetaKeys::FORM_EARNINGS()->getKeyAsCamelCase()}, give_get_currency());
78 - $self->totalNumberOfDonations = (int)$object->{DonationFormMetaKeys::FORM_SALES()->getKeyAsCamelCase()};
99 + $self->totalAmountDonated = Money::fromDecimal($object->{LegacyDonationFormMetaKeys::FORM_EARNINGS()->getKeyAsCamelCase()},
100 + give_get_currency());
101 + $self->totalNumberOfDonations = (int)$object->{LegacyDonationFormMetaKeys::FORM_SALES()->getKeyAsCamelCase()};
79 102 $self->status = new DonationFormStatus($object->status);
103 + $self->goalSettings = $self->getGoalSettings($object);
104 + $self->usesFormBuilder = (bool)$object->settings;
80 105
81 106 return $self;
82 107 }
83 108
@@ -101,11 +126,11 @@
101 126 * @return DonationFormLevel[]
102 127 */
103 128 public function getDonationFormLevels($object): array
104 129 {
105 - switch( $object->{DonationFormMetaKeys::PRICE_OPTION()->getKeyAsCamelCase()} ) {
130 + switch ($object->{LegacyDonationFormMetaKeys::PRICE_OPTION()->getKeyAsCamelCase()}) {
106 131 case 'multi':
107 - $levels = maybe_unserialize($object->{DonationFormMetaKeys::DONATION_LEVELS()->getKeyAsCamelCase()});
132 + $levels = maybe_unserialize($object->{LegacyDonationFormMetaKeys::DONATION_LEVELS()->getKeyAsCamelCase()});
108 133
109 134 if (empty($levels)) {
110 135 return [];
111 136 }
@@ -113,9 +138,9 @@
113 138 return array_map(static function ($level) {
114 139 return DonationFormLevel::fromArray($level);
115 140 }, $levels);
116 141 case 'set':
117 - $amount = $object->{DonationFormMetaKeys::SET_PRICE()->getKeyAsCamelCase()};
142 + $amount = $object->{LegacyDonationFormMetaKeys::SET_PRICE()->getKeyAsCamelCase()};
118 143
119 144 if (empty($amount)) {
120 145 return [];
121 146 }
@@ -124,7 +149,78 @@
124 149 DonationFormLevel::fromPrice($amount),
125 150 ];
126 151 default:
127 152 return [];
153 + }
154 + }
155 +
156 +
157 + /**
158 + * @since 4.6.0 Cast $queryObject->goalFormat to string
159 + * @since 4.3.0
160 + */
161 + private function getGoalSettings(object $queryObject): GoalSettings
162 + {
163 + $formSettings = $queryObject->{DonationFormMetaKeys::SETTINGS()->getKeyAsCamelCase()};
164 +
165 + // v3 form
166 + if ($formSettings) {
167 + $settings = FormSettings::fromjson($formSettings);
168 + // uses campaign goal settings
169 + if ($settings->goalSource->isCampaign()) {
170 + $campaign = Campaign::findByFormId($queryObject->id);
171 + $this->campaignId = $campaign->id;
172 +
173 + return GoalSettings::fromArray([
174 + 'goalSource' => $settings->goalSource->getValue(),
175 + 'enableDonationGoal' => $settings->enableDonationGoal,
176 + 'goalType' => $this->convertGoalType($campaign->goalType->getValue()),
177 + 'goalAmount' => $campaign->goal,
178 + ]);
179 + }
180 +
181 + return GoalSettings::fromArray([
182 + 'goalSource' => $settings->goalSource->getValue(),
183 + 'enableDonationGoal' => $settings->enableDonationGoal,
184 + 'goalType' => $settings->goalType,
185 + 'goalAmount' => $settings->goalAmount,
186 + ]);
187 + }
188 +
189 + // v2 form
190 + return GoalSettings::fromArray([
191 + 'goalSource' => 'form',
192 + 'enableDonationGoal' => $queryObject->goalOption === 'enabled',
193 + 'goalType' => $this->convertGoalType((string)$queryObject->goalFormat, (bool)$queryObject->recurringGoalFormat),
194 + 'goalAmount' => $queryObject->goalAmount,
195 + ]);
196 + }
197 +
198 +
199 + /**
200 + * @since 4.3.0
201 + */
202 + public function convertGoalType(string $type, bool $isRecurring = false): GoalType
203 + {
204 + switch ($type) {
205 + case 'donation':
206 + case 'donations':
207 + return $isRecurring
208 + ? GoalType::SUBSCRIPTIONS()
209 + : GoalType::DONATIONS();
210 + case 'donors':
211 + return $isRecurring
212 + ? GoalType::DONORS_FROM_SUBSCRIPTIONS()
213 + : GoalType::DONORS();
214 + case 'subscriptions':
215 + return GoalType::SUBSCRIPTIONS();
216 + case 'donorsFromSubscriptions':
217 + return GoalType::DONORS_FROM_SUBSCRIPTIONS();
218 + case 'amountFromSubscriptions':
219 + return GoalType::AMOUNT_FROM_SUBSCRIPTIONS();
220 + default:
221 + return $isRecurring
222 + ? GoalType::AMOUNT_FROM_SUBSCRIPTIONS()
223 + : GoalType::AMOUNT();
128 224 }
129 225 }
130 226 }