1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Donors DB |
4
|
|
|
* |
5
|
|
|
* @package Give |
6
|
|
|
* @subpackage Classes/Give_DB_Donors |
7
|
|
|
* @copyright Copyright (c) 2016, WordImpress |
8
|
|
|
* @license https://opensource.org/licenses/gpl-license GNU Public License |
9
|
|
|
* @since 1.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// Exit if accessed directly. |
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
14
|
|
|
exit; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Give_DB_Donors Class |
19
|
|
|
* |
20
|
|
|
* This class is for interacting with the donor database table. |
21
|
|
|
* |
22
|
|
|
* @since 1.0 |
23
|
|
|
*/ |
24
|
|
|
class Give_DB_Donors extends Give_DB { |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Give_DB_Donors constructor. |
28
|
|
|
* |
29
|
|
|
* Set up the Give DB Donor class. |
30
|
|
|
* |
31
|
|
|
* @since 1.0 |
32
|
|
|
* @access public |
33
|
|
|
*/ |
34
|
|
|
public function __construct() { |
35
|
|
|
/* @var WPDB $wpdb */ |
36
|
|
|
global $wpdb; |
37
|
|
|
|
38
|
|
|
$this->table_name = $wpdb->prefix . 'give_customers'; |
39
|
|
|
$this->primary_key = 'id'; |
40
|
|
|
$this->version = '1.0'; |
41
|
|
|
|
42
|
|
|
// Set hooks and register table only if instance loading first time. |
43
|
|
|
if ( ! ( Give()->donors instanceof Give_DB_Donors ) ) { |
44
|
|
|
// Setup hook. |
45
|
|
|
add_action( 'profile_update', array( $this, 'update_donor_email_on_user_update' ), 10, 2 ); |
46
|
|
|
|
47
|
|
|
// Install table. |
48
|
|
|
$this->register_table(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get columns and formats |
55
|
|
|
* |
56
|
|
|
* @since 1.0 |
57
|
|
|
* @access public |
58
|
|
|
* |
59
|
|
|
* @return array Columns and formats. |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function get_columns() { |
|
|
|
|
62
|
|
|
return array( |
63
|
|
|
'id' => '%d', |
64
|
|
|
'user_id' => '%d', |
65
|
|
|
'name' => '%s', |
66
|
|
|
'email' => '%s', |
67
|
|
|
'payment_ids' => '%s', |
68
|
|
|
'purchase_value' => '%f', |
69
|
|
|
'purchase_count' => '%d', |
70
|
|
|
'notes' => '%s', |
71
|
|
|
'date_created' => '%s', |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get default column values |
77
|
|
|
* |
78
|
|
|
* @since 1.0 |
79
|
|
|
* @access public |
80
|
|
|
* |
81
|
|
|
* @return array Default column values. |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
public function get_column_defaults() { |
|
|
|
|
84
|
|
|
return array( |
85
|
|
|
'user_id' => 0, |
86
|
|
|
'email' => '', |
87
|
|
|
'name' => '', |
88
|
|
|
'payment_ids' => '', |
89
|
|
|
'purchase_value' => 0.00, |
90
|
|
|
'purchase_count' => 0, |
91
|
|
|
'notes' => '', |
92
|
|
|
'date_created' => date( 'Y-m-d H:i:s' ), |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Add a donor |
98
|
|
|
* |
99
|
|
|
* @since 1.0 |
100
|
|
|
* @access public |
101
|
|
|
* |
102
|
|
|
* @param array $data |
103
|
|
|
* |
104
|
|
|
* @return int|bool |
105
|
|
|
*/ |
106
|
|
|
public function add( $data = array() ) { |
107
|
|
|
|
108
|
|
|
$defaults = array( |
109
|
|
|
'payment_ids' => '', |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$args = wp_parse_args( $data, $defaults ); |
113
|
|
|
|
114
|
|
|
if ( empty( $args['email'] ) ) { |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
View Code Duplication |
if ( ! empty( $args['payment_ids'] ) && is_array( $args['payment_ids'] ) ) { |
|
|
|
|
119
|
|
|
$args['payment_ids'] = implode( ',', array_unique( array_values( $args['payment_ids'] ) ) ); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$donor = $this->get_donor_by( 'email', $args['email'] ); |
123
|
|
|
|
124
|
|
|
// update an existing donor. |
125
|
|
|
if ( $donor ) { |
126
|
|
|
|
127
|
|
|
// Update the payment IDs attached to the donor |
128
|
|
|
if ( ! empty( $args['payment_ids'] ) ) { |
129
|
|
|
|
130
|
|
|
if ( empty( $donor->payment_ids ) ) { |
131
|
|
|
|
132
|
|
|
$donor->payment_ids = $args['payment_ids']; |
133
|
|
|
|
134
|
|
|
} else { |
135
|
|
|
|
136
|
|
|
$existing_ids = array_map( 'absint', explode( ',', $donor->payment_ids ) ); |
137
|
|
|
$payment_ids = array_map( 'absint', explode( ',', $args['payment_ids'] ) ); |
138
|
|
|
$payment_ids = array_merge( $payment_ids, $existing_ids ); |
139
|
|
|
$donor->payment_ids = implode( ',', array_unique( array_values( $payment_ids ) ) ); |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$args['payment_ids'] = $donor->payment_ids; |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$this->update( $donor->id, $args ); |
148
|
|
|
|
149
|
|
|
return $donor->id; |
150
|
|
|
|
151
|
|
|
} else { |
152
|
|
|
|
153
|
|
|
return $this->insert( $args, 'donor' ); |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Delete a donor. |
161
|
|
|
* |
162
|
|
|
* NOTE: This should not be called directly as it does not make necessary changes to |
163
|
|
|
* the payment meta and logs. Use give_donor_delete() instead. |
164
|
|
|
* |
165
|
|
|
* @since 1.0 |
166
|
|
|
* @access public |
167
|
|
|
* |
168
|
|
|
* @param bool|string|int $_id_or_email |
169
|
|
|
* |
170
|
|
|
* @return bool|int |
171
|
|
|
*/ |
172
|
|
|
public function delete( $_id_or_email = false ) { |
173
|
|
|
|
174
|
|
|
if ( empty( $_id_or_email ) ) { |
175
|
|
|
return false; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$column = is_email( $_id_or_email ) ? 'email' : 'id'; |
179
|
|
|
$donor = $this->get_donor_by( $column, $_id_or_email ); |
180
|
|
|
|
181
|
|
|
if ( $donor->id > 0 ) { |
182
|
|
|
|
183
|
|
|
global $wpdb; |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Deleting the donor meta. |
187
|
|
|
* |
188
|
|
|
* @since 1.8.14 |
189
|
|
|
*/ |
190
|
|
|
Give()->donor_meta->delete_all_meta( $donor->id ); |
191
|
|
|
|
192
|
|
|
return $wpdb->delete( $this->table_name, array( 'id' => $donor->id ), array( '%d' ) ); |
193
|
|
|
|
194
|
|
|
} else { |
195
|
|
|
return false; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Delete a donor by user ID. |
202
|
|
|
* |
203
|
|
|
* NOTE: This should not be called directly as it does not make necessary changes to |
204
|
|
|
* the payment meta and logs. Use give_donor_delete() instead. |
205
|
|
|
* |
206
|
|
|
* @since 1.0 |
207
|
|
|
* @access public |
208
|
|
|
* |
209
|
|
|
* @param int|bool $user_id |
210
|
|
|
* |
211
|
|
|
* @return bool|int |
212
|
|
|
*/ |
213
|
|
|
public function delete_by_user_id( $user_id = false ) { |
214
|
|
|
|
215
|
|
|
if ( empty( $user_id ) ) { |
216
|
|
|
return false; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Deleting the donor meta. |
221
|
|
|
* |
222
|
|
|
* @since 1.8.14 |
223
|
|
|
*/ |
224
|
|
|
$donor = new Give_Donor( $user_id, true ); |
|
|
|
|
225
|
|
|
if ( ! empty( $donor->id ) ) { |
226
|
|
|
Give()->donor_meta->delete_all_meta( $donor->id ); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
global $wpdb; |
230
|
|
|
|
231
|
|
|
return $wpdb->delete( $this->table_name, array( 'user_id' => $user_id ), array( '%d' ) ); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Checks if a donor exists |
236
|
|
|
* |
237
|
|
|
* @since 1.0 |
238
|
|
|
* @access public |
239
|
|
|
* |
240
|
|
|
* @param string $value The value to search for. Default is empty. |
241
|
|
|
* @param string $field The Donor ID or email to search in. Default is 'email'. |
242
|
|
|
* |
243
|
|
|
* @return bool True is exists, false otherwise. |
244
|
|
|
*/ |
245
|
|
|
public function exists( $value = '', $field = 'email' ) { |
246
|
|
|
|
247
|
|
|
$columns = $this->get_columns(); |
248
|
|
|
if ( ! array_key_exists( $field, $columns ) ) { |
249
|
|
|
return false; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return (bool) $this->get_column_by( 'id', $field, $value ); |
253
|
|
|
|
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Attaches a payment ID to a donor |
258
|
|
|
* |
259
|
|
|
* @since 1.0 |
260
|
|
|
* @access public |
261
|
|
|
* |
262
|
|
|
* @param int $donor_id Donor ID. |
263
|
|
|
* @param int $payment_id Payment ID. |
264
|
|
|
* |
265
|
|
|
* @return bool |
266
|
|
|
*/ |
267
|
|
|
public function attach_payment( $donor_id = 0, $payment_id = 0 ) { |
268
|
|
|
|
269
|
|
|
$donor = new Give_Donor( $donor_id ); |
|
|
|
|
270
|
|
|
|
271
|
|
|
if ( empty( $donor->id ) ) { |
272
|
|
|
return false; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
// Attach the payment, but don't increment stats, as this function previously did not |
276
|
|
|
return $donor->attach_payment( $payment_id, false ); |
277
|
|
|
|
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Removes a payment ID from a donor. |
282
|
|
|
* |
283
|
|
|
* @since 1.0 |
284
|
|
|
* @access public |
285
|
|
|
* |
286
|
|
|
* @param int $donor_id Donor ID. |
287
|
|
|
* @param int $payment_id Payment ID. |
288
|
|
|
* |
289
|
|
|
* @return bool |
290
|
|
|
*/ |
291
|
|
|
public function remove_payment( $donor_id = 0, $payment_id = 0 ) { |
292
|
|
|
|
293
|
|
|
$donor = new Give_Donor( $donor_id ); |
|
|
|
|
294
|
|
|
|
295
|
|
|
if ( ! $donor ) { |
296
|
|
|
return false; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
// Remove the payment, but don't decrease stats, as this function previously did not |
300
|
|
|
return $donor->remove_payment( $payment_id, false ); |
301
|
|
|
|
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Increments donor's donation stats. |
306
|
|
|
* |
307
|
|
|
* @access public |
308
|
|
|
* |
309
|
|
|
* @param int $donor_id Donor ID. |
310
|
|
|
* @param float $amount THe amount to increase. |
311
|
|
|
* |
312
|
|
|
* @return bool |
313
|
|
|
*/ |
314
|
|
View Code Duplication |
public function increment_stats( $donor_id = 0, $amount = 0.00 ) { |
|
|
|
|
315
|
|
|
|
316
|
|
|
$donor = new Give_Donor( $donor_id ); |
|
|
|
|
317
|
|
|
|
318
|
|
|
if ( empty( $donor->id ) ) { |
319
|
|
|
return false; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
$increased_count = $donor->increase_purchase_count(); |
323
|
|
|
$increased_value = $donor->increase_value( $amount ); |
324
|
|
|
|
325
|
|
|
return ( $increased_count && $increased_value ) ? true : false; |
|
|
|
|
326
|
|
|
|
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Decrements donor's donation stats. |
331
|
|
|
* |
332
|
|
|
* @since 1.0 |
333
|
|
|
* @access public |
334
|
|
|
* |
335
|
|
|
* @param int $donor_id Donor ID. |
336
|
|
|
* @param float $amount Amount. |
337
|
|
|
* |
338
|
|
|
* @return bool |
339
|
|
|
*/ |
340
|
|
View Code Duplication |
public function decrement_stats( $donor_id = 0, $amount = 0.00 ) { |
|
|
|
|
341
|
|
|
|
342
|
|
|
$donor = new Give_Donor( $donor_id ); |
|
|
|
|
343
|
|
|
|
344
|
|
|
if ( ! $donor ) { |
345
|
|
|
return false; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
$decreased_count = $donor->decrease_donation_count(); |
349
|
|
|
$decreased_value = $donor->decrease_value( $amount ); |
350
|
|
|
|
351
|
|
|
return ( $decreased_count && $decreased_value ) ? true : false; |
|
|
|
|
352
|
|
|
|
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Updates the email address of a donor record when the email on a user is updated |
357
|
|
|
* |
358
|
|
|
* @since 1.4.3 |
359
|
|
|
* @access public |
360
|
|
|
* |
361
|
|
|
* @param int $user_id User ID. |
362
|
|
|
* @param WP_User|bool $old_user_data User data. |
363
|
|
|
* |
364
|
|
|
* @return bool |
365
|
|
|
*/ |
366
|
|
|
public function update_donor_email_on_user_update( $user_id = 0, $old_user_data = false ) { |
|
|
|
|
367
|
|
|
|
368
|
|
|
$donor = new Give_Donor( $user_id, true ); |
|
|
|
|
369
|
|
|
|
370
|
|
|
if ( ! $donor ) { |
371
|
|
|
return false; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
$user = get_userdata( $user_id ); |
375
|
|
|
|
376
|
|
|
if ( ! empty( $user ) && $user->user_email !== $donor->email ) { |
377
|
|
|
|
378
|
|
|
if ( ! $this->get_donor_by( 'email', $user->user_email ) ) { |
379
|
|
|
|
380
|
|
|
$success = $this->update( $donor->id, array( 'email' => $user->user_email ) ); |
381
|
|
|
|
382
|
|
|
if ( $success ) { |
383
|
|
|
// Update some payment meta if we need to |
384
|
|
|
$payments_array = explode( ',', $donor->payment_ids ); |
385
|
|
|
|
386
|
|
|
if ( ! empty( $payments_array ) ) { |
387
|
|
|
|
388
|
|
|
foreach ( $payments_array as $payment_id ) { |
389
|
|
|
|
390
|
|
|
give_update_payment_meta( $payment_id, 'email', $user->user_email ); |
391
|
|
|
|
392
|
|
|
} |
|
|
|
|
393
|
|
|
|
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Fires after updating donor email on user update. |
398
|
|
|
* |
399
|
|
|
* @since 1.4.3 |
400
|
|
|
* |
401
|
|
|
* @param WP_User $user WordPress User object. |
402
|
|
|
* @param Give_Donor $donor Give donor object. |
403
|
|
|
*/ |
404
|
|
|
do_action( 'give_update_donor_email_on_user_update', $user, $donor ); |
405
|
|
|
|
406
|
|
|
} |
|
|
|
|
407
|
|
|
|
408
|
|
|
} |
|
|
|
|
409
|
|
|
|
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* Retrieves a single donor from the database |
416
|
|
|
* |
417
|
|
|
* @since 1.0 |
418
|
|
|
* @access public |
419
|
|
|
* |
420
|
|
|
* @param string $field ID or email. Default is 'id'. |
421
|
|
|
* @param mixed $value The Customer ID or email to search. Default is 0. |
422
|
|
|
* |
423
|
|
|
* @return mixed Upon success, an object of the donor. Upon failure, NULL |
424
|
|
|
*/ |
425
|
|
|
public function get_donor_by( $field = 'id', $value = 0 ) { |
426
|
|
|
$value = sanitize_text_field( $value ); |
427
|
|
|
|
428
|
|
|
// Bailout. |
429
|
|
|
if ( empty( $field ) || empty( $value ) ) { |
430
|
|
|
return null; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
// Verify values. |
434
|
|
|
if ( 'id' === $field || 'user_id' === $field ) { |
435
|
|
|
// Make sure the value is numeric to avoid casting objects, for example, |
436
|
|
|
// to int 1. |
437
|
|
|
if ( ! is_numeric( $value ) ) { |
438
|
|
|
return false; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
$value = absint( $value ); |
442
|
|
|
|
443
|
|
|
if ( $value < 1 ) { |
444
|
|
|
return false; |
445
|
|
|
} |
|
|
|
|
446
|
|
|
|
447
|
|
|
} elseif ( 'email' === $field ) { |
448
|
|
|
|
449
|
|
|
if ( ! is_email( $value ) ) { |
450
|
|
|
return false; |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
$value = trim( $value ); |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
// Bailout |
457
|
|
|
if ( ! $value ) { |
458
|
|
|
return false; |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
// Set query params. |
462
|
|
|
switch ( $field ) { |
463
|
|
|
case 'id': |
464
|
|
|
$args['donor'] = $value; |
465
|
|
|
break; |
466
|
|
|
case 'email': |
467
|
|
|
$args['email'] = $value; |
468
|
|
|
break; |
469
|
|
|
case 'user_id': |
470
|
|
|
$args['user'] = $value; |
471
|
|
|
break; |
472
|
|
|
default: |
473
|
|
|
return false; |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
// Get donors. |
477
|
|
|
$donor = new Give_Donors_Query( $args ); |
478
|
|
|
|
479
|
|
|
if ( ! $donor = $donor->get_donors() ) { |
480
|
|
|
// Look for donor from an additional email. |
481
|
|
|
$args = array( |
482
|
|
|
'meta_query' => array( |
|
|
|
|
483
|
|
|
array( |
484
|
|
|
'key' => 'additional_email', |
485
|
|
|
'value' => $value, |
486
|
|
|
), |
487
|
|
|
), |
488
|
|
|
); |
489
|
|
|
|
490
|
|
|
$donor = new Give_Donors_Query( $args ); |
491
|
|
|
$donor = $donor->get_donors(); |
492
|
|
|
|
493
|
|
|
if ( empty( $donor ) ) { |
494
|
|
|
return false; |
495
|
|
|
} |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
return current( $donor ); |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* Retrieve donors from the database. |
503
|
|
|
* |
504
|
|
|
* @since 1.0 |
505
|
|
|
* @access public |
506
|
|
|
* |
507
|
|
|
* @param array $args |
508
|
|
|
* |
509
|
|
|
* @return array|object|null Donors array or object. Null if not found. |
510
|
|
|
*/ |
511
|
|
|
public function get_donors( $args = array() ) { |
512
|
|
|
$this->bc_1814_params( $args ); |
513
|
|
|
|
514
|
|
|
$cache_key = md5( 'give_donors_' . serialize( $args ) ); |
515
|
|
|
|
516
|
|
|
$donors = wp_cache_get( $cache_key, 'donors' ); |
517
|
|
|
|
518
|
|
View Code Duplication |
if ( $donors === false ) { |
|
|
|
|
519
|
|
|
$donors = new Give_Donors_Query( $args ); |
520
|
|
|
$donors = $donors->get_donors(); |
521
|
|
|
|
522
|
|
|
wp_cache_set( $cache_key, $donors, 'donors', 3600 ); |
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
return $donors; |
526
|
|
|
|
527
|
|
|
} |
528
|
|
|
|
529
|
|
|
|
530
|
|
|
/** |
531
|
|
|
* Count the total number of donors in the database |
532
|
|
|
* |
533
|
|
|
* @since 1.0 |
534
|
|
|
* @access public |
535
|
|
|
* |
536
|
|
|
* @param array $args |
537
|
|
|
* |
538
|
|
|
* @return int Total number of donors. |
539
|
|
|
*/ |
540
|
|
|
public function count( $args = array() ) { |
541
|
|
|
$this->bc_1814_params( $args ); |
542
|
|
|
$args['count'] = true; |
543
|
|
|
|
544
|
|
|
$cache_key = md5( 'give_donors_count' . serialize( $args ) ); |
545
|
|
|
$count = wp_cache_get( $cache_key, 'donors' ); |
546
|
|
|
|
547
|
|
View Code Duplication |
if ( $count === false ) { |
|
|
|
|
548
|
|
|
$donors = new Give_Donors_Query( $args ); |
549
|
|
|
$count = $donors->get_donors(); |
550
|
|
|
|
551
|
|
|
wp_cache_set( $cache_key, $count, 'donors', 3600 ); |
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
return absint( $count ); |
555
|
|
|
|
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
/** |
559
|
|
|
* Create the table |
560
|
|
|
* |
561
|
|
|
* @since 1.0 |
562
|
|
|
* @access public |
563
|
|
|
* |
564
|
|
|
* @return void |
565
|
|
|
*/ |
566
|
|
View Code Duplication |
public function create_table() { |
|
|
|
|
567
|
|
|
|
568
|
|
|
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
569
|
|
|
|
570
|
|
|
$sql = "CREATE TABLE " . $this->table_name . " ( |
|
|
|
|
571
|
|
|
id bigint(20) NOT NULL AUTO_INCREMENT, |
572
|
|
|
user_id bigint(20) NOT NULL, |
573
|
|
|
email varchar(50) NOT NULL, |
574
|
|
|
name mediumtext NOT NULL, |
575
|
|
|
purchase_value mediumtext NOT NULL, |
576
|
|
|
purchase_count bigint(20) NOT NULL, |
577
|
|
|
payment_ids longtext NOT NULL, |
578
|
|
|
notes longtext NOT NULL, |
579
|
|
|
date_created datetime NOT NULL, |
580
|
|
|
PRIMARY KEY (id), |
581
|
|
|
UNIQUE KEY email (email), |
582
|
|
|
KEY user (user_id) |
583
|
|
|
) CHARACTER SET utf8 COLLATE utf8_general_ci;"; |
584
|
|
|
|
585
|
|
|
dbDelta( $sql ); |
586
|
|
|
|
587
|
|
|
update_option( $this->table_name . '_db_version', $this->version ); |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
/** |
591
|
|
|
* Check if the Customers table was ever installed |
592
|
|
|
* |
593
|
|
|
* @since 1.4.3 |
594
|
|
|
* @access public |
595
|
|
|
* |
596
|
|
|
* @return bool Returns if the donors table was installed and upgrade routine run. |
597
|
|
|
*/ |
598
|
|
|
public function installed() { |
599
|
|
|
return $this->table_exists( $this->table_name ); |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
/** |
603
|
|
|
* Add backward compatibility for deprecated param |
604
|
|
|
* |
605
|
|
|
* @since 1.8.14 |
606
|
|
|
* @access private |
607
|
|
|
* |
608
|
|
|
* @param $args |
609
|
|
|
*/ |
610
|
|
|
private function bc_1814_params( &$args ) { |
611
|
|
|
// Backward compatibility: user_id |
612
|
|
|
if ( ! empty( $args['user_id'] ) ) { |
613
|
|
|
$args['user'] = $args['user_id']; |
614
|
|
|
} |
615
|
|
|
|
616
|
|
|
// Backward compatibility: id |
617
|
|
|
if ( ! empty( $args['id'] ) ) { |
618
|
|
|
$args['donor'] = $args['id']; |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
// Backward compatibility: name |
622
|
|
|
if ( ! empty( $args['name'] ) ) { |
623
|
|
|
$args['s'] = "name:{$args['name']}"; |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
// Backward compatibility: date |
627
|
|
|
// Donors created for a specific date or in a date range. |
628
|
|
|
if ( ! empty( $args['date'] ) ) { |
629
|
|
|
|
630
|
|
|
if ( is_array( $args['date'] ) ) { |
631
|
|
|
|
632
|
|
View Code Duplication |
if ( ! empty( $args['date']['start'] ) ) { |
|
|
|
|
633
|
|
|
$args['date_query']['after'] = date( 'Y-m-d H:i:s', strtotime( $args['date']['start'] ) ); |
634
|
|
|
} |
635
|
|
|
|
636
|
|
View Code Duplication |
if ( ! empty( $args['date']['end'] ) ) { |
|
|
|
|
637
|
|
|
$args['date_query']['before'] = date( 'Y-m-d H:i:s', strtotime( $args['date']['end'] ) ); |
638
|
|
|
} |
|
|
|
|
639
|
|
|
|
640
|
|
|
} else { |
641
|
|
|
|
642
|
|
|
$args['date_query']['year'] = date( 'Y', strtotime( $args['date'] ) ); |
643
|
|
|
$args['date_query']['month'] = date( 'm', strtotime( $args['date'] ) ); |
644
|
|
|
$args['date_query']['day'] = date( 'd', strtotime( $args['date'] ) ); |
645
|
|
|
} |
|
|
|
|
646
|
|
|
|
647
|
|
|
} |
648
|
|
|
} |
649
|
|
|
} |
650
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.