Test Failed
Push — issues/1953 ( fc402a )
by Ravinder
05:33
created

Give_Sales_Log_Table   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 398
Duplicated Lines 5.28 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 21
loc 398
rs 8.6
c 0
b 0
f 0
wmc 37
lcom 1
cbo 5

14 Methods

Rating   Name   Duplication   Size   Complexity  
C get_meta_query() 0 70 9
A __construct() 12 12 1
A bulk_actions() 0 3 1
A give_forms_filter() 0 8 1
B get_logs() 0 28 4
A prepare_items() 0 16 1
A get_query_params() 0 13 1
B column_default() 9 61 8
A get_columns() 0 12 1
A get_paged() 0 3 2
A get_filtered_user() 0 3 2
A get_filtered_give_form() 0 3 2
A get_search() 0 3 2
A display_tablenav() 0 19 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Sales Log View Class
4
 *
5
 * @package     Give
6
 * @subpackage  Admin/Reports
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 */
10
11
// Exit if accessed directly.
12
if ( ! defined( 'ABSPATH' ) ) {
13
	exit;
14
}
15
16
// Load WP_List_Table if not loaded.
17
if ( ! class_exists( 'WP_List_Table' ) ) {
18
	require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
19
}
20
21
/**
22
 * Give_Sales_Log_Table Class
23
 *
24
 * Renders the sales log list table
25
 *
26
 * @since 1.0
27
 */
28
class Give_Sales_Log_Table extends WP_List_Table {
29
	/**
30
	 * Number of results to show per page
31
	 *
32
	 * @since 1.0
33
	 * @var int
34
	 */
35
	public $per_page = 30;
36
37
	/**
38
	 * Get things started
39
	 *
40
	 * @since 1.0
41
	 * @see   WP_List_Table::__construct()
42
	 */
43 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...
44
		global $status, $page;
45
46
		// Set parent defaults
47
		parent::__construct( array(
48
			'singular' => give_get_forms_label_singular(),    // Singular name of the listed records
49
			'plural'   => give_get_forms_label_plural(),        // Plural name of the listed records
50
			'ajax'     => false,// Does this table support ajax?
51
		) );
52
53
		add_action( 'give_log_view_actions', array( $this, 'give_forms_filter' ) );
54
	}
55
56
	/**
57
	 * This function renders most of the columns in the list table.
58
	 *
59
	 * @access public
60
	 * @since  1.0
61
	 *
62
	 * @param array  $item        Contains all the data of the discount code
63
	 * @param string $column_name The name of the column
64
	 *
65
	 * @return string Column Name
66
	 */
