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