|
1 <?php |
|
2 /** |
|
3 * Users List Table class. |
|
4 * |
|
5 * @package WordPress |
|
6 * @subpackage List_Table |
|
7 * @since 3.1.0 |
|
8 * @access private |
|
9 */ |
|
10 class WP_Users_List_Table extends WP_List_Table { |
|
11 |
|
12 var $site_id; |
|
13 var $is_site_users; |
|
14 |
|
15 function __construct( $args = array() ) { |
|
16 parent::__construct( array( |
|
17 'singular' => 'user', |
|
18 'plural' => 'users', |
|
19 'screen' => isset( $args['screen'] ) ? $args['screen'] : null, |
|
20 ) ); |
|
21 |
|
22 $this->is_site_users = 'site-users-network' == $this->screen->id; |
|
23 |
|
24 if ( $this->is_site_users ) |
|
25 $this->site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0; |
|
26 } |
|
27 |
|
28 function ajax_user_can() { |
|
29 if ( $this->is_site_users ) |
|
30 return current_user_can( 'manage_sites' ); |
|
31 else |
|
32 return current_user_can( 'list_users' ); |
|
33 } |
|
34 |
|
35 function prepare_items() { |
|
36 global $role, $usersearch; |
|
37 |
|
38 $usersearch = isset( $_REQUEST['s'] ) ? trim( $_REQUEST['s'] ) : ''; |
|
39 |
|
40 $role = isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : ''; |
|
41 |
|
42 $per_page = ( $this->is_site_users ) ? 'site_users_network_per_page' : 'users_per_page'; |
|
43 $users_per_page = $this->get_items_per_page( $per_page ); |
|
44 |
|
45 $paged = $this->get_pagenum(); |
|
46 |
|
47 $args = array( |
|
48 'number' => $users_per_page, |
|
49 'offset' => ( $paged-1 ) * $users_per_page, |
|
50 'role' => $role, |
|
51 'search' => $usersearch, |
|
52 'fields' => 'all_with_meta' |
|
53 ); |
|
54 |
|
55 if ( '' !== $args['search'] ) |
|
56 $args['search'] = '*' . $args['search'] . '*'; |
|
57 |
|
58 if ( $this->is_site_users ) |
|
59 $args['blog_id'] = $this->site_id; |
|
60 |
|
61 if ( isset( $_REQUEST['orderby'] ) ) |
|
62 $args['orderby'] = $_REQUEST['orderby']; |
|
63 |
|
64 if ( isset( $_REQUEST['order'] ) ) |
|
65 $args['order'] = $_REQUEST['order']; |
|
66 |
|
67 // Query the user IDs for this page |
|
68 $wp_user_search = new WP_User_Query( $args ); |
|
69 |
|
70 $this->items = $wp_user_search->get_results(); |
|
71 |
|
72 $this->set_pagination_args( array( |
|
73 'total_items' => $wp_user_search->get_total(), |
|
74 'per_page' => $users_per_page, |
|
75 ) ); |
|
76 } |
|
77 |
|
78 function no_items() { |
|
79 _e( 'No matching users were found.' ); |
|
80 } |
|
81 |
|
82 function get_views() { |
|
83 global $wp_roles, $role; |
|
84 |
|
85 if ( $this->is_site_users ) { |
|
86 $url = 'site-users.php?id=' . $this->site_id; |
|
87 switch_to_blog( $this->site_id ); |
|
88 $users_of_blog = count_users(); |
|
89 restore_current_blog(); |
|
90 } else { |
|
91 $url = 'users.php'; |
|
92 $users_of_blog = count_users(); |
|
93 } |
|
94 $total_users = $users_of_blog['total_users']; |
|
95 $avail_roles =& $users_of_blog['avail_roles']; |
|
96 unset($users_of_blog); |
|
97 |
|
98 $current_role = false; |
|
99 $class = empty($role) ? ' class="current"' : ''; |
|
100 $role_links = array(); |
|
101 $role_links['all'] = "<a href='$url'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_users, 'users' ), number_format_i18n( $total_users ) ) . '</a>'; |
|
102 foreach ( $wp_roles->get_names() as $this_role => $name ) { |
|
103 if ( !isset($avail_roles[$this_role]) ) |
|
104 continue; |
|
105 |
|
106 $class = ''; |
|
107 |
|
108 if ( $this_role == $role ) { |
|
109 $current_role = $role; |
|
110 $class = ' class="current"'; |
|
111 } |
|
112 |
|
113 $name = translate_user_role( $name ); |
|
114 /* translators: User role name with count */ |
|
115 $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n( $avail_roles[$this_role] ) ); |
|
116 $role_links[$this_role] = "<a href='" . esc_url( add_query_arg( 'role', $this_role, $url ) ) . "'$class>$name</a>"; |
|
117 } |
|
118 |
|
119 return $role_links; |
|
120 } |
|
121 |
|
122 function get_bulk_actions() { |
|
123 $actions = array(); |
|
124 |
|
125 if ( is_multisite() ) { |
|
126 if ( current_user_can( 'remove_users' ) ) |
|
127 $actions['remove'] = __( 'Remove' ); |
|
128 } else { |
|
129 if ( current_user_can( 'delete_users' ) ) |
|
130 $actions['delete'] = __( 'Delete' ); |
|
131 } |
|
132 |
|
133 return $actions; |
|
134 } |
|
135 |
|
136 function extra_tablenav( $which ) { |
|
137 if ( 'top' != $which ) |
|
138 return; |
|
139 ?> |
|
140 <div class="alignleft actions"> |
|
141 <?php if ( current_user_can( 'promote_users' ) ) : ?> |
|
142 <label class="screen-reader-text" for="new_role"><?php _e( 'Change role to…' ) ?></label> |
|
143 <select name="new_role" id="new_role"> |
|
144 <option value=''><?php _e( 'Change role to…' ) ?></option> |
|
145 <?php wp_dropdown_roles(); ?> |
|
146 </select> |
|
147 <?php |
|
148 submit_button( __( 'Change' ), 'button', 'changeit', false ); |
|
149 endif; |
|
150 |
|
151 do_action( 'restrict_manage_users' ); |
|
152 echo '</div>'; |
|
153 } |
|
154 |
|
155 function current_action() { |
|
156 if ( isset($_REQUEST['changeit']) && !empty($_REQUEST['new_role']) ) |
|
157 return 'promote'; |
|
158 |
|
159 return parent::current_action(); |
|
160 } |
|
161 |
|
162 function get_columns() { |
|
163 $c = array( |
|
164 'cb' => '<input type="checkbox" />', |
|
165 'username' => __( 'Username' ), |
|
166 'name' => __( 'Name' ), |
|
167 'email' => __( 'E-mail' ), |
|
168 'role' => __( 'Role' ), |
|
169 'posts' => __( 'Posts' ) |
|
170 ); |
|
171 |
|
172 if ( $this->is_site_users ) |
|
173 unset( $c['posts'] ); |
|
174 |
|
175 return $c; |
|
176 } |
|
177 |
|
178 function get_sortable_columns() { |
|
179 $c = array( |
|
180 'username' => 'login', |
|
181 'name' => 'name', |
|
182 'email' => 'email', |
|
183 ); |
|
184 |
|
185 if ( $this->is_site_users ) |
|
186 unset( $c['posts'] ); |
|
187 |
|
188 return $c; |
|
189 } |
|
190 |
|
191 function display_rows() { |
|
192 // Query the post counts for this page |
|
193 if ( ! $this->is_site_users ) |
|
194 $post_counts = count_many_users_posts( array_keys( $this->items ) ); |
|
195 |
|
196 $editable_roles = array_keys( get_editable_roles() ); |
|
197 |
|
198 $style = ''; |
|
199 foreach ( $this->items as $userid => $user_object ) { |
|
200 if ( count( $user_object->roles ) <= 1 ) { |
|
201 $role = reset( $user_object->roles ); |
|
202 } elseif ( $roles = array_intersect( array_values( $user_object->roles ), $editable_roles ) ) { |
|
203 $role = reset( $roles ); |
|
204 } else { |
|
205 $role = reset( $user_object->roles ); |
|
206 } |
|
207 |
|
208 if ( is_multisite() && empty( $user_object->allcaps ) ) |
|
209 continue; |
|
210 |
|
211 $style = ( ' class="alternate"' == $style ) ? '' : ' class="alternate"'; |
|
212 echo "\n\t" . $this->single_row( $user_object, $style, $role, isset( $post_counts ) ? $post_counts[ $userid ] : 0 ); |
|
213 } |
|
214 } |
|
215 |
|
216 /** |
|
217 * Generate HTML for a single row on the users.php admin panel. |
|
218 * |
|
219 * @since 2.1.0 |
|
220 * |
|
221 * @param object $user_object |
|
222 * @param string $style Optional. Attributes added to the TR element. Must be sanitized. |
|
223 * @param string $role Key for the $wp_roles array. |
|
224 * @param int $numposts Optional. Post count to display for this user. Defaults to zero, as in, a new user has made zero posts. |
|
225 * @return string |
|
226 */ |
|
227 function single_row( $user_object, $style = '', $role = '', $numposts = 0 ) { |
|
228 global $wp_roles; |
|
229 |
|
230 if ( !( is_object( $user_object ) && is_a( $user_object, 'WP_User' ) ) ) |
|
231 $user_object = get_userdata( (int) $user_object ); |
|
232 $user_object->filter = 'display'; |
|
233 $email = $user_object->user_email; |
|
234 |
|
235 if ( $this->is_site_users ) |
|
236 $url = "site-users.php?id={$this->site_id}&"; |
|
237 else |
|
238 $url = 'users.php?'; |
|
239 |
|
240 $checkbox = ''; |
|
241 // Check if the user for this row is editable |
|
242 if ( current_user_can( 'list_users' ) ) { |
|
243 // Set up the user editing link |
|
244 $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user_object->ID ) ) ); |
|
245 |
|
246 // Set up the hover actions for this user |
|
247 $actions = array(); |
|
248 |
|
249 if ( current_user_can( 'edit_user', $user_object->ID ) ) { |
|
250 $edit = "<strong><a href=\"$edit_link\">$user_object->user_login</a></strong><br />"; |
|
251 $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>'; |
|
252 } else { |
|
253 $edit = "<strong>$user_object->user_login</strong><br />"; |
|
254 } |
|
255 |
|
256 if ( !is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'delete_user', $user_object->ID ) ) |
|
257 $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url( "users.php?action=delete&user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Delete' ) . "</a>"; |
|
258 if ( is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'remove_user', $user_object->ID ) ) |
|
259 $actions['remove'] = "<a class='submitdelete' href='" . wp_nonce_url( $url."action=remove&user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Remove' ) . "</a>"; |
|
260 $actions = apply_filters( 'user_row_actions', $actions, $user_object ); |
|
261 $edit .= $this->row_actions( $actions ); |
|
262 |
|
263 // Set up the checkbox ( because the user is editable, otherwise it's empty ) |
|
264 $checkbox = '<label class="screen-reader-text" for="cb-select-' . $user_object->ID . '">' . sprintf( __( 'Select %s' ), $user_object->user_login ) . '</label>' |
|
265 . "<input type='checkbox' name='users[]' id='user_{$user_object->ID}' class='$role' value='{$user_object->ID}' />"; |
|
266 |
|
267 } else { |
|
268 $edit = '<strong>' . $user_object->user_login . '</strong>'; |
|
269 } |
|
270 $role_name = isset( $wp_roles->role_names[$role] ) ? translate_user_role( $wp_roles->role_names[$role] ) : __( 'None' ); |
|
271 $avatar = get_avatar( $user_object->ID, 32 ); |
|
272 |
|
273 $r = "<tr id='user-$user_object->ID'$style>"; |
|
274 |
|
275 list( $columns, $hidden ) = $this->get_column_info(); |
|
276 |
|
277 foreach ( $columns as $column_name => $column_display_name ) { |
|
278 $class = "class=\"$column_name column-$column_name\""; |
|
279 |
|
280 $style = ''; |
|
281 if ( in_array( $column_name, $hidden ) ) |
|
282 $style = ' style="display:none;"'; |
|
283 |
|
284 $attributes = "$class$style"; |
|
285 |
|
286 switch ( $column_name ) { |
|
287 case 'cb': |
|
288 $r .= "<th scope='row' class='check-column'>$checkbox</th>"; |
|
289 break; |
|
290 case 'username': |
|
291 $r .= "<td $attributes>$avatar $edit</td>"; |
|
292 break; |
|
293 case 'name': |
|
294 $r .= "<td $attributes>$user_object->first_name $user_object->last_name</td>"; |
|
295 break; |
|
296 case 'email': |
|
297 $r .= "<td $attributes><a href='mailto:$email' title='" . esc_attr( sprintf( __( 'E-mail: %s' ), $email ) ) . "'>$email</a></td>"; |
|
298 break; |
|
299 case 'role': |
|
300 $r .= "<td $attributes>$role_name</td>"; |
|
301 break; |
|
302 case 'posts': |
|
303 $attributes = 'class="posts column-posts num"' . $style; |
|
304 $r .= "<td $attributes>"; |
|
305 if ( $numposts > 0 ) { |
|
306 $r .= "<a href='edit.php?author=$user_object->ID' title='" . esc_attr__( 'View posts by this author' ) . "' class='edit'>"; |
|
307 $r .= $numposts; |
|
308 $r .= '</a>'; |
|
309 } else { |
|
310 $r .= 0; |
|
311 } |
|
312 $r .= "</td>"; |
|
313 break; |
|
314 default: |
|
315 $r .= "<td $attributes>"; |
|
316 $r .= apply_filters( 'manage_users_custom_column', '', $column_name, $user_object->ID ); |
|
317 $r .= "</td>"; |
|
318 } |
|
319 } |
|
320 $r .= '</tr>'; |
|
321 |
|
322 return $r; |
|
323 } |
|
324 } |