Completed
Push — release/2.0 ( 4d845f...d9fa8a )
by Ravinder
18:24
created

Give_Payment_History_Table::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 29 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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 refunded payments
73
	 *
74
	 * @var int
75
	 * @since 1.0
76
	 */
77
	public $refunded_count;
78
79
	/**
80
	 * Total number of failed payments
81
	 *
82
	 * @var int
83
	 * @since 1.0
84
	 */
85
	public $failed_count;
86
87
	/**
88
	 * Total number of revoked payments
89
	 *
90
	 * @var int
91
	 * @since 1.0
92
	 */
93
	public $revoked_count;
94
95
	/**
96
	 * Total number of cancelled payments
97
	 *
98
	 * @var int
99
	 * @since 1.4
100
	 */
101
	public $cancelled_count;
102
103
	/**
104
	 * Total number of abandoned payments
105
	 *
106
	 * @var int
107
	 * @since 1.6
108
	 */
109
	public $abandoned_count;
110
111
	/**
112
	 * Get things started.
113
	 *
114
	 * @since 1.0
115
	 * @uses  Give_Payment_History_Table::get_payment_counts()
116
	 * @see   WP_List_Table::__construct()
117
	 */
118
	public function __construct() {
119
120
		// Set parent defaults.
121
		parent::__construct( array(
122
			'singular' => give_get_forms_label_singular(),    // Singular name of the listed records.
123
			'plural'   => give_get_forms_label_plural(),      // Plural name of the listed records.
124
			'ajax'     => false,                              // Does this table support ajax?
125
		) );
126
127
		$this->get_payment_counts();
128
		$this->process_bulk_action();
129
		$this->base_url = admin_url( 'edit.php?post_type=give_forms&page=give-payment-history' );
130
	}
131
132
	/**
133
	 * Add donation search filter.
134
	 *
135
	 * @return void
136
	 */
137
	public function advanced_filters() {
138
		$start_date = isset( $_GET['start-date'] ) ? sanitize_text_field( $_GET['start-date'] ) : null;
139
		$end_date   = isset( $_GET['end-date'] ) ? sanitize_text_field( $_GET['end-date'] ) : null;
140
		$status     = isset( $_GET['status'] ) ? $_GET['status'] : '';
141
		?>
142
		<div id="give-payment-filters">
143
			<span id="give-payment-date-filters">
144
				<label for="start-date"
145
				       class="give-start-date-label"><?php esc_html_e( 'Start Date:', 'give' ); ?></label>
146
				<input type="text" id="start-date" name="start-date" class="give_datepicker"
147
				       value="<?php echo $start_date; ?>" placeholder="mm/dd/yyyy"/>
148
				<label for="end-date" class="give-end-date-label"><?php esc_html_e( 'End Date:', 'give' ); ?></label>
149
				<input type="text" id="end-date" name="end-date" class="give_datepicker"
150
				       value="<?php echo $end_date; ?>" placeholder="mm/dd/yyyy"/>
151
				<input type="submit" class="button-secondary" value="<?php esc_attr_e( 'Apply', 'give' ); ?>"/>
152
			</span>
153
			<?php if ( ! empty( $status ) ) : ?>
154
				<input type="hidden" name="status" value="<?php echo esc_attr( $status ); ?>"/>
155
			<?php endif; ?>
156
			<?php if ( ! empty( $start_date ) || ! empty( $end_date ) ) : ?>
157
				<a href="<?php echo admin_url( 'edit.php?post_type=give_forms&page=give-payment-history' ); ?>"
158
				   class="button-secondary"><?php esc_html_e( 'Clear Filter', 'give' ); ?></a>
159
			<?php endif; ?>
160
			<?php $this->search_box( esc_html__( 'Search', 'give' ), 'give-payments' ); ?>
161
		</div>
162
163
		<?php
164
	}
165
166
	/**
167
	 * Show the search field
168
	 *
169
	 * @since  1.0
170
	 * @access public
171
	 *
172
	 * @param string $text     Label for the search box
173
	 * @param string $input_id ID of the search box
174
	 *
175
	 * @return void
176
	 */
