PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.12
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.12
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 / queries / class-charitable-donations-query.php

class-charitable-donations-query.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.7.0.12, at includes/queries/class-charitable-donations-query.php

228 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * A helper class to retrieve Donations.
4 *
5 * @package Charitable/Classes/Charitable_Donations_Query
6 * @author David Bisset
7 * @copyright Copyright (c) 2022, WP Charitable LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.4.0
10 * @version 1.6.54
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 if ( ! class_exists( 'Charitable_Donations_Query' ) ) :
19
20 /**
21 * Charitable_Donations_Query
22 *
23 * @since 1.4.0
24 */
25 class Charitable_Donations_Query extends Charitable_Query {
26
27 /**
28 * Create class object.
29 *
30 * @since 1.4.0
31 * @since 1.7.0.9
32 *
33 * @param array $args Arguments used in query.
34 */
35 public function __construct( $args = array() ) {
36 $defaults = array(
37 // Use 'posts' to get standard post objects.
38 'output' => 'donations',
39 // Set to an array with statuses to only show certain statuses.
40 'status' => false,
41 // Currently only supports 'date'.
42 'orderby' => 'date',
43 // May be 'DESC' or 'ASC'.
44 'order' => 'DESC',
45 // Number of donations to retrieve.
46 'number' => 20,
47 // For paged results.
48 'paged' => 1,
49 // Only get donations for a specific campaign.
50 'campaign' => 0,
51 // Only get donations by a specific donor.
52 'donor_id' => 0,
53 // Only get donations by a specific donor email.
54 'donor_email' => false,
55 // Only get donations by a specific user.
56 'user_id' => 0,
57 // Donation plan is essentially post_parent.
58 'donation_plan' => 0,
59 // Filter donations by date.
60 'date_query' => array(),
61 // Filter donations by meta.
62 'meta_query' => array(),
63 // Return only the IDs passed.
64 'donation_ids' => array(),
65 );
66
67 $this->args = wp_parse_args( $args, $defaults );
68
69 $this->position = 0;
70 $this->prepare_query();
71 $this->results = $this->get_donations();
72 }
73
74 /**
75 * Return list of donation IDs together with the number of donations they have made.
76 *
77 * @since 1.4.0
78 *
79 * @return object[]
80 */
81 public function get_donations() {
82 $records = $this->query();
83
84 /* Return the raw records. */
85 if ( in_array( $this->get( 'output' ), array( 'count', 'ids' ) ) ) {
86 return $records;
87 }
88
89 /* Return Donations objects. */
90 if ( 'donations' == $this->get( 'output' ) ) {
91 return array_map( 'charitable_get_donation', wp_list_pluck( $records, 'ID' ) );
92 }
93
94 $currency_helper = charitable_get_currency_helper();
95
96 /**
97 * When the currency uses commas for decimals and periods for thousands,
98 * the amount returned from the database needs to be sanitized.
99 */
100 if ( $currency_helper->is_comma_decimal() ) {
101 foreach ( $records as $i => $row ) {
102 $records[ $i ]->amount = $currency_helper->sanitize_database_amount( $row->amount );
103 }
104 }
105
106 return $records;
107 }
108
109 /**
110 * Run the IDs query, returning the results.
111 *
112 * @global WPDB $wpdb
113 *
114 * @since 1.6.54
115 *
116 * @return array
117 */
118 public function run_query_ids() {
119 global $wpdb;
120
121 $sql = "SELECT {$this->fields()} {$this->from()} {$this->join()} {$this->where()} {$this->groupby()} {$this->orderby()} {$this->order()} {$this->limit()} {$this->offset()};";
122
123 return $wpdb->get_col( $this->get_prepared_sql( $sql ) );
124 }
125
126 /**
127 * Set up fields query argument.
128 *
129 * @since 1.4.0
130 *
131 * @return void
132 */
133 public function setup_fields() {
134 /* If we are returning IDs or Donation objects, we only need to return the donation IDs. */
135 if ( in_array( $this->get( 'output' ), array( 'donations', 'ids' ) ) ) {
136 return;
137 }
138
139 add_filter( 'charitable_query_fields', array( $this, 'donation_fields' ), 4 );
140 add_filter( 'charitable_query_fields', array( $this, 'donation_calc_fields' ), 5 );
141 }
142
143 /**
144 * Set up orderby query argument.
145 *
146 * @since 1.4.0
147 *
148 * @return void
149 */
150 public function setup_orderby() {
151 $orderby = $this->get( 'orderby', false );
152
153 if ( ! $orderby ) {
154 return;
155 }
156
157 switch ( $orderby ) {
158 case 'date':
159 add_filter( 'charitable_query_orderby', array( $this, 'orderby_date' ) );
160 break;
161
162 case 'amount':
163 add_filter( 'charitable_query_orderby', array( $this, 'orderby_donation_amount' ) );
164 break;
165 }
166 }
167
168 /**
169 * Remove any hooks that have been attached by the class to prevent contaminating other queries.
170 *
171 * @since 1.4.0
172 * @version 1.7.0.9
173 *
174 * @return void
175 */
176 public function unhook_callbacks() {
177 remove_action( 'charitable_pre_query', array( $this, 'setup_fields' ) );
178 remove_filter( 'charitable_query_fields', array( $this, 'donation_fields' ), 4 );
179 remove_filter( 'charitable_query_fields', array( $this, 'donation_calc_fields' ), 5 );
180 remove_filter( 'charitable_query_join', array( $this, 'join_campaign_donations_table_on_donation' ), 5 );
181 remove_filter( 'charitable_query_join', array( $this, 'join_campaign_donors_table_on_donation' ), 6 );
182 remove_filter( 'charitable_query_join', array( $this, 'join_meta' ), 7 );
183 remove_filter( 'charitable_query_where', array( $this, 'where_donation_plan_is_in' ), 8 );
184 remove_filter( 'charitable_query_where', array( $this, 'where_status_is_in' ), 9 );
185 remove_filter( 'charitable_query_where', array( $this, 'where_campaign_is_in' ), 10 );
186 remove_filter( 'charitable_query_where', array( $this, 'where_donor_id_is_in' ), 11 );
187 remove_filter( 'charitable_query_where', array( $this, 'where_donor_email_is_in' ), 12 );
188 remove_filter( 'charitable_query_where', array( $this, 'where_user_id_is_in' ), 13 );
189 remove_filter( 'charitable_query_where', array( $this, 'where_donation_id_is_in' ), 14 );
190 remove_filter( 'charitable_query_where', array( $this, 'where_date' ), 15 );
191 remove_filter( 'charitable_query_where', array( $this, 'where_meta' ), 16 );
192 remove_action( 'charitable_pre_query', array( $this, 'setup_orderby' ) );
193 remove_filter( 'charitable_query_orderby', array( $this, 'orderby_date' ) );
194 remove_filter( 'charitable_query_orderby', array( $this, 'orderby_donation_amount' ) );
195 remove_filter( 'charitable_query_groupby', array( $this, 'groupby_donation_id' ) );
196 remove_action( 'charitable_post_query', array( $this, 'unhook_callbacks' ) );
197 }
198
199 /**
200 * Set up callbacks for WP_Query filters.
201 *
202 * @since 1.4.0
203 * @version 1.7.0.9
204 *
205 * @return void
206 */
207 protected function prepare_query() {
208 add_action( 'charitable_pre_query', array( $this, 'setup_fields' ) );
209 add_action( 'charitable_pre_query', array( $this, 'setup_orderby' ) );
210 add_filter( 'charitable_query_join', array( $this, 'join_campaign_donations_table_on_donation' ), 5 );
211 add_filter( 'charitable_query_join', array( $this, 'join_campaign_donors_table_on_donation' ), 6 );
212 add_filter( 'charitable_query_join', array( $this, 'join_meta' ), 7 );
213 add_filter( 'charitable_query_where', array( $this, 'where_donation_plan_is_in' ), 8 );
214 add_filter( 'charitable_query_where', array( $this, 'where_status_is_in' ), 9 );
215 add_filter( 'charitable_query_where', array( $this, 'where_campaign_is_in' ), 10 );
216 add_filter( 'charitable_query_where', array( $this, 'where_donor_id_is_in' ), 11 );
217 add_filter( 'charitable_query_where', array( $this, 'where_donor_email_is_in' ), 12 );
218 add_filter( 'charitable_query_where', array( $this, 'where_user_id_is_in' ), 13 );
219 add_filter( 'charitable_query_where', array( $this, 'where_donation_id_is_in' ), 14 );
220 add_filter( 'charitable_query_where', array( $this, 'where_date' ), 15 );
221 add_filter( 'charitable_query_where', array( $this, 'where_meta' ), 16 );
222 add_filter( 'charitable_query_groupby', array( $this, 'groupby_donation_id' ) );
223 add_action( 'charitable_post_query', array( $this, 'unhook_callbacks' ) );
224 }
225 }
226
227 endif;
228