67
	public function column_default( $item, $column_name ) {
68
69
		$payment = give_get_payment_by( 'id', $item['payment_id'] );
70
71
		switch ( $column_name ) {
72
			case 'form' :
73
				$form_title = get_the_title( $item[ $column_name ] );
74
				$form_title = empty( $form_title ) ? sprintf( __( 'Untitled (#%s)', 'give' ), $item[ $column_name ] ) : $form_title;
75
76
				return '<a href="' . esc_url( add_query_arg( 'form', $item[ $column_name ] ) ) . '" >' . $form_title . '</a>';
77
78
			case 'amount' :
79
				$value = give_currency_filter( give_format_amount( $item['amount'], array( 'sanitize' => false ) ) );
80
				$value .= sprintf( '<br><small>%1$s %2$s</small>', __( 'via', 'give' ), give_get_gateway_admin_label( $payment->gateway ) );
81
82
				return $value;
83
84
			case 'status' :
85
86
				$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>';
87
88 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...
89
					$value .= Give()->tooltips->render_span( array(
90
						'label'       => __( 'This donation was made in test mode.', 'give' ),
91
						'tag_content' => __( 'Test', 'give' ),
92
						'attributes'  => array(
93
							'class' => 'give-item-label give-item-label-orange give-test-mode-transactions-label',
94
						),
95
					) );
96
				}
97
98
				return $value;
99
100
			case 'donation' :
101
				$value = Give()->tooltips->render_link( array(
102
					'label'       => sprintf( esc_attr__( 'View Donation #%s', 'give' ), $payment->ID ),
103
					'tag_content' => "#$payment->ID",
104
					'link'        => esc_url( add_query_arg( 'id', $payment->ID, admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-payment-details' ) ) ),
105
				) );
106
107
				if ( ! empty( $item['donor_id'] ) ) {
108
					$value .= sprintf(
109
						'&nbsp;%1$s&nbsp;<a href="%2$s">%3$s</a><br>',
110
						esc_html__( 'by', 'give' ),
111
						admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&donor=' . $item['donor_id'] ),
112
						$item['donor_name']
113
					);
114
				} else {
115
					$value .= sprintf(
116
						'&nbsp;%1$s&nbsp;%2$s<br>',
117
						esc_html__( 'by', 'give' ),
118
						__( 'No donor attached', 'give' )
119
					);;
120
				}
121
122
				return $value;
123
124
			default:
125
				return $item[ $column_name ];
126
		}
127
	}
128
129
	/**
130
	 * Retrieve the table columns
131
	 *
132
	 * @access public
133
	 * @since  1.0
134
	 * @return array $columns Array of all the list table columns
135
	 */
136
	public function get_columns() {
137
		$columns = array(
138
			'ID'       => __( 'Log ID', 'give' ),
139
			'donation' => __( 'Donation', 'give' ),
140
			'form'     => __( 'Form', 'give' ),
141
			'status'   => __( 'Status', 'give' ),
142
			'amount'   => __( 'Donation Amount', 'give' ),
143
			'date'     => __( 'Date', 'give' ),
144
		);
145
146
		return $columns;
147
	}
148
149
	/**
150
	 * Retrieve the current page number
151
	 *
152
	 * @access public
153
	 * @since  1.0
154
	 * @return int Current page number
155
	 */
156
	public function get_paged() {
157
		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...
158
	}
159
160
	/**
161
	 * Retrieves the user we are filtering logs by, if any
162
	 *
163
	 * @access public
164
	 * @since  1.0
165
	 * @return mixed int If User ID, string If Email/Login
166
	 */
167
	public function get_filtered_user() {
168
		return isset( $_GET['user'] ) ? absint( $_GET['user'] ) : false;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
169
	}
170
171
	/**
172
	 * Retrieves the ID of the give_form we're filtering logs by
173
	 *
174
	 * @access public
175
	 * @since  1.0
176
	 * @return int Download ID
177
	 */
178
	public function get_filtered_give_form() {
179
		return ! empty( $_GET['form'] ) ? absint( $_GET['form'] ) : false;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
180
	}
181
182
	/**
183
	 * Retrieves the search query string
184
	 *
185
	 * @access public
186
	 * @since  1.0
187
	 * @return string|bool string If search is present, false otherwise
188
	 */
189
	public function get_search() {
190
		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...
191
	}
192
193
194
	/**
195
	 * Display Tablenav (extended)
196
	 *
197
	 * Display the table navigation above or below the table even when no items in the logs, so nav doesn't disappear
198
	 *
199
	 * @see    : https://github.com/WordImpress/Give/issues/564
200
	 *
201
	 * @since  1.4.1
202
	 * @access protected
203
	 *
204
	 * @param string $which
205
	 */
206
	protected function display_tablenav( $which ) {
207
		?>
208
		<div class="tablenav <?php echo esc_attr( $which ); ?>">
209
210
			<?php if ( 'top' === $which ) : ?>
211
				<div class="alignleft actions bulkactions">
212
					<?php $this->bulk_actions( $which ); ?>
213
				</div>
214
			<?php endif; ?>
215
216
			<?php
217
			$this->extra_tablenav( $which );
218
			$this->pagination( $which );
219
			?>
220
221
			<br class="clear"/>
222
		</div>
223
		<?php
224
	}
225
226
227
	/**
228
	 * Gets the meta query for the log query
229
	 *
230
	 * This is used to return log entries that match our search query, user query, or form query
231
	 *
232
	 * @since  1.0
233
	 * @access public
234
	 *
235
	 * @return array $meta_query
236
	 */
237
	public function get_meta_query() {
238
		$user = $this->get_filtered_user();
239
		$give_form = $this->get_filtered_give_form();
240
241
		$meta_query = array();
242
243
		if ( $user ) {
244
			// Show only logs from a specific user.
245
			$meta_query[] = array(
246
				'key'   => '_give_log_user_id',
247
				'value' => $user,
248
			);
249
		}
250
251
		if ( $give_form ) {
252
			$meta_query[] = array(
253
				'key'   => '_give_log_form_id',
254
				'value' => $give_form,
255
			);
256
		}
257
258
		$search = $this->get_search();
259
		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...
260
			if ( is_email( $search ) ) {
261
				// This is an email search. We use this to ensure it works for guest users and logged-in users.
262
				$key     = '_give_log_user_info';
263
				$compare = 'LIKE';
264
			} else {
265
				// Look for a user
266
				$key     = '_give_log_user_id';
267
				$compare = 'LIKE';
268
269
				if ( ! is_numeric( $search ) ) {
270
					// Searching for user by username
271
					$user = get_user_by( 'login', $search );
272
273
					if ( $user ) {
274
						// Found one, set meta value to user's ID.
275
						$search = $user->ID;
276
					} else {
277
						// No user found so let's do a real search query.
278
						$users = new WP_User_Query( array(
279
							'search'         => $search,
280
							'search_columns' => array( 'user_url', 'user_nicename' ),
281
							'number'         => 1,
282
							'fields'         => 'ids',
283
						) );
284
285
						$found_user = $users->get_results();
286
287
						if ( $found_user ) {
288
							$search = $found_user[0];
289
						}
290
					}
291
				}
292
			}
293
294
			if ( ! $this->file_search ) {
295
				// Meta query only works for non file name search.
296
				$meta_query[] = array(
297
					'key'     => $key,
298
					'value'   => $search,
299
					'compare' => $compare,
300
				);
301
302
			}
303
		}
304
305
		return $meta_query;
306
	}
