Test Failed
Push — issues/2610 ( 4e0645 )
by Ravinder
08:18
created

Give_Payment_History_Table::column_default()   D

Complexity

Conditions 13
Paths 14

Size

Total Lines 83
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 56
nc 14
nop 2
dl 0
loc 83
rs 4.9922
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'] ) ? 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
197
			/**
198
			 * Action to add hidden fields and HTML in Payment search.
199
			 *
200
			 * @since 1.8.18
201
			 */
202
			do_action( 'give_payment_table_advanced_filters' );
203
			?>
204
205
			<?php if ( ! empty( $status ) ) : ?>
206
				<input type="hidden" name="status" value="<?php echo esc_attr( $status ); ?>"/>
207
			<?php endif; ?>
208
209
			<div class="give-filter">
210
				<?php submit_button( __( 'Apply', 'give' ), 'secondary', '', false ); ?>
211
				<?php
212
				// Clear active filters button.
213
				if ( ! empty( $start_date ) || ! empty( $end_date ) || ! empty( $donor ) || ! empty( $search ) || ! empty( $status ) || ! empty( $form_id ) ) :
214
					?>
215
					<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...
216
					   class="button give-clear-filters-button"><?php _e( 'Clear Filters', 'give' ); ?></a>
217
				<?php endif; ?>
218
			</div>
219
		</div>
220
221
		<?php
222
	}
223
224
	/**
225
	 * Show the search field
226
	 *
227
	 * @param string $text     Label for the search box.
228
	 * @param string $input_id ID of the search box.
229
	 *
230
	 * @since  1.0
231
	 * @access public
232
	 *
233
	 * @return void
234
	 */
235
	public function search_box( $text, $input_id ) {
236
		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...
237
			return;
238
		}
239
240
		$input_id = $input_id . '-search-input';
