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

  1. <?php 
  2. /** 
  3. * List Table API: WP_MS_Users_List_Table class 
  4. * 
  5. * @package WordPress 
  6. * @subpackage Administration 
  7. * @since 3.1.0 
  8. */ 
  9.  
  10. /** 
  11. * Core class used to implement displaying users in a list table for the network admin. 
  12. * 
  13. * @since 3.1.0 
  14. * @access private 
  15. * 
  16. * @see WP_List_Table 
  17. */ 
  18. class WP_MS_Users_List_Table extends WP_List_Table { 
  19. /** 
  20. * 
  21. * @return bool 
  22. */ 
  23. public function ajax_user_can() { 
  24. return current_user_can( 'manage_network_users' ); 
  25.  
  26. /** 
  27. * 
  28. * @global string $usersearch 
  29. * @global string $role 
  30. * @global wpdb $wpdb 
  31. * @global string $mode 
  32. */ 
  33. public function prepare_items() { 
  34. global $usersearch, $role, $wpdb, $mode; 
  35.  
  36. $usersearch = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : ''; 
  37.  
  38. $users_per_page = $this->get_items_per_page( 'users_network_per_page' ); 
  39.  
  40. $role = isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : ''; 
  41.  
  42. $paged = $this->get_pagenum(); 
  43.  
  44. $args = array( 
  45. 'number' => $users_per_page,  
  46. 'offset' => ( $paged-1 ) * $users_per_page,  
  47. 'search' => $usersearch,  
  48. 'blog_id' => 0,  
  49. 'fields' => 'all_with_meta' 
  50. ); 
  51.  
  52. if ( wp_is_large_network( 'users' ) ) { 
  53. $args['search'] = ltrim( $args['search'], '*' ); 
  54. } else if ( '' !== $args['search'] ) { 
  55. $args['search'] = trim( $args['search'], '*' ); 
  56. $args['search'] = '*' . $args['search'] . '*'; 
  57.  
  58. if ( $role === 'super' ) { 
  59. $logins = implode( "', '", get_super_admins() ); 
  60. $args['include'] = $wpdb->get_col( "SELECT ID FROM $wpdb->users WHERE user_login IN ('$logins')" ); 
  61.  
  62. /** 
  63. * If the network is large and a search is not being performed,  
  64. * show only the latest users with no paging in order to avoid 
  65. * expensive count queries. 
  66. */ 
  67. if ( !$usersearch && wp_is_large_network( 'users' ) ) { 
  68. if ( !isset($_REQUEST['orderby']) ) 
  69. $_GET['orderby'] = $_REQUEST['orderby'] = 'id'; 
  70. if ( !isset($_REQUEST['order']) ) 
  71. $_GET['order'] = $_REQUEST['order'] = 'DESC'; 
  72. $args['count_total'] = false; 
  73.  
  74. if ( isset( $_REQUEST['orderby'] ) ) 
  75. $args['orderby'] = $_REQUEST['orderby']; 
  76.  
  77. if ( isset( $_REQUEST['order'] ) ) 
  78. $args['order'] = $_REQUEST['order']; 
  79.  
  80. if ( ! empty( $_REQUEST['mode'] ) ) { 
  81. $mode = $_REQUEST['mode'] === 'excerpt' ? 'excerpt' : 'list'; 
  82. set_user_setting( 'network_users_list_mode', $mode ); 
  83. } else { 
  84. $mode = get_user_setting( 'network_users_list_mode', 'list' ); 
  85.  
  86. /** This filter is documented in wp-admin/includes/class-wp-users-list-table.php */ 
  87. $args = apply_filters( 'users_list_table_query_args', $args ); 
  88.  
  89. // Query the user IDs for this page 
  90. $wp_user_search = new WP_User_Query( $args ); 
  91.  
  92. $this->items = $wp_user_search->get_results(); 
  93.  
  94. $this->set_pagination_args( array( 
  95. 'total_items' => $wp_user_search->get_total(),  
  96. 'per_page' => $users_per_page,  
  97. ) ); 
  98.  
  99. /** 
  100. * 
  101. * @return array 
  102. */ 
  103. protected function get_bulk_actions() { 
  104. $actions = array(); 
  105. if ( current_user_can( 'delete_users' ) ) 
  106. $actions['delete'] = __( 'Delete' ); 
  107. $actions['spam'] = _x( 'Mark as Spam', 'user' ); 
  108. $actions['notspam'] = _x( 'Not Spam', 'user' ); 
  109.  
  110. return $actions; 
  111.  
  112. /** 
  113. * @access public 
  114. */ 
  115. public function no_items() { 
  116. _e( 'No users found.' ); 
  117.  
  118. /** 
  119. * 
  120. * @global string $role 
  121. * @return array 
  122. */ 
  123. protected function get_views() { 
  124. global $role; 
  125.  
  126. $total_users = get_user_count(); 
  127. $super_admins = get_super_admins(); 
  128. $total_admins = count( $super_admins ); 
  129.  
  130. $class = $role != 'super' ? ' class="current"' : ''; 
  131. $role_links = array(); 
  132. $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>'; 
  133. $class = $role === 'super' ? ' class="current"' : ''; 
  134. $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>'; 
  135.  
  136. return $role_links; 
  137.  
  138. /** 
  139. * @global string $mode 
  140. * @param string $which 
  141. */ 
  142. protected function pagination( $which ) { 
  143. global $mode; 
  144.  
  145. parent::pagination ( $which ); 
  146.  
  147. if ( 'top' === $which ) { 
  148. $this->view_switcher( $mode ); 
  149.  
  150. /** 
  151. * 
  152. * @return array 
  153. */ 
  154. public function get_columns() { 
  155. $users_columns = array( 
  156. 'cb' => '<input type="checkbox" />',  
  157. 'username' => __( 'Username' ),  
  158. 'name' => __( 'Name' ),  
  159. 'email' => __( 'Email' ),  
  160. 'registered' => _x( 'Registered', 'user' ),  
  161. 'blogs' => __( 'Sites' ) 
  162. ); 
  163. /** 
  164. * Filters the columns displayed in the Network Admin Users list table. 
  165. * 
  166. * @since MU 
  167. * 
  168. * @param array $users_columns An array of user columns. Default 'cb', 'username',  
  169. * 'name', 'email', 'registered', 'blogs'. 
  170. */ 
  171. return apply_filters( 'wpmu_users_columns', $users_columns ); 
  172.  
  173. /** 
  174. * 
  175. * @return array 
  176. */ 
  177. protected function get_sortable_columns() { 
  178. return array( 
  179. 'username' => 'login',  
  180. 'name' => 'name',  
  181. 'email' => 'email',  
  182. 'registered' => 'id',  
  183. ); 
  184.  
  185. /** 
  186. * Handles the checkbox column output. 
  187. * 
  188. * @since 4.3.0 
  189. * @access public 
  190. * 
  191. * @param WP_User $user The current WP_User object. 
  192. */ 
  193. public function column_cb( $user ) { 
  194. if ( is_super_admin( $user->ID ) ) { 
  195. return; 
  196. ?> 
  197. <label class="screen-reader-text" for="blog_<?php echo $user->ID; ?>"><?php echo sprintf( __( 'Select %s' ), $user->user_login ); ?></label> 
  198. <input type="checkbox" id="blog_<?php echo $user->ID ?>" name="allusers[]" value="<?php echo esc_attr( $user->ID ) ?>" /> 
  199. <?php 
  200.  
  201. /** 
  202. * Handles the ID column output. 
  203. * 
  204. * @since 4.4.0 
  205. * @access public 
  206. * 
  207. * @param WP_User $user The current WP_User object. 
  208. */ 
  209. public function column_id( $user ) { 
  210. echo $user->ID; 
  211.  
  212. /** 
  213. * Handles the username column output. 
  214. * 
  215. * @since 4.3.0 
  216. * @access public 
  217. * 
  218. * @param WP_User $user The current WP_User object. 
  219. */ 
  220. public function column_username( $user ) { 
  221. $super_admins = get_super_admins(); 
  222. $avatar = get_avatar( $user->user_email, 32 ); 
  223. $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user->ID ) ) ); 
  224.  
  225. echo $avatar; 
  226.  
  227. ?><strong><a href="<?php echo $edit_link; ?>" class="edit"><?php echo $user->user_login; ?></a><?php 
  228. if ( in_array( $user->user_login, $super_admins ) ) { 
  229. echo ' - ' . __( 'Super Admin' ); 
  230. ?></strong> 
  231. <?php 
  232.  
  233. /** 
  234. * Handles the name column output. 
  235. * 
  236. * @since 4.3.0 
  237. * @access public 
  238. * 
  239. * @param WP_User $user The current WP_User object. 
  240. */ 
  241. public function column_name( $user ) { 
  242. echo "$user->first_name $user->last_name"; 
  243.  
  244. /** 
  245. * Handles the email column output. 
  246. * 
  247. * @since 4.3.0 
  248. * @access public 
  249. * 
  250. * @param WP_User $user The current WP_User object. 
  251. */ 
  252. public function column_email( $user ) { 
  253. echo "<a href='" . esc_url( "mailto:$user->user_email" ) . "'>$user->user_email</a>"; 
  254.  
  255. /** 
  256. * Handles the registered date column output. 
  257. * 
  258. * @since 4.3.0 
  259. * @access public 
  260. * 
  261. * @global string $mode 
  262. * 
  263. * @param WP_User $user The current WP_User object. 
  264. */ 
  265. public function column_registered( $user ) { 
  266. global $mode; 
  267. if ( 'list' === $mode ) { 
  268. $date = __( 'Y/m/d' ); 
  269. } else { 
  270. $date = __( 'Y/m/d g:i:s a' ); 
  271. echo mysql2date( $date, $user->user_registered ); 
  272.  
  273. /** 
  274. * @since 4.3.0 
  275. * @access protected 
  276. * 
  277. * @param WP_User $user 
  278. * @param string $classes 
  279. * @param string $data 
  280. * @param string $primary 
  281. */ 
  282. protected function _column_blogs( $user, $classes, $data, $primary ) { 
  283. echo '<td class="', $classes, ' has-row-actions" ', $data, '>'; 
  284. echo $this->column_blogs( $user ); 
  285. echo $this->handle_row_actions( $user, 'blogs', $primary ); 
  286. echo '</td>'; 
  287.  
  288. /** 
  289. * Handles the sites column output. 
  290. * 
  291. * @since 4.3.0 
  292. * @access public 
  293. * 
  294. * @param WP_User $user The current WP_User object. 
  295. */ 
  296. public function column_blogs( $user ) { 
  297. $blogs = get_blogs_of_user( $user->ID, true ); 
  298. if ( ! is_array( $blogs ) ) { 
  299. return; 
  300.  
  301. foreach ( $blogs as $val ) { 
  302. if ( ! can_edit_network( $val->site_id ) ) { 
  303. continue; 
  304.  
  305. $path = ( $val->path === '/' ) ? '' : $val->path; 
  306. echo '<span class="site-' . $val->site_id . '" >'; 
  307. echo '<a href="'. esc_url( network_admin_url( 'site-info.php?id=' . $val->userblog_id ) ) .'">' . str_replace( '.' . get_network()->domain, '', $val->domain . $path ) . '</a>'; 
  308. echo ' <small class="row-actions">'; 
  309. $actions = array(); 
  310. $actions['edit'] = '<a href="'. esc_url( network_admin_url( 'site-info.php?id=' . $val->userblog_id ) ) .'">' . __( 'Edit' ) . '</a>'; 
  311.  
  312. $class = ''; 
  313. if ( $val->spam == 1 ) { 
  314. $class .= 'site-spammed '; 
  315. if ( $val->mature == 1 ) { 
  316. $class .= 'site-mature '; 
  317. if ( $val->deleted == 1 ) { 
  318. $class .= 'site-deleted '; 
  319. if ( $val->archived == 1 ) { 
  320. $class .= 'site-archived '; 
  321.  
  322. $actions['view'] = '<a class="' . $class . '" href="' . esc_url( get_home_url( $val->userblog_id ) ) . '">' . __( 'View' ) . '</a>'; 
  323.  
  324. /** 
  325. * Filters the action links displayed next the sites a user belongs to 
  326. * in the Network Admin Users list table. 
  327. * 
  328. * @since 3.1.0 
  329. * 
  330. * @param array $actions An array of action links to be displayed. 
  331. * Default 'Edit', 'View'. 
  332. * @param int $userblog_id The site ID. 
  333. */ 
  334. $actions = apply_filters( 'ms_user_list_site_actions', $actions, $val->userblog_id ); 
  335.  
  336. $i=0; 
  337. $action_count = count( $actions ); 
  338. foreach ( $actions as $action => $link ) { 
  339. ++$i; 
  340. $sep = ( $i == $action_count ) ? '' : ' | '; 
  341. echo "<span class='$action'>$link$sep</span>"; 
  342. echo '</small></span><br/>'; 
  343.  
  344. /** 
  345. * Handles the default column output. 
  346. * 
  347. * @since 4.3.0 
  348. * @access public 
  349. * 
  350. * @param WP_User $user The current WP_User object. 
  351. * @param string $column_name The current column name. 
  352. */ 
  353. public function column_default( $user, $column_name ) { 
  354. /** This filter is documented in wp-admin/includes/class-wp-users-list-table.php */ 
  355. echo apply_filters( 'manage_users_custom_column', '', $column_name, $user->ID ); 
  356.  
  357. public function display_rows() { 
  358. foreach ( $this->items as $user ) { 
  359. $class = ''; 
  360.  
  361. $status_list = array( 'spam' => 'site-spammed', 'deleted' => 'site-deleted' ); 
  362.  
  363. foreach ( $status_list as $status => $col ) { 
  364. if ( $user->$status ) { 
  365. $class .= " $col"; 
  366.  
  367. ?> 
  368. <tr class="<?php echo trim( $class ); ?>"> 
  369. <?php $this->single_row_columns( $user ); ?> 
  370. </tr> 
  371. <?php 
  372.  
  373. /** 
  374. * Gets the name of the default primary column. 
  375. * 
  376. * @since 4.3.0 
  377. * @access protected 
  378. * 
  379. * @return string Name of the default primary column, in this case, 'username'. 
  380. */ 
  381. protected function get_default_primary_column_name() { 
  382. return 'username'; 
  383.  
  384. /** 
  385. * Generates and displays row action links. 
  386. * 
  387. * @since 4.3.0 
  388. * @access protected 
  389. * 
  390. * @param object $user User being acted upon. 
  391. * @param string $column_name Current column name. 
  392. * @param string $primary Primary column name. 
  393. * @return string Row actions output for users in Multisite. 
  394. */ 
  395. protected function handle_row_actions( $user, $column_name, $primary ) { 
  396. if ( $primary !== $column_name ) { 
  397. return ''; 
  398.  
  399. $super_admins = get_super_admins(); 
  400. $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user->ID ) ) ); 
  401.  
  402. $actions = array(); 
  403. $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>'; 
  404.  
  405. if ( current_user_can( 'delete_user', $user->ID ) && ! in_array( $user->user_login, $super_admins ) ) { 
  406. $actions['delete'] = '<a href="' . $delete = esc_url( network_admin_url( add_query_arg( '_wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), wp_nonce_url( 'users.php', 'deleteuser' ) . '&action=deleteuser&id=' . $user->ID ) ) ) . '" class="delete">' . __( 'Delete' ) . '</a>'; 
  407.  
  408. /** 
  409. * Filters the action links displayed under each user in the Network Admin Users list table. 
  410. * 
  411. * @since 3.2.0 
  412. * 
  413. * @param array $actions An array of action links to be displayed. 
  414. * Default 'Edit', 'Delete'. 
  415. * @param WP_User $user WP_User object. 
  416. */ 
  417. $actions = apply_filters( 'ms_user_row_actions', $actions, $user ); 
  418. return $this->row_actions( $actions ); 
.