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
65 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 | $start = date_create( $request->get_param( 'start' ) ); |
| 19 | $end = date_create( $request->get_param( 'end' ) ); |
| 20 | |
| 21 | return $this->get_data( $start, $end ); |
| 22 | } |
| 23 | |
| 24 | public function get_data( $start, $end ) { |
| 25 | |
| 26 | $this->payments = $this->get_payments( $start->format( 'Y-m-d' ), $end->format( 'Y-m-d 23:i:s' ), 'date', -1 ); |
| 27 | |
| 28 | $donors = array(); |
| 29 | |
| 30 | foreach ( $this->payments as $payment ) { |
| 31 | if ( $payment->status === 'publish' || $payment->status === 'give_subscription' ) { |
| 32 | $donors[ $payment->donor_id ]['type'] = 'donor'; |
| 33 | $donors[ $payment->donor_id ]['earnings'] = isset( $donors[ $payment->donor_id ]['earnings'] ) ? $donors[ $payment->donor_id ]['earnings'] += $payment->total : $payment->total; |
| 34 | $donors[ $payment->donor_id ]['total'] = give_currency_filter( give_format_amount( $donors[ $payment->donor_id ]['earnings'], array( 'sanitize' => false ) ), array( 'decode_currency' => true ) ); |
| 35 | $donors[ $payment->donor_id ]['donations'] = isset( $donors[ $payment->donor_id ]['donations'] ) ? $donors[ $payment->donor_id ]['donations'] += 1 : 1; |
| 36 | $countLabel = _n( 'Donation', 'Donations', $donors[ $payment->donor_id ]['donations'], 'give' ); |
| 37 | $donors[ $payment->donor_id ]['count'] = $donors[ $payment->donor_id ]['donations'] . ' ' . $countLabel; |
| 38 | $donors[ $payment->donor_id ]['name'] = $payment->first_name . ' ' . $payment->last_name; |
| 39 | $donors[ $payment->donor_id ]['email'] = $payment->email; |
| 40 | $donors[ $payment->donor_id ]['image'] = give_validate_gravatar( $payment->email ) ? get_avatar_url( $payment->email, 60 ) : null; |
| 41 | $donors[ $payment->donor_id ]['url'] = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . absint( $payment->donor_id ) ); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | $sorted = usort( |
| 46 | $donors, |
| 47 | function ( $a, $b ) { |
| 48 | if ( $a['earnings'] == $b['earnings'] ) { |
| 49 | return 0; |
| 50 | } |
| 51 | return ( $a['earnings'] > $b['earnings'] ) ? -1 : 1; |
| 52 | } |
| 53 | ); |
| 54 | |
| 55 | if ( $sorted === true ) { |
| 56 | $donors = array_slice( $donors, 0, 25 ); |
| 57 | $donors = array_values( $donors ); |
| 58 | } |
| 59 | |
| 60 | return $donors; |
| 61 | |
| 62 | } |
| 63 | |
| 64 | } |
| 65 |