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  * Sales 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  */
 10 
 11 // Exit if accessed directly
 12 if ( ! defined( 'ABSPATH' ) ) exit;
 13 
 14 // Load WP_List_Table if not loaded
 15 if ( ! class_exists( 'WP_List_Table' ) ) {
 16     require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
 17 }
 18 
 19 /**
 20  * EDD_Sales_Log_Table Class
 21  *
 22  * Renders the sales log list table
 23  *
 24  * @since 1.4
 25  */
 26 class EDD_Sales_Log_Table extends WP_List_Table {
 27     /**
 28      * Number of results to show per page
 29      *
 30      * @since 1.4
 31      * @var int
 32      */
 33     public $per_page = 30;
 34 
 35     /**
 36      * Get things started
 37      *
 38      * @access public
 39      * @since 1.4
 40      * @see WP_List_Table::__construct()
 41      * @return void
 42      */
 43     public function __construct() {
 44         global $status, $page;
 45 
 46         // Set parent defaults
 47         parent::__construct( array(
 48             'singular'  => edd_get_label_singular(),    // Singular name of the listed records
 49             'plural'    => edd_get_label_plural(),      // Plural name of the listed records
 50             'ajax'      => false                        // Does this table support ajax?
 51         ) );
 52 
 53         add_action( 'edd_log_view_actions', array( $this, 'downloads_filter' ) );
 54     }
 55 
 56     /**
 57      * This function renders most of the columns in the list table.
 58      *
 59      * @access public
 60      * @since 1.4
 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         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      * Retrieve the table columns
 87      *
 88      * @access public
 89      * @since 1.4
 90      * @return array $columns Array of all the list table columns
 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      * Retrieve the current page number
107      *
108      * @access public
109      * @since 1.4
110      * @return int Current page number
111      */
112     public function get_paged() {
113         return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
114     }
115 
116     /**
117      * Retrieves the user we are filtering logs by, if any
118      *
119      * @access public
120      * @since 1.4
121      * @return mixed int If User ID, string If Email/Login
122      */
123     public function get_filtered_user() {
124         return isset( $_GET['user'] ) ? absint( $_GET['user'] ) : false;
125     }
126 
127     /**
128      * Retrieves the ID of the download we're filtering logs by
129      *
130      * @access public
131      * @since 1.4
132      * @return int Download ID
133      */
134     public function get_filtered_download() {
135         return ! empty( $_GET['download'] ) ? absint( $_GET['download'] ) : false;
136     }
137 
138     /**
139      * Retrieves the search query string
140      *
141      * @access public
142      * @since 1.4
143      * @return mixed string If search is present, false otherwise
144      */
145     public function get_search() {
146         return ! empty( $_GET['s'] ) ? urldecode( trim( $_GET['s'] ) ) : false;
147     }
148 
149     /**
150      * Gets the meta query for the log query
151      *
152      * This is used to return log entries that match our search query, user query, or download query
153      *
154      * @access public
155      * @since 1.4
156      * @return array $meta_query
157      */
158     public function get_meta_query() {
159         $user = $this->get_filtered_user();
160 
161         $meta_query = array();
162 
163         if( $user ) {
164             // Show only logs from a specific user
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                 // This is an email search. We use this to ensure it works for guest users and logged-in users
175                 $key     = '_edd_log_user_info';
176                 $compare = 'LIKE';
177             } else {
178                 // Look for a user
179                 $key = '_edd_log_user_id';
180                 $compare = 'LIKE';
181 
182                 if ( ! is_numeric( $search ) ) {
183                     // Searching for user by username
184                     $user = get_user_by( 'login', $search );
185 
186                     if ( $user ) {
187                         // Found one, set meta value to user's ID
188                         $search = $user->ID;
189                     } else {
190                         // No user found so let's do a real search query
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                 // Meta query only works for non file name searche
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      * Outputs the log views
223      *
224      * @access public
225      * @since 1.4
226      * @return void
227      */
228     function bulk_actions() {
229         // These aren't really bulk actions but this outputs the markup in the right place
230         edd_log_views();
231     }
232 
233     /**
234      * Sets up the downloads filter
235      *
236      * @access public
237      * @since 1.4
238      * @return void
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      * Gets the log entries for the current view
264      *
265      * @access public
266      * @since 1.4
267      * @global object $edd_logs EDD Logs Object
268      * @return array $logs_data Array of all the Log entires
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                 // Make sure this payment hasn't been deleted
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      * Setup the final data for the table
324      *
325      * @access public
326      * @since 1.4
327      * @global object $edd_logs EDD Logs Object
328      * @uses EDD_Sales_Log_Table::get_columns()
329      * @uses WP_List_Table::get_sortable_columns()
330      * @uses EDD_Sales_Log_Table::get_pagenum()
331      * @uses EDD_Sales_Log_Table::get_logs()
332      * @uses EDD_Sales_Log_Table::get_log_count()
333      * @return void
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 }
Easy Digital Downloads API documentation generated by ApiGen 2.8.0