PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 3.15.0
GiveWP – Donation Plugin and Fundraising Platform v3.15.0
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 2.31.0 2.31.1 All 253 releases
← All changes | src/Subscriptions/Repositories/SubscriptionRepository.php +14 -180 4.16.53.15.0 View file →
@@ -2,13 +2,9 @@
2 2
3 3 namespace Give\Subscriptions\Repositories;
4 4
5 5 use Exception;
6 -use Give\Donations\Models\Donation;
7 6 use Give\Donations\ValueObjects\DonationMetaKeys;
8 -use Give\Donations\ValueObjects\DonationMode;
9 -use Give\Donations\ValueObjects\DonationStatus;
10 -use Give\Donations\ValueObjects\DonationType;
11 7 use Give\Framework\Database\DB;
12 8 use Give\Framework\Exceptions\Primitives\InvalidArgumentException;
13 9 use Give\Framework\Models\ModelQueryBuilder;
14 10 use Give\Framework\Support\Facades\DateTime\Temporal;
@@ -15,22 +11,15 @@
15 11 use Give\Helpers\Hooks;
16 12 use Give\Log\Log;
17 13 use Give\Subscriptions\Models\Subscription;
18 14 use Give\Subscriptions\ValueObjects\SubscriptionMode;
19 -use Give\Subscriptions\ValueObjects\SubscriptionStatus;
20 15
21 16 /**
22 - * @since 4.8.0 Add notes repository
23 17 * @since 2.19.6
24 18 */
25 19 class SubscriptionRepository
26 20 {
27 21 /**
28 - * @var SubscriptionNotesRepository
29 - */
30 - public $notes;
31 -
32 - /**
33 22 * @var string[]
34 23 */
35 24 private $requiredSubscriptionProperties = [
36 25 'donorId',
@@ -41,16 +30,8 @@
41 30 'donationFormId',
42 31 ];
43 32
44 33 /**
45 - * @since 4.8.0
46 - */
47 - public function __construct()
48 - {
49 - $this->notes = give(SubscriptionNotesRepository::class);
50 - }
51 -
52 - /**
53 34 * @since 2.19.6
54 35 *
55 36 * @return Subscription|null
56 37 */
@@ -85,14 +66,16 @@
85 66
86 67 /**
87 68 * @since 2.21.0
88 69 *
70 + * @param string $gatewayTransactionId
71 + *
89 72 * @return ModelQueryBuilder<Subscription>
90 73 */
91 - public function queryByGatewaySubscriptionId(string $gatewaySubscriptionId): ModelQueryBuilder
74 + public function queryByGatewaySubscriptionId(string $gatewayTransactionId): ModelQueryBuilder
92 75 {
93 76 return $this->prepareQuery()
94 - ->where('profile_id', $gatewaySubscriptionId);
77 + ->where('profile_id', $gatewayTransactionId);
95 78 }
96 79
97 80 /**
98 81 * @since 2.19.6
@@ -120,22 +103,8 @@
120 103 ->where('customer_id', $donorId);
121 104 }
122 105
123 106 /**
124 - * @since 4.11.0
125 - *
126 - * @param int $campaignId
127 - *
128 - * @return ModelQueryBuilder<Subscription>
129 - */
130 - public function queryByCampaignId(int $campaignId): ModelQueryBuilder
131 - {
132 - return $this->prepareQuery()
133 - ->where('campaign_id', $campaignId);
134 - }
135 -
136 - /**
137 - * @deprecated Use give()->subscriptions->notes()->queryBySubscriptionId()->getAll() instead.
138 107 * @since 2.19.6
139 108 *
140 109 * @return object[]
141 110 */
@@ -140,20 +109,18 @@
140 109 * @return object[]
141 110 */
142 111 public function getNotesBySubscriptionId(int $id): array
143 112 {
144 - _give_deprecated_function(__METHOD__, '4.6.0', 'give()->subscriptions->notes()->queryBySubscriptionId()->getAll()');
113 + $notes = DB::table('comments')
114 + ->select(
115 + ['comment_content', 'note'],
116 + ['comment_date', 'date']
117 + )
118 + ->where('comment_post_ID', $id)
119 + ->where('comment_type', 'give_sub_note')
120 + ->orderBy('comment_date', 'DESC')
121 + ->getAll();
145 122
146 - $notes = array_map(
147 - static function ($note) {
148 - return array_merge($note->toArray(), [
149 - 'note' => $note->comment_content,
150 - 'date' => $note->comment_date,
151 - ]);
152 - },
153 - $this->notes->queryBySubscriptionId($id)->getAll()
154 - );
155 -
156 123 if (!$notes) {
157 124 return [];
158 125 }
159 126
@@ -160,9 +127,8 @@
160 127 return $notes;
161 128 }
162 129
163 130 /**
164 - * @since 4.11.0 add campaign_id column to insert
165 131 * @since 2.24.0 add payment_mode column to insert
166 132 * @since 2.21.0 replace actions with givewp_subscription_creating and givewp_subscription_created
167 133 * @since 2.19.6
168 134 *
@@ -176,14 +142,8 @@
176 142 if ($subscription->renewsAt === null) {
177 143 $subscription->bumpRenewalDate();
178 144 }
179 145
180 - // If the subscription is not associated with a campaign, try to find the campaign ID by the form ID
181 - if (!$subscription->campaignId) {
182 - $campaign = give()->campaigns->getByFormId($subscription->donationFormId);
183 - $subscription->campaignId = $campaign ? $campaign->id : null;
184 - }
185 -
186 146 Hooks::doAction('givewp_subscription_creating', $subscription);
187 147
188 148 $dateCreated = Temporal::withoutMicroseconds($subscription->createdAt ?: Temporal::getCurrentDateTime());
189 149
@@ -204,9 +164,8 @@
204 164 ) : 0,
205 165 'bill_times' => $subscription->installments,
206 166 'transaction_id' => $subscription->transactionId ?? '',
207 167 'product_id' => $subscription->donationFormId,
208 - 'campaign_id' => $subscription->campaignId,
209 168 'payment_mode' => $subscription->mode->getValue(),
210 169 ]);
211 170 } catch (Exception $exception) {
212 171 DB::query('ROLLBACK');
@@ -226,10 +185,8 @@
226 185 Hooks::doAction('givewp_subscription_created', $subscription);
227 186 }
228 187
229 188 /**
230 - * @since 4.11.0 add campaign_id column to update
231 - * @since 3.17.0 add expiration column to update
232 189 * @since 2.24.0 add payment_mode column to update
233 190 * @since 2.21.0 replace actions with givewp_subscription_updating and givewp_subscription_updated
234 191 * @since 2.19.6
235 192 *
@@ -251,9 +208,8 @@
251 208 try {
252 209 DB::table('give_subscriptions')
253 210 ->where('id', $subscription->id)
254 211 ->update([
255 - 'expiration' => Temporal::getFormattedDateTime($subscription->renewsAt),
256 212 'status' => $subscription->status->getValue(),
257 213 'profile_id' => $subscription->gatewaySubscriptionId,
258 214 'customer_id' => $subscription->donorId,
259 215 'period' => $subscription->period->getValue(),
@@ -264,9 +220,8 @@
264 220 ) : 0,
265 221 'bill_times' => $subscription->installments,
266 222 'transaction_id' => $subscription->transactionId ?? '',
267 223 'product_id' => $subscription->donationFormId,
268 - 'campaign_id' => $subscription->campaignId,
269 224 'payment_mode' => $subscription->mode->getValue(),
270 225 ]);
271 226 } catch (Exception $exception) {
272 227 DB::query('ROLLBACK');
@@ -317,82 +272,8 @@
317 272 return true;
318 273 }
319 274
320 275 /**
321 - * @since 4.8.0
322 - *
323 - * @throws Exception
324 - */
325 - public function trash(Subscription $subscription): bool
326 - {
327 - DB::query('START TRANSACTION');
328 -
329 - Hooks::doAction('givewp_subscription_trashing', $subscription);
330 -
331 - try {
332 - $previousStatus = DB::table('give_subscriptions')
333 - ->where('id', $subscription->id)
334 - ->value('status');
335 -
336 - give()->subscription_meta->update_meta($subscription->id, '_wp_trash_meta_status', $previousStatus);
337 - give()->subscription_meta->update_meta($subscription->id, '_wp_trash_meta_time', time());
338 -
339 - DB::table('give_subscriptions')
340 - ->where('id', $subscription->id)
341 - ->update(['status' => SubscriptionStatus::TRASHED()->getValue()]);
342 - } catch (Exception $exception) {
343 - DB::query('ROLLBACK');
344 -
345 - Log::error('Failed trashing a subscription', compact('subscription'));
346 -
347 - throw new $exception('Failed trashing a subscription');
348 - }
349 -
350 - DB::query('COMMIT');
351 -
352 - Hooks::doAction('givewp_subscription_trashed', $subscription);
353 -
354 - return true;
355 - }
356 -
357 - /**
358 - * @since 4.12.0
359 - *
360 - * @throws Exception
361 - */
362 - public function unTrash(Subscription $subscription): bool
363 - {
364 - DB::query('START TRANSACTION');
365 -
366 - Hooks::doAction('givewp_subscription_untrashing', $subscription);
367 -
368 - try {
369 - $previousStatus = give()->subscription_meta->get_meta($subscription->id, '_wp_trash_meta_status', true);
370 -
371 - // If no previous status was saved, default to 'active'
372 - if (empty($previousStatus)) {
373 - $previousStatus = SubscriptionStatus::ACTIVE;
374 - }
375 -
376 - DB::table('give_subscriptions')
377 - ->where('id', $subscription->id)
378 - ->update(['status' => $previousStatus]);
379 - } catch (Exception $exception) {
380 - DB::query('ROLLBACK');
381 -
382 - Log::error('Failed untrashing a subscription', compact('subscription'));
383 -
384 - throw new $exception('Failed untrashing a subscription');
385 - }
386 -
387 - DB::query('COMMIT');
388 -
389 - Hooks::doAction('givewp_subscription_untrashed', $subscription);
390 -
391 - return true;
392 - }
393 -
394 - /**
395 276 * Up to this point the donation is created first and then the subscription, and the donation is stored as the
396 277 * parent_payment_id of the subscription. This is backwards and should not be the case. But legacy code depends on
397 278 * this value, so we still need to store it for now.
398 279 *
@@ -492,53 +373,8 @@
492 373 return (int)$query->ID;
493 374 }
494 375
495 376 /**
496 - * @since 4.11.0 add campaignId to renewal
497 - * @since 4.8.1 Remove campaignId from the attributes array since it is auto-generated based on the subscription's form.
498 - * @since 4.8.0 Add campaignId support.
499 - * @since 3.20.0
500 - * @throws Exception
501 - */
502 - public function createRenewal(Subscription $subscription, array $attributes = []): Donation
503 - {
504 - $initialDonation = $subscription->initialDonation();
505 -
506 - $donation = Donation::create(
507 - array_merge([
508 - 'subscriptionId' => $subscription->id,
509 - 'gatewayId' => $subscription->gatewayId,
510 - 'amount' => $subscription->amount,
511 - 'status' => DonationStatus::COMPLETE(),
512 - 'type' => DonationType::RENEWAL(),
513 - 'donorId' => $subscription->donorId,
514 - 'formId' => $subscription->donationFormId,
515 - 'honorific' => $initialDonation->honorific,
516 - 'firstName' => $initialDonation->firstName,
517 - 'lastName' => $initialDonation->lastName,
518 - 'email' => $initialDonation->email,
519 - 'phone' => $initialDonation->phone,
520 - 'anonymous' => $initialDonation->anonymous,
521 - 'levelId' => $initialDonation->levelId,
522 - 'company' => $initialDonation->company,
523 - 'comment' => $initialDonation->comment,
524 - 'billingAddress' => $initialDonation->billingAddress,
525 - 'feeAmountRecovered' => $subscription->feeAmountRecovered,
526 - 'exchangeRate' => $initialDonation->exchangeRate,
527 - 'formTitle' => $initialDonation->formTitle,
528 - 'mode' => $subscription->mode->isLive() ? DonationMode::LIVE() : DonationMode::TEST(),
529 - 'donorIp' => $initialDonation->donorIp,
530 - 'campaignId' => $subscription->campaignId,
531 - ], $attributes)
532 - );
533 -
534 - $subscription->bumpRenewalDate();
535 - $subscription->save();
536 -
537 - return $donation;
538 - }
539 -
540 - /**
541 377 * @since 2.19.6
542 378 *
543 379 * @return void
544 380 */
@@ -555,9 +391,8 @@
555 391 }
556 392 }
557 393
558 394 /**
559 - * @since 4.11.0 add campaign_id column to select
560 395 * @since 2.19.6
561 396 *
562 397 * @return ModelQueryBuilder<Subscription>
563 398 */
@@ -579,10 +414,9 @@
579 414 ['recurring_amount', 'amount'],
580 415 ['recurring_fee_amount', 'feeAmount'],
581 416 'status',
582 417 ['profile_id', 'gatewaySubscriptionId'],
583 - ['product_id', 'donationFormId'],
584 - ['campaign_id', 'campaignId']
418 + ['product_id', 'donationFormId']
585 419 )
586 420 ->attachMeta(
587 421 'give_donationmeta',
588 422 'parent_payment_id',