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  * Customer Reports Table 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.5
 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_Customer_Reports_Table Class
 22  *
 23  * Renders the Customer Reports table
 24  *
 25  * @since 1.5
 26  */
 27 class EDD_Customer_Reports_Table extends WP_List_Table {
 28     /**
 29      * Number of items per page
 30      *
 31      * @var int
 32      * @since 1.5
 33      */
 34     public $per_page = 30;
 35 
 36     /**
 37      * Get things started
 38      *
 39      * @access public
 40      * @since 1.5
 41      * @see WP_List_Table::__construct()
 42      * @return void
 43      */
 44     public function __construct() {
 45         global $status, $page;
 46 
 47         // Set parent defaults
 48         parent::__construct( array(
 49             'singular'  => __( 'Customer', 'edd' ),     // Singular name of the listed records
 50             'plural'    => __( 'Customers', 'edd' ),    // Plural name of the listed records
 51             'ajax'      => false                        // Does this table support ajax?
 52         ) );
 53 
 54     }
 55 
 56     /**
 57      * This function renders most of the columns in the list table.
 58      *
 59      * @access public
 60      * @since 1.5
 61      *
 62      * @param array $item Contains all the data of the customers
 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 'name' :
 70                 return '<a href="' .
 71                         admin_url( '/edit.php?post_type=download&page=edd-payment-history&user=' . urlencode( $item['email'] )
 72                     ) . '">' . esc_html( $item[ $column_name ] ) . '</a>';
 73 
 74             case 'amount_spent' :
 75                 return edd_currency_filter( edd_format_amount( $item[ $column_name ] ) );
 76 
 77             case 'file_downloads' :
 78                     return '<a href="' . admin_url( '/edit.php?post_type=download&page=edd-reports&tab=logs&user=' . urlencode( ! empty( $item['ID'] ) ? $item['ID'] : $item['email'] ) ) . '" target="_blank">' . $item['file_downloads'] . '</a>';
 79 
 80             default:
 81                 $value = isset( $item[ $column_name ] ) ? $item[ $column_name ] : null;
 82                 return apply_filters( 'edd_report_column_' . $column_name, $value, $item['ID'] );
 83         }
 84     }
 85 
 86     /**
 87      * Retrieve the table columns
 88      *
 89      * @access public
 90      * @since 1.5
 91      * @return array $columns Array of all the list table columns
 92      */
 93     public function get_columns() {
 94         $columns = array(
 95             'name'          => __( 'Name', 'edd' ),
 96             'email'         => __( 'Email', 'edd' ),
 97             'num_purchases' => __( 'Purchases', 'edd' ),
 98             'amount_spent'  => __( 'Total Spent', 'edd' ),
 99             'file_downloads'=> __( 'Files Downloaded', 'edd' )
100         );
101 
102         return apply_filters( 'edd_report_customer_columns', $columns );
103     }
104 
105     /**
106      * Outputs the reporting views
107      *
108      * @access public
109      * @since 1.5
110      * @return void
111      */
112     public function bulk_actions() {
113         // These aren't really bulk actions but this outputs the markup in the right place
114         edd_report_views();
115     }
116 
117     /**
118      * Retrieve the current page number
119      *
120      * @access public
121      * @since 1.5
122      * @return int Current page number
123      */
124     public function get_paged() {
125         return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
126     }
127 
128     /**
129      * Retrieve the total customers from the database
130      *
131      * @access public
132      * @since 1.5
133      * @global object $wpdb Used to query the database using the WordPress
134      *   Database API
135      * @return int $count The number of customers from the database
136      */
137     public function get_total_customers() {
138         global $wpdb;
139         $count = $wpdb->get_col( "SELECT COUNT(DISTINCT meta_value) FROM $wpdb->postmeta WHERE meta_key = '_edd_payment_user_email'" );
140         return $count[0];
141     }
142 
143     /**
144      * Build all the reports data
145      *
146      * @access public
147      * @since 1.5
148       * @global object $wpdb Used to query the database using the WordPress
149      *   Database API
150      * @return array $reports_data All the data for customer reports
151      */
152     public function reports_data() {
153         global $wpdb;
154 
155         $reports_data = array();
156         $paged        = $this->get_paged();
157         $offset       = $this->per_page * ( $paged - 1 );
158         $customers    = $wpdb->get_col( "SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = '_edd_payment_user_email' ORDER BY meta_id DESC LIMIT $this->per_page OFFSET $offset" );
159 
160         if ( $customers ) {
161             foreach ( $customers as $customer_email ) {
162                 $wp_user = get_user_by( 'email', $customer_email );
163 
164                 $user_id = $wp_user ? $wp_user->ID : 0;
165 
166                 $reports_data[] = array(
167                     'ID'            => $user_id,
168                     'name'          => $wp_user ? $wp_user->display_name : __( 'Guest', 'edd' ),
169                     'email'         => $customer_email,
170                     'num_purchases' => edd_count_purchases_of_customer( $customer_email ),
171                     'amount_spent'  => edd_purchase_total_of_user( $customer_email ),
172                     'file_downloads'=> edd_count_file_downloads_of_user( ! empty( $user_id ) ? $user_id : $customer_email )
173                 );
174             }
175         }
176 
177         return $reports_data;
178     }
179 
180     /**
181      * Setup the final data for the table
182      *
183      * @access public
184      * @since 1.5
185      * @uses EDD_Customer_Reports_Table::get_columns()
186      * @uses WP_List_Table::get_sortable_columns()
187      * @uses EDD_Customer_Reports_Table::get_pagenum()
188      * @uses EDD_Customer_Reports_Table::get_total_customers()
189      * @return void
190      */
191     public function prepare_items() {
192         $columns = $this->get_columns();
193 
194         $hidden = array(); // No hidden columns
195 
196         $sortable = $this->get_sortable_columns();
197 
198         $this->_column_headers = array( $columns, $hidden, $sortable );
199 
200         $current_page = $this->get_pagenum();
201 
202         $total_items = $this->get_total_customers();
203 
204         //$data = array_slice( $data,( ( $current_page - 1 ) * $per_page ), $per_page );
205 
206         $this->items = $this->reports_data();
207 
208         $this->set_pagination_args( array(
209             'total_items' => $total_items,                      // WE have to calculate the total number of items
210             'per_page'    => $this->per_page,                       // WE have to determine how many items to show on a page
211             'total_pages' => ceil( $total_items / $this->per_page )   // WE have to calculate the total number of pages
212         ) );
213     }
214 }
Easy Digital Downloads API documentation generated by ApiGen 2.8.0