177
	public function search_box( $text, $input_id ) {
178
		if ( empty( $_REQUEST['s'] ) && ! $this->has_items() ) {
179
			return;
180
		}
181
182
		$input_id = $input_id . '-search-input';
183
184
		if ( ! empty( $_REQUEST['orderby'] ) ) {
185
			echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
186
		}
187
		if ( ! empty( $_REQUEST['order'] ) ) {
188
			echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
189
		}
190
		?>
191
		<p class="search-box" role="search">
192
			<?php
193
			/**
194
			 * Fires in the payment history search box.
195
			 *
196
			 * Allows you to add new elements before the search box.
197
			 *
198
			 * @since 1.7
199
			 */
200
			do_action( 'give_payment_history_search' );
201
			?>
202
			<label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label>
203
			<input type="search" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>"/>
204
			<?php submit_button( $text, 'button', false, false, array(
205
				'ID' => 'search-submit',
206
			) ); ?><br/>
207
		</p>
208
		<?php
209
	}
210
211
	/**
212
	 * Retrieve the view types
213
	 *
214
	 * @access public
215
	 * @since  1.0
216
	 * @return array $views All the views available
217
	 */
218
	public function get_views() {
219
220
		$current         = isset( $_GET['status'] ) ? $_GET['status'] : '';
221
		$total_count     = '&nbsp;<span class="count">(' . $this->total_count . ')</span>';
222
		$complete_count  = '&nbsp;<span class="count">(' . $this->complete_count . ')</span>';
223
		$cancelled_count = '&nbsp;<span class="count">(' . $this->cancelled_count . ')</span>';
224
		$pending_count   = '&nbsp;<span class="count">(' . $this->pending_count . ')</span>';
225
		$refunded_count  = '&nbsp;<span class="count">(' . $this->refunded_count . ')</span>';
226
		$failed_count    = '&nbsp;<span class="count">(' . $this->failed_count . ')</span>';
227
		$abandoned_count = '&nbsp;<span class="count">(' . $this->abandoned_count . ')</span>';
228
		$revoked_count   = '&nbsp;<span class="count">(' . $this->revoked_count . ')</span>';
229
230
		$views = array(
231
			'all'       => sprintf( '<a href="%s"%s>%s</a>', remove_query_arg( array(
232
				'status',
233
				'paged',
234
			) ), $current === 'all' || $current == '' ? ' class="current"' : '', esc_html__( 'All', 'give' ) . $total_count ),
235
			'publish'   => sprintf( '<a href="%s"%s>%s</a>', esc_url( add_query_arg( array(
236
				'status' => 'publish',
237
				'paged'  => false,
238
			) ) ), $current === 'publish' ? ' class="current"' : '', esc_html__( 'Completed', 'give' ) . $complete_count ),
239
			'pending'   => sprintf( '<a href="%s"%s>%s</a>', esc_url( add_query_arg( array(
240
				'status' => 'pending',
241
				'paged'  => false,
242
			) ) ), $current === 'pending' ? ' class="current"' : '', esc_html__( 'Pending', 'give' ) . $pending_count ),
243
			'refunded'  => sprintf( '<a href="%s"%s>%s</a>', esc_url( add_query_arg( array(
244
				'status' => 'refunded',
245
				'paged'  => false,
246
			) ) ), $current === 'refunded' ? ' class="current"' : '', esc_html__( 'Refunded', 'give' ) . $refunded_count ),
247
			'revoked'   => sprintf( '<a href="%s"%s>%s</a>', esc_url( add_query_arg( array(
248
				'status' => 'revoked',
249
				'paged'  => false,
250
			) ) ), $current === 'revoked' ? ' class="current"' : '', esc_html__( 'Revoked', 'give' ) . $revoked_count ),
251
			'failed'    => sprintf( '<a href="%s"%s>%s</a>', esc_url( add_query_arg( array(
252
				'status' => 'failed',
253
				'paged'  => false,
254
			) ) ), $current === 'failed' ? ' class="current"' : '', esc_html__( 'Failed', 'give' ) . $failed_count ),
255
			'cancelled' => sprintf( '<a href="%s"%s>%s</a>', esc_url( add_query_arg( array(
256
				'status' => 'cancelled',
257
				'paged'  => false,
258
			) ) ), $current === 'cancelled' ? ' class="current"' : '', esc_html__( 'Cancelled', 'give' ) . $cancelled_count ),
259
			'abandoned' => sprintf( '<a href="%s"%s>%s</a>', esc_url( add_query_arg( array(
260
				'status' => 'abandoned',
261
				'paged'  => false,
262
			) ) ), $current === 'abandoned' ? ' class="current"' : '', esc_html__( 'Abandoned', 'give' ) . $abandoned_count ),
263
		);
264
265
		return apply_filters( 'give_payments_table_views', $views );
266
	}
