Test Failed
Push — issues/2531 ( 9ee8d9...57cdd6 )
by Ravinder
04:36
created

Give_Payment_History_Table::payments_data()   F

Complexity

Conditions 18
Paths > 20000

Size

Total Lines 55
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

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