-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwp-user-list-filter.php
155 lines (122 loc) · 4.46 KB
/
wp-user-list-filter.php
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
<?php
if ( !defined( 'ABSPATH' ) )
die( '-1' );
if( !class_exists('WP_User_List_Filter') ){
class WP_User_List_Filter {
private static $_this;
private $is_active = false;
private $desired_screen = 'users.php';
private $filters = array();
private $selected = array();
private $default;
function __construct() {
$this->default = apply_filters( 'wp_user_list_filter_default', array(
'label' => __('Filter by', 'wp-user-list-filter'),
'column' => null,
'items' => array()
));
add_action( 'restrict_manage_users', array( $this, 'inline_filters' ) );
add_filter( 'pre_user_query', array( $this, 'pre_user_query' ) );
}
function pre_user_query( $user_query ){
if( $this->selected_filters() ){
global $wpdb;
remove_filter( 'pre_user_query', array( $this, 'pre_user_query' ) );
$user_id_args = array(
'fields' => 'ID',
'meta_query' => array()
);
foreach( $this->selected as $key => $value) {
$user_id_args['meta_query'][] = array(
'key' => $this->filters[ $key ]->column,
'value' => $value
);
}
$user_id_args = apply_filters( 'wp_user_list_filter_user_id_args', $user_id_args );
$wp_user_query = new WP_User_Query($user_id_args);
$user_ids = $wp_user_query->get_results();
if( !empty($user_ids)){
$user_query->query_vars['include'] = wp_parse_args( (array) $user_ids, (array) $user_query->query_vars['include'] );
$user_query->query_where .= sprintf( " AND ID IN (%s) ", implode(",", $user_ids) );
}
add_filter( 'pre_user_query', array( $this, 'pre_user_query' ) );
}
return $user_query;
}
function get_filter( $key ){
return !empty( $this->filters[ $key ] ) ? $this->filters[ $key ] : $this->default;
}
function add_filter( $key, $args ){
$this->filters[ $key ] = apply_filters( 'wp_user_list_filter_add_filter', (object) wp_parse_args( (array) $args, (array) $this->default ) );
// setup default values for items
if( empty($this->filters[ $key ]->items) )
$this->set_items_from_db( $key );
}
function set_items_from_db( $key ){
global $wpdb;
$items = array();
$values = $wpdb->get_col( "SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = '{$this->filters[ $key ]->column}' ORDER BY meta_value DESC;" );
if( !empty($values)){
foreach( $values as $value ){
$value = maybe_unserialize( $value );
$items[ $value ] = apply_filters( 'wp_user_list_filter_item_from_db', $value, $key, $this->filters[ $key ] );
}
$this->filters[ $key ]->items = apply_filters( 'wp_user_list_filter_set_items_from_db', $items );
}
}
function selected_filters(){
if( !empty($_REQUEST['filterit']) ){
foreach( $this->filters as $key => $filter ) {
if( isset( $_REQUEST[ $key ]) && ($_REQUEST[ $key ]==="0"||$_REQUEST[ $key ] ) ){
$this->selected[ $key ] = apply_filters( 'wp_user_list_filter_selected_filter', $_REQUEST[ $key ], $key );
}
}
}
$this->selected = apply_filters( 'wp_user_list_filter_selected_filters', $this->selected );
return count( $this->selected );
}
function inline_filters(){
$filters = '';
foreach( $this->filters as $key => $filter ) {
$add_filter = false;
$filter_html = sprintf('<label class="screen-reader-text" for="new_role">%s</label>',
$filter->label
);
$filter_html .= sprintf('<select name="%s"><option value="">%s</option>',
$key,
$filter->label
);
foreach ( $filter->items as $index => $option ) {
$add_filter = true;
$selected = isset( $this->selected[ $key ] ) && $this->selected[ $key ] == $index ? ' selected="selected" ' : '';
$filter_html .= sprintf('<option value="%s" %s>%s</option>',
esc_attr($index),
$selected,
$option
);
}
$filter_html .= '</select>';
if( $add_filter )
$filters .= apply_filters( 'wp_user_list_filter_inline_filters', $filter_html, $key, $filter );
}
if( !empty($filters)){
// close down "bulk change" and reopen div for filters
echo '</div><div class="alignleft actions">' . $filters;
submit_button( __( 'Filter', 'wp-user-list-filter' ), 'button', 'filterit', false );
}
}
/**
* Static Singleton Factory Method
*
* @return static $_this instance
* @readlink http://eamann.com/tech/the-case-for-singletons/
*/
public static function instance() {
if ( !isset( self::$_this ) ) {
$className = __CLASS__;
self::$_this = new $className;
}
return self::$_this;
}
}
}