PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

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