1 <?php
2 3 4 5 6 7 8 9
10
11
12 if ( ! defined( 'ABSPATH' ) ) exit;
13
14
15 if ( ! class_exists( 'WP_List_Table' ) ) {
16 require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
17 }
18
19 20 21 22 23 24 25
26 class EDD_Sales_Log_Table extends WP_List_Table {
27 28 29 30 31 32
33 public $per_page = 30;
34
35 36 37 38 39 40 41 42
43 public function __construct() {
44 global $status, $page;
45
46
47 parent::__construct( array(
48 'singular' => edd_get_label_singular(),
49 'plural' => edd_get_label_plural(),
50 'ajax' => false
51 ) );
52
53 add_action( 'edd_log_view_actions', array( $this, 'downloads_filter' ) );
54 }
55
56 57 58 59 60 61 62 63 64 65 66
67 public function column_default( $item, $column_name ) {
68 switch ( $column_name ){
69 case 'download' :
70 return '<a href="' . add_query_arg( 'download', $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=download&page=edd-payment-history&user=' . urlencode( $item['user_id'] ) ) .
75 '">' . $item[ 'user_name' ] . '</a>';
76
77 case 'amount' :
78 return edd_currency_filter( edd_format_amount( $item['amount'] ) );
79
80 default:
81 return $item[ $column_name ];
82 }
83 }
84
85 86 87 88 89 90 91
92 public function get_columns() {
93 $columns = array(
94 'ID' => __( 'Log ID', 'edd' ),
95 'user_id' => __( 'User', 'edd' ),
96 'download' => __( 'Download', 'edd' ),
97 'amount' => __( 'Item Amount', 'edd' ),
98 'payment_id'=> __( 'Payment ID', 'edd' ),
99 'date' => __( 'Date', 'edd' )
100 );
101
102 return $columns;
103 }
104
105 106 107 108 109 110 111
112 public function get_paged() {
113 return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
114 }
115
116 117 118 119 120 121 122
123 public function get_filtered_user() {
124 return isset( $_GET['user'] ) ? absint( $_GET['user'] ) : false;
125 }
126
127 128 129 130 131 132 133
134 public function get_filtered_download() {
135 return ! empty( $_GET['download'] ) ? absint( $_GET['download'] ) : false;
136 }
137
138 139 140 141 142 143 144
145 public function get_search() {
146 return ! empty( $_GET['s'] ) ? urldecode( trim( $_GET['s'] ) ) : false;
147 }
148
149 150 151 152 153 154 155 156 157
158 public function get_meta_query() {
159 $user = $this->get_filtered_user();
160
161 $meta_query = array();
162
163 if( $user ) {
164
165 $meta_query[] = array(
166 'key' => '_edd_log_user_id',
167 'value' => $user
168 );
169 }
170
171 $search = $this->get_search();
172 if ( $search ) {
173 if ( is_email( $search ) ) {
174
175 $key = '_edd_log_user_info';
176 $compare = 'LIKE';
177 } else {
178
179 $key = '_edd_log_user_id';
180 $compare = 'LIKE';
181
182 if ( ! is_numeric( $search ) ) {
183
184 $user = get_user_by( 'login', $search );
185
186 if ( $user ) {
187
188 $search = $user->ID;
189 } else {
190
191 $users = new WP_User_Query( array(
192 'search' => $search,
193 'search_columns' => array( 'user_url', 'user_nicename' ),
194 'number' => 1,
195 'fields' => 'ids'
196 ) );
197
198 $found_user = $users->get_results();
199
200 if ( $found_user ) {
201 $search = $found_user[0];
202 }
203 }
204 }
205 }
206
207 if ( ! $this->file_search ) {
208
209 $meta_query[] = array(
210 'key' => $key,
211 'value' => $search,
212 'compare' => $compare
213 );
214
215 }
216 }
217
218 return $meta_query;
219 }
220
221 222 223 224 225 226 227
228 function bulk_actions() {
229
230 edd_log_views();
231 }
232
233 234 235 236 237 238 239
240 public function downloads_filter() {
241 $downloads = get_posts( array(
242 'post_type' => 'download',
243 'post_status' => 'any',
244 'posts_per_page' => -1,
245 'orderby' => 'title',
246 'order' => 'ASC',
247 'fields' => 'ids',
248 'update_post_meta_cache' => false,
249 'update_post_term_cache' => false
250 ) );
251
252 if ( $downloads ) {
253 echo '<select name="download" id="edd-log-download-filter">';
254 echo '<option value="0">' . __( 'All', 'edd' ) . '</option>';
255 foreach ( $downloads as $download ) {
256 echo '<option value="' . $download . '"' . selected( $download, $this->get_filtered_download() ) . '>' . esc_html( get_the_title( $download ) ) . '</option>';
257 }
258 echo '</select>';
259 }
260 }
261
262 263 264 265 266 267 268 269
270 public function get_logs() {
271 global $edd_logs;
272
273 $logs_data = array();
274
275 $paged = $this->get_paged();
276 $download = empty( $_GET['s'] ) ? $this->get_filtered_download() : null;
277 $user = $this-> get_filtered_user();
278
279 $log_query = array(
280 'post_parent' => $download,
281 'log_type' => 'sale',
282 'paged' => $paged,
283 'meta_query' => $this->get_meta_query()
284 );
285
286 $logs = $edd_logs->get_connected_logs( $log_query );
287
288 if ( $logs ) {
289 foreach ( $logs as $log ) {
290 $payment_id = get_post_meta( $log->ID, '_edd_log_payment_id', true );
291
292
293 if ( get_post( $payment_id ) ) :
294 $user_info = edd_get_payment_meta_user_info( $payment_id );
295 $cart_items = edd_get_payment_meta_cart_details( $payment_id );
296 $amount = 0;
297 if ( is_array( $cart_items ) && is_array( $user_info ) ) {
298 foreach ( $cart_items as $item ) {
299 $price_override = isset( $item['price'] ) ? $item['price'] : null;
300 if ( isset( $item['id'] ) && $item['id'] == $log->post_parent ) {
301 $amount = edd_get_download_final_price( $item['id'], $user_info, $price_override );
302 }
303 }
304
305 $logs_data[] = array(
306 'ID' => $log->ID,
307 'payment_id'=> $payment_id,
308 'download' => $log->post_parent,
309 'amount' => $amount,
310 'user_id' => $user_info['id'],
311 'user_name' => $user_info['first_name'] . ' ' . $user_info['last_name'],
312 'date' => get_post_field( 'post_date', $payment_id )
313 );
314 }
315 endif;
316 }
317 }
318
319 return $logs_data;
320 }
321
322 323 324 325 326 327 328 329 330 331 332 333 334
335 public function prepare_items() {
336 global $edd_logs;
337
338 $columns = $this->get_columns();
339 $hidden = array();
340 $sortable = $this->get_sortable_columns();
341 $this->_column_headers = array( $columns, $hidden, $sortable );
342 $current_page = $this->get_pagenum();
343 $this->items = $this->get_logs();
344 $total_items = $edd_logs->get_log_count( $this->get_filtered_download(), 'sale', $this->get_meta_query() );
345
346 $this->set_pagination_args( array(
347 'total_items' => $total_items,
348 'per_page' => $this->per_page,
349 'total_pages' => ceil( $total_items / $this->per_page )
350 )
351 );
352 }
353 }