Completed
Push — master ( 4e5e5b...82e76a )
by Devin
37:33 queued 17:37
created

Give_Sales_Log_Table::display_tablenav()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 20
rs 9.4285
cc 2
eloc 13
nc 2
nop 1
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     http://opensource.org/licenses/gpl-2.0.php 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
		switch ( $column_name ) {
69
			case 'form' :
70
				return '<a href="' . esc_url( add_query_arg( 'form', $item[ $column_name ] ) ) . '" >' . get_the_title( $item[ $column_name ] ) . '</a>';
71
72
			case 'user_id' :
73
				return '<a href="' .
74
				       admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&user=' . ( ! empty( $item['user_id'] ) ? urlencode( $item['user_id'] ) : give_get_payment_user_email( $item['payment_id'] ) ) ) .
75
				       '">' . $item['user_name'] . '</a>';
76
77
			case 'amount' :
78
				return give_currency_filter( give_format_amount( $item['amount'] ) );
79
80
			case 'payment_id' :
81
				return '<a href="' . admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-order-details&id=' . $item['payment_id'] ) . '">' . give_get_payment_number( $item['payment_id'] ) . '</a>';
82
83
			default:
84
				return $item[ $column_name ];
85
		}
86
	}
87
88
	/**
89
	 * Retrieve the table columns
90
	 *
91
	 * @access public
92
	 * @since  1.0
93
	 * @return array $columns Array of all the list table columns
94
	 */
95
	public function get_columns() {
96
		$columns = array(
97
			'ID'         => __( 'Log ID', 'give' ),
98
			'user_id'    => __( 'User', 'give' ),
99
			'form'       => give_get_forms_label_singular(),
100
			'amount'     => __( 'Item Amount', 'give' ),
101
			'payment_id' => __( 'Payment ID', 'give' ),
102
			'date'       => __( 'Date', 'give' )
103
		);
104
105
		return $columns;
106
	}
107
108
	/**
109
	 * Retrieve the current page number
110
	 *
111
	 * @access public
112
	 * @since  1.0
113
	 * @return int Current page number
114
	 */
115
	public function get_paged() {
116
		return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
117
	}
118
119
	/**
120
	 * Retrieves the user we are filtering logs by, if any
121
	 *
122
	 * @access public
123
	 * @since  1.0
124
	 * @return mixed int If User ID, string If Email/Login
125
	 */
126
	public function get_filtered_user() {
127
		return isset( $_GET['user'] ) ? absint( $_GET['user'] ) : false;
128
	}
129
130
	/**
131
	 * Retrieves the ID of the give_form we're filtering logs by
132
	 *
133
	 * @access public
134
	 * @since  1.0
135
	 * @return int Download ID
136
	 */
137
	public function get_filtered_give_form() {
138
		return ! empty( $_GET['form'] ) ? absint( $_GET['form'] ) : false;
139
	}
140
141
	/**
142
	 * Retrieves the search query string
143
	 *
144
	 * @access public
145
	 * @since  1.0
146
	 * @return mixed string If search is present, false otherwise
147
	 */
148
	public function get_search() {
149
		return ! empty( $_GET['s'] ) ? urldecode( trim( $_GET['s'] ) ) : false;
150
	}
151
152
153
	/**
154
	 * Display Tablenav (extended)
155
	 * 
156
	 * @description: Display the table navigation above or below the table even when no items in the logs, so nav doesn't disappear
157
	 * 
158
	 * @see: https://github.com/WordImpress/Give/issues/564
159
	 *
160
	 * @since 1.4.1
161
	 * @access protected
162
	 *
163
	 * @param string $which
164
	 */
165
	protected function display_tablenav( $which ) {
166
167
		if ( 'top' === $which ) {
168
			wp_nonce_field( 'bulk-' . $this->_args['plural'] );
169
		}
170
		?>
171
		<div class="tablenav <?php echo esc_attr( $which ); ?>">
172
173
			<div class="alignleft actions bulkactions">
174
				<?php $this->bulk_actions( $which ); ?>
175
			</div>
176
			<?php
177
			$this->extra_tablenav( $which );
178
			$this->pagination( $which );
179
			?>
180
181
			<br class="clear"/>
182
		</div>
183
		<?php
184
	}
185
186
187
	/**
188
	 * Gets the meta query for the log query
189
	 *
190
	 * This is used to return log entries that match our search query, user query, or form query
191
	 *
192
	 * @access public
193
	 * @since  1.0
194
	 * @return array $meta_query
195
	 */
196
	public function get_meta_query() {
197
		$user = $this->get_filtered_user();
198
199
		$meta_query = array();
200
201
		if ( $user ) {
202
			// Show only logs from a specific user
203
			$meta_query[] = array(
204
				'key'   => '_give_log_user_id',
205
				'value' => $user
206
			);
207
		}
208
209
		$search = $this->get_search();
210
		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...
211
			if ( is_email( $search ) ) {
212
				// This is an email search. We use this to ensure it works for guest users and logged-in users
213
				$key     = '_give_log_user_info';
214
				$compare = 'LIKE';
215
			} else {
216
				// Look for a user
217
				$key     = '_give_log_user_id';
218
				$compare = 'LIKE';
219
220
				if ( ! is_numeric( $search ) ) {
221
					// Searching for user by username
222
					$user = get_user_by( 'login', $search );
223
224
					if ( $user ) {
225
						// Found one, set meta value to user's ID
226
						$search = $user->ID;
227
					} else {
228
						// No user found so let's do a real search query
229
						$users = new WP_User_Query( array(
230
							'search'         => $search,
231
							'search_columns' => array( 'user_url', 'user_nicename' ),
232
							'number'         => 1,
233
							'fields'         => 'ids'
234
						) );
235
236
						$found_user = $users->get_results();
237
238
						if ( $found_user ) {
239
							$search = $found_user[0];
240
						}
241
					}
242
				}
243
			}
244
245
			if ( ! $this->file_search ) {
246
				// Meta query only works for non file name searche
247
				$meta_query[] = array(
248
					'key'     => $key,
249
					'value'   => $search,
250
					'compare' => $compare
251
				);
252
253
			}
254
		}