267
268
	/**
269
	 * Retrieve the table columns
270
	 *
271
	 * @access public
272
	 * @since  1.0
273
	 * @return array $columns Array of all the list table columns
274
	 */
275
	public function get_columns() {
276
		$columns = array(
277
			'cb'            => '<input type="checkbox" />', // Render a checkbox instead of text.
278
			'donation'      => esc_html__( 'Donation', 'give' ),
279
			'donation_form' => esc_html__( 'Donation Form', 'give' ),
280
			'status'        => esc_html__( 'Status', 'give' ),
281
			'date'          => esc_html__( 'Date', 'give' ),
282
			'amount'        => esc_html__( 'Amount', 'give' ),
283
			'details'       => esc_html__( 'Details', 'give' ),
284
		);
285
286
		return apply_filters( 'give_payments_table_columns', $columns );
287
	}
288
289
	/**
290
	 * Retrieve the table's sortable columns
291
	 *
292
	 * @access public
293
	 * @since  1.0
294
	 * @return array Array of all the sortable columns
295
	 */
296
	public function get_sortable_columns() {
297
		$columns = array(
298
			'donation'      => array( 'ID', true ),
299
			'donation_form' => array( 'donation_form', false ),
300
			'status'        => array( 'status', false ),
301
			'amount'        => array( 'amount', false ),
302
			'date'          => array( 'date', false ),
303
		);
304
305
		return apply_filters( 'give_payments_table_sortable_columns', $columns );
306
	}
307
308
	/**
309
	 * Gets the name of the primary column.
310
	 *
311
	 * @since  1.5
312
	 * @access protected
313
	 *
314
	 * @return string Name of the primary column.
315
	 */
316
	protected function get_primary_column_name() {
317
		return 'donation';
318
	}
319
320
	/**
321
	 * This function renders most of the columns in the list table.
322
	 *
323
	 * @access public
324
	 * @since  1.0
325
	 *
326
	 * @param Give_Payment $payment     Payment ID.
327
	 * @param string       $column_name The name of the column
328
	 *
329
	 * @return string Column Name
330
	 */
331
	public function column_default( $payment, $column_name ) {
332
333
		$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' ) ) );
334
		$row_actions         = $this->get_row_actions( $payment );
335
336
		switch ( $column_name ) {
337
			case 'donation' :
338
				$value = sprintf( '<a href="%1$s" data-tooltip="%2$s">#%3$s</a>&nbsp;%4$s&nbsp;%5$s<br>', $single_donation_url, sprintf( esc_attr__( 'View Donation #%s', 'give' ), $payment->ID ), $payment->ID, esc_html__( 'by', 'give' ), $this->get_donor( $payment ) );
339
				$value .= $this->get_donor_email( $payment );
340
				$value .= $this->row_actions( $row_actions );
341
				break;
342
343
			case 'amount' :
344
				$amount = ! empty( $payment->total ) ? $payment->total : 0;
345
				$value  = give_currency_filter( give_format_amount( $amount ), give_get_payment_currency_code( $payment->ID ) );
346
				$value  .= sprintf( '<br><small>%1$s %2$s</small>', __( 'via', 'give' ), give_get_gateway_admin_label( $payment->gateway ) );
347
				break;
348
349
			case 'donation_form' :
350
				$form_title = empty( $payment->form_title ) ? sprintf( __( 'Untitled (#%s)', 'give' ), $payment->form_id ) : $payment->form_title;
351
				$value      = '<a href="' . admin_url( 'post.php?post=' . $payment->form_id . '&action=edit' ) . '">' . $form_title . '</a>';
352
				$level      = give_get_payment_form_title( $payment->meta, true );
0 ignored issues
show
Documentation introduced by
The property meta does not exist on object<Give_Payment>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
353
354
				if ( ! empty( $level ) ) {
355
					$value .= $level;
356
				}
357
358
				break;
359
360
			case 'date' :
361
				$date  = strtotime( $payment->date );
362
				$value = date_i18n( give_date_format(), $date );
363
				break;
364
365
			case 'status' :
366
				$value = $this->get_payment_status( $payment );
367
				break;
368
369
			case 'details' :
370
				$value = sprintf( '<div class="give-payment-details-link-wrap"><a href="%1$s" class="give-payment-details-link button button-small" data-tooltip="%2$s" aria-label="%2$s"><span class="dashicons dashicons-visibility"></span></a></div>', $single_donation_url, sprintf( esc_attr__( 'View Donation #%s', 'give' ), $payment->ID ) );
371
				break;
372
373
			default:
374
				$value = isset( $payment->$column_name ) ? $payment->$column_name : '';
375
				break;
376
377
		}// End switch().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
378
379
		return apply_filters( 'give_payments_table_column', $value, $payment->ID, $column_name );
380
	}
