Test Failed
Push — issues/1944 ( f13954...e5d7bc )
by Ravinder
05:26
created

Give_Payment_History_Table::column_default()   C

Complexity

Conditions 13
Paths 14

Size

Total Lines 78
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 53
nc 14
nop 2
dl 0
loc 78
rs 5.1663
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 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
				// Build URL.
332
				$staus_url = remove_query_arg( array( 'paged', '_wpnonce', '_wp_http_referer' ) );
333
				$staus_url = 'all' === $key ? add_query_arg( array( 'status' => false ), $staus_url ) : add_query_arg( array( 'status' => $key ), $staus_url );
334
335
				$views[ $key ] = sprintf(
336
					'<a href="%s"%s>%s&nbsp;<span class="count">(%s)</span></a>',
337
					esc_url( $staus_url ),
338
					( ( 'all' === $key && empty( $current ) ) ) ? ' class="current"' : ( $current == $key ? 'class="current"' : '' ),
339
					$name,
340
					$count
341
				);
342
			}
343
		}
344
345
		return apply_filters( 'give_payments_table_views', $views, $this );
346
	}
347
348
	/**
349
	 * Retrieve the table columns
350
	 *
351
	 * @access public
352
	 * @since  1.0
353
	 *
354
	 * @return array $columns Array of all the list table columns
355
	 */
356
	public function get_columns() {
357
		$columns = array(
358
			'cb'            => '<input type="checkbox" />', // Render a checkbox instead of text.
359
			'donation'      => __( 'Donation', 'give' ),
360
			'donation_form' => __( 'Donation Form', 'give' ),
361
			'status'        => __( 'Status', 'give' ),
362
			'date'          => __( 'Date', 'give' ),
363
			'amount'        => __( 'Amount', 'give' ),
364
		);
365
366
		if ( current_user_can( 'view_give_payments' ) ) {
367
			$columns['details'] = __( 'Details', 'give' );
368
		}
369
370
		return apply_filters( 'give_payments_table_columns', $columns );
371
	}
372
373
	/**
374
	 * Retrieve the table's sortable columns
375
	 *
376
	 * @access public
377
	 * @since  1.0
378
	 *
379
	 * @return array Array of all the sortable columns
380
	 */
381
	public function get_sortable_columns() {
382
		$columns = array(
383
			'donation'      => array( 'ID', true ),
384
			'donation_form' => array( 'donation_form', false ),
385
			'status'        => array( 'status', false ),
386
			'amount'        => array( 'amount', false ),
387
			'date'          => array( 'date', false ),
388
		);
389
390
		return apply_filters( 'give_payments_table_sortable_columns', $columns );
391
	}
392
393
	/**
394
	 * Gets the name of the primary column.
395
	 *
396
	 * @since  1.5
397
	 * @access protected
398
	 *
399
	 * @return string Name of the primary column.
400
	 */
401
	protected function get_primary_column_name() {
402
		return 'donation';
403
	}
404
405
	/**
406
	 * This function renders most of the columns in the list table.
407
	 *
408
	 * @param Give_Payment $payment     Payment ID.
409
	 * @param string       $column_name The name of the column.
410
	 *
411
	 * @access public
412
	 * @since  1.0
413
	 *
414
	 * @return string Column Name
415
	 */
