Completed
Pull Request — master (#664)
by Devin
19:01
created

Give_Payment_History_Table::column_default()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 36
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 10
eloc 28
c 2
b 0
f 1
nc 10
nop 2
dl 0
loc 36
rs 4.8196

How to fix   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
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 29 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
 * Payment History Table Class
4
 *
5
 * @package     Give
6
 * @subpackage  Admin/Payments
7
 * @copyright   Copyright (c) 2016, Give
8
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
// Load WP_List_Table if not loaded
18
if ( ! class_exists( 'WP_List_Table' ) ) {
19
	require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
20
}
21
22
/**
23
 * Give_Payment_History_Table Class
24
 *
25
 * Renders the Payment History table on the Payment History page
26
 *
27
 * @since 1.0
28
 */
29
class Give_Payment_History_Table extends WP_List_Table {
30
31
	/**
32
	 * Number of results to show per page
33
	 *
34
	 * @var string
35
	 * @since 1.0
36
	 */
37
	public $per_page = 30;
38
39
	/**
40
	 * URL of this page
41
	 *
42
	 * @var string
43
	 * @since 1.0.1
44
	 */
45
	public $base_url;
46
47
	/**
48
	 * Total number of payments
49
	 *
50
	 * @var int
51
	 * @since 1.0
52
	 */
53
	public $total_count;
54
55
	/**
56
	 * Total number of complete payments
57
	 *
58
	 * @var int
59
	 * @since 1.0
60
	 */
61
	public $complete_count;
62
63
	/**
64
	 * Total number of pending payments
65
	 *
66
	 * @var int
67
	 * @since 1.0
68
	 */
69
	public $pending_count;
70
71
	/**
72
	 * Total number of refunded payments
73
	 *
74
	 * @var int
75
	 * @since 1.0
76
	 */
77
	public $refunded_count;
78
79
	/**
80
	 * Total number of failed payments
81
	 *
82
	 * @var int
83
	 * @since 1.0
84
	 */
85
	public $failed_count;
86
87
	/**
88
	 * Total number of revoked payments
89
	 *
90
	 * @var int
91
	 * @since 1.0
92
	 */
93
	public $revoked_count;
94
95
	/**
96
	 * Total number of cancelled payments
97
	 *
98
	 * @var int
99
	 * @since 1.4
100
	 */
101
	public $cancelled_count;
102
103
	/**
104
	 * Total number of abandoned payments
105
	 *
106
	 * @var int
107
	 * @since 1.6
108
	 */
109
	public $abandoned_count;
110
111
	/**
112
	 * Get things started
113
	 *
114
	 * @since 1.0
115
	 * @uses  Give_Payment_History_Table::get_payment_counts()
116
	 * @see   WP_List_Table::__construct()
117
	 */
118
	public function __construct() {
119
120
		global $status, $page;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
121
122
		// Set parent defaults
123
		parent::__construct( array(
124
			'singular' => give_get_forms_label_singular(),    // Singular name of the listed records
125
			'plural'   => give_get_forms_label_plural(),        // Plural name of the listed records
126
			'ajax'     => false                        // Does this table support ajax?
127
		) );
128
129
		$this->get_payment_counts();
130
		$this->process_bulk_action();
131
		$this->base_url = admin_url( 'edit.php?post_type=give_forms&page=give-payment-history' );
132
	}
133
134
	public function advanced_filters() {
135
		$start_date = isset( $_GET['start-date'] ) ? sanitize_text_field( $_GET['start-date'] ) : null;
136
		$end_date   = isset( $_GET['end-date'] ) ? sanitize_text_field( $_GET['end-date'] ) : null;
137
		$status     = isset( $_GET['status'] ) ? $_GET['status'] : '';
138
		?>
139
		<div id="give-payment-filters">
140
			<span id="give-payment-date-filters">
141
				<label for="start-date" class="give-start-date-label"><?php _e( 'Start Date:', 'give' ); ?></label>
142
				<input type="text" id="start-date" name="start-date" class="give_datepicker" value="<?php echo $start_date; ?>" placeholder="mm/dd/yyyy"/>
143
				<label for="end-date" class="give-end-date-label"><?php _e( 'End Date:', 'give' ); ?></label>
144
				<input type="text" id="end-date" name="end-date" class="give_datepicker" value="<?php echo $end_date; ?>" placeholder="mm/dd/yyyy"/>
145
				<input type="submit" class="button-secondary" value="<?php _e( 'Apply', 'give' ); ?>"/>
146
			</span>
147
			<?php if ( ! empty( $status ) ) : ?>
148
				<input type="hidden" name="status" value="<?php echo esc_attr( $status ); ?>"/>
149
			<?php endif; ?>
150
			<?php if ( ! empty( $start_date ) || ! empty( $end_date ) ) : ?>
151
				<a href="<?php echo admin_url( 'edit.php?post_type=give_forms&page=give-payment-history' ); ?>" class="button-secondary"><?php _e( 'Clear Filter', 'give' ); ?></a>
152
			<?php endif; ?>
153
			<?php $this->search_box( __( 'Search', 'give' ), 'give-payments' ); ?>
154
		</div>
155
156
		<?php
157
	}
158
159
	/**
160
	 * Show the search field
161
	 *
162
	 * @since  1.0
163
	 * @access public
164
	 *
165
	 * @param string $text Label for the search box
166
	 * @param string $input_id ID of the search box
167
	 *
168
	 * @return void
169
	 */
170
	public function search_box( $text, $input_id ) {
171
		if ( empty( $_REQUEST['s'] ) && ! $this->has_items() ) {
172
			return;
173
		}
174
175
		$input_id = $input_id . '-search-input';
176
177
		if ( ! empty( $_REQUEST['orderby'] ) ) {
178
			echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
179
		}
180
		if ( ! empty( $_REQUEST['order'] ) ) {
181
			echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
182
		}
183
		?>
184
		<p class="search-box">
185
			<?php do_action( 'give_payment_history_search' ); ?>
186
			<label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label>
187
			<input type="search" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>"/>
188
			<?php submit_button( $text, 'button', false, false, array( 'ID' => 'search-submit' ) ); ?><br/>
189
		</p>
190
		<?php
191
	}
192
193
	/**
194
	 * Retrieve the view types
195
	 *
196
	 * @access public
197
	 * @since  1.0
198
	 * @return array $views All the views available
199
	 */
200
	public function get_views() {
201
202
		$current         = isset( $_GET['status'] ) ? $_GET['status'] : '';
203
		$total_count     = '&nbsp;<span class="count">(' . $this->total_count . ')</span>';
204
		$complete_count  = '&nbsp;<span class="count">(' . $this->complete_count . ')</span>';
205
		$cancelled_count = '&nbsp;<span class="count">(' . $this->cancelled_count . ')</span>';
206
		$pending_count   = '&nbsp;<span class="count">(' . $this->pending_count . ')</span>';
207
		$refunded_count  = '&nbsp;<span class="count">(' . $this->refunded_count . ')</span>';
208
		$failed_count    = '&nbsp;<span class="count">(' . $this->failed_count . ')</span>';
209
		$abandoned_count = '&nbsp;<span class="count">(' . $this->abandoned_count . ')</span>';
210
		$revoked_count   = '&nbsp;<span class="count">(' . $this->revoked_count . ')</span>';
211
212
		$views = array(
213
			'all'       => sprintf( '<a href="%s"%s>%s</a>', remove_query_arg( array(
214
				'status',
215
				'paged'
216
			) ), $current === 'all' || $current == '' ? ' class="current"' : '', __( 'All', 'give' ) . $total_count ),
217
			'publish'   => sprintf( '<a href="%s"%s>%s</a>', esc_url( add_query_arg( array(
218
				'status' => 'publish',
219
				'paged'  => false
220
			) ) ), $current === 'publish' ? ' class="current"' : '', __( 'Completed', 'give' ) . $complete_count ),
221
			'pending'   => sprintf( '<a href="%s"%s>%s</a>', esc_url( add_query_arg( array(
222
				'status' => 'pending',
223
				'paged'  => false
224
			) ) ), $current === 'pending' ? ' class="current"' : '', __( 'Pending', 'give' ) . $pending_count ),
225
			'refunded'  => sprintf( '<a href="%s"%s>%s</a>', esc_url( add_query_arg( array(
226
				'status' => 'refunded',
227
				'paged'  => false
228
			) ) ), $current === 'refunded' ? ' class="current"' : '', __( 'Refunded', 'give' ) . $refunded_count ),
229
			'revoked'   => sprintf( '<a href="%s"%s>%s</a>', esc_url( add_query_arg( array(
230
				'status' => 'revoked',
231
				'paged'  => false
232
			) ) ), $current === 'revoked' ? ' class="current"' : '', __( 'Revoked', 'give' ) . $revoked_count ),
233
			'failed'    => sprintf( '<a href="%s"%s>%s</a>', esc_url( add_query_arg( array(
234
				'status' => 'failed',
235
				'paged'  => false
236
			) ) ), $current === 'failed' ? ' class="current"' : '', __( 'Failed', 'give' ) . $failed_count ),
237
			'cancelled' => sprintf( '<a href="%s"%s>%s</a>', esc_url( add_query_arg( array(
238
				'status' => 'cancelled',
239
				'paged'  => false
240
			) ) ), $current === 'cancelled' ? ' class="current"' : '', __( 'Cancelled', 'give' ) . $cancelled_count ),
241
			'abandoned' => sprintf( '<a href="%s"%s>%s</a>', esc_url( add_query_arg( array(
242
				'status' => 'abandoned',
243
				'paged'  => false
244
			) ) ), $current === 'abandoned' ? ' class="current"' : '', __( 'Abandoned', 'give' ) . $abandoned_count )
245
		);
246
247
		return apply_filters( 'give_payments_table_views', $views );
248
	}
249
250
	/**
251
	 * Retrieve the table columns
252
	 *
253
	 * @access public
254
	 * @since  1.0
255
	 * @return array $columns Array of all the list table columns
256
	 */
257
	public function get_columns() {
258
		$columns = array(
259
			'cb'       => '<input type="checkbox" />', //Render a checkbox instead of text
260
			'email'    => __( 'Email', 'give' ),
261
			'details'  => __( 'Details', 'give' ),
262
			'amount'   => __( 'Amount', 'give' ),
263
			'donation' => __( 'Donation', 'give' ),
264
			'status'   => __( 'Status', 'give' ),
265
			'date'     => __( 'Date', 'give' ),
266
			'donor'    => __( 'Donor', 'give' ),
267
			'ID'       => __( 'ID', 'give' ),
268
		);
269
270
		return apply_filters( 'give_payments_table_columns', $columns );
271
	}
272
273
	/**
274
	 * Retrieve the table's sortable columns
275
	 *
276
	 * @access public
277
	 * @since  1.0
278
	 * @return array Array of all the sortable columns
279
	 */
280
	public function get_sortable_columns() {
281
		$columns = array(
282
			'ID'     => array( 'ID', true ),
283
			'amount' => array( 'amount', false ),
284
			'date'   => array( 'date', false )
285
		);
286
287
		return apply_filters( 'give_payments_table_sortable_columns', $columns );
288
	}
289
290
	/**
291
	 * Gets the name of the primary column.
292
	 *
293
	 * @since 1.5
294
	 * @access protected
295
	 *
296
	 * @return string Name of the primary column.
297
	 */
298
	protected function get_primary_column_name() {
299
		return 'ID';
300
	}
301
302
	/**
303
	 * This function renders most of the columns in the list table.
304
	 *
305
	 * @access public
306
	 * @since  1.0
307
	 *
308
	 * @param array $item Contains all the data of the discount code
0 ignored issues
show
Bug introduced by
There is no parameter named $item. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
309
	 * @param string $column_name The name of the column
310
	 *
311
	 * @return string Column Name
312
	 */
313
	public function column_default( $payment, $column_name ) {
314
		switch ( $column_name ) {
315
			case 'amount' :
316
				$amount = ! empty( $payment->total ) ? $payment->total : 0;
317
				$value  = give_currency_filter( give_format_amount( $amount ), give_get_payment_currency_code( $payment->ID ) );
318
				break;
319
			case 'donation' :
320
				$value = '<a href="' . get_permalink( $payment->form_id ) . '">' . $payment->form_title . '</a>';
321
				$level = give_get_payment_form_title( $payment->meta, true );
322
323
				if ( ! empty( $level ) ) {
324
					$value .= $level;
325
				}
326
327
				break;
328
			case 'date' :
329
				$date  = strtotime( $payment->date );
330
				$value = date_i18n( get_option( 'date_format' ), $date );
331
				break;
332
			case 'status' :
333
				$value = '<div class="give-donation-status status-' . sanitize_title( give_get_payment_status( $payment, true ) ) . '"><span class="give-donation-status-icon"></span> ' . give_get_payment_status( $payment, true ) . '</div>';
334
				if ( $payment->mode == 'test' ) {
335
					$value .= ' <span class="give-item-label give-item-label-orange give-test-mode-transactions-label" data-tooltip="' . __( 'This payment was made in test mode', 'give' ) . '">' . __( 'Test', 'give' ) . '</span>';
336
				}
337
				break;
338
			case 'details' :
339
				$value = '<div class="give-payment-details-link-wrap"><a href="' . esc_url( add_query_arg( 'id', $payment->ID, admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-order-details' ) ) ) . '" class="give-payment-details-link button button-small">' . __( 'View Details', 'give' ) . '</a></div>';
340
				break;
341
			default:
342
				$value = isset( $payment->$column_name ) ? $payment->$column_name : '';
343
				break;
344
345
		}
346
347
		return apply_filters( 'give_payments_table_column', $value, $payment->ID, $column_name );
348
	}
349
350
	/**
351
	 * Render the Email Column
352
	 *
353
	 * @access public
354
	 * @since  1.0
355
	 *
356
	 * @param array $payment Contains all the data of the payment
357
	 *
358
	 * @return string Data shown in the Email column
359
	 */
360
	public function column_email( $payment ) {
361
362
		$row_actions = array();
363
364
		$email = give_get_payment_user_email( $payment->ID );
365
366
		// Add search term string back to base URL
367
		$search_terms = ( isset( $_GET['s'] ) ? trim( $_GET['s'] ) : '' );
368
		if ( ! empty( $search_terms ) ) {
369
			$this->base_url = add_query_arg( 's', $search_terms, $this->base_url );
370
		}
371
372
		if ( give_is_payment_complete( $payment->ID ) && ! empty( $email ) ) {
373
			$row_actions['email_links'] = '<a href="' . add_query_arg( array(
374
					'give-action' => 'email_links',
375
					'purchase_id' => $payment->ID
376
				), $this->base_url ) . '">' . __( 'Resend Donation Receipt', 'give' ) . '</a>';
377
378
		}
379
380
		$row_actions['delete'] = '<a href="' . wp_nonce_url( add_query_arg( array(
381
				'give-action' => 'delete_payment',
382
				'purchase_id' => $payment->ID
383
			), $this->base_url ), 'give_payment_nonce' ) . '">' . __( 'Delete', 'give' ) . '</a>';
384
385
		$row_actions = apply_filters( 'give_payment_row_actions', $row_actions, $payment );
386
387
		if ( empty( $email ) ) {
388
			$email = __( '(unknown)', 'give' );
389
		}
390
391
		$value = '<span class="give-email-column-value">' . $email . '</span>' . $this->row_actions( $row_actions );
392
393
		return apply_filters( 'give_payments_table_column', $value, $payment->ID, 'email' );
394
	}
395
396
	/**
397
	 * Render the checkbox column
398
	 *
399
	 * @access public
400
	 * @since  1.0
401
	 *
402
	 * @param array $payment Contains all the data for the checkbox column
403
	 *
404
	 * @return string Displays a checkbox
405
	 */
406
	public function column_cb( $payment ) {
407
		return sprintf(
408
			'<input type="checkbox" name="%1$s[]" value="%2$s" />',
409
			'payment',
410
			$payment->ID
411
		);
412
	}
413
414
	/**
415
	 * Render the ID column
416
	 *
417
	 * @access public
418
	 * @since  1.0
419
	 *
420
	 * @param array $payment Contains all the data for the checkbox column
421
	 *
422
	 * @return string Displays a checkbox
423
	 */
424
	public function column_ID( $payment ) {
425
		return '<span class="give-payment-id">' . give_get_payment_number( $payment->ID ) . '</span>';
426
	}
427
428
	/**
429
	 * Render the User Column
430
	 *
431
	 * @access public
432
	 * @since  1.0
433
	 *
434
	 * @param array $payment Contains all the data of the payment
435
	 *
436
	 * @return string Data shown in the User column
437
	 */
438
	public function column_donor( $payment ) {
439
440
		$customer_id = give_get_payment_customer_id( $payment->ID );
441
442
		if ( ! empty( $customer_id ) ) {
443
			$customer = new Give_Customer( $customer_id );
444
			$value    = '<a href="' . esc_url( admin_url( "edit.php?post_type=give_forms&page=give-customers&view=overview&id=$customer_id" ) ) . '">' . $customer->name . '</a>';
445
		} else {
446
			$email = give_get_payment_user_email( $payment->ID );
447
			$value = '<a href="' . esc_url( admin_url( "edit.php?post_type=give_forms&page=give-payment-history&s=$email" ) ) . '">' . __( '(donor missing)', 'give' ) . '</a>';
448
		}
449
450
		return apply_filters( 'give_payments_table_column', $value, $payment->ID, 'donor' );
451
	}
452
453
	/**
454
	 * Retrieve the bulk actions
455
	 *
456
	 * @access public
457
	 * @since  1.0
458
	 * @return array $actions Array of the bulk actions
459
	 */
460
	public function get_bulk_actions() {
461
		$actions = array(
462
			'delete'               => __( 'Delete', 'give' ),
463
			'set-status-publish'   => __( 'Set To Completed', 'give' ),
464
			'set-status-pending'   => __( 'Set To Pending', 'give' ),
465
			'set-status-refunded'  => __( 'Set To Refunded', 'give' ),
466
			'set-status-revoked'   => __( 'Set To Revoked', 'give' ),
467
			'set-status-failed'    => __( 'Set To Failed', 'give' ),
468
			'set-status-cancelled' => __( 'Set To Cancelled', 'give' ),
469
			'set-status-abandoned' => __( 'Set To Abandoned', 'give' ),
470
			'resend-receipt'       => __( 'Resend Email Receipts', 'give' )
471
		);
472
473
		return apply_filters( 'give_payments_table_bulk_actions', $actions );
474
	}
475
476
	/**
477
	 * Process the bulk actions
478
	 *
479
	 * @access public
480
	 * @since  1.0
481
	 * @return void
482
	 */
483
	public function process_bulk_action() {
484
		$ids    = isset( $_GET['payment'] ) ? $_GET['payment'] : false;
485
		$action = $this->current_action();
486
487
		if ( ! is_array( $ids ) ) {
488
			$ids = array( $ids );
489
		}
490
491
		if ( empty( $action ) ) {
492
			return;
493
		}
494
495
		foreach ( $ids as $id ) {
496
497
			// Detect when a bulk action is being triggered...
498
			if ( 'delete' === $this->current_action() ) {
499
				give_delete_purchase( $id );
500
			}
501
502
			if ( 'set-status-publish' === $this->current_action() ) {
503
				give_update_payment_status( $id, 'publish' );
504
			}
505
506
			if ( 'set-status-pending' === $this->current_action() ) {
507
				give_update_payment_status( $id, 'pending' );
508
			}
509
510
			if ( 'set-status-refunded' === $this->current_action() ) {
511
				give_update_payment_status( $id, 'refunded' );
512
			}
513
514
			if ( 'set-status-revoked' === $this->current_action() ) {
515
				give_update_payment_status( $id, 'revoked' );
516
			}
517
518
			if ( 'set-status-failed' === $this->current_action() ) {
519
				give_update_payment_status( $id, 'failed' );
520
			}
521
522
			if ( 'set-status-cancelled' === $this->current_action() ) {
523
				give_update_payment_status( $id, 'cancelled' );
524
			}
525
526
			if ( 'set-status-abandoned' === $this->current_action() ) {
527
				give_update_payment_status( $id, 'abandoned' );
528
			}
529
530
			if ( 'set-status-preapproval' === $this->current_action() ) {
531
				give_update_payment_status( $id, 'preapproval' );
532
			}
533
534
			if ( 'resend-receipt' === $this->current_action() ) {
535
				give_email_donation_receipt( $id, false );
536
			}
537
538
			do_action( 'give_payments_table_do_bulk_action', $id, $this->current_action() );
539
		}
540
541
	}
542
543
	/**
544
	 * Retrieve the payment counts
545
	 *
546
	 * @access public
547
	 * @since  1.0
548
	 * @return void
549
	 */
550
	public function get_payment_counts() {
551
552
		global $wp_query;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
553
554
		$args = array();
555
556
		if ( isset( $_GET['user'] ) ) {
557
			$args['user'] = urldecode( $_GET['user'] );
558
		} elseif ( isset( $_GET['s'] ) ) {
559
			$is_user = strpos( $_GET['s'], strtolower( 'user:' ) ) !== false;
560
			if ( $is_user ) {
561
				$args['user'] = absint( trim( str_replace( 'user:', '', strtolower( $_GET['s'] ) ) ) );
562
				unset( $args['s'] );
563
			} else {
564
				$args['s'] = sanitize_text_field( $_GET['s'] );
565
			}
566
		}
567
568
		if ( ! empty( $_GET['start-date'] ) ) {
569
			$args['start-date'] = urldecode( $_GET['start-date'] );
570
		}
571
572
		if ( ! empty( $_GET['end-date'] ) ) {
573
			$args['end-date'] = urldecode( $_GET['end-date'] );
574
		}
575
576
		$payment_count         = give_count_payments( $args );
577
		$this->complete_count  = $payment_count->publish;
578
		$this->pending_count   = $payment_count->pending;
579
		$this->refunded_count  = $payment_count->refunded;
580
		$this->failed_count    = $payment_count->failed;
581
		$this->revoked_count   = $payment_count->revoked;
582
		$this->cancelled_count = $payment_count->cancelled;
583
		$this->abandoned_count = $payment_count->abandoned;
584
585
		foreach ( $payment_count as $count ) {
586
			$this->total_count += $count;
587
		}
588
	}
589
590
	/**
591
	 * Retrieve all the data for all the payments
592
	 *
593
	 * @access public
594
	 * @since  1.0
595
	 * @return array $payment_data Array of all the data for the payments
596
	 */
597
	public function payments_data() {
598
599
		$per_page   = $this->per_page;
600
		$orderby    = isset( $_GET['orderby'] ) ? urldecode( $_GET['orderby'] ) : 'ID';
601
		$order      = isset( $_GET['order'] ) ? $_GET['order'] : 'DESC';
602
		$user       = isset( $_GET['user'] ) ? $_GET['user'] : null;
603
		$status     = isset( $_GET['status'] ) ? $_GET['status'] : give_get_payment_status_keys();
604
		$meta_key   = isset( $_GET['meta_key'] ) ? $_GET['meta_key'] : null;
605
		$year       = isset( $_GET['year'] ) ? $_GET['year'] : null;
606
		$month      = isset( $_GET['m'] ) ? $_GET['m'] : null;
607
		$day        = isset( $_GET['day'] ) ? $_GET['day'] : null;
608
		$search     = isset( $_GET['s'] ) ? sanitize_text_field( $_GET['s'] ) : null;
609
		$start_date = isset( $_GET['start-date'] ) ? sanitize_text_field( $_GET['start-date'] ) : null;
610
		$end_date   = isset( $_GET['end-date'] ) ? sanitize_text_field( $_GET['end-date'] ) : $start_date;
611
612
		if ( ! empty( $search ) ) {
613
			$status = 'any'; // Force all payment statuses when searching
614
		}
615
616
		$args = array(
617
			'output'     => 'payments',
618
			'number'     => $per_page,
619
			'page'       => isset( $_GET['paged'] ) ? $_GET['paged'] : null,
620
			'orderby'    => $orderby,
621
			'order'      => $order,
622
			'user'       => $user,
623
			'status'     => $status,
624
			'meta_key'   => $meta_key,
625
			'year'       => $year,
626
			'month'      => $month,
627
			'day'        => $day,
628
			's'          => $search,
629
			'start_date' => $start_date,
630
			'end_date'   => $end_date,
631
		);
632
633
		if ( is_string( $search ) && false !== strpos( $search, 'txn:' ) ) {
634
635
			$args['search_in_notes'] = true;
636
			$args['s']               = trim( str_replace( 'txn:', '', $args['s'] ) );
637
638
		}
639
640
		$p_query = new Give_Payments_Query( $args );
641
642
		return $p_query->get_payments();
643
644
	}
645
646
	/**
647
	 * Setup the final data for the table
648
	 *
649
	 * @access public
650
	 * @since  1.0
651
	 * @uses   Give_Payment_History_Table::get_columns()
652
	 * @uses   Give_Payment_History_Table::get_sortable_columns()
653
	 * @uses   Give_Payment_History_Table::payments_data()
654
	 * @uses   WP_List_Table::get_pagenum()
655
	 * @uses   WP_List_Table::set_pagination_args()
656
	 * @return void
657
	 */
658
	public function prepare_items() {
659
660
		wp_reset_vars( array( 'action', 'payment', 'orderby', 'order', 's' ) );
661
662
		$columns  = $this->get_columns();
663
		$hidden   = array(); // No hidden columns
664
		$sortable = $this->get_sortable_columns();
665
		$data     = $this->payments_data();
666
		$status   = isset( $_GET['status'] ) ? $_GET['status'] : 'any';
667
668
		$this->_column_headers = array( $columns, $hidden, $sortable );
669
670
		switch ( $status ) {
671
			case 'publish':
672
				$total_items = $this->complete_count;
673
				break;
674
			case 'pending':
675
				$total_items = $this->pending_count;
676
				break;
677
			case 'refunded':
678
				$total_items = $this->refunded_count;
679
				break;
680
			case 'failed':
681
				$total_items = $this->failed_count;
682
				break;
683
			case 'revoked':
684
				$total_items = $this->revoked_count;
685
				break;
686
			case 'cancelled':
687
				$total_items = $this->cancelled_count;
688
				break;
689
			case 'abandoned':
690
				$total_items = $this->abandoned_count;
691
				break;
692
			case 'any':
693
				$total_items = $this->total_count;
694
				break;
695
			default:
696
				// Retrieve the count of the non-default-Give status
697
				$count       = wp_count_posts( 'give_payment' );
698
				$total_items = $count->{$status};
699
		}
700
701
		$this->items = $data;
702
703
		$this->set_pagination_args( array(
704
				'total_items' => $total_items,
705
				// WE have to calculate the total number of items
706
				'per_page'    => $this->per_page,
707
				// WE have to determine how many items to show on a page
708
				'total_pages' => ceil( $total_items / $this->per_page )
709
				// WE have to calculate the total number of pages
710
			)
711
		);
712
	}
713
}
714