/
class-sales-logs-list-table.php
executable file
·406 lines (351 loc) · 10.7 KB
/
class-sales-logs-list-table.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<?php
/**
* Sales Log View Class
*
* @package EDD
* @subpackage Admin/Reports
* @copyright Copyright (c) 2015, Pippin Williamson
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
// Load WP_List_Table if not loaded
if ( ! class_exists( 'WP_List_Table' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
/**
* EDD_Sales_Log_Table Class
*
* Renders the sales log list table
*
* @since 1.4
* @since 3.0 Updated to use the custom tables and new query classes.
*/
class EDD_Sales_Log_Table extends WP_List_Table {
/**
* Number of results to show per page
*
* @since 1.4
* @var int
*/
public $per_page = 30;
/**
* Get things started
*
* @since 1.4
* @see WP_List_Table::__construct()
*/
public function __construct() {
global $status, $page;
// Set parent defaults
parent::__construct( array(
'singular' => edd_get_label_singular(),
'plural' => edd_get_label_plural(),
'ajax' => false,
) );
add_action( 'edd_log_view_actions', array( $this, 'downloads_filter' ) );
}
/**
* Gets the name of the primary column.
*
* @since 2.5
* @access protected
*
* @return string Name of the primary column.
*/
protected function get_primary_column_name() {
return 'ID';
}
/**
* This function renders most of the columns in the list table.
*
* @since 1.4
*
* @param array $item Contains all the data of the log item
* @param string $column_name The name of the column
*
* @return string Column Name
*/
public function column_default( $item, $column_name ) {
$return = '';
$currency = $item['currency'];
switch ( $column_name ){
case 'download' :
$download_id = $item[ $column_name ];
$download = new EDD_Download( $download_id );
$title = $download->post_title;
if ( edd_has_variable_prices( $download->ID ) ) {
$price_id = $item['price_id'];
if ( ! is_null( $price_id ) && ! empty( $download->prices[ $price_id] ) ) {
$title .= ' — ' . $download->prices[ $price_id ]['name'];
}
}
$return = '<a href="' . add_query_arg( 'download', $item[ $column_name ] ) . '" >' . $title . '</a>';
break;
case 'customer' :
$name = ! empty( $item['customer']->name ) ? $item['customer']->name : '<em>' . __( 'Unnamed Customer','easy-digital-downloads' ) . '</em>';
$return = '<a href="' . esc_url( admin_url( 'edit.php?post_type=download&page=edd-customers&view=overview&id=' . $item['customer']->id ) ) . '">#' . $item['customer']->id . ' ' . $name . '</a>';
break;
case 'item_price' :
$return = edd_currency_filter( edd_format_amount( $item['item_price'] ), $currency );
break;
case 'amount' :
$return = edd_currency_filter( edd_format_amount( $item['amount'] / $item['quantity'] ), $currency );
break;
case 'payment_id' :
$return = '<a href="' . admin_url( 'edit.php?post_type=download&page=edd-payment-history&view=view-order-details&id=' . $item['payment_id'] ) . '">' . edd_get_payment_number( $item['payment_id'] ) . '</a>';
break;
default:
$return = $item[ $column_name ];
break;
}
return $return;
}
/**
* Retrieve the table columns
*
* @since 1.4
* @return array $columns Array of all the list table columns
*/
public function get_columns() {
$columns = array(
'ID' => __( 'Log ID', 'easy-digital-downloads' ),
'customer' => __( 'Customer', 'easy-digital-downloads' ),
'download' => edd_get_label_singular(),
'amount' => __( 'Item Amount', 'easy-digital-downloads' ),
'payment_id' => __( 'Payment ID', 'easy-digital-downloads' ),
'date' => __( 'Date', 'easy-digital-downloads' ),
);
return $columns;
}
/**
* Retrieve the current page number
*
* @since 1.4
* @return int Current page number
*/
public function get_paged() {
return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
}
/**
* Retrieves the user we are filtering logs by, if any
*
* @since 1.4
* @return mixed int If User ID, string If Email/Login
*/
public function get_filtered_user() {
return isset( $_GET['user'] ) ? absint( $_GET['user'] ) : false;
}
/**
* Retrieves the ID of the download we're filtering logs by
*
* @since 1.4
* @return int Download ID
*/
public function get_filtered_download() {
return ! empty( $_GET['download'] ) ? absint( $_GET['download'] ) : false;
}
/**
* Retrieves the search query string
*
* @since 1.4
* @return string|false string If search is present, false otherwise
*/
public function get_search() {
return ! empty( $_GET['s'] ) ? urldecode( trim( $_GET['s'] ) ) : false;
}
/**
* Gets the meta query for the log query
*
* This is used to return log entries that match our search query, user query, or download query
*
* @since 1.4
* @return array $meta_query
*/
public function get_meta_query() {
$user = $this->get_filtered_user();
$meta_query = array();
if( $user ) {
// Show only logs from a specific user
$meta_query[] = array(
'key' => '_edd_log_user_id',
'value' => $user,
);
}
$search = $this->get_search();
if ( $search ) {
if ( is_email( $search ) ) {
// This is an email search. We use this to ensure it works for guest users and logged-in users
$key = '_edd_log_user_info';
$compare = 'LIKE';
} else {
// Look for a user
$key = '_edd_log_user_id';
$compare = 'LIKE';
if ( ! is_numeric( $search ) ) {
// Searching for user by username
$user = get_user_by( 'login', $search );
if ( $user ) {
// Found one, set meta value to user's ID
$search = $user->ID;
} else {
// No user found so let's do a real search query
$users = new WP_User_Query( array(
'search' => $search,
'search_columns' => array( 'user_url', 'user_nicename' ),
'number' => 1,
'fields' => 'ids',
) );
$found_user = $users->get_results();
if ( $found_user ) {
$search = $found_user[0];
}
}
}
}
if ( ! $this->file_search ) {
// Meta query only works for non file name searche
$meta_query[] = array(
'key' => $key,
'value' => $search,
'compare' => $compare,
);
}
}
return $meta_query;
}
/**
* Outputs the log views
*
* @since 1.4
* @return void
*/
function bulk_actions( $which = '' ) {
// These aren't really bulk actions but this outputs the markup in the right place
edd_log_views();
}
/**
* Sets up the downloads filter
*
* @since 1.4
* @return void
*/
public function downloads_filter() {
$downloads = get_posts( array(
'post_type' => 'download',
'post_status' => 'any',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'fields' => 'ids',
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
) );
if ( $downloads ) {
echo '<select name="download" id="edd-log-download-filter">';
echo '<option value="0">' . __( 'All', 'easy-digital-downloads' ) . '</option>';
foreach ( $downloads as $download ) {
echo '<option value="' . $download . '"' . selected( $download, $this->get_filtered_download() ) . '>' . esc_html( get_the_title( $download ) ) . '</option>';
}
echo '</select>';
}
}
/**
* Gets the log entries for the current view
*
* @since 1.4
* @global object $edd_logs EDD Logs Object
* @return array $logs_data Array of all the Log entires
*/
public function get_logs() {
/** @var EDD_Logging $edd_logs */
global $edd_logs;
// Prevent the queries from getting cached. Without this there are occasional memory issues for some installs
wp_suspend_cache_addition( true );
$logs_data = array();
$paged = $this->get_paged();
$download = empty( $_GET['s'] ) ? $this->get_filtered_download() : null;
$log_query = array(
'type' => 'sale',
'offset' => $paged > 1 ? ( ( $paged - 1 ) * $this->per_page ) : 0,
'meta_query' => $this->get_meta_query(),
'number' => $this->per_page,
);
if ( $download ) {
$log_query['object_id'] = $download;
}
$logs = edd_get_logs( $log_query );
if ( $logs ) {
foreach ( $logs as $log ) {
/** @var EDD\Logs\Log $log */
$payment_id = $log->get_meta( 'payment_id' );
$payment = new EDD_Payment( $payment_id );
// Make sure this payment hasn't been deleted
if ( ! empty( $payment->ID ) ) {
$customer = new EDD_Customer( $payment->customer_id );
$cart_items = $payment->cart_details;
$amount = 0;
if ( is_array( $cart_items ) ) {
foreach ( $cart_items as $item ) {
// If the item has variable pricing, make sure it's the right variation
if ( $item['id'] == $log->get_object_id() ) {
if ( isset( $item['item_number']['options']['price_id'] ) ) {
$log_price_id = $log->get_meta( 'price_id' );
if ( (int) $item['item_number']['options']['price_id'] !== (int) $log_price_id ) {
continue;
}
}
$amount = isset( $item['price'] ) ? $item['price'] : $item['item_price'];
break;
}
}
$logs_data[] = array(
'ID' => $log->get_id(),
'payment_id' => $payment->ID,
'customer' => $customer,
'download' => $log->get_object_id(),
'price_id' => isset( $log_price_id ) ? $log_price_id : null,
'item_price' => isset( $item['item_price'] ) ? $item['item_price'] : $item['price'],
'amount' => $amount,
'date' => $payment->date,
'quantity' => $item['quantity'],
// Keep track of the currency. Vital to produce the correct report
'currency' => $payment->currency,
);
}
}
}
}
return $logs_data;
}
/**
* Setup the final data for the table.
*
* @since 1.4
*/
public function prepare_items() {
/** @var EDD_Logging $edd_logs */
global $edd_logs;
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
$this->_column_headers = array( $columns, $hidden, $sortable );
$this->items = $this->get_logs();
$total_items = $edd_logs->get_log_count( $this->get_filtered_download(), 'sale', $this->get_meta_query() );
$this->set_pagination_args( array(
'total_items' => $total_items,
'per_page' => $this->per_page,
'total_pages' => ceil( $total_items / $this->per_page ),
)
);
}
/**
* Since our "bulk actions" are navigational, we want them to always show, not just when there's items
*
* @since 2.5
* @return bool
*/
public function has_items() {
return true;
}
}