Test Failed
Push — issues/1944 ( 5c19d0...2ded50 )
by Ravinder
04:30
created

Give_Payment_History_Table::get_payment_status()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 17

Duplication

Lines 11
Ratio 37.93 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 4
nop 1
dl 11
loc 29
rs 8.5806
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'] ) ? sanitize_text_field( $_GET['start-date'] ) : null;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
157
		$end_date   = isset( $_GET['end-date'] ) ? sanitize_text_field( $_GET['end-date'] ) : null;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
158
		$status     = isset( $_GET['status'] ) ? sanitize_text_field( $_GET['status'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
159
		$donor      = isset( $_GET['donor'] ) ? sanitize_text_field( $_GET['donor'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
160
		$search     = isset( $_GET['s'] ) ? sanitize_text_field( $_GET['s'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
161
		$form_id    = ! empty( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : 0;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
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" id="start-date" name="start-date" class="give_datepicker"
170
						   value="<?php echo $start_date; ?>" placeholder="mm/dd/yyyy"/>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$start_date'
Loading history...
171
				</div>
172
				<div class="give-filter give-filter-half">
173
					<label for="end-date" class="give-end-date-label"><?php _e( 'End Date', 'give' ); ?></label>
174
					<input type="text" id="end-date" name="end-date" class="give_datepicker"
175
						   value="<?php echo $end_date; ?>" placeholder="mm/dd/yyyy"/>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$end_date'
Loading history...
176
				</div>
177
			</div>
178
			<div id="give-payment-form-filter" class="give-filter">
179
				<label for="give-donation-forms-filter"
180
					   class="give-donation-forms-filter-label"><?php _e( 'Form', 'give' ); ?></label>
181
				<?php
182
				// Filter Donations by Donation Forms.
183
				echo Give()->html->forms_dropdown(
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'Give'
Loading history...
184
					array(
185
						'name'     => 'form_id',
186
						'id'       => 'give-donation-forms-filter',
187
						'class'    => 'give-donation-forms-filter',
188
						'selected' => $form_id, // Make sure to have $form_id set to 0, if there is no selection.
189
						'chosen'   => true,
190
						'number'   => - 1,
191
					)
192
				);
193
				?>
194
			</div>
195
196
			<?php if ( ! empty( $status ) ) : ?>
197
				<input type="hidden" name="status" value="<?php echo esc_attr( $status ); ?>"/>
198
			<?php endif; ?>
199
200
			<div class="give-filter">
201
				<?php submit_button( __( 'Apply', 'give' ), 'secondary', '', false ); ?>
202
				<?php
203
				// Clear active filters button.
204
				if ( ! empty( $start_date ) || ! empty( $end_date ) || ! empty( $donor ) || ! empty( $search ) || ! empty( $status ) || ! empty( $form_id ) ) :
205
				?>
206
					<a href="<?php echo admin_url( 'edit.php?post_type=give_forms&page=give-payment-history' ); ?>"
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'admin_url'
Loading history...
207
					   class="button give-clear-filters-button"><?php _e( 'Clear Filters', 'give' ); ?></a>
208
				<?php endif; ?>
209
			</div>
210
		</div>
211
212
		<?php
213
	}
214
215
	/**
216
	 * Show the search field
217
	 *
218
	 * @param string $text     Label for the search box.
219
	 * @param string $input_id ID of the search box.
220
	 *
221
	 * @since  1.0
222
	 * @access public
223
	 *
224
	 * @return void
225
	 */
226
	public function search_box( $text, $input_id ) {
227
		if ( empty( $_REQUEST['s'] ) && ! $this->has_items() ) {
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
228
			return;
229
		}
230
231
		$input_id = $input_id . '-search-input';
232
233
		if ( ! empty( $_REQUEST['orderby'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
234
			echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
235
		}
236
		if ( ! empty( $_REQUEST['order'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
237
			echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
238
		}
239
		?>
240
		<div class="give-filter give-filter-search" role="search">
241
			<?php
242
			/**
243
			 * Fires in the payment history search box.
244
			 *
245
			 * Allows you to add new elements before the search box.
246
			 *
247
			 * @since 1.7
248
			 */
249
			do_action( 'give_payment_history_search' );
250
			?>
251
			<label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$input_id'
Loading history...
introduced by
Expected next thing to be a escaping function, not '$text'
Loading history...
252
			<input type="search" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>"/>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$input_id'
Loading history...
253
			<?php submit_button( $text, 'button', false, false, array(
254
				'ID' => 'search-submit',
255
			) ); ?><br/>
256
		</div>
257
		<?php
258
	}
259
260
	/**
261
	 * Retrieve the view types
262
	 *
263
	 * @access public
264
	 * @since  1.0
265
	 *
266
	 * @return array $views All the views available
267
	 */
268
	public function get_views() {
269
270
		$current = isset( $_GET['status'] ) ? $_GET['status'] : '';
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
271
		$views   = array();
272
		$tabs    = array(
273
			'all'         => array(
274
				'total_count',
275
				__( 'All', 'give' ),
276
			),
277
			'publish'     => array(
278
				'complete_count',
279
				__( 'Completed', 'give' ),
280
			),
281
			'pending'     => array(
282
				'pending_count',
283
				__( 'Pending', 'give' ),
284
			),
285
			'processing'  => array(
286
				'processing_count',
287
				__( 'Processing', 'give' ),
288
			),
289
			'refunded'    => array(
290
				'refunded_count',
291
				__( 'Refunded', 'give' ),
292
			),
293
			'revoked'     => array(
294
				'revoked_count',
295
				__( 'Revoked', 'give' ),
296
			),
297
			'failed'      => array(
298
				'failed_count',
299
				__( 'Failed', 'give' ),
300
			),
301
			'cancelled'   => array(
302
				'cancelled_count',
303
				__( 'Cancelled', 'give' ),
304
			),
305
			'abandoned'   => array(
306
				'abandoned_count',
307
				__( 'Abandoned', 'give' ),
308
			),
309
			'preapproval' => array(
310
				'preapproval_count',
311
				__( 'Preapproval Pending', 'give' ),
312
			),
313
		);
314
315
		foreach ( $tabs as $key => $tab ) {
316
			$count_key = $tab[0];
317
			$name      = $tab[1];
318
			$count     = $this->$count_key;
319
320
			/**
321
			 * Filter can be used to show all the status inside the donation tabs.
322
			 *
323
			 * Filter can be used to show all the status inside the donation submenu tabs return true to show all the tab.
324
			 *
325
			 * @param string $key   Current view tab value.
326
			 * @param int    $count Number of donation inside the tab.
327
			 *
328
			 * @since 1.8.12
329
			 */
330
			if ( 'all' === $key || $key === $current || apply_filters( 'give_payments_table_show_all_status', 0 < $count, $key, $count ) ) {
331
332
				$views[ $key ] = sprintf(
333
					'<a href="%s" %s >%s&nbsp;<span class="count">(%s)</span></a>',
334
					esc_url( ( 'all' === (string) $key ) ? remove_query_arg( array(
335
						'status',
336
						'paged'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
337
					) ) : add_query_arg( array(
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 24 spaces, but found 20.
Loading history...
338
						'status' => $key,
339
						'paged'  => false
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
340
					), admin_url( 'edit.php?post_type=give_forms&page=give-payment-history' ) ) ),
341
					( ( 'all' === $key && empty( $current ) ) ) ? 'class="current"' : ( $current == $key ) ? 'class="current"' : '',
342
					$name,
343
					$count
344
				);
345
			}
346
		}
347
348
		return apply_filters( 'give_payments_table_views', $views, $this );
349
	}
350
351
	/**
352
	 * Retrieve the table columns
353
	 *
354
	 * @access public
355
	 * @since  1.0
356
	 *
357
	 * @return array $columns Array of all the list table columns
358
	 */
359
	public function get_columns() {
360
		$columns = array(
361
			'cb'            => '<input type="checkbox" />', // Render a checkbox instead of text.
362
			'donation'      => __( 'Donation', 'give' ),
363
			'donation_form' => __( 'Donation Form', 'give' ),
364
			'status'        => __( 'Status', 'give' ),
365
			'date'          => __( 'Date', 'give' ),
366
			'amount'        => __( 'Amount', 'give' ),
367
		);
368
369
		if ( current_user_can( 'view_give_payments' ) ) {
370
			$columns['details'] = __( 'Details', 'give' );
371
		}
372
373
		return apply_filters( 'give_payments_table_columns', $columns );
374
	}
375
376
	/**
377
	 * Retrieve the table's sortable columns
378
	 *
379
	 * @access public
380
	 * @since  1.0
381
	 *
382
	 * @return array Array of all the sortable columns
383
	 */
384
	public function get_sortable_columns() {
385
		$columns = array(
386
			'donation'      => array( 'ID', true ),
387
			'donation_form' => array( 'donation_form', false ),
388
			'status'        => array( 'status', false ),
389
			'amount'        => array( 'amount', false ),
390
			'date'          => array( 'date', false ),
391
		);
392
393
		return apply_filters( 'give_payments_table_sortable_columns', $columns );
394
	}
395
396
	/**
397
	 * Gets the name of the primary column.
398
	 *
399
	 * @since  1.5
400
	 * @access protected
401
	 *
402
	 * @return string Name of the primary column.
403
	 */
404
	protected function get_primary_column_name() {
405
		return 'donation';
406
	}
407
408
	/**
409
	 * This function renders most of the columns in the list table.
410
	 *
411
	 * @param Give_Payment $payment     Payment ID.
412
	 * @param string       $column_name The name of the column.
413
	 *
414
	 * @access public
415
	 * @since  1.0
416
	 *
417
	 * @return string Column Name
418
	 */
419
	public function column_default( $payment, $column_name ) {
420
421
		$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' ) ) );
422
		$row_actions         = $this->get_row_actions( $payment );
423
		$value               = '';
424
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
425
426
		switch ( $column_name ) {
427
			case 'donation' :
428
				if ( current_user_can( 'view_give_payments' ) ) {
429
					$value = Give()->tooltips->render_link( array(
430
						'label'       => sprintf( __( 'View Donation #%s', 'give' ), $payment->ID ),
431
						'tag_content' => "#$payment->ID",
432
						'link'        => $single_donation_url,
433
					) );
434
				} else {
435
					$value = "#{$payment->ID}";
436
				}
437
438
				$value .= sprintf(
439
					'&nbsp;%1$s&nbsp;%2$s<br>',
440
					__( 'by', 'give' ),
441
					$this->get_donor( $payment )
442
				);
443
444
				$value .= $this->get_donor_email( $payment );
445
				$value .= $this->row_actions( $row_actions );
446
				break;
447
448
			case 'amount':
449
				$amount = ! empty( $payment->total ) ? $payment->total : 0;
450
				$value  = give_currency_filter(
451
					give_format_amount(
452
						$amount, array(
453
							'sanitize'    => false,
454
							'donation_id' => $payment->ID,
455
						)
456
					), give_get_payment_currency_code( $payment->ID )
457
				);
458
				$value .= sprintf( '<br><small>%1$s %2$s</small>', __( 'via', 'give' ), give_get_gateway_admin_label( $payment->gateway ) );
459
				break;
460
461
			case 'donation_form':
462
				$form_title = empty( $payment->form_title ) ? sprintf( __( 'Untitled (#%s)', 'give' ), $payment->form_id ) : $payment->form_title;
463
				$value      = '<a href="' . admin_url( 'post.php?post=' . $payment->form_id . '&action=edit' ) . '">' . $form_title . '</a>';
464
				$level      = give_get_payment_form_title( $payment->meta, true );
465
466
				if ( ! empty( $level ) ) {
467
					$value .= $level;
468
				}
469
470
				break;
471
472
			case 'date':
473
				$date  = strtotime( $payment->date );
474
				$value = date_i18n( give_date_format(), $date );
475
				break;
476
477
			case 'status':
478
				$value = $this->get_payment_status( $payment );
479
				break;
480
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
481
482
			case 'details' :
483
				if ( current_user_can( 'view_give_payments' ) ) {
484
					$value = Give()->tooltips->render_link( array(
485
						'label'       => sprintf( __( 'View Donation #%s', 'give' ), $payment->ID ),
486
						'tag_content' => '<span class="dashicons dashicons-visibility"></span>',
487
						'link'        => $single_donation_url,
488
						'attributes'  => array(
489
							'class' => 'give-payment-details-link button button-small',
490
						),
491
					) );
492
493
					$value = "<div class=\"give-payment-details-link-wrap\">{$value}</div>";
494
				}
495
				break;
496
497
			default:
498
				$value = isset( $payment->$column_name ) ? $payment->$column_name : '';
499
				break;
500
501
		}// End switch().
502
503
		return apply_filters( 'give_payments_table_column', $value, $payment->ID, $column_name );
504
	}
505
506
	/**
507
	 * Get donor email html.
508
	 *
509
	 * @param object $payment Contains all the data of the payment.
510
	 *
511
	 * @access public
512
	 * @since  1.0
513
	 *
514
	 * @return string Data shown in the Email column
515
	 */
516
	public function get_donor_email( $payment ) {
517
518
		$email = give_get_payment_user_email( $payment->ID );
519
520
		if ( empty( $email ) ) {
521
			$email = __( '(unknown)', 'give' );
522
		}
523
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
524
525
		$value = Give()->tooltips->render_link( array(
526
			'link'        => "mailto:{$email}",
527
			'label'       => __( 'Email donor', 'give' ),
528
			'tag_content' => $email,
529
		) );
530
531
		return apply_filters( 'give_payments_table_column', $value, $payment->ID, 'email' );
532
	}
533
534
	/**
535
	 * Get Row Actions
536
	 *
537
	 * @param object $payment Payment Data.
538
	 *
539
	 * @since 1.6
540
	 *
541
	 * @return array $actions
542
	 */
543
	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...
544
545
		$actions = array();
546
		$email   = give_get_payment_user_email( $payment->ID );
547
548
		// Add search term string back to base URL.
549
		$search_terms = ( isset( $_GET['s'] ) ? trim( $_GET['s'] ) : '' );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
550
		if ( ! empty( $search_terms ) ) {
551
			$this->base_url = add_query_arg( 's', $search_terms, $this->base_url );
552
		}
553
554 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...
555
556
			$actions['email_links'] = sprintf(
557
				'<a class="resend-single-donation-receipt" href="%1$s" aria-label="%2$s">%3$s</a>', wp_nonce_url(
558
					add_query_arg(
559
						array(
560
							'give-action' => 'email_links',
561
							'purchase_id' => $payment->ID,
562
						), $this->base_url
563
					), 'give_payment_nonce'
564
				), sprintf( __( 'Resend Donation %s Receipt', 'give' ), $payment->ID ), __( 'Resend Receipt', 'give' )
565
			);
566
567
		}
568
569 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...
570
			$actions['delete'] = sprintf(
571
				'<a class="delete-single-donation" href="%1$s" aria-label="%2$s">%3$s</a>',
572
				wp_nonce_url(
573
					add_query_arg(
574
						array(
575
							'give-action' => 'delete_payment',
576
							'purchase_id' => $payment->ID,
577
						), $this->base_url
578
					), 'give_donation_nonce'
579
				), sprintf( __( 'Delete Donation %s', 'give' ), $payment->ID ), __( 'Delete', 'give' )
580
			);
581
		}
582
583
		return apply_filters( 'give_payment_row_actions', $actions, $payment );
584
	}
585
586
587
	/**
588
	 *  Get payment status html.
589
	 *
590
	 * @since  1.0
591
	 * @access public
592
	 *
593
	 * @param Give_Payment $payment Contains all the data of the payment.
594
	 *
595
	 * @return string Data shown in the Email column
596
	 */
597
	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...
598
		$value = sprintf(
599
				'<div class="give-donation-status status-%1$s"><span class="give-donation-status-icon"></span>&nbsp;%2$s</div>',
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 16.
Loading history...
600
			sanitize_title( give_get_payment_status( $payment, true ) ),
601
			give_get_payment_status( $payment, true )
602
		);
603
604 View Code Duplication
		if ( $payment->mode == 'test' ) {
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...
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
605
			$value .= Give()->tooltips->render_span( array(
606
				'label'       => __( 'This donation was made in test mode.', 'give' ),
607
				'tag_content' => __( 'Test', 'give' ),
608
				'attributes'  => array(
609
					'class' => 'give-item-label give-item-label-orange give-test-mode-transactions-label',
610
				),
611
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
612
613
			) );
614
		}
615
616
		if ( true === $payment->import && true === (bool) apply_filters( 'give_payment_show_importer_label', false ) ) {
617
			$value .= sprintf(
618
					'&nbsp;<span class="give-item-label give-item-label-orange give-test-mode-transactions-label" data-tooltip="%1$s">%2$s</span>',
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 20.
Loading history...
619
				__( 'This donation was imported.', 'give' ),
620
				__( 'Import', 'give' )
621
			);
622
		}
623
624
		return $value;
625
	}
626
627
	/**
628
	 * Get checkbox html.
629
	 *
630
	 * @param object $payment Contains all the data for the checkbox column.
631
	 *
632
	 * @access public
633
	 * @since  1.0
634
	 *
635
	 * @return string Displays a checkbox.
636
	 */
637
	public function column_cb( $payment ) {
638
		return sprintf( '<input type="checkbox" name="%1$s[]" value="%2$s" />', 'payment', $payment->ID );
639
	}
640
641
	/**
642
	 * Get payment ID html.
643
	 *
644
	 * @param object $payment Contains all the data for the checkbox column.
645
	 *
646
	 * @access public
647
	 * @since  1.0
648
	 *
649
	 * @return string Displays a checkbox.
650
	 */
651
	public function get_payment_id( $payment ) {
652
		return '<span class="give-payment-id">' . give_get_payment_number( $payment->ID ) . '</span>';
653
	}
654
655
	/**
656
	 * Get donor html.
657
	 *
658
	 * @param object $payment Contains all the data of the payment.
659
	 *
660
	 * @access public
661
	 * @since  1.0
662
	 *
663
	 * @return string Data shown in the User column
664
	 */
665
	public function get_donor( $payment ) {
666
667
		$donor_id           = give_get_payment_donor_id( $payment->ID );
668
		$donor_billing_name = give_get_donor_name_by( $payment->ID, 'donation' );
669
		$donor_name         = give_get_donor_name_by( $donor_id, 'donor' );
670
671
		$value = '';
672
		if ( ! empty( $donor_id ) ) {
673
674
			// Check whether the donor name and WP_User name is same or not.
675
			if ( sanitize_title( $donor_billing_name ) !== sanitize_title( $donor_name ) ) {
676
				$value .= $donor_billing_name . ' (';
677
			}
678
679
			$value .= '<a href="' . esc_url( admin_url( "edit.php?post_type=give_forms&page=give-donors&view=overview&id=$donor_id" ) ) . '">' . $donor_name . '</a>';
680
681
			// Check whether the donor name and WP_User name is same or not.
682
			if ( sanitize_title( $donor_billing_name ) != sanitize_title( $donor_name ) ) {
683
				$value .= ')';
684
			}
685
		} else {
686
			$email  = give_get_payment_user_email( $payment->ID );
687
			$value .= '<a href="' . esc_url( admin_url( "edit.php?post_type=give_forms&page=give-payment-history&s=$email" ) ) . '">' . __( '(donor missing)', 'give' ) . '</a>';
688
		}
689
690
		return apply_filters( 'give_payments_table_column', $value, $payment->ID, 'donor' );
691
	}
692
693
	/**
694
	 * Retrieve the bulk actions
695
	 *
696
	 * @access public
697
	 * @since  1.0
698
	 *
699
	 * @return array $actions Array of the bulk actions
700
	 */
701
	public function get_bulk_actions() {
702
		$actions = array(
703
			'delete'                 => __( 'Delete', 'give' ),
704
			'set-status-publish'     => __( 'Set To Completed', 'give' ),
705
			'set-status-pending'     => __( 'Set To Pending', 'give' ),
706
			'set-status-processing'  => __( 'Set To Processing', 'give' ),
707
			'set-status-refunded'    => __( 'Set To Refunded', 'give' ),
708
			'set-status-revoked'     => __( 'Set To Revoked', 'give' ),
709
			'set-status-failed'      => __( 'Set To Failed', 'give' ),
710
			'set-status-cancelled'   => __( 'Set To Cancelled', 'give' ),
711
			'set-status-abandoned'   => __( 'Set To Abandoned', 'give' ),
712
			'set-status-preapproval' => __( 'Set To Preapproval', 'give' ),
713
			'resend-receipt'         => __( 'Resend Email Receipts', 'give' ),
714
		);
715
716
		return apply_filters( 'give_payments_table_bulk_actions', $actions );
717
	}
718
719
	/**
720
	 * Process the bulk actions
721
	 *
722
	 * @access public
723
	 * @since  1.0
724
	 *
725
	 * @return void
726
	 */
727
	public function process_bulk_action() {
728
		$ids    = isset( $_GET['payment'] ) ? $_GET['payment'] : false;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
729
		$action = $this->current_action();
730
731
		if ( ! is_array( $ids ) ) {
732
			$ids = array( $ids );
733
		}
734
735
		if ( empty( $action ) ) {
736
			return;
737
		}
738
739
		foreach ( $ids as $id ) {
740
741
			// Detect when a bulk action is being triggered.
742
			switch ( $this->current_action() ) {
743
744
				case 'delete':
745
					give_delete_donation( $id );
746
					break;
747
748
				case 'set-status-publish':
749
					give_update_payment_status( $id, 'publish' );
750
					break;
751
752
				case 'set-status-pending':
753
					give_update_payment_status( $id, 'pending' );
754
					break;
755
756
				case 'set-status-processing':
757
					give_update_payment_status( $id, 'processing' );
758
					break;
759
760
				case 'set-status-refunded':
761
					give_update_payment_status( $id, 'refunded' );
762
					break;
763
				case 'set-status-revoked':
764
					give_update_payment_status( $id, 'revoked' );
765
					break;
766
767
				case 'set-status-failed':
768
					give_update_payment_status( $id, 'failed' );
769
					break;
770
771
				case 'set-status-cancelled':
772
					give_update_payment_status( $id, 'cancelled' );
773
					break;
774
775
				case 'set-status-abandoned':
776
					give_update_payment_status( $id, 'abandoned' );
777
					break;
778
779
				case 'set-status-preapproval':
780
					give_update_payment_status( $id, 'preapproval' );
781
					break;
782
783
				case 'resend-receipt':
784
					/**
785
					 * Fire the action
786
					 *
787
					 * @since 2.0
788
					 */
789
					do_action( 'give_donation-receipt_email_notification', $id );
790
					break;
791
			}// End switch().
792
793
			/**
794
			 * Fires after triggering bulk action on payments table.
795
			 *
796
			 * @param int    $id             The ID of the payment.
797
			 * @param string $current_action The action that is being triggered.
798
			 *
799
			 * @since 1.7
800
			 */
801
			do_action( 'give_payments_table_do_bulk_action', $id, $this->current_action() );
802
		}// End foreach().
803
804
	}
805
806
	/**
807
	 * Retrieve the payment counts
808
	 *
809
	 * @access public
810
	 * @since  1.0
811
	 *
812
	 * @return object
813
	 */
814
	public function get_payment_counts() {
815
816
		$args = array();
817
818
		if ( isset( $_GET['user'] ) ) {
819
			$args['user'] = urldecode( $_GET['user'] );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
820
		} elseif ( isset( $_GET['donor'] ) ) {
821
			$args['donor'] = absint( $_GET['donor'] );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
822
		} elseif ( isset( $_GET['s'] ) ) {
823
			$is_user = strpos( $_GET['s'], strtolower( 'user:' ) ) !== false;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
824
			if ( $is_user ) {
825
				$args['user'] = absint( trim( str_replace( 'user:', '', strtolower( $_GET['s'] ) ) ) );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
826
				unset( $args['s'] );
827
			} else {
828
				$args['s'] = sanitize_text_field( $_GET['s'] );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
829
			}
830
		}
831
832
		if ( ! empty( $_GET['start-date'] ) ) {
833
			$args['start-date'] = urldecode( $_GET['start-date'] );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
834
		}
835
836
		if ( ! empty( $_GET['end-date'] ) ) {
837
			$args['end-date'] = urldecode( $_GET['end-date'] );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
838
		}
839
840
		$args['form_id'] = ! empty( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : null;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
841
		$args['gateway'] = ! empty( $_GET['gateway'] ) ? give_clean( $_GET['gateway'] ) : null;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
842
843
		$payment_count           = give_count_payments( $args );
844
		$this->complete_count    = $payment_count->publish;
845
		$this->pending_count     = $payment_count->pending;
846
		$this->processing_count  = $payment_count->processing;
847
		$this->refunded_count    = $payment_count->refunded;
848
		$this->failed_count      = $payment_count->failed;
849
		$this->revoked_count     = $payment_count->revoked;
850
		$this->cancelled_count   = $payment_count->cancelled;
851
		$this->abandoned_count   = $payment_count->abandoned;
852
		$this->preapproval_count = $payment_count->preapproval;
853
854
		foreach ( $payment_count as $count ) {
855
			$this->total_count += $count;
856
		}
857
858
		return $payment_count;
859
	}
860
861
	/**
862
	 * Retrieve all the data for all the payments.
863
	 *
864
	 * @access public
865
	 * @since  1.0
866
	 *
867
	 * @return array  objects in array containing all the data for the payments
868
	 */
869
	public function payments_data() {
870
871
		$per_page   = $this->per_page;
872
		$orderby    = isset( $_GET['orderby'] ) ? urldecode( $_GET['orderby'] ) : 'ID';
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
873
		$order      = isset( $_GET['order'] ) ? $_GET['order'] : 'DESC';
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
874
		$user       = isset( $_GET['user'] ) ? $_GET['user'] : null;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
875
		$donor      = isset( $_GET['donor'] ) ? $_GET['donor'] : null;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
876
		$status     = isset( $_GET['status'] ) ? $_GET['status'] : give_get_payment_status_keys();
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
877
		$meta_key   = isset( $_GET['meta_key'] ) ? $_GET['meta_key'] : null;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
878
		$year       = isset( $_GET['year'] ) ? $_GET['year'] : null;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
879
		$month      = isset( $_GET['m'] ) ? $_GET['m'] : null;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
880
		$day        = isset( $_GET['day'] ) ? $_GET['day'] : null;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
881
		$search     = isset( $_GET['s'] ) ? sanitize_text_field( $_GET['s'] ) : null;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
882
		$start_date = isset( $_GET['start-date'] ) ? sanitize_text_field( $_GET['start-date'] ) : null;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
883
		$end_date   = isset( $_GET['end-date'] ) ? sanitize_text_field( $_GET['end-date'] ) : $start_date;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
884
		$form_id    = ! empty( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : null;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
885
		$gateway    = ! empty( $_GET['gateway'] ) ? give_clean( $_GET['gateway'] ) : null;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
886
887
		if ( ! empty( $search ) ) {
888
			$status = 'any'; // Force all payment statuses when searching.
889
		}
890
891
		$args = array(
892
			'output'     => 'payments',
893
			'number'     => $per_page,
894
			'page'       => isset( $_GET['paged'] ) ? $_GET['paged'] : null,
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
895
			'orderby'    => $orderby,
896
			'order'      => $order,
897
			'user'       => $user,
898
			'donor'      => $donor,
899
			'status'     => $status,
900
			'meta_key'   => $meta_key,
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
901
			'year'       => $year,
902
			'month'      => $month,
903
			'day'        => $day,
904
			's'          => $search,
905
			'start_date' => $start_date,
906
			'gateway'    => $gateway,
907
			'end_date'   => $end_date,
908
			'give_forms' => $form_id,
909
		);
910
911
		if ( is_string( $search ) && false !== strpos( $search, 'txn:' ) ) {
912
			$args['search_in_notes'] = true;
913
			$args['s']               = trim( str_replace( 'txn:', '', $args['s'] ) );
914
		}
915
916
		$p_query = new Give_Payments_Query( $args );
917
918
		return $p_query->get_payments();
919
920
	}
921
922
	/**
923
	 * Setup the final data for the table
924
	 *
925
	 * @access public
926
	 * @since  1.0
927
	 * @uses   Give_Payment_History_Table::get_columns()
928
	 * @uses   Give_Payment_History_Table::get_sortable_columns()
929
	 * @uses   Give_Payment_History_Table::payments_data()
930
	 * @uses   WP_List_Table::get_pagenum()
931
	 * @uses   WP_List_Table::set_pagination_args()
932
	 *
933
	 * @return void
934
	 */
935
	public function prepare_items() {
936
937
		wp_reset_vars( array( 'action', 'payment', 'orderby', 'order', 's' ) );
938
939
		$columns  = $this->get_columns();
940
		$hidden   = array(); // No hidden columns.
941
		$sortable = $this->get_sortable_columns();
942
		$data     = $this->payments_data();
943
		$status   = isset( $_GET['status'] ) ? $_GET['status'] : 'any';
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
944
945
		$this->_column_headers = array( $columns, $hidden, $sortable );
946
947
		switch ( $status ) {
948
			case 'publish':
949
				$total_items = $this->complete_count;
950
				break;
951
			case 'pending':
952
				$total_items = $this->pending_count;
953
				break;
954
			case 'processing':
955
				$total_items = $this->processing_count;
956
				break;
957
			case 'refunded':
958
				$total_items = $this->refunded_count;
959
				break;
960
			case 'failed':
961
				$total_items = $this->failed_count;
962
				break;
963
			case 'revoked':
964
				$total_items = $this->revoked_count;
965
				break;
966
			case 'cancelled':
967
				$total_items = $this->cancelled_count;
968
				break;
969
			case 'abandoned':
970
				$total_items = $this->abandoned_count;
971
				break;
972
			case 'preapproval':
973
				$total_items = $this->preapproval_count;
974
				break;
975
			case 'any':
976
				$total_items = $this->total_count;
977
				break;
978
			default:
979
				// Retrieve the count of the non-default-Give status.
980
				$count       = wp_count_posts( 'give_payment' );
981
				$total_items = isset( $count->{$status} ) ? $count->{$status} : 0;
982
				break;
983
		}
984
985
		$this->items = $data;
986
987
		$this->set_pagination_args(
988
			array(
989
				'total_items' => $total_items,
990
				// We have to calculate the total number of items.
991
				'per_page'    => $this->per_page,
992
				// We have to determine how many items to show on a page.
993
				'total_pages' => ceil( $total_items / $this->per_page ),
994
			// We have to calculate the total number of pages.
995
			)
996
		);
997
	}
998
}
999