381
382
	/**
383
	 * Get donor email html.
384
	 *
385
	 * @access public
386
	 * @since  1.0
387
	 *
388
	 * @param  Give_Payment $payment Contains all the data of the payment
389
	 *
390
	 * @return string                Data shown in the Email column
391
	 */
392
	public function get_donor_email( $payment ) {
393
394
		$email = give_get_payment_user_email( $payment->ID );
395
396
		if ( empty( $email ) ) {
397
			$email = esc_html__( '(unknown)', 'give' );
398
		}
399
400
		$value = '<a href="mailto:' . $email . '" data-tooltip="' . esc_attr__( 'Email donor', 'give' ) . '">' . $email . '</a>';
401
402
		return apply_filters( 'give_payments_table_column', $value, $payment->ID, 'email' );
403
	}
404
405
	/**
406
	 * Get Row Actions
407
	 *
408
	 * @since 1.6
409
	 *
410
	 * @param Give_Payment $payment
411
	 *
412
	 * @return array $actions
413
	 */
414
	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...
415
416
		$actions = array();
417
		$email   = give_get_payment_user_email( $payment->ID );
418
419
		// Add search term string back to base URL.
420
		$search_terms = ( isset( $_GET['s'] ) ? trim( $_GET['s'] ) : '' );
421
		if ( ! empty( $search_terms ) ) {
422
			$this->base_url = add_query_arg( 's', $search_terms, $this->base_url );
423
		}
424
425
		if ( give_is_payment_complete( $payment->ID ) && ! empty( $email ) ) {
426
427
			$actions['email_links'] = sprintf( '<a href="%1$s" aria-label="%2$s">%3$s</a>', wp_nonce_url( add_query_arg( array(
428
					'give-action' => 'email_links',
429
					'purchase_id' => $payment->ID,
430
			), $this->base_url ), 'give_payment_nonce' ), sprintf( esc_attr__( 'Resend Donation %s Receipt', 'give' ), $payment->ID ), esc_html__( 'Resend Receipt', 'give' ) );
431
432
		}
433
434
		$actions['delete'] = sprintf( '<a href="%1$s" aria-label="%2$s">%3$s</a>', wp_nonce_url( add_query_arg( array(
435
				'give-action' => 'delete_payment',
436
				'purchase_id' => $payment->ID,
437
		), $this->base_url ), 'give_donation_nonce' ), sprintf( esc_attr__( 'Delete Donation %s', 'give' ), $payment->ID ), esc_html__( 'Delete', 'give' ) );
438
439
		return apply_filters( 'give_payment_row_actions', $actions, $payment );
440
	}
441
442
443
	/**
444
	 *  Get payment status html.
445
	 *
446
	 * @access public
447
	 * @since  1.0
448
	 *
449
	 * @param  Give_Payment $payment Contains all the data of the payment
450
	 *
451
	 * @return string                Data shown in the Email column
452
	 */
453
	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...
454
		$value = '<div class="give-donation-status status-' . sanitize_title( give_get_payment_status( $payment, true ) ) . '"><span class="give-donation-status-icon"></span> ' . give_get_payment_status( $payment, true ) . '</div>';
455
		if ( $payment->mode == 'test' ) {
456
			$value .= ' <span class="give-item-label give-item-label-orange give-test-mode-transactions-label" data-tooltip="' . esc_attr__( 'This donation was made in test mode.', 'give' ) . '">' . esc_html__( 'Test', 'give' ) . '</span>';
457
		}
