user.php
7.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
/**
* Addon class
*
* @since 1.0
*/
class CAC_Sortable_Model_User extends CAC_Sortable_Model {
/**
* Constructor
*
* @since 1.0
*/
function __construct( $storage_model ) {
parent::__construct( $storage_model );
// default sortby
$this->default_orderby = '';
// handle sorting request
add_action( 'pre_user_query', array( $this, 'handle_sorting_request' ), 1 );
// register sortable headings
add_filter( "manage_users_sortable_columns", array( $this, 'add_sortable_headings' ) );
// add reset button
add_action( 'restrict_manage_users', array( $this, 'add_reset_button' ) );
}
/**
* @see CAC_Sortable_Model::get_sortables()
* @since 1.0
*/
public function get_sortables() {
$column_names = array(
// WP default columns
'role',
'posts',
// Custom Columns
'column-first_name',
'column-display_name',
'column-last_name',
'column-meta',
'column-nickname',
'column-user_commentcount',
'column-user_description',
'column-user_id',
'column-user_postcount',
'column-user_registered',
'column-user_url',
// ACF Fields
'column-acf_field',
// WooCommerce
'column-wc-user-orders',
'column-wc-user-order_count'
//'column-wc-user-total-sales'
// WC Subscriptions extension
//'column-wc-user-subscription-expiration-date',
//'column-wc-user-subscription-trial-expiration-date',
//'column-wc-user-subscription-status',
//'column-wc-user-subscription-next-payment-date',
//'column-wc-user-subscription-last-payment-date',
//'column-wc-user-subscription-end-date'
);
return $column_names;
}
/**
* Get Users
*
* Do not use get_users() method.
*
* @since 1.0
*/
private function get_user_ids() {
global $wpdb;
return $wpdb->get_col( "SELECT ID FROM $wpdb->users" );
}
/**
* Admin requests for orderby column
*
* Only works for WP_Query objects ( such as posts and media )
*
* @since 1.0
*
* @param array $vars
* @return array Vars
*/
public function handle_sorting_request( $user_query ) {
global $wpdb;
$vars = $user_query->query_vars;
// prevent looping because this filter is trigered by get_users();
/*if ( 'ID' === $vars['fields'] ) {
return;
}*/
// sorting event?
if ( empty( $vars['orderby'] ) ) {
return;
}
$vars = $this->apply_sorting_preference( $vars );
$column = $this->get_column_by_orderby( $vars['orderby'] );
if ( empty( $column ) ) {
return;
}
// unsorted Users
$_users = array();
switch ( $column->properties->type ) :
// WP Default Columns
case 'role' :
$sort_flag = SORT_REGULAR;
foreach ( $this->get_user_ids() as $id ) {
$u = get_userdata( $id );
$role = ! empty( $u->roles[0] ) ? $u->roles[0] : '';
if ( $role ) {
$_users[ $id ] = $this->prepare_sort_string_value( $role );
}
}
break;
case 'posts' :
$sort_flag = SORT_NUMERIC;
foreach ( $this->get_user_ids() as $id ) {
$_users[ $id ] = $column->get_user_postcount( $id, 'post' );
}
break;
// Custom Columns
case 'column-user_id' :
$user_query->query_orderby = "ORDER BY ID {$vars['order']}";
$vars['orderby'] = 'ID';
break;
case 'column-user_registered' :
$user_query->query_orderby = "ORDER BY user_registered {$vars['order']}";
$vars['orderby'] = 'registered';
break;
case 'column-nickname' :
$sort_flag = SORT_REGULAR;
break;
case 'column-first_name' :
$sort_flag = SORT_REGULAR;
break;
case 'column-display_name' :
$sort_flag = SORT_REGULAR;
break;
case 'column-last_name' :
$sort_flag = SORT_REGULAR;
break;
case 'column-user_url' :
$sort_flag = SORT_REGULAR;
break;
case 'column-user_description' :
$sort_flag = SORT_REGULAR;
break;
case 'column-user_commentcount' :
// @todo: maybe use WP_Comment_Query to generate this subquery? penalty is extra query and bloat, advantage is WP_Comment_Query filters used
$sub_query = "
LEFT JOIN (
SELECT user_id, COUNT(user_id) AS comment_count
FROM {$wpdb->comments}
WHERE user_id <> 0
GROUP BY user_id
) AS comments
ON {$wpdb->users}.ID = comments.user_id
";
$user_query->query_from .= $sub_query;
$user_query->query_orderby = "ORDER BY comment_count " . $vars['order'];
if ( ! $this->show_all_results ) {
$user_query->query_where .= " AND comment_count IS NOT NULL";
}
break;
case 'column-user_postcount' :
$sort_flag = SORT_NUMERIC;
foreach ( $this->get_user_ids() as $id ) {
$_users[ $id ] = $column->get_count( $id );
}
break;
case 'column-meta' :
$sort_flag = SORT_REGULAR;
if ( 'numeric' == $column->options->field_type ) {
$sort_flag = SORT_NUMERIC;
}
if ( 'checkmark' == $column->options->field_type ) {
foreach ( $this->get_user_ids() as $id ) {
$value = $column->get_value( $id );
$_users[ $id ] = $this->prepare_sort_string_value( $value ? '1' : '0' );
}
}
if ( in_array( $column->options->field_type, array( 'image', 'library_id' ) ) ) {
$sort_flag = SORT_NUMERIC;
foreach ( $this->get_user_ids() as $id ) {
$thumbs = $column->get_thumbnails( $column->get_meta_by_id( $id ) );
$_users[ $id ] = $thumbs ? count( $thumbs ) : 0;
}
}
break;
case 'column-acf_field' :
if ( method_exists( $column, 'get_field' ) ) {
$field = $column->get_field();
$sort_flag = in_array( $field['type'], array( 'date_picker', 'number' ) ) ? SORT_NUMERIC : SORT_REGULAR;
foreach ( $this->get_user_ids() as $id ) {
$value = $column->get_sorting_value( $id );
if ( $value || $this->show_all_results ) {
$_users[ $id ] = $this->prepare_sort_string_value( $value );
}
}
}
break;
// WooCommerce
case 'column-wc-user-orders':
$sort_flag = SORT_NUMERIC;
foreach ( $this->get_user_ids() as $id ) {
$value = $column->get_raw_value( $id );
if ( $value || $this->show_all_results ) {
$_users[ $id ] = count( $value );
}
}
break;
case 'column-wc-user-order_count':
$sort_flag = SORT_NUMERIC;
break;
case 'column-wc-user-total-sales':
$sort_flag = SORT_NUMERIC;
foreach ( $this->get_user_ids() as $id ) {
$value = $column->get_sorting_value( $id );
if ( $value || $this->show_all_results ) {
$_users[ $id ] = $value;
}
}
break;
// Try to sort by raw value.
default :
$sort_flag = SORT_REGULAR;
foreach ( $this->get_user_ids() as $id ) {
$_users[ $id ] = $column->get_raw_value( $id );
}
endswitch;
if ( isset( $sort_flag ) ) {
// set sorting value
if ( empty( $_users ) ) {
foreach ( $this->get_user_ids() as $id ) {
$value = $column->get_raw_value( $id );
if ( $value || $this->show_all_results ) {
$_users[ $id ] = $this->prepare_sort_string_value( $value );
}
}
}
// sorting
if ( 'ASC' == $vars['order'] ) {
asort( $_users, $sort_flag );
} else {
arsort( $_users, $sort_flag );
}
// alter orderby SQL
if ( ! empty( $_users ) ) {
global $wpdb;
// for MU site compatibility
$prefix = $wpdb->base_prefix;
$column_names = implode( ',', array_keys( $_users ) );
$user_query->query_where .= " AND {$prefix}users.ID IN ({$column_names})";
$user_query->query_orderby = "ORDER BY FIELD({$prefix}users.ID,{$column_names})";
}
// cleanup the vars we dont need
$vars['order'] = '';
$vars['orderby'] = '';
}
$user_query->query_vars = array_merge( $user_query->query_vars, $vars );
}
}