function reports_data()
 {
     global $wpdb;
     $reports_data = array();
     $paged = $this->get_paged();
     $offset = $this->per_page * ($paged - 1);
     $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}");
     if ($customers) {
         foreach ($customers as $customer_email) {
             $wp_user = get_user_by('email', $customer_email);
             $user_id = $wp_user ? $wp_user->ID : 0;
             $reports_data[] = array('ID' => $user_id, 'name' => $wp_user ? $wp_user->display_name : __('Guest', 'edd'), 'email' => $customer_email, 'num_purchases' => edd_count_purchases_of_customer($customer_email), 'amount_spent' => edd_purchase_total_of_user($customer_email), 'file_downloads' => edd_count_file_downloads_of_user(!empty($user_id) ? $user_id : $customer_email));
         }
     }
     return $reports_data;
 }
 /**
  * Process Get Customers API Request
  *
  * @access public
  * @since 1.5
  * @author Daniel J Griffiths
  * @global object $wpdb Used to query the database using the WordPress
  *   Database API
  * @param int $customer Customer ID
  * @return array $customers Multidimensional array of the customers
  */
 public function get_customers($customer = null)
 {
     if ($customer == null) {
         global $wpdb;
         $paged = $this->get_paged();
         $per_page = $this->per_page();
         $offset = $per_page * ($paged - 1);
         $customer_list_query = $wpdb->get_col("SELECT DISTINCT meta_value FROM {$wpdb->postmeta} where meta_key = '_edd_payment_user_email' ORDER BY meta_id DESC LIMIT {$per_page} OFFSET {$offset}");
         $customer_count = 0;
         foreach ($customer_list_query as $customer_email) {
             $customer_info = get_user_by('email', $customer_email);
             if ($customer_info) {
                 // Customer with registered account
                 $customers['customers'][$customer_count]['info']['id'] = $customer_info->ID;
                 $customers['customers'][$customer_count]['info']['username'] = $customer_info->user_login;
                 $customers['customers'][$customer_count]['info']['display_name'] = $customer_info->display_name;
                 $customers['customers'][$customer_count]['info']['first_name'] = $customer_info->user_firstname;
                 $customers['customers'][$customer_count]['info']['last_name'] = $customer_info->user_lastname;
                 $customers['customers'][$customer_count]['info']['email'] = $customer_info->user_email;
             } else {
                 // Guest customer
                 $customers['customers'][$customer_count]['info']['id'] = -1;
                 $customers['customers'][$customer_count]['info']['username'] = __('Guest', 'edd');
                 $customers['customers'][$customer_count]['info']['display_name'] = __('Guest', 'edd');
                 $customers['customers'][$customer_count]['info']['first_name'] = __('Guest', 'edd');
                 $customers['customers'][$customer_count]['info']['last_name'] = __('Guest', 'edd');
                 $customers['customers'][$customer_count]['info']['email'] = $customer_email;
             }
             $customers['customers'][$customer_count]['stats']['total_purchases'] = edd_count_purchases_of_customer($customer_email);
             $customers['customers'][$customer_count]['stats']['total_spent'] = edd_purchase_total_of_user($customer_email);
             $customers['customers'][$customer_count]['stats']['total_downloads'] = edd_count_file_downloads_of_user($customer_email);
             $customer_count++;
         }
     } else {
         if (is_numeric($customer)) {
             $customer_info = get_userdata($customer);
         } else {
             $customer_info = get_user_by('email', $customer);
         }
         if ($customer_info && edd_has_purchases($customer_info->ID)) {
             $customers['customers'][0]['info']['id'] = $customer_info->ID;
             $customers['customers'][0]['info']['username'] = $customer_info->user_login;
             $customers['customers'][0]['info']['display_name'] = $customer_info->display_name;
             $customers['customers'][0]['info']['first_name'] = $customer_info->user_firstname;
             $customers['customers'][0]['info']['last_name'] = $customer_info->user_lastname;
             $customers['customers'][0]['info']['email'] = $customer_info->user_email;
             $customers['customers'][0]['stats']['total_purchases'] = edd_count_purchases_of_customer($customer);
             $customers['customers'][0]['stats']['total_spent'] = edd_purchase_total_of_user($customer);
             $customers['customers'][0]['stats']['total_downloads'] = edd_count_file_downloads_of_user($customer);
         } else {
             $error['error'] = sprintf(__('Customer %s not found!', 'edd'), $customer);
             return $error;
         }
     }
     return $customers;
 }
/**
 * Export all customers to CSV
 *
 * Using wpdb directly for performance reasons (workaround of calling all posts and fetch data respectively)
 *
 * @access      private
 * @since       1.2
 * @return      void
 */
function edd_export_all_customers()
{
    if (current_user_can('administrator')) {
        global $wpdb;
        ignore_user_abort(true);
        if (!edd_is_func_disabled('set_time_limit') && !ini_get('safe_mode')) {
            set_time_limit(0);
        }
        $emails = $wpdb->get_col("SELECT DISTINCT meta_value FROM {$wpdb->postmeta} WHERE meta_key = '_edd_payment_user_email' ");
        if (!empty($emails)) {
            header("Content-type: text/csv");
            $today = date("Y-m-d");
            header("Content-Disposition: attachment; filename=customers-{$today}.csv");
            header("Pragma: no-cache");
            header("Expires: 0");
            echo '"' . __('Email', 'edd') . '",';
            echo '"' . __('Name', 'edd') . '",';
            echo '"' . __('Total Purchases', 'edd') . '",';
            echo '"' . __('Total Purchased', 'edd') . '"';
            echo "\r\n";
            foreach ($emails as $email) {
                $wp_user = get_user_by('email', $email);
                echo $email . ',';
                echo $wp_user ? $wp_user->display_name : __('Guest', 'edd');
                echo ',';
                echo edd_count_purchases_of_customer($email) . ',';
                echo html_entity_decode(edd_currency_filter(edd_format_amount(edd_purchase_total_of_user($email))));
                echo "\n";
            }
            exit;
        }
    } else {
        wp_die(__('Export not allowed for non-administrators.', 'edd'));
    }
}