458
459
		return $value;
460
	}
461
462
	/**
463
	 * Get checkbox html.
464
	 *
465
	 * @access public
466
	 * @since  1.0
467
	 *
468
	 * @param  Give_Payment $payment Contains all the data for the checkbox column.
469
	 *
470
	 * @return string Displays a checkbox.
471
	 */
472
	public function column_cb( $payment ) {
473
		return sprintf( '<input type="checkbox" name="%1$s[]" value="%2$s" />', 'payment', $payment->ID );
474
	}
475
476
	/**
477
	 * Get payment ID html.
478
	 *
479
	 * @access public
480
	 * @since  1.0
481
	 *
482
	 * @param  Give_Payment $payment Contains all the data for the checkbox column.
483
	 *
484
	 * @return string Displays a checkbox.
485
	 */
486
	public function get_payment_id( $payment ) {
487
		return '<span class="give-payment-id">' . give_get_payment_number( $payment->ID ) . '</span>';
488
	}
489
490
	/**
491
	 * Get donor html.
492
	 *
493
	 * @access public
494
	 * @since  1.0
495
	 *
496
	 * @param  Give_Payment $payment Contains all the data of the payment
497
	 *
498
	 * @return string Data shown in the User column
499
	 */
500
	public function get_donor( $payment ) {
501
502
		$donor_id           = give_get_payment_donor_id( $payment->ID );
503
		$donor_billing_name = give_get_donor_name_by( $payment->ID, 'donation' );
504
		$donor_name         = give_get_donor_name_by( $donor_id, 'donor' );
505
506
		$value = '';
507
		if ( ! empty( $donor_id ) ) {
508
509
			// Check whether the donor name and WP_User name is same or not.
510
			if ( sanitize_title( $donor_billing_name ) != sanitize_title( $donor_name ) ) {
511
				$value .= $donor_billing_name . ' (';
512
			}
513
514
			$value .= '<a href="' . esc_url( admin_url( "edit.php?post_type=give_forms&page=give-donors&view=overview&id=$donor_id" ) ) . '">' . $donor_name . '</a>';
515
516
			// Check whether the donor name and WP_User name is same or not.
517
			if ( sanitize_title( $donor_billing_name ) != sanitize_title( $donor_name ) ) {
518
				$value .= ')';
519
			}
520
		} else {
521
			$email = give_get_payment_user_email( $payment->ID );
522
			$value .= '<a href="' . esc_url( admin_url( "edit.php?post_type=give_forms&page=give-payment-history&s=$email" ) ) . '">' . esc_html__( '(donor missing)', 'give' ) . '</a>';
523
		}
524
525
		return apply_filters( 'give_payments_table_column', $value, $payment->ID, 'donor' );
526
	}
527
528
	/**
529
	 * Retrieve the bulk actions
530
	 *
531
	 * @access public
532
	 * @since  1.0
533
	 * @return array $actions Array of the bulk actions
534
	 */
535
	public function get_bulk_actions() {
536
		$actions = array(
537
			'delete'               => esc_html__( 'Delete', 'give' ),
538
			'set-status-publish'   => esc_html__( 'Set To Completed', 'give' ),
539
			'set-status-pending'   => esc_html__( 'Set To Pending', 'give' ),
540
			'set-status-refunded'  => esc_html__( 'Set To Refunded', 'give' ),
541
			'set-status-revoked'   => esc_html__( 'Set To Revoked', 'give' ),
542
			'set-status-failed'    => esc_html__( 'Set To Failed', 'give' ),
543
			'set-status-cancelled' => esc_html__( 'Set To Cancelled', 'give' ),
544
			'set-status-abandoned' => esc_html__( 'Set To Abandoned', 'give' ),
545
			'resend-receipt'       => esc_html__( 'Resend Email Receipts', 'give' ),
546
		);
547
548
		return apply_filters( 'give_payments_table_bulk_actions', $actions );
549
	}
550
551
	/**
552
	 * Process the bulk actions
553
	 *
554
	 * @access public
555
	 * @since  1.0
556
	 * @return void
557
	 */
