PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 2.13.1
GiveWP – Donation Plugin and Fundraising Platform v2.13.1
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 / API / Endpoints / Reports / Income.php
Income.php
149 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Income over time endpoint
5 *
6 * @package Give
7 */
8
9 namespace Give\API\Endpoints\Reports;
10
11 class Income extends Endpoint {
12
13 protected $payments;
14
15 public function __construct() {
16 $this->endpoint = 'income';
17 }
18
19 public function getReport( $request ) {
20 $start = date_create( $request->get_param( 'start' ) );
21 $end = date_create( $request->get_param( 'end' ) );
22 $diff = date_diff( $start, $end );
23
24 $dataset = [];
25
26 switch ( true ) {
27 case ( $diff->days > 12 ):
28 $interval = round( $diff->days / 12 );
29 $data = $this->get_data( $start, $end, 'P' . $interval . 'D' );
30 break;
31 case ( $diff->days > 5 ):
32 $data = $this->get_data( $start, $end, 'P1D' );
33 break;
34 case ( $diff->days > 4 ):
35 $data = $this->get_data( $start, $end, 'PT12H' );
36 break;
37 case ( $diff->days > 2 ):
38 $data = $this->get_data( $start, $end, 'PT3H' );
39 break;
40 case ( $diff->days >= 0 ):
41 $data = $this->get_data( $start, $end, 'PT1H' );
42 break;
43 }
44
45 return $data;
46 }
47
48 public function get_data( $start, $end, $intervalStr ) {
49
50 $tooltips = [];
51 $income = [];
52
53 $interval = new \DateInterval( $intervalStr );
54
55 $periodStart = clone $start;
56 $periodEnd = clone $start;
57
58 // Subtract interval to set up period start
59 date_sub( $periodStart, $interval );
60
61 while ( $periodStart < $end ) {
62
63 $values = $this->get_values( $periodStart->format( 'Y-m-d H:i:s' ), $periodEnd->format( 'Y-m-d H:i:s' ) );
64 $incomeForPeriod = $values['earnings'];
65 $donorsForPeriod = $values['donor_count'];
66 $time = $periodEnd->format( 'Y-m-d H:i:s' );
67
68 switch ( $intervalStr ) {
69 case 'P1D':
70 $time = $periodStart->format( 'Y-m-d' );
71 $periodLabel = $periodStart->format( 'l' );
72 break;
73 case 'PT12H':
74 case 'PT3H':
75 case 'PT1H':
76 $periodLabel = $periodStart->format( 'D ga' ) . ' - ' . $periodEnd->format( 'D ga' );
77 break;
78 default:
79 $periodLabel = $periodStart->format( 'M j, Y' ) . ' - ' . $periodEnd->format( 'M j, Y' );
80 }
81
82 $income[] = [
83 'x' => $time,
84 'y' => $incomeForPeriod,
85 ];
86
87 $tooltips[] = [
88 'title' => give_currency_filter(
89 give_format_amount( $incomeForPeriod ),
90 [
91 'currency_code' => $this->currency,
92 'decode_currency' => true,
93 'sanitize' => false,
94 ]
95 ),
96 'body' => $donorsForPeriod . ' ' . __( 'Donors', 'give' ),
97 'footer' => $periodLabel,
98 ];
99
100 // Add interval to set up next period
101 date_add( $periodStart, $interval );
102 date_add( $periodEnd, $interval );
103 }
104
105 if ( $intervalStr === 'P1D' ) {
106 $income = array_slice( $income, 1 );
107 $tooltips = array_slice( $tooltips, 1 );
108 }
109
110 // Create data objec to be returned, with 'highlights' object containing total and average figures to display
111 $data = [
112 'datasets' => [
113 [
114 'data' => $income,
115 'tooltips' => $tooltips,
116 ],
117 ],
118 ];
119
120 return $data;
121
122 }
123
124 public function get_values( $startStr, $endStr ) {
125
126 $paymentObjects = $this->getPayments( $startStr, $endStr );
127
128 $earnings = 0;
129 $donors = [];
130
131 foreach ( $paymentObjects as $paymentObject ) {
132 if ( $paymentObject->date >= $startStr && $paymentObject->date < $endStr ) {
133 if ( $paymentObject->status == 'publish' || $paymentObject->status == 'give_subscription' ) {
134 $earnings += $paymentObject->total;
135 $donors[] = $paymentObject->donor_id;
136 }
137 }
138 }
139
140 $unique = array_unique( $donors );
141
142 return [
143 'earnings' => $earnings,
144 'donor_count' => count( $unique ),
145 ];
146 }
147
148 }
149