PageRenderTime 1830ms CodeModel.GetById 32ms RepoModel.GetById 6ms app.codeStats 0ms

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

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