241
242
		if ( ! empty( $_REQUEST['orderby'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
243
			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...
244
		}
245
		if ( ! empty( $_REQUEST['order'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
246
			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...
247
		}
248
		?>
249
		<div class="give-filter give-filter-search" role="search">
250
			<?php
251
			/**
252
			 * Fires in the payment history search box.
253
			 *
254
			 * Allows you to add new elements before the search box.
255
			 *
256
			 * @since 1.7
257
			 */
258
			do_action( 'give_payment_history_search' );
259
			?>
260
			<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...
261
			<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...
262
			<?php submit_button( $text, 'button', false, false, array(
263
				'ID' => 'search-submit',
264
			) ); ?><br/>
265
		</div>
266
		<?php
267
	}
268
269
	/**
270
	 * Retrieve the view types
271
	 *
272
	 * @access public
273
	 * @since  1.0
274
	 *
275
	 * @return array $views All the views available
276
	 */
277
	public function get_views() {
278
279
		$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...
280
		$views   = array();
281
		$tabs    = array(
282
			'all'         => array(
283
				'total_count',
284
				__( 'All', 'give' ),
285
			),
286
			'publish'     => array(
287
				'complete_count',
288
				__( 'Completed', 'give' ),
289
			),
290
			'pending'     => array(
291
				'pending_count',
292
				__( 'Pending', 'give' ),
293
			),
294
			'processing'  => array(
295
				'processing_count',
296
				__( 'Processing', 'give' ),
297
			),
298
			'refunded'    => array(
299
				'refunded_count',
300
				__( 'Refunded', 'give' ),
301
			),
302
			'revoked'     => array(
303
				'revoked_count',
304
				__( 'Revoked', 'give' ),
305
			),
306
			'failed'      => array(
307
				'failed_count',
308
				__( 'Failed', 'give' ),
309
			),
310
			'cancelled'   => array(
311
				'cancelled_count',
312
				__( 'Cancelled', 'give' ),
313
			),
314
			'abandoned'   => array(
315
				'abandoned_count',
316
				__( 'Abandoned', 'give' ),
317
			),
318
			'preapproval' => array(
319
				'preapproval_count',
320
				__( 'Preapproval Pending', 'give' ),
321
			),
322
		);
323
324
		/**
325
		 * Remove Query from Args of the URL that are being pass to Donation Status.
326
		 *
327
		 * @since 1.8.18
328
		 */
329
		$args = (array) apply_filters( 'give_payments_table_status_remove_query_arg', array( 'paged', '_wpnonce', '_wp_http_referer' ) );
330
331
		// Build URL.
332
		$staus_url = remove_query_arg( $args );
333
334
		foreach ( $tabs as $key => $tab ) {
335
			$count_key = $tab[0];
336
			$name      = $tab[1];
337
			$count     = $this->$count_key;
338
339
			/**
340
			 * Filter can be used to show all the status inside the donation tabs.
341
			 *
342
			 * Filter can be used to show all the status inside the donation submenu tabs return true to show all the tab.
343
			 *
344
			 * @param string $key Current view tab value.
345
			 * @param int $count Number of donation inside the tab.
346
			 *
347
			 * @since 1.8.12
348
			 */
349
			if ( 'all' === $key || $key === $current || apply_filters( 'give_payments_table_show_all_status', 0 < $count, $key, $count ) ) {
350
351
				$staus_url = 'all' === $key ?
352
					add_query_arg( array( 'status' => false ), $staus_url ) :
353
					add_query_arg( array( 'status' => $key ), $staus_url );
354
355
				$views[ $key ] = sprintf(
356
					'<a href="%s"%s>%s&nbsp;<span class="count">(%s)</span></a>',
357
					esc_url( $staus_url ),
358
					( ( 'all' === $key && empty( $current ) ) ) ? ' class="current"' : ( $current == $key ? 'class="current"' : '' ),
359
					$name,
360
					$count
361
				);
362
			}
363
		}
364
365
		/**
366
		 * Filter the donation listing page views.
367
		 *
368
		 * @since 1.0
369
		 *
370
		 * @param array $views
371
		 * @param Give_Payment_History_Table
372
		 */
373
		return apply_filters( 'give_payments_table_views', $views, $this );
374
	}
375
376
	/**
377
	 * Retrieve the table columns
378
	 *
379
	 * @access public
380
	 * @since  1.0
381
	 *
382
	 * @return array $columns Array of all the list table columns
383
	 */
384
	public function get_columns() {
385
		$columns = array(
386
			'cb'            => '<input type="checkbox" />', // Render a checkbox instead of text.
387
			'donation'      => __( 'Donation', 'give' ),
388
			'donation_form' => __( 'Donation Form', 'give' ),
389
			'status'        => __( 'Status', 'give' ),
390
			'date'          => __( 'Date', 'give' ),
391
			'amount'        => __( 'Amount', 'give' ),
392
		);
393
394
		if ( current_user_can( 'view_give_payments' ) ) {
395
			$columns['details'] = __( 'Details', 'give' );
396
		}
397
398
		return apply_filters( 'give_payments_table_columns', $columns );
399
	}
400
401
	/**
402
	 * Retrieve the table's sortable columns
403
	 *
404
	 * @access public
405
	 * @since  1.0
406
	 *
407
	 * @return array Array of all the sortable columns
408
	 */
409
	public function get_sortable_columns() {
410
		$columns = array(
411
			'donation'      => array( 'ID', true ),
412
			'donation_form' => array( 'donation_form', false ),
413
			'status'        => array( 'status', false ),
414
			'amount'        => array( 'amount', false ),
415
			'date'          => array( 'date', false ),
416
		);
417
418
		return apply_filters( 'give_payments_table_sortable_columns', $columns );
419
	}
420
421
	/**
422
	 * Gets the name of the primary column.
423
	 *
424
	 * @since  1.5
425
	 * @access protected
426
	 *
427
	 * @return string Name of the primary column.
428
	 */
429
	protected function get_primary_column_name() {
430
		return 'donation';
431
	}
432
433
	/**
434
	 * This function renders most of the columns in the list table.
435
	 *
436
	 * @param Give_Payment $payment     Payment ID.
437
	 * @param string       $column_name The name of the column.
438
	 *
439
	 * @access public
440
	 * @since  1.0
441
	 *
442
	 * @return string Column Name
443
	 */
444
	public function column_default( $payment, $column_name ) {
445
446
		$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' ) ) );
447
		$row_actions         = $this->get_row_actions( $payment );
448
		$value               = '';
449
450
		switch ( $column_name ) {
451
			case 'donation' :
452
				if ( current_user_can( 'view_give_payments' ) ) {
453
					$value = Give()->tooltips->render_link( array(
454
						'label'       => sprintf( __( 'View Donation #%s', 'give' ), $payment->ID ),
455
						'tag_content' => "#$payment->ID",
456
						'link'        => $single_donation_url,
457
					) );
458
				} else {
459
					$value = "#{$payment->ID}";
460
				}
461
462
				$value .= sprintf(
463
					'&nbsp;%1$s&nbsp;%2$s<br>',
464
					__( 'by', 'give' ),
465
					$this->get_donor( $payment )
466
				);
467
468
				$value .= $this->get_donor_email( $payment );
469
				$value .= $this->row_actions( $row_actions );
470
				break;
471
472
			case 'amount':
473
				$amount = ! empty( $payment->total ) ? $payment->total : 0;
0 ignored issues
show
Unused Code introduced by
$amount is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
474
				$value  = give_donation_amount( $payment, true );
475
				$value .= sprintf( '<br><small>%1$s %2$s</small>', __( 'via', 'give' ), give_get_gateway_admin_label( $payment->gateway ) );
476
				break;
477
478
			case 'donation_form':
479
				$form_title = empty( $payment->form_title ) ? sprintf( __( 'Untitled (#%s)', 'give' ), $payment->form_id ) : $payment->form_title;
480
				$value      = '<a href="' . admin_url( 'post.php?post=' . $payment->form_id . '&action=edit' ) . '">' . $form_title . '</a>';
481
				$level      = give_get_donation_form_title(
482
					$payment,
483
					array(
484
						'only_level' => true,
485
					)
486
				);
487
488
				if ( ! empty( $level ) ) {
489
					$value .= $level;
490
				}
491
492
				break;
493
494
			case 'date':
495
				$date  = strtotime( $payment->date );
496
				$value = date_i18n( give_date_format(), $date );
497
				break;
498
499
			case 'status':
500
				$value = $this->get_payment_status( $payment );
501
				break;
502
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
503
504
			case 'details' :
505
				if ( current_user_can( 'view_give_payments' ) ) {
506
					$value = Give()->tooltips->render_link( array(
507
						'label'       => sprintf( __( 'View Donation #%s', 'give' ), $payment->ID ),
508
						'tag_content' => '<span class="dashicons dashicons-visibility"></span>',
509
						'link'        => $single_donation_url,
510
						'attributes'  => array(
511
							'class' => 'give-payment-details-link button button-small',
512
						),
513
					) );
514
515
					$value = "<div class=\"give-payment-details-link-wrap\">{$value}</div>";
516
				}
517
				break;
518
519
			default:
520
				$value = isset( $payment->$column_name ) ? $payment->$column_name : '';
521
				break;
522
523
		}// End switch().
524
525
		return apply_filters( 'give_payments_table_column', $value, $payment->ID, $column_name );
526
	}
527
528
	/**
529
	 * Get donor email html.
530
	 *
531
	 * @param object $payment Contains all the data of the payment.
532
	 *
533
	 * @access public
534
	 * @since  1.0
535
	 *
536
	 * @return string Data shown in the Email column
537
	 */
538
	public function get_donor_email( $payment ) {
539
540
		$email = give_get_payment_user_email( $payment->ID );
541
542
		if ( empty( $email ) ) {
543
			$email = __( '(unknown)', 'give' );
544
		}
545
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
546
547
		$value = Give()->tooltips->render_link( array(
548
			'link'        => "mailto:{$email}",
549
			'label'       => __( 'Email donor', 'give' ),
550
			'tag_content' => $email,
551
		) );
552
553
		return apply_filters( 'give_payments_table_column', $value, $payment->ID, 'email' );
554
	}
555
556
	/**
557
	 * Get Row Actions
558
	 *
559
	 * @param object $payment Payment Data.
560
	 *
561
	 * @since 1.6
562
	 *
563
	 * @return array $actions
564
	 */
565
	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...
566
567
		$actions = array();
568
		$email   = give_get_payment_user_email( $payment->ID );
569
570
		// Add search term string back to base URL.
571
		$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...
572
		if ( ! empty( $search_terms ) ) {
573
			$this->base_url = add_query_arg( 's', $search_terms, $this->base_url );
574
		}
575
576 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...
577
578
			$actions['email_links'] = sprintf(
579
				'<a class="resend-single-donation-receipt" href="%1$s" aria-label="%2$s">%3$s</a>', wp_nonce_url(
580
				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...
581
					array(
582
						'give-action' => 'email_links',
583
						'purchase_id' => $payment->ID,
584
					), $this->base_url
585
				), '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...
586
			), 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...
587
			);
588
589
		}
590
591 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...
592
			$actions['delete'] = sprintf(
593
				'<a class="delete-single-donation" href="%1$s" aria-label="%2$s">%3$s</a>',
594
				wp_nonce_url(
595
					add_query_arg(
596
						array(
597
							'give-action' => 'delete_payment',
598
							'purchase_id' => $payment->ID,
599
						), $this->base_url
600
					), 'give_donation_nonce'
601
				), sprintf( __( 'Delete Donation %s', 'give' ), $payment->ID ), __( 'Delete', 'give' )
602
			);
603
		}
