Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

563 lignes
16 KiB

  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. ?>
  222. <div class="alignleft actions">
  223. <?php if ( current_user_can( 'promote_users' ) && $this->has_items() ) : ?>
  224. <label class="screen-reader-text" for="<?php echo $id ?>"><?php _e( 'Change role to&hellip;' ) ?></label>
  225. <select name="<?php echo $id ?>" id="<?php echo $id ?>">
  226. <option value=""><?php _e( 'Change role to&hellip;' ) ?></option>
  227. <?php wp_dropdown_roles(); ?>
  228. </select>
  229. <?php
  230. submit_button( __( 'Change' ), '', 'changeit', false );
  231. endif;
  232. /**
  233. * Fires just before the closing div containing the bulk role-change controls
  234. * in the Users list table.
  235. *
  236. * @since 3.5.0
  237. * @since 4.6.0 The `$which` parameter was added.
  238. *
  239. * @param string $which The location of the extra table nav markup: 'top' or 'bottom'.
  240. */
  241. do_action( 'restrict_manage_users', $which );
  242. echo '</div>';
  243. }
  244. /**
  245. * Capture the bulk action required, and return it.
  246. *
  247. * Overridden from the base class implementation to capture
  248. * the role change drop-down.
  249. *
  250. * @since 3.1.0
  251. * @access public
  252. *
  253. * @return string The bulk action required.
  254. */
  255. public function current_action() {
  256. if ( isset( $_REQUEST['changeit'] ) &&
  257. ( ! empty( $_REQUEST['new_role'] ) || ! empty( $_REQUEST['new_role2'] ) ) ) {
  258. return 'promote';
  259. }
  260. return parent::current_action();
  261. }
  262. /**
  263. * Get a list of columns for the list table.
  264. *
  265. * @since 3.1.0
  266. * @access public
  267. *
  268. * @return array Array in which the key is the ID of the column,
  269. * and the value is the description.
  270. */
  271. public function get_columns() {
  272. $c = array(
  273. 'cb' => '<input type="checkbox" />',
  274. 'username' => __( 'Username' ),
  275. 'name' => __( 'Name' ),
  276. 'email' => __( 'Email' ),
  277. 'role' => __( 'Role' ),
  278. 'posts' => __( 'Posts' )
  279. );
  280. if ( $this->is_site_users )
  281. unset( $c['posts'] );
  282. return $c;
  283. }
  284. /**
  285. * Get a list of sortable columns for the list table.
  286. *
  287. * @since 3.1.0
  288. * @access protected
  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. * @access public
  304. */
  305. public function display_rows() {
  306. // Query the post counts for this page
  307. if ( ! $this->is_site_users )
  308. $post_counts = count_many_users_posts( array_keys( $this->items ) );
  309. foreach ( $this->items as $userid => $user_object ) {
  310. if ( is_multisite() && empty( $user_object->allcaps ) )
  311. continue;
  312. echo "\n\t" . $this->single_row( $user_object, '', '', isset( $post_counts ) ? $post_counts[ $userid ] : 0 );
  313. }
  314. }
  315. /**
  316. * Generate HTML for a single row on the users.php admin panel.
  317. *
  318. * @since 3.1.0
  319. * @since 4.2.0 The `$style` parameter was deprecated.
  320. * @since 4.4.0 The `$role` parameter was deprecated.
  321. * @access public
  322. *
  323. * @param object $user_object The current user object.
  324. * @param string $style Deprecated. Not used.
  325. * @param string $role Deprecated. Not used.
  326. * @param int $numposts Optional. Post count to display for this user. Defaults
  327. * to zero, as in, a new user has made zero posts.
  328. * @return string Output for a single row.
  329. */
  330. public function single_row( $user_object, $style = '', $role = '', $numposts = 0 ) {
  331. if ( ! ( $user_object instanceof WP_User ) ) {
  332. $user_object = get_userdata( (int) $user_object );
  333. }
  334. $user_object->filter = 'display';
  335. $email = $user_object->user_email;
  336. if ( $this->is_site_users )
  337. $url = "site-users.php?id={$this->site_id}&amp;";
  338. else
  339. $url = 'users.php?';
  340. $user_roles = $this->get_role_list( $user_object );
  341. // Set up the hover actions for this user
  342. $actions = array();
  343. $checkbox = '';
  344. // Check if the user for this row is editable
  345. if ( current_user_can( 'list_users' ) ) {
  346. // Set up the user editing link
  347. $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user_object->ID ) ) );
  348. if ( current_user_can( 'edit_user', $user_object->ID ) ) {
  349. $edit = "<strong><a href=\"$edit_link\">$user_object->user_login</a></strong><br />";
  350. $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
  351. } else {
  352. $edit = "<strong>$user_object->user_login</strong><br />";
  353. }
  354. if ( !is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'delete_user', $user_object->ID ) )
  355. $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url( "users.php?action=delete&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Delete' ) . "</a>";
  356. if ( is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'remove_user', $user_object->ID ) )
  357. $actions['remove'] = "<a class='submitdelete' href='" . wp_nonce_url( $url."action=remove&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Remove' ) . "</a>";
  358. /**
  359. * Filters the action links displayed under each user in the Users list table.
  360. *
  361. * @since 2.8.0
  362. *
  363. * @param array $actions An array of action links to be displayed.
  364. * Default 'Edit', 'Delete' for single site, and
  365. * 'Edit', 'Remove' for Multisite.
  366. * @param WP_User $user_object WP_User object for the currently-listed user.
  367. */
  368. $actions = apply_filters( 'user_row_actions', $actions, $user_object );
  369. // Role classes.
  370. $role_classes = esc_attr( implode( ' ', array_keys( $user_roles ) ) );
  371. // Set up the checkbox ( because the user is editable, otherwise it's empty )
  372. $checkbox = '<label class="screen-reader-text" for="user_' . $user_object->ID . '">' . sprintf( __( 'Select %s' ), $user_object->user_login ) . '</label>'
  373. . "<input type='checkbox' name='users[]' id='user_{$user_object->ID}' class='{$role_classes}' value='{$user_object->ID}' />";
  374. } else {
  375. $edit = '<strong>' . $user_object->user_login . '</strong>';
  376. }
  377. $avatar = get_avatar( $user_object->ID, 32 );
  378. // Comma-separated list of user roles.
  379. $roles_list = implode( ', ', $user_roles );
  380. $r = "<tr id='user-$user_object->ID'>";
  381. list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
  382. foreach ( $columns as $column_name => $column_display_name ) {
  383. $classes = "$column_name column-$column_name";
  384. if ( $primary === $column_name ) {
  385. $classes .= ' has-row-actions column-primary';
  386. }
  387. if ( 'posts' === $column_name ) {
  388. $classes .= ' num'; // Special case for that column
  389. }
  390. if ( in_array( $column_name, $hidden ) ) {
  391. $classes .= ' hidden';
  392. }
  393. $data = 'data-colname="' . wp_strip_all_tags( $column_display_name ) . '"';
  394. $attributes = "class='$classes' $data";
  395. if ( 'cb' === $column_name ) {
  396. $r .= "<th scope='row' class='check-column'>$checkbox</th>";
  397. } else {
  398. $r .= "<td $attributes>";
  399. switch ( $column_name ) {
  400. case 'username':
  401. $r .= "$avatar $edit";
  402. break;
  403. case 'name':
  404. $r .= "$user_object->first_name $user_object->last_name";
  405. break;
  406. case 'email':
  407. $r .= "<a href='" . esc_url( "mailto:$email" ) . "'>$email</a>";
  408. break;
  409. case 'role':
  410. $r .= esc_html( $roles_list );
  411. break;
  412. case 'posts':
  413. if ( $numposts > 0 ) {
  414. $r .= "<a href='edit.php?author=$user_object->ID' class='edit'>";
  415. $r .= '<span aria-hidden="true">' . $numposts . '</span>';
  416. $r .= '<span class="screen-reader-text">' . sprintf( _n( '%s post by this author', '%s posts by this author', $numposts ), number_format_i18n( $numposts ) ) . '</span>';
  417. $r .= '</a>';
  418. } else {
  419. $r .= 0;
  420. }
  421. break;
  422. default:
  423. /**
  424. * Filters the display output of custom columns in the Users list table.
  425. *
  426. * @since 2.8.0
  427. *
  428. * @param string $output Custom column output. Default empty.
  429. * @param string $column_name Column name.
  430. * @param int $user_id ID of the currently-listed user.
  431. */
  432. $r .= apply_filters( 'manage_users_custom_column', '', $column_name, $user_object->ID );
  433. }
  434. if ( $primary === $column_name ) {
  435. $r .= $this->row_actions( $actions );
  436. }
  437. $r .= "</td>";
  438. }
  439. }
  440. $r .= '</tr>';
  441. return $r;
  442. }
  443. /**
  444. * Gets the name of the default primary column.
  445. *
  446. * @since 4.3.0
  447. * @access protected
  448. *
  449. * @return string Name of the default primary column, in this case, 'username'.
  450. */
  451. protected function get_default_primary_column_name() {
  452. return 'username';
  453. }
  454. /**
  455. * Returns an array of user roles for a given user object.
  456. *
  457. * @since 4.4.0
  458. * @access protected
  459. *
  460. * @param WP_User $user_object The WP_User object.
  461. * @return array An array of user roles.
  462. */
  463. protected function get_role_list( $user_object ) {
  464. $wp_roles = wp_roles();
  465. $role_list = array();
  466. foreach ( $user_object->roles as $role ) {
  467. if ( isset( $wp_roles->role_names[ $role ] ) ) {
  468. $role_list[ $role ] = translate_user_role( $wp_roles->role_names[ $role ] );
  469. }
  470. }
  471. if ( empty( $role_list ) ) {
  472. $role_list['none'] = _x( 'None', 'no user roles' );
  473. }
  474. /**
  475. * Filters the returned array of roles for a user.
  476. *
  477. * @since 4.4.0
  478. *
  479. * @param array $role_list An array of user roles.
  480. * @param WP_User $user_object A WP_User object.
  481. */
  482. return apply_filters( 'get_role_list', $role_list, $user_object );
  483. }
  484. }