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

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