| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Tracking\TrackingData; |
| 4 |
|
| 5 |
use Give\Framework\Database\DB; |
| 6 |
use Give\Tracking\Contracts\TrackData; |
| 7 |
use Give\Tracking\Helpers\DonationStatuses; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class DonationMetricsData |
| 11 |
* @package Give\Tracking\TrackingData |
| 12 |
* |
| 13 |
* @since 2.10.2 |
| 14 |
*/ |
| 15 |
class DonationMetricsData implements TrackData |
| 16 |
{ |
| 17 |
/** |
| 18 |
* @var array |
| 19 |
*/ |
| 20 |
private $donationData = []; |
| 21 |
|
| 22 |
/** |
| 23 |
* @var int |
| 24 |
*/ |
| 25 |
private $donorCount = 0; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var int |
| 29 |
*/ |
| 30 |
private $formCount = 0; |
| 31 |
|
| 32 |
/** |
| 33 |
* @inheritdoc |
| 34 |
* @return array|void |
| 35 |
*/ |
| 36 |
public function get() |
| 37 |
{ |
| 38 |
$this->donorCount = $this->getDonorCount(); |
| 39 |
$this->formCount = $this->getDonationFormCount(); |
| 40 |
$this->donationData = (new DonationData())->get(); |
| 41 |
|
| 42 |
$data = [ |
| 43 |
'form_count' => $this->formCount, |
| 44 |
'donor_count' => $this->donorCount, |
| 45 |
'avg_donation_amount_by_donor' => $this->getAvgDonationAmountByDonor(), |
| 46 |
]; |
| 47 |
|
| 48 |
return array_merge($data, $this->donationData); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Returns donor count which donated greater then zero |
| 53 |
* |
| 54 |
* @since 2.10.0 |
| 55 |
* @return int |
| 56 |
*/ |
| 57 |
private function getDonorCount() |
| 58 |
{ |
| 59 |
global $wpdb; |
| 60 |
|
| 61 |
$statues = DonationStatuses::getCompletedDonationsStatues(true); |
| 62 |
|
| 63 |
$donorCount = DB::get_var( |
| 64 |
" |
| 65 |
SELECT COUNT(DISTINCT dm.meta_value) |
| 66 |
FROM {$wpdb->donationmeta} as dm |
| 67 |
INNER JOIN {$wpdb->posts} as p ON dm.donation_id = p.ID |
| 68 |
INNER JOIN {$wpdb->donationmeta} as dm2 ON dm.donation_id = dm2.donation_id |
| 69 |
INNER JOIN {$wpdb->donors} as donor ON dm.meta_value = donor.id |
| 70 |
WHERE p.post_status IN ({$statues}) |
| 71 |
AND p.post_type='give_payment' |
| 72 |
AND dm2.meta_key='_give_payment_mode' |
| 73 |
AND dm2.meta_value='live' |
| 74 |
AND dm.meta_key='_give_payment_donor_id' |
| 75 |
AND donor.purchase_value > 0 |
| 76 |
" |
| 77 |
); |
| 78 |
|
| 79 |
return (int)$donorCount; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Get average donation by donor. |
| 84 |
* |
| 85 |
* @since 2.10.0 |
| 86 |
* @return int |
| 87 |
*/ |
| 88 |
private function getAvgDonationAmountByDonor() |
| 89 |
{ |
| 90 |
$amount = 0; |
| 91 |
|
| 92 |
if ($this->donationData['revenue']) { |
| 93 |
$amount = (int)($this->donationData['revenue'] / $this->donorCount); |
| 94 |
} |
| 95 |
|
| 96 |
return $amount; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Returns donation form count |
| 101 |
* |
| 102 |
* @since 2.10.0 |
| 103 |
* @return int |
| 104 |
*/ |
| 105 |
private function getDonationFormCount() |
| 106 |
{ |
| 107 |
global $wpdb; |
| 108 |
|
| 109 |
$statues = DonationStatuses::getCompletedDonationsStatues(true); |
| 110 |
|
| 111 |
$formCount = DB::get_var( |
| 112 |
" |
| 113 |
SELECT COUNT(DISTINCT dm.meta_value) |
| 114 |
FROM {$wpdb->donationmeta} as dm |
| 115 |
INNER JOIN {$wpdb->posts} as p ON dm.donation_id = p.ID |
| 116 |
INNER JOIN {$wpdb->donationmeta} as dm2 ON dm.donation_id = dm2.donation_id |
| 117 |
WHERE p.post_status IN ({$statues}) |
| 118 |
AND p.post_type='give_payment' |
| 119 |
AND dm2.meta_key='_give_payment_mode' |
| 120 |
AND dm2.meta_value='live' |
| 121 |
AND dm.meta_key='_give_payment_form_id' |
| 122 |
" |
| 123 |
); |
| 124 |
|
| 125 |
return (int)$formCount; |
| 126 |
} |
| 127 |
} |
| 128 |
|