PageRenderTime 70ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/ljmc-admin/includes/class-ljmc-ms-users-list-table.php

https://bitbucket.org/lpservice-it/ljmc
PHP | 306 lines | 205 code | 54 blank | 47 comment | 34 complexity | f224d77377e7a173ec59043b537c02c6 MD5 | raw file
Possible License(s): Apache-2.0, GPL-3.0, MIT
  1. <?php
  2. /**
  3. * Multisite Users List Table class.
  4. *
  5. * @package system
  6. * @subpackage List_Table
  7. * @since 3.1.0
  8. * @access private
  9. */
  10. class LJMC_MS_Users_List_Table extends LJMC_List_Table {
  11. public function ajax_user_can() {
  12. return current_user_can( 'manage_network_users' );
  13. }
  14. public function prepare_items() {
  15. global $usersearch, $role, $ljmcdb, $mode;
  16. $usersearch = isset( $_REQUEST['s'] ) ? ljmc_unslash( trim( $_REQUEST['s'] ) ) : '';
  17. $users_per_page = $this->get_items_per_page( 'users_network_per_page' );
  18. $role = isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : '';
  19. $paged = $this->get_pagenum();
  20. $args = array(
  21. 'number' => $users_per_page,
  22. 'offset' => ( $paged-1 ) * $users_per_page,
  23. 'search' => $usersearch,
  24. 'blog_id' => 0,
  25. 'fields' => 'all_with_meta'
  26. );
  27. if ( ljmc_is_large_network( 'users' ) )
  28. $args['search'] = ltrim( $args['search'], '*' );
  29. if ( $role == 'super' ) {
  30. $logins = implode( "', '", get_super_admins() );
  31. $args['include'] = $ljmcdb->get_col( "SELECT ID FROM $ljmcdb->users WHERE user_login IN ('$logins')" );
  32. }
  33. /*
  34. * If the network is large and a search is not being performed,
  35. * show only the latest users with no paging in order to avoid
  36. * expensive count queries.
  37. */
  38. if ( !$usersearch && ljmc_is_large_network( 'users' ) ) {
  39. if ( !isset($_REQUEST['orderby']) )
  40. $_GET['orderby'] = $_REQUEST['orderby'] = 'id';
  41. if ( !isset($_REQUEST['order']) )
  42. $_GET['order'] = $_REQUEST['order'] = 'DESC';
  43. $args['count_total'] = false;
  44. }
  45. if ( isset( $_REQUEST['orderby'] ) )
  46. $args['orderby'] = $_REQUEST['orderby'];
  47. if ( isset( $_REQUEST['order'] ) )
  48. $args['order'] = $_REQUEST['order'];
  49. $mode = empty( $_REQUEST['mode'] ) ? 'list' : $_REQUEST['mode'];
  50. // Query the user IDs for this page
  51. $ljmc_user_search = new LJMC_User_Query( $args );
  52. $this->items = $ljmc_user_search->get_results();
  53. $this->set_pagination_args( array(
  54. 'total_items' => $ljmc_user_search->get_total(),
  55. 'per_page' => $users_per_page,
  56. ) );
  57. }
  58. protected function get_bulk_actions() {
  59. $actions = array();
  60. if ( current_user_can( 'delete_users' ) )
  61. $actions['delete'] = __( 'Delete' );
  62. $actions['spam'] = _x( 'Mark as Spam', 'user' );
  63. $actions['notspam'] = _x( 'Not Spam', 'user' );
  64. return $actions;
  65. }
  66. public function no_items() {
  67. _e( 'No users found.' );
  68. }
  69. protected function get_views() {
  70. global $role;
  71. $total_users = get_user_count();
  72. $super_admins = get_super_admins();
  73. $total_admins = count( $super_admins );
  74. $class = $role != 'super' ? ' class="current"' : '';
  75. $role_links = array();
  76. $role_links['all'] = "<a href='" . network_admin_url('users.php') . "'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_users, 'users' ), number_format_i18n( $total_users ) ) . '</a>';
  77. $class = $role == 'super' ? ' class="current"' : '';
  78. $role_links['super'] = "<a href='" . network_admin_url('users.php?role=super') . "'$class>" . sprintf( _n( 'Super Admin <span class="count">(%s)</span>', 'Super Admins <span class="count">(%s)</span>', $total_admins ), number_format_i18n( $total_admins ) ) . '</a>';
  79. return $role_links;
  80. }
  81. /**
  82. * @global string $mode
  83. * @param string $which
  84. */
  85. protected function pagination( $which ) {
  86. global $mode;
  87. parent::pagination ( $which );
  88. if ( 'top' == $which )
  89. $this->view_switcher( $mode );
  90. }
  91. public function get_columns() {
  92. $users_columns = array(
  93. 'cb' => '<input type="checkbox" />',
  94. 'username' => __( 'Username' ),
  95. 'name' => __( 'Name' ),
  96. 'email' => __( 'E-mail' ),
  97. 'registered' => _x( 'Registered', 'user' ),
  98. 'blogs' => __( 'Sites' )
  99. );
  100. /**
  101. * Filter the columns displayed in the Network Admin Users list table.
  102. *
  103. * @since MU
  104. *
  105. * @param array $users_columns An array of user columns. Default 'cb', 'username',
  106. * 'name', 'email', 'registered', 'blogs'.
  107. */
  108. $users_columns = apply_filters( 'ljmcmu_users_columns', $users_columns );
  109. return $users_columns;
  110. }
  111. protected function get_sortable_columns() {
  112. return array(
  113. 'username' => 'login',
  114. 'name' => 'name',
  115. 'email' => 'email',
  116. 'registered' => 'id',
  117. );
  118. }
  119. public function display_rows() {
  120. global $mode;
  121. $super_admins = get_super_admins();
  122. foreach ( $this->items as $user ) {
  123. $class = '';
  124. $status_list = array( 'spam' => 'site-spammed', 'deleted' => 'site-deleted' );
  125. foreach ( $status_list as $status => $col ) {
  126. if ( $user->$status )
  127. $class .= " $col";
  128. }
  129. ?>
  130. <tr class="<?php echo trim( $class ); ?>">
  131. <?php
  132. list( $columns, $hidden ) = $this->get_column_info();
  133. foreach ( $columns as $column_name => $column_display_name ) :
  134. $class = "class='$column_name column-$column_name'";
  135. $style = '';
  136. if ( in_array( $column_name, $hidden ) )
  137. $style = ' style="display:none;"';
  138. $attributes = "$class$style";
  139. switch ( $column_name ) {
  140. case 'cb': ?>
  141. <th scope="row" class="check-column">
  142. <label class="screen-reader-text" for="blog_<?php echo $user->ID; ?>"><?php echo sprintf( __( 'Select %s' ), $user->user_login ); ?></label>
  143. <input type="checkbox" id="blog_<?php echo $user->ID ?>" name="allusers[]" value="<?php echo esc_attr( $user->ID ) ?>" />
  144. </th>
  145. <?php
  146. break;
  147. case 'username':
  148. $avatar = get_avatar( $user->user_email, 32 );
  149. $edit_link = esc_url( add_query_arg( 'ljmc_http_referer', urlencode( ljmc_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user->ID ) ) );
  150. echo "<td $attributes>"; ?>
  151. <?php echo $avatar; ?><strong><a href="<?php echo $edit_link; ?>" class="edit"><?php echo $user->user_login; ?></a><?php
  152. if ( in_array( $user->user_login, $super_admins ) )
  153. echo ' - ' . __( 'Super Admin' );
  154. ?></strong>
  155. <br/>
  156. <?php
  157. $actions = array();
  158. $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
  159. if ( current_user_can( 'delete_user', $user->ID ) && ! in_array( $user->user_login, $super_admins ) ) {
  160. $actions['delete'] = '<a href="' . $delete = esc_url( network_admin_url( add_query_arg( '_ljmc_http_referer', urlencode( ljmc_unslash( $_SERVER['REQUEST_URI'] ) ), ljmc_nonce_url( 'users.php', 'deleteuser' ) . '&amp;action=deleteuser&amp;id=' . $user->ID ) ) ) . '" class="delete">' . __( 'Delete' ) . '</a>';
  161. }
  162. /**
  163. * Filter the action links displayed under each user
  164. * in the Network Admin Users list table.
  165. *
  166. * @since 3.2.0
  167. *
  168. * @param array $actions An array of action links to be displayed.
  169. * Default 'Edit', 'Delete'.
  170. * @param LJMC_User $user LJMC_User object.
  171. */
  172. $actions = apply_filters( 'ms_user_row_actions', $actions, $user );
  173. echo $this->row_actions( $actions );
  174. ?>
  175. </td>
  176. <?php
  177. break;
  178. case 'name':
  179. echo "<td $attributes>$user->first_name $user->last_name</td>";
  180. break;
  181. case 'email':
  182. echo "<td $attributes><a href='mailto:$user->user_email'>$user->user_email</a></td>";
  183. break;
  184. case 'registered':
  185. if ( 'list' == $mode )
  186. $date = __( 'Y/m/d' );
  187. else
  188. $date = __( 'Y/m/d g:i:s a' );
  189. echo "<td $attributes>" . mysql2date( $date, $user->user_registered ) . "</td>";
  190. break;
  191. case 'blogs':
  192. $blogs = get_blogs_of_user( $user->ID, true );
  193. echo "<td $attributes>";
  194. if ( is_array( $blogs ) ) {
  195. foreach ( (array) $blogs as $key => $val ) {
  196. if ( !can_edit_network( $val->site_id ) )
  197. continue;
  198. $path = ( $val->path == '/' ) ? '' : $val->path;
  199. echo '<span class="site-' . $val->site_id . '" >';
  200. echo '<a href="'. esc_url( network_admin_url( 'site-info.php?id=' . $val->userblog_id ) ) .'">' . str_replace( '.' . get_current_site()->domain, '', $val->domain . $path ) . '</a>';
  201. echo ' <small class="row-actions">';
  202. $actions = array();
  203. $actions['edit'] = '<a href="'. esc_url( network_admin_url( 'site-info.php?id=' . $val->userblog_id ) ) .'">' . __( 'Edit' ) . '</a>';
  204. $class = '';
  205. if ( get_blog_status( $val->userblog_id, 'spam' ) == 1 )
  206. $class .= 'site-spammed ';
  207. if ( get_blog_status( $val->userblog_id, 'mature' ) == 1 )
  208. $class .= 'site-mature ';
  209. if ( get_blog_status( $val->userblog_id, 'deleted' ) == 1 )
  210. $class .= 'site-deleted ';
  211. if ( get_blog_status( $val->userblog_id, 'archived' ) == 1 )
  212. $class .= 'site-archived ';
  213. $actions['view'] = '<a class="' . $class . '" href="' . esc_url( get_home_url( $val->userblog_id ) ) . '">' . __( 'View' ) . '</a>';
  214. /**
  215. * Filter the action links displayed next the sites a user belongs to
  216. * in the Network Admin Users list table.
  217. *
  218. * @since 3.1.0
  219. *
  220. * @param array $actions An array of action links to be displayed.
  221. * Default 'Edit', 'View'.
  222. * @param int $userblog_id The site ID.
  223. */
  224. $actions = apply_filters( 'ms_user_list_site_actions', $actions, $val->userblog_id );
  225. $i=0;
  226. $action_count = count( $actions );
  227. foreach ( $actions as $action => $link ) {
  228. ++$i;
  229. ( $i == $action_count ) ? $sep = '' : $sep = ' | ';
  230. echo "<span class='$action'>$link$sep</span>";
  231. }
  232. echo '</small></span><br/>';
  233. }
  234. }
  235. ?>
  236. </td>
  237. <?php
  238. break;
  239. default:
  240. echo "<td $attributes>";
  241. /** This filter is documented in ljmc-admin/includes/class-ljmc-users-list-table.php */
  242. echo apply_filters( 'manage_users_custom_column', '', $column_name, $user->ID );
  243. echo "</td>";
  244. break;
  245. }
  246. endforeach
  247. ?>
  248. </tr>
  249. <?php
  250. }
  251. }
  252. }