AverageDonation.php
6 years ago
DonationsVsIncome.php
6 years ago
Endpoint.php
6 years ago
FormPerformance.php
6 years ago
Income.php
6 years ago
IncomeBreakdown.php
6 years ago
PaymentMethods.php
6 years ago
PaymentStatuses.php
6 years ago
RecentDonations.php
6 years ago
TopDonors.php
6 years ago
TotalDonors.php
6 years ago
TotalIncome.php
6 years ago
TotalRefunds.php
6 years ago
TopDonors.php
91 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Top Donors endpoint |
| 5 | * |
| 6 | * @package Give |
| 7 | */ |
| 8 | |
| 9 | namespace Give\API\Endpoints\Reports; |
| 10 | |
| 11 | class TopDonors extends Endpoint { |
| 12 | |
| 13 | public function __construct() { |
| 14 | $this->endpoint = 'top-donors'; |
| 15 | } |
| 16 | |
| 17 | public function get_report( $request ) { |
| 18 | |
| 19 | // Check if a cached version exists |
| 20 | $cached_report = $this->get_cached_report( $request ); |
| 21 | if ( $cached_report !== null ) { |
| 22 | // Bail and return the cached version |
| 23 | return new \WP_REST_Response( |
| 24 | [ |
| 25 | 'data' => $cached_report, |
| 26 | ] |
| 27 | ); |
| 28 | } |
| 29 | |
| 30 | $start = date_create( $request['start'] ); |
| 31 | $end = date_create( $request['end'] ); |
| 32 | $diff = date_diff( $start, $end ); |
| 33 | |
| 34 | $dataset = []; |
| 35 | |
| 36 | $data = $this->get_data( $start, $end ); |
| 37 | |
| 38 | // Cache the report data |
| 39 | $result = $this->cache_report( $request, $data ); |
| 40 | $status = $this->get_give_status(); |
| 41 | |
| 42 | return new \WP_REST_Response( |
| 43 | [ |
| 44 | 'data' => $data, |
| 45 | 'status' => $status, |
| 46 | ] |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | public function get_data( $start, $end ) { |
| 51 | |
| 52 | $this->payments = $this->get_payments( $start->format( 'Y-m-d' ), $end->format( 'Y-m-d 23:i:s' ), 'date', -1 ); |
| 53 | |
| 54 | $donors = []; |
| 55 | |
| 56 | foreach ( $this->payments as $payment ) { |
| 57 | if ( $payment->status === 'publish' || $payment->status === 'give_subscription' ) { |
| 58 | $donors[ $payment->donor_id ]['type'] = 'donor'; |
| 59 | $donors[ $payment->donor_id ]['earnings'] = isset( $donors[ $payment->donor_id ]['earnings'] ) ? $donors[ $payment->donor_id ]['earnings'] += $payment->total : $payment->total; |
| 60 | $donors[ $payment->donor_id ]['total'] = give_currency_filter( give_format_amount( $donors[ $payment->donor_id ]['earnings'], array( 'sanitize' => false ) ), [ 'decode_currency' => true ] ); |
| 61 | $donors[ $payment->donor_id ]['donations'] = isset( $donors[ $payment->donor_id ]['donations'] ) ? $donors[ $payment->donor_id ]['donations'] += 1 : 1; |
| 62 | $countLabel = _n( 'Donation', 'Donations', $donors[ $payment->donor_id ]['donations'], 'give' ); |
| 63 | $donors[ $payment->donor_id ]['count'] = $donors[ $payment->donor_id ]['donations'] . ' ' . $countLabel; |
| 64 | $donors[ $payment->donor_id ]['name'] = $payment->first_name . ' ' . $payment->last_name; |
| 65 | $donors[ $payment->donor_id ]['email'] = $payment->email; |
| 66 | $donors[ $payment->donor_id ]['image'] = give_validate_gravatar( $payment->email ) ? get_avatar_url( $payment->email, 60 ) : null; |
| 67 | $donors[ $payment->donor_id ]['url'] = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . absint( $payment->donor_id ) ); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | $sorted = usort( |
| 72 | $donors, |
| 73 | function ( $a, $b ) { |
| 74 | if ( $a['earnings'] == $b['earnings'] ) { |
| 75 | return 0; |
| 76 | } |
| 77 | return ( $a['earnings'] > $b['earnings'] ) ? -1 : 1; |
| 78 | } |
| 79 | ); |
| 80 | |
| 81 | if ( $sorted === true ) { |
| 82 | $donors = array_slice( $donors, 0, 25 ); |
| 83 | $donors = array_values( $donors ); |
| 84 | } |
| 85 | |
| 86 | return $donors; |
| 87 | |
| 88 | } |
| 89 | |
| 90 | } |
| 91 |