class-wp-users-list-table.php 18 KB

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