Give_Payment_History_Table::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Payment History Table Class
4
 *
5
 * @package     Give
6
 * @subpackage  Admin/Payments
7
 * @copyright   Copyright (c) 2016, Give
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
// 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 processing payments
73
	 *
74
	 * @var int
75
	 * @since 1.8.9
76
	 */
77
	public $processing_count;
78
79
	/**
80
	 * Total number of refunded payments
81
	 *
82
	 * @var int
83
	 * @since 1.0
84
	 */
85
	public $refunded_count;
86
87
	/**
88
	 * Total number of failed payments
89
	 *
90
	 * @var int
91
	 * @since 1.0
92
	 */
93
	public $failed_count;
94
95
	/**
96
	 * Total number of revoked payments
97
	 *
98
	 * @var int
99
	 * @since 1.0
100
	 */
101
	public $revoked_count;
102
103
	/**
104
	 * Total number of cancelled payments
105
	 *
106
	 * @var int
107
	 * @since 1.4
108
	 */
109
	public $cancelled_count;
110
111
	/**
112
	 * Total number of abandoned payments
113
	 *
114
	 * @var int
115
	 * @since 1.6
116
	 */
117
	public $abandoned_count;
118
119
	/**
120
	 * Total number of pre-approved payments
121
	 *
122
	 * @var int
123
	 * @since 1.8.13
124
	 */
125
	public $preapproval_count;
126
127
	/**
128
	 * Get things started.
129
	 *
130
	 * @since 1.0
131
	 * @uses  Give_Payment_History_Table::get_payment_counts()
132
	 * @see   WP_List_Table::__construct()
133
	 */
134
	public function __construct() {
135
136
		// Set parent defaults.
137
		parent::__construct(
138
			array(
139
				'singular' => give_get_forms_label_singular(),    // Singular name of the listed records.
140
				'plural'   => give_get_forms_label_plural(),      // Plural name of the listed records.
141
				'ajax'     => false,                              // Does this table support ajax?
142
			)
143
		);
144
145
		$this->process_bulk_action();
146
		$this->get_payment_counts();
147
		$this->base_url = admin_url( 'edit.php?post_type=give_forms&page=give-payment-history' );
148
	}
149
150
	/**
151
	 * Add donation search filter.
152
	 *
153
	 * @return void
154
	 */
