PageRenderTime 33ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/terry69/fluxflex_wordpress
PHP | 319 lines | 233 code | 59 blank | 27 comment | 48 complexity | 0023b6f79f4591d1fed87281fcf61656 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 WP_Users_List_Table() {
  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::WP_List_Table( 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. $args['search'] = '*' . $args['search'] . '*';
  44. if ( $this->is_site_users )
  45. $args['blog_id'] = $this->site_id;
  46. if ( isset( $_REQUEST['orderby'] ) )
  47. $args['orderby'] = $_REQUEST['orderby'];
  48. if ( isset( $_REQUEST['order'] ) )
  49. $args['order'] = $_REQUEST['order'];
  50. // Query the user IDs for this page
  51. $wp_user_search = new WP_User_Query( $args );
  52. $this->items = $wp_user_search->get_results();
  53. $this->set_pagination_args( array(
  54. 'total_items' => $wp_user_search->get_total(),
  55. 'per_page' => $users_per_page,
  56. ) );
  57. }
  58. function no_items() {
  59. _e( 'No matching users were found.' );
  60. }
  61. function get_views() {
  62. global $wp_roles, $role;
  63. if ( $this->is_site_users ) {
  64. $url = 'site-users.php?id=' . $this->site_id;
  65. switch_to_blog( $this->site_id );
  66. $users_of_blog = count_users();
  67. restore_current_blog();
  68. } else {
  69. $url = 'users.php';
  70. $users_of_blog = count_users();
  71. }
  72. $total_users = $users_of_blog['total_users'];
  73. $avail_roles =& $users_of_blog['avail_roles'];
  74. unset($users_of_blog);
  75. $current_role = false;
  76. $class = empty($role) ? ' class="current"' : '';
  77. $role_links = array();
  78. $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>';
  79. foreach ( $wp_roles->get_names() as $this_role => $name ) {
  80. if ( !isset($avail_roles[$this_role]) )
  81. continue;
  82. $class = '';
  83. if ( $this_role == $role ) {
  84. $current_role = $role;
  85. $class = ' class="current"';
  86. }
  87. $name = translate_user_role( $name );
  88. /* translators: User role name with count */
  89. $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, $avail_roles[$this_role] );
  90. $role_links[$this_role] = "<a href='" . add_query_arg( 'role', $this_role, $url ) . "'$class>$name</a>";
  91. }
  92. return $role_links;
  93. }
  94. function get_bulk_actions() {
  95. $actions = array();
  96. if ( is_multisite() ) {
  97. if ( current_user_can( 'remove_users' ) )
  98. $actions['remove'] = __( 'Remove' );
  99. } else {
  100. if ( current_user_can( 'delete_users' ) )
  101. $actions['delete'] = __( 'Delete' );
  102. }
  103. return $actions;
  104. }
  105. function extra_tablenav( $which ) {
  106. if ( 'top' != $which )
  107. return;
  108. if ( ! current_user_can( 'promote_users' ) )
  109. return;
  110. ?>
  111. <div class="alignleft actions">
  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 submit_button( __( 'Change' ), 'secondary', 'changeit', false ); ?>
  118. </div>
  119. <?php
  120. }
  121. function current_action() {
  122. if ( isset($_REQUEST['changeit']) && !empty($_REQUEST['new_role']) )
  123. return 'promote';
  124. return parent::current_action();
  125. }
  126. function get_columns() {
  127. $c = array(
  128. 'cb' => '<input type="checkbox" />',
  129. 'username' => __( 'Username' ),
  130. 'name' => __( 'Name' ),
  131. 'email' => __( 'E-mail' ),
  132. 'role' => __( 'Role' ),
  133. 'posts' => __( 'Posts' )
  134. );
  135. if ( $this->is_site_users )
  136. unset( $c['posts'] );
  137. return $c;
  138. }
  139. function get_sortable_columns() {
  140. $c = array(
  141. 'username' => 'login',
  142. 'name' => 'name',
  143. 'email' => 'email',
  144. );
  145. if ( $this->is_site_users )
  146. unset( $c['posts'] );
  147. return $c;
  148. }
  149. function display_rows() {
  150. // Query the post counts for this page
  151. if ( ! $this->is_site_users )
  152. $post_counts = count_many_users_posts( array_keys( $this->items ) );
  153. $style = '';
  154. foreach ( $this->items as $userid => $user_object ) {
  155. $role = reset( $user_object->roles );
  156. if ( is_multisite() && empty( $role ) )
  157. continue;
  158. $style = ( ' class="alternate"' == $style ) ? '' : ' class="alternate"';
  159. echo "\n\t", $this->single_row( $user_object, $style, $role, isset( $post_counts ) ? $post_counts[ $userid ] : 0 );
  160. }
  161. }
  162. /**
  163. * Generate HTML for a single row on the users.php admin panel.
  164. *
  165. * @since 2.1.0
  166. *
  167. * @param object $user_object
  168. * @param string $style Optional. Attributes added to the TR element. Must be sanitized.
  169. * @param string $role Key for the $wp_roles array.
  170. * @param int $numposts Optional. Post count to display for this user. Defaults to zero, as in, a new user has made zero posts.
  171. * @return string
  172. */
  173. function single_row( $user_object, $style = '', $role = '', $numposts = 0 ) {
  174. global $wp_roles;
  175. if ( !( is_object( $user_object ) && is_a( $user_object, 'WP_User' ) ) )
  176. $user_object = new WP_User( (int) $user_object );
  177. $user_object = sanitize_user_object( $user_object, 'display' );
  178. $email = $user_object->user_email;
  179. if ( $this->is_site_users )
  180. $url = "site-users.php?id={$this->site_id}&amp;";
  181. else
  182. $url = 'users.php?';
  183. $checkbox = '';
  184. // Check if the user for this row is editable
  185. if ( current_user_can( 'list_users' ) ) {
  186. // Set up the user editing link
  187. // TODO: make profile/user-edit determination a separate function
  188. if ( get_current_user_id() == $user_object->ID ) {
  189. $edit_link = 'profile.php';
  190. } else {
  191. $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( stripslashes( $_SERVER['REQUEST_URI'] ) ), "user-edit.php?user_id=$user_object->ID" ) );
  192. }
  193. // Set up the hover actions for this user
  194. $actions = array();
  195. if ( current_user_can( 'edit_user', $user_object->ID ) ) {
  196. $edit = "<strong><a href=\"$edit_link\">$user_object->user_login</a></strong><br />";
  197. $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
  198. } else {
  199. $edit = "<strong>$user_object->user_login</strong><br />";
  200. }
  201. if ( !is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'delete_user', $user_object->ID ) )
  202. $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url( "users.php?action=delete&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Delete' ) . "</a>";
  203. if ( is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'remove_user', $user_object->ID ) )
  204. $actions['remove'] = "<a class='submitdelete' href='" . wp_nonce_url( $url."action=remove&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Remove' ) . "</a>";
  205. $actions = apply_filters( 'user_row_actions', $actions, $user_object );
  206. $edit .= $this->row_actions( $actions );
  207. // Set up the checkbox ( because the user is editable, otherwise its empty )
  208. $checkbox = "<input type='checkbox' name='users[]' id='user_{$user_object->ID}' class='$role' value='{$user_object->ID}' />";
  209. } else {
  210. $edit = '<strong>' . $user_object->user_login . '</strong>';
  211. }
  212. $role_name = isset( $wp_roles->role_names[$role] ) ? translate_user_role( $wp_roles->role_names[$role] ) : __( 'None' );
  213. $avatar = get_avatar( $user_object->ID, 32 );
  214. $r = "<tr id='user-$user_object->ID'$style>";
  215. list( $columns, $hidden ) = $this->get_column_info();
  216. foreach ( $columns as $column_name => $column_display_name ) {
  217. $class = "class=\"$column_name column-$column_name\"";
  218. $style = '';
  219. if ( in_array( $column_name, $hidden ) )
  220. $style = ' style="display:none;"';
  221. $attributes = "$class$style";
  222. switch ( $column_name ) {
  223. case 'cb':
  224. $r .= "<th scope='row' class='check-column'>$checkbox</th>";
  225. break;
  226. case 'username':
  227. $r .= "<td $attributes>$avatar $edit</td>";
  228. break;
  229. case 'name':
  230. $r .= "<td $attributes>$user_object->first_name $user_object->last_name</td>";
  231. break;
  232. case 'email':
  233. $r .= "<td $attributes><a href='mailto:$email' title='" . esc_attr( sprintf( __( 'E-mail: %s' ), $email ) ) . "'>$email</a></td>";
  234. break;
  235. case 'role':
  236. $r .= "<td $attributes>$role_name</td>";
  237. break;
  238. case 'posts':
  239. $attributes = 'class="posts column-posts num"' . $style;
  240. $r .= "<td $attributes>";
  241. if ( $numposts > 0 ) {
  242. $r .= "<a href='edit.php?author=$user_object->ID' title='" . esc_attr__( 'View posts by this author' ) . "' class='edit'>";
  243. $r .= $numposts;
  244. $r .= '</a>';
  245. } else {
  246. $r .= 0;
  247. }
  248. $r .= "</td>";
  249. break;
  250. default:
  251. $r .= "<td $attributes>";
  252. $r .= apply_filters( 'manage_users_custom_column', '', $column_name, $user_object->ID );
  253. $r .= "</td>";
  254. }
  255. }
  256. $r .= '</tr>';
  257. return $r;
  258. }
  259. }
  260. ?>