/** * 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) { $customers = array(); $error = array(); if (!user_can($this->user_id, 'view_shop_sensitive_data') && !$this->override) { return $customers; } global $wpdb; $paged = $this->get_paged(); $per_page = $this->per_page(); $offset = $per_page * ($paged - 1); if (is_numeric($customer)) { $field = 'id'; } else { $field = 'email'; } $customer_query = EDD()->customers->get_customers(array('number' => $per_page, 'offset' => $offset, $field => $customer)); $customer_count = 0; if ($customer_query) { foreach ($customer_query as $customer_obj) { $names = explode(' ', $customer_obj->name); $first_name = !empty($names[0]) ? $names[0] : ''; $last_name = ''; if (!empty($names[1])) { unset($names[0]); $last_name = implode(' ', $names); } $customers['customers'][$customer_count]['info']['id'] = ''; $customers['customers'][$customer_count]['info']['user_id'] = ''; $customers['customers'][$customer_count]['info']['username'] = ''; $customers['customers'][$customer_count]['info']['display_name'] = ''; $customers['customers'][$customer_count]['info']['customer_id'] = $customer_obj->id; $customers['customers'][$customer_count]['info']['first_name'] = $first_name; $customers['customers'][$customer_count]['info']['last_name'] = $last_name; $customers['customers'][$customer_count]['info']['email'] = $customer_obj->email; if (!empty($customer_obj->user_id) && $customer_obj->user_id > 0) { $user_data = get_userdata($customer_obj->user_id); // Customer with registered account // id is going to get deprecated in the future, user user_id or customer_id instead $customers['customers'][$customer_count]['info']['id'] = $customer_obj->user_id; $customers['customers'][$customer_count]['info']['user_id'] = $customer_obj->user_id; $customers['customers'][$customer_count]['info']['username'] = $user_data->user_login; $customers['customers'][$customer_count]['info']['display_name'] = $user_data->display_name; } $customers['customers'][$customer_count]['stats']['total_purchases'] = $customer_obj->purchase_count; $customers['customers'][$customer_count]['stats']['total_spent'] = $customer_obj->purchase_value; $customers['customers'][$customer_count]['stats']['total_downloads'] = edd_count_file_downloads_of_user($customer_obj->email); $customer_count++; } } elseif ($customer) { $error['error'] = sprintf(__('Customer %s not found!', 'easy-digital-downloads'), $customer); return $error; } else { $error['error'] = __('No customers found!', 'easy-digital-downloads'); return $error; } return $customers; }
/** * 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; }
/** * Process Get Customers API Request * * @access public * @since 2.6 * @global object $wpdb Used to query the database using the WordPress Database API * @param array $args Array of arguments for filters customers * @return array $customers Multidimensional array of the customers */ public function get_customers($args = array()) { global $wpdb; $paged = $this->get_paged(); $per_page = $this->per_page(); $offset = $per_page * ($paged - 1); $defaults = array('customer' => null, 'date' => null, 'startdate' => null, 'enddate' => null, 'number' => $per_page, 'offset' => $offset); $args = wp_parse_args($args, $defaults); $customers = array(); $error = array(); if (!user_can($this->user_id, 'view_shop_sensitive_data') && !$this->override) { return $customers; } if (is_numeric($args['customer'])) { $field = 'id'; } else { $field = 'email'; } $args[$field] = $args['customer']; $dates = $this->get_dates($args); if ($args['date'] === 'range') { // Ensure the end date is later than the start date if (!empty($args['enddate']) && !empty($args['enddate']) && $args['enddate'] < $args['startdate']) { $error['error'] = __('The end date must be later than the start date!', 'easy-digital-downloads'); } $date_range = array(); if (!empty($args['startdate'])) { $date_range['start'] = $dates['year'] . sprintf('%02d', $dates['m_start']) . $dates['day_start']; } if (!empty($args['enddate'])) { $date_range['end'] = $dates['year_end'] . sprintf('%02d', $dates['m_end']) . $dates['day_end']; } $args['date'] = $date_range; } elseif (!empty($args['date'])) { if ($args['date'] == 'this_quarter' || $args['date'] == 'last_quarter') { $args['date'] = array('start' => $dates['year'] . sprintf('%02d', $dates['m_start']) . '01', 'end' => $dates['year'] . sprintf('%02d', $dates['m_end']) . cal_days_in_month(CAL_GREGORIAN, $dates['m_end'], $dates['year'])); } else { if ($args['date'] == 'this_month' || $args['date'] == 'last_month') { $args['date'] = array('start' => $dates['year'] . sprintf('%02d', $dates['m_start']) . '01', 'end' => $dates['year'] . sprintf('%02d', $dates['m_end']) . cal_days_in_month(CAL_GREGORIAN, $dates['m_end'], $dates['year'])); } else { if ($args['date'] == 'this_year' || $args['date'] == 'last_year') { $args['date'] = array('start' => $dates['year'] . '0101', 'end' => $dates['year'] . '1231'); } else { $args['date'] = $dates['year'] . sprintf('%02d', $dates['m_start']) . $dates['day']; } } } } unset($args['startdate'], $args['enddate']); $customer_query = EDD()->customers->get_customers($args); $customer_count = 0; if ($customer_query) { foreach ($customer_query as $customer_obj) { // Setup a new EDD_Customer object so additional details are defined (like additional emails) $customer_obj = new EDD_Customer($customer_obj->id); $names = explode(' ', $customer_obj->name); $first_name = !empty($names[0]) ? $names[0] : ''; $last_name = ''; if (!empty($names[1])) { unset($names[0]); $last_name = implode(' ', $names); } $customers['customers'][$customer_count]['info']['customer_id'] = $customer_obj->id; $customers['customers'][$customer_count]['info']['user_id'] = 0; $customers['customers'][$customer_count]['info']['username'] = ''; $customers['customers'][$customer_count]['info']['display_name'] = ''; $customers['customers'][$customer_count]['info']['first_name'] = $first_name; $customers['customers'][$customer_count]['info']['last_name'] = $last_name; $customers['customers'][$customer_count]['info']['email'] = $customer_obj->email; $customers['customers'][$customer_count]['info']['additional_emails'] = null; $customers['customers'][$customer_count]['info']['date_created'] = $customer_obj->date_created; if (!empty($customer_obj->emails) && count($customer_obj->emails) > 1) { $additional_emails = $customer_obj->emails; $primary_email_key = array_search($customer_obj->email, $customer_obj->emails); if (false !== $primary_email_key) { unset($additional_emails[$primary_email_key]); } $customers['customers'][$customer_count]['info']['additional_emails'] = $additional_emails; } if (!empty($customer_obj->user_id) && $customer_obj->user_id > 0) { $user_data = get_userdata($customer_obj->user_id); // Customer with registered account // id is going to get deprecated in the future, user user_id or customer_id instead $customers['customers'][$customer_count]['info']['user_id'] = $customer_obj->user_id; $customers['customers'][$customer_count]['info']['username'] = $user_data->user_login; $customers['customers'][$customer_count]['info']['display_name'] = $user_data->display_name; } $customers['customers'][$customer_count]['stats']['total_purchases'] = $customer_obj->purchase_count; $customers['customers'][$customer_count]['stats']['total_spent'] = $customer_obj->purchase_value; $customers['customers'][$customer_count]['stats']['total_downloads'] = edd_count_file_downloads_of_user($customer_obj->email); $customer_count++; } } elseif ($args['customer']) { $error['error'] = sprintf(__('Customer %s not found!', 'easy-digital-downloads'), $customer); return $error; } else { $error['error'] = __('No customers found!', 'easy-digital-downloads'); return $error; } return apply_filters('edd_api_customers', $customers, $this); }
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; }