|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Customers Table Class |
|
4
|
|
|
* |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
// Exit if accessed directly |
|
8
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
9
|
|
|
exit; |
|
10
|
|
|
} |
|
11
|
|
|
|
|
12
|
|
|
// Load WP_List_Table if not loaded |
|
13
|
|
|
if ( ! class_exists( 'WP_List_Table' ) ) { |
|
14
|
|
|
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* WPInv_Customers_Table Class |
|
19
|
|
|
* |
|
20
|
|
|
* Renders the Gateway Reports table |
|
21
|
|
|
* |
|
22
|
|
|
* @since 1.0.19 |
|
23
|
|
|
*/ |
|
24
|
|
|
class WPInv_Customers_Table extends WP_List_Table { |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var int Number of items per page |
|
28
|
|
|
* @since 1.0.19 |
|
29
|
|
|
*/ |
|
30
|
|
|
public $per_page = 25; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var int Number of items |
|
34
|
|
|
* @since 1.0.19 |
|
35
|
|
|
*/ |
|
36
|
|
|
public $total_count = 0; |
|
37
|
|
|
|
|
38
|
|
|
public $query; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get things started |
|
42
|
|
|
* |
|
43
|
|
|
* @since 1.0.19 |
|
44
|
|
|
* @see WP_List_Table::__construct() |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct() { |
|
47
|
|
|
|
|
48
|
|
|
// Set parent defaults |
|
49
|
|
|
parent::__construct( |
|
50
|
|
|
array( |
|
51
|
|
|
'singular' => 'id', |
|
52
|
|
|
'plural' => 'ids', |
|
53
|
|
|
'ajax' => false, |
|
54
|
|
|
) |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Gets the name of the primary column. |
|
61
|
|
|
* |
|
62
|
|
|
* @since 1.0.19 |
|
63
|
|
|
* @access protected |
|
64
|
|
|
* |
|
65
|
|
|
* @return string Name of the primary column. |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function get_primary_column_name() { |
|
68
|
|
|
return 'customer'; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* This function renders most of the columns in the list table. |
|
73
|
|
|
* |
|
74
|
|
|
* @since 1.0.19 |
|
75
|
|
|
* |
|
76
|
|
|
* @param GetPaid_Customer $customer |
|
77
|
|
|
* @param string $column_name The name of the column |
|
78
|
|
|
* |
|
79
|
|
|
* @return string Column Name |
|
80
|
|
|
*/ |
|
81
|
|
|
public function column_default( $customer, $column_name ) { |
|
82
|
|
|
$value = esc_html( $customer->get( $column_name ) ); |
|
83
|
|
|
return apply_filters( 'wpinv_customers_table_column' . $column_name, $value, $customer ); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Displays the country column. |
|
88
|
|
|
* |
|
89
|
|
|
* @since 1.0.19 |
|
90
|
|
|
* |
|
91
|
|
|
* @param GetPaid_Customer $customer |
|
92
|
|
|
* |
|
93
|
|
|
* @return string Column Name |
|
94
|
|
|
*/ |
|
95
|
|
|
public function column_country( $customer ) { |
|
96
|
|
|
$country = wpinv_sanitize_country( $customer->get( 'country' ) ); |
|
97
|
|
|
if ( $country ) { |
|
|
|
|
|
|
98
|
|
|
$country = wpinv_country_name( $country ); |
|
99
|
|
|
} |
|
100
|
|
|
return esc_html( $country ); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Displays the state column. |
|
105
|
|
|
* |
|
106
|
|
|
* @since 1.0.19 |
|
107
|
|
|
* |
|
108
|
|
|
* @param GetPaid_Customer $customer |
|
109
|
|
|
* |
|
110
|
|
|
* @return string Column Name |
|
111
|
|
|
*/ |
|
112
|
|
|
public function column_state( $customer ) { |
|
113
|
|
|
$country = wpinv_sanitize_country( $customer->get( 'country' ) ); |
|
114
|
|
|
$state = $customer->get( 'state' ); |
|
115
|
|
|
if ( $state ) { |
|
116
|
|
|
$state = wpinv_state_name( $state, $country ); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return esc_html( $state ); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Displays the signup column. |
|
124
|
|
|
* |
|
125
|
|
|
* @since 1.0.19 |
|
126
|
|
|
* |
|
127
|
|
|
* @param GetPaid_Customer $customer |
|
128
|
|
|
* |
|
129
|
|
|
* @return string Column Name |
|
130
|
|
|
*/ |
|
131
|
|
|
public function column_date_created( $customer ) { |
|
132
|
|
|
return getpaid_format_date_value( $customer->get( 'date_created' ) ); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Displays the total spent column. |
|
137
|
|
|
* |
|
138
|
|
|
* @since 1.0.19 |
|
139
|
|
|
* |
|
140
|
|
|
* @param GetPaid_Customer $customer |
|
141
|
|
|
* |
|
142
|
|
|
* @return string Column Name |
|
143
|
|
|
*/ |
|
144
|
|
|
public function column_purchase_value( $customer ) { |
|
145
|
|
|
return wpinv_price( (float) $customer->get( 'purchase_value' ) ); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Displays the total spent column. |
|
150
|
|
|
* |
|
151
|
|
|
* @since 1.0.19 |
|
152
|
|
|
* |
|
153
|
|
|
* @param GetPaid_Customer $customer |
|
154
|
|
|
* |
|
155
|
|
|
* @return string Column Name |
|
156
|
|
|
*/ |
|
157
|
|
|
public function column_purchase_count( $customer ) { |
|
158
|
|
|
$value = $customer->get( 'purchase_count' ); |
|
159
|
|
|
$url = $customer->get( 'user_id' ) ? add_query_arg( array( 'post_type' => 'wpi_invoice', 'author' => $customer->get( 'user_id' ), ), admin_url( 'edit.php' ) ) : ''; |
|
160
|
|
|
|
|
161
|
|
|
return ( empty( $value ) || empty( $url ) ) ? (int) $value : '<a href="' . esc_url( $url ) . '">' . absint( $value ) . '</a>'; |
|
162
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Displays the customers name |
|
167
|
|
|
* |
|
168
|
|
|
* @param GetPaid_Customer $customer customer. |
|
169
|
|
|
* @return string |
|
170
|
|
|
*/ |
|
171
|
|
|
public function column_customer( $customer ) { |
|
172
|
|
|
|
|
173
|
|
|
$first_name = $customer->get( 'first_name' ); |
|
174
|
|
|
$last_name = $customer->get( 'last_name' ); |
|
175
|
|
|
$email = $customer->get( 'email' ); |
|
176
|
|
|
$avatar = get_avatar( $customer->get( 'user_id' ) ? $customer->get( 'user_id' ) : $email, 32 ); |
|
177
|
|
|
|
|
178
|
|
|
// Customer view URL. |
|
179
|
|
|
$view_url = $customer->get( 'user_id' ) ? esc_url( add_query_arg( 'user_id', $customer->get( 'user_id' ), admin_url( 'user-edit.php' ) ) ) : false; |
|
180
|
|
|
$row_actions = $view_url ? $this->row_actions( |
|
181
|
|
|
array( |
|
182
|
|
|
'view' => '<a href="' . $view_url . '#getpaid-fieldset-billing">' . __( 'Edit Details', 'invoicing' ) . '</a>', |
|
183
|
|
|
) |
|
184
|
|
|
) : ''; |
|
185
|
|
|
|
|
186
|
|
|
// Customer's name. |
|
187
|
|
|
$name = esc_html( trim( "$first_name $last_name" ) ); |
|
188
|
|
|
|
|
189
|
|
|
if ( ! empty( $name ) ) { |
|
190
|
|
|
$name = "<div style='overflow: hidden;height: 18px;'>$name</div>"; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
$email = "<div class='row-title'><a href='mailto:$email'>$email</a></div>"; |
|
194
|
|
|
|
|
195
|
|
|
return "<div style='display: flex;'><div>$avatar</div><div style='margin-left: 10px;'>$name<strong>$email</strong>$row_actions</div></div>"; |
|
196
|
|
|
|
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Retrieve the current page number |
|
201
|
|
|
* |
|
202
|
|
|
* @since 1.0.19 |
|
203
|
|
|
* @return int Current page number |
|
204
|
|
|
*/ |
|
205
|
|
|
public function get_paged() { |
|
206
|
|
|
return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Returns bulk actions. |
|
211
|
|
|
* |
|
212
|
|
|
* @since 1.0.19 |
|
213
|
|
|
* @return void |
|
214
|
|
|
*/ |
|
215
|
|
|
public function bulk_actions( $which = '' ) { |
|
216
|
|
|
return array(); |
|
|
|
|
|
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Prepares the display query |
|
221
|
|
|
*/ |
|
222
|
|
|
public function prepare_query() { |
|
223
|
|
|
|
|
224
|
|
|
// Prepare query args. |
|
225
|
|
|
$query = array( |
|
226
|
|
|
'number' => $this->per_page, |
|
227
|
|
|
'paged' => $this->get_paged(), |
|
228
|
|
|
); |
|
229
|
|
|
|
|
230
|
|
|
foreach ( array( 'orderby', 'order', 's' ) as $field ) { |
|
231
|
|
|
if ( isset( $_GET[ $field ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
232
|
|
|
$query[ $field ] = wpinv_clean( rawurlencode_deep( $_GET[ $field ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
237
|
|
|
|
|
238
|
|
|
if ( isset( $_GET[ $field ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
239
|
|
|
$query[ $field ] = wpinv_clean( rawurlencode_deep( $_GET[ $field ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
// Min max. |
|
243
|
|
|
if ( '%f' === $type || '%d' === $type ) { |
|
244
|
|
|
|
|
245
|
|
|
if ( isset( $_GET[ $field . '_min' ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
246
|
|
|
$query[ $field . '_min' ] = floatval( $_GET[ $field . '_min' ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
if ( isset( $_GET[ $field . '_max' ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
250
|
|
|
$query[ $field . '_max' ] = floatval( $_GET[ $field . '_max' ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
// Prepare class properties. |
|
256
|
|
|
$this->query = getpaid_get_customers( $query, 'query' ); |
|
257
|
|
|
$this->total_count = $this->query->get_total(); |
|
258
|
|
|
$this->items = $this->query->get_results(); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Setup the final data for the table |
|
263
|
|
|
* |
|
264
|
|
|
*/ |
|
265
|
|
|
public function prepare_items() { |
|
266
|
|
|
|
|
267
|
|
|
$columns = $this->get_columns(); |
|
268
|
|
|
$hidden = array(); |
|
269
|
|
|
$sortable = $this->get_sortable_columns(); |
|
270
|
|
|
$this->prepare_query(); |
|
271
|
|
|
|
|
272
|
|
|
$this->_column_headers = array( $columns, $hidden, $sortable ); |
|
273
|
|
|
|
|
274
|
|
|
$this->set_pagination_args( |
|
275
|
|
|
array( |
|
276
|
|
|
'total_items' => $this->total_count, |
|
277
|
|
|
'per_page' => $this->per_page, |
|
278
|
|
|
'total_pages' => ceil( $this->total_count / $this->per_page ), |
|
279
|
|
|
) |
|
280
|
|
|
); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Sortable table columns. |
|
285
|
|
|
* |
|
286
|
|
|
* @return array |
|
287
|
|
|
*/ |
|
288
|
|
|
public function get_sortable_columns() { |
|
289
|
|
|
$sortable = array( |
|
290
|
|
|
'customer' => array( 'first_name', true ), |
|
291
|
|
|
); |
|
292
|
|
|
|
|
293
|
|
|
foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
294
|
|
|
$sortable[ $field ] = array( $field, true ); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
return apply_filters( 'manage_getpaid_customers_sortable_table_columns', $sortable ); |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* Table columns |
|
302
|
|
|
* |
|
303
|
|
|
* @return array |
|
304
|
|
|
*/ |
|
305
|
|
|
public function get_columns() { |
|
306
|
|
|
$columns = array( |
|
307
|
|
|
'cb' => '<input type="checkbox" />', |
|
308
|
|
|
'customer' => __( 'Customer', 'invoicing' ), |
|
309
|
|
|
); |
|
310
|
|
|
|
|
311
|
|
|
// Add address fields. |
|
312
|
|
|
foreach ( getpaid_user_address_fields() as $key => $value ) { |
|
313
|
|
|
|
|
314
|
|
|
// Skip id, user_id and email. |
|
315
|
|
|
if ( ! in_array( $key, array( 'id', 'user_id', 'email', 'purchase_value', 'purchase_count', 'date_created', 'date_modified', 'uuid', 'first_name', 'last_name' ), true ) ) { |
|
316
|
|
|
$columns[ $key ] = $value; |
|
317
|
|
|
} |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
$columns['purchase_value'] = __( 'Total Spend', 'invoicing' ); |
|
321
|
|
|
$columns['purchase_count'] = __( 'Invoices', 'invoicing' ); |
|
322
|
|
|
$columns['date_created'] = __( 'Date created', 'invoicing' ); |
|
323
|
|
|
|
|
324
|
|
|
return apply_filters( 'manage_getpaid_customers_table_columns', $columns ); |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.