Test Failed
Pull Request — master (#2551)
by Devin
04:51
created

Give_Payments_Query::get_sql()   F

Complexity

Conditions 16
Paths 384

Size

Total Lines 84
Code Lines 46

Duplication

Lines 6
Ratio 7.14 %

Code Coverage

Tests 0
CRAP Score 272

Importance

Changes 0
Metric Value
cc 16
eloc 46
nc 384
nop 0
dl 6
loc 84
rs 3.7109
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 272

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Payments Query
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/Stats
7
 * @copyright   Copyright (c) 2016, WordImpress
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 52
58
	/**
59 52
	 * Default query arguments.
60 52
	 *
61 52
	 * Not all of these are valid arguments that can be passed to WP_Query. The ones that are not, are modified before
62 52
	 * the query is run to convert them to the proper syntax.
63 52
	 *
64 52
	 * @since  1.0
65 52
	 * @access public
66 52
	 *
67 52
	 * @param  $args array The array of arguments that can be passed in and used for setting up this payment query.
68 52
	 */
69 52
	public function __construct( $args = array() ) {
70 52
		$defaults = array(
71 52
			'output'          => 'payments',
72 52
			'post_type'       => array( 'give_payment' ),
73 52
			'start_date'      => false,
74 52
			'end_date'        => false,
75 52
			'number'          => 20,
76 52
			'page'            => null,
77
			'orderby'         => 'ID',
78 52
			'order'           => 'DESC',
79
			'user'            => null,
80 52
			'donor'           => null,
81
			'status'          => give_get_payment_status_keys(),
82 52
			'meta_key'        => null,
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
83 52
			'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 52
			'give_forms'      => null,
92 52
			'offset'          => null,
93 32
94 32
			// Currently these params only works with get_payment_by_group
95 52
			'group_by'        => '',
96
			'count'           => false,
97 52
		);
98
99
		$this->args = $this->_args = wp_parse_args( $args, $defaults );
100
101
		$this->init();
102
	}
103
104
	/**
105 52
	 * Set a query variable.
106 52
	 *
107 52
	 * @since  1.0
108
	 * @access public
109
	 *
110
	 * @param $query_var
111
	 * @param $value
112
	 */
113
	public function __set( $query_var, $value ) {
114
		if ( in_array( $query_var, array( 'meta_query', 'tax_query' ) ) ) {
115
			$this->args[ $query_var ][] = $value;
116 52
		} else {
117
			$this->args[ $query_var ] = $value;
118 52
		}
119 52
	}
120
121 52
	/**
122 52
	 * Unset a query variable.
123 52
	 *
124 52
	 * @since  1.0
125 52
	 * @access public
126 52
	 *
127 52
	 * @param $query_var
128 52
	 */
129 52
	public function __unset( $query_var ) {
130 52
		unset( $this->args[ $query_var ] );
131 52
	}
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 52
145
	/**
146 52
	 * Set query filter.
147
	 *
148 52
	 * @since  1.8.9
149
	 * @access private
150
	 */
151 52
	private function set_filters() {
152 52
		// Reset param to apply filters.
153 52
		// While set filters $args will get override and multiple get_payments call will not work.
154
		$this->args = $this->_args;
155 52
156 52
		$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 52
	 *
185 52
	 * @since  1.8.9
186 52
	 * @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
		// Modify the query/query arguments before we retrieve payments.
216
		$this->set_filters();
217 52
218 52
		$query          = new WP_Query( $this->args );
219 42
		$this->payments = array();
220
221
		$custom_output = array(
222 52
			'payments',
223 52
			'give_payments',
224 52
		);
225
226
		if ( ! in_array( $this->args['output'], $custom_output ) ) {
227
			return $query->posts;
228
		}
229
230
		if ( $query->have_posts() ) {
231
			while ( $query->have_posts() ) {
232
				$query->the_post();
233 52
234 52
				$payment_id = get_post()->ID;
235 52
				$payment    = new Give_Payment( $payment_id );
236
237
				$this->payments[] = apply_filters( 'give_payment', $payment, $payment_id, $this );
238 11
			}
239 11
240 11
			wp_reset_postdata();
241
		}
242
243
		// Remove query filters after we retrieve payments.
244
		$this->unset_filters();
245
246
		return $this->payments;
247
	}
248
249 52
	/**
250
	 * Get payments by group
251 52
	 *
252 42
	 * @since  1.8.17
253
	 * @access public
254
	 *
255 52
	 * @return array
256 42
	 */
257 42
	public function get_payment_by_group() {
258 52
		global $wpdb;
259
260
		$allowed_groups = array( 'post_status' );
261 52
		$result         = array();
262 52
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
263
264
		if ( in_array( $this->args['group_by'], $allowed_groups ) ) {
265
			// Set only count in result.
266
			if ( $this->args['count'] ) {
267
268
				$this->set_filters();
269
270
				$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...
271 52
272 52
				$this->unset_filters();
273 52
274
				foreach ( $new_results as $results ) {
275
					$result[ $results[0] ] = $results[1];
276
				}
277
278
				switch ( $this->args['group_by'] ) {
279
					case 'post_status':
280
281
						/* @var Give_Payment $donation */
282
						foreach ( give_get_payment_status_keys() as $status ) {
283
							if ( ! isset( $result[ $status ] ) ) {
284
								$result[ $status ] = 0;
285
							}
286
						}
287 52
288 52
						break;
289 52
				}
290
			} else {
291
				$donations = $this->get_payments();
292
293 52
				/* @var $donation Give_Payment */
294 52
				foreach ( $donations as $donation ) {
295 52
					$result[ $donation->{$this->args['group_by']} ][] = $donation;
296 52
				}
297 52
			}
298
		}
299
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
300
301
		/**
302
		 * Filter the result
303
		 *
304
		 * @since 1.8.17
305
		 */
306 52
		return apply_filters( 'give_get_payment_by_group', $result, $this );
307 52
	}
308 52
309
	/**
310
	 * If querying a specific date, add the proper filters.
311
	 *
312
	 * @since  1.0
313
	 * @access public
314
	 *
315
	 * @return void
316
	 */
317
	public function date_filter_pre() {
318
		if ( ! ( $this->args['start_date'] || $this->args['end_date'] ) ) {
319
			return;
320
		}
321
322
		$this->setup_dates( $this->args['start_date'], $this->args['end_date'] );
323
		$is_start_date = property_exists( __CLASS__, 'start_date' );
324
		$is_end_date   = property_exists( __CLASS__, 'end_date' );
325
326
		if ( $is_start_date || $is_end_date ) {
327
			$date_query = array();
328
329
			if ( $is_start_date && ! is_wp_error( $this->start_date ) ) {
330 52
				$date_query['after'] = date( 'Y-m-d H:i:s', $this->start_date );
331
			}
332 52
333 52
			if ( $is_end_date && ! is_wp_error( $this->end_date ) ) {
334
				$date_query['before'] = date( 'Y-m-d H:i:s', $this->end_date );
335
			}
336 32
337
			$this->__set( 'date_query', $date_query );
338 32
339
		}
340
341
	}
342 32
343 32
	/**
344
	 * Post Status
345 32
	 *
346
	 * @since  1.0
347
	 * @access public
348
	 *
349
	 * @return void
350
	 */
351
	public function status() {
352
		if ( ! isset( $this->args['status'] ) ) {
353
			return;
354
		}
355
356
		$this->__set( 'post_status', $this->args['status'] );
357
		$this->__unset( 'status' );
358 32
	}
359
360 32
	/**
361
	 * Current Page
362 32
	 *
363 32
	 * @since  1.0
364
	 * @access public
365 32
	 *
366
	 * @return void
367 32
	 */
368 32
	public function page() {
369
		if ( ! isset( $this->args['page'] ) ) {
370 32
			return;
371
		}
372
373
		$this->__set( 'paged', $this->args['page'] );
374
		$this->__unset( 'page' );
375
	}
376
377
	/**
378
	 * Posts Per Page
379
	 *
380
	 * @since  1.0
381
	 * @access public
382
	 *
383
	 * @return void
384
	 */
385
	public function per_page() {
386
387
		if ( ! isset( $this->args['number'] ) ) {
388
			return;
389
		}
390
391
		if ( $this->args['number'] == - 1 ) {
392
			$this->__set( 'nopaging', true );
393
		} else {
394
			$this->__set( 'posts_per_page', $this->args['number'] );
395
		}
396
397
		$this->__unset( 'number' );
398
	}
399
400
	/**
401
	 * Current Month
402
	 *
403
	 * @since  1.0
404
	 * @access public
405
	 *
406
	 * @return void
407
	 */
408
	public function month() {
409
		if ( ! isset( $this->args['month'] ) ) {
410
			return;
411
		}
412
413
		$this->__set( 'monthnum', $this->args['month'] );
414
		$this->__unset( 'month' );
415
	}
416
417
	/**
418
	 * Order by
419
	 *
420
	 * @since  1.0
421
	 * @access public
422
	 *
423
	 * @return void
424
	 */
425
	public function orderby() {
426
		switch ( $this->args['orderby'] ) {
427
			case 'amount':
428
				$this->__set( 'orderby', 'meta_value_num' );
429
				$this->__set( 'meta_key', '_give_payment_total' );
430
				break;
431
432
			case 'status':
433
				$this->__set( 'orderby', 'post_status' );
434
				break;
435
436 32
			case 'donation_form':
437
				$this->__set( 'orderby', 'meta_value' );
438
				$this->__set( 'meta_key', '_give_payment_form_title' );
439
				break;
440
441
			default:
442
				$this->__set( 'orderby', $this->args['orderby'] );
443
				break;
444
		}
445 52
	}
446 52
447 52
	/**
448
	 * Custom orderby.
449 52
	 * Note: currently custom sorting is only used for donation listing page.
450
	 *
451
	 * @since  1.8
452
	 * @access public
453
	 *
454
	 * @param string   $order
455
	 * @param WP_Query $query
456
	 *
457
	 * @return mixed
458
	 */
459
	public function custom_orderby( $order, $query ) {
460
461
		if ( ! empty( $query->query['post_type'] ) ) {
462
			$post_types = is_array( $query->query['post_type'] ) ? $query->query['post_type'] : array( $query->query['post_type'] );
463
464
			if ( ! in_array( 'give_payment', $post_types ) || is_array( $query->query['orderby'] ) ) {
465 52
				return $order;
466 52
			}
467 52
468 52
			global $wpdb;
469 52
			switch ( $query->query['orderby'] ) {
470 52
				case 'post_status':
471
					$order = $wpdb->posts . '.post_status ' . strtoupper( $query->query['order'] );
472
					break;
473
			}
474
		}
475
476
		return $order;
477
	}
478
479 52
	/**
480
	 * Specific User
481 52
	 *
482 52
	 * @since  1.0
483
	 * @access public
484
	 *
485
	 * @return void
486
	 */
487
	public function user() {
488
		if ( is_null( $this->args['user'] ) ) {
489
			return;
490
		}
491
492
		if ( is_numeric( $this->args['user'] ) ) {
493
			$user_key = '_give_payment_user_id';
494
		} else {
495
			$user_key = '_give_payment_user_email';
496
		}
497
498
		$this->__set(
499
			'meta_query', array(
500
				'key'   => $user_key,
501
				'value' => $this->args['user'],
502
			)
503
		);
504
	}
505
506
	/**
507
	 * Specific donor id
508
	 *
509
	 * @access  public
510
	 * @since   1.8.9
511
	 * @return  void
512
	 */
513
	public function donor() {
514
		if ( is_null( $this->args['donor'] ) || ! is_numeric( $this->args['donor'] ) ) {
515
			return;
516
		}
517
518
		$this->__set(
519
			'meta_query', array(
520
				'key'   => '_give_payment_customer_id',
521
				'value' => (int) $this->args['donor'],
522
			)
523
		);
524
	}
525
526
	/**
527
	 * Search
528
	 *
529
	 * @since  1.0
530
	 * @access public
531
	 *
532
	 * @return void
533
	 */
534
	public function search() {
535
536
		if ( ! isset( $this->args['s'] ) ) {
537
			return;
538
		}
539
540
		$search = trim( $this->args['s'] );
541
542
		if ( empty( $search ) ) {
543
			return;
544
		}
545
546
		$is_email = is_email( $search ) || strpos( $search, '@' ) !== false;
547
		$is_user  = strpos( $search, strtolower( 'user:' ) ) !== false;
548
549
		if ( ! empty( $this->args['search_in_notes'] ) ) {
550
551
			$notes = give_get_payment_notes( 0, $search );
552
553
			if ( ! empty( $notes ) ) {
554
555
				$payment_ids = wp_list_pluck( (array) $notes, 'comment_post_ID' );
556
557
				$this->__set( 'post__in', $payment_ids );
558
			}
559
560
			$this->__unset( 's' );
561
562
		} elseif ( $is_email || strlen( $search ) == 32 ) {
563
564
			$key         = $is_email ? '_give_payment_user_email' : '_give_payment_purchase_key';
565
			$search_meta = array(
566
				'key'     => $key,
567
				'value'   => $search,
568
				'compare' => 'LIKE',
569
			);
570
571
			$this->__set( 'meta_query', $search_meta );
572
			$this->__unset( 's' );
573
574
		} elseif ( $is_user ) {
575
576
			$search_meta = array(
577
				'key'   => '_give_payment_user_id',
578
				'value' => trim( str_replace( 'user:', '', strtolower( $search ) ) ),
579
			);
580
581
			$this->__set( 'meta_query', $search_meta );
582
583
			if ( give_get_option( 'enable_sequential' ) ) {
584
585
				$search_meta = array(
586
					'key'     => '_give_payment_number',
587
					'value'   => $search,
588
					'compare' => 'LIKE',
589
				);
590
591
				$this->__set( 'meta_query', $search_meta );
592
593
				$this->args['meta_query']['relation'] = 'OR';
594
595
			}
596
597
			$this->__unset( 's' );
598
599
		} elseif (
600
			give_get_option( 'enable_sequential' ) &&
601
			(
602
				false !== strpos( $search, give_get_option( 'sequential_prefix' ) ) ||
603
				false !== strpos( $search, give_get_option( 'sequential_postfix' ) )
604
			)
605
		) {
606
607
			$search_meta = array(
608
				'key'     => '_give_payment_number',
609
				'value'   => $search,
610
				'compare' => 'LIKE',
611
			);
612
613
			$this->__set( 'meta_query', $search_meta );
614
			$this->__unset( 's' );
615
616
		} elseif ( is_numeric( $search ) ) {
617
618
			$post = get_post( $search );
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 {
635
			$this->__set( 's', $search );
636
637
		}
638
639
	}
640
641
	/**
642
	 * Payment Mode
643
	 *
644
	 * @since  1.0
645
	 * @access public
646
	 *
647
	 * @return void
648
	 */
649
	public function mode() {
650
		if ( empty( $this->args['mode'] ) || $this->args['mode'] == 'all' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
651
			$this->__unset( 'mode' );
652
653
			return;
654
		}
655
656
		$this->__set(
657
			'meta_query', array(
658
				'key'   => '_give_payment_mode',
659
				'value' => $this->args['mode'],
660
			)
661
		);
662
	}
663
664
	/**
665
	 * Children
666
	 *
667
	 * @since  1.0
668
	 * @access public
669
	 *
670
	 * @return void
671
	 */
672
	public function children() {
673
		if ( empty( $this->args['children'] ) ) {
674
			$this->__set( 'post_parent', 0 );
675
		}
676
		$this->__unset( 'children' );
677
	}
678
679
	/**
680
	 * Specific Give Form
681
	 *
682
	 * @since  1.0
683
	 * @access public
684
	 *
685
	 * @return void
686
	 */
687 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...
688
689
		if ( empty( $this->args['give_forms'] ) ) {
690
			return;
691
		}
692
693
		$compare = '=';
694
695
		if ( is_array( $this->args['give_forms'] ) ) {
696
			$compare = 'IN';
697
		}
698
699
		$this->__set(
700
			'meta_query', array(
701
				array(
702
					'key'     => '_give_payment_form_id',
703
					'value'   => $this->args['give_forms'],
704
					'compare' => $compare,
705
				),
706
			)
707
		);
708
709
		$this->__unset( 'give_forms' );
710
711
	}
712
713
	/**
714
	 * Specific Gateway
715
	 *
716
	 * @since  1.8.17
717
	 * @access public
718
	 *
719
	 * @return void
720
	 */
721 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...
722
723
		if ( empty( $this->args['gateway'] ) ) {
724
			return;
725
		}
726
727
		$compare = '=';
728
729
		if ( is_array( $this->args['gateway'] ) ) {
730
			$compare = 'IN';
731
		}
732
733
		$this->__set(
734
			'meta_query', array(
735
				array(
736
					'key'     => '_give_payment_gateway',
737
					'value'   => $this->args['gateway'],
738
					'compare' => $compare,
739
				),
740
			)
741
		);
742
743
		$this->__unset( 'gateway' );
744
745
	}
