PluginProbe
Members – Membership & User Role Editor Plugin / 3.2.24
Members – Membership & User Role Editor Plugin v3.2.24
3.2.25 3.2.26 3.2.24 3.2.23 3.2.22 3.2.21 trunk 0.1 0.1.1 0.2 0.2.1 0.2.2 0.2.3 0.2.4 0.2.5 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 2.0.0 2.0.1 2.0.2 All 66 releases
members / admin / class-manage-users.php

class-manage-users.php in Members – Membership & User Role Editor Plugin 3.2.24, at admin/class-manage-users.php

467 lines 12.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles custom functionality on the manage users screen.
4 *
5 * @package Members
6 * @subpackage Admin
7 * @author The MemberPress Team
8 * @copyright Copyright (c) 2009 - 2018, The MemberPress Team
9 * @link https://members-plugin.com/
10 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
11 */
12 namespace Members\Admin;
13
14 defined('ABSPATH') || exit;
15 /**
16 * Manager users screen class.
17 *
18 * @since 2.0.0
19 * @access public
20 */
21 final class Manage_Users {
22
23 /**
24 * Holds the instances of this class.
25 *
26 * @since 2.0.0
27 * @access private
28 * @var object
29 */
30 private static $instance = null;
31
32 /**
33 * Custom admin notices.
34 *
35 * @since 2.0.0
36 * @access public
37 * @var array
38 */
39 public $notices = array();
40
41 /**
42 * Constructore method.
43 *
44 * @since 2.0.0
45 * @access private
46 * @return void
47 */
48 private function __construct() {}
49
50 /**
51 * Sets up needed actions/filters.
52 *
53 * @since 2.0.0
54 * @access private
55 * @return void
56 */
57 private function setup_actions() {
58
59 // If multiple roles per user is not enabled, bail.
60 if ( ! members_multiple_user_roles_enabled() )
61 return;
62
63 // Add our primary actions to the load hook.
64 add_action( 'load-users.php', array( $this, 'load' ) );
65 add_action( 'load-users.php', array( $this, 'role_bulk_add' ) );
66 add_action( 'load-users.php', array( $this, 'role_bulk_remove' ) );
67 }
68
69 /**
70 * Adds actions/filters on load.
71 *
72 * @since 2.0.0
73 * @access public
74 * @return void
75 */
76 public function load() {
77
78 // Add custom bulk fields.
79 add_action( 'restrict_manage_users', array( $this, 'bulk_fields' ), 5 );
80
81 // Custom manage users columns.
82 add_filter( 'manage_users_columns', array( $this, 'manage_users_columns' ) );
83
84 // Handle scripts and styles.
85 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
86 add_action( 'admin_footer', array( $this, 'print_scripts' ), 25 );
87 add_action( 'admin_head', array( $this, 'print_styles' ) );
88
89 // If there was an update, add notices if they're from our plugin.
90 if ( isset( $_GET['update'] ) ) {
91
92 $action = sanitize_key( $_GET['update'] );
93
94 // If a role was added.
95 if ( 'members-role-added' === $action ) {
96
97 $this->notices['role_added'] = array( 'message' => esc_html__( 'Role added to selected users.', 'members' ), 'type' => 'success' );
98
99 // If a role was removed.
100 } elseif ( 'members-role-removed' === $action ) {
101
102 $this->notices['role_removed'] = array( 'message' => esc_html__( 'Role removed from selected users.', 'members' ), 'type' => 'success' );
103
104 } elseif ( 'members-error-remove-admin' === $action ) {
105
106 $this->notices['error_remove_admin'] = array( 'message' => esc_html__( 'The current user&#8217;s role must have user editing capabilities.', 'members' ), 'type' => 'error' );
107 $this->notices['role_removed'] = array( 'message' => esc_html__( 'Role removed from other selected users.', 'members' ), 'type' => 'success' );
108 }
109
110 // If we have notices, hook them in.
111 if ( $this->notices )
112 add_action( 'admin_notices', array( $this, 'notices' ) );
113 }
114 }
115
116 /**
117 * Adds a single role to users in bulk.
118 *
119 * @since 2.0.0
120 * @access public
121 * @return void
122 */
123 public function role_bulk_add() {
124
125 // Bail if we ain't got users.
126 if ( empty( $_REQUEST['users'] ) )
127 return;
128
129 // Figure out if we have a role selected.
130 if ( ! empty( $_REQUEST['members-add-role-top'] ) && ! empty( $_REQUEST['members-add-role-submit-top'] ) )
131 $role = members_sanitize_role( $_REQUEST['members-add-role-top'] );
132
133 elseif ( ! empty( $_REQUEST['members-add-role-bottom'] ) && ! empty( $_REQUEST['members-add-role-submit-bottom'] ) )
134 $role = members_sanitize_role( $_REQUEST['members-add-role-bottom'] );
135
136 // Get only editable roles.
137 $editable_roles = members_get_editable_roles();
138
139 // If we don't have a role or the role is not editable, bail.
140 if ( empty( $role ) || ! in_array( $role, $editable_roles ) )
141 return;
142
143 // Validate our nonce.
144 check_admin_referer( 'members-bulk-users', 'members-bulk-users-nonce' );
145
146 // If the current user cannot promote users, bail.
147 if ( ! current_user_can( 'promote_users' ) )
148 return;
149
150 // Loop through the users and add the role if possible.
151 foreach ( (array) $_REQUEST['users'] as $user_id ) {
152
153 $user_id = absint( $user_id );
154
155 // If the user doesn't already belong to the blog, bail.
156 if ( is_multisite() && ! is_user_member_of_blog( $user_id ) ) {
157
158 wp_die(
159 sprintf(
160 '<h1>%s</h1> <p>%s</p>',
161 esc_html__( 'Whoah, partner!', 'members' ),
162 esc_html__( 'One of the selected users is not a member of this site.', 'members' )
163 ),
164 403
165 );
166 }
167
168 // Check that the current user can promote this specific user.
169 if ( ! current_user_can( 'promote_user', $user_id ) )
170 continue;
171
172 // Get the user object.
173 $user = new \WP_User( $user_id );
174
175 // If the user doesn't have the role, add it.
176 if ( ! in_array( $role, $user->roles ) )
177 $user->add_role( $role );
178 }
179
180 // Redirect to the users screen.
181 wp_redirect( add_query_arg( 'update', 'members-role-added', 'users.php' ) );
182 }
183
184 /**
185 * Removes a single role from users in bulk.
186 *
187 * @since 2.0.0
188 * @access public
189 * @return void
190 */
191 public function role_bulk_remove() {
192
193 // Bail if we ain't got users.
194 if ( empty( $_REQUEST['users'] ) )
195 return;
196
197 // Figure out if we have a role selected.
198 if ( ! empty( $_REQUEST['members-remove-role-top'] ) && ! empty( $_REQUEST['members-remove-role-submit-top'] ) )
199 $role = members_sanitize_role( $_REQUEST['members-remove-role-top'] );
200
201 elseif ( ! empty( $_REQUEST['members-remove-role-bottom'] ) && ! empty( $_REQUEST['members-remove-role-submit-bottom'] ) )
202 $role = members_sanitize_role( $_REQUEST['members-remove-role-bottom'] );
203
204 // Get only editable roles.
205 $editable_roles = members_get_editable_roles();
206
207 // If we don't have a role or the role is not editable, bail.
208 if ( empty( $role ) || ! in_array( $role, $editable_roles ) )
209 return;
210
211 // Validate our nonce.
212 check_admin_referer( 'members-bulk-users', 'members-bulk-users-nonce' );
213
214 // If the current user cannot promote users, bail.
215 if ( ! current_user_can( 'promote_users' ) )
216 return;
217
218 // Get the current user.
219 $current_user = wp_get_current_user();
220
221 $m_role = members_get_role( $role );
222
223 $update = 'members-role-removed';
224
225 // Loop through the users and remove the role if possible.
226 foreach ( (array) $_REQUEST['users'] as $user_id ) {
227
228 $user_id = absint( $user_id );
229
230 // If the user doesn't already belong to the blog, bail.
231 if ( is_multisite() && ! is_user_member_of_blog( $user_id ) ) {
232
233 wp_die(
234 sprintf(
235 '<h1>%s</h1> <p>%s</p>',
236 esc_html__( 'Whoah, partner!', 'members' ),
237 esc_html__( 'One of the selected users is not a member of this site.', 'members' )
238 ),
239 403
240 );
241 }
242
243 // Check that the current user can promote this specific user.
244 if ( ! current_user_can( 'promote_user', $user_id ) )
245 continue;
246
247 $is_current_user = $user_id == $current_user->ID;
248 $role_can_promote = in_array( 'promote_users', $m_role->granted_caps );
249 $can_manage_network = is_multisite() && current_user_can( 'manage_network_users' );
250
251 // If the removed role has the `promote_users` cap and user is removing it from themselves.
252 if ( $is_current_user && $role_can_promote && ! $can_manage_network ) {
253
254 $can_remove = false;
255
256 // Loop through the current user's roles.
257 foreach ( $current_user->roles as $_r ) {
258
259 // If the current user has another role that can promote users, it's
260 // safe to remove the role. Else, the current user needs to keep
261 // the role.
262 if ( $role !== $_r && in_array( 'promote_users', members_get_role( $_r )->granted_caps ) ) {
263
264 $can_remove = true;
265 break;
266 }
267 }
268
269 if ( ! $can_remove ) {
270 $update = 'members-error-remove-admin';
271 continue;
272 }
273 }
274
275 // Get the user object.
276 $user = new \WP_User( $user_id );
277
278 // If the user has the role, remove it.
279 if ( in_array( $role, $user->roles ) )
280 $user->remove_role( $role );
281 }
282
283 // Redirect to the users screen.
284 wp_redirect( add_query_arg( 'update', $update, 'users.php' ) );
285 }
286
287 /**
288 * Print admin notices.
289 *
290 * @since 2.0.0
291 * @access public
292 * @param string $which
293 * @return void
294 */
295 public function notices() {
296
297 if ( $this->notices ) : ?>
298
299 <?php foreach ( $this->notices as $notice ) : ?>
300
301 <div class="notice notice-<?php echo esc_attr( $notice['type'] ); ?> is-dismissible">
302 <?php echo wpautop( '<strong>' . $notice['message'] . '</strong>' ); ?>
303 </div>
304
305 <?php endforeach;
306
307 endif;
308 }
309
310 /**
311 * Outputs "add role" and "remove role" dropdown select fields.
312 *
313 * @since 2.0.0
314 * @access public
315 * @param string $which
316 * @return void
317 */
318 public function bulk_fields( $which ) {
319
320 if ( ! current_user_can( 'promote_users' ) )
321 return;
322
323 wp_nonce_field( 'members-bulk-users', 'members-bulk-users-nonce' ); ?>
324
325 <label class="screen-reader-text" for="<?php echo esc_attr( "members-add-role-{$which}" ); ?>">
326 <?php esc_html_e( 'Add role&hellip;', 'members' ); ?>
327 </label>
328
329 <select name="<?php echo esc_attr( "members-add-role-{$which}" ); ?>" id="<?php echo esc_attr( "members-add-role-{$which}" ); ?>" style="display: inline-block; float: none;">
330 <option value=""><?php esc_html_e( 'Add role&hellip;', 'members' ); ?></option>
331 <?php wp_dropdown_roles(); ?>
332 </select>
333
334 <?php submit_button( esc_html__( 'Add', 'members' ), 'secondary', esc_attr( "members-add-role-submit-{$which}" ), false ); ?>
335
336 <label class="screen-reader-text" for="<?php echo esc_attr( "members-remove-role-{$which}" ); ?>">
337 <?php esc_html_e( 'Remove role&hellip;', 'members' ); ?>
338 </label>
339
340 <select name="<?php echo esc_attr( "members-remove-role-{$which}" ); ?>" id="<?php echo esc_attr( "members-remove-role-{$which}" ); ?>" style="display: inline-block; float: none;">
341 <option value=""><?php esc_html_e( 'Remove role&hellip;', 'members' ); ?></option>
342 <?php wp_dropdown_roles(); ?>
343 </select>
344
345 <?php submit_button( esc_html__( 'Remove', 'members' ), 'secondary', esc_attr( "members-remove-role-submit-{$which}" ), false );
346 }
347
348 /**
349 * Handles table column headers.
350 *
351 * @since 2.0.0
352 * @access public
353 * @param array $columns
354 * @return array
355 */
356 public function manage_users_columns( $columns ) {
357
358 // Make sure role column is named correctly.
359 if ( isset( $columns['role'] ) )
360 $columns['role'] = esc_html__( 'Roles', 'members' );
361
362 return $columns;
363 }
364
365 /**
366 * Handles the output of the roles column on the `users.php` screen.
367 *
368 * @since 2.0.0
369 * @access public
370 * @param string $output
371 * @param string $column
372 * @param int $user_id
373 * @return string
374 */
375 public function manage_users_custom_column( $output, $column, $user_id ) {
376
377 if ( 'roles' === $column ) {
378
379 $user = new \WP_User( $user_id );
380
381 $user_roles = array();
382 $output = esc_html__( 'None', 'members' );
383
384 if ( is_array( $user->roles ) ) {
385
386 foreach ( $user->roles as $role ) {
387
388 if ( members_role_exists( $role ) )
389 $user_roles[] = members_translate_role( $role );
390 }
391
392 $output = join( ', ', $user_roles );
393 }
394 }
395
396 return $output;
397 }
398
399 /**
400 * Enqueue scripts.
401 *
402 * @since 2.0.0
403 * @access public
404 * @return void
405 */
406 public function enqueue() {
407
408 wp_enqueue_script( 'jquery' );
409 }
410
411 /**
412 * Enqueue the plugin admin CSS.
413 *
414 * @since 2.0.0
415 * @access public
416 * @return void
417 */
418 public function print_scripts() { ?>
419
420 <script>
421 jQuery( document ).ready( function() {
422
423 jQuery(
424 'label[for="new_role"], label[for="new_role2"], #new_role, #new_role2, #changeit, #changeit2'
425 ).remove();
426 } );
427 </script>
428
429 <?php }
430
431 /**
432 * Hides the core WP change role form fields because these are hardcoded in.
433 *
434 * @since 2.0.0
435 * @access public
436 * @return void
437 */
438 public function print_styles() { ?>
439
440 <style type="text/css">
441 label[for="new_role"], #new_role, #changeit,
442 label[for="new_role2"], #new_role2, #changeit2 { display: none !important; }
443 </style>
444
445 <?php }
446
447 /**
448 * Returns the instance.
449 *
450 * @since 2.0.0
451 * @access public
452 * @return object
453 */
454 public static function get_instance() {
455
456 if ( is_null( self::$instance ) ) {
457 self::$instance = new self;
458
459 self::$instance->setup_actions();
460 }
461
462 return self::$instance;
463 }
464 }
465
466 Manage_Users::get_instance();
467