Easy Digital Downloads
  • Package
  • Class
  • Tree

Packages

  • EDD
    • Admin
      • Actions
      • Add-ons
      • Dashboard
      • Discounts
      • Downloads
      • Export
      • Notices
      • Pages
      • Payments
      • Reports
      • Settings
      • System
      • Upgrades
      • Upload
      • Welcome
    • Cart
    • Checkout
    • Classes
      • API
      • Fees
      • HTML
      • Roles
      • Session
    • Emails
    • Functions
      • AJAX
      • Compatibility
      • Errors
      • Formatting
      • Install
      • Login
      • Taxes
      • Templates
    • Gateways
    • Logging
    • Payments
    • Shortcodes
    • Widgets

Classes

  • EDD_API_Request_Log_Table
  • EDD_Customer_Reports_Table
  • EDD_Customers_Export
  • EDD_Download_History_Export
  • EDD_Download_Reports_Table
  • EDD_Export
  • EDD_File_Downloads_Log_Table
  • EDD_Gateway_Error_Log_Table
  • EDD_Payments_Export
  • EDD_Sales_Log_Table

Functions

  • edd_draw_chart_image
  • edd_estimated_monthly_stats
  • edd_generate_pdf
  • edd_get_report_dates
  • edd_log_default_views
  • edd_log_views
  • edd_logs_view_api_requests
  • edd_logs_view_file_downloads
  • edd_logs_view_gateway_errors
  • edd_logs_view_sales
  • edd_parse_report_dates
  • edd_report_views
  • edd_reporting_contextual_help
  • edd_reports_customers_table
  • edd_reports_default_views
  • edd_reports_downloads_table
  • edd_reports_earnings
  • edd_reports_graph
  • edd_reports_graph_controls
  • edd_reports_page
  • edd_reports_tab_export
  • edd_reports_tab_logs
  • edd_reports_tab_reports
  • edd_reports_taxes
  1 <?php
  2 /**
  3  * File Downloads Log View Class
  4  *
  5  * @package     EDD
  6  * @subpackage  Admin/Reports
  7  * @copyright   Copyright (c) 2013, Pippin Williamson
  8  * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
  9  * @since       1.4.4
 10  */
 11 
 12 // Exit if accessed directly
 13 if ( ! defined( 'ABSPATH' ) ) exit;
 14 
 15 // Load WP_List_Table if not loaded
 16 if ( ! class_exists( 'WP_List_Table' ) ) {
 17     require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
 18 }
 19 
 20 /**
 21  * EDD_File_Downloads_Log_Table Class
 22  *
 23  * Renders the file downloads log view
 24  *
 25  * @since 1.4
 26  */
 27 class EDD_File_Downloads_Log_Table extends WP_List_Table {
 28     /**
 29      * Number of items per page
 30      *
 31      * @var int
 32      * @since 1.4
 33      */
 34     public $per_page = 30;
 35 
 36     /**
 37      * Are we searching for files?
 38      *
 39      * @var bool
 40      * @since 1.4
 41      */
 42     public $file_search = false;
 43 
 44     /**
 45      * Get things started
 46      *
 47      * @access public
 48      * @since 1.4
 49      * @see WP_List_Table::__construct()
 50      * @return void
 51      */
 52     public function __construct() {
 53         global $status, $page;
 54 
 55         // Set parent defaults
 56         parent::__construct( array(
 57             'singular'  => edd_get_label_singular(),    // Singular name of the listed records
 58             'plural'    => edd_get_label_plural(),      // Plural name of the listed records
 59             'ajax'      => false                        // Does this table support ajax?
 60         ) );
 61 
 62         add_action( 'edd_log_view_actions', array( $this, 'downloads_filter' ) );
 63     }
 64 
 65     /**
 66      * Show the search field
 67      *
 68      * @since 1.4
 69      * @access public
 70      *
 71      * @param string $text Label for the search box
 72      * @param string $input_id ID of the search box
 73      *
 74      * @return void
 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      * This function renders most of the columns in the list table.
 94      *
 95      * @access public
 96      * @since 1.4
 97      *
 98      * @param array $item Contains all the data of the discount code
 99      * @param string $column_name The name of the column
100      *
101      * @return string Column Name
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      * Retrieve the table columns
116      *
117      * @access public
118      * @since 1.4
119      * @return array $columns Array of all the list table columns
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      * Retrieves the user we are filtering logs by, if any
136      *
137      * @access public
138      * @since 1.4
139      * @return mixed int If User ID, string If Email/Login
140      */
141     public function get_filtered_user() {
142         return isset( $_GET['user'] ) ? absint( $_GET['user'] ) : false;
143     }
144 
145     /**
146      * Retrieves the ID of the download we're filtering logs by
147      *
148      * @access public
149      * @since 1.4
150      * @return int Download ID
151      */
152     public function get_filtered_download() {
153         return ! empty( $_GET['download'] ) ? absint( $_GET['download'] ) : false;
154     }
155 
156     /**
157      * Retrieves the search query string
158      *
159      * @access public
160      * @since 1.4
161      * @return mixed string If search is present, false otherwise
162      */
163     public function get_search() {
164         return ! empty( $_GET['s'] ) ? urldecode( trim( $_GET['s'] ) ) : false;
165     }
166 
167     /**
168      * Gets the meta query for the log query
169      *
170      * This is used to return log entries that match our search query, user query, or download query
171      *
172      * @access public
173      * @since 1.4
174      * @return array $meta_query
175      */
176     public function get_meta_query() {
177         $user = $this->get_filtered_user();
178 
179         $meta_query = array();
180 
181         if ( $user ) {
182             // Show only logs from a specific user
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                 // This is an IP address search
194                 $key     = '_edd_log_ip';
195                 $compare = '=';
196             } else if ( is_email( $search ) ) {
197                 // This is an email search. We use this to ensure it works for guest users and logged-in users
198                 $key     = '_edd_log_user_info';
199                 $compare = 'LIKE';
200             } else {
201                 // Look for a user
202                 $key = '_edd_log_user_id';
203                 $compare = 'LIKE';
204 
205                 if ( ! is_numeric( $search ) ) {
206                     // Searching for user by username
207                     $user = get_user_by( 'login', $search );
208 
209                     if ( $user ) {
210                         // Found one, set meta value to user's ID
211                         $search = $user->ID;
212                     } else {
213                         // No user found so let's do a real search query
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                             // No users were found so let's look for file names instead
227                             $this->file_search = true;
228                         }
229                     }
230                 }
231             }
232 
233             if ( ! $this->file_search ) {
234                 // Meta query only works for non file name searche
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      * Retrieve the current page number
248      *
249      * @access public
250      * @since 1.4
251      * @return int Current page number
252      */
253     function get_paged() {
254         return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
255     }
256 
257     /**
258      * Outputs the log views
259      *
260      * @access public
261      * @since 1.4
262      * @return void
263      */
264     public function bulk_actions() {
265         // These aren't really bulk actions but this outputs the markup in the right place
266         edd_log_views();
267     }
268 
269     /**
270      * Sets up the downloads filter
271      *
272      * @access public
273      * @since 1.4
274      * @return void
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      * Gets the log entries for the current view
300      *
301      * @access public
302      * @since 1.4
303      * @global object $edd_logs EDD Logs Object
304      * @return array $logs_data Array of all the Log entires
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      * Setup the final data for the table
355      *
356      * @access public
357      * @since 1.5
358      * @global object $edd_logs EDD Logs Object
359      * @uses EDD_File_Downloads_Log_Table::get_columns()
360      * @uses WP_List_Table::get_sortable_columns()
361      * @uses EDD_File_Downloads_Log_Table::get_pagenum()
362      * @uses EDD_File_Downloads_Log_Table::get_logs()
363      * @uses EDD_File_Downloads_Log_Table::get_log_count()
364      * @uses WP_List_Table::set_pagination_args()
365      * @return void
366      */
367     function prepare_items() {
368         global $edd_logs;
369 
370         $columns               = $this->get_columns();
371         $hidden                = array(); // No hidden columns
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 }
Easy Digital Downloads API documentation generated by ApiGen 2.8.0