[ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * List Table API: WP_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. 12 * 13 * @since 3.1.0 14 * @access private 15 * 16 * @see WP_List_Table 17 */ 18 class WP_Users_List_Table extends WP_List_Table { 19 20 /** 21 * Site ID to generate the Users list table for. 22 * 23 * @since 3.1.0 24 * @var int 25 */ 26 public $site_id; 27 28 /** 29 * Whether or not the current Users list table is for Multisite. 30 * 31 * @since 3.1.0 32 * @var bool 33 */ 34 public $is_site_users; 35 36 /** 37 * Constructor. 38 * 39 * @since 3.1.0 40 * 41 * @see WP_List_Table::__construct() for more information on default arguments. 42 * 43 * @param array $args An associative array of arguments. 44 */ 45 public function __construct( $args = array() ) { 46 parent::__construct( 47 array( 48 'singular' => 'user', 49 'plural' => 'users', 50 'screen' => isset( $args['screen'] ) ? $args['screen'] : null, 51 ) 52 ); 53 54 $this->is_site_users = 'site-users-network' === $this->screen->id; 55 56 if ( $this->is_site_users ) { 57 $this->site_id = isset( $_REQUEST['id'] ) ? (int) $_REQUEST['id'] : 0; 58 } 59 } 60 61 /** 62 * Check the current user's permissions. 63 * 64 * @since 3.1.0 65 * 66 * @return bool 67 */ 68 public function ajax_user_can() { 69 if ( $this->is_site_users ) { 70 return current_user_can( 'manage_sites' ); 71 } else { 72 return current_user_can( 'list_users' ); 73 } 74 } 75 76 /** 77 * Prepare the users list for display. 78 * 79 * @since 3.1.0 80 * 81 * @global string $role 82 * @global string $usersearch 83 */ 84 public function prepare_items() { 85 global $role, $usersearch; 86 87 $usersearch = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : ''; 88 89 $role = isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : ''; 90 91 $per_page = ( $this->is_site_users ) ? 'site_users_network_per_page' : 'users_per_page'; 92 $users_per_page = $this->get_items_per_page( $per_page ); 93 94 $paged = $this->get_pagenum(); 95 96 if ( 'none' === $role ) { 97 $args = array( 98 'number' => $users_per_page, 99 'offset' => ( $paged - 1 ) * $users_per_page, 100 'include' => wp_get_users_with_no_role( $this->site_id ), 101 'search' => $usersearch, 102 'fields' => 'all_with_meta', 103 ); 104 } else { 105 $args = array( 106 'number' => $users_per_page, 107 'offset' => ( $paged - 1 ) * $users_per_page, 108 'role' => $role, 109 'search' => $usersearch, 110 'fields' => 'all_with_meta', 111 ); 112 } 113 114 if ( '' !== $args['search'] ) { 115 $args['search'] = '*' . $args['search'] . '*'; 116 } 117 118 if ( $this->is_site_users ) { 119 $args['blog_id'] = $this->site_id; 120 } 121 122 if ( isset( $_REQUEST['orderby'] ) ) { 123 $args['orderby'] = $_REQUEST['orderby']; 124 } 125 126 if ( isset( $_REQUEST['order'] ) ) { 127 $args['order'] = $_REQUEST['order']; 128 } 129 130 /** 131 * Filters the query arguments used to retrieve users for the current users list table. 132 * 133 * @since 4.4.0 134 * 135 * @param array $args Arguments passed to WP_User_Query to retrieve items for the current 136 * users list table. 137 */ 138 $args = apply_filters( 'users_list_table_query_args', $args ); 139 140 // Query the user IDs for this page. 141 $wp_user_search = new WP_User_Query( $args ); 142 143 $this->items = $wp_user_search->get_results(); 144 145 $this->set_pagination_args( 146 array( 147 'total_items' => $wp_user_search->get_total(), 148 'per_page' => $users_per_page, 149 ) 150 ); 151 } 152 153 /** 154 * Output 'no users' message. 155 * 156 * @since 3.1.0 157 */ 158 public function no_items() { 159 _e( 'No users found.' ); 160 } 161 162 /** 163 * Return an associative array listing all the views that can be used 164 * with this table. 165 * 166 * Provides a list of roles and user count for that role for easy 167 * Filtersing of the user table. 168 * 169 * @since 3.1.0 170 * 171 * @global string $role 172 * 173 * @return string[] An array of HTML links keyed by their view. 174 */ 175 protected function get_views() { 176 global $role; 177 178 $wp_roles = wp_roles(); 179 180 $count_users = ! wp_is_large_user_count(); 181 182 if ( $this->is_site_users ) { 183 $url = 'site-users.php?id=' . $this->site_id; 184 } else { 185 $url = 'users.php'; 186 } 187 188 $role_links = array(); 189 $avail_roles = array(); 190 $all_text = __( 'All' ); 191 $current_link_attributes = empty( $role ) ? ' class="current" aria-current="page"' : ''; 192 193 if ( $count_users ) { 194 if ( $this->is_site_users ) { 195 switch_to_blog( $this->site_id ); 196 $users_of_blog = count_users( 'time', $this->site_id ); 197 restore_current_blog(); 198 } else { 199 $users_of_blog = count_users(); 200 } 201 202 $total_users = $users_of_blog['total_users']; 203 $avail_roles =& $users_of_blog['avail_roles']; 204 unset( $users_of_blog ); 205 206 $all_text = sprintf( 207 /* translators: %s: Number of users. */ 208 _nx( 209 'All <span class="count">(%s)</span>', 210 'All <span class="count">(%s)</span>', 211 $total_users, 212 'users' 213 ), 214 number_format_i18n( $total_users ) 215 ); 216 } 217 218 $role_links['all'] = sprintf( '<a href="%s"%s>%s</a>', $url, $current_link_attributes, $all_text ); 219 220 foreach ( $wp_roles->get_names() as $this_role => $name ) { 221 if ( $count_users && ! isset( $avail_roles[ $this_role ] ) ) { 222 continue; 223 } 224 225 $current_link_attributes = ''; 226 227 if ( $this_role === $role ) { 228 $current_link_attributes = ' class="current" aria-current="page"'; 229 } 230 231 $name = translate_user_role( $name ); 232 if ( $count_users ) { 233 $name = sprintf( 234 /* translators: 1: User role name, 2: Number of users. */ 235 __( '%1$s <span class="count">(%2$s)</span>' ), 236 $name, 237 number_format_i18n( $avail_roles[ $this_role ] ) 238 ); 239 } 240 241 $role_links[ $this_role ] = "<a href='" . esc_url( add_query_arg( 'role', $this_role, $url ) ) . "'$current_link_attributes>$name</a>"; 242 } 243 244 if ( ! empty( $avail_roles['none'] ) ) { 245 246 $current_link_attributes = ''; 247 248 if ( 'none' === $role ) { 249 $current_link_attributes = ' class="current" aria-current="page"'; 250 } 251 252 $name = __( 'No role' ); 253 $name = sprintf( 254 /* translators: 1: User role name, 2: Number of users. */ 255 __( '%1$s <span class="count">(%2$s)</span>' ), 256 $name, 257 number_format_i18n( $avail_roles['none'] ) 258 ); 259 260 $role_links['none'] = "<a href='" . esc_url( add_query_arg( 'role', 'none', $url ) ) . "'$current_link_attributes>$name</a>"; 261 } 262 263 return $role_links; 264 } 265 266 /** 267 * Retrieve an associative array of bulk actions available on this table. 268 * 269 * @since 3.1.0 270 * 271 * @return array Array of bulk action labels keyed by their action. 272 */ 273 protected function get_bulk_actions() { 274 $actions = array(); 275 276 if ( is_multisite() ) { 277 if ( current_user_can( 'remove_users' ) ) { 278 $actions['remove'] = __( 'Remove' ); 279 } 280 } else { 281 if ( current_user_can( 'delete_users' ) ) { 282 $actions['delete'] = __( 'Delete' ); 283 } 284 } 285 286 // Add a password reset link to the bulk actions dropdown. 287 if ( current_user_can( 'edit_users' ) ) { 288 $actions['resetpassword'] = __( 'Send password reset' ); 289 } 290 291 return $actions; 292 } 293 294 /** 295 * Output the controls to allow user roles to be changed in bulk. 296 * 297 * @since 3.1.0 298 * 299 * @param string $which Whether this is being invoked above ("top") 300 * or below the table ("bottom"). 301 */ 302 protected function extra_tablenav( $which ) { 303 $id = 'bottom' === $which ? 'new_role2' : 'new_role'; 304 $button_id = 'bottom' === $which ? 'changeit2' : 'changeit'; 305 ?> 306 <div class="alignleft actions"> 307 <?php if ( current_user_can( 'promote_users' ) && $this->has_items() ) : ?> 308 <label class="screen-reader-text" for="<?php echo $id; ?>"><?php _e( 'Change role to…' ); ?></label> 309 <select name="<?php echo $id; ?>" id="<?php echo $id; ?>"> 310 <option value=""><?php _e( 'Change role to…' ); ?></option> 311 <?php wp_dropdown_roles(); ?> 312 <option value="none"><?php _e( '— No role for this site —' ); ?></option> 313 </select> 314 <?php 315 submit_button( __( 'Change' ), '', $button_id, false ); 316 endif; 317 318 /** 319 * Fires just before the closing div containing the bulk role-change controls 320 * in the Users list table. 321 * 322 * @since 3.5.0 323 * @since 4.6.0 The `$which` parameter was added. 324 * 325 * @param string $which The location of the extra table nav markup: 'top' or 'bottom'. 326 */ 327 do_action( 'restrict_manage_users', $which ); 328 ?> 329 </div> 330 <?php 331 /** 332 * Fires immediately following the closing "actions" div in the tablenav for the users 333 * list table. 334 * 335 * @since 4.9.0 336 * 337 * @param string $which The location of the extra table nav markup: 'top' or 'bottom'. 338 */ 339 do_action( 'manage_users_extra_tablenav', $which ); 340 } 341 342 /** 343 * Capture the bulk action required, and return it. 344 * 345 * Overridden from the base class implementation to capture 346 * the role change drop-down. 347 * 348 * @since 3.1.0 349 * 350 * @return string The bulk action required. 351 */ 352 public function current_action() { 353 if ( isset( $_REQUEST['changeit'] ) && ! empty( $_REQUEST['new_role'] ) ) { 354 return 'promote'; 355 } 356 357 return parent::current_action(); 358 } 359 360 /** 361 * Get a list of columns for the list table. 362 * 363 * @since 3.1.0 364 * 365 * @return string[] Array of column titles keyed by their column name. 366 */ 367 public function get_columns() { 368 $c = array( 369 'cb' => '<input type="checkbox" />', 370 'username' => __( 'Username' ), 371 'name' => __( 'Name' ), 372 'email' => __( 'Email' ), 373 'role' => __( 'Role' ), 374 'posts' => _x( 'Posts', 'post type general name' ), 375 ); 376 377 if ( $this->is_site_users ) { 378 unset( $c['posts'] ); 379 } 380 381 return $c; 382 } 383 384 /** 385 * Get a list of sortable columns for the list table. 386 * 387 * @since 3.1.0 388 * 389 * @return array Array of sortable columns. 390 */ 391 protected function get_sortable_columns() { 392 $c = array( 393 'username' => 'login', 394 'email' => 'email', 395 ); 396 397 return $c; 398 } 399 400 /** 401 * Generate the list table rows. 402 * 403 * @since 3.1.0 404 */ 405 public function display_rows() { 406 // Query the post counts for this page. 407 if ( ! $this->is_site_users ) { 408 $post_counts = count_many_users_posts( array_keys( $this->items ) ); 409 } 410 411 foreach ( $this->items as $userid => $user_object ) { 412 echo "\n\t" . $this->single_row( $user_object, '', '', isset( $post_counts ) ? $post_counts[ $userid ] : 0 ); 413 } 414 } 415 416 /** 417 * Generate HTML for a single row on the users.php admin panel. 418 * 419 * @since 3.1.0 420 * @since 4.2.0 The `$style` parameter was deprecated. 421 * @since 4.4.0 The `$role` parameter was deprecated. 422 * 423 * @param WP_User $user_object The current user object. 424 * @param string $style Deprecated. Not used. 425 * @param string $role Deprecated. Not used. 426 * @param int $numposts Optional. Post count to display for this user. Defaults 427 * to zero, as in, a new user has made zero posts. 428 * @return string Output for a single row. 429 */ 430 public function single_row( $user_object, $style = '', $role = '', $numposts = 0 ) { 431 if ( ! ( $user_object instanceof WP_User ) ) { 432 $user_object = get_userdata( (int) $user_object ); 433 } 434 $user_object->filter = 'display'; 435 $email = $user_object->user_email; 436 437 if ( $this->is_site_users ) { 438 $url = "site-users.php?id={$this->site_id}&"; 439 } else { 440 $url = 'users.php?'; 441 } 442 443 $user_roles = $this->get_role_list( $user_object ); 444 445 // Set up the hover actions for this user. 446 $actions = array(); 447 $checkbox = ''; 448 $super_admin = ''; 449 450 if ( is_multisite() && current_user_can( 'manage_network_users' ) ) { 451 if ( in_array( $user_object->user_login, get_super_admins(), true ) ) { 452 $super_admin = ' — ' . __( 'Super Admin' ); 453 } 454 } 455 456 // Check if the user for this row is editable. 457 if ( current_user_can( 'list_users' ) ) { 458 // Set up the user editing link. 459 $edit_link = esc_url( 460 add_query_arg( 461 'wp_http_referer', 462 urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 463 get_edit_user_link( $user_object->ID ) 464 ) 465 ); 466 467 if ( current_user_can( 'edit_user', $user_object->ID ) ) { 468 $edit = "<strong><a href=\"{$edit_link}\">{$user_object->user_login}</a>{$super_admin}</strong><br />"; 469 $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>'; 470 } else { 471 $edit = "<strong>{$user_object->user_login}{$super_admin}</strong><br />"; 472 } 473 474 if ( ! is_multisite() 475 && get_current_user_id() !== $user_object->ID 476 && current_user_can( 'delete_user', $user_object->ID ) 477 ) { 478 $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url( "users.php?action=delete&user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Delete' ) . '</a>'; 479 } 480 481 if ( is_multisite() 482 && current_user_can( 'remove_user', $user_object->ID ) 483 ) { 484 $actions['remove'] = "<a class='submitdelete' href='" . wp_nonce_url( $url . "action=remove&user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Remove' ) . '</a>'; 485 } 486 487 // Add a link to the user's author archive, if not empty. 488 $author_posts_url = get_author_posts_url( $user_object->ID ); 489 if ( $author_posts_url ) { 490 $actions['view'] = sprintf( 491 '<a href="%s" aria-label="%s">%s</a>', 492 esc_url( $author_posts_url ), 493 /* translators: %s: Author's display name. */ 494 esc_attr( sprintf( __( 'View posts by %s' ), $user_object->display_name ) ), 495 __( 'View' ) 496 ); 497 } 498 499 // Add a link to send the user a reset password link by email. 500 if ( get_current_user_id() !== $user_object->ID 501 && current_user_can( 'edit_user', $user_object->ID ) 502 ) { 503 $actions['resetpassword'] = "<a class='resetpassword' href='" . wp_nonce_url( "users.php?action=resetpassword&users=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Send password reset' ) . '</a>'; 504 } 505 506 /** 507 * Filters the action links displayed under each user in the Users list table. 508 * 509 * @since 2.8.0 510 * 511 * @param string[] $actions An array of action links to be displayed. 512 * Default 'Edit', 'Delete' for single site, and 513 * 'Edit', 'Remove' for Multisite. 514 * @param WP_User $user_object WP_User object for the currently listed user. 515 */ 516 $actions = apply_filters( 'user_row_actions', $actions, $user_object ); 517 518 // Role classes. 519 $role_classes = esc_attr( implode( ' ', array_keys( $user_roles ) ) ); 520 521 // Set up the checkbox (because the user is editable, otherwise it's empty). 522 $checkbox = sprintf( 523 '<label class="screen-reader-text" for="user_%1$s">%2$s</label>' . 524 '<input type="checkbox" name="users[]" id="user_%1$s" class="%3$s" value="%1$s" />', 525 $user_object->ID, 526 /* translators: %s: User login. */ 527 sprintf( __( 'Select %s' ), $user_object->user_login ), 528 $role_classes 529 ); 530 531 } else { 532 $edit = "<strong>{$user_object->user_login}{$super_admin}</strong>"; 533 } 534 535 $avatar = get_avatar( $user_object->ID, 32 ); 536 537 // Comma-separated list of user roles. 538 $roles_list = implode( ', ', $user_roles ); 539 540 $r = "<tr id='user-$user_object->ID'>"; 541 542 list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info(); 543 544 foreach ( $columns as $column_name => $column_display_name ) { 545 $classes = "$column_name column-$column_name"; 546 if ( $primary === $column_name ) { 547 $classes .= ' has-row-actions column-primary'; 548 } 549 if ( 'posts' === $column_name ) { 550 $classes .= ' num'; // Special case for that column. 551 } 552 553 if ( in_array( $column_name, $hidden, true ) ) { 554 $classes .= ' hidden'; 555 } 556 557 $data = 'data-colname="' . esc_attr( wp_strip_all_tags( $column_display_name ) ) . '"'; 558 559 $attributes = "class='$classes' $data"; 560 561 if ( 'cb' === $column_name ) { 562 $r .= "<th scope='row' class='check-column'>$checkbox</th>"; 563 } else { 564 $r .= "<td $attributes>"; 565 switch ( $column_name ) { 566 case 'username': 567 $r .= "$avatar $edit"; 568 break; 569 case 'name': 570 if ( $user_object->first_name && $user_object->last_name ) { 571 $r .= "$user_object->first_name $user_object->last_name"; 572 } elseif ( $user_object->first_name ) { 573 $r .= $user_object->first_name; 574 } elseif ( $user_object->last_name ) { 575 $r .= $user_object->last_name; 576 } else { 577 $r .= sprintf( 578 '<span aria-hidden="true">—</span><span class="screen-reader-text">%s</span>', 579 _x( 'Unknown', 'name' ) 580 ); 581 } 582 break; 583 case 'email': 584 $r .= "<a href='" . esc_url( "mailto:$email" ) . "'>$email</a>"; 585 break; 586 case 'role': 587 $r .= esc_html( $roles_list ); 588 break; 589 case 'posts': 590 if ( $numposts > 0 ) { 591 $r .= sprintf( 592 '<a href="%s" class="edit"><span aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></a>', 593 "edit.php?author={$user_object->ID}", 594 $numposts, 595 sprintf( 596 /* translators: %s: Number of posts. */ 597 _n( '%s post by this author', '%s posts by this author', $numposts ), 598 number_format_i18n( $numposts ) 599 ) 600 ); 601 } else { 602 $r .= 0; 603 } 604 break; 605 default: 606 /** 607 * Filters the display output of custom columns in the Users list table. 608 * 609 * @since 2.8.0 610 * 611 * @param string $output Custom column output. Default empty. 612 * @param string $column_name Column name. 613 * @param int $user_id ID of the currently-listed user. 614 */ 615 $r .= apply_filters( 'manage_users_custom_column', '', $column_name, $user_object->ID ); 616 } 617 618 if ( $primary === $column_name ) { 619 $r .= $this->row_actions( $actions ); 620 } 621 $r .= '</td>'; 622 } 623 } 624 $r .= '</tr>'; 625 626 return $r; 627 } 628 629 /** 630 * Gets the name of the default primary column. 631 * 632 * @since 4.3.0 633 * 634 * @return string Name of the default primary column, in this case, 'username'. 635 */ 636 protected function get_default_primary_column_name() { 637 return 'username'; 638 } 639 640 /** 641 * Returns an array of translated user role names for a given user object. 642 * 643 * @since 4.4.0 644 * 645 * @param WP_User $user_object The WP_User object. 646 * @return string[] An array of user role names keyed by role. 647 */ 648 protected function get_role_list( $user_object ) { 649 $wp_roles = wp_roles(); 650 651 $role_list = array(); 652 653 foreach ( $user_object->roles as $role ) { 654 if ( isset( $wp_roles->role_names[ $role ] ) ) { 655 $role_list[ $role ] = translate_user_role( $wp_roles->role_names[ $role ] ); 656 } 657 } 658 659 if ( empty( $role_list ) ) { 660 $role_list['none'] = _x( 'None', 'no user roles' ); 661 } 662 663 /** 664 * Filters the returned array of translated role names for a user. 665 * 666 * @since 4.4.0 667 * 668 * @param string[] $role_list An array of translated user role names keyed by role. 669 * @param WP_User $user_object A WP_User object. 670 */ 671 return apply_filters( 'get_role_list', $role_list, $user_object ); 672 } 673 674 }
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 |