Test Failed
Push — release/1.8.12 ( b58a2f...d255b1 )
by Ravinder
375:09 queued 372:17
created

includes/payments/class-payments-query.php (7 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
	 * The args to pass to the give_get_payments() query
30
	 *
31
	 * @since  1.0
32
	 * @access public
33
	 *
34
	 * @var    array
35
	 */
36
	public $args = array();
37
38
	/**
39
	 * The payments found based on the criteria set
40
	 *
41
	 * @since  1.0
42
	 * @access public
43
	 *
44
	 * @var    array
45
	 */
46
	public $payments = array();
47
48
	/**
49
	 * Default query arguments.
50
	 *
51
	 * Not all of these are valid arguments that can be passed to WP_Query. The ones that are not, are modified before the query is run to convert them to the proper syntax.
52
	 *
53
	 * @since  1.0
54
	 * @access public
55
	 *
56
	 * @param  $args array The array of arguments that can be passed in and used for setting up this payment query.
57 52
	 */
58
	public function __construct( $args = array() ) {
59 52
		$defaults = array(
60 52
			'output'          => 'payments',
61 52
			'post_type'       => array( 'give_payment' ),
62 52
			'start_date'      => false,
63 52
			'end_date'        => false,
64 52
			'number'          => 20,
65 52
			'page'            => null,
66 52
			'orderby'         => 'ID',
67 52
			'order'           => 'DESC',
68 52
			'user'            => null,
69 52
			'donor'           => null,
70 52
			'status'          => give_get_payment_status_keys(),
71 52
			'meta_key'        => null,
0 ignored issues
show
Detected usage of meta_key, possible slow query.
Loading history...
72 52
			'year'            => null,
73 52
			'month'           => null,
74 52
			'day'             => null,
75 52
			's'               => null,
76 52
			'search_in_notes' => false,
77
			'children'        => false,
78 52
			'fields'          => null,
79
			'give_forms'      => null,
80 52
		);
81
82 52
		$this->args = wp_parse_args( $args, $defaults );
83 52
84
		$this->init();
85
	}
86
87
	/**
88
	 * Set a query variable.
89
	 *
90
	 * @since  1.0
91 52
	 * @access public
92 52
	 *
93 32
	 * @param $query_var
94 32
	 * @param $value
95 52
	 */
96
	public function __set( $query_var, $value ) {
97 52
		if ( in_array( $query_var, array( 'meta_query', 'tax_query' ) ) ) {
98
			$this->args[ $query_var ][] = $value;
99
		} else {
100
			$this->args[ $query_var ] = $value;
101
		}
102
	}
103
104
	/**
105 52
	 * Unset a query variable.
106 52
	 *
107 52
	 * @since  1.0
108
	 * @access public
109
	 *
110
	 * @param $query_var
111
	 */
112
	public function __unset( $query_var ) {
113
		unset( $this->args[ $query_var ] );
114
	}
115
116 52
	/**
117
	 * Modify the query/query arguments before we retrieve payments.
118 52
	 *
119 52
	 * @since  1.0
120
	 * @access public
121 52
	 *
122 52
	 * @return void
123 52
	 */
124 52
	public function init() {
125 52
	}
126 52
127 52
128 52
	/**
129 52
	 * Set query filter.
130 52
	 *
131 52
	 * @since  1.8.9
132
	 * @access private
133
	 */
134
	private function set_filters() {
135
		$this->date_filter_pre();
136
		$this->orderby();
137
		$this->status();
138
		$this->month();
139
		$this->per_page();
140
		$this->page();
141
		$this->user();
142
		$this->donor();
143
		$this->search();
144 52
		$this->mode();
145
		$this->children();
146 52
		$this->give_forms();
147
148 52
		add_filter( 'posts_orderby', array( $this, 'custom_orderby' ), 10, 2 );
149
	}
150
151 52
	/**
152 52
	 * Unset query filter.
153 52
	 *
154
	 * @since  1.8.9
155 52
	 * @access private
156 52
	 */
157
	private function unset_filters() {
158
		$this->date_filter_post();
159
		remove_filter( 'posts_orderby', array( $this, 'custom_orderby' ) );
160
	}
161
162
163
	/**
164
	 * Retrieve payments.
165
	 *
166
	 * The query can be modified in two ways; either the action before the
167
	 * query is run, or the filter on the arguments (existing mainly for backwards
168
	 * compatibility).
169
	 *
170
	 * @since  1.0
171
	 * @access public
172
	 *
173
	 * @return array
174
	 */
175
	public function get_payments() {
176
		// Modify the query/query arguments before we retrieve payments.
177
		$this->set_filters();
178
179
		/**
180
		 * Fires before retrieving payments.
181
		 *
182
		 * @since 1.0
183
		 *
184 52
		 * @param Give_Payments_Query $this Payments query object.
185 52
		 */
186 52
		do_action( 'give_pre_get_payments', $this );
187
188
		$query = new WP_Query( $this->args );
189
190
		$custom_output = array(
191
			'payments',
192
			'give_payments',
193
		);
194
195
		if ( ! in_array( $this->args['output'], $custom_output ) ) {
196
			return $query->posts;
197
		}
198
199
		if ( $query->have_posts() ) {
200
			while ( $query->have_posts() ) {
201
				$query->the_post();
202
203
				$payment_id = get_post()->ID;
204
				$payment    = new Give_Payment( $payment_id );
205
206
				$this->payments[] = apply_filters( 'give_payment', $payment, $payment_id, $this );
207
			}
208
209
			wp_reset_postdata();
210
		}
211
0 ignored issues
show
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
212
213
		// Remove query filters after we retrieve payments.
214
		$this->unset_filters();
215
216
		/**
217 52
		 * Fires after retrieving payments.
218 52
		 *
219 42
		 * @since 1.0
220
		 *
221
		 * @param Give_Payments_Query $this Payments query object.
222 52
		 */
223 52
		do_action( 'give_post_get_payments', $this );
224 52
225
		return $this->payments;
226
	}
227
228
	/**
229
	 * If querying a specific date, add the proper filters.
230
	 *
231
	 * @since  1.0
232
	 * @access public
233 52
	 *
234 52
	 * @return void
235 52
	 */
236
	public function date_filter_pre() {
237
		if ( ! ( $this->args['start_date'] || $this->args['end_date'] ) ) {
238 11
			return;
239 11
		}
240 11
241
		$this->setup_dates( $this->args['start_date'], $this->args['end_date'] );
242
243
		add_filter( 'posts_where', array( $this, 'payments_where' ) );
244
	}
245
246
	/**
247
	 * If querying a specific date, remove filters after the query has been run
248
	 * to avoid affecting future queries.
249 52
	 *
250
	 * @since  1.0
251 52
	 * @access public
252 42
	 *
253
	 * @return void
254
	 */
255 52
	public function date_filter_post() {
256 42
		if ( ! ( $this->args['start_date'] || $this->args['end_date'] ) ) {
257 42
			return;
258 52
		}
259
260
		remove_filter( 'posts_where', array( $this, 'payments_where' ) );
261 52
	}
262 52
263
	/**
264
	 * Post Status
265
	 *
266
	 * @since  1.0
267
	 * @access public
268
	 *
269
	 * @return void
270
	 */
271 52
	public function status() {
272 52
		if ( ! isset ( $this->args['status'] ) ) {
0 ignored issues
show
Space before opening parenthesis of function call prohibited
Loading history...
273 52
			return;
274
		}
275
276
		$this->__set( 'post_status', $this->args['status'] );
277
		$this->__unset( 'status' );
278
	}
279
280
	/**
281
	 * Current Page
282
	 *
283
	 * @since  1.0
284
	 * @access public
285
	 *
286
	 * @return void
287 52
	 */
288 52
	public function page() {
289 52
		if ( ! isset ( $this->args['page'] ) ) {
0 ignored issues
show
Space before opening parenthesis of function call prohibited
Loading history...
290
			return;
291
		}
292
293 52
		$this->__set( 'paged', $this->args['page'] );
294 52
		$this->__unset( 'page' );
295 52
	}
296 52
297 52
	/**
298
	 * Posts Per Page
299
	 *
300
	 * @since  1.0
301
	 * @access public
302
	 *
303
	 * @return void
304
	 */
305
	public function per_page() {
306 52
307 52
		if ( ! isset( $this->args['number'] ) ) {
308 52
			return;
309
		}
310
311
		if ( $this->args['number'] == - 1 ) {
312
			$this->__set( 'nopaging', true );
313
		} else {
314
			$this->__set( 'posts_per_page', $this->args['number'] );
315
		}
316
317
		$this->__unset( 'number' );
318
	}
319
320
	/**
321
	 * Current Month
322
	 *
323
	 * @since  1.0
324
	 * @access public
325
	 *
326
	 * @return void
327
	 */
328
	public function month() {
329
		if ( ! isset ( $this->args['month'] ) ) {
0 ignored issues
show
Space before opening parenthesis of function call prohibited
Loading history...
330 52
			return;
331
		}
332 52
333 52
		$this->__set( 'monthnum', $this->args['month'] );
334
		$this->__unset( 'month' );
335
	}
336 32
337
	/**
338 32
	 * Order by
339
	 *
340
	 * @since  1.0
341
	 * @access public
342 32
	 *
343 32
	 * @return void
344
	 */
345 32
	public function orderby() {
346
		switch ( $this->args['orderby'] ) {
347
			case 'amount' :
348
				$this->__set( 'orderby', 'meta_value_num' );
349
				$this->__set( 'meta_key', '_give_payment_total' );
350
				break;
351
352
			case 'status' :
353
				$this->__set( 'orderby', 'post_status' );
354
				break;
355
356
			case 'donation_form' :
357
				$this->__set( 'orderby', 'meta_value' );
358 32
				$this->__set( 'meta_key', '_give_payment_form_title' );
359
				break;
360 32
361
			default :
362 32
				$this->__set( 'orderby', $this->args['orderby'] );
363 32
				break;
364
		}
365 32
	}
366
367 32
	/**
368 32
	 * Custom orderby.
369
	 * Note: currently custom sorting is only used for donation listing page.
370 32
	 *
371
	 * @since  1.8
372
	 * @access public
373
	 *
374
	 * @param string   $order
375
	 * @param WP_Query $query
376
	 *
377
	 * @return mixed
378
	 */
379
	public function custom_orderby( $order, $query ) {
380
		global $wpdb;
381
382
		$post_types = is_array( $query->query['post_type'] ) ? $query->query['post_type'] : array( $query->query['post_type'] );
383
		if ( ! in_array( 'give_payment', $post_types ) || is_array( $query->query['orderby'] ) ) {
384
			return $order;
385
		}
386
387
		switch ( $query->query['orderby'] ) {
388
			case 'post_status':
389
				$order = $wpdb->posts .'.post_status ' . strtoupper( $query->query['order'] );
390
				break;
391
		}
392
393
		return $order;
394
	}
395
396
	/**
397
	 * Specific User
398
	 *
399
	 * @since  1.0
400
	 * @access public
401
	 *
402
	 * @return void
403
	 */
404
	public function user() {
405
		if ( is_null( $this->args['user'] ) ) {
406
			return;
407
		}
408
409
		if ( is_numeric( $this->args['user'] ) ) {
410
			$user_key = '_give_payment_user_id';
411
		} else {
412
			$user_key = '_give_payment_user_email';
413
		}
414
415
		$this->__set( 'meta_query', array(
416
			'key'   => $user_key,
417
			'value' => $this->args['user'],
418
		) );
419
	}
420
421
	/**
422
	 * Specific donor id
423
	 *
424
	 * @access  public
425
	 * @since   1.8.9
426
	 * @return  void
427
	 */
428
	public function donor() {
429
		if ( is_null( $this->args['donor'] ) || ! is_numeric( $this->args['donor'] ) ) {
430
			return;
431
		}
432
433
		$this->__set( 'meta_query', array(
434
			'key'   => '_give_payment_customer_id',
435
			'value' => (int) $this->args['donor'],
436 32
		) );
437
	}
438
439
	/**
440
	 * Search
441
	 *
442
	 * @since  1.0
443
	 * @access public
444
	 *
445 52
	 * @return void
446 52
	 */
447 52
	public function search() {
448
449 52
		if ( ! isset( $this->args['s'] ) ) {
450
			return;
451
		}
452
453
		$search = trim( $this->args['s'] );
454
455
		if ( empty( $search ) ) {
456
			return;
457
		}
458
459
		$is_email = is_email( $search ) || strpos( $search, '@' ) !== false;
460
		$is_user  = strpos( $search, strtolower( 'user:' ) ) !== false;
461
462
		if ( ! empty( $this->args['search_in_notes'] ) ) {
463
464
			$notes = give_get_payment_notes( 0, $search );
465 52
466 52
			if ( ! empty( $notes ) ) {
467 52
468 52
				$payment_ids = wp_list_pluck( (array) $notes, 'comment_post_ID' );
469 52
470 52
				$this->__set( 'post__in', $payment_ids );
471
			}
472
473
			$this->__unset( 's' );
474
475
		} elseif ( $is_email || strlen( $search ) == 32 ) {
476
477
			$key         = $is_email ? '_give_payment_user_email' : '_give_payment_purchase_key';
478
			$search_meta = array(
479 52
				'key'     => $key,
480
				'value'   => $search,
481 52
				'compare' => 'LIKE',
482 52
			);
483
484
			$this->__set( 'meta_query', $search_meta );
485
			$this->__unset( 's' );
486
487
		} elseif ( $is_user ) {
488
489
			$search_meta = array(
490
				'key'   => '_give_payment_user_id',
491
				'value' => trim( str_replace( 'user:', '', strtolower( $search ) ) ),
492
			);
493
494
			$this->__set( 'meta_query', $search_meta );
495
496
			if ( give_get_option( 'enable_sequential' ) ) {
497
498
				$search_meta = array(
499
					'key'     => '_give_payment_number',
500
					'value'   => $search,
501
					'compare' => 'LIKE',
502
				);
503
504
				$this->__set( 'meta_query', $search_meta );
505
506
				$this->args['meta_query']['relation'] = 'OR';
507
508
			}
509
510
			$this->__unset( 's' );
511
512
		} elseif (
513
			give_get_option( 'enable_sequential' ) &&
514
			(
515
				false !== strpos( $search, give_get_option( 'sequential_prefix' ) ) ||
516
				false !== strpos( $search, give_get_option( 'sequential_postfix' ) )
517
			)
518
		) {
519
520
			$search_meta = array(
521
				'key'     => '_give_payment_number',
522
				'value'   => $search,
523
				'compare' => 'LIKE',
524
			);
525
526
			$this->__set( 'meta_query', $search_meta );
527
			$this->__unset( 's' );
528
529
		} elseif ( is_numeric( $search ) ) {
530
531
			$post = get_post( $search );
532
533
			if ( is_object( $post ) && $post->post_type == 'give_payment' ) {
0 ignored issues
show
Found "== '". Use Yoda Condition checks, you must
Loading history...
534
535
				$arr   = array();
536
				$arr[] = $search;
537
				$this->__set( 'post__in', $arr );
538
				$this->__unset( 's' );
539
			}
540
		} elseif ( '#' == substr( $search, 0, 1 ) ) {
541
542
			$search = str_replace( '#:', '', $search );
543
			$search = str_replace( '#', '', $search );
544
			$this->__set( 'give_forms', $search );
545
			$this->__unset( 's' );
546
547
		} else {
548
			$this->__set( 's', $search );
549
550
		}
551
552
	}
553
554
	/**
555
	 * Payment Mode
556
	 *
557
	 * @since  1.0
558
	 * @access public
559
	 *
560
	 * @return void
561
	 */
562
	public function mode() {
563
		if ( empty( $this->args['mode'] ) || $this->args['mode'] == 'all' ) {
0 ignored issues
show
Found "== '". Use Yoda Condition checks, you must
Loading history...
564
			$this->__unset( 'mode' );
565
566
			return;
567
		}
568
569
		$this->__set( 'meta_query', array(
570
			'key'   => '_give_payment_mode',
571
			'value' => $this->args['mode'],
572
		) );
573
	}
574
575
	/**
576
	 * Children
577
	 *
578
	 * @since  1.0
579
	 * @access public
580
	 *
581
	 * @return void
582
	 */
583
	public function children() {
584
		if ( empty( $this->args['children'] ) ) {
585
			$this->__set( 'post_parent', 0 );
586
		}
587
		$this->__unset( 'children' );
588
	}
589
590
	/**
591
	 * Specific Give Form
592
	 *
593
	 * @since  1.0
594
	 * @access public
595
	 *
596
	 * @return void
597
	 */
598
	public function give_forms() {
599
600
		if ( empty( $this->args['give_forms'] ) ) {
601
			return;
602
		}
603
604
		$compare = '=';
605
606
		if ( is_array( $this->args['give_forms'] ) ) {
607
			$compare = 'IN';
608
		}
609
610
		$this->__set( 'meta_query', array(
611
			array(
612
				'key'     => '_give_payment_form_id',
613
				'value'   => $this->args['give_forms'],
614
				'compare' => $compare,
615
			),
616
		) );
617
618
		$this->__unset( 'give_forms' );
619
620
	}
621
622
}
623