PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/ch3tag/mothers
PHP | 318 lines | 233 code | 58 blank | 27 comment | 49 complexity | 06cda9c2191d8b7ad7b271d8ef5a124e MD5 | raw file
  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() {
  14. $screen = get_current_screen();
  15. $this->is_site_users = 'site-users-network' == $screen->id;
  16. if ( $this->is_site_users )
  17. $this->site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
  18. parent::__construct( array(
  19. 'singular' => 'user',
  20. 'plural' => 'users'
  21. ) );
  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'] ) ? $_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. if ( ! current_user_can( 'promote_users' ) )
  110. return;
  111. ?>
  112. <div class="alignleft actions">
  113. <label class="screen-reader-text" for="new_role"><?php _e( 'Change role to&hellip;' ) ?></label>
  114. <select name="new_role" id="new_role">
  115. <option value=''><?php _e( 'Change role to&hellip;' ) ?></option>
  116. <?php wp_dropdown_roles(); ?>
  117. </select>
  118. <?php submit_button( __( 'Change' ), 'secondary', 'changeit', false ); ?>
  119. </div>
  120. <?php
  121. }
  122. function current_action() {
  123. if ( isset($_REQUEST['changeit']) && !empty($_REQUEST['new_role']) )
  124. return 'promote';
  125. return parent::current_action();
  126. }
  127. function get_columns() {
  128. $c = array(
  129. 'cb' => '<input type="checkbox" />',
  130. 'username' => __( 'Username' ),
  131. 'name' => __( 'Name' ),
  132. 'email' => __( 'E-mail' ),
  133. 'role' => __( 'Role' ),
  134. 'posts' => __( 'Posts' )
  135. );
  136. if ( $this->is_site_users )
  137. unset( $c['posts'] );
  138. return $c;
  139. }
  140. function get_sortable_columns() {
  141. $c = array(
  142. 'username' => 'login',
  143. 'name' => 'name',
  144. 'email' => 'email',
  145. );
  146. if ( $this->is_site_users )
  147. unset( $c['posts'] );
  148. return $c;
  149. }
  150. function display_rows() {
  151. // Query the post counts for this page
  152. if ( ! $this->is_site_users )
  153. $post_counts = count_many_users_posts( array_keys( $this->items ) );
  154. $style = '';
  155. foreach ( $this->items as $userid => $user_object ) {
  156. $role = reset( $user_object->roles );
  157. if ( is_multisite() && empty( $role ) )
  158. continue;
  159. $style = ( ' class="alternate"' == $style ) ? '' : ' class="alternate"';
  160. echo "\n\t", $this->single_row( $user_object, $style, $role, isset( $post_counts ) ? $post_counts[ $userid ] : 0 );
  161. }
  162. }
  163. /**
  164. * Generate HTML for a single row on the users.php admin panel.
  165. *
  166. * @since 2.1.0
  167. *
  168. * @param object $user_object
  169. * @param string $style Optional. Attributes added to the TR element. Must be sanitized.
  170. * @param string $role Key for the $wp_roles array.
  171. * @param int $numposts Optional. Post count to display for this user. Defaults to zero, as in, a new user has made zero posts.
  172. * @return string
  173. */
  174. function single_row( $user_object, $style = '', $role = '', $numposts = 0 ) {
  175. global $wp_roles;
  176. if ( !( is_object( $user_object ) && is_a( $user_object, 'WP_User' ) ) )
  177. $user_object = new WP_User( (int) $user_object );
  178. $user_object->filter = 'display';
  179. $email = $user_object->user_email;
  180. if ( $this->is_site_users )
  181. $url = "site-users.php?id={$this->site_id}&amp;";
  182. else
  183. $url = 'users.php?';
  184. $checkbox = '';
  185. // Check if the user for this row is editable
  186. if ( current_user_can( 'list_users' ) ) {
  187. // Set up the user editing link
  188. // TODO: make profile/user-edit determination a separate function
  189. if ( get_current_user_id() == $user_object->ID ) {
  190. $edit_link = 'profile.php';
  191. } else {
  192. $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( stripslashes( $_SERVER['REQUEST_URI'] ) ), "user-edit.php?user_id=$user_object->ID" ) );
  193. }
  194. // Set up the hover actions for this user
  195. $actions = array();
  196. if ( current_user_can( 'edit_user', $user_object->ID ) ) {
  197. $edit = "<strong><a href=\"$edit_link\">$user_object->user_login</a></strong><br />";
  198. $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
  199. } else {
  200. $edit = "<strong>$user_object->user_login</strong><br />";
  201. }
  202. if ( !is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'delete_user', $user_object->ID ) )
  203. $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url( "users.php?action=delete&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Delete' ) . "</a>";
  204. if ( is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'remove_user', $user_object->ID ) )
  205. $actions['remove'] = "<a class='submitdelete' href='" . wp_nonce_url( $url."action=remove&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Remove' ) . "</a>";
  206. $actions = apply_filters( 'user_row_actions', $actions, $user_object );
  207. $edit .= $this->row_actions( $actions );
  208. // Set up the checkbox ( because the user is editable, otherwise its empty )
  209. $checkbox = "<input type='checkbox' name='users[]' id='user_{$user_object->ID}' class='$role' value='{$user_object->ID}' />";
  210. } else {
  211. $edit = '<strong>' . $user_object->user_login . '</strong>';
  212. }
  213. $role_name = isset( $wp_roles->role_names[$role] ) ? translate_user_role( $wp_roles->role_names[$role] ) : __( 'None' );
  214. $avatar = get_avatar( $user_object->ID, 32 );
  215. $r = "<tr id='user-$user_object->ID'$style>";
  216. list( $columns, $hidden ) = $this->get_column_info();
  217. foreach ( $columns as $column_name => $column_display_name ) {
  218. $class = "class=\"$column_name column-$column_name\"";
  219. $style = '';
  220. if ( in_array( $column_name, $hidden ) )
  221. $style = ' style="display:none;"';
  222. $attributes = "$class$style";
  223. switch ( $column_name ) {
  224. case 'cb':
  225. $r .= "<th scope='row' class='check-column'>$checkbox</th>";
  226. break;
  227. case 'username':
  228. $r .= "<td $attributes>$avatar $edit</td>";
  229. break;
  230. case 'name':
  231. $r .= "<td $attributes>$user_object->first_name $user_object->last_name</td>";
  232. break;
  233. case 'email':
  234. $r .= "<td $attributes><a href='mailto:$email' title='" . esc_attr( sprintf( __( 'E-mail: %s' ), $email ) ) . "'>$email</a></td>";
  235. break;
  236. case 'role':
  237. $r .= "<td $attributes>$role_name</td>";
  238. break;
  239. case 'posts':
  240. $attributes = 'class="posts column-posts num"' . $style;
  241. $r .= "<td $attributes>";
  242. if ( $numposts > 0 ) {
  243. $r .= "<a href='edit.php?author=$user_object->ID' title='" . esc_attr__( 'View posts by this author' ) . "' class='edit'>";
  244. $r .= $numposts;
  245. $r .= '</a>';
  246. } else {
  247. $r .= 0;
  248. }
  249. $r .= "</td>";
  250. break;
  251. default:
  252. $r .= "<td $attributes>";
  253. $r .= apply_filters( 'manage_users_custom_column', '', $column_name, $user_object->ID );
  254. $r .= "</td>";
  255. }
  256. }
  257. $r .= '</tr>';
  258. return $r;
  259. }
  260. }