604
605
		return apply_filters( 'give_payment_row_actions', $actions, $payment );
606
	}
607
608
609
	/**
610
	 *  Get payment status html.
611
	 *
612
	 * @since  1.0
613
	 * @access public
614
	 *
615
	 * @param Give_Payment $payment Contains all the data of the payment.
616
	 *
617
	 * @return string Data shown in the Email column
618
	 */
619
	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...
620
		$value = sprintf(
621
			'<div class="give-donation-status status-%1$s"><span class="give-donation-status-icon"></span>&nbsp;%2$s</div>',
622
			sanitize_title( give_get_payment_status( $payment, true ) ),
623
			give_get_payment_status( $payment, true )
624
		);
625
626 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...
627
			$value .= Give()->tooltips->render_span( array(
628
				'label'       => __( 'This donation was made in test mode.', 'give' ),
629
				'tag_content' => __( 'Test', 'give' ),
630
				'attributes'  => array(
631
					'class' => 'give-item-label give-item-label-orange give-test-mode-transactions-label',
632
				),
633
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
634
635
			) );
636
		}
637
638
		if ( true === $payment->import && true === (bool) apply_filters( 'give_payment_show_importer_label', false ) ) {
639
			$value .= sprintf(
640
				'&nbsp;<span class="give-item-label give-item-label-orange give-test-mode-transactions-label" data-tooltip="%1$s">%2$s</span>',
641
				__( 'This donation was imported.', 'give' ),
642
				__( 'Import', 'give' )
643
			);
644
		}