155
	public function advanced_filters() {
156
		$start_date  = isset( $_GET['start-date'] ) ? strtotime( give_clean( $_GET['start-date'] ) ) : '';
157
		$end_date    = isset( $_GET['end-date'] ) ? strtotime( give_clean( $_GET['end-date'] ) ) : '';
158
		$status      = isset( $_GET['status'] ) ? give_clean( $_GET['status'] ) : '';
159
		$donor       = isset( $_GET['donor'] ) ? absint( $_GET['donor'] ) : '';
160
		$search      = isset( $_GET['s'] ) ? give_clean( $_GET['s'] ) : '';
161
		$form_id     = ! empty( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : 0;
162
		?>
163
		<div id="give-payment-filters" class="give-filters">
164
			<?php $this->search_box( __( 'Search', 'give' ), 'give-payments' ); ?>
165
			<div id="give-payment-date-filters">
166
				<div class="give-filter give-filter-half">
167
					<label for="start-date"
168
					       class="give-start-date-label"><?php _e( 'Start Date', 'give' ); ?></label>
169
					<input type="text"
170
					       id="start-date"
171
					       name="start-date"
172
					       class="give_datepicker"
173
					       autocomplete="off"
174
					       value="<?php echo $start_date ? date_i18n( give_date_format(), $start_date ) : ''; ?>"
175
					       data-standard-date="<?php echo $start_date ? date( 'Y-m-d', $start_date ) : $start_date; ?>"
176
					       placeholder="<?php _e( 'Start Date', 'give' ); ?>"
177
					/>
178
				</div>
179
				<div class="give-filter give-filter-half">
180
					<label for="end-date" class="give-end-date-label"><?php _e( 'End Date', 'give' ); ?></label>
181
					<input type="text"
182
					       id="end-date"
183
					       name="end-date"
184
					       class="give_datepicker"
185
					       autocomplete="off"
186
					       value="<?php echo $end_date ? date_i18n( give_date_format(), $end_date ) : ''; ?>"
187
					       data-standard-date="<?php echo $end_date ? date( 'Y-m-d', $end_date ) : $end_date; ?>"
188
					       placeholder="<?php _e( 'End Date', 'give' ); ?>"
189
					/>
190
				</div>
191
			</div>
192
			<div id="give-payment-form-filter" class="give-filter">
193
				<label for="give-donation-forms-filter"
194
				       class="give-donation-forms-filter-label"><?php _e( 'Form', 'give' ); ?></label>
195
				<?php
196
				// Filter Donations by Donation Forms.
197
				echo Give()->html->forms_dropdown(
198
					array(
199
						'name'     => 'form_id',
200
						'id'       => 'give-donation-forms-filter',
201
						'class'    => 'give-donation-forms-filter',
202
						'selected' => $form_id, // Make sure to have $form_id set to 0, if there is no selection.
203
						'chosen'   => true,
204
						'number'   => 30,
205
					)
206
				);
207
				?>
208
			</div>
209
210
			<?php
211
			/**
212
			 * Action to add hidden fields and HTML in Payment search.
213
			 *
214
			 * @since 1.8.18
215
			 */
216
			do_action( 'give_payment_table_advanced_filters' );
217
218
219
			if ( ! empty( $status ) ) {
220
				echo sprintf( '<input type="hidden" name="status" value="%s"/>', esc_attr( $status ) );
221
			}
222
223
			if ( ! empty( $donor ) ) {
224
				echo sprintf( '<input type="hidden" name="donor" value="%s"/>', absint( $donor ) );
225
			}
226
			?>
227
228
			<div class="give-filter">
229
				<?php submit_button( __( 'Apply', 'give' ), 'secondary', '', false ); ?>
230
				<?php
231
				// Clear active filters button.
232 View Code Duplication
				if ( ! empty( $start_date ) || ! empty( $end_date ) || ! empty( $donor ) || ! empty( $search ) || ! empty( $status ) || ! empty( $form_id ) ) :
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...
233
					?>
234
					<a href="<?php echo admin_url( 'edit.php?post_type=give_forms&page=give-payment-history' ); ?>"
235
					   class="button give-clear-filters-button"><?php _e( 'Clear Filters', 'give' ); ?></a>
236
				<?php endif; ?>
237
			</div>
238
		</div>
239
240
		<?php
241
	}
242
243
	/**
244
	 * Show the search field
245
	 *
246
	 * @param string $text     Label for the search box.
247
	 * @param string $input_id ID of the search box.
248
	 *
249
	 * @since  1.0
250
	 * @access public
251
	 *
252
	 * @return void
253
	 */
254
	public function search_box( $text, $input_id ) {
255
		$input_id = $input_id . '-search-input';
256
257
		if ( ! empty( $_REQUEST['orderby'] ) ) {
258
			echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
259
		}
260
		if ( ! empty( $_REQUEST['order'] ) ) {
261
			echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
262
		}
263
		?>
264
		<div class="give-filter give-filter-search" role="search">
265
			<?php
266
			/**
267
			 * Fires in the payment history search box.
268
			 *
269
			 * Allows you to add new elements before the search box.
270
			 *
271
			 * @since 1.7
272
			 */
273
			do_action( 'give_payment_history_search' );
274
			?>
275
			<label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label>
276
			<input type="search" id="<?php echo $input_id ?>" name="s"
277
			       value="<?php _admin_search_query(); ?>"
278
			       placeholder="<?php _e( 'Name, Email, or Donation ID', 'give' ); ?>" />
279
			<?php submit_button( $text, 'button', false, false, array(
280
				'ID' => 'search-submit',
281
			) ); ?><br />
282
		</div>
283
		<?php
284
	}
285
286
	/**
287
	 * Retrieve the view types
288
	 *
289
	 * @access public
290
	 * @since  1.0
291
	 *
292
	 * @return array $views All the views available
293
	 */
294
	public function get_views() {
295
296
		$current = isset( $_GET['status'] ) ? $_GET['status'] : '';
297
		$views   = array();
298
		$tabs    = array(
299
			'all'         => array(
300
				'total_count',
301
				__( 'All', 'give' ),
302
			),
303
			'publish'     => array(
304
				'complete_count',
305
				__( 'Completed', 'give' ),
306
			),
307
			'pending'     => array(
308
				'pending_count',
309
				__( 'Pending', 'give' ),
310
			),
311
			'processing'  => array(
312
				'processing_count',
313
				__( 'Processing', 'give' ),
314
			),
315
			'refunded'    => array(
316
				'refunded_count',
317
				__( 'Refunded', 'give' ),
318
			),
319
			'revoked'     => array(
320
				'revoked_count',
321
				__( 'Revoked', 'give' ),
322
			),
323
			'failed'      => array(
324
				'failed_count',
325
				__( 'Failed', 'give' ),
326
			),
327
			'cancelled'   => array(
328
				'cancelled_count',
329
				__( 'Cancelled', 'give' ),
330
			),
331
			'abandoned'   => array(
332
				'abandoned_count',
333
				__( 'Abandoned', 'give' ),
334
			),
335
			'preapproval' => array(
336
				'preapproval_count',
337
				__( 'Preapproval Pending', 'give' ),
338
			),
339
		);
340
341
		/**
342
		 * Remove Query from Args of the URL that are being pass to Donation Status.
343
		 *
344
		 * @since 1.8.18
345
		 */
346
		$args = (array) apply_filters( 'give_payments_table_status_remove_query_arg', array( 'paged', '_wpnonce', '_wp_http_referer' ) );
347
348
		// Build URL.
349
		$staus_url = remove_query_arg( $args );
350
351
		foreach ( $tabs as $key => $tab ) {
352
			$count_key = $tab[0];
353
			$name      = $tab[1];
354
			$count     = $this->$count_key;
355
356
			/**
357
			 * Filter can be used to show all the status inside the donation tabs.
358
			 *
359
			 * Filter can be used to show all the status inside the donation submenu tabs return true to show all the tab.
360
			 *
361
			 * @param string $key Current view tab value.
362
			 * @param int $count Number of donation inside the tab.
363
			 *
364
			 * @since 1.8.12
365
			 */
366
			if ( 'all' === $key || $key === $current || apply_filters( 'give_payments_table_show_all_status', 0 < $count, $key, $count ) ) {
367
368
				$staus_url = 'all' === $key ?
369
					add_query_arg( array( 'status' => false ), $staus_url ) :
370
					add_query_arg( array( 'status' => $key ), $staus_url );
371
372
				$views[ $key ] = sprintf(
373
					'<a href="%s"%s>%s&nbsp;<span class="count">(%s)</span></a>',
374
					esc_url( $staus_url ),
375
					( ( 'all' === $key && empty( $current ) ) ) ? ' class="current"' : ( $current == $key ? 'class="current"' : '' ),
376
					$name,
377
					$count
378
				);
379
			}
380
		}
381
382
		/**
383
		 * Filter the donation listing page views.
384
		 *
385
		 * @since 1.0
386
		 *
387
		 * @param array $views
388
		 * @param Give_Payment_History_Table
389
		 */
390
		return apply_filters( 'give_payments_table_views', $views, $this );
391
	}
392
393
	/**
394
	 * Retrieve the table columns
395
	 *
396
	 * @access public
397
	 * @since  1.0
398
	 *
399
	 * @return array $columns Array of all the list table columns
400
	 */
401
	public function get_columns() {
402
		$columns = array(
403
			'cb'            => '<input type="checkbox" />', // Render a checkbox instead of text.
404
			'donation'      => __( 'Donation', 'give' ),
405
			'donation_form' => __( 'Donation Form', 'give' ),
406
			'status'        => __( 'Status', 'give' ),
407
			'date'          => __( 'Date', 'give' ),
408
			'amount'        => __( 'Amount', 'give' ),
409
		);
410
411
		if ( current_user_can( 'view_give_payments' ) ) {
412
			$columns['details'] = __( 'Details', 'give' );
413
		}
414
415
		return apply_filters( 'give_payments_table_columns', $columns );
416
	}
417
418
	/**
419
	 * Retrieve the table's sortable columns
420
	 *
421
	 * @access public
422
	 * @since  1.0
423
	 *
424
	 * @return array Array of all the sortable columns
425
	 */
426
	public function get_sortable_columns() {
427
		$columns = array(
428
			'donation'      => array( 'ID', true ),
429
			'donation_form' => array( 'donation_form', false ),
430
			'status'        => array( 'status', false ),
431
			'amount'        => array( 'amount', false ),
432
			'date'          => array( 'date', false ),
433
		);
434
435
		return apply_filters( 'give_payments_table_sortable_columns', $columns );
436
	}
437
438
	/**
439
	 * Gets the name of the primary column.
440
	 *
441
	 * @since  1.5
442
	 * @access protected
443
	 *
444
	 * @return string Name of the primary column.
445
	 */
446
	protected function get_primary_column_name() {
447
		return 'donation';
448
	}
449
450
	/**
451
	 * This function renders most of the columns in the list table.
452
	 *
453
	 * @param Give_Payment $payment     Payment ID.
454
	 * @param string       $column_name The name of the column.
455
	 *
456
	 * @access public
457
	 * @since  1.0
458
	 *
459
	 * @return string Column Name
460
	 */
461
	public function column_default( $payment, $column_name ) {
462
463
		$single_donation_url = esc_url( add_query_arg( 'id', $payment->ID, admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-payment-details' ) ) );
464
		$row_actions         = $this->get_row_actions( $payment );
465
		$value               = '';
466
467
		switch ( $column_name ) {
468
			case 'donation' :
469
				$serial_code = Give()->seq_donation_number->get_serial_code( $payment );
470
				if ( current_user_can( 'view_give_payments' ) ) {
471
					$value = Give()->tooltips->render_link( array(
472
						'label'       => sprintf( __( 'View Donation %s', 'give' ), $serial_code ),
473
						'tag_content' => $serial_code,
474
						'link'        => $single_donation_url,
475
					) );
476
				} else {
477
					$value = $serial_code;
478
				}
479
480
				$value .= sprintf(
481
					'&nbsp;%1$s&nbsp;%2$s<br>',
482
					__( 'by', 'give' ),
483
					$this->get_donor( $payment )
484
				);
485
486
				$value .= $this->get_donor_email( $payment );
487
				$value .= $this->row_actions( $row_actions );
488
				break;
489
490
			case 'amount':
491
				$value  = give_donation_amount( $payment, true );
492
				$value .= sprintf( '<br><small>%1$s %2$s</small>', __( 'via', 'give' ), give_get_gateway_admin_label( $payment->gateway ) );
493
				break;
494
495
			case 'donation_form':
496
				$form_title = empty( $payment->form_title ) ? sprintf( __( 'Untitled (#%s)', 'give' ), $payment->form_id ) : $payment->form_title;
497
				$value      = '<a href="' . admin_url( 'post.php?post=' . $payment->form_id . '&action=edit' ) . '">' . $form_title . '</a>';
498
				$level      = give_get_donation_form_title(
499
					$payment,
500
					array(
501
						'only_level' => true,
502
					)
503
				);
504
505
				if ( ! empty( $level ) ) {
506
					$value .= $level;
507
				}
508
509
				break;
510
511
			case 'date':
512
				$date  = strtotime( $payment->date );
513
				$value = date_i18n( give_date_format(), $date );
514
				break;
515
516
			case 'status':
517
				$value = $this->get_payment_status( $payment );
518
				break;
519
520
521
			case 'details' :
522
				if ( current_user_can( 'view_give_payments' ) ) {
523
					$value = Give()->tooltips->render_link( array(
524
						'label'       => sprintf( __( 'View Donation #%s', 'give' ), $payment->ID ),
525
						'tag_content' => '<span class="dashicons dashicons-visibility"></span>',
526
						'link'        => $single_donation_url,
527
						'attributes'  => array(
528
							'class' => 'give-payment-details-link button button-small',
529
						),
530
					) );
531
532
					$value = "<div class=\"give-payment-details-link-wrap\">{$value}</div>";
533
				}
534
				break;
535
536
			default:
537
				$value = isset( $payment->$column_name ) ? $payment->$column_name : '';
538
				break;
539
540
		}// End switch().
541
542
		return apply_filters( 'give_payments_table_column', $value, $payment->ID, $column_name );
543
	}
544
545
	/**
546
	 * Get donor email html.
547
	 *
548
	 * @param object $payment Contains all the data of the payment.
549
	 *
550
	 * @access public
551
	 * @since  1.0
552
	 *
553
	 * @return string Data shown in the Email column
554
	 */
555
	public function get_donor_email( $payment ) {
556
557
		$email = give_get_payment_user_email( $payment->ID );
558
559
		if ( empty( $email ) ) {
560
			$email = __( '(unknown)', 'give' );
561
		}
562
563
564
		$value = Give()->tooltips->render_link( array(
565
			'link'        => "mailto:{$email}",
566
			'label'       => __( 'Email donor', 'give' ),
567
			'tag_content' => $email,
568
		) );
569
570
		return apply_filters( 'give_payments_table_column', $value, $payment->ID, 'email' );
571
	}
572
573
	/**
574
	 * Get Row Actions
575
	 *
576
	 * @param object $payment Payment Data.
577
	 *
578
	 * @since 1.6
579
	 *
580
	 * @return array $actions
581
	 */
582
	function get_row_actions( $payment ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
583
584
		$actions = array();
585
		$email   = give_get_payment_user_email( $payment->ID );
586
587
		// Add search term string back to base URL.
588
		$search_terms = ( isset( $_GET['s'] ) ? trim( $_GET['s'] ) : '' );
589
		if ( ! empty( $search_terms ) ) {
590
			$this->base_url = add_query_arg( 's', $search_terms, $this->base_url );
591
		}
592
593 View Code Duplication
		if ( give_is_payment_complete( $payment->ID ) && ! empty( $email ) ) {
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...
594
595
			$actions['email_links'] = sprintf(
596
				'<a class="resend-single-donation-receipt" href="%1$s" aria-label="%2$s">%3$s</a>', wp_nonce_url(
597
				add_query_arg(
598
					array(
599
						'give-action' => 'email_links',
600
						'purchase_id' => $payment->ID,
601
					), $this->base_url
602
				), 'give_payment_nonce'
603
			), sprintf( __( 'Resend Donation %s Receipt', 'give' ), $payment->ID ), __( 'Resend Receipt', 'give' )
604
			);
605
606
		}
607
608 View Code Duplication
		if ( current_user_can( 'view_give_payments' ) ) {
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...
609
			$actions['delete'] = sprintf(
610
				'<a class="delete-single-donation" href="%1$s" aria-label="%2$s">%3$s</a>',
611
				wp_nonce_url(
612
					add_query_arg(
613
						array(
614
							'give-action' => 'delete_payment',
615
							'purchase_id' => $payment->ID,
616
						), $this->base_url
617
					), 'give_donation_nonce'
618
				), sprintf( __( 'Delete Donation %s', 'give' ), $payment->ID ), __( 'Delete', 'give' )
619
			);
620
		}
621
622
		return apply_filters( 'give_payment_row_actions', $actions, $payment );
623
	}
624
625
626
	/**
627
	 *  Get payment status html.
628
	 *
629
	 * @since  1.0
630
	 * @access public
631
	 *
632
	 * @param Give_Payment $payment Contains all the data of the payment.
633
	 *
634
	 * @return string Data shown in the Email column
635
	 */
636
	function get_payment_status( $payment ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
637
		$value = sprintf(
638
			'<div class="give-donation-status status-%1$s"><span class="give-donation-status-icon"></span>&nbsp;%2$s</div>',
639
			$payment->status,
640
			give_get_payment_status( $payment, true )
641
		);
642
643
		if ( $payment->mode == 'test' ) {
644
			$value .= Give()->tooltips->render_span( array(
645
				'label'       => __( 'This donation was made in test mode.', 'give' ),
646
				'tag_content' => __( 'Test', 'give' ),
647
				'attributes'  => array(
648
					'class' => 'give-item-label give-item-label-orange give-test-mode-transactions-label',
649
				),
650
651
652
			) );
653
		}
654
655
		if ( true === $payment->import && true === (bool) apply_filters( 'give_payment_show_importer_label', false ) ) {
656
			$value .= sprintf(
657
				'&nbsp;<span class="give-item-label give-item-label-orange give-test-mode-transactions-label" data-tooltip="%1$s">%2$s</span>',
658
				__( 'This donation was imported.', 'give' ),
659
				__( 'Import', 'give' )
660
			);
661
		}
662
663
		return $value;
664
	}
665
666
	/**
667
	 * Get checkbox html.
668
	 *
669
	 * @param object $payment Contains all the data for the checkbox column.
670
	 *
671
	 * @access public
672
	 * @since  1.0
673
	 *
674
	 * @return string Displays a checkbox.
675
	 */
676
	public function column_cb( $payment ) {
677
		return sprintf( '<input type="checkbox" name="%1$s[]" value="%2$s" />', 'payment', $payment->ID );
678
	}
679
680
	/**
681
	 * Get payment ID html.
682
	 *
683
	 * @param object $payment Contains all the data for the checkbox column.
684
	 *
685
	 * @access public
686
	 * @since  1.0
687
	 *
688
	 * @return string Displays a checkbox.
689
	 */
690
	public function get_payment_id( $payment ) {
691
		return '<span class="give-payment-id">' . give_get_payment_number( $payment->ID ) . '</span>';
692
	}
693
694
	/**
695
	 * Get donor html.
696
	 *
697
	 * @param object $payment Contains all the data of the payment.
698
	 *
699
	 * @access public
700
	 * @since  1.0
701
	 *
702
	 * @return string Data shown in the User column
703
	 */
704
	public function get_donor( $payment ) {
705
706
		$donor_id           = give_get_payment_donor_id( $payment->ID );
707
		$donor_billing_name = give_get_donor_name_by( $payment->ID, 'donation' );
708
		$donor_name         = give_get_donor_name_by( $donor_id, 'donor' );
709
710
		$value = '';
711
		if ( ! empty( $donor_id ) ) {
712
713
			// Check whether the donor name and WP_User name is same or not.
714
			if ( sanitize_title( $donor_billing_name ) !== sanitize_title( $donor_name ) ) {
715
				$value .= $donor_billing_name . ' (';
716
			}
717
718
			$value .= '<a href="' . esc_url( admin_url( "edit.php?post_type=give_forms&page=give-donors&view=overview&id=$donor_id" ) ) . '">' . $donor_name . '</a>';
719
720
			// Check whether the donor name and WP_User name is same or not.
721
			if ( sanitize_title( $donor_billing_name ) != sanitize_title( $donor_name ) ) {
722
				$value .= ')';
723
			}
724
		} else {
725
			$email  = give_get_payment_user_email( $payment->ID );
726
			$value .= '<a href="' . esc_url( admin_url( "edit.php?post_type=give_forms&page=give-payment-history&s=$email" ) ) . '">' . __( '(donor missing)', 'give' ) . '</a>';
727
		}
728
729
		return apply_filters( 'give_payments_table_column', $value, $payment->ID, 'donor' );
730
	}
731
732
	/**
733
	 * Retrieve the bulk actions
734
	 *
735
	 * @access public
736
	 * @since  1.0
737
	 *
738
	 * @return array $actions Array of the bulk actions
739
	 */
740
	public function get_bulk_actions() {
741
		$actions = array(
742
			'delete'                 => __( 'Delete', 'give' ),
743
			'set-status-publish'     => __( 'Set To Completed', 'give' ),
744
			'set-status-pending'     => __( 'Set To Pending', 'give' ),
745
			'set-status-processing'  => __( 'Set To Processing', 'give' ),
746
			'set-status-refunded'    => __( 'Set To Refunded', 'give' ),
747
			'set-status-revoked'     => __( 'Set To Revoked', 'give' ),
748
			'set-status-failed'      => __( 'Set To Failed', 'give' ),
749
			'set-status-cancelled'   => __( 'Set To Cancelled', 'give' ),
750
			'set-status-abandoned'   => __( 'Set To Abandoned', 'give' ),
751
			'set-status-preapproval' => __( 'Set To Preapproval', 'give' ),
752
			'resend-receipt'         => __( 'Resend Email Receipts', 'give' ),
753
		);
754
755
		return apply_filters( 'give_payments_table_bulk_actions', $actions );
756
	}
757
758
	/**
759
	 * Process the bulk actions
760
	 *
761
	 * @access public
762
	 * @since  1.0
763
	 *
764
	 * @return void
765
	 */
766
	public function process_bulk_action() {
767
		$ids    = isset( $_GET['payment'] ) ? $_GET['payment'] : false;
768
		$action = $this->current_action();
769
770
		if ( ! is_array( $ids ) ) {
771
			$ids = array( $ids );
772
		}
773
774
		if ( empty( $action ) ) {
775
			return;
776
		}
777
778
		foreach ( $ids as $id ) {
779
780
			// Detect when a bulk action is being triggered.
781
			switch ( $this->current_action() ) {
782
783
				case 'delete':
784
					give_delete_donation( $id );
785
					break;
786
787
				case 'set-status-publish':
788
					give_update_payment_status( $id, 'publish' );
789
					break;
790
791
				case 'set-status-pending':
792
					give_update_payment_status( $id, 'pending' );
793
					break;
794
795
				case 'set-status-processing':
796
					give_update_payment_status( $id, 'processing' );
797
					break;
798
799
				case 'set-status-refunded':
800
					give_update_payment_status( $id, 'refunded' );
801
					break;
802
				case 'set-status-revoked':
803
					give_update_payment_status( $id, 'revoked' );
804
					break;
805
806
				case 'set-status-failed':
807
					give_update_payment_status( $id, 'failed' );
808
					break;
809
810
				case 'set-status-cancelled':
811
					give_update_payment_status( $id, 'cancelled' );
812
					break;
813
814
				case 'set-status-abandoned':
815
					give_update_payment_status( $id, 'abandoned' );
816
					break;
817
818
				case 'set-status-preapproval':
819
					give_update_payment_status( $id, 'preapproval' );
820
					break;
821
822
				case 'resend-receipt':
823
					/**
824
					 * Fire the action
825
					 *
826
					 * @since 2.0
827
					 */
828
					do_action( 'give_donation-receipt_email_notification', $id );
829
					break;
830
			}// End switch().
831
832
			/**
833
			 * Fires after triggering bulk action on payments table.
834
			 *
835
			 * @param int    $id             The ID of the payment.
836
			 * @param string $current_action The action that is being triggered.
837
			 *
838
			 * @since 1.7
839
			 */
840
			do_action( 'give_payments_table_do_bulk_action', $id, $this->current_action() );
841
		}// End foreach().
842
843
	}
844
845
	/**
846
	 * Retrieve the payment counts
847
	 *
848
	 * @access public
849
	 * @since  1.0
850
	 *
851
	 * @return object
852
	 */
853
	public function get_payment_counts() {
854
855
		$args = array();
856
857
		if ( isset( $_GET['user'] ) ) {
858
			$args['user'] = urldecode( $_GET['user'] );
859
		} elseif ( isset( $_GET['donor'] ) ) {
860
			$args['donor'] = absint( $_GET['donor'] );
861
		} elseif ( isset( $_GET['s'] ) ) {
862
			$is_user = strpos( $_GET['s'], strtolower( 'user:' ) ) !== false;
863
			if ( $is_user ) {
864
				$args['user'] = absint( trim( str_replace( 'user:', '', strtolower( $_GET['s'] ) ) ) );
865
				unset( $args['s'] );
866
			} else {
867
				$args['s'] = sanitize_text_field( $_GET['s'] );
868
			}
869
		}
870
871
		if ( ! empty( $_GET['start-date'] ) ) {
872
			$args['start-date'] = urldecode( $_GET['start-date'] );
873
		}
874
875
		if ( ! empty( $_GET['end-date'] ) ) {
876
			$args['end-date'] = urldecode( $_GET['end-date'] );
877
		}
878
879
		$args['form_id'] = ! empty( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : null;
880
		$args['gateway'] = ! empty( $_GET['gateway'] ) ? give_clean( $_GET['gateway'] ) : null;
881
882
		$payment_count           = give_count_payments( $args );
883
		$this->complete_count    = $payment_count->publish;
884
		$this->pending_count     = $payment_count->pending;
885
		$this->processing_count  = $payment_count->processing;
886
		$this->refunded_count    = $payment_count->refunded;
887
		$this->failed_count      = $payment_count->failed;
888
		$this->revoked_count     = $payment_count->revoked;
889
		$this->cancelled_count   = $payment_count->cancelled;
890
		$this->abandoned_count   = $payment_count->abandoned;
891
		$this->preapproval_count = $payment_count->preapproval;
892
893
		foreach ( $payment_count as $count ) {
894
			$this->total_count += $count;
895
		}
896
897
		return $payment_count;
898
	}
899
900
	/**
901
	 * Retrieve all the data for all the payments.
902
	 *
903
	 * @access public
904
	 * @since  1.0
905
	 *
906
	 * @return array  objects in array containing all the data for the payments
907
	 */
908
	public function payments_data() {
909
		$per_page   = $this->per_page;
910
		$orderby    = isset( $_GET['orderby'] ) ? urldecode( $_GET['orderby'] ) : 'ID';
911
		$order      = isset( $_GET['order'] ) ? $_GET['order'] : 'DESC';
912
		$user       = isset( $_GET['user'] ) ? $_GET['user'] : null;
913
		$donor      = isset( $_GET['donor'] ) ? $_GET['donor'] : null;
914
		$status     = isset( $_GET['status'] ) ? $_GET['status'] : give_get_payment_status_keys();
915
		$meta_key   = isset( $_GET['meta_key'] ) ? $_GET['meta_key'] : null;
916
		$year       = isset( $_GET['year'] ) ? $_GET['year'] : null;
917
		$month      = isset( $_GET['m'] ) ? $_GET['m'] : null;
918
		$day        = isset( $_GET['day'] ) ? $_GET['day'] : null;
919
		$search     = isset( $_GET['s'] ) ? sanitize_text_field( $_GET['s'] ) : null;
920
		$start_date = ! empty ( $_GET['start-date'] )
921
			? give_clean( $_GET['start-date'] )
922
			: date( 'Y-m-d', 0 );
923
		$end_date   = ! empty( $_GET['end-date'] )
924
			? give_clean( $_GET['end-date'] )
925
			: date( 'Y-m-d', current_time( 'timestamp' ) );
926
		$form_id    = ! empty( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : null;
927
		$gateway    = ! empty( $_GET['gateway'] ) ? give_clean( $_GET['gateway'] ) : null;
928
929
		$args = array(
930
			'output'     => 'payments',
931
			'number'     => $per_page,
932
			'page'       => isset( $_GET['paged'] ) ? $_GET['paged'] : null,
933
			'orderby'    => $orderby,
934
			'order'      => $order,
935
			'user'       => $user,
936
			'donor'      => $donor,
937
			'status'     => $status,
938
			'meta_key'   => $meta_key,
939
			'year'       => $year,
940
			'month'      => $month,
941
			'day'        => $day,
942
			's'          => $search,
943
			'start_date' => $start_date,
944
			'gateway'    => $gateway,
945
			'end_date'   => $end_date,
946
			'give_forms' => $form_id,
947
		);
948
949
		if ( is_string( $search ) && false !== strpos( $search, 'txn:' ) ) {
950
			$args['search_in_notes'] = true;
951
			$args['s']               = trim( str_replace( 'txn:', '', $args['s'] ) );
952
		}
953
954
		/**
955
		 * Filter to modify payment table argument.
956
		 *
957
		 * @since 1.8.18
958
		 */
959
		$args = (array) apply_filters( 'give_payment_table_payments_query', $args );
960
961
		$p_query = new Give_Payments_Query( $args );
962
963
		return $p_query->get_payments();
964
965
	}
966
967
	/**
968
	 * Setup the final data for the table
969
	 *
970
	 * @access public
971
	 * @since  1.0
972
	 * @uses   Give_Payment_History_Table::get_columns()
973
	 * @uses   Give_Payment_History_Table::get_sortable_columns()
974
	 * @uses   Give_Payment_History_Table::payments_data()
975
	 * @uses   WP_List_Table::get_pagenum()
976
	 * @uses   WP_List_Table::set_pagination_args()
977
	 *
978
	 * @return void
979
	 */
980
	public function prepare_items() {
981
982
		wp_reset_vars( array( 'action', 'payment', 'orderby', 'order', 's' ) );
983
984
		$columns  = $this->get_columns();
985
		$hidden   = array(); // No hidden columns.
986
		$sortable = $this->get_sortable_columns();
987
		$data     = $this->payments_data();
988
		$status   = isset( $_GET['status'] ) ? $_GET['status'] : 'any';
989
990
		$this->_column_headers = array( $columns, $hidden, $sortable );
991
992
		switch ( $status ) {
993
			case 'publish':
994
				$total_items = $this->complete_count;
995
				break;
996
			case 'pending':
997
				$total_items = $this->pending_count;
998
				break;
999
			case 'processing':
1000
				$total_items = $this->processing_count;
1001
				break;
1002
			case 'refunded':
1003
				$total_items = $this->refunded_count;
1004
				break;
1005
			case 'failed':
1006
				$total_items = $this->failed_count;
1007
				break;
1008
			case 'revoked':
1009
				$total_items = $this->revoked_count;
1010
				break;
1011
			case 'cancelled':
1012
				$total_items = $this->cancelled_count;
1013
				break;
1014
			case 'abandoned':
1015
				$total_items = $this->abandoned_count;
1016
				break;
1017
			case 'preapproval':
1018
				$total_items = $this->preapproval_count;
1019
				break;
1020
			case 'any':
1021
				$total_items = $this->total_count;
1022
				break;
1023
			default:
1024
				// Retrieve the count of the non-default-Give status.
1025
				$count       = wp_count_posts( 'give_payment' );
1026
				$total_items = isset( $count->{$status} ) ? $count->{$status} : 0;
1027
				break;
1028
		}
1029
1030
		$this->items = $data;
1031
1032
		/**
1033
		 * Filter to modify total count of the pagination.
1034
		 *
1035
		 * @since 1.8.19
1036
		 */
1037
		$total_items = (int) apply_filters( 'give_payment_table_pagination_total_count', $total_items, $this );
1038
1039
		$this->set_pagination_args(
1040
			array(
1041
				'total_items' => $total_items,
1042
				// We have to calculate the total number of items.
1043
				'per_page'    => $this->per_page,
1044
				// We have to determine how many items to show on a page.
1045
				'total_pages' => ceil( $total_items / $this->per_page ),
1046
				// We have to calculate the total number of pages.
1047
			)
1048
		);
1049
	}
1050
}
1051