Test Failed
Push — issues/2397 ( f367c1...92dbfa )
by Ravinder
04:29
created

Give_Donor_List_Table::display()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 69
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 28
nc 2
nop 0
dl 0
loc 69
rs 8.56
c 0
b 0
f 0

How to fix   Long Method   

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
 * Donor List Table Class.
4
 *
5
 * The list view under WP-Admin > Donations > Donors.
6
 *
7
 * @package     Give
8
 * @subpackage  Admin/Reports
9
 * @copyright   Copyright (c) 2016, WordImpress
10
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
11
 * @since       1.0
12
 */
13
14
// Exit if accessed directly.
15
if ( ! defined( 'ABSPATH' ) ) {
16
	exit;
17
}
18
19
// Load WP_List_Table if not loaded.
20
if ( ! class_exists( 'WP_List_Table' ) ) {
21
	require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
22
}
23
24
/**
25
 * Give_Donor_List_Table Class.
26
 *
27
 * @since 1.0
28
 */
29
class Give_Donor_List_Table extends WP_List_Table {
30
31
	/**
32
	 * Number of items per page.
33
	 *
34
	 * @var int
35
	 * @since 1.0
36
	 */
37
	public $per_page = 30;
38
39
	/**
40
	 * Number of donors found.
41
	 *
42
	 * @var int
43
	 * @since 1.0
44
	 */
45
	public $count = 0;
46
47
	/**
48
	 * Total donors.
49
	 *
50
	 * @var int
51
	 * @since 1.0
52
	 */
53
	public $total = 0;
54
55
	/**
56
	 * Get things started.
57
	 *
58
	 * @since 1.0
59
	 * @see   WP_List_Table::__construct()
60
	 */
61 View Code Duplication
	public function __construct() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
62
63
		// Set parent defaults.
64
		parent::__construct( array(
65
			'singular' => __( 'Donor', 'give' ), // Singular name of the listed records.
66
			'plural'   => __( 'Donors', 'give' ), // Plural name of the listed records.
67
			'ajax'     => false, // Does this table support ajax?.
68
		) );
69
70
	}
71
72
	/**
73
	 * Show the search field.
74
	 *
75
	 * @param string $text     Label for the search box.
76
	 * @param string $input_id ID of the search box.
77
	 *
78
	 * @since  1.0
79
	 * @access public
80
	 *
81
	 * @return void
82
	 */
