Completed
Push — issues/1132 ( b0ddd9...34d612 )
by Ravinder
22:33 queued 02:37
created

Give_Sales_Log_Table::column_default()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 62
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 39
nc 8
nop 2
dl 0
loc 62
rs 6.943
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
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 28 and the first side effect is on line 13.

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
 * 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
	public function __construct() {
44
		global $status, $page;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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
79
			case 'amount' :
80
				$value = give_currency_filter( give_format_amount( $item['amount'] ) );
81
				$value .= sprintf( '<br><small>%1$s %2$s</small>', __( 'via', 'give' ), give_get_gateway_admin_label( $payment->gateway ) );
82
83
				return $value;
84
85
			case 'status' :
86
87
				$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>';
88
89
				if ( $payment->mode == 'test' ) {
90
					$value .= Give()->tooltips->render_span( array(
91
						'label'       => __( 'This donation was made in test mode.', 'give' ),
92
						'tag_content' => __( 'Test', 'give' ),
93
						'attributes'  => array(
94
							'class' => 'give-item-label give-item-label-orange give-test-mode-transactions-label',
95
						),
96
					) );
97
				}
98
99
				return $value;
100
101
			case 'donation' :
102
				$value = Give()->tooltips->render_link( array(
103
					'label'       => sprintf( esc_attr__( 'View Donation #%s', 'give' ), $payment->ID ),
104
					'tag_content' => "#$payment->ID",
105
					'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' ) ) ),
106
				) );
107
108
				if ( ! empty( $item['user_id'] ) ) {
109
					$value .= sprintf(
110
						'&nbsp;%1$s&nbsp;<a href="%2$s">%3$s</a><br>',
111
						esc_html__( 'by', 'give' ),
112
						admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&user=' . $item['user_id'] ),
113
						$item['donor_name']
114
					);
115
				} else {
116
					$value .= sprintf(
117
						'&nbsp;%1$s&nbsp;%2$s<br>',
118
						esc_html__( 'by', 'give' ),
119
						__( 'No donor attached', 'give' )
120
					);;
121
				}
122
123
				return $value;
124
125
			default:
126
				return $item[ $column_name ];
127
		}
128
	}
129
130
	/**
131
	 * Retrieve the table columns
132
	 *
133
	 * @access public
134
	 * @since  1.0
135
	 * @return array $columns Array of all the list table columns
136
	 */
137
	public function get_columns() {
138
		$columns = array(
139
			'ID'       => __( 'Log ID', 'give' ),
140
			'donation' => __( 'Donation', 'give' ),
141
			'form'     => __( 'Form', 'give' ),
142
			'status'   => __( 'Status', 'give' ),
143
			'amount'   => __( 'Donation Amount', 'give' ),
144
			'date'     => __( 'Date', 'give' ),
145
		);
146
147
		return $columns;
148
	}
149
150
	/**
151
	 * Retrieve the current page number
152
	 *
153
	 * @access public
154
	 * @since  1.0
155
	 * @return int Current page number
156
	 */
157
	public function get_paged() {
158
		return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
159
	}
160
161
	/**
162
	 * Retrieves the user we are filtering logs by, if any
163
	 *
164
	 * @access public
165
	 * @since  1.0
166
	 * @return mixed int If User ID, string If Email/Login
167
	 */
168
	public function get_filtered_user() {
169
		return isset( $_GET['user'] ) ? absint( $_GET['user'] ) : false;
170
	}
171
172
	/**
173
	 * Retrieves the ID of the give_form we're filtering logs by
174
	 *
175
	 * @access public
176
	 * @since  1.0
177
	 * @return int Download ID
178
	 */
179
	public function get_filtered_give_form() {
180
		return ! empty( $_GET['form'] ) ? absint( $_GET['form'] ) : false;
181
	}
182
183
	/**
184
	 * Retrieves the search query string
185
	 *
186
	 * @access public
187
	 * @since  1.0
188
	 * @return string|bool string If search is present, false otherwise
189
	 */
190
	public function get_search() {
191
		return ! empty( $_GET['s'] ) ? urldecode( trim( $_GET['s'] ) ) : false;
192
	}
193
194
195
	/**
196
	 * Display Tablenav (extended)
197
	 *
198
	 * Display the table navigation above or below the table even when no items in the logs, so nav doesn't disappear
199
	 *
200
	 * @see    : https://github.com/WordImpress/Give/issues/564
201
	 *
202
	 * @since  1.4.1
203
	 * @access protected
204
	 *
205
	 * @param string $which
206
	 */
