PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.24.0
GiveWP – Donation Plugin and Fundraising Platform v2.24.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-meta.php 5 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
651 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 * @param int $row_id
162 * @param array $data
163 * @param string $where
164 *
165 * @return bool
166 */
167 public function update( $row_id, $data = array(), $where = '' ) {
168
169 $status = parent::update( $row_id, $data, $where );
170
171 if ( $status ) {
172 Give_Cache::delete_group( $row_id, 'give-donors' );
173 }
174
175 return $status;
176 }
177
178 /**
179 * Insert a donor.
180 *
181 * @param array $data
182 * @param string $type
183 *
184 * @return int
185 */
186 public function insert( $data, $type = '' ) {
187 $donor_id = parent::insert( $data, $type );
188
189 if ( $donor_id ) {
190 Give_Cache::delete_group( $donor_id, 'give-donors' );
191 }
192
193 return $donor_id;
194 }
195
196 /**
197 * Delete a donor.
198 *
199 * NOTE: This should not be called directly as it does not make necessary changes to
200 * the payment meta and logs. Use give_donor_delete() instead.
201 *
202 * @param bool|string|int $_id_or_email ID or Email of Donor.
203 *
204 * @since 1.0
205 * @access public
206 *
207 * @return bool|int
208 */
209 public function delete( $_id_or_email = false ) {
210
211 if ( empty( $_id_or_email ) ) {
212 return false;
213 }
214
215 $column = is_email( $_id_or_email ) ? 'email' : 'id';
216 $donor = $this->get_donor_by( $column, $_id_or_email );
217
218 if ( $donor->id > 0 ) {
219
220 global $wpdb;
221
222 /**
223 * Deleting the donor meta.
224 *
225 * @since 1.8.14
226 */
227 Give()->donor_meta->delete_all_meta( $donor->id );
228
229 // Cache already deleted in delete_all_meta fn.
230
231 return $wpdb->delete( $this->table_name, array( 'id' => $donor->id ), array( '%d' ) );
232
233 } else {
234 return false;
235 }
236
237 }
238
239 /**
240 * Delete a donor by user ID.
241 *
242 * NOTE: This should not be called directly as it does not make necessary changes to
243 * the payment meta and logs. Use give_donor_delete() instead.
244 *
245 * @since 1.0
246 * @access public
247 *
248 * @param int|bool $user_id
249 *
250 * @return bool|int
251 */
252 public function delete_by_user_id( $user_id = false ) {
253 global $wpdb;
254
255 if ( empty( $user_id ) ) {
256 return false;
257 }
258
259 /**
260 * Deleting the donor meta.
261 *
262 * @since 1.8.14
263 */
264 $donor = new Give_Donor( $user_id, true );
265 if ( ! empty( $donor->id ) ) {
266 Give()->donor_meta->delete_all_meta( $donor->id );
267 }
268
269 // Cache is already deleted in delete_all_meta fn.
270
271 return $wpdb->delete( $this->table_name, array( 'user_id' => $user_id ), array( '%d' ) );
272 }
273
274 /**
275 * Checks if a donor exists
276 *
277 * @param string $value The value to search for. Default is empty.
278 * @param string $field The Donor ID or email to search in. Default is 'email'.
279 *
280 * @since 1.0
281 * @access public
282 *
283 * @return bool True is exists, false otherwise.
284 */
285 public function exists( $value = '', $field = 'email' ) {
286
287 $columns = $this->get_columns();
288 if ( ! array_key_exists( $field, $columns ) ) {
289 return false;
290 }
291
292 return (bool) $this->get_column_by( 'id', $field, $value );
293
294 }
295
296 /**
297 * Attaches a payment ID to a donor
298 *
299 * @since 1.0
300 * @access public
301 *
302 * @param int $donor_id Donor ID.
303 * @param int $payment_id Payment ID.
304 *
305 * @return bool
306 */
307 public function attach_payment( $donor_id = 0, $payment_id = 0 ) {
308
309 $donor = new Give_Donor( $donor_id );
310
311 if ( empty( $donor->id ) ) {
312 return false;
313 }
314
315 // Attach the payment, but don't increment stats, as this function previously did not
316 return $donor->attach_payment( $payment_id, false );
317
318 }
319
320 /**
321 * Removes a payment ID from a donor.
322 *
323 * @since 1.0
324 * @access public
325 *
326 * @param int $donor_id Donor ID.
327 * @param int $payment_id Payment ID.
328 *
329 * @return bool
330 */
331 public function remove_payment( $donor_id = 0, $payment_id = 0 ) {
332
333 $donor = new Give_Donor( $donor_id );
334
335 if ( ! $donor ) {
336 return false;
337 }
338
339 // Remove the payment, but don't decrease stats, as this function previously did not
340 return $donor->remove_payment( $payment_id, false );
341
342 }
343
344 /**
345 * Increments donor's donation stats.
346 *
347 * @access public
348 *
349 * @param int $donor_id Donor ID.
350 * @param float $amount THe amount to increase.
351 *
352 * @return bool
353 */
354 public function increment_stats( $donor_id = 0, $amount = 0.00 ) {
355
356 $donor = new Give_Donor( $donor_id );
357
358 if ( empty( $donor->id ) ) {
359 return false;
360 }
361
362 $increased_count = $donor->increase_purchase_count();
363 $increased_value = $donor->increase_value( $amount );
364
365 return ( $increased_count && $increased_value ) ? true : false;
366
367 }
368
369 /**
370 * Decrements donor's donation stats.
371 *
372 * @since 1.0
373 * @access public
374 *
375 * @param int $donor_id Donor ID.
376 * @param float $amount Amount.
377 *
378 * @return bool
379 */
380 public function decrement_stats( $donor_id = 0, $amount = 0.00 ) {
381
382 $donor = new Give_Donor( $donor_id );
383
384 if ( ! $donor ) {
385 return false;
386 }
387
388 $decreased_count = $donor->decrease_donation_count();
389 $decreased_value = $donor->decrease_value( $amount );
390
391 return ( $decreased_count && $decreased_value ) ? true : false;
392
393 }
394
395 /**
396 * Retrieves a single donor from the database
397 *
398 * @since 1.0
399 * @access public
400 *
401 * @param string $field ID or email. Default is 'id'.
402 * @param mixed $value The Customer ID or email to search. Default is 0.
403 *
404 * @return mixed Upon success, an object of the donor. Upon failure, NULL
405 */
406 public function get_donor_by( $field = 'id', $value = 0 ) {
407 $value = sanitize_text_field( $value );
408
409 // Bailout.
410 if ( empty( $field ) || empty( $value ) ) {
411 return null;
412 }
413
414 // Verify values.
415 if ( 'id' === $field || 'user_id' === $field ) {
416 // Make sure the value is numeric to avoid casting objects, for example,
417 // to int 1.
418 if ( ! is_numeric( $value ) ) {
419 return false;
420 }
421
422 $value = absint( $value );
423
424 if ( $value < 1 ) {
425 return false;
426 }
427 } elseif ( 'email' === $field ) {
428
429 if ( ! is_email( $value ) ) {
430 return false;
431 }
432
433 $value = trim( $value );
434 }
435
436 // Bailout
437 if ( ! $value ) {
438 return false;
439 }
440
441 // Set query params.
442 switch ( $field ) {
443 case 'id':
444 $args['donor'] = $value;
445 break;
446 case 'email':
447 $args['email'] = $value;
448 break;
449 case 'user_id':
450 $args['user'] = $value;
451 break;
452 default:
453 return false;
454 }
455
456 // Get donors.
457 $donor = new Give_Donors_Query( $args );
458
459 if ( ! $donor = $donor->get_donors() ) {
460 // Look for donor from an additional email.
461 $args = array(
462 'meta_query' => array(
463 array(
464 'key' => 'additional_email',
465 'value' => $value,
466 ),
467 ),
468 );
469
470 $donor = new Give_Donors_Query( $args );
471 $donor = $donor->get_donors();
472
473 if ( empty( $donor ) ) {
474 return false;
475 }
476 }
477
478 $donor = current( $donor );
479
480 isset( $donor->id ) && Give_Donors_Query::update_meta_cache( array( $donor->id ) );
481
482 return $donor;
483 }
484
485 /**
486 * This function will return donor details by token id.
487 *
488 * Note: This function is for internal purposes only. Don't use this function as it will be deprecated soon.
489 *
490 * @param int $id Email Access Token ID.
491 *
492 * @since 2.3.1
493 *
494 * @return object
495 */
496 public function get_donor_by_token( $id ) {
497 global $wpdb;
498 $row = $wpdb->get_row(
499 $wpdb->prepare( "SELECT * FROM {$wpdb->donors} WHERE verify_key = %s LIMIT 1", $id )
500 );
501 return $row;
502 }
503
504 /**
505 * Retrieve donors from the database.
506 *
507 * @since 1.0
508 * @access public
509 *
510 * @param array $args
511 *
512 * @return array|object|null Donors array or object. Null if not found.
513 */
514 public function get_donors( $args = array() ) {
515 $this->bc_1814_params( $args );
516
517 $donors = new Give_Donors_Query( $args );
518
519 return $donors->get_donors();
520
521 }
522
523
524 /**
525 * Count the total number of donors in the database
526 *
527 * @since 1.0
528 * @access public
529 *
530 * @param array $args
531 *
532 * @return int Total number of donors.
533 */
534 public function count( $args = array() ) {
535 $this->bc_1814_params( $args );
536 $args['count'] = true;
537
538 $cache_key = md5( 'give_donors_count' . serialize( $args ) );
539 $count = Give_Cache::get_group( $cache_key, 'donors' );
540
541 if ( is_null( $count ) ) {
542 $donors = new Give_Donors_Query( $args );
543 $count = $donors->get_donors();
544
545 Give_Cache::set_group( $cache_key, $count, 'donors', 3600 );
546 }
547
548 return absint( $count );
549
550 }
551
552 /**
553 * Create the table
554 *
555 * @since 1.0
556 * @access public
557 *
558 * @return void
559 */
560 public function create_table() {
561
562 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
563
564 $sql = "CREATE TABLE {$this->table_name} (
565 id bigint(20) NOT NULL AUTO_INCREMENT,
566 user_id bigint(20) NOT NULL,
567 email varchar(255) NOT NULL,
568 name mediumtext NOT NULL,
569 purchase_value mediumtext NOT NULL,
570 purchase_count bigint(20) NOT NULL,
571 payment_ids longtext NOT NULL,
572 date_created datetime NOT NULL,
573 token VARCHAR(255) CHARACTER SET utf8 NOT NULL,
574 verify_key VARCHAR(255) CHARACTER SET utf8 NOT NULL,
575 verify_throttle DATETIME NOT NULL,
576 PRIMARY KEY (id),
577 UNIQUE KEY email (email),
578 KEY user (user_id)
579 ) CHARACTER SET utf8 COLLATE utf8_general_ci;";
580
581 dbDelta( $sql );
582
583 update_option( $this->table_name . '_db_version', $this->version, false );
584 }
585
586 /**
587 * Add backward compatibility for old table name
588 *
589 * @since 2.0
590 * @access private
591 * @global wpdb $wpdb
592 */
593 private function bc_200_params() {
594 /* @var wpdb $wpdb */
595 global $wpdb;
596
597 if (
598 ! give_has_upgrade_completed( 'v20_rename_donor_tables' ) &&
599 $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', "{$wpdb->prefix}give_customers" ) )
600 ) {
601 $wpdb->donors = $this->table_name = "{$wpdb->prefix}give_customers";
602 }
603 }
604
605 /**
606 * Add backward compatibility for deprecated param
607 *
608 * @since 1.8.14
609 * @access private
610 *
611 * @param $args
612 */
613 private function bc_1814_params( &$args ) {
614 // Backward compatibility: user_id
615 if ( ! empty( $args['user_id'] ) ) {
616 $args['user'] = $args['user_id'];
617 }
618
619 // Backward compatibility: id
620 if ( ! empty( $args['id'] ) ) {
621 $args['donor'] = $args['id'];
622 }
623
624 // Backward compatibility: name
625 if ( ! empty( $args['name'] ) ) {
626 $args['s'] = "name:{$args['name']}";
627 }
628
629 // Backward compatibility: date
630 // Donors created for a specific date or in a date range.
631 if ( ! empty( $args['date'] ) ) {
632
633 if ( is_array( $args['date'] ) ) {
634
635 if ( ! empty( $args['date']['start'] ) ) {
636 $args['date_query']['after'] = date( 'Y-m-d H:i:s', strtotime( $args['date']['start'] ) );
637 }
638
639 if ( ! empty( $args['date']['end'] ) ) {
640 $args['date_query']['before'] = date( 'Y-m-d H:i:s', strtotime( $args['date']['end'] ) );
641 }
642 } else {
643
644 $args['date_query']['year'] = date( 'Y', strtotime( $args['date'] ) );
645 $args['date_query']['month'] = date( 'm', strtotime( $args['date'] ) );
646 $args['date_query']['day'] = date( 'd', strtotime( $args['date'] ) );
647 }
648 }
649 }
650 }
651