645
646
		return $value;
647
	}
648
649
	/**
650
	 * Get checkbox html.
651
	 *
652
	 * @param object $payment Contains all the data for the checkbox column.
653
	 *
654
	 * @access public
655
	 * @since  1.0
656
	 *
657
	 * @return string Displays a checkbox.
658
	 */
659
	public function column_cb( $payment ) {
660
		return sprintf( '<input type="checkbox" name="%1$s[]" value="%2$s" />', 'payment', $payment->ID );
661
	}
662
663
	/**
664
	 * Get payment ID html.
665
	 *
666
	 * @param object $payment Contains all the data for the checkbox column.
667
	 *
668
	 * @access public
669
	 * @since  1.0
670
	 *
671
	 * @return string Displays a checkbox.
672
	 */
673
	public function get_payment_id( $payment ) {
674
		return '<span class="give-payment-id">' . give_get_payment_number( $payment->ID ) . '</span>';
675
	}
676
677
	/**
678
	 * Get donor html.
679
	 *
680
	 * @param object $payment Contains all the data of the payment.
681
	 *
682
	 * @access public
683
	 * @since  1.0
684
	 *
685
	 * @return string Data shown in the User column
686
	 */
687
	public function get_donor( $payment ) {
688
689
		$donor_id           = give_get_payment_donor_id( $payment->ID );
690
		$donor_billing_name = give_get_donor_name_by( $payment->ID, 'donation' );
691
		$donor_name         = give_get_donor_name_by( $donor_id, 'donor' );
692
693
		$value = '';
694
		if ( ! empty( $donor_id ) ) {
695
696
			// Check whether the donor name and WP_User name is same or not.
697
			if ( sanitize_title( $donor_billing_name ) !== sanitize_title( $donor_name ) ) {
698
				$value .= $donor_billing_name . ' (';
699
			}
700
701
			$value .= '<a href="' . esc_url( admin_url( "edit.php?post_type=give_forms&page=give-donors&view=overview&id=$donor_id" ) ) . '">' . $donor_name . '</a>';
702
703
			// Check whether the donor name and WP_User name is same or not.
704
			if ( sanitize_title( $donor_billing_name ) != sanitize_title( $donor_name ) ) {
705
				$value .= ')';
706
			}
707
		} else {
708
			$email  = give_get_payment_user_email( $payment->ID );
709
			$value .= '<a href="' . esc_url( admin_url( "edit.php?post_type=give_forms&page=give-payment-history&s=$email" ) ) . '">' . __( '(donor missing)', 'give' ) . '</a>';
710
		}
711
712
		return apply_filters( 'give_payments_table_column', $value, $payment->ID, 'donor' );
713
	}