207
	protected function display_tablenav( $which ) {
208
		?>
209
		<div class="tablenav <?php echo esc_attr( $which ); ?>">
210
211
			<?php if ( 'top' === $which ) : ?>
212
				<div class="alignleft actions bulkactions">
213
					<?php $this->bulk_actions( $which ); ?>
214
				</div>
215
			<?php endif; ?>
216
217
			<?php
218
			$this->extra_tablenav( $which );
219
			$this->pagination( $which );
220
			?>
221
222
			<br class="clear"/>
223
		</div>
224
		<?php
225
	}
226
227
228
	/**
229
	 * Gets the meta query for the log query
230
	 *
231
	 * This is used to return log entries that match our search query, user query, or form query
232
	 *
233
	 * @access public
234
	 * @since  1.0
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 searche
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
	 * @return void
314
	 */
315
	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...
316
		give_log_views();
317
	}
318
319
	/**
320
	 * Sets up the forms filter
321
	 *
322
	 * @access public
323
	 * @since  1.0
324
	 * @return void
325
	 */
326
	public function give_forms_filter() {
327
		echo Give()->html->forms_dropdown( array(
328
			'selected' => $this->get_filtered_give_form(),
329
			'name'   => 'form',
330
			'id'     => 'give-log-form-filter',
331
			'chosen' => true,
332
		) );
333
	}
334
335
	/**
336
	 * Gets the log entries for the current view
337
	 *
338
	 * @access public
339
	 * @since  1.0
340
	 *
341
	 * @return array $logs_data Array of all the Log entires
342
	 */
343
	public function get_logs() {
344
		$logs_data = array();
345
		$log_query = $this->get_query_params();
346
		$logs = Give()->logs->get_connected_logs( $log_query );
347
348
		if ( $logs ) {
349
			foreach ( $logs as $log ) {
350
				/* @var Give_payment $payment */
351
				$payment = new Give_Payment( $log->log_parent );
352
353
				// Make sure this payment hasn't been deleted
354
				if ( get_post( $payment->ID ) ) :
355
					$logs_data[] = array(
356
						'ID'         => '<span class="give-item-label give-item-label-gray">' . $log->ID . '</span>',
357
						'payment_id' => $payment->ID,
358
						'form'       => $payment->form_id,
359
						'amount'     => $payment->total,
360
						'user_id'    => $payment->user_id,
361
						'donor_name' => trim( "{$payment->first_name} $payment->last_name" ),
362
						'date'       => $payment->date,
363
					);
364
365
				endif;
366
			}
367
		}
368
369
		return $logs_data;
370
	}
371
372
	/**
373
	 * Setup the final data for the table
374
	 *
375
	 * @access public
376
	 * @since  1.0
377
	 * @uses   Give_Sales_Log_Table::get_columns()
378
	 * @uses   WP_List_Table::get_sortable_columns()
379
	 * @uses   Give_Sales_Log_Table::get_pagenum()
380
	 * @uses   Give_Sales_Log_Table::get_logs()
381
	 * @uses   Give_Sales_Log_Table::get_log_count()
382
	 *
383
	 * @return void
384
	 */
385
	public function prepare_items() {
386
		$columns               = $this->get_columns();
387
		$hidden                = array();
388
		$sortable              = $this->get_sortable_columns();
389
		$this->_column_headers = array( $columns, $hidden, $sortable );
390
		$current_page          = $this->get_pagenum();
391
		$this->items           = $this->get_logs();
392
		$total_items           = Give()->logs->get_log_count( 0, 'sale', $this->get_meta_query() );
393
394
		$this->set_pagination_args( array(
395
				'total_items' => $total_items,
396
				'per_page'    => $this->per_page,
397
				'total_pages' => ceil( $total_items / $this->per_page ),
398
			)
399
		);
400
	}
401
402
403
	/**
404
	 * Get log query param.
405
	 *
406
	 * @since  2.0
407
	 * @access public
408
	 *
409
	 * @return array
410
	 */
411
	public function get_query_params() {
412
		$paged     = $this->get_paged();
413
		$user      = $this->get_filtered_user();
414
415
		$log_query = array(
416
			'log_type'   => 'sale',
417
			'paged'      => $paged,
418
			'meta_query' => $this->get_meta_query(),
419
			'number'     => $this->per_page,
420
		);
421
422
		return $log_query;
423
	}
424
}
425