255
256
		return $meta_query;
257
	}
258
259
	/**
260
	 * Outputs the log views
261
	 *
262
	 * @access public
263
	 * @since  1.0
264
	 * @return void
265
	 */
266
	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...
267
		give_log_views();
268
	}
269
270
	/**
271
	 * Sets up the forms filter
272
	 *
273
	 * @access public
274
	 * @since  1.0
275
	 * @return void
276
	 */
277
	public function give_forms_filter() {
278
		$give_forms = get_posts( array(
279
			'post_type'              => 'give_forms',
280
			'post_status'            => 'any',
281
			'posts_per_page'         => - 1,
282
			'orderby'                => 'title',
283
			'order'                  => 'ASC',
284
			'fields'                 => 'ids',
285
			'update_post_meta_cache' => false,
286
			'update_post_term_cache' => false
287
		) );
288
289
		if ( $give_forms ) {
290
			echo '<select name="form" id="give-log-form-filter">';
291
			echo '<option value="0">' . __( 'All', 'give' ) . '</option>';
292
			foreach ( $give_forms as $form ) {
293
				echo '<option value="' . $form . '"' . selected( $form, $this->get_filtered_give_form() ) . '>' . esc_html( get_the_title( $form ) ) . '</option>';
294
			}
295
			echo '</select>';
296
		}
297
	}
298
299
	/**
300
	 * Gets the log entries for the current view
301
	 *
302
	 * @access public
303
	 * @since  1.0
304
	 * @global object $give_logs Give Logs Object
305
	 * @return array $logs_data Array of all the Log entires
306
	 */
307
	public function get_logs() {
308
		global $give_logs;
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...
309
310
		// Prevent the queries from getting cached. Without this there are occasional memory issues for some installs
311
		wp_suspend_cache_addition( true );
312
313
		$logs_data = array();
314
		$paged     = $this->get_paged();
315
		$give_form = empty( $_GET['s'] ) ? $this->get_filtered_give_form() : null;
316
		$user      = $this->get_filtered_user();
317
318
		$log_query = array(
319
			'post_parent' => $give_form,
320
			'log_type'    => 'sale',
321
			'paged'       => $paged,
322
			'meta_query'  => $this->get_meta_query()
323
		);
324
325
		$logs = $give_logs->get_connected_logs( $log_query );
326
327
		if ( $logs ) {
328
			foreach ( $logs as $log ) {
329
				$payment_id = get_post_meta( $log->ID, '_give_log_payment_id', true );
330
331
				// Make sure this payment hasn't been deleted
332
				if ( get_post( $payment_id ) ) :
333
					$user_info      = give_get_payment_meta_user_info( $payment_id );
334
					$payment_meta   = give_get_payment_meta( $payment_id );
335
					$payment_amount = give_get_payment_amount( $payment_id );
336
337
					$logs_data[] = array(
338
						'ID'         => '<span class="give-item-label give-item-label-gray">' . $log->ID . '</span>',
339
						'payment_id' => $payment_id,
340
						'form'       => $log->post_parent,
341
						'amount'     => $payment_amount,
342
						'user_id'    => $user_info['id'],
343
						'user_name'  => $user_info['first_name'] . ' ' . $user_info['last_name'],
344
						'date'       => get_post_field( 'post_date', $payment_id )
345
					);
346
347
				endif;
348
			}
349
		}
350
351
		return $logs_data;
352
	}
353
354
	/**
355
	 * Setup the final data for the table
356
	 *
357
	 * @access public
358
	 * @since  1.0
359
	 * @global object $give_logs Give Logs Object
360
	 * @uses   Give_Sales_Log_Table::get_columns()
361
	 * @uses   WP_List_Table::get_sortable_columns()
362
	 * @uses   Give_Sales_Log_Table::get_pagenum()
363
	 * @uses   Give_Sales_Log_Table::get_logs()
364
	 * @uses   Give_Sales_Log_Table::get_log_count()
365
	 * @return void
366
	 */
367
	public function prepare_items() {
368
		global $give_logs;
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...
369
370
		$columns               = $this->get_columns();
371
		$hidden                = array();
372
		$sortable              = $this->get_sortable_columns();
373
		$this->_column_headers = array( $columns, $hidden, $sortable );
374
		$current_page          = $this->get_pagenum();
375
		$this->items           = $this->get_logs();
376
		$total_items           = $give_logs->get_log_count( $this->get_filtered_give_form(), 'sale', $this->get_meta_query() );
377
378
		$this->set_pagination_args( array(
379
				'total_items' => $total_items,
380
				'per_page'    => $this->per_page,
381
				'total_pages' => ceil( $total_items / $this->per_page )
382
			)
383
		);
384
	}
385
}
386