Test Failed
Push — issues/1944 ( 647dde...ea2ff6 )
by Ravinder
04:42
created

Give_Payments_Query::get_sql()   D

Complexity

Conditions 15
Paths 192

Size

Total Lines 81
Code Lines 45

Duplication

Lines 9
Ratio 11.11 %

Importance

Changes 0
Metric Value
cc 15
eloc 45
nc 192
nop 0
dl 9
loc 81
rs 4.668
c 0
b 0
f 0

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