Give_Payment_History_Table::get_views()   C
last analyzed

Complexity

Conditions 10
Paths 8

Size

Total Lines 98

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 8
nop 0
dl 0
loc 98
rs 6.1769
c 0
b 0
f 0

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