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

229 lines 8.0 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) 2023, 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 // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter -- get_prepared_sql() internally calls $wpdb->prepare() which properly escapes the SQL.
124 return $wpdb->get_col( $this->get_prepared_sql( $sql ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.NotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter
125 }
126
127 /**
128 * Set up fields query argument.
129 *
130 * @since 1.4.0
131 *
132 * @return void
133 */
134 public function setup_fields() {
135 /* If we are returning IDs or Donation objects, we only need to return the donation IDs. */
136 if ( in_array( $this->get( 'output' ), array( 'donations', 'ids' ) ) ) {
137 return;
138 }
139
140 add_filter( 'charitable_query_fields', array( $this, 'donation_fields' ), 4 );
141 add_filter( 'charitable_query_fields', array( $this, 'donation_calc_fields' ), 5 );
142 }
143
144 /**
145 * Set up orderby query argument.
146 *
147 * @since 1.4.0
148 *
149 * @return void
150 */
151 public function setup_orderby() {
152 $orderby = $this->get( 'orderby', false );
153
154 if ( ! $orderby ) {
155 return;
156 }
157
158 switch ( $orderby ) {
159 case 'date':
160 add_filter( 'charitable_query_orderby', array( $this, 'orderby_date' ) );
161 break;
162
163 case 'amount':
164 add_filter( 'charitable_query_orderby', array( $this, 'orderby_donation_amount' ) );
165 break;
166 }
167 }
168
169 /**
170 * Remove any hooks that have been attached by the class to prevent contaminating other queries.
171 *
172 * @since 1.4.0
173 * @version 1.7.0.9
174 *
175 * @return void
176 */
177 public function unhook_callbacks() {
178 remove_action( 'charitable_pre_query', array( $this, 'setup_fields' ) );
179 remove_filter( 'charitable_query_fields', array( $this, 'donation_fields' ), 4 );
180 remove_filter( 'charitable_query_fields', array( $this, 'donation_calc_fields' ), 5 );
181 remove_filter( 'charitable_query_join', array( $this, 'join_campaign_donations_table_on_donation' ), 4 );
182 remove_filter( 'charitable_query_join', array( $this, 'join_campaign_donors_table_on_donation' ), 6 );
183 remove_filter( 'charitable_query_join', array( $this, 'join_meta' ), 7 );
184 remove_filter( 'charitable_query_where', array( $this, 'where_donation_plan_is_in' ), 8 );
185 remove_filter( 'charitable_query_where', array( $this, 'where_status_is_in' ), 9 );
186 remove_filter( 'charitable_query_where', array( $this, 'where_campaign_is_in' ), 10 );
187 remove_filter( 'charitable_query_where', array( $this, 'where_donor_id_is_in' ), 11 );
188 remove_filter( 'charitable_query_where', array( $this, 'where_donor_email_is_in' ), 12 );
189 remove_filter( 'charitable_query_where', array( $this, 'where_user_id_is_in' ), 13 );
190 remove_filter( 'charitable_query_where', array( $this, 'where_donation_id_is_in' ), 14 );
191 remove_filter( 'charitable_query_where', array( $this, 'where_date' ), 15 );
192 remove_filter( 'charitable_query_where', array( $this, 'where_meta' ), 16 );
193 remove_action( 'charitable_pre_query', array( $this, 'setup_orderby' ) );
194 remove_filter( 'charitable_query_orderby', array( $this, 'orderby_date' ) );
195 remove_filter( 'charitable_query_orderby', array( $this, 'orderby_donation_amount' ) );
196 remove_filter( 'charitable_query_groupby', array( $this, 'groupby_donation_id' ) );
197 remove_action( 'charitable_post_query', array( $this, 'unhook_callbacks' ) );
198 }
199
200 /**
201 * Set up callbacks for WP_Query filters.
202 *
203 * @since 1.4.0
204 * @version 1.7.0.9
205 *
206 * @return void
207 */
208 protected function prepare_query() {
209 add_action( 'charitable_pre_query', array( $this, 'setup_fields' ) );
210 add_action( 'charitable_pre_query', array( $this, 'setup_orderby' ) );
211 add_filter( 'charitable_query_join', array( $this, 'join_campaign_donations_table_on_donation' ), 4 );
212 add_filter( 'charitable_query_join', array( $this, 'join_campaign_donors_table_on_donation' ), 6 );
213 add_filter( 'charitable_query_join', array( $this, 'join_meta' ), 7 );
214 add_filter( 'charitable_query_where', array( $this, 'where_donation_plan_is_in' ), 8 );
215 add_filter( 'charitable_query_where', array( $this, 'where_status_is_in' ), 9 );
216 add_filter( 'charitable_query_where', array( $this, 'where_campaign_is_in' ), 10 );
217 add_filter( 'charitable_query_where', array( $this, 'where_donor_id_is_in' ), 11 );
218 add_filter( 'charitable_query_where', array( $this, 'where_donor_email_is_in' ), 12 );
219 add_filter( 'charitable_query_where', array( $this, 'where_user_id_is_in' ), 13 );
220 add_filter( 'charitable_query_where', array( $this, 'where_donation_id_is_in' ), 14 );
221 add_filter( 'charitable_query_where', array( $this, 'where_date' ), 15 );
222 add_filter( 'charitable_query_where', array( $this, 'where_meta' ), 16 );
223 add_filter( 'charitable_query_groupby', array( $this, 'groupby_donation_id' ) );
224 add_action( 'charitable_post_query', array( $this, 'unhook_callbacks' ) );
225 }
226 }
227
228 endif;
229