1 <?php
2 3 4 5 6 7 8 9 10 11 12
13
14
15 if ( ! defined( 'ABSPATH' ) ) exit;
16
17 18 19 20 21
22 class EDD_Customers_Export extends EDD_Export {
23 24 25 26 27 28
29 public $export_type = 'customers';
30
31 32 33 34 35 36 37
38 public function () {
39 ignore_user_abort( true );
40
41 if ( ! edd_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) )
42 set_time_limit( 0 );
43
44 $extra = '';
45
46 if ( ! empty( $_POST['edd_export_download'] ) ) {
47 $extra = sanitize_title( get_the_title( absint( $_POST['edd_export_download'] ) ) ) . '-';
48 }
49
50 nocache_headers();
51 header( 'Content-Type: text/csv; charset=utf-8' );
52 header( 'Content-Disposition: attachment; filename=edd-export-' . $extra . $this->export_type . '-' . date( 'm-d-Y' ) . '.csv' );
53 header( "Expires: 0" );
54 }
55
56 57 58 59 60 61 62
63 public function csv_cols() {
64 if ( ! empty( $_POST['edd_export_download'] ) ) {
65 $cols = array(
66 'name' => __( 'Name', 'edd' ),
67 'email' => __( 'Email', 'edd' ),
68 'date' => __( 'Date Purchased', 'edd' )
69 );
70 } else {
71 $cols = array(
72 'name' => __( 'Name', 'edd' ),
73 'email' => __( 'Email', 'edd' ),
74 'purchases' => __( 'Total Purchases', 'edd' ),
75 'amount' => __( 'Total Purchased', 'edd' )
76 );
77 }
78
79 return $cols;
80 }
81
82 83 84 85 86 87 88 89 90 91
92 public function get_data() {
93 global $wpdb;
94
95 $data = array();
96
97 if ( ! empty( $_POST['edd_export_download'] ) ) {
98
99 global $edd_logs;
100
101 $args = array(
102 'post_parent' => absint( $_POST['edd_export_download'] ),
103 'log_type' => 'sale',
104 'no_paging' => true
105 );
106
107 $logs = $edd_logs->get_connected_logs( $args );
108
109 if ( $logs ) {
110 foreach ( $logs as $log ) {
111 $payment_id = get_post_meta( $log->ID, '_edd_log_payment_id', true );
112 $email = edd_get_payment_user_email( $payment_id );
113
114 $wp_user = get_user_by( 'email', $email );
115
116 $data[] = array(
117 'name' => $wp_user ? $wp_user->display_name : __( 'Guest', 'edd' ),
118 'email' => $email,
119 'date' => $log->post_date
120 );
121 }
122 }
123 } else {
124
125 $emails = $wpdb->get_col( "SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = '_edd_payment_user_email' " );
126
127 foreach ( $emails as $email ) {
128 $wp_user = get_user_by( 'email', $email );
129
130 $data[] = array(
131 'name' => $wp_user ? $wp_user->display_name : __( 'Guest', 'edd' ),
132 'email' => $email,
133 'purchases' => edd_count_purchases_of_customer( $email ),
134 'amount' => html_entity_decode( edd_currency_filter( edd_format_amount( edd_purchase_total_of_user( $email ) ) ) )
135 );
136 }
137 }
138
139 $data = apply_filters( 'edd_export_get_data', $data );
140 $data = apply_filters( 'edd_export_get_data_' . $this->export_type, $data );
141
142 return $data;
143 }
144 }