PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-admin/includes/class-wp-users-list-table.php

https://bitbucket.org/julianelve/vendor-wordpress
PHP | 324 lines | 238 code | 60 blank | 26 comment | 48 complexity | 0045dd9f35ea2874b06597a83cf31d4f MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * Users List Table class.
  4. *
  5. * @package WordPress
  6. * @subpackage List_Table
  7. * @since 3.1.0
  8. * @access private
  9. */
  10. class WP_Users_List_Table extends WP_List_Table {
  11. var $site_id;
  12. var $is_site_users;
  13. function __construct( $args = array() ) {
  14. parent::__construct( array(
  15. 'singular' => 'user',
  16. 'plural' => 'users',
  17. 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
  18. ) );
  19. $this->is_site_users = 'site-users-network' == $this->screen->id;
  20. if ( $this->is_site_users )
  21. $this->site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
  22. }
  23. function ajax_user_can() {
  24. if ( $this->is_site_users )
  25. return current_user_can( 'manage_sites' );
  26. else
  27. return current_user_can( 'list_users' );
  28. }
  29. function prepare_items() {
  30. global $role, $usersearch;
  31. $usersearch = isset( $_REQUEST['s'] ) ? trim( $_REQUEST['s'] ) : '';
  32. $role = isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : '';
  33. $per_page = ( $this->is_site_users ) ? 'site_users_network_per_page' : 'users_per_page';
  34. $users_per_page = $this->get_items_per_page( $per_page );
  35. $paged = $this->get_pagenum();
  36. $args = array(
  37. 'number' => $users_per_page,
  38. 'offset' => ( $paged-1 ) * $users_per_page,
  39. 'role' => $role,
  40. 'search' => $usersearch,
  41. 'fields' => 'all_with_meta'
  42. );
  43. if ( '' !== $args['search'] )
  44. $args['search'] = '*' . $args['search'] . '*';
  45. if ( $this->is_site_users )
  46. $args['blog_id'] = $this->site_id;
  47. if ( isset( $_REQUEST['orderby'] ) )
  48. $args['orderby'] = $_REQUEST['orderby'];
  49. if ( isset( $_REQUEST['order'] ) )
  50. $args['order'] = $_REQUEST['order'];
  51. // Query the user IDs for this page
  52. $wp_user_search = new WP_User_Query( $args );
  53. $this->items = $wp_user_search->get_results();
  54. $this->set_pagination_args( array(
  55. 'total_items' => $wp_user_search->get_total(),
  56. 'per_page' => $users_per_page,
  57. ) );
  58. }
  59. function no_items() {
  60. _e( 'No matching users were found.' );
  61. }
  62. function get_views() {
  63. global $wp_roles, $role;
  64. if ( $this->is_site_users ) {
  65. $url = 'site-users.php?id=' . $this->site_id;
  66. switch_to_blog( $this->site_id );
  67. $users_of_blog = count_users();
  68. restore_current_blog();
  69. } else {
  70. $url = 'users.php';
  71. $users_of_blog = count_users();
  72. }
  73. $total_users = $users_of_blog['total_users'];
  74. $avail_roles =& $users_of_blog['avail_roles'];
  75. unset($users_of_blog);
  76. $current_role = false;
  77. $class = empty($role) ? ' class="current"' : '';
  78. $role_links = array();
  79. $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>';
  80. foreach ( $wp_roles->get_names() as $this_role => $name ) {
  81. if ( !isset($avail_roles[$this_role]) )
  82. continue;
  83. $class = '';
  84. if ( $this_role == $role ) {
  85. $current_role = $role;
  86. $class = ' class="current"';
  87. }
  88. $name = translate_user_role( $name );
  89. /* translators: User role name with count */
  90. $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n( $avail_roles[$this_role] ) );
  91. $role_links[$this_role] = "<a href='" . esc_url( add_query_arg( 'role', $this_role, $url ) ) . "'$class>$name</a>";
  92. }
  93. return $role_links;
  94. }
  95. function get_bulk_actions() {
  96. $actions = array();
  97. if ( is_multisite() ) {
  98. if ( current_user_can( 'remove_users' ) )
  99. $actions['remove'] = __( 'Remove' );
  100. } else {
  101. if ( current_user_can( 'delete_users' ) )
  102. $actions['delete'] = __( 'Delete' );
  103. }
  104. return $actions;
  105. }
  106. function extra_tablenav( $which ) {
  107. if ( 'top' != $which )
  108. return;
  109. ?>
  110. <div class="alignleft actions">
  111. <?php if ( current_user_can( 'promote_users' ) ) : ?>
  112. <label class="screen-reader-text" for="new_role"><?php _e( 'Change role to&hellip;' ) ?></label>
  113. <select name="new_role" id="new_role">
  114. <option value=''><?php _e( 'Change role to&hellip;' ) ?></option>
  115. <?php wp_dropdown_roles(); ?>
  116. </select>
  117. <?php
  118. submit_button( __( 'Change' ), 'button', 'changeit', false );
  119. endif;
  120. do_action( 'restrict_manage_users' );
  121. echo '</div>';
  122. }
  123. function current_action() {
  124. if ( isset($_REQUEST['changeit']) && !empty($_REQUEST['new_role']) )
  125. return 'promote';
  126. return parent::current_action();
  127. }
  128. function get_columns() {
  129. $c = array(
  130. 'cb' => '<input type="checkbox" />',
  131. 'username' => __( 'Username' ),
  132. 'name' => __( 'Name' ),
  133. 'email' => __( 'E-mail' ),
  134. 'role' => __( 'Role' ),
  135. 'posts' => __( 'Posts' )
  136. );
  137. if ( $this->is_site_users )
  138. unset( $c['posts'] );
  139. return $c;
  140. }
  141. function get_sortable_columns() {
  142. $c = array(
  143. 'username' => 'login',
  144. 'name' => 'name',
  145. 'email' => 'email',
  146. );
  147. if ( $this->is_site_users )
  148. unset( $c['posts'] );
  149. return $c;
  150. }
  151. function display_rows() {
  152. // Query the post counts for this page
  153. if ( ! $this->is_site_users )
  154. $post_counts = count_many_users_posts( array_keys( $this->items ) );
  155. $editable_roles = array_keys( get_editable_roles() );
  156. $style = '';
  157. foreach ( $this->items as $userid => $user_object ) {
  158. if ( count( $user_object->roles ) <= 1 ) {
  159. $role = reset( $user_object->roles );
  160. } elseif ( $roles = array_intersect( array_values( $user_object->roles ), $editable_roles ) ) {
  161. $role = reset( $roles );
  162. } else {
  163. $role = reset( $user_object->roles );
  164. }
  165. if ( is_multisite() && empty( $user_object->allcaps ) )
  166. continue;
  167. $style = ( ' class="alternate"' == $style ) ? '' : ' class="alternate"';
  168. echo "\n\t", $this->single_row( $user_object, $style, $role, isset( $post_counts ) ? $post_counts[ $userid ] : 0 );
  169. }
  170. }
  171. /**
  172. * Generate HTML for a single row on the users.php admin panel.
  173. *
  174. * @since 2.1.0
  175. *
  176. * @param object $user_object
  177. * @param string $style Optional. Attributes added to the TR element. Must be sanitized.
  178. * @param string $role Key for the $wp_roles array.
  179. * @param int $numposts Optional. Post count to display for this user. Defaults to zero, as in, a new user has made zero posts.
  180. * @return string
  181. */
  182. function single_row( $user_object, $style = '', $role = '', $numposts = 0 ) {
  183. global $wp_roles;
  184. if ( !( is_object( $user_object ) && is_a( $user_object, 'WP_User' ) ) )
  185. $user_object = get_userdata( (int) $user_object );
  186. $user_object->filter = 'display';
  187. $email = $user_object->user_email;
  188. if ( $this->is_site_users )
  189. $url = "site-users.php?id={$this->site_id}&amp;";
  190. else
  191. $url = 'users.php?';
  192. $checkbox = '';
  193. // Check if the user for this row is editable
  194. if ( current_user_can( 'list_users' ) ) {
  195. // Set up the user editing link
  196. $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( stripslashes( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user_object->ID ) ) );
  197. // Set up the hover actions for this user
  198. $actions = array();
  199. if ( current_user_can( 'edit_user', $user_object->ID ) ) {
  200. $edit = "<strong><a href=\"$edit_link\">$user_object->user_login</a></strong><br />";
  201. $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
  202. } else {
  203. $edit = "<strong>$user_object->user_login</strong><br />";
  204. }
  205. if ( !is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'delete_user', $user_object->ID ) )
  206. $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url( "users.php?action=delete&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Delete' ) . "</a>";
  207. if ( is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'remove_user', $user_object->ID ) )
  208. $actions['remove'] = "<a class='submitdelete' href='" . wp_nonce_url( $url."action=remove&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Remove' ) . "</a>";
  209. $actions = apply_filters( 'user_row_actions', $actions, $user_object );
  210. $edit .= $this->row_actions( $actions );
  211. // Set up the checkbox ( because the user is editable, otherwise its empty )
  212. $checkbox = '<label class="screen-reader-text" for="cb-select-' . $user_object->ID . '">' . sprintf( __( 'Select %s' ), $user_object->user_login ) . '</label>'
  213. . "<input type='checkbox' name='users[]' id='user_{$user_object->ID}' class='$role' value='{$user_object->ID}' />";
  214. } else {
  215. $edit = '<strong>' . $user_object->user_login . '</strong>';
  216. }
  217. $role_name = isset( $wp_roles->role_names[$role] ) ? translate_user_role( $wp_roles->role_names[$role] ) : __( 'None' );
  218. $avatar = get_avatar( $user_object->ID, 32 );
  219. $r = "<tr id='user-$user_object->ID'$style>";
  220. list( $columns, $hidden ) = $this->get_column_info();
  221. foreach ( $columns as $column_name => $column_display_name ) {
  222. $class = "class=\"$column_name column-$column_name\"";
  223. $style = '';
  224. if ( in_array( $column_name, $hidden ) )
  225. $style = ' style="display:none;"';
  226. $attributes = "$class$style";
  227. switch ( $column_name ) {
  228. case 'cb':
  229. $r .= "<th scope='row' class='check-column'>$checkbox</th>";
  230. break;
  231. case 'username':
  232. $r .= "<td $attributes>$avatar $edit</td>";
  233. break;
  234. case 'name':
  235. $r .= "<td $attributes>$user_object->first_name $user_object->last_name</td>";
  236. break;
  237. case 'email':
  238. $r .= "<td $attributes><a href='mailto:$email' title='" . esc_attr( sprintf( __( 'E-mail: %s' ), $email ) ) . "'>$email</a></td>";
  239. break;
  240. case 'role':
  241. $r .= "<td $attributes>$role_name</td>";
  242. break;
  243. case 'posts':
  244. $attributes = 'class="posts column-posts num"' . $style;
  245. $r .= "<td $attributes>";
  246. if ( $numposts > 0 ) {
  247. $r .= "<a href='edit.php?author=$user_object->ID' title='" . esc_attr__( 'View posts by this author' ) . "' class='edit'>";
  248. $r .= $numposts;
  249. $r .= '</a>';
  250. } else {
  251. $r .= 0;
  252. }
  253. $r .= "</td>";
  254. break;
  255. default:
  256. $r .= "<td $attributes>";
  257. $r .= apply_filters( 'manage_users_custom_column', '', $column_name, $user_object->ID );
  258. $r .= "</td>";
  259. }
  260. }
  261. $r .= '</tr>';
  262. return $r;
  263. }
  264. }