PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.0.2
GiveWP – Donation Plugin and Fundraising Platform v2.0.2
4.16.4 4.16.3 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 / donors / class-give-donors-query.php
give / includes / donors Last commit date
backward-compatibility.php 8 years ago class-give-donors-query.php 8 years ago
class-give-donors-query.php
449 lines
1 <?php
2 /**
3 * Donors Query
4 *
5 * @package Give
6 * @subpackage Classes/Stats
7 * @copyright Copyright (c) 2017, WordImpress
8 * @license https://opensource.org/licenses/gpl-license GNU Public License
9 * @since 1.8.14
10 */
11
12 // Exit if accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * Give_Donors_Query Class
19 *
20 * This class is for retrieving donors data.
21 *
22 * Donors can be retrieved for date ranges and pre-defined periods.
23 *
24 * @since 1.8.14
25 */
26 class Give_Donors_Query {
27
28 /**
29 * The args to pass to the give_get_donors() query
30 *
31 * @since 1.8.14
32 * @access public
33 *
34 * @var array
35 */
36 public $args = array();
37
38 /**
39 * The donors found based on the criteria set
40 *
41 * @since 1.8.14
42 * @access public
43 *
44 * @var array
45 */
46 public $donors = array();
47
48 /**
49 * The donors found based on the criteria set
50 *
51 * @since 1.8.14
52 * @access public
53 *
54 * @var string
55 */
56 public $table_name = '';
57
58 /**
59 * The donors found based on the criteria set
60 *
61 * @since 1.8.14
62 * @access public
63 *
64 * @var string
65 */
66 public $meta_table_name = '';
67
68 /**
69 * The donors found based on the criteria set
70 *
71 * @since 1.8.14
72 * @access public
73 *
74 * @var string
75 */
76 public $meta_type = '';
77
78 /**
79 * Default query arguments.
80 *
81 * Not all of these are valid arguments that can be passed to WP_Query. The ones that are not, are modified before
82 * the query is run to convert them to the proper syntax.
83 *
84 * @since 1.8.14
85 * @access public
86 *
87 * @param $args array The array of arguments that can be passed in and used for setting up this payment query.
88 */
89 public function __construct( $args = array() ) {
90 $defaults = array(
91 'number' => 20,
92 'offset' => 0,
93 'paged' => 1,
94 'orderby' => 'id',
95 'order' => 'DESC',
96 'user' => null,
97 'email' => null,
98 'donor' => null,
99 'meta_query' => array(),
100 'date_query' => array(),
101 's' => null,
102 'fields' => 'all', // Support donors (all fields) or valid column as string or array list
103 'count' => false,
104 // 'form' => array(),
105 );
106
107 $this->args = wp_parse_args( $args, $defaults );
108 $this->table_name = Give()->donors->table_name;
109 $this->meta_table_name = Give()->donor_meta->table_name;
110 $this->meta_type = Give()->donor_meta->meta_type;
111 }
112
113 /**
114 * Modify the query/query arguments before we retrieve donors.
115 *
116 * @since 1.8.14
117 * @access public
118 *
119 * @return void
120 */
121 public function init() {
122 }
123
124
125 /**
126 * Retrieve donors.
127 *
128 * The query can be modified in two ways; either the action before the
129 * query is run, or the filter on the arguments (existing mainly for backwards
130 * compatibility).
131 *
132 * @since 1.8.14
133 * @access public
134 *
135 * @global wpdb $wpdb
136 *
137 * @return array
138 */
139 public function get_donors() {
140 global $wpdb;
141
142 /**
143 * Fires before retrieving donors.
144 *
145 * @since 1.8.14
146 *
147 * @param Give_Donors_Query $this Donors query object.
148 */
149 do_action( 'give_pre_get_donors', $this );
150
151 $cache_key = Give_Cache::get_key( 'give_donor', $this->get_sql(), false );
152
153 // Get donors from cache.
154 $this->donors = Give_Cache::get_db_query( $cache_key );
155
156 if ( is_null( $this->donors ) ) {
157 if ( empty( $this->args['count'] ) ) {
158 $this->donors = $wpdb->get_results( $this->get_sql() );
159 } else {
160 $this->donors = $wpdb->get_var( $this->get_sql() );
161 }
162
163 Give_Cache::set_db_query( $cache_key, $this->donors );
164 }
165
166
167 /**
168 * Fires after retrieving donors.
169 *
170 * @since 1.8.14
171 *
172 * @param Give_Donors_Query $this Donors query object.
173 */
174 do_action( 'give_post_get_donors', $this );
175
176 return $this->donors;
177 }
178
179 /**
180 * Get sql query from queried array.
181 *
182 * @since 2.0
183 * @access public
184 *
185 * @global wpdb $wpdb
186 * @return string
187 */
188 public function get_sql() {
189 global $wpdb;
190
191 if ( $this->args['number'] < 1 ) {
192 $this->args['number'] = 99999999999;
193 }
194
195 $where = $this->get_where_query();
196
197
198 // Set offset.
199 if ( empty( $this->args['offset'] ) && ( 0 < $this->args['paged'] ) ) {
200 $this->args['offset'] = $this->args['number'] * ( $this->args['paged'] - 1 );
201 }
202
203 // Set fields.
204 $fields = "{$this->table_name}.*";
205 if ( ! empty( $this->args['fields'] ) && 'all' !== $this->args['fields'] ) {
206 if ( is_string( $this->args['fields'] ) ) {
207 $fields = "{$this->table_name}.{$this->args['fields']}";
208 } elseif ( is_array( $this->args['fields'] ) ) {
209 $fields = "{$this->table_name}." . implode( " , {$this->table_name}.", $this->args['fields'] );
210 }
211 }
212
213 // Set count.
214 if ( ! empty( $this->args['count'] ) ) {
215 $fields = "COUNT({$this->table_name}.id)";
216 }
217
218 $orderby = $this->get_order_query();
219
220 $sql = $wpdb->prepare(
221 "SELECT {$fields} FROM {$this->table_name} LIMIT %d,%d;",
222 absint( $this->args['offset'] ),
223 absint( $this->args['number'] )
224 );
225
226 // $where, $orderby and order already prepared query they can generate notice if you re prepare them in above.
227 // WordPress consider LIKE condition as placeholder if start with s,f, or d.
228 $sql = str_replace( 'LIMIT', "{$where} {$orderby} {$this->args['order']} LIMIT", $sql );
229
230 return $sql;
231 }
232
233 /**
234 * Set query where clause.
235 *
236 * @since 1.8.14
237 * @access private
238 *
239 * @global wpdb $wpdb
240 * @return string
241 */
242 private function get_where_query() {
243 $where = '';
244
245 // Get sql query for meta.
246 if ( ! empty( $this->args['meta_query'] ) ) {
247 $meta_query_object = new WP_Meta_Query( $this->args['meta_query'] );
248 $meta_query = $meta_query_object->get_sql(
249 $this->meta_type,
250 $this->table_name,
251 'id'
252 );
253
254 $where = implode( '', $meta_query );
255 }
256
257 $where .= 'WHERE 1=1 ';
258 $where .= $this->get_where_search();
259 $where .= $this->get_where_email();
260 $where .= $this->get_where_donor();
261 $where .= $this->get_where_user();
262 $where .= $this->get_where_date();
263
264 return trim( $where );
265
266 }
267
268 /**
269 * Set email where clause.
270 *
271 * @since 1.8.14
272 * @access private
273 *
274 * @global wpdb $wpdb
275 * @return string
276 */
277 private function get_where_email() {
278 global $wpdb;
279
280 $where = '';
281
282 if ( ! empty( $this->args['email'] ) ) {
283
284 if ( is_array( $this->args['email'] ) ) {
285
286 $emails_count = count( $this->args['email'] );
287 $emails_placeholder = array_fill( 0, $emails_count, '%s' );
288 $emails = implode( ', ', $emails_placeholder );
289
290 $where .= $wpdb->prepare( "AND {$this->table_name}.email IN( $emails )", $this->args['email'] );
291 } else {
292 $where .= $wpdb->prepare( "AND {$this->table_name}.email = %s", $this->args['email'] );
293 }
294 }
295
296 return $where;
297 }
298
299 /**
300 * Set donor where clause.
301 *
302 * @since 1.8.14
303 * @access private
304 *
305 * @global wpdb $wpdb
306 * @return string
307 */
308 private function get_where_donor() {
309 $where = '';
310
311 // Specific donors.
312 if ( ! empty( $this->args['donor'] ) ) {
313 if ( ! is_array( $this->args['donor'] ) ) {
314 $this->args['donor'] = explode( ',', $this->args['donor'] );
315 }
316 $donor_ids = implode( ',', array_map( 'intval', $this->args['donor'] ) );
317
318 $where .= "AND {$this->table_name}.id IN( {$donor_ids} )";
319 }
320
321 return $where;
322 }
323
324 /**
325 * Set date where clause.
326 *
327 * @since 1.8.14
328 * @access private
329 *
330 * @global wpdb $wpdb
331 * @return string
332 */
333 private function get_where_date() {
334 $where = '';
335
336 // Donors created for a specific date or in a date range
337 if ( ! empty( $this->args['date_query'] ) ) {
338 $date_query_object = new WP_Date_Query(
339 is_array( $this->args['date_query'] ) ? $this->args['date_query'] : wp_parse_args( $this->args['date_query'] ),
340 "{$this->table_name}.date_created"
341 );
342
343 $where .= str_replace(
344 array(
345 "\n",
346 '( (',
347 '))',
348 ),
349 array(
350 '',
351 '( (',
352 ') )',
353 ),
354 $date_query_object->get_sql()
355 );
356 }
357
358 return $where;
359 }
360
361 /**
362 * Set search where clause.
363 *
364 * @since 1.8.14
365 * @access private
366 *
367 * @global wpdb $wpdb
368 * @return string
369 */
370 private function get_where_search() {
371 $where = '';
372
373 // Donors created for a specific date or in a date range
374 if ( ! empty( $this->args['s'] ) && false !== strpos( $this->args['s'], ':' ) ) {
375 $search_parts = explode( ':', $this->args['s'] );
376
377 if ( ! empty( $search_parts[0] ) ) {
378 switch ( $search_parts[0] ) {
379 case 'name':
380 $where = "AND {$this->table_name}.name LIKE '%{$search_parts[1]}%'";
381 break;
382
383 case 'note':
384 $where = "AND {$this->table_name}.notes LIKE '%{$search_parts[1]}%'";
385 break;
386 }
387 }
388 }
389
390 return $where;
391 }
392
393 /**
394 * Set user where clause.
395 *
396 * @since 1.8.14
397 * @access private
398 *
399 * @global wpdb $wpdb
400 * @return string
401 */
402 private function get_where_user() {
403 $where = '';
404
405 // Donors create for specific wp user.
406 if ( ! empty( $this->args['user'] ) ) {
407 if ( ! is_array( $this->args['user'] ) ) {
408 $this->args['user'] = explode( ',', $this->args['user'] );
409 }
410 $user_ids = implode( ',', array_map( 'intval', $this->args['user'] ) );
411
412 $where .= "AND {$this->table_name}.user_id IN( {$user_ids} )";
413 }
414
415 return $where;
416 }
417
418 /**
419 * Set orderby query
420 *
421 * @since 1.8.14
422 * @access private
423 *
424 * @return string
425 */
426 private function get_order_query() {
427 $table_columns = Give()->donors->get_columns();
428
429 $this->args['orderby'] = ! array_key_exists( $this->args['orderby'], $table_columns ) ?
430 'id' :
431 $this->args['orderby'];
432
433 $this->args['orderby'] = esc_sql( $this->args['orderby'] );
434 $this->args['order'] = esc_sql( $this->args['order'] );
435
436 switch ( $table_columns[ $this->args['orderby'] ] ) {
437 case '%d':
438 case '%f':
439 $query = "ORDER BY {$this->table_name}.{$this->args['orderby']}+0";
440 break;
441
442 default:
443 $query = "ORDER BY {$this->table_name}.{$this->args['orderby']}";
444 }
445
446 return $query;
447 }
448 }
449