416
	public function column_default( $payment, $column_name ) {
417
418
		$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' ) ) );
419
		$row_actions         = $this->get_row_actions( $payment );
420
		$value               = '';
421
422
		switch ( $column_name ) {
423
			case 'donation' :
424
				if ( current_user_can( 'view_give_payments' ) ) {
425
					$value = Give()->tooltips->render_link( array(
426
						'label'       => sprintf( __( 'View Donation #%s', 'give' ), $payment->ID ),
427
						'tag_content' => "#$payment->ID",
428
						'link'        => $single_donation_url,
429
					) );
430
				} else {
431
					$value = "#{$payment->ID}";
432
				}
433
434
				$value .= sprintf(
435
					'&nbsp;%1$s&nbsp;%2$s<br>',
436
					__( 'by', 'give' ),
437
					$this->get_donor( $payment )
438
				);
439
440
				$value .= $this->get_donor_email( $payment );
441
				$value .= $this->row_actions( $row_actions );
442
				break;
443
444
			case 'amount':
445
				$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...
446
				$value  = give_donation_amount( $payment, true );
447
				$value .= sprintf( '<br><small>%1$s %2$s</small>', __( 'via', 'give' ), give_get_gateway_admin_label( $payment->gateway ) );
448
				break;
449
450
			case 'donation_form':
451
				$form_title = empty( $payment->form_title ) ? sprintf( __( 'Untitled (#%s)', 'give' ), $payment->form_id ) : $payment->form_title;
452
				$value      = '<a href="' . admin_url( 'post.php?post=' . $payment->form_id . '&action=edit' ) . '">' . $form_title . '</a>';
453
				$level      = give_get_payment_form_title( $payment->meta, true );
454
455
				if ( ! empty( $level ) ) {
456
					$value .= $level;
457
				}
458
459
				break;
460
461
			case 'date':
462
				$date  = strtotime( $payment->date );
463
				$value = date_i18n( give_date_format(), $date );
464
				break;
465
466
			case 'status':
467
				$value = $this->get_payment_status( $payment );
468
				break;
469
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
470
471
			case 'details' :
472
				if ( current_user_can( 'view_give_payments' ) ) {
473
					$value = Give()->tooltips->render_link( array(
474
						'label'       => sprintf( __( 'View Donation #%s', 'give' ), $payment->ID ),
475
						'tag_content' => '<span class="dashicons dashicons-visibility"></span>',
476
						'link'        => $single_donation_url,
477
						'attributes'  => array(
478
							'class' => 'give-payment-details-link button button-small',
479
						),
480
					) );
481
482
					$value = "<div class=\"give-payment-details-link-wrap\">{$value}</div>";
483
				}
484
				break;
485
486
			default:
487
				$value = isset( $payment->$column_name ) ? $payment->$column_name : '';
488
				break;
489
490
		}// End switch().
491
492
		return apply_filters( 'give_payments_table_column', $value, $payment->ID, $column_name );
493
	}
494
495
	/**
496
	 * Get donor email html.
497
	 *
498
	 * @param object $payment Contains all the data of the payment.
499
	 *
500
	 * @access public
501
	 * @since  1.0
502
	 *
503
	 * @return string Data shown in the Email column
504
	 */
505
	public function get_donor_email( $payment ) {
506
507
		$email = give_get_payment_user_email( $payment->ID );
508
509
		if ( empty( $email ) ) {
510
			$email = __( '(unknown)', 'give' );
511
		}
512
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
513
514
		$value = Give()->tooltips->render_link( array(
515
			'link'        => "mailto:{$email}",
516
			'label'       => __( 'Email donor', 'give' ),
517
			'tag_content' => $email,
518
		) );
519
520
		return apply_filters( 'give_payments_table_column', $value, $payment->ID, 'email' );
521
	}
522
523
	/**
524
	 * Get Row Actions
525
	 *
526
	 * @param object $payment Payment Data.
527
	 *
528
	 * @since 1.6
529
	 *
530
	 * @return array $actions
531
	 */
532
	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...
533
534
		$actions = array();
535
		$email   = give_get_payment_user_email( $payment->ID );
536
537
		// Add search term string back to base URL.
538
		$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...
539
		if ( ! empty( $search_terms ) ) {
540
			$this->base_url = add_query_arg( 's', $search_terms, $this->base_url );
541
		}
542
543 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...
544
545
			$actions['email_links'] = sprintf(
546
				'<a class="resend-single-donation-receipt" href="%1$s" aria-label="%2$s">%3$s</a>', wp_nonce_url(
547
					add_query_arg(
548
						array(
549
							'give-action' => 'email_links',
550
							'purchase_id' => $payment->ID,
551
						), $this->base_url
552
					), 'give_payment_nonce'
553
				), sprintf( __( 'Resend Donation %s Receipt', 'give' ), $payment->ID ), __( 'Resend Receipt', 'give' )
554
			);
555
556
		}
557
558 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...
559
			$actions['delete'] = sprintf(
560
				'<a class="delete-single-donation" href="%1$s" aria-label="%2$s">%3$s</a>',
561
				wp_nonce_url(
562
					add_query_arg(
563
						array(
564
							'give-action' => 'delete_payment',
565
							'purchase_id' => $payment->ID,
566
						), $this->base_url
567
					), 'give_donation_nonce'
568
				), sprintf( __( 'Delete Donation %s', 'give' ), $payment->ID ), __( 'Delete', 'give' )
569
			);
570
		}
571
572
		return apply_filters( 'give_payment_row_actions', $actions, $payment );
573
	}