307
308
	/**
309
	 * Outputs the log views
310
	 *
311
	 * @access public
312
	 * @since  1.0
313
	 * @param string $which
314
	 * @return void
315
	 */
316
	function bulk_actions( $which = '' ) {
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...
317
		give_log_views();
318
	}
319
320
	/**
321
	 * Sets up the forms filter
322
	 *
323
	 * @access public
324
	 * @since  1.0
325
	 * @return void
326
	 */
327
	public function give_forms_filter() {
328
		echo Give()->html->forms_dropdown( array(
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'Give'
Loading history...
329
			'selected' => $this->get_filtered_give_form(),
330
			'name'   => 'form',
331
			'id'     => 'give-log-form-filter',
332
			'chosen' => true,
333
		) );
334
	}
335
336
	/**
337
	 * Gets the log entries for the current view
338
	 *
339
	 * @access public
340
	 * @since  1.0
341
	 *
342
	 * @return array $logs_data Array of all the Log entires
343
	 */
344
	public function get_logs() {
345
		$logs_data = array();
346
		$log_query = $this->get_query_params();
347
		$logs = Give()->logs->get_connected_logs( $log_query );
348
349
		if ( $logs ) {
350
			foreach ( $logs as $log ) {
351
				/* @var Give_payment $payment */
352
				$payment = new Give_Payment( $log->log_parent );
353
354
				// Make sure this payment hasn't been deleted
355
				if ( get_post( $payment->ID ) ) :
356
					$logs_data[] = array(
357
						'ID'         => '<span class="give-item-label give-item-label-gray">' . $log->ID . '</span>',
358
						'payment_id' => $payment->ID,
359
						'form'       => $payment->form_id,
360
						'amount'     => $payment->total,
361
						'donor_id'    => $payment->customer_id,
362
						'donor_name' => trim( "{$payment->first_name} $payment->last_name" ),
363
						'date'       => $payment->date,
364
					);
365
366
				endif;
367
			}
368
		}
369
370
		return $logs_data;
371
	}
372
373
	/**
374
	 * Setup the final data for the table
375
	 *
376
	 * @access public
377
	 * @since  1.0
378
	 * @uses   Give_Sales_Log_Table::get_columns()
379
	 * @uses   WP_List_Table::get_sortable_columns()
380
	 * @uses   Give_Sales_Log_Table::get_pagenum()
381
	 * @uses   Give_Sales_Log_Table::get_logs()
382
	 * @uses   Give_Sales_Log_Table::get_log_count()
383
	 *
384
	 * @return void
385
	 */
386
	public function prepare_items() {
387
		$columns               = $this->get_columns();
388
		$hidden                = array();
389
		$sortable              = $this->get_sortable_columns();
390
		$this->_column_headers = array( $columns, $hidden, $sortable );
391
		$current_page          = $this->get_pagenum();
0 ignored issues
show
Unused Code introduced by
$current_page 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...
392
		$this->items           = $this->get_logs();
393
		$total_items           = Give()->logs->get_log_count( 0, 'sale', $this->get_meta_query() );
394
395
		$this->set_pagination_args( array(
396
				'total_items' => $total_items,
397
				'per_page'    => $this->per_page,
398
				'total_pages' => ceil( $total_items / $this->per_page ),
399
			)
400
		);
401
	}
402
403
404
	/**
405
	 * Get log query param.
406
	 *
407
	 * @since  2.0
408
	 * @access public
409
	 *
410
	 * @return array
411
	 */
412
	public function get_query_params() {
413
		$paged     = $this->get_paged();
414
		$user      = $this->get_filtered_user();
0 ignored issues
show
Unused Code introduced by
$user 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...
415
416
		$log_query = array(
417
			'log_type'   => 'sale',
418
			'paged'      => $paged,
419
			'meta_query' => $this->get_meta_query(),
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
420
			'number'     => $this->per_page,
421
		);
422
423
		return $log_query;
424
	}
425
}
426