1 <?php
2 3 4 5 6 7 8 9 10
11
12
13 if ( ! defined( 'ABSPATH' ) ) exit;
14
15
16 if ( ! class_exists( 'WP_List_Table' ) ) {
17 require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
18 }
19
20 21 22 23 24 25 26
27 class EDD_Customer_Reports_Table extends WP_List_Table {
28 29 30 31 32 33
34 public $per_page = 30;
35
36 37 38 39 40 41 42 43
44 public function __construct() {
45 global $status, $page;
46
47
48 parent::__construct( array(
49 'singular' => __( 'Customer', 'edd' ),
50 'plural' => __( 'Customers', 'edd' ),
51 'ajax' => false
52 ) );
53
54 }
55
56 57 58 59 60 61 62 63 64 65 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 88 89 90 91 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 107 108 109 110 111
112 public function bulk_actions() {
113
114 edd_report_views();
115 }
116
117 118 119 120 121 122 123
124 public function get_paged() {
125 return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
126 }
127
128 129 130 131 132 133 134 135 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 145 146 147 148 149 150 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 182 183 184 185 186 187 188 189 190
191 public function prepare_items() {
192 $columns = $this->get_columns();
193
194 $hidden = array();
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
205
206 $this->items = $this->reports_data();
207
208 $this->set_pagination_args( array(
209 'total_items' => $total_items,
210 'per_page' => $this->per_page,
211 'total_pages' => ceil( $total_items / $this->per_page )
212 ) );
213 }
214 }