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

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