558
	public function process_bulk_action() {
559
		$ids    = isset( $_GET['payment'] ) ? $_GET['payment'] : false;
560
		$action = $this->current_action();
561
562
		if ( ! is_array( $ids ) ) {
563
			$ids = array( $ids );
564
		}
565
566
		if ( empty( $action ) ) {
567
			return;
568
		}
569
570
		foreach ( $ids as $id ) {
571
572
			// Detect when a bulk action is being triggered.
573
			switch ( $this->current_action() ) {
574
575
				case'delete':
576
					give_delete_donation( $id );
577
					break;
578
579
				case 'set-status-publish':
580
					give_update_payment_status( $id, 'publish' );
581
					break;
582
583
				case 'set-status-pending':
584
					give_update_payment_status( $id, 'pending' );
585
					break;
586
587
				case 'set-status-refunded':
588
					give_update_payment_status( $id, 'refunded' );
589
					break;
590
				case 'set-status-revoked':
591
					give_update_payment_status( $id, 'revoked' );
592
					break;
593
594
				case 'set-status-failed':
595
					give_update_payment_status( $id, 'failed' );
596
					break;
597
598
				case 'set-status-cancelled':
599
					give_update_payment_status( $id, 'cancelled' );
600
					break;
601
602
				case 'set-status-abandoned':
603
					give_update_payment_status( $id, 'abandoned' );
604
					break;
605
606
				case 'set-status-preapproval':
607
					give_update_payment_status( $id, 'preapproval' );
608
					break;
609
610
				case 'resend-receipt':
611
					/**
612
					 * Fire the action
613
					 * @since 2.0
614
					 */
615
					do_action( 'give_donation-receipt_email_notification', $id );
616
					break;
617
			}// End switch().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
618
619
			/**
620
			 * Fires after triggering bulk action on payments table.
621
			 *
622
			 * @since 1.7
623
			 *
624
			 * @param int    $id             The ID of the payment.
625
			 * @param string $current_action The action that is being triggered.
626
			 */
627
			do_action( 'give_payments_table_do_bulk_action', $id, $this->current_action() );
628
		}// End foreach().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
629
630
	}
631
632
	/**
633
	 * Retrieve the payment counts
634
	 *
635
	 * @access public
636
	 * @since  1.0
637
	 * @return void
638
	 */
639
	public function get_payment_counts() {
640
641
		$args = array();
642
643
		if ( isset( $_GET['user'] ) ) {
644
			$args['user'] = urldecode( $_GET['user'] );
645
		} elseif ( isset( $_GET['s'] ) ) {
646
			$is_user = strpos( $_GET['s'], strtolower( 'user:' ) ) !== false;
647
			if ( $is_user ) {
648
				$args['user'] = absint( trim( str_replace( 'user:', '', strtolower( $_GET['s'] ) ) ) );
649
				unset( $args['s'] );
650
			} else {
651
				$args['s'] = sanitize_text_field( $_GET['s'] );
652
			}
653
		}
654
655
		if ( ! empty( $_GET['start-date'] ) ) {
656
			$args['start-date'] = urldecode( $_GET['start-date'] );
657
		}
658
659
		if ( ! empty( $_GET['end-date'] ) ) {
660
			$args['end-date'] = urldecode( $_GET['end-date'] );
661
		}
662
663
		$payment_count         = give_count_payments( $args );
664
		$this->complete_count  = $payment_count->publish;
665
		$this->pending_count   = $payment_count->pending;
666
		$this->refunded_count  = $payment_count->refunded;
667
		$this->failed_count    = $payment_count->failed;
668
		$this->revoked_count   = $payment_count->revoked;
669
		$this->cancelled_count = $payment_count->cancelled;
670
		$this->abandoned_count = $payment_count->abandoned;
671
672
		foreach ( $payment_count as $count ) {
673
			$this->total_count += $count;
674
		}
675
	}
676
677
	/**
678
	 * Retrieve all the data for all the payments.
679
	 *
680
	 * @access public
681
	 * @since  1.0
682
	 * @return array  objects in array containing all the data for the payments
683
	 */
