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

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