1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Donors Query |
4
|
|
|
* |
5
|
|
|
* @package Give |
6
|
|
|
* @subpackage Classes/Stats |
7
|
|
|
* @copyright Copyright (c) 2017, GiveWP |
8
|
|
|
* @license https://opensource.org/licenses/gpl-license GNU Public License |
9
|
|
|
* @since 1.8.14 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// Exit if accessed directly. |
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
14
|
|
|
exit; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Give_Donors_Query Class |
19
|
|
|
* |
20
|
|
|
* This class is for retrieving donors data. |
21
|
|
|
* |
22
|
|
|
* Donors can be retrieved for date ranges and pre-defined periods. |
23
|
|
|
* |
24
|
|
|
* @since 1.8.14 |
25
|
|
|
*/ |
26
|
|
|
class Give_Donors_Query { |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The args to pass to the give_get_donors() query |
30
|
|
|
* |
31
|
|
|
* @since 1.8.14 |
32
|
|
|
* @access public |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
public $args = array(); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The donors found based on the criteria set |
40
|
|
|
* |
41
|
|
|
* @since 1.8.14 |
42
|
|
|
* @access public |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
public $donors = array(); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The donors found based on the criteria set |
50
|
|
|
* |
51
|
|
|
* @since 1.8.14 |
52
|
|
|
* @access public |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
public $table_name = ''; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The donors found based on the criteria set |
60
|
|
|
* |
61
|
|
|
* @since 1.8.14 |
62
|
|
|
* @access public |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
public $meta_table_name = ''; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The donors found based on the criteria set |
70
|
|
|
* |
71
|
|
|
* @since 1.8.14 |
72
|
|
|
* @access public |
73
|
|
|
* |
74
|
|
|
* @var string |
75
|
|
|
*/ |
76
|
|
|
public $meta_type = ''; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Preserve args |
80
|
|
|
* |
81
|
|
|
* @since 2.4.0 |
82
|
|
|
* @access public |
83
|
|
|
* |
84
|
|
|
* @var array |
85
|
|
|
*/ |
86
|
|
|
public $_args = array(); |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Default query arguments. |
90
|
|
|
* |
91
|
|
|
* Not all of these are valid arguments that can be passed to WP_Query. The ones that are not, are modified before |
92
|
|
|
* the query is run to convert them to the proper syntax. |
93
|
|
|
* |
94
|
|
|
* @since 1.8.14 |
95
|
|
|
* @access public |
96
|
|
|
* |
97
|
|
|
* @param $args array The array of arguments that can be passed in and used for setting up this payment query. |
98
|
|
|
*/ |
99
|
|
|
public function __construct( $args = array() ) { |
100
|
|
|
$defaults = array( |
101
|
|
|
'number' => 20, |
102
|
|
|
'offset' => 0, |
103
|
|
|
'paged' => 1, |
104
|
|
|
'orderby' => 'id', |
105
|
|
|
'order' => 'DESC', |
106
|
|
|
'user' => null, |
107
|
|
|
'email' => null, |
108
|
|
|
'donor' => null, |
109
|
|
|
'meta_query' => array(), |
|
|
|
|
110
|
|
|
'date_query' => array(), |
111
|
|
|
's' => null, |
112
|
|
|
'fields' => 'all', // Supports donors (all fields) or valid column as string or array list. |
113
|
|
|
'count' => false, |
114
|
|
|
'give_forms' => array(), |
115
|
|
|
'start_date' => false, |
116
|
|
|
'end_date' => false, |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* donation_amount will contain value like: |
120
|
|
|
* array( |
121
|
|
|
* 'compare' => *compare symbol* (by default set to > ) |
122
|
|
|
* 'amount' => *numeric_value* |
123
|
|
|
* ) |
124
|
|
|
* |
125
|
|
|
* You can also pass number value to this param then compare symbol will auto set to > |
126
|
|
|
*/ |
127
|
|
|
'donation_amount' => array(), |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
$this->args = $this->_args = wp_parse_args( $args, $defaults ); |
131
|
|
|
$this->table_name = Give()->donors->table_name; |
132
|
|
|
$this->meta_table_name = Give()->donor_meta->table_name; |
133
|
|
|
$this->meta_type = Give()->donor_meta->meta_type; |
134
|
|
|
|
135
|
|
|
$this->date_filter_pre(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Modify the query/query arguments before we retrieve donors. |
140
|
|
|
* |
141
|
|
|
* @since 1.8.14 |
142
|
|
|
* @access public |
143
|
|
|
* |
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
|
|
public function init() { |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Retrieve donors. |
152
|
|
|
* |
153
|
|
|
* The query can be modified in two ways; either the action before the |
154
|
|
|
* query is run, or the filter on the arguments (existing mainly for backwards |
155
|
|
|
* compatibility). |
156
|
|
|
* |
157
|
|
|
* @since 1.8.14 |
158
|
|
|
* @access public |
159
|
|
|
* |
160
|
|
|
* @global wpdb $wpdb |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
public function get_donors() { |
165
|
|
|
global $wpdb; |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Fires before retrieving donors. |
169
|
|
|
* |
170
|
|
|
* @since 1.8.14 |
171
|
|
|
* |
172
|
|
|
* @param Give_Donors_Query $this Donors query object. |
173
|
|
|
*/ |
174
|
|
|
do_action( 'give_pre_get_donors', $this ); |
175
|
|
|
|
176
|
|
|
$cache_key = Give_Cache::get_key( 'give_donor', $this->get_sql(), false ); |
|
|
|
|
177
|
|
|
|
178
|
|
|
// Get donors from cache. |
179
|
|
|
$this->donors = Give_Cache::get_db_query( $cache_key ); |
|
|
|
|
180
|
|
|
|
181
|
|
|
if ( is_null( $this->donors ) ) { |
182
|
|
|
if ( empty( $this->args['count'] ) ) { |
183
|
|
|
$this->donors = $wpdb->get_results( $this->get_sql() ); |
|
|
|
|
184
|
|
|
$this->update_meta_cache( wp_list_pluck( (array) $this->donors, 'id' ) ); |
185
|
|
|
} else { |
186
|
|
|
$this->donors = $wpdb->get_var( $this->get_sql() ); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
Give_Cache::set_db_query( $cache_key, $this->donors ); |
190
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Fires after retrieving donors. |
195
|
|
|
* |
196
|
|
|
* @since 1.8.14 |
197
|
|
|
* |
198
|
|
|
* @param Give_Donors_Query $this Donors query object. |
199
|
|
|
*/ |
200
|
|
|
do_action( 'give_post_get_donors', $this ); |
201
|
|
|
|
202
|
|
|
return $this->donors; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Get sql query from queried array. |
207
|
|
|
* |
208
|
|
|
* @since 2.0 |
209
|
|
|
* @access public |
210
|
|
|
* |
211
|
|
|
* @global wpdb $wpdb |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
public function get_sql() { |
215
|
|
|
global $wpdb; |
216
|
|
|
|
217
|
|
|
if ( $this->args['number'] < 1 ) { |
218
|
|
|
$this->args['number'] = 99999999999; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$where = $this->get_where_query(); |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
224
|
|
|
// Set offset. |
225
|
|
|
if ( empty( $this->args['offset'] ) && ( 0 < $this->args['paged'] ) ) { |
226
|
|
|
$this->args['offset'] = $this->args['number'] * ( $this->args['paged'] - 1 ); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
// Set fields. |
230
|
|
|
$fields = "{$this->table_name}.*"; |
231
|
|
|
if ( ! empty( $this->args['fields'] ) && 'all' !== $this->args['fields'] ) { |
232
|
|
|
if ( is_string( $this->args['fields'] ) ) { |
233
|
|
|
$fields = "{$this->table_name}.{$this->args['fields']}"; |
234
|
|
View Code Duplication |
} elseif ( is_array( $this->args['fields'] ) ) { |
|
|
|
|
235
|
|
|
$fields = "{$this->table_name}." . implode( " , {$this->table_name}.", $this->args['fields'] ); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
// Set count. |
240
|
|
|
if ( ! empty( $this->args['count'] ) ) { |
241
|
|
|
$fields = "COUNT({$this->table_name}.id)"; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$orderby = $this->get_order_query(); |
245
|
|
|
|
246
|
|
|
$sql = $wpdb->prepare( "SELECT {$fields} FROM {$this->table_name} LIMIT %d,%d;", absint( $this->args['offset'] ), absint( $this->args['number'] ) ); |
247
|
|
|
|
248
|
|
|
// $where, $orderby and order already prepared query they can generate notice if you re prepare them in above. |
249
|
|
|
// WordPress consider LIKE condition as placeholder if start with s,f, or d. |
250
|
|
|
$sql = str_replace( 'LIMIT', "{$where} {$orderby} LIMIT", $sql ); |
251
|
|
|
|
252
|
|
|
return $sql; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Set query where clause. |
257
|
|
|
* |
258
|
|
|
* @since 1.8.14 |
259
|
|
|
* @access private |
260
|
|
|
* |
261
|
|
|
* @global wpdb $wpdb |
262
|
|
|
* @return string |
263
|
|
|
*/ |
264
|
|
|
private function get_where_query() { |
265
|
|
|
|
266
|
|
|
// Get sql query for meta. |
267
|
|
|
if ( ! empty( $this->args['meta_query'] ) ) { |
268
|
|
|
$meta_query_object = new WP_Meta_Query( $this->args['meta_query'] ); |
269
|
|
|
$meta_query = $meta_query_object->get_sql( $this->meta_type, $this->table_name, 'id' ); |
270
|
|
|
|
271
|
|
|
$where[] = implode( '', $meta_query ); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
$where[] = 'WHERE 1=1'; |
275
|
|
|
$where[] = $this->get_where_search(); |
276
|
|
|
$where[] = $this->get_where_email(); |
277
|
|
|
$where[] = $this->get_where_donor(); |
278
|
|
|
$where[] = $this->get_where_user(); |
279
|
|
|
$where[] = $this->get_where_date(); |
280
|
|
|
$where[] = $this->get_where_donation_amount(); |
281
|
|
|
$where[] = $this->get_where_donation_count(); |
282
|
|
|
$where[] = $this->get_where_give_forms(); |
283
|
|
|
|
284
|
|
|
$where = array_filter( $where ); |
285
|
|
|
|
286
|
|
|
return trim( implode( ' ', array_map( 'trim', $where ) ) ); |
287
|
|
|
|
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Set email where clause. |
292
|
|
|
* |
293
|
|
|
* @since 1.8.14 |
294
|
|
|
* @access private |
295
|
|
|
* |
296
|
|
|
* @global wpdb $wpdb |
297
|
|
|
* @return string |
298
|
|
|
*/ |
299
|
|
|
private function get_where_email() { |
300
|
|
|
global $wpdb; |
301
|
|
|
|
302
|
|
|
$where = ''; |
303
|
|
|
|
304
|
|
|
if ( ! empty( $this->args['email'] ) ) { |
305
|
|
|
|
306
|
|
|
if ( is_array( $this->args['email'] ) ) { |
307
|
|
|
|
308
|
|
|
$emails_count = count( $this->args['email'] ); |
309
|
|
|
$emails_placeholder = array_fill( 0, $emails_count, '%s' ); |
310
|
|
|
$emails = implode( ', ', $emails_placeholder ); |
311
|
|
|
|
312
|
|
|
$where .= $wpdb->prepare( "AND {$this->table_name}.email IN( $emails )", $this->args['email'] ); |
313
|
|
|
} else { |
314
|
|
|
$where .= $wpdb->prepare( "AND {$this->table_name}.email = %s", $this->args['email'] ); |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
return $where; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Set donor where clause. |
323
|
|
|
* |
324
|
|
|
* @since 1.8.14 |
325
|
|
|
* @access private |
326
|
|
|
* |
327
|
|
|
* @global wpdb $wpdb |
328
|
|
|
* @return string |
329
|
|
|
*/ |
330
|
|
View Code Duplication |
private function get_where_donor() { |
|
|
|
|
331
|
|
|
$where = ''; |
332
|
|
|
|
333
|
|
|
// Specific donors. |
334
|
|
|
if ( ! empty( $this->args['donor'] ) ) { |
335
|
|
|
if ( ! is_array( $this->args['donor'] ) ) { |
336
|
|
|
$this->args['donor'] = explode( ',', $this->args['donor'] ); |
337
|
|
|
} |
338
|
|
|
$donor_ids = implode( ',', array_map( 'intval', $this->args['donor'] ) ); |
339
|
|
|
|
340
|
|
|
$where .= "AND {$this->table_name}.id IN( {$donor_ids} )"; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
return $where; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Set date where clause. |
348
|
|
|
* |
349
|
|
|
* @since 1.8.14 |
350
|
|
|
* @access private |
351
|
|
|
* |
352
|
|
|
* @global wpdb $wpdb |
353
|
|
|
* @return string |
354
|
|
|
*/ |
355
|
|
|
private function get_where_date() { |
356
|
|
|
$where = ''; |
357
|
|
|
|
358
|
|
|
// Donors created for a specific date or in a date range |
359
|
|
|
if ( ! empty( $this->args['date_query'] ) ) { |
360
|
|
|
$date_query_object = new WP_Date_Query( is_array( $this->args['date_query'] ) ? $this->args['date_query'] : wp_parse_args( $this->args['date_query'] ), "{$this->table_name}.date_created" ); |
361
|
|
|
|
362
|
|
|
$where .= str_replace( array( |
363
|
|
|
"\n", |
364
|
|
|
'( (', |
365
|
|
|
'))', |
366
|
|
|
), array( |
|
|
|
|
367
|
|
|
'', |
368
|
|
|
'( (', |
369
|
|
|
') )', |
370
|
|
|
), $date_query_object->get_sql() ); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
return $where; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Set search where clause. |
378
|
|
|
* |
379
|
|
|
* @since 1.8.14 |
380
|
|
|
* @access private |
381
|
|
|
* |
382
|
|
|
* @global wpdb $wpdb |
383
|
|
|
* @return string |
384
|
|
|
*/ |
385
|
|
|
private function get_where_search() { |
386
|
|
|
$where = ''; |
387
|
|
|
|
388
|
|
|
// Bailout. |
389
|
|
|
if( empty( $this->args['s'] ) ) { |
|
|
|
|
390
|
|
|
return $where; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
// Donors created for a specific date or in a date range |
394
|
|
|
if ( false !== strpos( $this->args['s'], ':' ) ) { |
395
|
|
|
$search_parts = explode( ':', $this->args['s'] ); |
396
|
|
|
if ( ! empty( $search_parts[0] ) ) { |
397
|
|
|
switch ( $search_parts[0] ) { |
398
|
|
|
// Backward compatibility. |
399
|
|
|
case 'name': |
400
|
|
|
$where = "AND {$this->table_name}.name LIKE '%{$search_parts[1]}%'"; |
401
|
|
|
break; |
402
|
|
|
case 'note': |
403
|
|
|
$where = "AND {$this->table_name}.notes LIKE '%{$search_parts[1]}%'"; |
404
|
|
|
break; |
405
|
|
|
} |
406
|
|
|
} |
|
|
|
|
407
|
|
|
|
408
|
|
|
} else if ( is_numeric( $this->args['s'] ) ) { |
409
|
|
|
$where = "AND {$this->table_name}.id ='{$this->args['s']}'"; |
410
|
|
|
|
411
|
|
|
} else { |
412
|
|
|
$search_field = is_email( $this->args['s'] ) ? 'email' : 'name'; |
413
|
|
|
$where = "AND {$this->table_name}.$search_field LIKE '%{$this->args['s']}%'"; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
return $where; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Set user where clause. |
421
|
|
|
* |
422
|
|
|
* @since 1.8.14 |
423
|
|
|
* @access private |
424
|
|
|
* |
425
|
|
|
* @global wpdb $wpdb |
426
|
|
|
* @return string |
427
|
|
|
*/ |
428
|
|
View Code Duplication |
private function get_where_user() { |
|
|
|
|
429
|
|
|
$where = ''; |
430
|
|
|
|
431
|
|
|
// Donors create for specific wp user. |
432
|
|
|
if ( ! empty( $this->args['user'] ) ) { |
433
|
|
|
if ( ! is_array( $this->args['user'] ) ) { |
434
|
|
|
$this->args['user'] = explode( ',', $this->args['user'] ); |
435
|
|
|
} |
436
|
|
|
$user_ids = implode( ',', array_map( 'intval', $this->args['user'] ) ); |
437
|
|
|
|
438
|
|
|
$where .= "AND {$this->table_name}.user_id IN( {$user_ids} )"; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
return $where; |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* Set orderby query |
446
|
|
|
* |
447
|
|
|
* @since 1.8.14 |
448
|
|
|
* @access private |
449
|
|
|
* |
450
|
|
|
* @return string |
451
|
|
|
*/ |
452
|
|
|
private function get_order_query() { |
453
|
|
|
$table_columns = Give()->donors->get_columns(); |
454
|
|
|
|
455
|
|
|
$query = array(); |
456
|
|
|
$ordersby = $this->args['orderby']; |
457
|
|
|
|
458
|
|
|
if( ! is_array( $ordersby ) ) { |
|
|
|
|
459
|
|
|
$ordersby = array( |
460
|
|
|
$this->args['orderby'] => $this->args['order'] |
461
|
|
|
); |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
// Remove non existing column. |
465
|
|
|
// Filter orderby values. |
466
|
|
|
foreach ( $ordersby as $orderby => $order ) { |
467
|
|
|
if( ! array_key_exists( $orderby, $table_columns ) ) { |
|
|
|
|
468
|
|
|
unset( $ordersby[$orderby] ); |
|
|
|
|
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
$ordersby[ esc_sql( $orderby ) ] = esc_sql( $order ); |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
if( empty( $ordersby ) ) { |
|
|
|
|
475
|
|
|
$ordersby = array( |
476
|
|
|
'id' => $this->args['order'] |
477
|
|
|
); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
// Create query. |
481
|
|
|
foreach ( $ordersby as $orderby => $order ) { |
482
|
|
|
switch ( $table_columns[ $orderby ] ) { |
483
|
|
|
case '%d': |
484
|
|
|
case '%f': |
485
|
|
|
$query[] = "{$this->table_name}.{$orderby}+0 {$order}"; |
486
|
|
|
break; |
487
|
|
|
|
488
|
|
|
default: |
489
|
|
|
$query[] = "{$this->table_name}.{$orderby} {$order}"; |
490
|
|
|
} |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
return ! empty( $query ) ? 'ORDER BY ' . implode( ', ', $query ) : ''; |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* Set donation count value where clause. |
498
|
|
|
* @todo: add phpunit test |
499
|
|
|
* |
500
|
|
|
* @since 2.2.0 |
501
|
|
|
* @access private |
502
|
|
|
* |
503
|
|
|
* @global wpdb $wpdb |
504
|
|
|
* @return string |
505
|
|
|
*/ |
506
|
|
View Code Duplication |
private function get_where_donation_count() { |
|
|
|
|
507
|
|
|
$where = ''; |
508
|
|
|
|
509
|
|
|
if ( ! empty( $this->args['donation_count'] ) ) { |
510
|
|
|
$compare = '>'; |
511
|
|
|
$amount = $this->args['donation_count']; |
512
|
|
|
if ( is_array( $this->args['donation_count'] ) ) { |
513
|
|
|
$compare = $this->args['donation_count'] ['compare']; |
514
|
|
|
$amount = $this->args['donation_count']['amount']; |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
$where .= "AND {$this->table_name}.purchase_count{$compare}{$amount}"; |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
return $where; |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
/** |
524
|
|
|
* Set purchase value where clause. |
525
|
|
|
* @todo: add phpunit test |
526
|
|
|
* |
527
|
|
|
* @since 2.1.0 |
528
|
|
|
* @access private |
529
|
|
|
* |
530
|
|
|
* @global wpdb $wpdb |
531
|
|
|
* @return string |
532
|
|
|
*/ |
533
|
|
View Code Duplication |
private function get_where_donation_amount() { |
|
|
|
|
534
|
|
|
$where = ''; |
535
|
|
|
|
536
|
|
|
if ( ! empty( $this->args['donation_amount'] ) ) { |
537
|
|
|
$compare = '>'; |
538
|
|
|
$amount = $this->args['donation_amount']; |
539
|
|
|
if ( is_array( $this->args['donation_amount'] ) ) { |
540
|
|
|
$compare = $this->args['donation_amount'] ['compare']; |
541
|
|
|
$amount = $this->args['donation_amount']['amount']; |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
$where .= "AND {$this->table_name}.purchase_value{$compare}{$amount}"; |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
return $where; |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
/** |
551
|
|
|
* Set give_forms where clause. |
552
|
|
|
* |
553
|
|
|
* @todo : add phpunit test |
554
|
|
|
* |
555
|
|
|
* @since 2.1.0 |
556
|
|
|
* @access private |
557
|
|
|
* |
558
|
|
|
* @global wpdb $wpdb |
559
|
|
|
* @return string |
560
|
|
|
*/ |
561
|
|
|
private function get_where_give_forms() { |
562
|
|
|
global $wpdb; |
563
|
|
|
$where = ''; |
564
|
|
|
|
565
|
|
|
if ( ! empty( $this->args['give_forms'] ) ) { |
566
|
|
|
if ( ! is_array( $this->args['give_forms'] ) ) { |
567
|
|
|
$this->args['give_forms'] = explode( ',', $this->args['give_forms'] ); |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
$form_ids = implode( ',', array_map( 'intval', $this->args['give_forms'] ) ); |
571
|
|
|
$donation_id_col = Give()->payment_meta->get_meta_type() . '_id'; |
572
|
|
|
|
573
|
|
|
$query = $wpdb->prepare( |
574
|
|
|
" |
575
|
|
|
SELECT DISTINCT meta_value as donor_id |
576
|
|
|
FROM {$wpdb->donationmeta} |
577
|
|
|
WHERE meta_key=%s |
578
|
|
|
AND {$donation_id_col} IN( |
579
|
|
|
SELECT {$donation_id_col} |
580
|
|
|
FROM {$wpdb->paymentmeta} |
581
|
|
|
WHERE meta_key=%s |
582
|
|
|
AND meta_value IN (%s) |
583
|
|
|
) |
584
|
|
|
", |
585
|
|
|
'_give_payment_donor_id', |
586
|
|
|
'_give_payment_form_id', |
587
|
|
|
$form_ids |
588
|
|
|
); |
589
|
|
|
|
590
|
|
|
$donor_ids = $wpdb->get_results( $query, ARRAY_A ); |
|
|
|
|
591
|
|
|
|
592
|
|
|
if ( ! empty( $donor_ids ) ) { |
593
|
|
|
$donor_ids = wp_list_pluck( $donor_ids, 'donor_id' ); |
594
|
|
|
$donor_ids = implode( ',', array_map( 'intval', $donor_ids ) ); |
595
|
|
|
$where .= "AND {$this->table_name}.id IN ({$donor_ids})"; |
596
|
|
|
} else { |
597
|
|
|
$where .= "AND {$this->table_name}.id IN ('0')"; |
598
|
|
|
} |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
return $where; |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
/** |
605
|
|
|
* If querying a specific date, add the proper filters. |
606
|
|
|
* Note: This function currently only accept dates with admin defined core date format |
607
|
|
|
* |
608
|
|
|
* @since 2.4.0 |
609
|
|
|
* @access public |
610
|
|
|
* |
611
|
|
|
* @return void |
612
|
|
|
*/ |
613
|
|
|
public function date_filter_pre() { |
614
|
|
|
if ( |
615
|
|
|
! empty( $this->args['date_query'] ) |
616
|
|
|
|| empty( $this->args['start_date'] ) |
617
|
|
|
|| empty( $this->args['end_date'] ) |
618
|
|
|
) { |
619
|
|
|
return; |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
$date_query = array(); |
623
|
|
|
|
624
|
|
View Code Duplication |
if ( ! empty ( $this->args['start_date'] ) ) { |
|
|
|
|
625
|
|
|
$date_query['after'] = date( |
626
|
|
|
'Y-m-d H:i:s', |
627
|
|
|
is_numeric( $this->args['start_date'] ) |
628
|
|
|
? $this->args['start_date'] |
629
|
|
|
: strtotime( $this->args['start_date'] ) |
630
|
|
|
); |
631
|
|
|
} |
632
|
|
|
|
633
|
|
View Code Duplication |
if ( ! empty ( $this->args['end_date'] ) ) { |
|
|
|
|
634
|
|
|
$date_query['before'] = date( |
635
|
|
|
'Y-m-d H:i:s', |
636
|
|
|
is_numeric( $this->args['end_date'] ) |
637
|
|
|
? $this->args['end_date'] |
638
|
|
|
: strtotime( $this->args['end_date'] ) |
639
|
|
|
); |
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
// Include Start Date and End Date while querying. |
643
|
|
|
$date_query['inclusive'] = true; |
644
|
|
|
|
645
|
|
|
$this->__set( 'date_query', $date_query ); |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
/** |
649
|
|
|
* Update donors meta cache |
650
|
|
|
* |
651
|
|
|
* @since 2.5.0 |
652
|
|
|
* @access private |
653
|
|
|
* |
654
|
|
|
* @param array $donor_ids |
655
|
|
|
*/ |
656
|
|
|
public static function update_meta_cache( $donor_ids ){ |
657
|
|
|
// Exit. |
658
|
|
|
if ( empty( $donor_ids ) ) { |
659
|
|
|
return; |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
update_meta_cache( Give()->donor_meta->get_meta_type(), $donor_ids ); |
663
|
|
|
} |
664
|
|
|
|
665
|
|
|
/** |
666
|
|
|
* Set a query variable. |
667
|
|
|
* |
668
|
|
|
* @since 2.4.0 |
669
|
|
|
* @access public |
670
|
|
|
* |
671
|
|
|
* @param $query_var |
672
|
|
|
* @param $value |
673
|
|
|
*/ |
674
|
|
View Code Duplication |
public function __set( $query_var, $value ) { |
|
|
|
|
675
|
|
|
if ( in_array( $query_var, array( 'meta_query', 'tax_query' ) ) ) { |
676
|
|
|
$this->args[ $query_var ][] = $value; |
677
|
|
|
} else { |
678
|
|
|
$this->args[ $query_var ] = $value; |
679
|
|
|
} |
680
|
|
|
} |
681
|
|
|
|
682
|
|
|
} |
683
|
|
|
|