WordPress 4.0
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-ms-users-list-table.php
1 <?php
2 /**
3  * Multisite Users List Table class.
4  *
5  * @package WordPress
6  * @subpackage List_Table
7  * @since 3.1.0
8  * @access private
9  */
10 class WP_MS_Users_List_Table extends WP_List_Table {
11
12         public function ajax_user_can() {
13                 return current_user_can( 'manage_network_users' );
14         }
15
16         public function prepare_items() {
17                 global $usersearch, $role, $wpdb, $mode;
18
19                 $usersearch = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : '';
20
21                 $users_per_page = $this->get_items_per_page( 'users_network_per_page' );
22
23                 $role = isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : '';
24
25                 $paged = $this->get_pagenum();
26
27                 $args = array(
28                         'number' => $users_per_page,
29                         'offset' => ( $paged-1 ) * $users_per_page,
30                         'search' => $usersearch,
31                         'blog_id' => 0,
32                         'fields' => 'all_with_meta'
33                 );
34
35                 if ( wp_is_large_network( 'users' ) )
36                         $args['search'] = ltrim( $args['search'], '*' );
37
38                 if ( $role == 'super' ) {
39                         $logins = implode( "', '", get_super_admins() );
40                         $args['include'] = $wpdb->get_col( "SELECT ID FROM $wpdb->users WHERE user_login IN ('$logins')" );
41                 }
42
43                 /*
44                  * If the network is large and a search is not being performed,
45                  * show only the latest users with no paging in order to avoid
46                  * expensive count queries.
47                  */
48                 if ( !$usersearch && wp_is_large_network( 'users' ) ) {
49                         if ( !isset($_REQUEST['orderby']) )
50                                 $_GET['orderby'] = $_REQUEST['orderby'] = 'id';
51                         if ( !isset($_REQUEST['order']) )
52                                 $_GET['order'] = $_REQUEST['order'] = 'DESC';
53                         $args['count_total'] = false;
54                 }
55
56                 if ( isset( $_REQUEST['orderby'] ) )
57                         $args['orderby'] = $_REQUEST['orderby'];
58
59                 if ( isset( $_REQUEST['order'] ) )
60                         $args['order'] = $_REQUEST['order'];
61
62                 $mode = empty( $_REQUEST['mode'] ) ? 'list' : $_REQUEST['mode'];
63
64                 // Query the user IDs for this page
65                 $wp_user_search = new WP_User_Query( $args );
66
67                 $this->items = $wp_user_search->get_results();
68
69                 $this->set_pagination_args( array(
70                         'total_items' => $wp_user_search->get_total(),
71                         'per_page' => $users_per_page,
72                 ) );
73         }
74
75         protected function get_bulk_actions() {
76                 $actions = array();
77                 if ( current_user_can( 'delete_users' ) )
78                         $actions['delete'] = __( 'Delete' );
79                 $actions['spam'] = _x( 'Mark as Spam', 'user' );
80                 $actions['notspam'] = _x( 'Not Spam', 'user' );
81
82                 return $actions;
83         }
84
85         public function no_items() {
86                 _e( 'No users found.' );
87         }
88
89         protected function get_views() {
90                 global $role;
91
92                 $total_users = get_user_count();
93                 $super_admins = get_super_admins();
94                 $total_admins = count( $super_admins );
95
96                 $class = $role != 'super' ? ' class="current"' : '';
97                 $role_links = array();
98                 $role_links['all'] = "<a href='" . network_admin_url('users.php') . "'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_users, 'users' ), number_format_i18n( $total_users ) ) . '</a>';
99                 $class = $role == 'super' ? ' class="current"' : '';
100                 $role_links['super'] = "<a href='" . network_admin_url('users.php?role=super') . "'$class>" . sprintf( _n( 'Super Admin <span class="count">(%s)</span>', 'Super Admins <span class="count">(%s)</span>', $total_admins ), number_format_i18n( $total_admins ) ) . '</a>';
101
102                 return $role_links;
103         }
104
105         protected function pagination( $which ) {
106                 global $mode;
107
108                 parent::pagination ( $which );
109
110                 if ( 'top' == $which )
111                         $this->view_switcher( $mode );
112         }
113
114         public function get_columns() {
115                 $users_columns = array(
116                         'cb'         => '<input type="checkbox" />',
117                         'username'   => __( 'Username' ),
118                         'name'       => __( 'Name' ),
119                         'email'      => __( 'E-mail' ),
120                         'registered' => _x( 'Registered', 'user' ),
121                         'blogs'      => __( 'Sites' )
122                 );
123                 /**
124                  * Filter the columns displayed in the Network Admin Users list table.
125                  *
126                  * @since MU
127                  *
128                  * @param array $users_columns An array of user columns. Default 'cb', 'username',
129                  *                             'name', 'email', 'registered', 'blogs'.
130                  */
131                 $users_columns = apply_filters( 'wpmu_users_columns', $users_columns );
132
133                 return $users_columns;
134         }
135
136         protected function get_sortable_columns() {
137                 return array(
138                         'username'   => 'login',
139                         'name'       => 'name',
140                         'email'      => 'email',
141                         'registered' => 'id',
142                 );
143         }
144
145         public function display_rows() {
146                 global $mode;
147
148                 $alt = '';
149                 $super_admins = get_super_admins();
150                 foreach ( $this->items as $user ) {
151                         $alt = ( 'alternate' == $alt ) ? '' : 'alternate';
152
153                         $status_list = array( 'spam' => 'site-spammed', 'deleted' => 'site-deleted' );
154
155                         foreach ( $status_list as $status => $col ) {
156                                 if ( $user->$status )
157                                         $alt .= " $col";
158                         }
159
160                         ?>
161                         <tr class="<?php echo $alt; ?>">
162                         <?php
163
164                         list( $columns, $hidden ) = $this->get_column_info();
165
166                         foreach ( $columns as $column_name => $column_display_name ) :
167                                 $class = "class='$column_name column-$column_name'";
168
169                                 $style = '';
170                                 if ( in_array( $column_name, $hidden ) )
171                                         $style = ' style="display:none;"';
172
173                                 $attributes = "$class$style";
174
175                                 switch ( $column_name ) {
176                                         case 'cb': ?>
177                                                 <th scope="row" class="check-column">
178                                                         <label class="screen-reader-text" for="blog_<?php echo $user->ID; ?>"><?php echo sprintf( __( 'Select %s' ), $user->user_login ); ?></label>
179                                                         <input type="checkbox" id="blog_<?php echo $user->ID ?>" name="allusers[]" value="<?php echo esc_attr( $user->ID ) ?>" />
180                                                 </th>
181                                         <?php
182                                         break;
183
184                                         case 'username':
185                                                 $avatar = get_avatar( $user->user_email, 32 );
186                                                 $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user->ID ) ) );
187
188                                                 echo "<td $attributes>"; ?>
189                                                         <?php echo $avatar; ?><strong><a href="<?php echo $edit_link; ?>" class="edit"><?php echo $user->user_login; ?></a><?php
190                                                         if ( in_array( $user->user_login, $super_admins ) )
191                                                                 echo ' - ' . __( 'Super Admin' );
192                                                         ?></strong>
193                                                         <br/>
194                                                         <?php
195                                                                 $actions = array();
196                                                                 $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
197
198                                                                 if ( current_user_can( 'delete_user', $user->ID ) && ! in_array( $user->user_login, $super_admins ) ) {
199                                                                         $actions['delete'] = '<a href="' . $delete = esc_url( network_admin_url( add_query_arg( '_wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), wp_nonce_url( 'users.php', 'deleteuser' ) . '&amp;action=deleteuser&amp;id=' . $user->ID ) ) ) . '" class="delete">' . __( 'Delete' ) . '</a>';
200                                                                 }
201
202                                                                 /**
203                                                                  * Filter the action links displayed under each user
204                                                                  * in the Network Admin Users list table.
205                                                                  *
206                                                                  * @since 3.2.0
207                                                                  *
208                                                                  * @param array   $actions An array of action links to be displayed.
209                                                                  *                         Default 'Edit', 'Delete'.
210                                                                  * @param WP_User $user    WP_User object.
211                                                                  */
212                                                                 $actions = apply_filters( 'ms_user_row_actions', $actions, $user );
213                                                                 echo $this->row_actions( $actions );
214                                                         ?>
215                                                 </td>
216                                         <?php
217                                         break;
218
219                                         case 'name':
220                                                 echo "<td $attributes>$user->first_name $user->last_name</td>";
221                                         break;
222
223                                         case 'email':
224                                                 echo "<td $attributes><a href='mailto:$user->user_email'>$user->user_email</a></td>";
225                                         break;
226
227                                         case 'registered':
228                                                 if ( 'list' == $mode )
229                                                         $date = 'Y/m/d';
230                                                 else
231                                                         $date = 'Y/m/d \<\b\r \/\> g:i:s a';
232
233                                                 echo "<td $attributes>" . mysql2date( $date, $user->user_registered ) . "</td>";
234                                         break;
235
236                                         case 'blogs':
237                                                 $blogs = get_blogs_of_user( $user->ID, true );
238                                                 echo "<td $attributes>";
239                                                         if ( is_array( $blogs ) ) {
240                                                                 foreach ( (array) $blogs as $key => $val ) {
241                                                                         if ( !can_edit_network( $val->site_id ) )
242                                                                                 continue;
243
244                                                                         $path   = ( $val->path == '/' ) ? '' : $val->path;
245                                                                         echo '<span class="site-' . $val->site_id . '" >';
246                                                                         echo '<a href="'. esc_url( network_admin_url( 'site-info.php?id=' . $val->userblog_id ) ) .'">' . str_replace( '.' . get_current_site()->domain, '', $val->domain . $path ) . '</a>';
247                                                                         echo ' <small class="row-actions">';
248                                                                         $actions = array();
249                                                                         $actions['edit'] = '<a href="'. esc_url( network_admin_url( 'site-info.php?id=' . $val->userblog_id ) ) .'">' . __( 'Edit' ) . '</a>';
250
251                                                                         $class = '';
252                                                                         if ( get_blog_status( $val->userblog_id, 'spam' ) == 1 )
253                                                                                 $class .= 'site-spammed ';
254                                                                         if ( get_blog_status( $val->userblog_id, 'mature' ) == 1 )
255                                                                                 $class .= 'site-mature ';
256                                                                         if ( get_blog_status( $val->userblog_id, 'deleted' ) == 1 )
257                                                                                 $class .= 'site-deleted ';
258                                                                         if ( get_blog_status( $val->userblog_id, 'archived' ) == 1 )
259                                                                                 $class .= 'site-archived ';
260
261                                                                         $actions['view'] = '<a class="' . $class . '" href="' . esc_url( get_home_url( $val->userblog_id ) ) . '">' . __( 'View' ) . '</a>';
262
263                                                                         /**
264                                                                          * Filter the action links displayed next the sites a user belongs to
265                                                                          * in the Network Admin Users list table.
266                                                                          *
267                                                                          * @since 3.1.0
268                                                                          *
269                                                                          * @param array $actions     An array of action links to be displayed.
270                                                                          *                           Default 'Edit', 'View'.
271                                                                          * @param int   $userblog_id The site ID.
272                                                                          */
273                                                                         $actions = apply_filters( 'ms_user_list_site_actions', $actions, $val->userblog_id );
274
275                                                                         $i=0;
276                                                                         $action_count = count( $actions );
277                                                                         foreach ( $actions as $action => $link ) {
278                                                                                 ++$i;
279                                                                                 ( $i == $action_count ) ? $sep = '' : $sep = ' | ';
280                                                                                 echo "<span class='$action'>$link$sep</span>";
281                                                                         }
282                                                                         echo '</small></span><br/>';
283                                                                 }
284                                                         }
285                                                         ?>
286                                                 </td>
287                                         <?php
288                                         break;
289
290                                         default:
291                                                 echo "<td $attributes>";
292                                                 /** This filter is documented in wp-admin/includes/class-wp-users-list-table.php */
293                                                 echo apply_filters( 'manage_users_custom_column', '', $column_name, $user->ID );
294                                                 echo "</td>";
295                                         break;
296                                 }
297                         endforeach
298                         ?>
299                         </tr>
300                         <?php
301                 }
302         }
303 }