574
575
576
	/**
577
	 *  Get payment status html.
578
	 *
579
	 * @since  1.0
580
	 * @access public
581
	 *
582
	 * @param Give_Payment $payment Contains all the data of the payment.
583
	 *
584
	 * @return string Data shown in the Email column
585
	 */
586
	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...
587
		$value = sprintf(
588
				'<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...
589
			sanitize_title( give_get_payment_status( $payment, true ) ),
590
			give_get_payment_status( $payment, true )
591
		);
592
593 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...
594
			$value .= Give()->tooltips->render_span( array(
595
				'label'       => __( 'This donation was made in test mode.', 'give' ),
596
				'tag_content' => __( 'Test', 'give' ),
597
				'attributes'  => array(
598
					'class' => 'give-item-label give-item-label-orange give-test-mode-transactions-label',
599
				),
600
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
601
602
			) );
603
		}
604
605
		if ( true === $payment->import && true === (bool) apply_filters( 'give_payment_show_importer_label', false ) ) {
606
			$value .= sprintf(
607
					'&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...
608
				__( 'This donation was imported.', 'give' ),
609
				__( 'Import', 'give' )
610
			);
611
		}
612
613
		return $value;
614
	}
615
616
	/**
617
	 * Get checkbox html.
618
	 *
619
	 * @param object $payment Contains all the data for the checkbox column.
620
	 *
621
	 * @access public
622
	 * @since  1.0
623
	 *
624
	 * @return string Displays a checkbox.
625
	 */
626
	public function column_cb( $payment ) {
627
		return sprintf( '<input type="checkbox" name="%1$s[]" value="%2$s" />', 'payment', $payment->ID );
628
	}
629
630
	/**
631
	 * Get payment ID html.
632
	 *
633
	 * @param object $payment Contains all the data for the checkbox column.
634
	 *
635
	 * @access public
636
	 * @since  1.0
637
	 *
638
	 * @return string Displays a checkbox.
639
	 */
640
	public function get_payment_id( $payment ) {
641
		return '<span class="give-payment-id">' . give_get_payment_number( $payment->ID ) . '</span>';
642
	}
643
644
	/**
645
	 * Get donor html.
646
	 *
647
	 * @param object $payment Contains all the data of the payment.
648
	 *
649
	 * @access public
650
	 * @since  1.0
651
	 *
652
	 * @return string Data shown in the User column
653
	 */
654
	public function get_donor( $payment ) {
655
656
		$donor_id           = give_get_payment_donor_id( $payment->ID );
657
		$donor_billing_name = give_get_donor_name_by( $payment->ID, 'donation' );
658
		$donor_name         = give_get_donor_name_by( $donor_id, 'donor' );
659
660
		$value = '';
661
		if ( ! empty( $donor_id ) ) {
662
663
			// Check whether the donor name and WP_User name is same or not.
664
			if ( sanitize_title( $donor_billing_name ) !== sanitize_title( $donor_name ) ) {
665
				$value .= $donor_billing_name . ' (';
666
			}
667
668
			$value .= '<a href="' . esc_url( admin_url( "edit.php?post_type=give_forms&page=give-donors&view=overview&id=$donor_id" ) ) . '">' . $donor_name . '</a>';
669
670
			// Check whether the donor name and WP_User name is same or not.
671
			if ( sanitize_title( $donor_billing_name ) != sanitize_title( $donor_name ) ) {
672
				$value .= ')';
673
			}
674
		} else {
675
			$email  = give_get_payment_user_email( $payment->ID );
676
			$value .= '<a href="' . esc_url( admin_url( "edit.php?post_type=give_forms&page=give-payment-history&s=$email" ) ) . '">' . __( '(donor missing)', 'give' ) . '</a>';
677
		}
678
679
		return apply_filters( 'give_payments_table_column', $value, $payment->ID, 'donor' );
680
	}
681
682
	/**
683
	 * Retrieve the bulk actions
684
	 *
685
	 * @access public
686
	 * @since  1.0
687
	 *
688
	 * @return array $actions Array of the bulk actions
689
	 */
