Completed
Push — master ( e1d700...a14d96 )
by Devin
37:09 queued 17:09
created

Give_Sales_Log_Table::column_default()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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