PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.1.0
GiveWP – Donation Plugin and Fundraising Platform v4.1.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
give / src / Campaigns / CampaignsDataQuery.php
CampaignsDataQuery.php
135 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\Campaigns;
4
5 use Give\Donations\ValueObjects\DonationMetaKeys;
6 use Give\Framework\QueryBuilder\JoinQueryBuilder;
7 use Give\Framework\QueryBuilder\QueryBuilder;
8
9 /**
10 * Class used for loading the number of donors, donations and revenue amounts for multiple campaigns
11 *
12 * @since 4.0.0
13 */
14 class CampaignsDataQuery extends QueryBuilder
15 {
16 /**
17 * @since 4.0.0
18 *
19 * @param int[] $campaignIds
20 */
21 private function __construct(array $campaignIds)
22 {
23 $this->select('campaignId.meta_value as campaign_id');
24 $this->whereIn('donation.post_status', ['publish', 'give_subscription']);
25 $this->whereIn('campaignId.meta_value', $campaignIds);
26 $this->groupBy('campaign_id');
27 }
28
29
30 /**
31 * Donations query for campaigns
32 *
33 * @param int[] $campaignIds - campaign ids
34 *
35 * @return CampaignsDataQuery
36 */
37 public static function donations(array $campaignIds): CampaignsDataQuery
38 {
39 return (new self($campaignIds))
40 ->from('posts', 'donation')
41 ->where('post_type', 'give_payment')
42 ->joinDonationMeta(DonationMetaKeys::CAMPAIGN_ID, 'campaignId')
43 ->joinDonationMeta(DonationMetaKeys::MODE, 'paymentMode')
44 ->where('paymentMode.meta_value', give_is_test_mode() ? 'test' : 'live');
45 }
46
47 /**
48 * Subscriptions query for campaigns
49 *
50 * @param int[] $campaignIds - campaign ids
51 *
52 * @return CampaignsDataQuery
53 */
54 public static function subscriptions(array $campaignIds): CampaignsDataQuery
55 {
56 return (new self($campaignIds))
57 ->from('give_subscriptions', 'subscription')
58 ->join(function (JoinQueryBuilder $builder) {
59 $builder
60 ->leftJoin('posts', 'donation')
61 ->on('subscription.parent_payment_id', 'donation.ID');
62 })
63 ->joinDonationMeta(DonationMetaKeys::CAMPAIGN_ID, 'campaignId')
64 ->where('payment_mode', give_is_test_mode() ? 'test' : 'live')
65 ->where('donation.post_type', 'give_payment');
66 }
67
68 /**
69 * Returns a calculated sum of the intended amounts (without recovered fees) for the donations.
70 *
71 * @since 4.0.0
72 *
73 * @return array|object|null
74 */
75 public function collectIntendedAmounts()
76 {
77 return (clone $this)
78 ->select('SUM(COALESCE(NULLIF(intendedAmount.meta_value,0), NULLIF(amount.meta_value,0))) as sum')
79 ->joinDonationMeta(DonationMetaKeys::AMOUNT, 'amount')
80 ->joinDonationMeta('_give_fee_donation_amount', 'intendedAmount')
81 ->getAll(ARRAY_A);
82 }
83
84 /**
85 * Returns a calculated sum of the initial amounts
86 *
87 * @since 4.0.0
88 *
89 * @return array|object|null
90 */
91 public function collectInitialAmounts()
92 {
93 return (clone $this)
94 ->select('SUM(initial_amount) as sum')
95 ->getAll(ARRAY_A);
96 }
97
98 /**
99 * @since 4.0.0
100 */
101 public function collectDonations()
102 {
103 return (clone $this)
104 ->select('COUNT(donation.ID) as count')
105 ->getAll(ARRAY_A);
106 }
107
108 /**
109 * @since 4.0.0
110 */
111 public function collectDonors()
112 {
113 return (clone $this)
114 ->select('COUNT(DISTINCT donorId.meta_value) as count')
115 ->joinDonationMeta(DonationMetaKeys::DONOR_ID, 'donorId')
116 ->getAll(ARRAY_A);
117 }
118
119 /**
120 * An opinionated join method for the donation meta table.
121 * @since 4.0.0
122 */
123 private function joinDonationMeta($key, $alias): self
124 {
125 $this->join(function (JoinQueryBuilder $builder) use ($key, $alias) {
126 $builder
127 ->leftJoin('give_donationmeta', $alias)
128 ->on('donation.ID', $alias . '.donation_id')
129 ->andOn($alias . '.meta_key', $key, true);
130 });
131
132 return $this;
133 }
134 }
135