746
747
748
	/**
749
	 * Get sql query
750
	 *
751
	 * Note: Internal purpose only. We are developing on this fn.
752
	 *
753
	 * @since  1.8.18
754
	 * @access public
755
	 * @global $wpdb
756
	 *
757
	 * @return string
758
	 */
759
	private function get_sql() {
760
		global $wpdb;
761
762
		$where = "WHERE {$wpdb->posts}.post_type = 'give_payment'";
763
		$where .= " AND {$wpdb->posts}.post_status IN ('" . implode( "','", $this->args['post_status'] ) . "')";
764
765
		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...
766
			$where .= " AND {$wpdb->posts}.post_parent={$this->args['post_parent']}";
767
		}
768
769
		// Set orderby.
770
		$orderby  = "ORDER BY {$wpdb->posts}.{$this->args['orderby']}";
771
		$group_by = '';
772
773
		// Set group by.
774 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...
775
			$group_by = "GROUP BY {$wpdb->posts}.{$this->args['group_by']}";
776
		}
777
778
		// Set offset.
779
		if (
780
			empty( $this->args['nopaging'] ) &&
781
			empty( $this->args['offset'] ) &&
782
			( ! empty( $this->args['page'] ) && 0 < $this->args['page'] )
783
		) {
784
			$this->args['offset'] = $this->args['posts_per_page'] * ( $this->args['page'] - 1 );
785
		}