684
	public function payments_data() {
685
686
		$per_page   = $this->per_page;
687
		$orderby    = isset( $_GET['orderby'] ) ? urldecode( $_GET['orderby'] ) : 'ID';
688
		$order      = isset( $_GET['order'] ) ? $_GET['order'] : 'DESC';
689
		$user       = isset( $_GET['user'] ) ? $_GET['user'] : null;
690
		$status     = isset( $_GET['status'] ) ? $_GET['status'] : give_get_payment_status_keys();
691
		$meta_key   = isset( $_GET['meta_key'] ) ? $_GET['meta_key'] : null;
692
		$year       = isset( $_GET['year'] ) ? $_GET['year'] : null;
693
		$month      = isset( $_GET['m'] ) ? $_GET['m'] : null;
694
		$day        = isset( $_GET['day'] ) ? $_GET['day'] : null;
695
		$search     = isset( $_GET['s'] ) ? sanitize_text_field( $_GET['s'] ) : null;
696
		$start_date = isset( $_GET['start-date'] ) ? sanitize_text_field( $_GET['start-date'] ) : null;
697
		$end_date   = isset( $_GET['end-date'] ) ? sanitize_text_field( $_GET['end-date'] ) : $start_date;
698
699
		if ( ! empty( $search ) ) {
700
			$status = 'any'; // Force all payment statuses when searching.
701
		}
702
703
		$args = array(
704
			'output'     => 'payments',
705
			'number'     => $per_page,
706
			'page'       => isset( $_GET['paged'] ) ? $_GET['paged'] : null,
707
			'orderby'    => $orderby,
708
			'order'      => $order,
709
			'user'       => $user,
710
			'status'     => $status,
711
			'meta_key'   => $meta_key,
712
			'year'       => $year,
713
			'month'      => $month,
714
			'day'        => $day,
715
			's'          => $search,
716
			'start_date' => $start_date,
717
			'end_date'   => $end_date,
718
		);
719
720
		if ( is_string( $search ) && false !== strpos( $search, 'txn:' ) ) {
721
			$args['search_in_notes'] = true;
722
			$args['s']               = trim( str_replace( 'txn:', '', $args['s'] ) );
723
		}
724
725
		$p_query = new Give_Payments_Query( $args );
726
727
		return $p_query->get_payments();
728
729
	}
730
731
	/**
732
	 * Setup the final data for the table
733
	 *
734
	 * @access public
735
	 * @since  1.0
736
	 * @uses   Give_Payment_History_Table::get_columns()
737
	 * @uses   Give_Payment_History_Table::get_sortable_columns()
738
	 * @uses   Give_Payment_History_Table::payments_data()
739
	 * @uses   WP_List_Table::get_pagenum()
740
	 * @uses   WP_List_Table::set_pagination_args()
741
	 * @return void
742
	 */
743
	public function prepare_items() {
744
745
		wp_reset_vars( array( 'action', 'payment', 'orderby', 'order', 's' ) );
746
747
		$columns  = $this->get_columns();
748
		$hidden   = array(); // No hidden columns.
749
		$sortable = $this->get_sortable_columns();
750
		$data     = $this->payments_data();
751
		$status   = isset( $_GET['status'] ) ? $_GET['status'] : 'any';
752
753
		$this->_column_headers = array( $columns, $hidden, $sortable );
754
755
		switch ( $status ) {
756
			case 'publish':
757
				$total_items = $this->complete_count;
758
				break;
759
			case 'pending':
760
				$total_items = $this->pending_count;
761
				break;
762
			case 'refunded':
763
				$total_items = $this->refunded_count;
764
				break;
765
			case 'failed':
766
				$total_items = $this->failed_count;
767
				break;
768
			case 'revoked':
769
				$total_items = $this->revoked_count;
770
				break;
771
			case 'cancelled':
772
				$total_items = $this->cancelled_count;
773
				break;
774
			case 'abandoned':
775
				$total_items = $this->abandoned_count;
776
				break;
777
			case 'any':
778
				$total_items = $this->total_count;
779
				break;
780
			default:
781
				// Retrieve the count of the non-default-Give status.
782
				$count       = wp_count_posts( 'give_payment' );
783
				$total_items = isset( $count->{$status} ) ? $count->{$status} : 0;
784
				break;
785
		}
786
787
		$this->items = $data;
788
789
		$this->set_pagination_args( array(
790
				'total_items' => $total_items,
791
				// We have to calculate the total number of items.
792
				'per_page'    => $this->per_page,
793
				// We have to determine how many items to show on a page.
794
				'total_pages' => ceil( $total_items / $this->per_page ),
795
				// We have to calculate the total number of pages.
796
		) );
797
	}
798
}
799