714
715
	/**
716
	 * Retrieve the bulk actions
717
	 *
718
	 * @access public
719
	 * @since  1.0
720
	 *
721
	 * @return array $actions Array of the bulk actions
722
	 */
723
	public function get_bulk_actions() {
724
		$actions = array(
725
			'delete'                 => __( 'Delete', 'give' ),
726
			'set-status-publish'     => __( 'Set To Completed', 'give' ),
727
			'set-status-pending'     => __( 'Set To Pending', 'give' ),
728
			'set-status-processing'  => __( 'Set To Processing', 'give' ),
729
			'set-status-refunded'    => __( 'Set To Refunded', 'give' ),
730
			'set-status-revoked'     => __( 'Set To Revoked', 'give' ),
731
			'set-status-failed'      => __( 'Set To Failed', 'give' ),
732
			'set-status-cancelled'   => __( 'Set To Cancelled', 'give' ),
733
			'set-status-abandoned'   => __( 'Set To Abandoned', 'give' ),
734
			'set-status-preapproval' => __( 'Set To Preapproval', 'give' ),
735
			'resend-receipt'         => __( 'Resend Email Receipts', 'give' ),
736
		);
737
738
		return apply_filters( 'give_payments_table_bulk_actions', $actions );
739
	}
740
741
	/**
742
	 * Process the bulk actions
743
	 *
744
	 * @access public
745
	 * @since  1.0
746
	 *
747
	 * @return void
748
	 */
