Completed
Push — issues/1837 ( de81e4 )
by Ravinder
20:07
created

Give_Payments_Query::donor()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 26 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

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