|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* Customer (Donor) Reports Table Class. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Give |
|
6
|
|
|
* @subpackage Admin/Reports |
|
7
|
|
|
* @copyright Copyright (c) 2016, WordImpress |
|
8
|
|
|
* @license https://opensource.org/licenses/gpl-license GNU Public License |
|
9
|
|
|
* @since 1.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// Exit if accessed directly. |
|
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
14
|
|
|
exit; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
// Load WP_List_Table if not loaded |
|
18
|
|
|
if ( ! class_exists( 'WP_List_Table' ) ) { |
|
19
|
|
|
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Give_Customer_Reports_Table Class. |
|
24
|
|
|
* |
|
25
|
|
|
* Renders the Customer Reports table. |
|
26
|
|
|
* |
|
27
|
|
|
* @since 1.0 |
|
28
|
|
|
*/ |
|
29
|
|
|
class Give_Customer_Reports_Table extends WP_List_Table { |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Number of items per page. |
|
33
|
|
|
* |
|
34
|
|
|
* @var int |
|
35
|
|
|
* @since 1.0 |
|
36
|
|
|
*/ |
|
37
|
|
|
public $per_page = 30; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Number of donors found. |
|
41
|
|
|
* |
|
42
|
|
|
* @var int |
|
43
|
|
|
* @since 1.0 |
|
44
|
|
|
*/ |
|
45
|
|
|
public $count = 0; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Total donors. |
|
49
|
|
|
* |
|
50
|
|
|
* @var int |
|
51
|
|
|
* @since 1.0 |
|
52
|
|
|
*/ |
|
53
|
|
|
public $total = 0; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get things started. |
|
57
|
|
|
* |
|
58
|
|
|
* @since 1.0 |
|
59
|
|
|
* @see WP_List_Table::__construct() |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __construct() { |
|
62
|
|
|
|
|
63
|
|
|
// Set parent defaults |
|
64
|
|
|
parent::__construct( array( |
|
65
|
|
|
'singular' => esc_html__( 'Donor', 'give' ), // Singular name of the listed records |
|
66
|
|
|
'plural' => esc_html__( 'Donors', 'give' ), // Plural name of the listed records |
|
67
|
|
|
'ajax' => false // Does this table support ajax? |
|
68
|
|
|
) ); |
|
69
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Show the search field. |
|
74
|
|
|
* |
|
75
|
|
|
* @since 1.0 |
|
76
|
|
|
* @access public |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $text Label for the search box. |
|
79
|
|
|
* @param string $input_id ID of the search box. |
|
80
|
|
|
* |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
public function search_box( $text, $input_id ) { |
|
84
|
|
|
$input_id = $input_id . '-search-input'; |
|
85
|
|
|
|
|
86
|
|
|
if ( ! empty( $_REQUEST['orderby'] ) ) { |
|
87
|
|
|
echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />'; |
|
88
|
|
|
} |
|
89
|
|
|
if ( ! empty( $_REQUEST['order'] ) ) { |
|
90
|
|
|
echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />'; |
|
91
|
|
|
} |
|
92
|
|
|
?> |
|
93
|
|
|
<p class="search-box" role="search"> |
|
94
|
|
|
<label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label> |
|
95
|
|
|
<input type="search" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>"/> |
|
96
|
|
|
<?php submit_button( $text, 'button', false, false, array( 'ID' => 'search-submit' ) ); ?> |
|
97
|
|
|
</p> |
|
98
|
|
|
<?php |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* This function renders most of the columns in the list table. |
|
103
|
|
|
* |
|
104
|
|
|
* @access public |
|
105
|
|
|
* @since 1.0 |
|
106
|
|
|
* |
|
107
|
|
|
* @param array $item Contains all the data of the customers. |
|
108
|
|
|
* @param string $column_name The name of the column. |
|
109
|
|
|
* |
|
110
|
|
|
* @return string Column Name. |
|
111
|
|
|
*/ |
|
112
|
|
|
public function column_default( $item, $column_name ) { |
|
113
|
|
|
switch ( $column_name ) { |
|
114
|
|
|
|
|
115
|
|
|
case 'num_purchases' : |
|
116
|
|
|
$value = '<a href="' . |
|
117
|
|
|
admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&user=' . urlencode( $item['email'] ) |
|
118
|
|
|
) . '">' . esc_html( $item['num_purchases'] ) . '</a>'; |
|
119
|
|
|
break; |
|
120
|
|
|
|
|
121
|
|
|
case 'amount_spent' : |
|
122
|
|
|
$value = give_currency_filter( give_format_amount( $item[ $column_name ] ) ); |
|
123
|
|
|
break; |
|
124
|
|
|
|
|
125
|
|
|
case 'date_created' : |
|
126
|
|
|
$value = date_i18n( give_date_format(), strtotime( $item['date_created'] ) ); |
|
127
|
|
|
break; |
|
128
|
|
|
|
|
129
|
|
|
default: |
|
130
|
|
|
$value = isset( $item[ $column_name ] ) ? $item[ $column_name ] : null; |
|
131
|
|
|
break; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return apply_filters( "give_report_column_{$column_name}", $value, $item['id'] ); |
|
135
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Column name. |
|
140
|
|
|
* |
|
141
|
|
|
* @param $item |
|
142
|
|
|
* |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
|
|
public function column_name( $item ) { |
|
146
|
|
|
$name = '#' . $item['id'] . ' '; |
|
147
|
|
|
$name .= ! empty( $item['name'] ) ? $item['name'] : '<em>' . esc_html__( 'Unnamed Donor', 'give' ) . '</em>'; |
|
148
|
|
|
$view_url = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $item['id'] ); |
|
149
|
|
|
$actions = $this->get_row_actions( $item ); |
|
150
|
|
|
|
|
151
|
|
|
return '<a href="' . esc_url( $view_url ) . '">' . $name . '</a>' . $this->row_actions( $actions ); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Retrieve the table columns. |
|
156
|
|
|
* |
|
157
|
|
|
* @access public |
|
158
|
|
|
* @since 1.0 |
|
159
|
|
|
* @return array $columns Array of all the list table columns. |
|
160
|
|
|
*/ |
|
161
|
|
|
public function get_columns() { |
|
162
|
|
|
$columns = array( |
|
163
|
|
|
'name' => esc_html__( 'Name', 'give' ), |
|
164
|
|
|
'email' => esc_html__( 'Email', 'give' ), |
|
165
|
|
|
'num_purchases' => esc_html__( 'Donations', 'give' ), |
|
166
|
|
|
'amount_spent' => esc_html__( 'Total Donated', 'give' ), |
|
167
|
|
|
'date_created' => esc_html__( 'Date Created', 'give' ) |
|
168
|
|
|
); |
|
169
|
|
|
|
|
170
|
|
|
return apply_filters( 'give_report_customer_columns', $columns ); |
|
171
|
|
|
|
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Get the sortable columns. |
|
176
|
|
|
* |
|
177
|
|
|
* @access public |
|
178
|
|
|
* @since 2.1 |
|
179
|
|
|
* @return array Array of all the sortable columns. |
|
180
|
|
|
*/ |
|
181
|
|
|
public function get_sortable_columns() { |
|
182
|
|
|
return array( |
|
183
|
|
|
'date_created' => array( 'date_created', true ), |
|
184
|
|
|
'name' => array( 'name', true ), |
|
185
|
|
|
'num_purchases' => array( 'purchase_count', false ), |
|
186
|
|
|
'amount_spent' => array( 'purchase_value', false ), |
|
187
|
|
|
); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Retrieve row actions. |
|
192
|
|
|
* |
|
193
|
|
|
* @since 1.7 |
|
194
|
|
|
* @access public |
|
195
|
|
|
* |
|
196
|
|
|
* @return array An array of action links. |
|
197
|
|
|
*/ |
|
198
|
|
|
public function get_row_actions( $item ) { |
|
199
|
|
|
|
|
200
|
|
|
$actions = array( |
|
201
|
|
|
|
|
202
|
|
|
'view' => sprintf( |
|
203
|
|
|
'<a href="%1$s" aria-label="%2$s">%3$s</a>', |
|
204
|
|
|
admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $item['id'] ), |
|
205
|
|
|
sprintf( esc_attr__( 'View "%s"', 'give' ), $item['name'] ), |
|
206
|
|
|
esc_html__( 'View Donor', 'give' ) |
|
207
|
|
|
), |
|
208
|
|
|
|
|
209
|
|
|
'notes' => sprintf( |
|
210
|
|
|
'<a href="%1$s" aria-label="%2$s">%3$s</a>', |
|
211
|
|
|
admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=notes&id=' . $item['id'] ), |
|
212
|
|
|
sprintf( esc_attr__( 'Notes for "%s"', 'give' ), $item['name'] ), |
|
213
|
|
|
esc_html__( 'Notes', 'give' ) |
|
214
|
|
|
), |
|
215
|
|
|
|
|
216
|
|
|
'delete' => sprintf( |
|
217
|
|
|
'<a href="%1$s" aria-label="%2$s">%3$s</a>', |
|
218
|
|
|
admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=delete&id=' . $item['id'] ), |
|
219
|
|
|
sprintf( esc_attr__( 'Delete "%s"', 'give' ), $item['name'] ), |
|
220
|
|
|
esc_html__( 'Delete', 'give' ) |
|
221
|
|
|
) |
|
222
|
|
|
|
|
223
|
|
|
); |
|
224
|
|
|
|
|
225
|
|
|
return apply_filters( 'give_donor_row_actions', $actions, $item ); |
|
226
|
|
|
|
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Outputs the reporting views. |
|
231
|
|
|
* |
|
232
|
|
|
* @access public |
|
233
|
|
|
* @since 1.0 |
|
234
|
|
|
* @return void |
|
235
|
|
|
*/ |
|
236
|
|
|
public function bulk_actions( $which = '' ) { |
|
237
|
|
|
// These aren't really bulk actions but this outputs the markup in the right place. |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Retrieve the current page number. |
|
242
|
|
|
* |
|
243
|
|
|
* @access public |
|
244
|
|
|
* @since 1.0 |
|
245
|
|
|
* @return int Current page number. |
|
246
|
|
|
*/ |
|
247
|
|
|
public function get_paged() { |
|
248
|
|
|
return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Retrieves the search query string. |
|
253
|
|
|
* |
|
254
|
|
|
* @access public |
|
255
|
|
|
* @since 1.0 |
|
256
|
|
|
* @return mixed string If search is present, false otherwise. |
|
257
|
|
|
*/ |
|
258
|
|
|
public function get_search() { |
|
259
|
|
|
return ! empty( $_GET['s'] ) ? urldecode( trim( $_GET['s'] ) ) : false; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Build all the reports data. |
|
264
|
|
|
* |
|
265
|
|
|
* @access public |
|
266
|
|
|
* @since 1.0 |
|
267
|
|
|
* @global object $wpdb Used to query the database using the WordPress. |
|
268
|
|
|
* Database API |
|
269
|
|
|
* @return array $reports_data All the data for customer reports. |
|
270
|
|
|
*/ |
|
271
|
|
|
public function reports_data() { |
|
272
|
|
|
global $wpdb; |
|
|
|
|
|
|
273
|
|
|
|
|
274
|
|
|
$data = array(); |
|
275
|
|
|
|
|
276
|
|
|
// Get donor query. |
|
277
|
|
|
$args = $this->get_donor_query(); |
|
278
|
|
|
$customers = Give()->customers->get_customers( $args ); |
|
279
|
|
|
|
|
280
|
|
|
if ( $customers ) { |
|
281
|
|
|
|
|
282
|
|
|
foreach ( $customers as $customer ) { |
|
283
|
|
|
|
|
284
|
|
|
$user_id = ! empty( $customer->user_id ) ? intval( $customer->user_id ) : 0; |
|
285
|
|
|
|
|
286
|
|
|
$data[] = array( |
|
287
|
|
|
'id' => $customer->id, |
|
288
|
|
|
'user_id' => $user_id, |
|
289
|
|
|
'name' => $customer->name, |
|
290
|
|
|
'email' => $customer->email, |
|
291
|
|
|
'num_purchases' => $customer->purchase_count, |
|
292
|
|
|
'amount_spent' => $customer->purchase_value, |
|
293
|
|
|
'date_created' => $customer->date_created, |
|
294
|
|
|
); |
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
return $data; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* Get donor count. |
|
303
|
|
|
* |
|
304
|
|
|
* @since 1.8.1 |
|
305
|
|
|
* @access private |
|
306
|
|
|
*/ |
|
307
|
|
|
private function get_donor_count() { |
|
308
|
|
|
// Get donor query. |
|
309
|
|
|
$_donor_query = $this->get_donor_query(); |
|
310
|
|
|
|
|
311
|
|
|
$_donor_query['number'] = -1; |
|
312
|
|
|
$donors = Give()->customers->get_customers( $_donor_query ); |
|
313
|
|
|
|
|
314
|
|
|
return count( $donors ); |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* Get donor query. |
|
319
|
|
|
* |
|
320
|
|
|
* @since 1.8.1 |
|
321
|
|
|
* @access public |
|
322
|
|
|
* @return array |
|
323
|
|
|
*/ |
|
324
|
|
|
public function get_donor_query() { |
|
325
|
|
|
$paged = $this->get_paged(); |
|
326
|
|
|
$offset = $this->per_page * ( $paged - 1 ); |
|
327
|
|
|
$search = $this->get_search(); |
|
328
|
|
|
$order = isset( $_GET['order'] ) ? sanitize_text_field( $_GET['order'] ) : 'DESC'; |
|
329
|
|
|
$orderby = isset( $_GET['orderby'] ) ? sanitize_text_field( $_GET['orderby'] ) : 'id'; |
|
330
|
|
|
|
|
331
|
|
|
$args = array( |
|
332
|
|
|
'number' => $this->per_page, |
|
333
|
|
|
'offset' => $offset, |
|
334
|
|
|
'order' => $order, |
|
335
|
|
|
'orderby' => $orderby, |
|
336
|
|
|
); |
|
337
|
|
|
|
|
338
|
|
|
if( $search ) { |
|
|
|
|
|
|
339
|
|
|
if ( is_email( $search ) ) { |
|
340
|
|
|
$args['email'] = $search; |
|
341
|
|
|
} elseif ( is_numeric( $search ) ) { |
|
342
|
|
|
$args['id'] = $search; |
|
343
|
|
|
} else { |
|
344
|
|
|
$args['name'] = $search; |
|
345
|
|
|
} |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
return $args; |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
/** |
|
352
|
|
|
* Setup the final data for the table. |
|
353
|
|
|
* |
|
354
|
|
|
* @access public |
|
355
|
|
|
* @since 1.0 |
|
356
|
|
|
* @uses Give_Customer_Reports_Table::get_columns() |
|
357
|
|
|
* @uses WP_List_Table::get_sortable_columns() |
|
358
|
|
|
* @uses Give_Customer_Reports_Table::get_pagenum() |
|
359
|
|
|
* @uses Give_Customer_Reports_Table::get_total_customers() |
|
360
|
|
|
* @return void |
|
361
|
|
|
*/ |
|
362
|
|
|
public function prepare_items() { |
|
363
|
|
|
|
|
364
|
|
|
$columns = $this->get_columns(); |
|
365
|
|
|
$hidden = array(); // No hidden columns |
|
366
|
|
|
$sortable = $this->get_sortable_columns(); |
|
367
|
|
|
|
|
368
|
|
|
$this->_column_headers = array( $columns, $hidden, $sortable ); |
|
369
|
|
|
|
|
370
|
|
|
$this->items = $this->reports_data(); |
|
371
|
|
|
|
|
372
|
|
|
$this->total = $this->get_donor_count(); |
|
373
|
|
|
|
|
374
|
|
|
$this->set_pagination_args( array( |
|
375
|
|
|
'total_items' => $this->total, |
|
376
|
|
|
'per_page' => $this->per_page, |
|
377
|
|
|
'total_pages' => ceil( $this->total / $this->per_page ) |
|
378
|
|
|
) ); |
|
379
|
|
|
} |
|
380
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.