749
	public function process_bulk_action() {
750
		$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...
751
		$action = $this->current_action();
752
753
		if ( ! is_array( $ids ) ) {
754
			$ids = array( $ids );
755
		}
756
757
		if ( empty( $action ) ) {
758
			return;
759
		}
760
761
		foreach ( $ids as $id ) {
762
763
			// Detect when a bulk action is being triggered.
764
			switch ( $this->current_action() ) {
765
766
				case 'delete':
767
					give_delete_donation( $id );
768
					break;
769
770
				case 'set-status-publish':
771
					give_update_payment_status( $id, 'publish' );
772
					break;
773
774
				case 'set-status-pending':
775
					give_update_payment_status( $id, 'pending' );
776
					break;
777
778
				case 'set-status-processing':
779
					give_update_payment_status( $id, 'processing' );
780
					break;
781
782
				case 'set-status-refunded':
783
					give_update_payment_status( $id, 'refunded' );
784
					break;
785
				case 'set-status-revoked':
786
					give_update_payment_status( $id, 'revoked' );
787
					break;
788
789
				case 'set-status-failed':
790
					give_update_payment_status( $id, 'failed' );
791
					break;
792
793
				case 'set-status-cancelled':
794
					give_update_payment_status( $id, 'cancelled' );
795
					break;
796
797
				case 'set-status-abandoned':
798
					give_update_payment_status( $id, 'abandoned' );
799
					break;
800
801
				case 'set-status-preapproval':
802
					give_update_payment_status( $id, 'preapproval' );
803
					break;
804
805
				case 'resend-receipt':
806
					/**
807
					 * Fire the action
808
					 *
809
					 * @since 2.0
810
					 */
811
					do_action( 'give_donation-receipt_email_notification', $id );
812
					break;
813
			}// End switch().
814
815
			/**
816
			 * Fires after triggering bulk action on payments table.
817
			 *
818
			 * @param int    $id             The ID of the payment.
819
			 * @param string $current_action The action that is being triggered.
820
			 *
821
			 * @since 1.7
822
			 */
823
			do_action( 'give_payments_table_do_bulk_action', $id, $this->current_action() );
824
		}// End foreach().
825
826
	}
827
828
	/**
829
	 * Retrieve the payment counts
830
	 *
831
	 * @access public
832
	 * @since  1.0
833
	 *
834
	 * @return object
835
	 */
836
	public function get_payment_counts() {
837
838
		$args = array();
839
840
		if ( isset( $_GET['user'] ) ) {
841
			$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...
842
		} elseif ( isset( $_GET['donor'] ) ) {
843
			$args['donor'] = absint( $_GET['donor'] );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
844
		} elseif ( isset( $_GET['s'] ) ) {
845
			$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...
846
			if ( $is_user ) {
847
				$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...
848
				unset( $args['s'] );
849
			} else {
850
				$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...
851
			}
852
		}
853
854
		if ( ! empty( $_GET['start-date'] ) ) {
855
			$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...
856
		}
857
858
		if ( ! empty( $_GET['end-date'] ) ) {
859
			$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...
860
		}
861
862
		$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...
863
		$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...
864
865
		$payment_count           = give_count_payments( $args );
866
		$this->complete_count    = $payment_count->publish;
867
		$this->pending_count     = $payment_count->pending;
868
		$this->processing_count  = $payment_count->processing;
869
		$this->refunded_count    = $payment_count->refunded;
870
		$this->failed_count      = $payment_count->failed;
871
		$this->revoked_count     = $payment_count->revoked;
872
		$this->cancelled_count   = $payment_count->cancelled;
873
		$this->abandoned_count   = $payment_count->abandoned;
874
		$this->preapproval_count = $payment_count->preapproval;
875
876
		foreach ( $payment_count as $count ) {
877
			$this->total_count += $count;
878
		}
879
880
		return $payment_count;
881
	}
882
883
	/**
884
	 * Retrieve all the data for all the payments.
885
	 *
886
	 * @access public
887
	 * @since  1.0
888
	 *
889
	 * @return array  objects in array containing all the data for the payments
890
	 */
