Completed
Push — issues/1781 ( da0482 )
by Ravinder
19:23
created

Give_Payment_History_Table::get_donor()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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