PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.6.0
GiveWP – Donation Plugin and Fundraising Platform v2.6.0
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 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / database / class-give-db-donors.php
give / includes / database Last commit date
class-give-db-comments-meta.php 6 years ago class-give-db-comments.php 6 years ago class-give-db-donor-meta.php 6 years ago class-give-db-donors.php 6 years ago class-give-db-form-meta.php 6 years ago class-give-db-logs-meta.php 6 years ago class-give-db-logs.php 6 years ago class-give-db-meta.php 6 years ago class-give-db-payment-meta.php 6 years ago class-give-db-sequential-ordering.php 6 years ago class-give-db-sessions.php 6 years ago class-give-db.php 6 years ago
class-give-db-donors.php
654 lines
1 <?php
2 /**
3 * Donors DB
4 *
5 * @package Give
6 * @subpackage Classes/Give_DB_Donors
7 * @copyright Copyright (c) 2016, GiveWP
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 $wpdb->donors = $this->table_name = "{$wpdb->prefix}give_donors";
39 $this->primary_key = 'id';
40 $this->version = '1.0';
41
42 $this->bc_200_params();
43
44 parent::__construct();
45 }
46
47 /**
48 * Get columns and formats
49 *
50 * @since 1.0
51 * @access public
52 *
53 * @return array Columns and formats.
54 */
55 public function get_columns() {
56 return array(
57 'id' => '%d',
58 'user_id' => '%d',
59 'name' => '%s',
60 'email' => '%s',
61 'payment_ids' => '%s',
62 'purchase_value' => '%f',
63 'purchase_count' => '%d',
64 'date_created' => '%s',
65 'token' => '%s',
66 'verify_key' => '%s',
67 'verify_throttle' => '%s',
68 );
69 }
70
71 /**
72 * Get default column values
73 *
74 * @since 1.0
75 * @access public
76 *
77 * @return array Default column values.
78 */
79 public function get_column_defaults() {
80 return array(
81 'user_id' => 0,
82 'email' => '',
83 'name' => '',
84 'payment_ids' => '',
85 'purchase_value' => 0.00,
86 'purchase_count' => 0,
87 'date_created' => date( 'Y-m-d H:i:s' ),
88 'token' => '',
89 'verify_key' => '',
90 'verify_throttle' => '',
91 );
92 }
93
94 /**
95 * Add a donor
96 *
97 * @param array $data List of donor data to add.
98 *
99 * @since 1.0
100 * @access public
101 *
102 * @return int|bool
103 */
104 public function add( $data = array() ) {
105
106 $defaults = array(
107 'payment_ids' => '',
108 );
109
110 $args = wp_parse_args( $data, $defaults );
111
112 if ( empty( $args['email'] ) ) {
113 return false;
114 }
115
116 if ( ! empty( $args['payment_ids'] ) && is_array( $args['payment_ids'] ) ) {
117 $args['payment_ids'] = implode( ',', array_unique( array_values( $args['payment_ids'] ) ) );
118 }
119
120 $donor = $this->get_donor_by( 'email', $args['email'] );
121
122 // update an existing donor.
123 if ( $donor ) {
124
125 // Update the payment IDs attached to the donor
126 if ( ! empty( $args['payment_ids'] ) ) {
127
128 if ( empty( $donor->payment_ids ) ) {
129
130 $donor->payment_ids = $args['payment_ids'];
131
132 } else {
133
134 $existing_ids = array_map( 'absint', explode( ',', $donor->payment_ids ) );
135 $payment_ids = array_map( 'absint', explode( ',', $args['payment_ids'] ) );
136 $payment_ids = array_merge( $payment_ids, $existing_ids );
137 $donor->payment_ids = implode( ',', array_unique( array_values( $payment_ids ) ) );
138
139 }
140
141 $args['payment_ids'] = $donor->payment_ids;
142
143 }
144
145 $this->update( $donor->id, $args );
146
147 return $donor->id;
148
149 } else {
150
151 return $this->insert( $args, 'donor' );
152
153 }
154
155 }
156
157
158 /**
159 * Update a donor.
160 *
161 *
162 * @param int $row_id
163 * @param array $data
164 * @param string $where
165 *
166 * @return bool
167 */
168 public function update( $row_id, $data = array(), $where = '' ) {
169
170 $status = parent::update( $row_id, $data, $where );
171
172 if ( $status ) {
173 Give_Cache::delete_group( $row_id, 'give-donors' );
174 }
175
176 return $status;
177 }
178
179 /**
180 * Insert a donor.
181 *
182 * @param array $data
183 * @param string $type
184 *
185 * @return int
186 */
187 public function insert( $data, $type = '' ) {
188 $donor_id = parent::insert( $data, $type );
189
190 if ( $donor_id ) {
191 Give_Cache::delete_group( $donor_id, 'give-donors' );
192 }
193
194 return $donor_id;
195 }
196
197 /**
198 * Delete a donor.
199 *
200 * NOTE: This should not be called directly as it does not make necessary changes to
201 * the payment meta and logs. Use give_donor_delete() instead.
202 *
203 * @param bool|string|int $_id_or_email ID or Email of Donor.
204 *
205 * @since 1.0
206 * @access public
207 *
208 * @return bool|int
209 */
210 public function delete( $_id_or_email = false ) {
211
212 if ( empty( $_id_or_email ) ) {
213 return false;
214 }
215
216 $column = is_email( $_id_or_email ) ? 'email' : 'id';
217 $donor = $this->get_donor_by( $column, $_id_or_email );
218
219 if ( $donor->id > 0 ) {
220
221 global $wpdb;
222
223 /**
224 * Deleting the donor meta.
225 *
226 * @since 1.8.14
227 */
228 Give()->donor_meta->delete_all_meta( $donor->id );
229
230 // Cache already deleted in delete_all_meta fn.
231
232 return $wpdb->delete( $this->table_name, array( 'id' => $donor->id ), array( '%d' ) );
233
234 } else {
235 return false;
236 }
237
238 }
239
240 /**
241 * Delete a donor by user ID.
242 *
243 * NOTE: This should not be called directly as it does not make necessary changes to
244 * the payment meta and logs. Use give_donor_delete() instead.
245 *
246 * @since 1.0
247 * @access public
248 *
249 * @param int|bool $user_id
250 *
251 * @return bool|int
252 */
253 public function delete_by_user_id( $user_id = false ) {
254 global $wpdb;
255
256 if ( empty( $user_id ) ) {
257 return false;
258 }
259
260 /**
261 * Deleting the donor meta.
262 *
263 * @since 1.8.14
264 */
265 $donor = new Give_Donor( $user_id, true );
266 if ( ! empty( $donor->id ) ) {
267 Give()->donor_meta->delete_all_meta( $donor->id );
268 }
269
270 // Cache is already deleted in delete_all_meta fn.
271
272 return $wpdb->delete( $this->table_name, array( 'user_id' => $user_id ), array( '%d' ) );
273 }
274
275 /**
276 * Checks if a donor exists
277 *
278 * @param string $value The value to search for. Default is empty.
279 * @param string $field The Donor ID or email to search in. Default is 'email'.
280 *
281 * @since 1.0
282 * @access public
283 *
284 * @return bool True is exists, false otherwise.
285 */
286 public function exists( $value = '', $field = 'email' ) {
287
288 $columns = $this->get_columns();
289 if ( ! array_key_exists( $field, $columns ) ) {
290 return false;
291 }
292
293 return (bool) $this->get_column_by( 'id', $field, $value );
294
295 }
296
297 /**
298 * Attaches a payment ID to a donor
299 *
300 * @since 1.0
301 * @access public
302 *
303 * @param int $donor_id Donor ID.
304 * @param int $payment_id Payment ID.
305 *
306 * @return bool
307 */
308 public function attach_payment( $donor_id = 0, $payment_id = 0 ) {
309
310 $donor = new Give_Donor( $donor_id );
311
312 if ( empty( $donor->id ) ) {
313 return false;
314 }
315
316 // Attach the payment, but don't increment stats, as this function previously did not
317 return $donor->attach_payment( $payment_id, false );
318
319 }
320
321 /**
322 * Removes a payment ID from a donor.
323 *
324 * @since 1.0
325 * @access public
326 *
327 * @param int $donor_id Donor ID.
328 * @param int $payment_id Payment ID.
329 *
330 * @return bool
331 */
332 public function remove_payment( $donor_id = 0, $payment_id = 0 ) {
333
334 $donor = new Give_Donor( $donor_id );
335
336 if ( ! $donor ) {
337 return false;
338 }
339
340 // Remove the payment, but don't decrease stats, as this function previously did not
341 return $donor->remove_payment( $payment_id, false );
342
343 }
344
345 /**
346 * Increments donor's donation stats.
347 *
348 * @access public
349 *
350 * @param int $donor_id Donor ID.
351 * @param float $amount THe amount to increase.
352 *
353 * @return bool
354 */
355 public function increment_stats( $donor_id = 0, $amount = 0.00 ) {
356
357 $donor = new Give_Donor( $donor_id );
358
359 if ( empty( $donor->id ) ) {
360 return false;
361 }
362
363 $increased_count = $donor->increase_purchase_count();
364 $increased_value = $donor->increase_value( $amount );
365
366 return ( $increased_count && $increased_value ) ? true : false;
367
368 }
369
370 /**
371 * Decrements donor's donation stats.
372 *
373 * @since 1.0
374 * @access public
375 *
376 * @param int $donor_id Donor ID.
377 * @param float $amount Amount.
378 *
379 * @return bool
380 */
381 public function decrement_stats( $donor_id = 0, $amount = 0.00 ) {
382
383 $donor = new Give_Donor( $donor_id );
384
385 if ( ! $donor ) {
386 return false;
387 }
388
389 $decreased_count = $donor->decrease_donation_count();
390 $decreased_value = $donor->decrease_value( $amount );
391
392 return ( $decreased_count && $decreased_value ) ? true : false;
393
394 }
395
396 /**
397 * Retrieves a single donor from the database
398 *
399 * @since 1.0
400 * @access public
401 *
402 * @param string $field ID or email. Default is 'id'.
403 * @param mixed $value The Customer ID or email to search. Default is 0.
404 *
405 * @return mixed Upon success, an object of the donor. Upon failure, NULL
406 */
407 public function get_donor_by( $field = 'id', $value = 0 ) {
408 $value = sanitize_text_field( $value );
409
410 // Bailout.
411 if ( empty( $field ) || empty( $value ) ) {
412 return null;
413 }
414
415 // Verify values.
416 if ( 'id' === $field || 'user_id' === $field ) {
417 // Make sure the value is numeric to avoid casting objects, for example,
418 // to int 1.
419 if ( ! is_numeric( $value ) ) {
420 return false;
421 }
422
423 $value = absint( $value );
424
425 if ( $value < 1 ) {
426 return false;
427 }
428
429 } elseif ( 'email' === $field ) {
430
431 if ( ! is_email( $value ) ) {
432 return false;
433 }
434
435 $value = trim( $value );
436 }
437
438 // Bailout
439 if ( ! $value ) {
440 return false;
441 }
442
443 // Set query params.
444 switch ( $field ) {
445 case 'id':
446 $args['donor'] = $value;
447 break;
448 case 'email':
449 $args['email'] = $value;
450 break;
451 case 'user_id':
452 $args['user'] = $value;
453 break;
454 default:
455 return false;
456 }
457
458 // Get donors.
459 $donor = new Give_Donors_Query( $args );
460
461 if ( ! $donor = $donor->get_donors() ) {
462 // Look for donor from an additional email.
463 $args = array(
464 'meta_query' => array(
465 array(
466 'key' => 'additional_email',
467 'value' => $value,
468 ),
469 ),
470 );
471
472 $donor = new Give_Donors_Query( $args );
473 $donor = $donor->get_donors();
474
475 if ( empty( $donor ) ) {
476 return false;
477 }
478 }
479
480 $donor = current( $donor );
481
482 isset( $donor->id ) && Give_Donors_Query::update_meta_cache( array( $donor->id ) );
483
484 return $donor;
485 }
486
487 /**
488 * This function will return donor details by token id.
489 *
490 * Note: This function is for internal purposes only. Don't use this function as it will be deprecated soon.
491 *
492 * @param int $id Email Access Token ID.
493 *
494 * @since 2.3.1
495 *
496 * @return object
497 */
498 public function get_donor_by_token( $id ) {
499 global $wpdb;
500 $row = $wpdb->get_row(
501 $wpdb->prepare( "SELECT * FROM {$wpdb->donors} WHERE verify_key = %s LIMIT 1", $id )
502 );
503 return $row;
504 }
505
506 /**
507 * Retrieve donors from the database.
508 *
509 * @since 1.0
510 * @access public
511 *
512 * @param array $args
513 *
514 * @return array|object|null Donors array or object. Null if not found.
515 */
516 public function get_donors( $args = array() ) {
517 $this->bc_1814_params( $args );
518
519 $donors = new Give_Donors_Query( $args );
520
521 return $donors->get_donors();
522
523 }
524
525
526 /**
527 * Count the total number of donors in the database
528 *
529 * @since 1.0
530 * @access public
531 *
532 * @param array $args
533 *
534 * @return int Total number of donors.
535 */
536 public function count( $args = array() ) {
537 $this->bc_1814_params( $args );
538 $args['count'] = true;
539
540 $cache_key = md5( 'give_donors_count' . serialize( $args ) );
541 $count = Give_Cache::get_group( $cache_key, 'donors' );
542
543 if ( is_null( $count ) ) {
544 $donors = new Give_Donors_Query( $args );
545 $count = $donors->get_donors();
546
547 Give_Cache::set_group( $cache_key, $count, 'donors', 3600 );
548 }
549
550 return absint( $count );
551
552 }
553
554 /**
555 * Create the table
556 *
557 * @since 1.0
558 * @access public
559 *
560 * @return void
561 */
562 public function create_table() {
563
564 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
565
566 $sql = "CREATE TABLE {$this->table_name} (
567 id bigint(20) NOT NULL AUTO_INCREMENT,
568 user_id bigint(20) NOT NULL,
569 email varchar(255) NOT NULL,
570 name mediumtext NOT NULL,
571 purchase_value mediumtext NOT NULL,
572 purchase_count bigint(20) NOT NULL,
573 payment_ids longtext NOT NULL,
574 date_created datetime NOT NULL,
575 token VARCHAR(255) CHARACTER SET utf8 NOT NULL,
576 verify_key VARCHAR(255) CHARACTER SET utf8 NOT NULL,
577 verify_throttle DATETIME NOT NULL,
578 PRIMARY KEY (id),
579 UNIQUE KEY email (email),
580 KEY user (user_id)
581 ) CHARACTER SET utf8 COLLATE utf8_general_ci;";
582
583 dbDelta( $sql );
584
585 update_option( $this->table_name . '_db_version', $this->version, false );
586 }
587
588 /**
589 * Add backward compatibility for old table name
590 *
591 * @since 2.0
592 * @access private
593 * @global wpdb $wpdb
594 */
595 private function bc_200_params() {
596 /* @var wpdb $wpdb */
597 global $wpdb;
598
599 if (
600 ! give_has_upgrade_completed( 'v20_rename_donor_tables' ) &&
601 $wpdb->query( $wpdb->prepare( "SHOW TABLES LIKE %s", "{$wpdb->prefix}give_customers" ) )
602 ) {
603 $wpdb->donors = $this->table_name = "{$wpdb->prefix}give_customers";
604 }
605 }
606
607 /**
608 * Add backward compatibility for deprecated param
609 *
610 * @since 1.8.14
611 * @access private
612 *
613 * @param $args
614 */
615 private function bc_1814_params( &$args ) {
616 // Backward compatibility: user_id
617 if ( ! empty( $args['user_id'] ) ) {
618 $args['user'] = $args['user_id'];
619 }
620
621 // Backward compatibility: id
622 if ( ! empty( $args['id'] ) ) {
623 $args['donor'] = $args['id'];
624 }
625
626 // Backward compatibility: name
627 if ( ! empty( $args['name'] ) ) {
628 $args['s'] = "name:{$args['name']}";
629 }
630
631 // Backward compatibility: date
632 // Donors created for a specific date or in a date range.
633 if ( ! empty( $args['date'] ) ) {
634
635 if ( is_array( $args['date'] ) ) {
636
637 if ( ! empty( $args['date']['start'] ) ) {
638 $args['date_query']['after'] = date( 'Y-m-d H:i:s', strtotime( $args['date']['start'] ) );
639 }
640
641 if ( ! empty( $args['date']['end'] ) ) {
642 $args['date_query']['before'] = date( 'Y-m-d H:i:s', strtotime( $args['date']['end'] ) );
643 }
644
645 } else {
646
647 $args['date_query']['year'] = date( 'Y', strtotime( $args['date'] ) );
648 $args['date_query']['month'] = date( 'm', strtotime( $args['date'] ) );
649 $args['date_query']['day'] = date( 'd', strtotime( $args['date'] ) );
650 }
651 }
652 }
653 }
654