Give_Payments_Query::donor()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Payments Query
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/Stats
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_Payments_Query Class
19
 *
20
 * This class is for retrieving payments data.
21
 *
22
 * Payments can be retrieved for date ranges and pre-defined periods.
23
 *
24
 * @since 1.0
25
 */
26
class Give_Payments_Query extends Give_Stats {
27
28
	/**
29
	 * Preserve args
30
	 *
31
	 * @since  1.8.17
32
	 * @access public
33
	 *
34
	 * @var    array
35
	 */
36
	public $_args = array();
37
38
	/**
39
	 * The args to pass to the give_get_payments() query
40
	 *
41
	 * @since  1.0
42
	 * @access public
43
	 *
44
	 * @var    array
45
	 */
46
	public $args = array();
47
48
	/**
49
	 * The payments found based on the criteria set
50
	 *
51
	 * @since  1.0
52
	 * @access public
53
	 *
54
	 * @var    array
55
	 */
56
	public $payments = array();
57
58
	/**
59
	 * Default query arguments.
60
	 *
61
	 * Not all of these are valid arguments that can be passed to WP_Query. The ones that are not, are modified before
62
	 * the query is run to convert them to the proper syntax.
63
	 *
64
	 * @since  1.0
65
	 * @access public
66
	 *
67
	 * @param  $args array The array of arguments that can be passed in and used for setting up this payment query.
68
	 */
69
	public function __construct( $args = array() ) {
70
		$defaults = array(
71
			'output'          => 'payments',
72
			'post_type'       => array( 'give_payment' ),
73
			'start_date'      => false,
74
			'end_date'        => false,
75
			'number'          => 20,
76
			'page'            => null,
77
			'orderby'         => 'ID',
78
			'order'           => 'DESC',
79
			'user'            => null, // deprecated, use donor
80
			'donor'           => null,
81
			'status'          => give_get_payment_status_keys(),
82
			'meta_key'        => null,
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
83
			'year'            => null,
84
			'month'           => null,
85
			'day'             => null,
86
			's'               => null,
87
			'search_in_notes' => false,
88
			'children'        => false,
89
			'fields'          => null,
90
			'gateway'         => null,
91
			'give_forms'      => null,
92
			'offset'          => null,
93
94
			// Currently these params only works with get_payment_by_group
95
			'group_by'        => '',
96
			'count'           => false,
97
		);
98
99
		$this->args = $this->_args = wp_parse_args( $args, $defaults );
100
101
		$this->init();
102
	}
103
104
	/**
105
	 * Set a query variable.
106
	 *
107
	 * @since  1.0
108
	 * @access public
109
	 *
110
	 * @param $query_var
111
	 * @param $value
112
	 */
113 View Code Duplication
	public function __set( $query_var, $value ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
		if ( in_array( $query_var, array( 'meta_query', 'tax_query' ) ) ) {
115
			$this->args[ $query_var ][] = $value;
116
		} else {
117
			$this->args[ $query_var ] = $value;
118
		}
119
	}
120
121
	/**
122
	 * Unset a query variable.
123
	 *
124
	 * @since  1.0
125
	 * @access public
126
	 *
127
	 * @param $query_var
128
	 */
129
	public function __unset( $query_var ) {
130
		unset( $this->args[ $query_var ] );
131
	}
132
133
	/**
134
	 * Modify the query/query arguments before we retrieve payments.
135
	 *
136
	 * @since  1.0
137
	 * @access public
138
	 *
139
	 * @return void
140
	 */
141
	public function init() {
142
	}
143
144
145
	/**
146
	 * Set query filter.
147
	 *
148
	 * @since  1.8.9
149
	 * @access private
150
	 */
151
	private function set_filters() {
152
		// Reset param to apply filters.
153
		// While set filters $args will get override and multiple get_payments call will not work.
154
		$this->args = $this->_args;
155
156
		$this->date_filter_pre();
157
		$this->orderby();
158
		$this->status();
159
		$this->month();
160
		$this->per_page();
161
		$this->page();
162
		$this->user();
163
		$this->donor();
164
		$this->search();
165
		$this->mode();
166
		$this->children();
167
		$this->give_forms();
168
		$this->gateway_filter();
169
170
		add_filter( 'posts_orderby', array( $this, 'custom_orderby' ), 10, 2 );
171
172
		/**
173
		 * Fires after setup filters.
174
		 *
175
		 * @since 1.0
176
		 *
177
		 * @param Give_Payments_Query $this Payments query object.
178
		 */
179
		do_action( 'give_pre_get_payments', $this );
180
	}
181
182
	/**
183
	 * Unset query filter.
184
	 *
185
	 * @since  1.8.9
186
	 * @access private
187
	 */
188
	private function unset_filters() {
189
		remove_filter( 'posts_orderby', array( $this, 'custom_orderby' ) );
190
191
		/**
192
		 * Fires after retrieving payments.
193
		 *
194
		 * @since 1.0
195
		 *
196
		 * @param Give_Payments_Query $this Payments query object.
197
		 */
198
		do_action( 'give_post_get_payments', $this );
199
	}
200
201
202
	/**
203
	 * Retrieve payments.
204
	 *
205
	 * The query can be modified in two ways; either the action before the
206
	 * query is run, or the filter on the arguments (existing mainly for backwards
207
	 * compatibility).
208
	 *
209
	 * @since  1.0
210
	 * @access public
211
	 *
212
	 * @return array
213
	 */
214
	public function get_payments() {
215
		global $post;
216
217
		$cache_key      = Give_Cache::get_key( 'give_payment_query', $this->args, false );
218
		$this->payments = Give_Cache::get_db_query( $cache_key );
0 ignored issues
show
Documentation Bug introduced by
It seems like \Give_Cache::get_db_query($cache_key) of type * is incompatible with the declared type array of property $payments.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
219
220
		// Return cached result.
221
		if ( ! is_null( $this->payments ) ) {
222
			return $this->payments;
223
		}
224
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
225
226
		// Modify the query/query arguments before we retrieve payments.
227
		$this->set_filters();
228
229
		$query          = new WP_Query( $this->args );
230
		$this->payments = array();
231
232
		$custom_output = array(
233
			'payments',
234
			'give_payments',
235
		);
236
237
		if ( ! in_array( $this->args['output'], $custom_output ) ) {
238
			return $query->posts;
239
		}
240
241
		if ( $query->have_posts() ) {
242
			$previous_post = $post;
243
244
			while ( $query->have_posts() ) {
245
				$query->the_post();
246
247
				$payment_id = get_post()->ID;
248
				$payment    = new Give_Payment( $payment_id );
249
250
				$this->payments[] = apply_filters( 'give_payment', $payment, $payment_id, $this );
251
			}
252
253
			wp_reset_postdata();
254
255
			// Prevent nest loop from producing unexpected results.
256
			if( $previous_post instanceof WP_Post ) {
0 ignored issues
show
Bug introduced by
The class WP_Post does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
257
				$post = $previous_post;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
258
				setup_postdata( $post );
259
			}
260
		}
261
262
		Give_Cache::set_db_query( $cache_key, $this->payments );
263
264
		// Remove query filters after we retrieve payments.
265
		$this->unset_filters();
266
267
		return $this->payments;
268
	}
269
270
	/**
271
	 * Get payments by group
272
	 *
273
	 * @since  1.8.17
274
	 * @access public
275
	 *
276
	 * @return array
277
	 */
278
	public function get_payment_by_group() {
279
		global $wpdb;
280
281
		$allowed_groups = array( 'post_status' );
282
		$result         = array();
283
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
284
285
		if ( in_array( $this->args['group_by'], $allowed_groups ) ) {
286
			// Set only count in result.
287
			if ( $this->args['count'] ) {
288
289
				$this->set_filters();
290
291
				$new_results = $wpdb->get_results( $this->get_sql(), ARRAY_N );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
292
293
				$this->unset_filters();
294
295
				foreach ( $new_results as $results ) {
296
					$result[ $results[0] ] = $results[1];
297
				}
298
299
				switch ( $this->args['group_by'] ) {
300
					case 'post_status':
301
302
						/* @var Give_Payment $donation */
303
						foreach ( give_get_payment_status_keys() as $status ) {
304
							if ( ! isset( $result[ $status ] ) ) {
305
								$result[ $status ] = 0;
306
							}
307
						}
308
309
						break;
310
				}
311
			} else {
312
				$donations = $this->get_payments();
313
314
				/* @var $donation Give_Payment */
315
				foreach ( $donations as $donation ) {
316
					$result[ $donation->{$this->args['group_by']} ][] = $donation;
317
				}
318
			}
319
		}
320
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
321
322
		/**
323
		 * Filter the result
324
		 *
325
		 * @since 1.8.17
326
		 */
327
		return apply_filters( 'give_get_payment_by_group', $result, $this );
328
	}
329
330
	/**
331
	 * If querying a specific date, add the proper filters.
332
	 *
333
	 * @since  1.0
334
	 * @access public
335
	 *
336
	 * @return void
337
	 */
338
	public function date_filter_pre() {
339
		if ( ! ( $this->args['start_date'] || $this->args['end_date'] ) ) {
340
			return;
341
		}
342
343
		$this->setup_dates( $this->args['start_date'], $this->args['end_date'] );
344
345
		$is_start_date = property_exists( __CLASS__, 'start_date' );
346
		$is_end_date   = property_exists( __CLASS__, 'end_date' );
347
348
		if ( $is_start_date || $is_end_date ) {
349
			$date_query = array();
350
351
			if ( $is_start_date && ! is_wp_error( $this->start_date ) ) {
352
				$date_query['after'] = date( 'Y-m-d H:i:s', $this->start_date );
353
			}
354
355
			if ( $is_end_date && ! is_wp_error( $this->end_date ) ) {
356
				$date_query['before'] = date( 'Y-m-d H:i:s', $this->end_date );
357
			}
358
359
			// Include Start Date and End Date while querying.
360
			$date_query['inclusive'] = true;
361
362
			$this->__set( 'date_query', $date_query );
363
364
		}
365
	}
366
367
	/**
368
	 * Post Status
369
	 *
370
	 * @since  1.0
371
	 * @access public
372
	 *
373
	 * @return void
374
	 */
375
	public function status() {
376
		if ( ! isset( $this->args['status'] ) ) {
377
			return;
378
		}
379
380
		$this->__set( 'post_status', $this->args['status'] );
381
		$this->__unset( 'status' );
382
	}
383
384
	/**
385
	 * Current Page
386
	 *
387
	 * @since  1.0
388
	 * @access public
389
	 *
390
	 * @return void
391
	 */
392
	public function page() {
393
		if ( ! isset( $this->args['page'] ) ) {
394
			return;
395
		}
396
397
		$this->__set( 'paged', $this->args['page'] );
398
		$this->__unset( 'page' );
399
	}
400
401
	/**
402
	 * Posts Per Page
403
	 *
404
	 * @since  1.0
405
	 * @access public
406
	 *
407
	 * @return void
408
	 */
409
	public function per_page() {
410
411
		if ( ! isset( $this->args['number'] ) ) {
412
			return;
413
		}
414
415
		if ( $this->args['number'] == - 1 ) {
416
			$this->__set( 'nopaging', true );
417
		} else {
418
			$this->__set( 'posts_per_page', $this->args['number'] );
419
		}
420
421
		$this->__unset( 'number' );
422
	}
423
424
	/**
425
	 * Current Month
426
	 *
427
	 * @since  1.0
428
	 * @access public
429
	 *
430
	 * @return void
431
	 */
432
	public function month() {
433
		if ( ! isset( $this->args['month'] ) ) {
434
			return;
435
		}
436
437
		$this->__set( 'monthnum', $this->args['month'] );
438
		$this->__unset( 'month' );
439
	}
440
441
	/**
442
	 * Order by
443
	 *
444
	 * @since  1.0
445
	 * @access public
446
	 *
447
	 * @return void
448
	 */
449
	public function orderby() {
450
		switch ( $this->args['orderby'] ) {
451
			case 'amount':
452
				$this->__set( 'orderby', 'meta_value_num' );
453
				$this->__set( 'meta_key', '_give_payment_total' );
454
				break;
455
456
			case 'status':
457
				$this->__set( 'orderby', 'post_status' );
458
				break;
459
460
			case 'donation_form':
461
				$this->__set( 'orderby', 'meta_value' );
462
				$this->__set( 'meta_key', '_give_payment_form_title' );
463
				break;
464
465
			default:
466
				$this->__set( 'orderby', $this->args['orderby'] );
467
				break;
468
		}
469
	}
470
471
	/**
472
	 * Custom orderby.
473
	 * Note: currently custom sorting is only used for donation listing page.
474
	 *
475
	 * @since  1.8
476
	 * @access public
477
	 *
478
	 * @param string   $order
479
	 * @param WP_Query $query
480
	 *
481
	 * @return mixed
482
	 */
483
	public function custom_orderby( $order, $query ) {
484
485
		if ( ! empty( $query->query['post_type'] ) ) {
486
			$post_types = is_array( $query->query['post_type'] ) ? $query->query['post_type'] : array( $query->query['post_type'] );
487
488
			if ( ! in_array( 'give_payment', $post_types ) || ! isset( $query->query['orderby'] ) || is_array( $query->query['orderby'] ) ) {
489
				return $order;
490
			}
491
492
			global $wpdb;
493
			switch ( $query->query['orderby'] ) {
494
				case 'post_status':
495
					$order = $wpdb->posts . '.post_status ' . strtoupper( $query->query['order'] );
496
					break;
497
			}
498
		}
499
500
		return $order;
501
	}
502
503
	/**
504
	 * Specific User
505
	 *
506
	 * @since  1.0
507
	 * @access public
508
	 *
509
	 * @return void
510
	 */
511
	public function user() {
512
		if ( is_null( $this->args['user'] ) ) {
513
			return;
514
		}
515
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
516
517
		$args = array();
518
519
		if ( is_numeric( $this->args['user'] ) ) {
520
			// Backward compatibility: user donor param to get payment attached to donor instead of user
521
			$donor_id = Give()->donors->get_column_by( 'id', is_numeric( $this->args['user'] ) ? 'user_id' : 'email', $this->args['user'] );
522
523
			$args = array(
524
				'key'   => '_give_payment_donor_id',
525
				'value' => absint( $donor_id ),
526
			);
527
		} elseif ( is_email( $this->args['user'] ) ) {
528
			$args = array(
529
				'key'   => '_give_payment_donor_email',
530
				'value' => $this->args['user'],
531
			);
532
		}
533
534
		$this->__set( 'meta_query',$args );
535
	}
536
537
	/**
538
	 * Specific donor id
539
	 *
540
	 * @access  public
541
	 * @since   1.8.9
542
	 * @return  void
543
	 */
544
	public function donor() {
545
		if ( is_null( $this->args['donor'] ) || ! is_numeric( $this->args['donor'] ) ) {
546
			return;
547
		}
548
549
		$donor_meta_type = Give()->donor_meta->meta_type;
550
551
		$this->__set( 'meta_query', array(
552
			'key'   => "_give_payment_{$donor_meta_type}_id",
553
			'value' => (int) $this->args['donor'],
554
		) );
555
	}
556
557
	/**
558
	 * Search
559
	 *
560
	 * @since  1.0
561
	 * @access public
562
	 *
563
	 * @return void
564
	 */
565
	public function search() {
566
567
		if ( ! isset( $this->args['s'] ) ) {
568
			return;
569
		}
570
571
		$search = trim( $this->args['s'] );
572
573
		if ( empty( $search ) ) {
574
			return;
575
		}
576
577
		$is_email = is_email( $search ) || strpos( $search, '@' ) !== false;
578
		$is_user  = strpos( $search, strtolower( 'user:' ) ) !== false;
579
580
		if ( ! empty( $this->args['search_in_notes'] ) ) {
581
582
			$notes = give_get_payment_notes( 0, $search );
583
584
			if ( ! empty( $notes ) ) {
585
586
				$payment_ids = wp_list_pluck( (array) $notes, 'comment_post_ID' );
587
588
				$this->__set( 'post__in', $payment_ids );
589
			}
590
591
			$this->__unset( 's' );
592
593
		} elseif ( $is_email || strlen( $search ) == 32 ) {
594
595
			$key         = $is_email ? '_give_payment_donor_email' : '_give_payment_purchase_key';
596
			$search_meta = array(
597
				'key'     => $key,
598
				'value'   => $search,
599
				'compare' => 'LIKE',
600
			);
601
602
			$this->__set( 'meta_query', $search_meta );
603
			$this->__unset( 's' );
604
605
		} elseif ( $is_user ) {
606
607
			$search_meta = array(
608
				'key'   => '_give_payment_donor_id',
609
				'value' => trim( str_replace( 'user:', '', strtolower( $search ) ) ),
610
			);
611
612
			$this->__set( 'meta_query', $search_meta );
613
614
			$this->__unset( 's' );
615
616
		} elseif ( is_numeric( $search ) ) {
617
618
			$post = get_post( $search );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
619
620
			if ( is_object( $post ) && $post->post_type == 'give_payment' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
621
622
				$arr   = array();
623
				$arr[] = $search;
624
				$this->__set( 'post__in', $arr );
625
				$this->__unset( 's' );
626
			}
627
		} elseif ( '#' == substr( $search, 0, 1 ) ) {
628
629
			$search = str_replace( '#:', '', $search );
630
			$search = str_replace( '#', '', $search );
631
			$this->__set( 'give_forms', $search );
632
			$this->__unset( 's' );
633
634
		} else if ( ! empty( $search ) ) {
635
			$search_parts = preg_split( '/\s+/', $search );
636
			if ( is_array( $search_parts ) && 2 === count( $search_parts ) ) {
637
				$search_meta = array(
638
					'relation' => 'AND',
639
					array(
640
						'key'     => '_give_donor_billing_first_name',
641
						'value'   => $search_parts[0],
642
						'compare' => '=',
643
					),
644
					array(
645
						'key'     => '_give_donor_billing_last_name',
646
						'value'   => $search_parts[1],
647
						'compare' => '=',
648
					),
649
				);
650
			} else {
651
				$search_meta = array(
652
					'relation' => 'OR',
653
					array(
654
						'key'     => '_give_donor_billing_first_name',
655
						'value'   => $search,
656
						'compare' => 'LIKE',
657
					),
658
					array(
659
						'key'     => '_give_donor_billing_last_name',
660
						'value'   => $search,
661
						'compare' => 'LIKE',
662
					),
663
				);
664
			}
665
			$this->__set( 'meta_query', $search_meta );
666
667
			$this->__unset( 's' );
668
669
		} else {
670
			$this->__set( 's', $search );
671
672
		}
673
674
	}
675
676
	/**
677
	 * Payment Mode
678
	 *
679
	 * @since  1.0
680
	 * @access public
681
	 *
682
	 * @return void
683
	 */
684
	public function mode() {
685
		if ( empty( $this->args['mode'] ) || $this->args['mode'] == 'all' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
686
			$this->__unset( 'mode' );
687
688
			return;
689
		}
690
691
		$this->__set(
692
			'meta_query', array(
693
				'key'   => '_give_payment_mode',
694
				'value' => $this->args['mode'],
695
			)
696
		);
697
	}
698
699
	/**
700
	 * Children
701
	 *
702
	 * @since  1.0
703
	 * @access public
704
	 *
705
	 * @return void
706
	 */
707
	public function children() {
708
		if ( empty( $this->args['children'] ) ) {
709
			$this->__set( 'post_parent', 0 );
710
		}
711
		$this->__unset( 'children' );
712
	}
713
714
	/**
715
	 * Specific Give Form
716
	 *
717
	 * @since  1.0
718
	 * @access public
719
	 *
720
	 * @return void
721
	 */
722 View Code Duplication
	public function give_forms() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
723
724
		if ( empty( $this->args['give_forms'] ) ) {
725
			return;
726
		}
727
728
		$compare = '=';
729
730
		if ( is_array( $this->args['give_forms'] ) ) {
731
			$compare = 'IN';
732
		}
733
734
		$this->__set(
735
			'meta_query',
736
			array(
737
				'key'     => '_give_payment_form_id',
738
				'value'   => $this->args['give_forms'],
739
				'compare' => $compare,
740
			)
741
		);
742
743
		$this->__unset( 'give_forms' );
744
745
	}