786
787
		// Set fields.
788
		$fields = "{$wpdb->posts}.*";
789
		if ( ! empty( $this->args['fields'] ) && 'all' !== $this->args['fields'] ) {
790
			if ( is_string( $this->args['fields'] ) ) {
791
				$fields = "{$wpdb->posts}.{$this->args['fields']}";
792
			} elseif ( is_array( $this->args['fields'] ) ) {
793
				$fields = "{$wpdb->posts}." . implode( " , {$wpdb->posts}.", $this->args['fields'] );
794
			}
795
		}
796
797
		// Set count.
798
		if ( ! empty( $this->args['count'] ) ) {
799
			$fields = "COUNT({$wpdb->posts}.ID)";
800
801 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...
802
				$fields = "{$wpdb->posts}.{$this->args['group_by']}, {$fields}";
803
			}
804
		}
805
806
		// Date query.
807
		if ( ! empty( $this->args['date_query'] ) ) {
808
			$date_query_obj = new WP_Date_Query( $this->args['date_query'] );
809
			$where          .= str_replace(
810
				array(
811
					"\n",
812
					'(   (',
813
					'))',
814
				),
815
				array(
816
					'',
817
					'( (',
818
					') )',
819
				),
820
				$date_query_obj->get_sql()
821
			);
822
		}
823
824
		// Meta query.
825
		if ( ! empty( $this->args['meta_query'] ) ) {
826
			$meta_query_obj = new WP_Meta_Query( $this->args['meta_query'] );
827
			$where          = implode( ' ', $meta_query_obj->get_sql( 'post', $wpdb->posts, 'ID' ) ) . " {$where}";
828
		}
829
830
		// Set sql query.
831
		$sql = $wpdb->prepare(
832
			"SELECT {$fields} FROM {$wpdb->posts} LIMIT %d,%d;",
833
			absint( $this->args['offset'] ),
834
			( empty( $this->args['nopaging'] ) ? absint( $this->args['posts_per_page'] ) : 999999999999999 )
835
		);
836
837
		// $where, $orderby and order already prepared query they can generate notice if you re prepare them in above.
838
		// WordPress consider LIKE condition as placeholder if start with s,f, or d.
839
		$sql = str_replace( 'LIMIT', "{$where} {$group_by} {$orderby} {$this->args['order']} LIMIT", $sql );
840
841
		return $sql;
842
	}
843
844
}
845