891
	public function payments_data() {
892
893
		$per_page   = $this->per_page;
894
		$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...
895
		$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...
896
		$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...
897
		$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...
898
		$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...
899
		$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...
900
		$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...
901
		$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...
902
		$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...
903
		$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...
904
		$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...
905
		$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...
906
		$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...
907
		$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...
908
909
		$args = array(
910
			'output'     => 'payments',
911
			'number'     => $per_page,
912
			'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...
913
			'orderby'    => $orderby,
914
			'order'      => $order,
915
			'user'       => $user,
916
			'donor'      => $donor,
917
			'status'     => $status,
918
			'meta_key'   => $meta_key,
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
919
			'year'       => $year,
920
			'month'      => $month,
921
			'day'        => $day,
922
			's'          => $search,
923
			'start_date' => $start_date,
924
			'gateway'    => $gateway,
925
			'end_date'   => $end_date,
926
			'give_forms' => $form_id,
927
		);
928
929
		if ( is_string( $search ) && false !== strpos( $search, 'txn:' ) ) {
930
			$args['search_in_notes'] = true;
931
			$args['s']               = trim( str_replace( 'txn:', '', $args['s'] ) );
932
		}
933
934
		/**
935
		 * Filter to modify payment table argument.
936
		 *
937
		 * @since 1.8.18
938
		 */
939
		$args = (array) apply_filters( 'give_payment_table_payments_query', $args );
940
941
		$p_query = new Give_Payments_Query( $args );
942
943
		return $p_query->get_payments();
944
945
	}
946
947
	/**
948
	 * Setup the final data for the table
949
	 *
950
	 * @access public
951
	 * @since  1.0
952
	 * @uses   Give_Payment_History_Table::get_columns()
953
	 * @uses   Give_Payment_History_Table::get_sortable_columns()
954
	 * @uses   Give_Payment_History_Table::payments_data()
955
	 * @uses   WP_List_Table::get_pagenum()
956
	 * @uses   WP_List_Table::set_pagination_args()
957
	 *
958
	 * @return void
959
	 */
960
	public function prepare_items() {
961
962
		wp_reset_vars( array( 'action', 'payment', 'orderby', 'order', 's' ) );
963
964
		$columns  = $this->get_columns();
965
		$hidden   = array(); // No hidden columns.
966
		$sortable = $this->get_sortable_columns();
967
		$data     = $this->payments_data();
968
		$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...
969
970
		$this->_column_headers = array( $columns, $hidden, $sortable );
971
972
		switch ( $status ) {
973
			case 'publish':
974
				$total_items = $this->complete_count;
975
				break;
976
			case 'pending':
977
				$total_items = $this->pending_count;
978
				break;
979
			case 'processing':
980
				$total_items = $this->processing_count;
981
				break;
982
			case 'refunded':
983
				$total_items = $this->refunded_count;
984
				break;
985
			case 'failed':
986
				$total_items = $this->failed_count;
987
				break;
988
			case 'revoked':
989
				$total_items = $this->revoked_count;
990
				break;
991
			case 'cancelled':
992
				$total_items = $this->cancelled_count;
993
				break;
994
			case 'abandoned':
995
				$total_items = $this->abandoned_count;
996
				break;
997
			case 'preapproval':
998
				$total_items = $this->preapproval_count;
999
				break;
1000
			case 'any':
1001
				$total_items = $this->total_count;
1002
				break;
1003
			default:
1004
				// Retrieve the count of the non-default-Give status.
1005
				$count       = wp_count_posts( 'give_payment' );
1006
				$total_items = isset( $count->{$status} ) ? $count->{$status} : 0;
1007
				break;
1008
		}
1009
1010
		$this->items = $data;
1011
1012
		/**
1013
		 * Filter to modify total count of the pagination.
1014
		 *
1015
		 * @since 1.8.19
1016
		 */
1017
		$total_items = (int) apply_filters( 'give_payment_table_pagination_total_count', $total_items, $this );
1018
1019
		$this->set_pagination_args(
1020
			array(
1021
				'total_items' => $total_items,
1022
				// We have to calculate the total number of items.
1023
				'per_page'    => $this->per_page,
1024
				// We have to determine how many items to show on a page.
1025
				'total_pages' => ceil( $total_items / $this->per_page ),
1026
				// We have to calculate the total number of pages.
1027
			)
1028
		);
1029
	}
1030
}
1031