[ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * List Table API: WP_MS_Users_List_Table class 4 * 5 * @package WordPress 6 * @subpackage Administration 7 * @since 3.1.0 8 */ 9 10 /** 11 * Core class used to implement displaying users in a list table for the network admin. 12 * 13 * @since 3.1.0 14 * @access private 15 * 16 * @see WP_List_Table 17 */ 18 class WP_MS_Users_List_Table extends WP_List_Table { 19 /** 20 * @return bool 21 */ 22 public function ajax_user_can() { 23 return current_user_can( 'manage_network_users' ); 24 } 25 26 /** 27 * @global string $mode List table view mode. 28 * @global string $usersearch 29 * @global string $role 30 */ 31 public function prepare_items() { 32 global $mode, $usersearch, $role; 33 34 if ( ! empty( $_REQUEST['mode'] ) ) { 35 $mode = 'excerpt' === $_REQUEST['mode'] ? 'excerpt' : 'list'; 36 set_user_setting( 'network_users_list_mode', $mode ); 37 } else { 38 $mode = get_user_setting( 'network_users_list_mode', 'list' ); 39 } 40 41 $usersearch = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : ''; 42 43 $users_per_page = $this->get_items_per_page( 'users_network_per_page' ); 44 45 $role = isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : ''; 46 47 $paged = $this->get_pagenum(); 48 49 $args = array( 50 'number' => $users_per_page, 51 'offset' => ( $paged - 1 ) * $users_per_page, 52 'search' => $usersearch, 53 'blog_id' => 0, 54 'fields' => 'all_with_meta', 55 ); 56 57 if ( wp_is_large_network( 'users' ) ) { 58 $args['search'] = ltrim( $args['search'], '*' ); 59 } elseif ( '' !== $args['search'] ) { 60 $args['search'] = trim( $args['search'], '*' ); 61 $args['search'] = '*' . $args['search'] . '*'; 62 } 63 64 if ( 'super' === $role ) { 65 $args['login__in'] = get_super_admins(); 66 } 67 68 /* 69 * If the network is large and a search is not being performed, 70 * show only the latest users with no paging in order to avoid 71 * expensive count queries. 72 */ 73 if ( ! $usersearch && wp_is_large_network( 'users' ) ) { 74 if ( ! isset( $_REQUEST['orderby'] ) ) { 75 $_GET['orderby'] = 'id'; 76 $_REQUEST['orderby'] = 'id'; 77 } 78 if ( ! isset( $_REQUEST['order'] ) ) { 79 $_GET['order'] = 'DESC'; 80 $_REQUEST['order'] = 'DESC'; 81 } 82 $args['count_total'] = false; 83 } 84 85 if ( isset( $_REQUEST['orderby'] ) ) { 86 $args['orderby'] = $_REQUEST['orderby']; 87 } 88 89 if ( isset( $_REQUEST['order'] ) ) { 90 $args['order'] = $_REQUEST['order']; 91 } 92 93 /** This filter is documented in wp-admin/includes/class-wp-users-list-table.php */ 94 $args = apply_filters( 'users_list_table_query_args', $args ); 95 96 // Query the user IDs for this page. 97 $wp_user_search = new WP_User_Query( $args ); 98 99 $this->items = $wp_user_search->get_results(); 100 101 $this->set_pagination_args( 102 array( 103 'total_items' => $wp_user_search->get_total(), 104 'per_page' => $users_per_page, 105 ) 106 ); 107 } 108 109 /** 110 * @return array 111 */ 112 protected function get_bulk_actions() { 113 $actions = array(); 114 if ( current_user_can( 'delete_users' ) ) { 115 $actions['delete'] = __( 'Delete' ); 116 } 117 $actions['spam'] = _x( 'Mark as spam', 'user' ); 118 $actions['notspam'] = _x( 'Not spam', 'user' ); 119 120 return $actions; 121 } 122 123 /** 124 */ 125 public function no_items() { 126 _e( 'No users found.' ); 127 } 128 129 /** 130 * @global string $role 131 * @return array 132 */ 133 protected function get_views() { 134 global $role; 135 136 $total_users = get_user_count(); 137 $super_admins = get_super_admins(); 138 $total_admins = count( $super_admins ); 139 140 $current_link_attributes = 'super' !== $role ? ' class="current" aria-current="page"' : ''; 141 $role_links = array(); 142 $role_links['all'] = sprintf( 143 '<a href="%s"%s>%s</a>', 144 network_admin_url( 'users.php' ), 145 $current_link_attributes, 146 sprintf( 147 /* translators: Number of users. */ 148 _nx( 149 'All <span class="count">(%s)</span>', 150 'All <span class="count">(%s)</span>', 151 $total_users, 152 'users' 153 ), 154 number_format_i18n( $total_users ) 155 ) 156 ); 157 $current_link_attributes = 'super' === $role ? ' class="current" aria-current="page"' : ''; 158 $role_links['super'] = sprintf( 159 '<a href="%s"%s>%s</a>', 160 network_admin_url( 'users.php?role=super' ), 161 $current_link_attributes, 162 sprintf( 163 /* translators: Number of users. */ 164 _n( 165 'Super Admin <span class="count">(%s)</span>', 166 'Super Admins <span class="count">(%s)</span>', 167 $total_admins 168 ), 169 number_format_i18n( $total_admins ) 170 ) 171 ); 172 173 return $role_links; 174 } 175 176 /** 177 * @global string $mode List table view mode. 178 * 179 * @param string $which 180 */ 181 protected function pagination( $which ) { 182 global $mode; 183 184 parent::pagination( $which ); 185 186 if ( 'top' === $which ) { 187 $this->view_switcher( $mode ); 188 } 189 } 190 191 /** 192 * @return array 193 */ 194 public function get_columns() { 195 $users_columns = array( 196 'cb' => '<input type="checkbox" />', 197 'username' => __( 'Username' ), 198 'name' => __( 'Name' ), 199 'email' => __( 'Email' ), 200 'registered' => _x( 'Registered', 'user' ), 201 'blogs' => __( 'Sites' ), 202 ); 203 /** 204 * Filters the columns displayed in the Network Admin Users list table. 205 * 206 * @since MU (3.0.0) 207 * 208 * @param string[] $users_columns An array of user columns. Default 'cb', 'username', 209 * 'name', 'email', 'registered', 'blogs'. 210 */ 211 return apply_filters( 'wpmu_users_columns', $users_columns ); 212 } 213 214 /** 215 * @return array 216 */ 217 protected function get_sortable_columns() { 218 return array( 219 'username' => 'login', 220 'name' => 'name', 221 'email' => 'email', 222 'registered' => 'id', 223 ); 224 } 225 226 /** 227 * Handles the checkbox column output. 228 * 229 * @since 4.3.0 230 * @since 5.9.0 Renamed `$user` to `$item` to match parent class for PHP 8 named parameter support. 231 * 232 * @param WP_User $item The current WP_User object. 233 */ 234 public function column_cb( $item ) { 235 // Restores the more descriptive, specific name for use within this method. 236 $user = $item; 237 238 if ( is_super_admin( $user->ID ) ) { 239 return; 240 } 241 ?> 242 <label class="screen-reader-text" for="blog_<?php echo $user->ID; ?>"> 243 <?php 244 /* translators: %s: User login. */ 245 printf( __( 'Select %s' ), $user->user_login ); 246 ?> 247 </label> 248 <input type="checkbox" id="blog_<?php echo $user->ID; ?>" name="allusers[]" value="<?php echo esc_attr( $user->ID ); ?>" /> 249 <?php 250 } 251 252 /** 253 * Handles the ID column output. 254 * 255 * @since 4.4.0 256 * 257 * @param WP_User $user The current WP_User object. 258 */ 259 public function column_id( $user ) { 260 echo $user->ID; 261 } 262 263 /** 264 * Handles the username column output. 265 * 266 * @since 4.3.0 267 * 268 * @param WP_User $user The current WP_User object. 269 */ 270 public function column_username( $user ) { 271 $super_admins = get_super_admins(); 272 $avatar = get_avatar( $user->user_email, 32 ); 273 274 echo $avatar; 275 276 if ( current_user_can( 'edit_user', $user->ID ) ) { 277 $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user->ID ) ) ); 278 $edit = "<a href=\"{$edit_link}\">{$user->user_login}</a>"; 279 } else { 280 $edit = $user->user_login; 281 } 282 283 ?> 284 <strong> 285 <?php 286 echo $edit; 287 288 if ( in_array( $user->user_login, $super_admins, true ) ) { 289 echo ' — ' . __( 'Super Admin' ); 290 } 291 ?> 292 </strong> 293 <?php 294 } 295 296 /** 297 * Handles the name column output. 298 * 299 * @since 4.3.0 300 * 301 * @param WP_User $user The current WP_User object. 302 */ 303 public function column_name( $user ) { 304 if ( $user->first_name && $user->last_name ) { 305 echo "$user->first_name $user->last_name"; 306 } elseif ( $user->first_name ) { 307 echo $user->first_name; 308 } elseif ( $user->last_name ) { 309 echo $user->last_name; 310 } else { 311 echo '<span aria-hidden="true">—</span><span class="screen-reader-text">' . _x( 'Unknown', 'name' ) . '</span>'; 312 } 313 } 314 315 /** 316 * Handles the email column output. 317 * 318 * @since 4.3.0 319 * 320 * @param WP_User $user The current WP_User object. 321 */ 322 public function column_email( $user ) { 323 echo "<a href='" . esc_url( "mailto:$user->user_email" ) . "'>$user->user_email</a>"; 324 } 325 326 /** 327 * Handles the registered date column output. 328 * 329 * @since 4.3.0 330 * 331 * @global string $mode List table view mode. 332 * 333 * @param WP_User $user The current WP_User object. 334 */ 335 public function column_registered( $user ) { 336 global $mode; 337 if ( 'list' === $mode ) { 338 $date = __( 'Y/m/d' ); 339 } else { 340 $date = __( 'Y/m/d g:i:s a' ); 341 } 342 echo mysql2date( $date, $user->user_registered ); 343 } 344 345 /** 346 * @since 4.3.0 347 * 348 * @param WP_User $user 349 * @param string $classes 350 * @param string $data 351 * @param string $primary 352 */ 353 protected function _column_blogs( $user, $classes, $data, $primary ) { 354 echo '<td class="', $classes, ' has-row-actions" ', $data, '>'; 355 echo $this->column_blogs( $user ); 356 echo $this->handle_row_actions( $user, 'blogs', $primary ); 357 echo '</td>'; 358 } 359 360 /** 361 * Handles the sites column output. 362 * 363 * @since 4.3.0 364 * 365 * @param WP_User $user The current WP_User object. 366 */ 367 public function column_blogs( $user ) { 368 $blogs = get_blogs_of_user( $user->ID, true ); 369 if ( ! is_array( $blogs ) ) { 370 return; 371 } 372 373 foreach ( $blogs as $site ) { 374 if ( ! can_edit_network( $site->site_id ) ) { 375 continue; 376 } 377 378 $path = ( '/' === $site->path ) ? '' : $site->path; 379 $site_classes = array( 'site-' . $site->site_id ); 380 /** 381 * Filters the span class for a site listing on the mulisite user list table. 382 * 383 * @since 5.2.0 384 * 385 * @param string[] $site_classes Array of class names used within the span tag. Default "site-#" with the site's network ID. 386 * @param int $site_id Site ID. 387 * @param int $network_id Network ID. 388 * @param WP_User $user WP_User object. 389 */ 390 $site_classes = apply_filters( 'ms_user_list_site_class', $site_classes, $site->userblog_id, $site->site_id, $user ); 391 if ( is_array( $site_classes ) && ! empty( $site_classes ) ) { 392 $site_classes = array_map( 'sanitize_html_class', array_unique( $site_classes ) ); 393 echo '<span class="' . esc_attr( implode( ' ', $site_classes ) ) . '">'; 394 } else { 395 echo '<span>'; 396 } 397 echo '<a href="' . esc_url( network_admin_url( 'site-info.php?id=' . $site->userblog_id ) ) . '">' . str_replace( '.' . get_network()->domain, '', $site->domain . $path ) . '</a>'; 398 echo ' <small class="row-actions">'; 399 $actions = array(); 400 $actions['edit'] = '<a href="' . esc_url( network_admin_url( 'site-info.php?id=' . $site->userblog_id ) ) . '">' . __( 'Edit' ) . '</a>'; 401 402 $class = ''; 403 if ( 1 === (int) $site->spam ) { 404 $class .= 'site-spammed '; 405 } 406 if ( 1 === (int) $site->mature ) { 407 $class .= 'site-mature '; 408 } 409 if ( 1 === (int) $site->deleted ) { 410 $class .= 'site-deleted '; 411 } 412 if ( 1 === (int) $site->archived ) { 413 $class .= 'site-archived '; 414 } 415 416 $actions['view'] = '<a class="' . $class . '" href="' . esc_url( get_home_url( $site->userblog_id ) ) . '">' . __( 'View' ) . '</a>'; 417 418 /** 419 * Filters the action links displayed next the sites a user belongs to 420 * in the Network Admin Users list table. 421 * 422 * @since 3.1.0 423 * 424 * @param string[] $actions An array of action links to be displayed. Default 'Edit', 'View'. 425 * @param int $userblog_id The site ID. 426 */ 427 $actions = apply_filters( 'ms_user_list_site_actions', $actions, $site->userblog_id ); 428 429 $action_count = count( $actions ); 430 431 $i = 0; 432 433 foreach ( $actions as $action => $link ) { 434 ++$i; 435 436 $sep = ( $i < $action_count ) ? ' | ' : ''; 437 438 echo "<span class='$action'>$link$sep</span>"; 439 } 440 441 echo '</small></span><br/>'; 442 } 443 } 444 445 /** 446 * Handles the default column output. 447 * 448 * @since 4.3.0 449 * @since 5.9.0 Renamed `$user` to `$item` to match parent class for PHP 8 named parameter support. 450 * 451 * @param WP_User $item The current WP_User object. 452 * @param string $column_name The current column name. 453 */ 454 public function column_default( $item, $column_name ) { 455 /** This filter is documented in wp-admin/includes/class-wp-users-list-table.php */ 456 echo apply_filters( 457 'manage_users_custom_column', 458 '', // Custom column output. Default empty. 459 $column_name, 460 $item->ID // User ID. 461 ); 462 } 463 464 public function display_rows() { 465 foreach ( $this->items as $user ) { 466 $class = ''; 467 468 $status_list = array( 469 'spam' => 'site-spammed', 470 'deleted' => 'site-deleted', 471 ); 472 473 foreach ( $status_list as $status => $col ) { 474 if ( $user->$status ) { 475 $class .= " $col"; 476 } 477 } 478 479 ?> 480 <tr class="<?php echo trim( $class ); ?>"> 481 <?php $this->single_row_columns( $user ); ?> 482 </tr> 483 <?php 484 } 485 } 486 487 /** 488 * Gets the name of the default primary column. 489 * 490 * @since 4.3.0 491 * 492 * @return string Name of the default primary column, in this case, 'username'. 493 */ 494 protected function get_default_primary_column_name() { 495 return 'username'; 496 } 497 498 /** 499 * Generates and displays row action links. 500 * 501 * @since 4.3.0 502 * @since 5.9.0 Renamed `$user` to `$item` to match parent class for PHP 8 named parameter support. 503 * 504 * @param WP_User $item User being acted upon. 505 * @param string $column_name Current column name. 506 * @param string $primary Primary column name. 507 * @return string Row actions output for users in Multisite, or an empty string 508 * if the current column is not the primary column. 509 */ 510 protected function handle_row_actions( $item, $column_name, $primary ) { 511 if ( $primary !== $column_name ) { 512 return ''; 513 } 514 515 // Restores the more descriptive, specific name for use within this method. 516 $user = $item; 517 $super_admins = get_super_admins(); 518 519 $actions = array(); 520 521 if ( current_user_can( 'edit_user', $user->ID ) ) { 522 $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user->ID ) ) ); 523 $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>'; 524 } 525 526 if ( current_user_can( 'delete_user', $user->ID ) && ! in_array( $user->user_login, $super_admins, true ) ) { 527 $actions['delete'] = '<a href="' . esc_url( network_admin_url( add_query_arg( '_wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), wp_nonce_url( 'users.php', 'deleteuser' ) . '&action=deleteuser&id=' . $user->ID ) ) ) . '" class="delete">' . __( 'Delete' ) . '</a>'; 528 } 529 530 /** 531 * Filters the action links displayed under each user in the Network Admin Users list table. 532 * 533 * @since 3.2.0 534 * 535 * @param string[] $actions An array of action links to be displayed. Default 'Edit', 'Delete'. 536 * @param WP_User $user WP_User object. 537 */ 538 $actions = apply_filters( 'ms_user_row_actions', $actions, $user ); 539 540 return $this->row_actions( $actions ); 541 } 542 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Dec 22 01:00:02 2024 | Cross-referenced by PHPXref 0.7.1 |