746
747
	/**
748
	 * Specific Gateway
749
	 *
750
	 * @since  1.8.17
751
	 * @access public
752
	 *
753
	 * @return void
754
	 */
755 View Code Duplication
	public function gateway_filter() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
756
757
		if ( empty( $this->args['gateway'] ) ) {
758
			return;
759
		}
760
761
		$compare = '=';
762
763
		if ( is_array( $this->args['gateway'] ) ) {
764
			$compare = 'IN';
765
		}
766
767
		$this->__set(
768
			'meta_query', array(
769
				array(
770
					'key'     => '_give_payment_gateway',
771
					'value'   => $this->args['gateway'],
772
					'compare' => $compare,
773
				),
774
			)
775
		);
776
777
		$this->__unset( 'gateway' );
778
779
	}
780
781
782
	/**
783
	 * Get sql query
784
	 *
785
	 * Note: Internal purpose only. We are developing on this fn.
786
	 *
787
	 * @since  1.8.18
788
	 * @access public
789
	 * @global $wpdb
790
	 *
791
	 * @return string
792
	 */
793
	private function get_sql() {
794
		global $wpdb;
795
796
		$where = "WHERE {$wpdb->posts}.post_type = 'give_payment'";
797
		$where .= " AND {$wpdb->posts}.post_status IN ('" . implode( "','", $this->args['post_status'] ) . "')";
798
799
		if( is_numeric( $this->args['post_parent'] ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
800
			$where .= " AND {$wpdb->posts}.post_parent={$this->args['post_parent']}";
801
		}
802
803
		// Set orderby.
804
		$orderby  = "ORDER BY {$wpdb->posts}.{$this->args['orderby']}";
805
		$group_by = '';
806
807
		// Set group by.
808 View Code Duplication
		if ( ! empty( $this->args['group_by'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
809
			$group_by = "GROUP BY {$wpdb->posts}.{$this->args['group_by']}";
810
		}
811
812
		// Set offset.
813
		if (
814
			empty( $this->args['nopaging'] ) &&
815
			empty( $this->args['offset'] ) &&
816
			( ! empty( $this->args['page'] ) && 0 < $this->args['page'] )
817
		) {
818
			$this->args['offset'] = $this->args['posts_per_page'] * ( $this->args['page'] - 1 );
819
		}
820
821
		// Set fields.
822
		$fields = "{$wpdb->posts}.*";
823
		if ( ! empty( $this->args['fields'] ) && 'all' !== $this->args['fields'] ) {
824
			if ( is_string( $this->args['fields'] ) ) {
825
				$fields = "{$wpdb->posts}.{$this->args['fields']}";
826 View Code Duplication
			} elseif ( is_array( $this->args['fields'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
827
				$fields = "{$wpdb->posts}." . implode( " , {$wpdb->posts}.", $this->args['fields'] );
828
			}
829
		}
830
831
		// Set count.
832
		if ( ! empty( $this->args['count'] ) ) {
833
			$fields = "COUNT({$wpdb->posts}.ID)";
834
835 View Code Duplication
			if ( ! empty( $this->args['group_by'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
836
				$fields = "{$wpdb->posts}.{$this->args['group_by']}, {$fields}";
837
			}
838
		}
839
840
		// Date query.
841
		if ( ! empty( $this->args['date_query'] ) ) {
842
			$date_query_obj = new WP_Date_Query( $this->args['date_query'] );
843
			$where          .= str_replace(
844
				array(
845
					"\n",
846
					'(   (',
847
					'))',
848
				),
849
				array(
850
					'',
851
					'( (',
852
					') )',
853
				),
854
				$date_query_obj->get_sql()
855
			);
856
		}
857
858
		// Meta query.
859
		if ( ! empty( $this->args['meta_query'] ) ) {
860
			$meta_query_obj = new WP_Meta_Query( $this->args['meta_query'] );
861
			$where          = implode( ' ', $meta_query_obj->get_sql( 'post', $wpdb->posts, 'ID' ) ) . " {$where}";
862
			$where          = Give()->payment_meta->__rename_meta_table_name( $where, 'posts_where' );
863
		}
864
865
		// Set sql query.
866
		$sql = $wpdb->prepare(
867
			"SELECT {$fields} FROM {$wpdb->posts} LIMIT %d,%d;",
868
			absint( $this->args['offset'] ),
869
			( empty( $this->args['nopaging'] ) ? absint( $this->args['posts_per_page'] ) : 99999999999 )
870
		);
871
872
		// $where, $orderby and order already prepared query they can generate notice if you re prepare them in above.
873
		// WordPress consider LIKE condition as placeholder if start with s,f, or d.
874
		$sql = str_replace( 'LIMIT', "{$where} {$group_by} {$orderby} {$this->args['order']} LIMIT", $sql );
875
876
		return $sql;
877
	}
878
879
}
880