Completed
Push — master ( cbdcdc...d800f4 )
by Devin
39:33 queued 19:39
created

Give_Sales_Log_Table::column_default()   D

Complexity

Conditions 9
Paths 8

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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