83
	public function search_box( $text, $input_id ) {
84
		$input_id = $input_id . '-search-input';
85
86
		if ( ! empty( $_REQUEST['orderby'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
87
			echo sprintf( '<input type="hidden" name="orderby" value="%1$s" />', esc_attr( $_REQUEST['orderby'] ) );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
88
		}
89
90
		if ( ! empty( $_REQUEST['order'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
91
			echo sprintf( '<input type="hidden" name="order" value="%1$s" />', esc_attr( $_REQUEST['order'] ) );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
92
		}
93
		?>
94
		<p class="search-box" role="search">
95
			<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...
96
			<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...
97
			<?php submit_button( $text, 'button', false, false, array(
98
				'ID' => 'search-submit',
99
			) ); ?>
100
		</p>
101
		<?php
102
	}
103
104
	/**
105
	 * This function renders most of the columns in the list table.
106
	 *
107
	 * @param array  $donor        Contains all the data of the donors.
108
	 * @param string $column_name The name of the column.
109
	 *
110
	 * @access public
111
	 * @since  1.0
112
	 *
113
	 * @return string Column Name.
114
	 */
115
	public function column_default( $donor, $column_name ) {
116
117
		switch ( $column_name ) {
118
119 View Code Duplication
			case 'num_donations' :
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...
120
				$value = sprintf(
121
					'<a href="%s">%s</a>',
122
					admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&donor=' . absint( $donor['id'] ) ),
123
					esc_html( $donor['num_donations'] )
124
				);
125
				break;
126
127 View Code Duplication
			case 'amount_spent' :
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...
128
				$value = give_currency_filter( give_format_amount( $donor[ $column_name ], array( 'sanitize' => false ) ) );
129
				break;
130
131
			case 'date_created' :
132
				$value = date_i18n( give_date_format(), strtotime( $donor['date_created'] ) );
133
				break;
134
135
			default:
136
				$value = isset( $donor[ $column_name ] ) ? $donor[ $column_name ] : null;
137
				break;
138
		}
139
140
		return apply_filters( "give_donors_column_{$column_name}", $value, $donor['id'] );
141
142
	}
143
144
	/**
145
	 * For CheckBox Column
146
	 *
147
	 * @param array $donor Donor Data.
148
	 *
149
	 * @access public
150
	 * @since  1.8.16
151
	 *
152
	 * @return string
153
	 */
154
	public function column_cb( $donor ){
155
		return sprintf(
156
			'<input class="donor-selector" type="checkbox" name="%1$s[]" value="%2$d" data-name="%3$s" />',
157
			$this->_args['singular'],
158
			$donor['id'],
159
			$donor['name']
160
		);
161
	}
162
163
	/**
164
	 * Column name.
165
	 *
166
	 * @param array $donor Donor Data.
167
	 *
168
	 * @access public
169
	 * @since  1.0
170
	 *
171
	 * @return string
172
	 */
173
	public function column_name( $donor ) {
174
		$name     = '#' . $donor['id'] . ' ';
175
		$name     .= ! empty( $donor['name'] ) ? $donor['name'] : '<em>' . __( 'Unnamed Donor', 'give' ) . '</em>';
176
		$view_url = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor['id'] );
177
		$actions  = $this->get_row_actions( $donor );
178
179
		return '<a href="' . esc_url( $view_url ) . '">' . $name . '</a>' . $this->row_actions( $actions );
180
	}
181
182
	/**
183
	 * Retrieve the table columns.
184
	 *
185
	 * @access public
186
	 * @since  1.0
187
	 *
188
	 * @return array $columns Array of all the list table columns.
189
	 */
190
	public function get_columns() {
191
		$columns = array(
192
			'cb'            => '<input type="checkbox" />', // Render a checkbox instead of text.
193
			'name'          => __( 'Name', 'give' ),
194
			'email'         => __( 'Email', 'give' ),
195
			'num_donations' => __( 'Donations', 'give' ),
196
			'amount_spent'  => __( 'Total Donated', 'give' ),
197
			'date_created'  => __( 'Date Created', 'give' ),
198
		);
199
200
		return apply_filters( 'give_list_donors_columns', $columns );
201
202
	}
203
204
	/**
205
	 * Get the sortable columns.
206
	 *
207
	 * @access public
208
	 * @since  2.1
209
	 * @return array Array of all the sortable columns.
210
	 */
211 View Code Duplication
	public function get_sortable_columns() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
212
213
		$columns = array(
214
			'date_created'  => array( 'date_created', true ),
215
			'name'          => array( 'name', true ),
216
			'num_donations' => array( 'purchase_count', false ),
217
			'amount_spent'  => array( 'purchase_value', false ),
218
		);
219
220
		return apply_filters( 'give_list_donors_sortable_columns', $columns );
221
	}
222
223
	/**
224
	 * Retrieve row actions.
225
	 *
226
	 * @param array $donor Donor Data.
227
	 *
228
	 * @since  1.7
229
	 * @access public
230
	 *
231
	 * @return array An array of action links.
232
	 */
233
	public function get_row_actions( $donor ) {
234
235
		$actions = array(
236
			'view'   => sprintf( '<a href="%1$s" aria-label="%2$s">%3$s</a>', admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor['id'] ), sprintf( esc_attr__( 'View "%s"', 'give' ), $donor['name'] ), __( 'View Donor', 'give' ) ),
237
			'notes'  => sprintf( '<a href="%1$s" aria-label="%2$s">%3$s</a>', admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=notes&id=' . $donor['id'] ), sprintf( esc_attr__( 'Notes for "%s"', 'give' ), $donor['name'] ), __( 'Notes', 'give' ) ),
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$donor'
Loading history...
238
			'delete' => sprintf( '<a class="%1$s" href="#" aria-label="%2$s">%3$s</a>', 'give-single-donor-delete', sprintf( esc_attr__( 'Delete "%s"', 'give' ), $donor['name'] ), __( 'Delete', 'give' ) ),
239
		);
240
241
		return apply_filters( 'give_donor_row_actions', $actions, $donor );
242
243
	}
244
245
	/**
246
	 * Retrieve the current page number.
247
	 *
248
	 * @access public
249
	 * @since  1.0
250
	 *
251
	 * @return int Current page number.
252
	 */
253
	public function get_paged() {
254
		return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
255
	}
256
257
	/**
258
	 * Retrieves the search query string.
259
	 *
260
	 * @access public
261
	 * @since  1.0
262
	 *
263
	 * @return mixed string If search is present, false otherwise.
264
	 */
265
	public function get_search() {
266
		return ! empty( $_GET['s'] ) ? urldecode( trim( $_GET['s'] ) ) : 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...
267
	}
268
269
	/**
270
	 * Get the Bulk Actions.
271
	 *
272
	 * @access public
273
	 * @since  1.8.16
274
	 *
275
	 * @return array
276
	 */
277
	public function get_bulk_actions() {
278
		$actions = array(
279
			'delete' => __( 'Delete', 'give' ),
280
		);
281
		return $actions;
282
	}
283
284
	/**
285
	 * Generate the table navigation above or below the table
286
	 *
287
	 * @param string $which Position to trigger i.e. Top/Bottom.
288
	 *
289
	 * @access protected
290
	 * @since  1.8.16
291
	 */
292 View Code Duplication
	protected function display_tablenav( $which ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
293
		if ( 'top' === $which ) {
294
			wp_nonce_field( 'bulk-' . $this->_args['plural'], '_wpnonce', false );
295
		}
296
		?>
297
		<div class="tablenav <?php echo esc_attr( $which ); ?>">
298
			<?php if ( $this->has_items() ): ?>
299
				<div class="alignleft actions bulkactions">
300
					<?php $this->bulk_actions( $which ); ?>
301
				</div>
302
			<?php endif;
303
			$this->extra_tablenav( $which );
304
			$this->pagination( $which );
305
			?>
306
			<br class="clear" />
307
		</div>
308
		<?php
309
	}
310
311
	/**
312
	 * Retrieves the donor data from db.
313
	 *
314
	 * @access public
315
	 * @since  1.0
316
	 *
317
	 * @return array $data The Donor data.
318
	 */
319 View Code Duplication
	public function donor_data() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
320
321
		$data = array();
322
323
		// Get donor query.
324
		$args   = $this->get_donor_query();
325
		$donors = Give()->donors->get_donors( $args );
326
327
		if ( $donors ) {
328
329
			foreach ( $donors as $donor ) {
330
331
				$user_id = ! empty( $donor->user_id ) ? intval( $donor->user_id ) : 0;
332
333
				$data[] = array(
334
					'id'            => $donor->id,
335
					'user_id'       => $user_id,
336
					'name'          => $donor->name,
337
					'email'         => $donor->email,
338
					'num_donations' => $donor->purchase_count,
339
					'amount_spent'  => $donor->purchase_value,
340
					'date_created'  => $donor->date_created,
341
				);
342
			}
343
		}
344
345
		return apply_filters( 'give_donors_column_query_data', $data );
346
	}
347
348
	/**
349
	 * Get donor count.
350
	 *
351
	 * @since  1.8.1
352
	 * @access private
353
	 */
354 View Code Duplication
	private function get_donor_count() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
355
		// Get donor query.
356
		$_donor_query = $this->get_donor_query();
357
358
		$_donor_query['number'] = - 1;
359
		$_donor_query['offset'] = 0;
360
		$donors                 = Give()->donors->get_donors( $_donor_query );
361
362
		return count( $donors );
363
	}