690
	public function get_bulk_actions() {
691
		$actions = array(
692
			'delete'                 => __( 'Delete', 'give' ),
693
			'set-status-publish'     => __( 'Set To Completed', 'give' ),
694
			'set-status-pending'     => __( 'Set To Pending', 'give' ),
695
			'set-status-processing'  => __( 'Set To Processing', 'give' ),
696
			'set-status-refunded'    => __( 'Set To Refunded', 'give' ),
697
			'set-status-revoked'     => __( 'Set To Revoked', 'give' ),
698
			'set-status-failed'      => __( 'Set To Failed', 'give' ),
699
			'set-status-cancelled'   => __( 'Set To Cancelled', 'give' ),
700
			'set-status-abandoned'   => __( 'Set To Abandoned', 'give' ),
701
			'set-status-preapproval' => __( 'Set To Preapproval', 'give' ),
702
			'resend-receipt'         => __( 'Resend Email Receipts', 'give' ),
703
		);
704
705
		return apply_filters( 'give_payments_table_bulk_actions', $actions );
706
	}
707
708
	/**
709
	 * Process the bulk actions
710
	 *
711
	 * @access public
712
	 * @since  1.0
713
	 *
714
	 * @return void
715
	 */
716
	public function process_bulk_action() {
717
		$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...
718
		$action = $this->current_action();
719
720
		if ( ! is_array( $ids ) ) {
721
			$ids = array( $ids );
722
		}
723
724
		if ( empty( $action ) ) {
725
			return;
726
		}
727
728
		foreach ( $ids as $id ) {
729
730
			// Detect when a bulk action is being triggered.
731
			switch ( $this->current_action() ) {
732
733
				case 'delete':
734
					give_delete_donation( $id );
735
					break;
736
737
				case 'set-status-publish':
738
					give_update_payment_status( $id, 'publish' );
739
					break;
740
741
				case 'set-status-pending':
742
					give_update_payment_status( $id, 'pending' );
743
					break;
744
745
				case 'set-status-processing':
746
					give_update_payment_status( $id, 'processing' );
747
					break;
748
749
				case 'set-status-refunded':
750
					give_update_payment_status( $id, 'refunded' );
751
					break;
752
				case 'set-status-revoked':
753
					give_update_payment_status( $id, 'revoked' );
754
					break;
755
756
				case 'set-status-failed':
757
					give_update_payment_status( $id, 'failed' );
758
					break;
759
760
				case 'set-status-cancelled':
761
					give_update_payment_status( $id, 'cancelled' );
762
					break;
763
764
				case 'set-status-abandoned':
765
					give_update_payment_status( $id, 'abandoned' );
766
					break;
767
768
				case 'set-status-preapproval':
769
					give_update_payment_status( $id, 'preapproval' );
770
					break;
771
772
				case 'resend-receipt':
773
					/**
774
					 * Fire the action
775
					 *
776
					 * @since 2.0
777
					 */
778
					do_action( 'give_donation-receipt_email_notification', $id );
779
					break;
780
			}// End switch().
781
782
			/**
783
			 * Fires after triggering bulk action on payments table.
784
			 *
785
			 * @param int    $id             The ID of the payment.
786
			 * @param string $current_action The action that is being triggered.
787
			 *
788
			 * @since 1.7
789
			 */
790
			do_action( 'give_payments_table_do_bulk_action', $id, $this->current_action() );
791
		}// End foreach().
792
793
	}
794
795
	/**
796
	 * Retrieve the payment counts
797
	 *
798
	 * @access public
799
	 * @since  1.0
800
	 *
801
	 * @return object
802
	 */
803
	public function get_payment_counts() {
804
805
		$args = array();
806
807
		if ( isset( $_GET['user'] ) ) {
808
			$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...
809
		} elseif ( isset( $_GET['donor'] ) ) {
810
			$args['donor'] = absint( $_GET['donor'] );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
811
		} elseif ( isset( $_GET['s'] ) ) {
812
			$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...
813
			if ( $is_user ) {
814
				$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...
815
				unset( $args['s'] );
816
			} else {
817
				$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...
818
			}
819
		}
820
821
		if ( ! empty( $_GET['start-date'] ) ) {
822
			$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...
823
		}
824
825
		if ( ! empty( $_GET['end-date'] ) ) {
826
			$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...
827
		}
828
829
		$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...
830
		$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...
831
832
		$payment_count           = give_count_payments( $args );
833
		$this->complete_count    = $payment_count->publish;
834
		$this->pending_count     = $payment_count->pending;
835
		$this->processing_count  = $payment_count->processing;
836
		$this->refunded_count    = $payment_count->refunded;
837
		$this->failed_count      = $payment_count->failed;
838
		$this->revoked_count     = $payment_count->revoked;
839
		$this->cancelled_count   = $payment_count->cancelled;
840
		$this->abandoned_count   = $payment_count->abandoned;
841
		$this->preapproval_count = $payment_count->preapproval;
842
843
		foreach ( $payment_count as $count ) {
844
			$this->total_count += $count;
845
		}
846
847
		return $payment_count;
848
	}
