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-user-edit.php

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

245 lines 5.8 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 edit user screen, such as multiple user roles.
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 * Edit user screen class.
17 *
18 * @since 2.0.0
19 * @access public
20 */
21 final class User_Edit {
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;
31
32 /**
33 * Sets up needed actions/filters for the admin to initialize.
34 *
35 * @since 2.0.0
36 * @access public
37 * @return void
38 */
39 public function __construct() {
40
41 // If multiple roles per user is not enabled, bail.
42 if ( ! members_multiple_user_roles_enabled() )
43 return;
44
45 // Only run our customization on the 'user-edit.php' page in the admin.
46 add_action( 'load-user-edit.php', array( $this, 'load_user_edit' ) );
47 add_action( 'load-profile.php', array( $this, 'load_user_edit' ) );
48 }
49
50 /**
51 * Adds actions/filters on load.
52 *
53 * @since 2.0.0
54 * @access public
55 * @return void
56 */
57 public function load_user_edit() {
58
59 // Handle scripts and styles.
60 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
61 add_action( 'admin_footer', array( $this, 'print_scripts' ), 25 );
62 add_action( 'admin_head', array( $this, 'print_styles' ) );
63
64 add_action( 'show_user_profile', array( $this, 'profile_fields' ) );
65 add_action( 'edit_user_profile', array( $this, 'profile_fields' ) );
66
67 // Must use `profile_update` to change role. Otherwise, WP will wipe it out.
68 add_action( 'profile_update', array( $this, 'role_update' ), 10, 2 );
69 }
70
71 /**
72 * Adds custom profile fields.
73 *
74 * @since 2.0.0
75 * @access public
76 * @param object $user
77 * @return void
78 */
79 public function profile_fields( $user ) {
80 global $wp_roles;
81
82 if ( ! current_user_can( 'promote_users' ) || ! current_user_can( 'edit_user', $user->ID ) )
83 return;
84
85 $user_roles = (array) $user->roles;
86
87 $roles = members_get_roles();
88
89 ksort( $roles );
90
91 wp_nonce_field( 'new_user_roles', 'members_new_user_roles_nonce' ); ?>
92
93 <h2><?php esc_html_e( 'Roles', 'members' ); ?></h2>
94
95 <table class="form-table">
96
97 <tr>
98 <th><?php esc_html_e( 'User Roles', 'members' ); ?></th>
99
100 <td>
101 <div class="wp-tab-panel">
102 <ul>
103 <?php foreach ( $roles as $role ) : ?>
104
105 <?php if ( members_is_role_editable( $role->name ) ) :?>
106 <li>
107 <label>
108 <input type="checkbox" name="members_user_roles[]" value="<?php echo esc_attr( $role->name ); ?>" <?php checked( in_array( $role->name, $user_roles ) ); ?> />
109 <?php echo esc_html( $role->get( 'label' ) ); ?>
110 </label>
111 </li>
112 <?php endif; ?>
113
114 <?php endforeach; ?>
115 </ul>
116 </div>
117 </td>
118 </tr>
119
120 </table>
121 <?php }
122
123 /**
124 * Callback function for handling user role changes. Note that we needed to execute this function
125 * on a different hook, `profile_update`. Using the normal hooks on the edit user screen won't work
126 * because WP will wipe out the role.
127 *
128 * @since 2.0.0
129 * @access public
130 * @param int $user_id
131 * @param object $old_user_data
132 * @return void
133 */
134 public function role_update( $user_id, $old_user_data ) {
135
136 // If the current user can't promote users or edit this particular user, bail.
137 if ( ! current_user_can( 'promote_users' ) || ! current_user_can( 'edit_user', $user_id ) )
138 return;
139
140 // Is this a role change?
141 if ( ! isset( $_POST['members_new_user_roles_nonce'] ) || ! wp_verify_nonce( $_POST['members_new_user_roles_nonce'], 'new_user_roles' ) )
142 return;
143
144 // Create a new user object.
145 //$user = new WP_User( $user_id );
146
147 // If we have an array of roles.
148 if ( ! empty( $_POST['members_user_roles'] ) ) {
149
150 // Get the current user roles.
151 $old_roles = (array) $old_user_data->roles;
152
153 // Sanitize the posted roles.
154 $new_roles = array_map( 'members_sanitize_role', $_POST['members_user_roles'] );
155
156 // Loop through the posted roles.
157 foreach ( $new_roles as $new_role ) {
158
159 // If the user doesn't already have the role, add it.
160 if ( members_is_role_editable( $new_role ) && ! in_array( $new_role, (array) $old_user_data->roles ) )
161 $old_user_data->add_role( $new_role );
162 }
163
164 // Loop through the current user roles.
165 foreach ( $old_roles as $old_role ) {
166
167 // If the role is editable and not in the new roles array, remove it.
168 if ( members_is_role_editable( $old_role ) && ! in_array( $old_role, $new_roles ) )
169 $old_user_data->remove_role( $old_role );
170 }
171
172 // If the posted roles are empty.
173 } else {
174
175 // Loop through the current user roles.
176 foreach ( (array) $old_user_data->roles as $old_role ) {
177
178 // Remove the role if it is editable.
179 if ( members_is_role_editable( $old_role ) )
180 $old_user_data->remove_role( $old_role );
181 }
182 }
183 }
184
185 /**
186 * Enqueue scripts.
187 *
188 * @since 2.0.0
189 * @access public
190 * @return void
191 */
192 public function enqueue() {
193
194 wp_enqueue_script( 'jquery' );
195 }
196
197 /**
198 * Enqueue the plugin admin CSS.
199 *
200 * @since 2.0.0
201 * @access public
202 * @return void
203 */
204 public function print_scripts() { ?>
205
206 <script>
207 jQuery( document ).ready( function() {
208
209 jQuery( '.user-role-wrap' ).remove();
210 } );
211 </script>
212
213 <?php }
214
215 /**
216 * Enqueue the plugin admin CSS.
217 *
218 * @since 2.0.0
219 * @access public
220 * @return void
221 */
222 public function print_styles() { ?>
223
224 <style type="text/css">.user-role-wrap{ display: none !important; }</style>
225
226 <?php }
227
228 /**
229 * Returns the instance.
230 *
231 * @since 2.0.0
232 * @access public
233 * @return object
234 */
235 public static function get_instance() {
236
237 if ( ! self::$instance )
238 self::$instance = new self;
239
240 return self::$instance;
241 }
242 }
243
244 User_Edit::get_instance();
245