|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Earnings / Sales Stats |
|
4
|
|
|
* |
|
5
|
|
|
* @package Give |
|
6
|
|
|
* @subpackage Classes/Stats |
|
7
|
|
|
* @copyright Copyright (c) 2018, GiveWP |
|
8
|
|
|
* @license https://opensource.org/licenses/gpl-license GNU Public License |
|
9
|
|
|
* @since 2.4.1 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// Exit if accessed directly. |
|
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
14
|
|
|
exit; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Give_Donation_Stats Class |
|
19
|
|
|
* |
|
20
|
|
|
* This class is for retrieving stats for earnings and sales. |
|
21
|
|
|
* |
|
22
|
|
|
* Stats can be retrieved for date ranges and pre-defined periods. |
|
23
|
|
|
* |
|
24
|
|
|
* @since 2.4.1 |
|
25
|
|
|
*/ |
|
26
|
|
|
class Give_Donation_Stats extends Give_Stats { |
|
27
|
|
|
/** |
|
28
|
|
|
* Give_Donation_Stats constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param array $query |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct( array $query = array() ) { |
|
33
|
|
|
// Add additional default query params |
|
34
|
|
|
$this->query_var_defaults = array_merge( array( |
|
35
|
|
|
'status' => array( 'publish' ), |
|
36
|
|
|
'give_forms' => array(), |
|
37
|
|
|
'gateways' => array(), |
|
38
|
|
|
'donor_id' => 0, |
|
39
|
|
|
), $this->query_var_defaults ); |
|
40
|
|
|
|
|
41
|
|
|
parent::__construct( $query ); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Retrieve sale stats |
|
46
|
|
|
* |
|
47
|
|
|
* @since 2.4.1 |
|
48
|
|
|
* @access public |
|
49
|
|
|
* |
|
50
|
|
|
* @param array $query |
|
51
|
|
|
* |
|
52
|
|
|
* @return stdClass |
|
53
|
|
|
*/ |
|
54
|
|
|
public function get_sales( $query = array() ) { |
|
55
|
|
|
// Add table and column name to query_vars to assist with date query generation. |
|
56
|
|
|
$this->query_vars['table'] = $this->get_db()->posts; |
|
57
|
|
|
$this->query_vars['column'] = $this->query_vars['inner_join_at'] = 'ID'; |
|
58
|
|
|
|
|
59
|
|
|
// Run pre-query checks and maybe generate SQL. |
|
60
|
|
|
$this->pre_query( $query ); |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Return custom result |
|
64
|
|
|
* |
|
65
|
|
|
* @since 2.4.1 |
|
66
|
|
|
*/ |
|
67
|
|
|
$result = apply_filters( 'give_donation_stats_pre_get_sales', null, $this ); |
|
68
|
|
|
if ( ! is_null( $result ) ) { |
|
69
|
|
|
return $result; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$allowed_functions = array( 'COUNT', 'AVG' ); |
|
73
|
|
|
|
|
74
|
|
|
$is_relative = true === $this->query_vars['relative']; |
|
75
|
|
|
|
|
76
|
|
|
$function = isset( $this->query_vars['function'] ) && in_array( $this->query_vars['function'], $allowed_functions, true ) |
|
77
|
|
|
? "{$this->query_vars['function']}({$this->query_vars['table']}.{$this->query_vars['column']})" |
|
78
|
|
|
: "COUNT({$this->query_vars['table']}.{$this->query_vars['column']})"; |
|
79
|
|
|
|
|
80
|
|
|
if ( $is_relative ) { |
|
81
|
|
|
$sql = "SELECT IFNULL(COUNT({$this->query_vars['table']}.{$this->query_vars['column']}), 0) AS sales, IFNULL(relative, 0) AS relative |
|
82
|
|
|
FROM {$this->query_vars['table']} |
|
83
|
|
|
CROSS JOIN ( |
|
84
|
|
|
SELECT IFNULL(COUNT({$this->query_vars['table']}.{$this->query_vars['column']}), 0) AS relative |
|
85
|
|
|
FROM {$this->query_vars['table']} |
|
86
|
|
|
{$this->query_vars['inner_join_sql']} |
|
87
|
|
|
WHERE 1=1 |
|
88
|
|
|
{$this->query_vars['where_sql']} |
|
89
|
|
|
{$this->query_vars['relative_date_sql']} |
|
90
|
|
|
) o |
|
91
|
|
|
WHERE 1=1 |
|
92
|
|
|
{$this->query_vars['where_sql']} |
|
93
|
|
|
{$this->query_vars['date_sql']} |
|
94
|
|
|
"; |
|
95
|
|
|
} else { |
|
96
|
|
|
$sql = "SELECT IFNULL({$function}, 0) AS sales |
|
97
|
|
|
FROM {$this->query_vars['table']} |
|
98
|
|
|
{$this->query_vars['inner_join_sql']} |
|
99
|
|
|
WHERE 1=1 |
|
100
|
|
|
{$this->query_vars['where_sql']} |
|
101
|
|
|
{$this->query_vars['date_sql']} |
|
102
|
|
|
"; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$result = $this->get_db()->get_row( $sql ); |
|
106
|
|
|
|
|
107
|
|
|
if ( is_null( $result ) ) { |
|
108
|
|
|
$result = new stdClass(); |
|
109
|
|
|
$result->sales = $result->relative = 0; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if ( $is_relative ) { |
|
113
|
|
|
$result->growth = $this->get_growth( $result->sales, $result->relative ); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// Reset query vars. |
|
117
|
|
|
$result->sql = $sql; |
|
118
|
|
|
$result->query_vars = $this->query_vars; |
|
119
|
|
|
$this->reset_query(); |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Filter the result |
|
123
|
|
|
* |
|
124
|
|
|
* @since 2.4.1 |
|
125
|
|
|
*/ |
|
126
|
|
|
$result = apply_filters( 'give_donation_stats_get_sales', $result, $this ); |
|
127
|
|
|
|
|
128
|
|
|
return $result; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Retrieve earning stats |
|
134
|
|
|
* |
|
135
|
|
|
* @since 2.4.1 |
|
136
|
|
|
* @access public |
|
137
|
|
|
* |
|
138
|
|
|
* @param array $query |
|
139
|
|
|
* |
|
140
|
|
|
* @return stdClass |
|
141
|
|
|
*/ |
|
142
|
|
|
public function get_earnings( $query = array() ) { |
|
143
|
|
|
// Add table and column name to query_vars to assist with date query generation. |
|
144
|
|
|
$this->query_vars['table'] = $this->get_db()->donationmeta; |
|
145
|
|
|
$this->query_vars['column'] = 'meta_value'; |
|
146
|
|
|
|
|
147
|
|
|
// Run pre-query checks and maybe generate SQL. |
|
148
|
|
|
$this->pre_query( $query ); |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Return custom result |
|
152
|
|
|
* |
|
153
|
|
|
* @since 2.4.1 |
|
154
|
|
|
*/ |
|
155
|
|
|
$result = apply_filters( 'give_donation_stats_pre_get_earnings', null, $this ); |
|
156
|
|
|
if ( ! is_null( $result ) ) { |
|
157
|
|
|
return $result; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$allowed_functions = array( 'SUM', 'AVG' ); |
|
161
|
|
|
|
|
162
|
|
|
$is_relative = true === $this->query_vars['relative']; |
|
163
|
|
|
|
|
164
|
|
|
$function = isset( $this->query_vars['function'] ) && in_array( $this->query_vars['function'], $allowed_functions, true ) |
|
165
|
|
|
? "{$this->query_vars['function']}({$this->query_vars['table']}.{$this->query_vars['column']})" |
|
166
|
|
|
: "SUM({$this->query_vars['table']}.{$this->query_vars['column']})"; |
|
167
|
|
|
|
|
168
|
|
|
if ( $is_relative ) { |
|
169
|
|
|
$sql = "SELECT IFNULL({$function}, 0) AS total, IFNULL(relative, 0) AS relative |
|
170
|
|
|
FROM {$this->query_vars['table']} |
|
171
|
|
|
CROSS JOIN ( |
|
172
|
|
|
SELECT IFNULL($function, 0) AS relative |
|
173
|
|
|
FROM {$this->query_vars['table']} |
|
174
|
|
|
INNER JOIN {$this->get_db()->posts} on {$this->get_db()->posts}.ID = {$this->query_vars['table']}.{$this->query_vars['inner_join_at']} |
|
175
|
|
|
{$this->query_vars['inner_join_sql']} |
|
176
|
|
|
WHERE 1=1 |
|
177
|
|
|
{$this->query_vars['where_sql']} |
|
178
|
|
|
{$this->query_vars['relative_date_sql']} |
|
179
|
|
|
AND {$this->query_vars['table']}.meta_key='_give_payment_total' |
|
180
|
|
|
) o |
|
181
|
|
|
INNER JOIN {$this->get_db()->posts} on {$this->get_db()->posts}.ID = {$this->query_vars['table']}.{$this->query_vars['inner_join_at']} |
|
182
|
|
|
{$this->query_vars['inner_join_sql']} |
|
183
|
|
|
WHERE 1=1 |
|
184
|
|
|
{$this->query_vars['where_sql']} |
|
185
|
|
|
{$this->query_vars['date_sql']} |
|
186
|
|
|
AND {$this->query_vars['table']}.meta_key='_give_payment_total' |
|
187
|
|
|
"; |
|
188
|
|
|
} else { |
|
189
|
|
|
$sql = "SELECT IFNULL({$function}, 0) AS total |
|
190
|
|
|
FROM {$this->query_vars['table']} |
|
191
|
|
|
INNER JOIN {$this->get_db()->posts} on {$this->get_db()->posts}.ID = {$this->query_vars['table']}.{$this->query_vars['inner_join_at']} |
|
192
|
|
|
{$this->query_vars['inner_join_sql']} |
|
193
|
|
|
WHERE 1=1 |
|
194
|
|
|
{$this->query_vars['where_sql']} |
|
195
|
|
|
{$this->query_vars['date_sql']} |
|
196
|
|
|
AND {$this->query_vars['table']}.meta_key='_give_payment_total' |
|
197
|
|
|
"; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$result = $this->get_db()->get_row( $sql ); |
|
201
|
|
|
|
|
202
|
|
|
if ( is_null( $result ) ) { |
|
203
|
|
|
$result = new stdClass(); |
|
204
|
|
|
$result->total = $result->relative = $result->growth = 0; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
if ( $is_relative ) { |
|
208
|
|
|
$result->growth = $this->get_growth( $result->total, $result->relative ); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
// Reset query vars. |
|
212
|
|
|
$result->sql = $sql; |
|
213
|
|
|
$result->query_vars = $this->query_vars; |
|
214
|
|
|
$this->reset_query(); |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Filter the result |
|
218
|
|
|
* |
|
219
|
|
|
* @since 2.4.1 |
|
220
|
|
|
*/ |
|
221
|
|
|
$result = apply_filters( 'give_donation_stats_get_earnings', $result, $this ); |
|
222
|
|
|
|
|
223
|
|
|
return $result; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Get donation earning and sales information |
|
228
|
|
|
* |
|
229
|
|
|
* @since 2.4.1 |
|
230
|
|
|
* @access public |
|
231
|
|
|
* |
|
232
|
|
|
* @param array $query |
|
233
|
|
|
* |
|
234
|
|
|
* @return stdClass |
|
235
|
|
|
*/ |
|
236
|
|
|
public function get_donation_statistics( $query = array() ) { |
|
237
|
|
|
$day_by_day = true; |
|
238
|
|
|
|
|
239
|
|
|
$this->query_vars['table'] = $this->get_db()->posts; |
|
240
|
|
|
$this->query_vars['column'] = 'post_date_gmt'; |
|
241
|
|
|
|
|
242
|
|
|
$this->pre_query( $query ); |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Return custom result |
|
246
|
|
|
* |
|
247
|
|
|
* @since 2.4.1 |
|
248
|
|
|
*/ |
|
249
|
|
|
$result = apply_filters( 'give_donation_stats_pre_get_donation_statistics', null, $this ); |
|
250
|
|
|
if ( ! is_null( $result ) ) { |
|
251
|
|
|
return $result; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
$column = "{$this->query_vars['table']}.{$this->query_vars['column']}"; |
|
255
|
|
|
|
|
256
|
|
|
$sql_clauses = array( |
|
257
|
|
|
'select' => "YEAR({$column}) AS year, MONTH({$column}) AS month, DAY({$column}) AS day", |
|
258
|
|
|
'groupby' => "YEAR({$column}), MONTH({$column}), DAY({$column})", |
|
259
|
|
|
'orderby' => "YEAR({$column}), MONTH({$column}), DAY({$column})", |
|
260
|
|
|
); |
|
261
|
|
|
|
|
262
|
|
|
$sql = "SELECT COUNT(ID) AS sales, SUM(m1.meta_value) AS earnings, {$sql_clauses['select']} |
|
263
|
|
|
FROM {$this->query_vars['table']} |
|
264
|
|
|
INNER JOIN {$this->get_db()->donationmeta} as m1 on m1.donation_id={$this->query_vars['table']}.{$this->query_vars['inner_join_at']} |
|
265
|
|
|
{$this->query_vars['where_sql']} |
|
266
|
|
|
AND m1.meta_key='_give_payment_total' |
|
267
|
|
|
{$this->query_vars['date_sql']} |
|
268
|
|
|
GROUP BY {$sql_clauses['groupby']} |
|
269
|
|
|
ORDER BY {$sql_clauses['orderby']} ASC"; |
|
270
|
|
|
|
|
271
|
|
|
$results = $this->get_db()->get_results( $sql ); |
|
272
|
|
|
|
|
273
|
|
|
$sales = array(); |
|
274
|
|
|
$earnings = array(); |
|
275
|
|
|
$dates = array( |
|
276
|
|
|
'start' => $this->query_vars['start_date'], |
|
277
|
|
|
'end' => $this->query_vars['end_date'], |
|
278
|
|
|
); |
|
279
|
|
|
|
|
280
|
|
|
// Initialise all arrays with timestamps and set values to 0. |
|
281
|
|
|
while ( strtotime( $dates['start']->copy()->format( 'mysql' ) ) <= strtotime( $dates['end']->copy()->format( 'mysql' ) ) ) { |
|
282
|
|
|
$day = ( true === $day_by_day ) |
|
283
|
|
|
? $dates['start']->day |
|
284
|
|
|
: 1; |
|
285
|
|
|
|
|
286
|
|
|
$timestamp = Give_Date::create( $dates['start']->year, $dates['start']->month, $day, 0, 0, 0, $this->date->getWpTimezone() )->timestamp; |
|
287
|
|
|
|
|
288
|
|
|
$sales[ $timestamp ]['x'] = $earnings[ $timestamp ]['x'] = $timestamp * 1000; |
|
289
|
|
|
$sales[ $timestamp ]['y'] = 0; |
|
290
|
|
|
$earnings[ $timestamp ]['y'] = 0.00; |
|
291
|
|
|
|
|
292
|
|
|
$dates['start'] = ( true === $day_by_day ) |
|
293
|
|
|
? $dates['start']->addDays( 1 ) |
|
294
|
|
|
: $dates['start']->addMonth( 1 ); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
foreach ( $results as $result ) { |
|
298
|
|
|
$day = ( true === $day_by_day ) |
|
299
|
|
|
? $result->day |
|
300
|
|
|
: 1; |
|
301
|
|
|
|
|
302
|
|
|
$timestamp = Give_Date::create( $result->year, $result->month, $day, 0, 0, 0, $this->date->getWpTimezone() )->timestamp; |
|
303
|
|
|
|
|
304
|
|
|
$sales[ $timestamp ]['y'] = $result->sales; |
|
305
|
|
|
$earnings[ $timestamp ]['y'] = floatval( $result->earnings ); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
$sales = array_values( $sales ); |
|
309
|
|
|
$earnings = array_values( $earnings ); |
|
310
|
|
|
|
|
311
|
|
|
$results = new stdClass(); |
|
312
|
|
|
|
|
313
|
|
|
$results->sales = $sales; |
|
314
|
|
|
$results->earnings = $earnings; |
|
315
|
|
|
|
|
316
|
|
|
// Reset query vars. |
|
317
|
|
|
$results->sql = $sql; |
|
318
|
|
|
$results->query_vars = $this->query_vars; |
|
319
|
|
|
$this->reset_query(); |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* Filter the result |
|
323
|
|
|
* |
|
324
|
|
|
* @since 2.4.1 |
|
325
|
|
|
*/ |
|
326
|
|
|
$results = apply_filters( 'give_donation_stats_get_statistics', $results, $this ); |
|
327
|
|
|
|
|
328
|
|
|
return $results; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* Get the best selling forms |
|
333
|
|
|
* |
|
334
|
|
|
* @since 2.4.1 |
|
335
|
|
|
* @access public |
|
336
|
|
|
* |
|
337
|
|
|
* @param array $query Array of query arguments |
|
338
|
|
|
* |
|
339
|
|
|
* @return stdClass |
|
340
|
|
|
*/ |
|
341
|
|
|
public function get_busiest_day( $query = array() ) { |
|
342
|
|
|
// Add table and column name to query_vars to assist with date query generation. |
|
343
|
|
|
$this->query_vars['table'] = $this->get_db()->posts; |
|
344
|
|
|
$this->query_vars['column'] = 'post_date'; |
|
345
|
|
|
|
|
346
|
|
|
$this->pre_query( $query ); |
|
347
|
|
|
|
|
348
|
|
|
/** |
|
349
|
|
|
* Return custom result |
|
350
|
|
|
* |
|
351
|
|
|
* @since 2.4.1 |
|
352
|
|
|
*/ |
|
353
|
|
|
$result = apply_filters( 'give_donation_stats_pre_get_busiest_day', null, $this ); |
|
354
|
|
|
if ( ! is_null( $result ) ) { |
|
355
|
|
|
return $result; |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
$sql = "SELECT DAYOFWEEK({$this->query_vars['column']}) AS day, COUNT(ID) as total |
|
359
|
|
|
FROM {$this->query_vars['table']} |
|
360
|
|
|
{$this->query_vars['inner_join_sql']} |
|
361
|
|
|
WHERE 1=1 |
|
362
|
|
|
{$this->query_vars['where_sql'] } |
|
363
|
|
|
{$this->query_vars['date_sql'] } |
|
364
|
|
|
GROUP BY day |
|
365
|
|
|
ORDER BY day DESC |
|
366
|
|
|
LIMIT 1"; |
|
367
|
|
|
|
|
368
|
|
|
$result = $this->get_db()->get_row( $sql ); |
|
369
|
|
|
|
|
370
|
|
|
$result->day = is_null( $result ) |
|
371
|
|
|
? '' |
|
372
|
|
|
: Give_Date::getDays()[ $result->day - 1 ]; |
|
373
|
|
|
|
|
374
|
|
|
// Reset query vars. |
|
375
|
|
|
$result->sql = $sql; |
|
376
|
|
|
$result->query_vars = $this->query_vars; |
|
377
|
|
|
$this->reset_query(); |
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* Filter the result |
|
381
|
|
|
* |
|
382
|
|
|
* @since 2.4.1 |
|
383
|
|
|
*/ |
|
384
|
|
|
$result = apply_filters( 'give_donation_stats_get_busiest_day', $result, $this ); |
|
385
|
|
|
|
|
386
|
|
|
return $result; |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
|
|
/** |
|
390
|
|
|
* Get the best selling forms |
|
391
|
|
|
* @todo : make this function dynamic with new api |
|
392
|
|
|
* |
|
393
|
|
|
* @since 2.4.1 |
|
394
|
|
|
* @access public |
|
395
|
|
|
* @global wpdb $wpdb |
|
396
|
|
|
* |
|
397
|
|
|
* @param array $query |
|
398
|
|
|
*/ |
|
399
|
|
|
public function get_best_selling( $query = array() ) { |
|
|
|
|
|
|
400
|
|
|
} |
|
401
|
|
|
|
|
402
|
|
|
/** |
|
403
|
|
|
* Get most valuable cause |
|
404
|
|
|
* |
|
405
|
|
|
* @since 2.4.1 |
|
406
|
|
|
* @access public |
|
407
|
|
|
* |
|
408
|
|
|
* @param array $query |
|
409
|
|
|
* |
|
410
|
|
|
* @return stdClass |
|
411
|
|
|
*/ |
|
412
|
|
|
public function get_most_valuable_cause( $query = array() ) { |
|
413
|
|
|
$donation_col_name = Give()->payment_meta->get_meta_type() . '_id'; |
|
414
|
|
|
|
|
415
|
|
|
// Add table and column name to query_vars to assist with date query generation. |
|
416
|
|
|
$this->query_vars['table'] = $this->get_db()->donationmeta; |
|
417
|
|
|
$this->query_vars['column'] = 'meta_value'; |
|
418
|
|
|
|
|
419
|
|
|
$this->pre_query( $query ); |
|
420
|
|
|
|
|
421
|
|
|
/** |
|
422
|
|
|
* Return custom result |
|
423
|
|
|
* |
|
424
|
|
|
* @since 2.4.1 |
|
425
|
|
|
*/ |
|
426
|
|
|
$result = apply_filters( 'give_donation_stats_pre_get_most_valuable_cause', null, $this ); |
|
427
|
|
|
if ( ! is_null( $result ) ) { |
|
428
|
|
|
return $result; |
|
429
|
|
|
} |
|
430
|
|
|
|
|
431
|
|
|
$sql = "SELECT {$this->query_vars['table']}.{$this->query_vars['column']} as form, COUNT({$this->query_vars['table']}.{$donation_col_name}) as total_donation |
|
432
|
|
|
FROM {$this->query_vars['table']} |
|
433
|
|
|
INNER JOIN {$this->get_db()->posts} ON {$this->query_vars['table']}.{$donation_col_name}={$this->get_db()->posts}.ID |
|
434
|
|
|
{$this->query_vars['inner_join_sql']} |
|
435
|
|
|
WHERE 1=1 |
|
436
|
|
|
{$this->query_vars['where_sql']} |
|
437
|
|
|
{$this->query_vars['date_sql']} |
|
438
|
|
|
AND {$this->query_vars['table']}.meta_key='_give_payment_form_id' |
|
439
|
|
|
GROUP BY form |
|
440
|
|
|
ORDER BY total_donation DESC |
|
441
|
|
|
LIMIT 1 |
|
442
|
|
|
"; |
|
443
|
|
|
|
|
444
|
|
|
$result = $this->get_db()->get_row( $sql ); |
|
445
|
|
|
|
|
446
|
|
|
$result->form = is_null( $result ) ? 0 : absint( $result->form ); |
|
447
|
|
|
|
|
448
|
|
|
// Reset query vars. |
|
449
|
|
|
$result->sql = $sql; |
|
450
|
|
|
$result->query_vars = $this->query_vars; |
|
451
|
|
|
$this->reset_query(); |
|
452
|
|
|
|
|
453
|
|
|
/** |
|
454
|
|
|
* Filter the result |
|
455
|
|
|
* |
|
456
|
|
|
* @since 2.4.1 |
|
457
|
|
|
*/ |
|
458
|
|
|
$result = apply_filters( 'give_donation_stats_get_most_valuable_sause', $result, $this ); |
|
459
|
|
|
|
|
460
|
|
|
return $result; |
|
461
|
|
|
} |
|
462
|
|
|
|
|
463
|
|
|
/** |
|
464
|
|
|
* Calculate number of refunded donations. |
|
465
|
|
|
* |
|
466
|
|
|
* @since 2.4.1 |
|
467
|
|
|
* @acess public |
|
468
|
|
|
* |
|
469
|
|
|
* @param array $query |
|
470
|
|
|
* |
|
471
|
|
|
* @return stdClass |
|
472
|
|
|
*/ |
|
473
|
|
View Code Duplication |
public function get_refund_count( $query = array() ) { |
|
|
|
|
|
|
474
|
|
|
$query['status'] = isset( $query['status'] ) |
|
475
|
|
|
? $query['status'] |
|
476
|
|
|
: array( 'refunded' ); |
|
477
|
|
|
|
|
478
|
|
|
$result = $this->get_sales( $query ); |
|
479
|
|
|
|
|
480
|
|
|
return $result; |
|
481
|
|
|
} |
|
482
|
|
|
|
|
483
|
|
|
/** |
|
484
|
|
|
* Calculate amount of refunded donations. |
|
485
|
|
|
* |
|
486
|
|
|
* @since 2.4.1 |
|
487
|
|
|
* @acess public |
|
488
|
|
|
* |
|
489
|
|
|
* @param array $query |
|
490
|
|
|
* |
|
491
|
|
|
* @return stdClass |
|
492
|
|
|
*/ |
|
493
|
|
View Code Duplication |
public function get_refund( $query = array() ) { |
|
|
|
|
|
|
494
|
|
|
$query['status'] = isset( $query['status'] ) |
|
495
|
|
|
? $query['status'] |
|
496
|
|
|
: array( 'refunded' ); |
|
497
|
|
|
|
|
498
|
|
|
$result = $this->get_earnings( $query ); |
|
499
|
|
|
|
|
500
|
|
|
return $result; |
|
501
|
|
|
} |
|
502
|
|
|
|
|
503
|
|
|
/** |
|
504
|
|
|
* Set meta query |
|
505
|
|
|
* |
|
506
|
|
|
* @since 2.4.1 |
|
507
|
|
|
* @access public |
|
508
|
|
|
* |
|
509
|
|
|
* @param string $query_key |
|
510
|
|
|
* @param string $meta_key |
|
511
|
|
|
* |
|
512
|
|
|
*/ |
|
513
|
|
|
private function set_meta_sql( $query_key, $meta_key ) { |
|
514
|
|
|
// Bailout. |
|
515
|
|
|
if ( empty( $this->query_vars[ $query_key ] ) ) { |
|
516
|
|
|
return; |
|
517
|
|
|
} |
|
518
|
|
|
|
|
519
|
|
|
$donation_col_name = Give()->payment_meta->get_meta_type() . '_id'; |
|
520
|
|
|
$this->query_vars[ $query_key ] = (array) $this->query_vars[ $query_key ]; |
|
521
|
|
|
|
|
522
|
|
|
$alias = "m{$this->get_counter( $this->get_db()->donationmeta )}"; |
|
523
|
|
|
$data = implode( '\',\'', $this->query_vars[ $query_key ] ); |
|
524
|
|
|
|
|
525
|
|
|
$this->query_vars['inner_join_sql'][] = "INNER JOIN {$this->get_db()->donationmeta} as {$alias} on {$alias}.{$donation_col_name}={$this->query_vars['table']}.{$this->query_vars['inner_join_at']}"; |
|
526
|
|
|
|
|
527
|
|
|
$this->query_vars['where_sql'][] = " AND {$alias}.meta_key='{$meta_key}'"; |
|
528
|
|
|
$this->query_vars['where_sql'][] = " AND {$alias}.meta_value IN ('{$data}')"; |
|
529
|
|
|
|
|
|
|
|
|
|
530
|
|
|
|
|
531
|
|
|
// Set counter. |
|
532
|
|
|
$this->set_counter( $this->get_db()->donationmeta ); |
|
533
|
|
|
} |
|
534
|
|
|
|
|
535
|
|
|
/** |
|
536
|
|
|
* Pre process query |
|
537
|
|
|
* |
|
538
|
|
|
* @since 2.4.1 |
|
539
|
|
|
* @access protected |
|
540
|
|
|
* |
|
541
|
|
|
* @param array $query |
|
542
|
|
|
*/ |
|
543
|
|
|
protected function pre_query( $query = array() ) { |
|
544
|
|
|
parent::pre_query( $query ); |
|
545
|
|
|
|
|
546
|
|
|
$this->query_vars['function'] = strtoupper( $this->query_vars['function'] ); |
|
547
|
|
|
|
|
548
|
|
|
$sql_types = array( 'relative_date_sql', 'date_sql', 'inner_join_sql', 'where_sql' ); |
|
549
|
|
|
|
|
550
|
|
|
// Set empty sql collection string to array |
|
551
|
|
|
foreach ( $sql_types as $sql_type ) { |
|
552
|
|
|
$this->query_vars[ $sql_type ] = array_filter( (array) $this->query_vars['where_sql'] ); |
|
553
|
|
|
} |
|
554
|
|
|
|
|
555
|
|
|
// Where sql. |
|
556
|
|
|
if ( ! empty( $this->query_vars['status'] ) ) { |
|
557
|
|
|
if ( 'any' !== $this->query_vars['status'] ) { |
|
558
|
|
|
$this->query_vars['status'] = array_map( 'sanitize_text_field', $this->query_vars['status'] ); |
|
559
|
|
|
|
|
560
|
|
|
$placeholders = implode( ', ', array_fill( 0, count( $this->query_vars['status'] ), '%s' ) ); |
|
561
|
|
|
|
|
562
|
|
|
$this->query_vars['where_sql'][] = $this->get_db()->prepare( "AND {$this->get_db()->posts}.post_status IN ({$placeholders})", $this->query_vars['status'] ); |
|
563
|
|
|
} |
|
564
|
|
|
} |
|
565
|
|
|
$this->query_vars['where_sql'][] = $this->get_db()->prepare( "AND {$this->get_db()->posts}.post_type=%s", 'give_payment' ); |
|
566
|
|
|
|
|
567
|
|
|
// Date sql. |
|
568
|
|
View Code Duplication |
if ( $this->query_vars["start_date"] ) { |
|
|
|
|
|
|
569
|
|
|
$this->query_vars['date_sql'][] = "AND {$this->get_db()->posts}.post_date>='{$this->query_vars["start_date"]->format('mysql')}'"; |
|
570
|
|
|
} |
|
571
|
|
|
|
|
572
|
|
View Code Duplication |
if ( $this->query_vars["end_date"] ) { |
|
|
|
|
|
|
573
|
|
|
$this->query_vars['date_sql'][] = "AND {$this->get_db()->posts}.post_date<='{$this->query_vars["end_date"]->format('mysql')}'"; |
|
574
|
|
|
} |
|
575
|
|
|
|
|
576
|
|
|
// Relative date query. |
|
577
|
|
|
if ( $this->query_vars['range'] ) { |
|
578
|
|
View Code Duplication |
if ( $this->query_vars["relative_start_date"] ) { |
|
|
|
|
|
|
579
|
|
|
$this->query_vars['relative_date_sql'][] = "AND {$this->get_db()->posts}.post_date>='{$this->query_vars["relative_start_date"]->format('mysql')}'"; |
|
580
|
|
|
} |
|
581
|
|
|
|
|
582
|
|
View Code Duplication |
if ( $this->query_vars["relative_end_date"] ) { |
|
|
|
|
|
|
583
|
|
|
$this->query_vars['relative_date_sql'][] = "AND {$this->get_db()->posts}.post_date<='{$this->query_vars["relative_end_date"]->format('mysql')}'"; |
|
584
|
|
|
} |
|
585
|
|
|
} |
|
586
|
|
|
|
|
587
|
|
|
// Add sql for specific donation form. |
|
588
|
|
|
$this->set_meta_sql( 'give_forms', '_give_payment_form_id' ); |
|
589
|
|
|
|
|
590
|
|
|
// Add sql for specific donation payment gateways. |
|
591
|
|
|
$this->set_meta_sql( 'gateways', '_give_payment_gateway' ); |
|
592
|
|
|
|
|
593
|
|
|
// Add sql for specific donation donor id. |
|
594
|
|
|
$this->set_meta_sql( 'donor_id', '_give_payment_donor_id' ); |
|
595
|
|
|
|
|
596
|
|
|
// Create sql query string |
|
597
|
|
|
foreach ( $sql_types as $sql_type ) { |
|
598
|
|
|
$this->query_vars[ $sql_type ] = is_array( $this->query_vars[ $sql_type ] ) |
|
599
|
|
|
? implode( ' ', $this->query_vars[ $sql_type ] ) |
|
600
|
|
|
: $this->query_vars[ $sql_type ]; |
|
601
|
|
|
} |
|
602
|
|
|
} |
|
603
|
|
|
|
|
604
|
|
|
} |
|
605
|
|
|
|
|
606
|
|
|
// @todo: compatibility with recurring, fee recovery and currency switcher |
|
607
|
|
|
// @todo: currency formatting compatibility for earnings and other |
|
608
|
|
|
// @todo review donation earning growth logic |
|
609
|
|
|
// @todo: develop logic to sent raw and formatted value |
|
610
|
|
|
// @todo: review number decimal format |
|
611
|
|
|
// @todo: document stat query params |
|
612
|
|
|
// @todo: think about table backward compatibility for paymentmeta |
|
613
|
|
|
// @todo: think about custom dates |
|
614
|
|
|
// @todo: add snippet to show how to use donation api filters |
|
615
|
|
|
// @todo: update REST api integration |
|
616
|
|
|
|
|
617
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.