PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.6.6
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.6.6
1.8.13.1 1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 All 212 releases
charitable / includes / db / class-charitable-campaign-donations-db.php

class-charitable-campaign-donations-db.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.6.6, at includes/db/class-charitable-campaign-donations-db.php

877 lines 23.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Charitable Campaign Donations DB class.
4 *
5 * @package Charitable/Classes/Charitable_Campaign_Donations_DB
6 * @author Eric Daams
7 * @copyright Copyright (c) 2018, Studio 164a
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.0.0
10 * @version 1.5.4
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) { exit; }
15
16 if ( ! class_exists( 'Charitable_Campaign_Donations_DB' ) ) :
17
18 /**
19 * Charitable_Campaign_Donations_DB
20 *
21 * @since 1.0.0
22 */
23 class Charitable_Campaign_Donations_DB extends Charitable_DB {
24
25 /**
26 * The version of our database table
27 *
28 * @since 1.0.0
29 *
30 * @var string
31 */
32 public $version = '1.5.4';
33
34 /**
35 * The name of the primary column
36 *
37 * @since 1.0.0
38 *
39 * @var string
40 */
41 public $primary_key = 'campaign_donation_id';
42
43 /**
44 * Stores whether the site is using commas for decimals in amounts.
45 *
46 * @since 1.3.0
47 *
48 * @var boolean
49 */
50 private $comma_decimal;
51
52 /**
53 * Set up the database table name.
54 *
55 * @since 1.0.0
56 *
57 * @global WPDB $wpdb
58 */
59 public function __construct() {
60 global $wpdb;
61
62 $this->table_name = $wpdb->prefix . 'charitable_campaign_donations';
63 }
64
65 /**
66 * Create the table.
67 *
68 * @since 1.0.0
69 *
70 * @global WPDB $wpdb
71 */
72 public function create_table() {
73 global $wpdb;
74
75 $charset_collate = $wpdb->get_charset_collate();
76
77 $sql = "CREATE TABLE {$this->table_name} (
78 campaign_donation_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
79 donation_id bigint(20) unsigned NOT NULL,
80 donor_id bigint(20) unsigned NOT NULL,
81 campaign_id bigint(20) unsigned NOT NULL,
82 campaign_name text NOT NULL,
83 amount decimal(13, 4) NOT NULL,
84 PRIMARY KEY (campaign_donation_id),
85 KEY donation (donation_id),
86 KEY campaign (campaign_id),
87 KEY donor (donor_id)
88 ) $charset_collate;";
89
90 $this->_create_table( $sql );
91 }
92
93 /**
94 * Whitelist of columns.
95 *
96 * @since 1.0.0
97 *
98 * @return array
99 */
100 public function get_columns() {
101 return array(
102 'campaign_donation_id' => '%d',
103 'donation_id' => '%d',
104 'donor_id' => '%d',
105 'campaign_id' => '%d',
106 'campaign_name' => '%s',
107 'amount' => '%f',
108 );
109 }
110
111 /**
112 * Default column values.
113 *
114 * @since 1.0.0
115 *
116 * @return array
117 */
118 public function get_column_defaults() {
119 return array(
120 'campaign_donation_id' => '',
121 'donation_id' => '',
122 'donor_id' => '',
123 'campaign_id' => '',
124 'campaign_name' => '',
125 'amount' => '',
126 );
127 }
128
129 /**
130 * Add a new campaign donation.
131 *
132 * @since 1.0.0
133 *
134 * @param array $data Data we are inserting.
135 * @param string $type Type of record we are inserting.
136 * @return int The ID of the inserted campaign donation
137 */
138 public function insert( $data, $type = 'campaign_donation' ) {
139 if ( ! isset( $data['campaign_name'] ) ) {
140 $data['campaign_name'] = get_the_title( $data['campaign_id'] );
141 }
142
143 Charitable_Campaign::flush_donations_cache( $data['campaign_id'] );
144
145 return parent::insert( $data, $type );
146 }
147
148 /**
149 * Update campaign donation record.
150 *
151 * @since 1.0.0
152 *
153 * @param int $row_id The primary key ID for the row we are retrieving.
154 * @param array $data The updated date.
155 * @param string $where Column used in where argument.
156 * @return boolean
157 */
158 public function update( $row_id, $data = array(), $where = '' ) {
159 $this->flush_campaign_caches( $row_id, $where );
160
161 return parent::update( $row_id, $data, $where );
162 }
163
164 /**
165 * Delete a row identified by the primary key.
166 *
167 * @since 1.0.0
168 *
169 * @param int $row_id The primary key ID.
170 * @return boolean
171 */
172 public function delete( $row_id = 0 ) {
173 $this->flush_campaign_caches( $row_id );
174
175 return parent::delete( $row_id );
176 }
177
178 /**
179 * Delete all campaign donation records for a given donation.
180 *
181 * @since 1.2.0
182 *
183 * @param int $donation_id The donation ID.
184 * @return boolean
185 */
186 public static function delete_donation_records( $donation_id ) {
187 $table = charitable_get_table( 'campaign_donations' );
188
189 foreach ( $table->get_campaigns_for_donation( $donation_id ) as $campaign_id ) {
190 Charitable_Campaign::flush_donations_cache( $campaign_id );
191 }
192
193 return $table->delete_by( 'donation_id', $donation_id );
194 }
195
196 /**
197 * Get the total amount donated, ever.
198 *
199 * @since 1.0.0
200 *
201 * @global $wpdb WPDB
202 * @param string[] $statuses List of statuses.
203 * @return float
204 */
205 public function get_total( $statuses = array() ) {
206 global $wpdb;
207
208 if ( empty( $statuses ) ) {
209 $statuses = charitable_get_approval_statuses();
210 }
211
212 list( $status_clause, $parameters ) = $this->get_donation_status_clause( $statuses );
213
214 $sql = "SELECT COALESCE( SUM(cd.amount), 0 )
215 FROM $this->table_name cd
216 INNER JOIN $wpdb->posts p
217 ON p.ID = cd.donation_id
218 WHERE 1 = 1
219 $status_clause";
220
221 $total = $wpdb->get_var( $wpdb->prepare( $sql, $parameters ) );
222
223 if ( $this->is_comma_decimal() ) {
224 $total = Charitable_Currency::get_instance()->sanitize_database_amount( $total );
225 }
226
227 return $total;
228 }
229
230 /**
231 * Return an object containing all campaign donations associated with a particular
232 * campaign ID or a particular donation ID.
233 *
234 * @since 1.4.0
235 *
236 * @global WPDB $wpdb
237 * @param string $field The field we are retrieving donations by. Either 'campaign' or 'donation'.
238 * @param int|int[] $donation_id A single donation ID or an array of IDs.
239 * @return object
240 */
241 public function get_campaign_donations_by( $field, $donation_id ) {
242 global $wpdb;
243
244 $column = $this->get_sanitized_column( $field );
245
246 list( $in, $parameters ) = $this->get_in_clause_params( $donation_id );
247
248 $sql = "SELECT *
249 FROM $this->table_name
250 WHERE $column IN ( $in );";
251
252 $records = $wpdb->get_results( $wpdb->prepare( $sql, $parameters ), OBJECT_K );
253
254 if ( $this->is_comma_decimal() ) {
255 $records = array_map( array( $this, 'sanitize_amounts' ), $records );
256 }
257
258 return $records;
259 }
260
261 /**
262 * Return a list of all distinct IDs based on a particular field.
263 *
264 * @since 1.4.0
265 *
266 * @global WPDB $wpdb
267 * @param string $field The distinct field we are retrieving.
268 * @param int|int[] $id An ID or an array of IDs.
269 * @param string $where_field Column used for the where argument.
270 * @return object
271 */
272 public function get_distinct_ids( $field, $id, $where_field = 'campaign_id' ) {
273 global $wpdb;
274
275 $select_column = $this->get_sanitized_column( $field );
276 $where_column = $this->get_sanitized_column( $where_field );
277
278 list( $in, $parameters ) = $this->get_in_clause_params( $id );
279
280 $sql = "SELECT DISTINCT $select_column
281 FROM $this->table_name
282 WHERE $where_column IN ( $in );";
283
284 return $wpdb->get_col( $wpdb->prepare( $sql, $parameters ) );
285 }
286
287 /**
288 * Get an object of all campaign donations associated with one or more donations.
289 *
290 * @uses Charitable_Campaign_Donations_DB::get_campaign_donations_by()
291 *
292 * @since 1.0.0
293 *
294 * @param int|int[] $donation_id A single donation ID or an array of IDs.
295 * @return object
296 */
297 public function get_donation_records( $donation_id ) {
298 return $this->get_campaign_donations_by( 'donation_id', $donation_id );
299 }
300
301 /**
302 * Get the amount donated in a single donation.
303 *
304 * @since 1.3.0
305 *
306 * @global WPDB $wpdb
307 * @param int|int[] $donation_id A single donation ID or an array of IDs.
308 * @param int|int[] $campaign Optional. If set, this will only return the total donated to that campaign, or array of campaigns.
309 * @return decimal
310 */
311 public function get_donation_amount( $donation_id, $campaign = '' ) {
312 global $wpdb;
313
314 list( $in, $parameters ) = $this->get_in_clause_params( $donation_id );
315
316 $where_clause = "donation_id IN ( $in )";
317
318 if ( ! empty( $campaign ) ) {
319
320 list( $campaigns_in, $campaigns_parameters ) = $this->get_in_clause_params( $campaign );
321
322 $where_clause .= " AND campaign_id IN ( $campaigns_in )";
323 $parameters = array_merge( $parameters, $campaigns_parameters );
324
325 }
326
327 $sql = "SELECT SUM(amount)
328 FROM $this->table_name
329 WHERE $where_clause;";
330
331 $total = $wpdb->get_var( $wpdb->prepare( $sql, $parameters ) );
332
333 if ( $this->is_comma_decimal() ) {
334 $total = Charitable_Currency::get_instance()->sanitize_database_amount( $total );
335 }
336
337 return $total;
338 }
339
340 /**
341 * Get the total amount donated in a single donation.
342 *
343 * @since 1.0.0
344 *
345 * @param int $donation_id
346 * @return decimal
347 */
348 public function get_donation_total_amount( $donation_id ) {
349 return $this->get_donation_amount( $donation_id );
350 }
351
352 /**
353 * Return an array of campaigns donated to in a single donation.
354 *
355 * @since 1.2.0
356 *
357 * @global wpdb $wpdb
358 * @return object
359 */
360 public function get_campaigns_for_donation( $donation_id ) {
361 global $wpdb;
362
363 $sql = "SELECT DISTINCT campaign_id
364 FROM $this->table_name
365 WHERE donation_id = %d;";
366
367 return $wpdb->get_col( $wpdb->prepare( $sql, intval( $donation_id ) ) );
368 }
369
370 /**
371 * Get an object of all donations on a campaign.
372 *
373 * @uses Charitable_Campaign_Donations_DB::get_campaign_donations_by()
374 *
375 * @since 1.0.0
376 *
377 * @param int|int[] $campaign_id
378 * @return object
379 */
380 public function get_donations_on_campaign( $campaign_id ) {
381 return $this->get_campaign_donations_by( 'campaign_id', $campaign_id );
382 }
383
384 /**
385 * Get total amount donated to a campaign.
386 *
387 * @since 1.0.0
388 *
389 * @global wpdb $wpdb
390 * @param int|int[] $campaigns A campaign ID. Optionally, you can pass an array of campaign IDs to get the total of all put together.
391 * @param boolean $include_all Whether donations with non-approved statuses should be included.
392 * @param boolean $sanitize Whether to sanitize the amount if we're using commas for decimals.
393 * @return string
394 */
395 public function get_campaign_donated_amount( $campaigns, $include_all = false, $sanitize = true ) {
396 global $wpdb;
397
398 $statuses = $include_all ? array() : charitable_get_approval_statuses();
399
400 list( $status_clause, $status_parameters ) = $this->get_donation_status_clause( $statuses );
401 list( $campaigns_in, $campaigns_parameters ) = $this->get_in_clause_params( $campaigns );
402
403 $parameters = array_merge( $campaigns_parameters, $status_parameters );
404
405 $sql = "SELECT COALESCE( SUM(amount), 0 )
406 FROM $this->table_name cd
407 INNER JOIN $wpdb->posts p
408 ON p.ID = cd.donation_id
409 WHERE cd.campaign_id IN ( $campaigns_in )
410 $status_clause;";
411
412 $total = $wpdb->get_var( $wpdb->prepare( $sql, $parameters ) );
413
414 if ( $this->is_comma_decimal() && $sanitize ) {
415 $total = Charitable_Currency::get_instance()->sanitize_database_amount( $total );
416 }
417
418 return $total;
419 }
420
421 /**
422 * Get an array of all donation ids for a campaign.
423 *
424 * @uses Charitable_Campaign_Donations_DB::get_distinct_ids()
425 *
426 * @since 1.0.0
427 *
428 * @param int $campaign_id Campaign ID.
429 * @return object
430 */
431 public function get_donation_ids_for_campaign( $campaign_id ) {
432 return $this->get_distinct_ids( 'donation_id', $campaign_id, 'campaign_id' );
433 }
434
435 /**
436 * The donor IDs of all who have donated to the given campaign.
437 *
438 * @uses Charitable_Campaign_Donations_DB::get_distinct_ids()
439 *
440 * @since 1.0.0
441 *
442 * @param int $campaign_id The campaign ID to get donors for.
443 * @return object
444 */
445 public function get_campaign_donors( $campaign_id ) {
446 return $this->get_distinct_ids( 'donor_id', $campaign_id, 'campaign_id' );
447 }
448
449 /**
450 * Return the number of users who have donated to the given campaign.
451 *
452 * @since 1.0.0
453 *
454 * @global wpdb $wpdb
455 * @param int|int[] $campaign The campaign ID, or list of campaign IDs.
456 * @param boolean $include_all Whether to include all donations (true), or only include approved donations (false).
457 * @return int
458 */
459 public function count_campaign_donors( $campaign, $include_all = false ) {
460 global $wpdb;
461
462 $statuses = $include_all ? array() : charitable_get_approval_statuses();
463
464 list( $status_clause, $status_parameters ) = $this->get_donation_status_clause( $statuses );
465 list( $campaigns_in, $campaigns_parameters ) = $this->get_in_clause_params( $campaign );
466
467 $parameters = array_merge( $campaigns_parameters, $status_parameters );
468
469 $sql = "SELECT COUNT( DISTINCT cd.donor_id )
470 FROM $this->table_name cd
471 INNER JOIN $wpdb->posts p ON p.ID = cd.donation_id
472 WHERE cd.campaign_id IN ( $campaigns_in )
473 $status_clause;";
474
475 return $wpdb->get_var( $wpdb->prepare( $sql, $parameters ) );
476 }
477
478 /**
479 * Return all donations made by a donor.
480 *
481 * @since 1.0.0
482 *
483 * @global wpdb $wpdb
484 * @param int $donor_id The donor ID.
485 * @param boolean $distinct_donations Whether to get distinct donations.
486 * @return object[]
487 */
488 public function get_donations_by_donor( $donor_id, $distinct_donations = false ) {
489 global $wpdb;
490
491 if ( $distinct_donations ) {
492 $select_fields = 'DISTINCT( cd.donation_id ), cd.campaign_id, cd.campaign_name, cd.amount';
493 } else {
494 $select_fields = 'cd.campaign_donation_id, cd.donation_id, cd.campaign_id, cd.campaign_name, cd.amount';
495 }
496
497 $sql = "SELECT $select_fields
498 FROM $this->table_name cd
499 WHERE cd.donor_id = %d;";
500
501 $results = $wpdb->get_results( $wpdb->prepare( $sql, $donor_id ), OBJECT_K );
502
503 if ( $this->is_comma_decimal() ) {
504 $results = array_map( array( $this, 'sanitize_amounts' ), $results );
505 }
506
507 return $results;
508 }
509
510 /**
511 * Return total amount donated by a donor.
512 *
513 * @since 1.0.0
514 *
515 * @global WPDB $wpdb
516 * @param int $donor_id The donor ID.
517 * @return int
518 */
519 public function get_total_donated_by_donor( $donor_id ) {
520 global $wpdb;
521
522 $sql = "SELECT COALESCE( SUM(cd.amount), 0 )
523 FROM $this->table_name cd
524 WHERE cd.donor_id = %d;";
525
526 $total = $wpdb->get_var( $wpdb->prepare( $sql, $donor_id ) );
527
528 if ( $this->is_comma_decimal() ) {
529 $total = Charitable_Currency::get_instance()->sanitize_database_amount( $total );
530 }
531
532 return $total;
533 }
534
535 /**
536 * Count the number of donations made by the donor.
537 *
538 * @since 1.0.0
539 *
540 * @global wpdb $wpdb
541 * @param int $donor_id The donor ID.
542 * @param boolean $distinct_donations If true, will only count unique donations.
543 * @return int
544 */
545 public function count_donations_by_donor( $donor_id, $distinct_donations = false ) {
546 global $wpdb;
547
548 $count = $distinct_donations ? 'DISTINCT donation_id' : 'donation_id';
549
550 $sql = "SELECT COUNT( $count )
551 FROM $this->table_name
552 WHERE donor_id = %d;";
553
554 return $wpdb->get_var( $wpdb->prepare( $sql, $donor_id ) );
555 }
556
557 /**
558 * Count the number of campaigns that the donor has supported.
559 *
560 * @since 1.0.0
561 *
562 * @global wpdb $wpdb
563 * @param int $donor_id The donor to retrieve campaigns for.
564 * @return int
565 */
566 public function count_campaigns_supported_by_donor( $donor_id ) {
567 global $wpdb;
568
569 $sql = "SELECT COUNT( DISTINCT campaign_id )
570 FROM $this->table_name
571 WHERE donor_id = %d;";
572
573 return $wpdb->get_var( $wpdb->prepare( $sql, $donor_id ) );
574 }
575
576 /**
577 * Return a set of donations, filtered by the provided arguments.
578 *
579 * @since 1.0.0
580 *
581 * @param array $args Query arguments.
582 * @return array
583 */
584 public function get_donations_report( $args ) {
585 global $wpdb;
586
587 $parameters = array();
588 $sql_order = $this->get_orderby_clause( $args, 'ORDER BY p.post_date ASC' );
589 $sql_where = '';
590 $sql_where_clauses = array();
591
592 if ( isset( $args['campaign_id'] ) ) {
593
594 list( $campaigns_in, $campaigns_parameters ) = $this->get_in_clause_params( $args['campaign_id'] );
595
596 $sql_where_clauses[] = "cd.campaign_id IN ( $campaigns_in )";
597 $parameters = array_merge( $parameters, $campaigns_parameters );
598
599 }
600
601 if ( isset( $args['status'] ) ) {
602
603 $sql_where_clauses[] = 'p.post_status = %s';
604 $parameters[] = $args['status'];
605
606 } else {
607 // if ALL: select all valid statuses
608 $statuses = array_keys( charitable_get_valid_donation_statuses() );
609 $in = charitable_get_query_placeholders( count( $statuses ), '%s' );
610 $sql_where_clauses[] = "p.post_status IN ( $in )";
611 $parameters = array_merge( $parameters, $statuses );
612 }
613
614 if ( isset( $args['start_date'] ) ) {
615
616 $sql_where_clauses[] = 'p.post_date >= %s';
617 $parameters[] = $args['start_date'];
618
619 }
620
621 if ( isset( $args['end_date'] ) ) {
622 $sql_where_clauses[] = 'p.post_date <= %s';
623 $parameters[] = $args['end_date'];
624 }
625
626 if ( ! empty( $sql_where_clauses ) ) {
627 $sql_where = 'WHERE ' . implode( ' AND ', $sql_where_clauses );
628 }
629
630 /* This is our base SQL query */
631 $sql = "SELECT cd.donation_id, cd.campaign_id, cd.campaign_name, cd.amount, d.email, d.first_name, d.last_name, p.post_date, p.post_content, p.post_status
632 FROM $this->table_name cd
633 INNER JOIN {$wpdb->prefix}charitable_donors d
634 ON d.donor_id = cd.donor_id
635 INNER JOIN $wpdb->posts p
636 ON p.ID = cd.donation_id
637 $sql_where
638 $sql_order";
639
640 if ( ! empty( $parameters ) ) {
641 $sql = $wpdb->prepare( $sql, $parameters );
642 }
643
644 $results = $wpdb->get_results( $sql );
645
646 if ( $this->is_comma_decimal() ) {
647 $results = array_map( array( $this, 'sanitize_amounts' ), $results );
648 }
649
650 return $results;
651 }
652
653 /**
654 * Return a count and sum of donations for a given period.
655 *
656 * @since 1.2.0
657 *
658 * @global WPDB $wpdb
659 * @param string $period The period to get donations for.
660 * @param string[] $statuses List of statuses.
661 * @return array
662 */
663 public function get_donations_summary_by_period( $period = '', $statuses = array() ) {
664 global $wpdb;
665
666 if ( empty( $statuses ) ) {
667 $statuses = charitable_get_approval_statuses();
668 }
669
670 list( $status_clause, $parameters ) = $this->get_donation_status_clause( $statuses );
671
672 array_unshift( $parameters, $period );
673
674 $sql = "SELECT COALESCE( SUM( cd.amount ), 0 ) as amount, COUNT( cd.donation_id ) as count
675 FROM {$wpdb->prefix}charitable_campaign_donations cd
676 INNER JOIN $wpdb->posts p ON p.ID = cd.donation_id
677 WHERE p.post_date LIKE %s
678 $status_clause;";
679
680 $results = $wpdb->get_results( $wpdb->prepare( $sql, $parameters ) );
681
682 $result = $results[0];
683
684 if ( $this->is_comma_decimal() ) {
685 $result = $this->sanitize_amounts( $result );
686 }
687
688 return $result;
689 }
690
691 /**
692 * Returns the orderby clause.
693 *
694 * @since 1.3.4
695 *
696 * @param array $args Query arguments.
697 * @param string $default Default.
698 * @return string
699 */
700 public function get_orderby_clause( $args, $default = '' ) {
701 if ( ! isset( $args['orderby'] ) && ! isset( $args['order'] ) ) {
702 return $default;
703 }
704
705 $orderby = isset( $args['orderby'] ) ? $args['orderby'] : 'ID';
706 $order = isset( $args['order'] ) && in_array( $args['order'], array( 'ASC', 'DESC' ) ) ? $args['order'] : 'ASC';
707
708 switch ( $orderby ) {
709 case 'date' :
710 $ret = 'ORDER BY p.post_date ';
711 break;
712
713 default :
714 $ret = 'ORDER BY cd.campaign_donation_id ';
715 }
716
717 $ret .= $order;
718
719 return $ret;
720 }
721
722 /**
723 * Count donations by status.
724 *
725 * @since 1.0.0
726 *
727 * @param string|string[] $statuses List of statuses.
728 * @return int
729 */
730 public function count_donations_by_status( $statuses ) {
731 global $wpdb;
732
733 if ( ! is_array( $statuses ) ) {
734 $statuses = array( $statuses );
735 }
736
737 list( $status_clause, $parameters ) = $this->get_donation_status_clause( $statuses );
738
739 $sql = "SELECT COUNT( * )
740 FROM {$wpdb->prefix}charitable_campaign_donations cd
741 INNER JOIN $wpdb->posts p ON p.ID = cd.donation_id
742 WHERE 1 = 1
743 $status_clause;";
744
745 return $wpdb->get_var( $wpdb->prepare( $sql, $parameters ) );
746 }
747
748 /**
749 * Returns the donation status clause.
750 *
751 * @since 1.0.0
752 *
753 * @param string[] $statuses List of statuses.
754 * @return string
755 */
756 private function get_donation_status_clause( $statuses = array() ) {
757 if ( empty( $statuses ) ) {
758 return array( '', array() );
759 }
760
761 $statuses = array_filter( $statuses, 'charitable_is_valid_donation_status' );
762 $in = charitable_get_query_placeholders( count( $statuses ), '%s' );
763 $sql = "AND p.post_status IN ( $in )";
764
765 return array( $sql, $statuses );
766 }
767
768 /**
769 * Returns an array containing the placeholders and sanitized parameters for an IN clause.
770 *
771 * @since 1.4.0
772 *
773 * @param int|int[] $list List of elements.
774 * @return array
775 */
776 private function get_in_clause_params( $list ) {
777 if ( ! is_array( $list ) ) {
778 $list = array( $list );
779 }
780
781 /* Filter out any non-numeric campaign IDs, then convert to int */
782 $list = array_filter( $list, 'is_numeric' );
783 $list = array_map( 'intval', $list );
784
785 return array( charitable_get_query_placeholders( count( $list ), '%d' ), $list );
786 }
787
788 /**
789 * Checks whether we are using commas for decimals.
790 *
791 * @since 1.3.0
792 *
793 * @return boolean
794 */
795 private function is_comma_decimal() {
796 if ( ! isset( $this->comma_decimal ) ) {
797 $this->comma_decimal = Charitable_Currency::get_instance()->is_comma_decimal();
798 }
799
800 return $this->comma_decimal;
801 }
802
803 /**
804 * Sanitize amounts retrieved from the database.
805 *
806 * @since 1.3.0
807 *
808 * @param object $campaign_donation Campaign donation record.
809 * @return object
810 */
811 private function sanitize_amounts( $campaign_donation ) {
812 $campaign_donation->amount = Charitable_Currency::get_instance()->sanitize_database_amount( $campaign_donation->amount );
813 return $campaign_donation;
814 }
815
816 /**
817 * Return a sanitized column name.
818 *
819 * @since 1.4.0
820 *
821 * @param string $field Return the column name.
822 * @return string
823 */
824 private function get_sanitized_column( $field ) {
825 switch ( $field ) {
826 case 'campaign':
827 case 'campaign_id':
828 $column = 'campaign_id';
829 break;
830
831 case 'donation':
832 case 'donation_id':
833 $column = 'donation_id';
834 break;
835
836 case 'donor':
837 case 'donor_id':
838 $column = 'donor_id';
839 break;
840
841 default:
842 charitable_get_deprecated()->doing_it_wrong(
843 __METHOD__,
844 __( 'Field expected to be `campaign`, `campaign_id`, `donation`, `donation_id`, `donor` or `donor_id`.', 'charitable' ),
845 '1.4.0'
846 );
847
848 $column = false;
849 }
850
851 return $column;
852 }
853
854 /**
855 * Flush campaign caches when updating one or more rows.
856 *
857 * @since 1.5.11
858 *
859 * @param int $row_id The primary key ID for the row we are retrieving.
860 * @param string $where Column used in where argument.
861 * @return void
862 */
863 private function flush_campaign_caches( $row_id, $where = '' ) {
864 if ( empty( $where ) ) {
865 $where = $this->primary_key;
866 }
867
868 $campaign_ids = array_unique( $this->get_column_all_by( 'campaign_id', $where, $row_id ) );
869
870 foreach ( $campaign_ids as $campaign_id ) {
871 Charitable_Campaign::flush_donations_cache( $campaign_id );
872 }
873 }
874 }
875
876 endif;
877