364
365
	/**
366
	 * Get donor query.
367
	 *
368
	 * @since  1.8.1
369
	 * @access public
370
	 *
371
	 * @return array
372
	 */
373 View Code Duplication
	public function get_donor_query() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
374
		$paged   = $this->get_paged();
375
		$offset  = $this->per_page * ( $paged - 1 );
376
		$search  = $this->get_search();
377
		$order   = isset( $_GET['order'] ) ? sanitize_text_field( $_GET['order'] ) : 'DESC';
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
378
		$orderby = isset( $_GET['orderby'] ) ? sanitize_text_field( $_GET['orderby'] ) : 'id';
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
379
380
		$args = array(
381
			'number'  => $this->per_page,
382
			'offset'  => $offset,
383
			'order'   => $order,
384
			'orderby' => $orderby,
385
		);
386
387
		if ( $search ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $search of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
388
			if ( is_email( $search ) ) {
389
				$args['email'] = $search;
390
			} elseif ( is_numeric( $search ) ) {
391
				$args['id'] = $search;
392
			} else {
393
				$args['name'] = $search;
394
			}
395
		}
396
397
		return $args;
398
	}
399
400
	/**
401
	 * Generates content for a single row of the table
402
	 *
403
	 * @param object $item The current item.
404
	 *
405
	 * @since  1.8.17
406
	 * @access public
407
	 */