849
850
	/**
851
	 * Retrieve all the data for all the payments.
852
	 *
853
	 * @access public
854
	 * @since  1.0
855
	 *
856
	 * @return array  objects in array containing all the data for the payments
857
	 */
858
	public function payments_data() {
859
860
		$per_page   = $this->per_page;
861
		$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...
862
		$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...
863
		$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...
864
		$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...
865
		$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...
866
		$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...
867
		$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...
868
		$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...
869
		$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...
870
		$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...
871
		$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...
872
		$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...
873
		$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...
874
		$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...
875
876
		$args = array(
877
			'output'     => 'payments',
878
			'number'     => $per_page,
879
			'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...
880
			'orderby'    => $orderby,
881
			'order'      => $order,
882
			'user'       => $user,
883
			'donor'      => $donor,
884
			'status'     => $status,
885
			'meta_key'   => $meta_key,
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
886
			'year'       => $year,
887
			'month'      => $month,
888
			'day'        => $day,
889
			's'          => $search,
890
			'start_date' => $start_date,
891
			'gateway'    => $gateway,
892
			'end_date'   => $end_date,
893
			'give_forms' => $form_id,
894
		);
895
896
		if ( is_string( $search ) && false !== strpos( $search, 'txn:' ) ) {
897
			$args['search_in_notes'] = true;
898
			$args['s']               = trim( str_replace( 'txn:', '', $args['s'] ) );
899
		}
900
901
		$p_query = new Give_Payments_Query( $args );
902
		
903
		return $p_query->get_payments();
904
905
	}
906
907
	/**
908
	 * Setup the final data for the table
909
	 *
910
	 * @access public
911
	 * @since  1.0
912
	 * @uses   Give_Payment_History_Table::get_columns()
913
	 * @uses   Give_Payment_History_Table::get_sortable_columns()
914
	 * @uses   Give_Payment_History_Table::payments_data()
915
	 * @uses   WP_List_Table::get_pagenum()
916
	 * @uses   WP_List_Table::set_pagination_args()
917
	 *
918
	 * @return void
919
	 */
920
	public function prepare_items() {
921
922
		wp_reset_vars( array( 'action', 'payment', 'orderby', 'order', 's' ) );
923
924
		$columns  = $this->get_columns();
925
		$hidden   = array(); // No hidden columns.
926
		$sortable = $this->get_sortable_columns();
927
		$data     = $this->payments_data();
928
		$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...
929
930
		$this->_column_headers = array( $columns, $hidden, $sortable );
931
932
		switch ( $status ) {
933
			case 'publish':
934
				$total_items = $this->complete_count;
935
				break;
936
			case 'pending':
937
				$total_items = $this->pending_count;
938
				break;
939
			case 'processing':
940
				$total_items = $this->processing_count;
941
				break;
942
			case 'refunded':
943
				$total_items = $this->refunded_count;
944
				break;
945
			case 'failed':
946
				$total_items = $this->failed_count;
947
				break;
948
			case 'revoked':
949
				$total_items = $this->revoked_count;
950
				break;
951
			case 'cancelled':
952
				$total_items = $this->cancelled_count;
953
				break;
954
			case 'abandoned':
955
				$total_items = $this->abandoned_count;
956
				break;
957
			case 'preapproval':
958
				$total_items = $this->preapproval_count;
959
				break;
960
			case 'any':
961
				$total_items = $this->total_count;
962
				break;
963
			default:
964
				// Retrieve the count of the non-default-Give status.
965
				$count       = wp_count_posts( 'give_payment' );
966
				$total_items = isset( $count->{$status} ) ? $count->{$status} : 0;
967
				break;
968
		}
969
970
		$this->items = $data;
971
972
		$this->set_pagination_args(
973
			array(
974
				'total_items' => $total_items,
975
				// We have to calculate the total number of items.
976
				'per_page'    => $this->per_page,
977
				// We have to determine how many items to show on a page.
978
				'total_pages' => ceil( $total_items / $this->per_page ),
979
			// We have to calculate the total number of pages.
980
			)
981
		);
982
	}
983
}
984