PageRenderTime 59ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/html/blog/wp-admin/includes/class-wp-users-list-table.php

https://github.com/jimmytidey/jimmytidey.co.uk
PHP | 457 lines | 238 code | 63 blank | 156 comment | 48 complexity | 7a04bf7b080447c2c3c419836551ee8d MD5 | raw file
  1. <?php
  2. /**
  3. * Users List Table class.
  4. *
  5. * @since 3.1.0
  6. * @access private
  7. *
  8. * @package WordPress
  9. * @subpackage List_Table
  10. */
  11. class WP_Users_List_Table extends WP_List_Table {
  12. /**
  13. * Site ID to generate the Users list table for.
  14. *
  15. * @since 3.1.0
  16. * @access public
  17. * @var int
  18. */
  19. var $site_id;
  20. /**
  21. * Whether or not the current Users list table is for Multisite.
  22. *
  23. * @since 3.1.0
  24. * @access public
  25. * @var bool
  26. */
  27. var $is_site_users;
  28. /**
  29. * Constructor.
  30. *
  31. * @since 3.1.0
  32. * @access public
  33. */
  34. function __construct( $args = array() ) {
  35. parent::__construct( array(
  36. 'singular' => 'user',
  37. 'plural' => 'users',
  38. 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
  39. ) );
  40. $this->is_site_users = 'site-users-network' == $this->screen->id;
  41. if ( $this->is_site_users )
  42. $this->site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
  43. }
  44. /**
  45. * Check the current user's permissions.
  46. *
  47. * @since 3.1.0
  48. * @access public
  49. */
  50. function ajax_user_can() {
  51. if ( $this->is_site_users )
  52. return current_user_can( 'manage_sites' );
  53. else
  54. return current_user_can( 'list_users' );
  55. }
  56. /**
  57. * Prepare the users list for display.
  58. *
  59. * @since 3.1.0
  60. * @access public
  61. */
  62. function prepare_items() {
  63. global $role, $usersearch;
  64. $usersearch = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : '';
  65. $role = isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : '';
  66. $per_page = ( $this->is_site_users ) ? 'site_users_network_per_page' : 'users_per_page';
  67. $users_per_page = $this->get_items_per_page( $per_page );
  68. $paged = $this->get_pagenum();
  69. $args = array(
  70. 'number' => $users_per_page,
  71. 'offset' => ( $paged-1 ) * $users_per_page,
  72. 'role' => $role,
  73. 'search' => $usersearch,
  74. 'fields' => 'all_with_meta'
  75. );
  76. if ( '' !== $args['search'] )
  77. $args['search'] = '*' . $args['search'] . '*';
  78. if ( $this->is_site_users )
  79. $args['blog_id'] = $this->site_id;
  80. if ( isset( $_REQUEST['orderby'] ) )
  81. $args['orderby'] = $_REQUEST['orderby'];
  82. if ( isset( $_REQUEST['order'] ) )
  83. $args['order'] = $_REQUEST['order'];
  84. // Query the user IDs for this page
  85. $wp_user_search = new WP_User_Query( $args );
  86. $this->items = $wp_user_search->get_results();
  87. $this->set_pagination_args( array(
  88. 'total_items' => $wp_user_search->get_total(),
  89. 'per_page' => $users_per_page,
  90. ) );
  91. }
  92. /**
  93. * Output 'no users' message.
  94. *
  95. * @since 3.1.0
  96. * @access public
  97. */
  98. function no_items() {
  99. _e( 'No matching users were found.' );
  100. }
  101. /**
  102. * Return an associative array listing all the views that can be used
  103. * with this table.
  104. *
  105. * Provides a list of roles and user count for that role for easy
  106. * filtering of the user table.
  107. *
  108. * @since 3.1.0
  109. * @access public
  110. *
  111. * @return array An array of HTML links, one for each view.
  112. */
  113. function get_views() {
  114. global $wp_roles, $role;
  115. if ( $this->is_site_users ) {
  116. $url = 'site-users.php?id=' . $this->site_id;
  117. switch_to_blog( $this->site_id );
  118. $users_of_blog = count_users();
  119. restore_current_blog();
  120. } else {
  121. $url = 'users.php';
  122. $users_of_blog = count_users();
  123. }
  124. $total_users = $users_of_blog['total_users'];
  125. $avail_roles =& $users_of_blog['avail_roles'];
  126. unset($users_of_blog);
  127. $current_role = false;
  128. $class = empty($role) ? ' class="current"' : '';
  129. $role_links = array();
  130. $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>';
  131. foreach ( $wp_roles->get_names() as $this_role => $name ) {
  132. if ( !isset($avail_roles[$this_role]) )
  133. continue;
  134. $class = '';
  135. if ( $this_role == $role ) {
  136. $current_role = $role;
  137. $class = ' class="current"';
  138. }
  139. $name = translate_user_role( $name );
  140. /* translators: User role name with count */
  141. $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n( $avail_roles[$this_role] ) );
  142. $role_links[$this_role] = "<a href='" . esc_url( add_query_arg( 'role', $this_role, $url ) ) . "'$class>$name</a>";
  143. }
  144. return $role_links;
  145. }
  146. /**
  147. * Retrieve an associative array of bulk actions available on this table.
  148. *
  149. * @since 3.1.0
  150. * @access public
  151. *
  152. * @return array Array of bulk actions.
  153. */
  154. function get_bulk_actions() {
  155. $actions = array();
  156. if ( is_multisite() ) {
  157. if ( current_user_can( 'remove_users' ) )
  158. $actions['remove'] = __( 'Remove' );
  159. } else {
  160. if ( current_user_can( 'delete_users' ) )
  161. $actions['delete'] = __( 'Delete' );
  162. }
  163. return $actions;
  164. }
  165. /**
  166. * Output the controls to allow user roles to be changed in bulk.
  167. *
  168. * @since 3.1.0
  169. * @access public
  170. *
  171. * @param string $which Whether this is being invoked above ("top")
  172. * or below the table ("bottom").
  173. */
  174. function extra_tablenav( $which ) {
  175. if ( 'top' != $which )
  176. return;
  177. ?>
  178. <div class="alignleft actions">
  179. <?php if ( current_user_can( 'promote_users' ) ) : ?>
  180. <label class="screen-reader-text" for="new_role"><?php _e( 'Change role to&hellip;' ) ?></label>
  181. <select name="new_role" id="new_role">
  182. <option value=''><?php _e( 'Change role to&hellip;' ) ?></option>
  183. <?php wp_dropdown_roles(); ?>
  184. </select>
  185. <?php
  186. submit_button( __( 'Change' ), 'button', 'changeit', false );
  187. endif;
  188. /**
  189. * Fires just before the closing div containing the bulk role-change controls
  190. * in the Users list table.
  191. *
  192. * @since 3.5.0
  193. */
  194. do_action( 'restrict_manage_users' );
  195. echo '</div>';
  196. }
  197. /**
  198. * Capture the bulk action required, and return it.
  199. *
  200. * Overridden from the base class implementation to capture
  201. * the role change drop-down.
  202. *
  203. * @since 3.1.0
  204. * @access public
  205. *
  206. * @return string The bulk action required.
  207. */
  208. function current_action() {
  209. if ( isset($_REQUEST['changeit']) && !empty($_REQUEST['new_role']) )
  210. return 'promote';
  211. return parent::current_action();
  212. }
  213. /**
  214. * Get a list of columns for the list table.
  215. *
  216. * @since 3.1.0
  217. * @access public
  218. *
  219. * @return array Array in which the key is the ID of the column,
  220. * and the value is the description.
  221. */
  222. function get_columns() {
  223. $c = array(
  224. 'cb' => '<input type="checkbox" />',
  225. 'username' => __( 'Username' ),
  226. 'name' => __( 'Name' ),
  227. 'email' => __( 'E-mail' ),
  228. 'role' => __( 'Role' ),
  229. 'posts' => __( 'Posts' )
  230. );
  231. if ( $this->is_site_users )
  232. unset( $c['posts'] );
  233. return $c;
  234. }
  235. /**
  236. * Get a list of sortable columns for the list table.
  237. *
  238. * @since 3.1.0
  239. * @access public
  240. *
  241. * @return array Array of sortable columns.
  242. */
  243. function get_sortable_columns() {
  244. $c = array(
  245. 'username' => 'login',
  246. 'name' => 'name',
  247. 'email' => 'email',
  248. );
  249. if ( $this->is_site_users )
  250. unset( $c['posts'] );
  251. return $c;
  252. }
  253. /**
  254. * Generate the list table rows.
  255. *
  256. * @since 3.1.0
  257. * @access public
  258. */
  259. function display_rows() {
  260. // Query the post counts for this page
  261. if ( ! $this->is_site_users )
  262. $post_counts = count_many_users_posts( array_keys( $this->items ) );
  263. $editable_roles = array_keys( get_editable_roles() );
  264. $style = '';
  265. foreach ( $this->items as $userid => $user_object ) {
  266. if ( count( $user_object->roles ) <= 1 ) {
  267. $role = reset( $user_object->roles );
  268. } elseif ( $roles = array_intersect( array_values( $user_object->roles ), $editable_roles ) ) {
  269. $role = reset( $roles );
  270. } else {
  271. $role = reset( $user_object->roles );
  272. }
  273. if ( is_multisite() && empty( $user_object->allcaps ) )
  274. continue;
  275. $style = ( ' class="alternate"' == $style ) ? '' : ' class="alternate"';
  276. echo "\n\t" . $this->single_row( $user_object, $style, $role, isset( $post_counts ) ? $post_counts[ $userid ] : 0 );
  277. }
  278. }
  279. /**
  280. * Generate HTML for a single row on the users.php admin panel.
  281. *
  282. * @since 3.1.0
  283. * @access public
  284. *
  285. * @param object $user_object The current user object.
  286. * @param string $style Optional. Style attributes added to the <tr> element.
  287. * Must be sanitized. Default empty.
  288. * @param string $role Optional. Key for the $wp_roles array. Default empty.
  289. * @param int $numposts Optional. Post count to display for this user. Defaults
  290. * to zero, as in, a new user has made zero posts.
  291. * @return string Output for a single row.
  292. */
  293. function single_row( $user_object, $style = '', $role = '', $numposts = 0 ) {
  294. global $wp_roles;
  295. if ( !( is_object( $user_object ) && is_a( $user_object, 'WP_User' ) ) )
  296. $user_object = get_userdata( (int) $user_object );
  297. $user_object->filter = 'display';
  298. $email = $user_object->user_email;
  299. if ( $this->is_site_users )
  300. $url = "site-users.php?id={$this->site_id}&amp;";
  301. else
  302. $url = 'users.php?';
  303. $checkbox = '';
  304. // Check if the user for this row is editable
  305. if ( current_user_can( 'list_users' ) ) {
  306. // Set up the user editing link
  307. $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user_object->ID ) ) );
  308. // Set up the hover actions for this user
  309. $actions = array();
  310. if ( current_user_can( 'edit_user', $user_object->ID ) ) {
  311. $edit = "<strong><a href=\"$edit_link\">$user_object->user_login</a></strong><br />";
  312. $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
  313. } else {
  314. $edit = "<strong>$user_object->user_login</strong><br />";
  315. }
  316. if ( !is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'delete_user', $user_object->ID ) )
  317. $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url( "users.php?action=delete&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Delete' ) . "</a>";
  318. if ( is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'remove_user', $user_object->ID ) )
  319. $actions['remove'] = "<a class='submitdelete' href='" . wp_nonce_url( $url."action=remove&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Remove' ) . "</a>";
  320. /**
  321. * Filter the action links displayed under each user in the Users list table.
  322. *
  323. * @since 2.8.0
  324. *
  325. * @param array $actions An array of action links to be displayed.
  326. * Default 'Edit', 'Delete' for single site, and
  327. * 'Edit', 'Remove' for Multisite.
  328. * @param WP_User $user_object WP_User object for the currently-listed user.
  329. */
  330. $actions = apply_filters( 'user_row_actions', $actions, $user_object );
  331. $edit .= $this->row_actions( $actions );
  332. // Set up the checkbox ( because the user is editable, otherwise it's empty )
  333. $checkbox = '<label class="screen-reader-text" for="cb-select-' . $user_object->ID . '">' . sprintf( __( 'Select %s' ), $user_object->user_login ) . '</label>'
  334. . "<input type='checkbox' name='users[]' id='user_{$user_object->ID}' class='$role' value='{$user_object->ID}' />";
  335. } else {
  336. $edit = '<strong>' . $user_object->user_login . '</strong>';
  337. }
  338. $role_name = isset( $wp_roles->role_names[$role] ) ? translate_user_role( $wp_roles->role_names[$role] ) : __( 'None' );
  339. $avatar = get_avatar( $user_object->ID, 32 );
  340. $r = "<tr id='user-$user_object->ID'$style>";
  341. list( $columns, $hidden ) = $this->get_column_info();
  342. foreach ( $columns as $column_name => $column_display_name ) {
  343. $class = "class=\"$column_name column-$column_name\"";
  344. $style = '';
  345. if ( in_array( $column_name, $hidden ) )
  346. $style = ' style="display:none;"';
  347. $attributes = "$class$style";
  348. switch ( $column_name ) {
  349. case 'cb':
  350. $r .= "<th scope='row' class='check-column'>$checkbox</th>";
  351. break;
  352. case 'username':
  353. $r .= "<td $attributes>$avatar $edit</td>";
  354. break;
  355. case 'name':
  356. $r .= "<td $attributes>$user_object->first_name $user_object->last_name</td>";
  357. break;
  358. case 'email':
  359. $r .= "<td $attributes><a href='mailto:$email' title='" . esc_attr( sprintf( __( 'E-mail: %s' ), $email ) ) . "'>$email</a></td>";
  360. break;
  361. case 'role':
  362. $r .= "<td $attributes>$role_name</td>";
  363. break;
  364. case 'posts':
  365. $attributes = 'class="posts column-posts num"' . $style;
  366. $r .= "<td $attributes>";
  367. if ( $numposts > 0 ) {
  368. $r .= "<a href='edit.php?author=$user_object->ID' title='" . esc_attr__( 'View posts by this author' ) . "' class='edit'>";
  369. $r .= $numposts;
  370. $r .= '</a>';
  371. } else {
  372. $r .= 0;
  373. }
  374. $r .= "</td>";
  375. break;
  376. default:
  377. $r .= "<td $attributes>";
  378. /**
  379. * Filter the display output of custom columns in the Users list table.
  380. *
  381. * @since 2.8.0
  382. *
  383. * @param string $output Custom column output. Default empty.
  384. * @param string $column_name Column name.
  385. * @param int $user_id ID of the currently-listed user.
  386. */
  387. $r .= apply_filters( 'manage_users_custom_column', '', $column_name, $user_object->ID );
  388. $r .= "</td>";
  389. }
  390. }
  391. $r .= '</tr>';
  392. return $r;
  393. }
  394. }