408
	public function single_row( $item ) {
409
		echo sprintf( '<tr id="donor-%1$d" data-id="%2$d" data-name="%3$s">', $item['id'], $item['id'], $item['name'] );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
410
		$this->single_row_columns( $item );
411
		echo '</tr>';
412
	}
413
414
	/**
415
	 * Display the final donor table
416
	 *
417
	 * @since 1.8.17
418
	 * @access public
419
	 */
420
	public function display() {
421
		$singular = $this->_args['singular'];
422
423
		$this->display_tablenav( 'top' );
424
425
		$this->screen->render_screen_reader_content( 'heading_list' );
426
		?>
427
		<table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>">
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'implode'
Loading history...
428
			<thead>
429
			<tr>
430
				<?php $this->print_column_headers(); ?>
431
			</tr>
432
			</thead>
433
434
			<tbody id="the-list"<?php
435
			if ( $singular ) {
436
				echo " data-wp-lists='list:$singular'";
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '" data-wp-lists='list:$singular'"'
Loading history...
437
			} ?>>
438
			<tr class="hidden"></tr>
439
			<tr id="give-bulk-delete" class="inline-edit-row inline-edit-row-page inline-edit-page bulk-edit-row bulk-edit-row-page bulk-edit-page inline-editor" style="display: none;" >
440
				<td colspan="6" class="colspanchange">
441
442
					<fieldset class="inline-edit-col-left">
443
						<legend class="inline-edit-legend"><?php _e( 'BULK DELETE', 'give' ); ?></legend>
444
						<div class="inline-edit-col">
445
							<div id="bulk-titles">
446
								<div id="give-bulk-donors" class="give-bulk-donors">
447
448
								</div>
449
							</div>
450
					</fieldset>
451
452
					<fieldset class="inline-edit-col-right">
453
						<div class="inline-edit-col">
454
							<label>
455
								<input id="give-delete-donor-confirm" type="checkbox" name="give-delete-donor-confirm"/>
456
								<?php _e( 'Are you sure you want to delete the selected donor(s)?', 'give' ); ?>
457
							</label>
458
							<label>
459
								<input id="give-delete-donor-records" type="checkbox" name="give-delete-donor-records"/>
460
								<?php _e( 'Delete all associated donations and records?', 'give' ); ?>
461
							</label>
462
						</div>
463
					</fieldset>
464
465
					<p class="submit inline-edit-save">
466
						<input type="hidden" name="give_action" value="delete_donor"/>
467
						<input type="hidden" name="s" value="<?php echo ( ! empty( $_GET['s'] ) ) ? $_GET['s'] : ''; ?>"/>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
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...
468
						<input type="hidden" name="orderby" value="<?php echo ( ! empty( $_GET['orderby'] ) ) ? $_GET['orderby'] : 'id'; ?>"/>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
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...
469
						<input type="hidden" name="order" value="<?php echo ( ! empty( $_GET['order'] ) ) ? $_GET['order'] : 'desc'; ?>"/>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
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...
470
						<button type="button" id="give-bulk-delete-cancel" class="button cancel alignleft"><?php _e( 'Cancel', 'give' ); ?></button>
471
						<input type="submit" id="give-bulk-delete-button" disabled class="button button-primary alignright" value="<?php _e( 'Delete', 'give' ); ?>">
472
						<br class="clear">
473
					</p>
474
				</td>
475
			</tr>
476
			<?php $this->display_rows_or_placeholder(); ?>
477
			</tbody>
478
479
			<tfoot>
480
			<tr>
481
				<?php $this->print_column_headers( false ); ?>
482
			</tr>
483
			</tfoot>
484
485
		</table>
486
		<?php
487
		$this->display_tablenav( 'bottom' );
488
	}
489
490
	/**
491
	 * Setup the final data for the table.
492
	 *
493
	 * @access public
494
	 * @since  1.0
495
	 *
496
	 * @return void
497
	 */
498 View Code Duplication
	public function prepare_items() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
499
500
		$columns  = $this->get_columns();
501
		$hidden   = array(); // No hidden columns.
502
		$sortable = $this->get_sortable_columns();
503
504
		$this->_column_headers = array( $columns, $hidden, $sortable );
505
506
		$this->items = $this->donor_data();
507
508
		$this->total = $this->get_donor_count();
509
510
		$this->set_pagination_args( array(
511
			'total_items' => $this->total,
512
			'per_page'    => $this->per_page,
513
			'total_pages' => ceil( $this->total / $this->per_page ),
514
		) );
515
	}
516
}
517