PluginProbe
Groups – Memberships and Access Control / 3.9.0
Groups – Memberships and Access Control v3.9.0
4.7.1 4.7.0 4.6.0 4.5.0 4.4.0 4.3.0 trunk 1.0.0-beta-1 1.0.0-beta-2 1.0.0-beta-3 1.0.0-beta-3b 1.0.0-beta-3c 1.0.0-beta-3d 1.1.4 1.1.5 1.10.0 1.10.1 1.10.2 1.10.3 1.11.0 1.11.1 1.11.2 1.11.3 1.12.0 1.13.0 All 131 releases
groups / lib / admin / class-groups-admin-user-profile.php

class-groups-admin-user-profile.php in Groups – Memberships and Access Control 3.9.0, at lib/admin/class-groups-admin-user-profile.php

311 lines 10.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * class-groups-admin-user-profile.php
4 *
5 * Copyright (c) 2013 "kento" Karim Rahimpur www.itthinx.com
6 *
7 * This code is released under the GNU General Public License.
8 * See COPYRIGHT.txt and LICENSE.txt.
9 *
10 * This code is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * This header and all notices must be kept intact.
16 *
17 * @author Karim Rahimpur
18 * @package groups
19 * @since groups 1.3.11
20 */
21
22 if ( !defined( 'ABSPATH' ) ) {
23 exit;
24 }
25
26 // phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
27
28 /**
29 * Show group info on user profile pages and let admins edit group membership.
30 */
31 class Groups_Admin_User_Profile {
32
33 /**
34 * Adds user profile actions.
35 */
36 public static function init() {
37 add_action( 'user_new_form', array( __CLASS__, 'user_new_form' ) );
38 add_action( 'user_register', array( __CLASS__, 'user_register' ) );
39 add_action( 'show_user_profile', array( __CLASS__, 'show_user_profile' ) );
40 add_action( 'edit_user_profile', array( __CLASS__, 'edit_user_profile' ) );
41 add_action( 'personal_options_update', array( __CLASS__, 'personal_options_update' ) );
42 add_action( 'edit_user_profile_update', array( __CLASS__, 'edit_user_profile_update' ) );
43 add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ) );
44 }
45
46 /**
47 * Enqueues the select script on the user-edit and profile screens.
48 */
49 public static function admin_enqueue_scripts() {
50 $screen = get_current_screen();
51 if ( isset( $screen->id ) ) {
52 switch( $screen->id ) {
53 case 'user' : // creating a new user
54 case 'user-edit' :
55 case 'profile' :
56 require_once GROUPS_VIEWS_LIB . '/class-groups-uie.php';
57 Groups_UIE::enqueue( 'select' );
58 break;
59 }
60 }
61 }
62
63 /**
64 * Hook for the form to create a new user.
65 *
66 * See wp-admin/user-new.php
67 *
68 * @param string $type form context, expecting 'add-existing-user' (Multisite), or 'add-new-user' (single site and network admin)
69 */
70 public static function user_new_form( $type = null ) {
71 global $wpdb;
72 if ( $type == 'add-new-user' ) {
73 if ( Groups_User::current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
74 $output = '<h3>' . _x( 'Groups', 'Groups section heading (add user)', 'groups' ) . '</h3>';
75 $groups_table = _groups_get_tablename( 'group' );
76 /**
77 * Allow to filter the groups.
78 *
79 * @since 2.20.0
80 *
81 * @param array $groups
82 * @param string $type form context
83 *
84 * @return array
85 */
86 $groups = apply_filters(
87 'groups_admin_user_profile_user_new_form_groups',
88 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
89 $wpdb->get_results( "SELECT * FROM $groups_table ORDER BY name" ),
90 $type
91 );
92 if ( $groups ) {
93 $output .= '<style type="text/css">';
94 $output .= '.groups .selectize-input { font-size: inherit; }';
95 $output .= '</style>';
96 $output .= sprintf(
97 '<select id="user-groups" class="groups" name="group_ids[]" multiple="multiple" placeholder="%s" data-placeholder="%s">',
98 esc_attr__( 'Choose groups &hellip;', 'groups' ),
99 esc_attr__( 'Choose groups &hellip;', 'groups' )
100 );
101 foreach( $groups as $group ) {
102 $output .= sprintf(
103 '<option value="%d">%s</option>',
104 Groups_Utility::id( $group->group_id ),
105 $group->name ? stripslashes( wp_filter_nohtml_kses( $group->name ) ) : ''
106 );
107 }
108 $output .= '</select>';
109 $output .= Groups_UIE::render_select( '#user-groups' );
110 $output .= '<p class="description">' . esc_html__( 'The user is a member of the chosen groups.', 'groups' ) . '</p>';
111 }
112 echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
113 }
114 }
115 }
116
117 /**
118 * Adds the new user to chosen groups when creating a new user account
119 * from the admin side.
120 *
121 * @param int $user_id
122 */
123 public static function user_register( $user_id ) {
124
125 global $wpdb;
126
127 if ( is_admin() ) {
128 if ( function_exists( 'get_current_screen' ) ) {
129 $screen = get_current_screen();
130 if ( isset( $screen->id ) && $screen->id === 'user' ) {
131 if ( Groups_User::current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
132 $groups_table = _groups_get_tablename( 'group' );
133 /**
134 * Allow to filter the groups offered.
135 *
136 * @since 2.20.0
137 *
138 * @param array $groups
139 * @param int $user_id
140 *
141 * @return array
142 */
143 $groups = apply_filters(
144 'groups_admin_user_profile_user_register_groups',
145 $wpdb->get_results( "SELECT * FROM $groups_table" ), // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
146 $user_id
147 );
148 if ( $groups ) {
149 $user_group_ids = isset( $_POST['group_ids'] ) && is_array( $_POST['group_ids'] ) ? $_POST['group_ids'] : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
150 foreach( $groups as $group ) {
151 if ( in_array( $group->group_id, $user_group_ids ) ) {
152 // Do NOT use Groups_User::user_is_member( ... ) here, as this must not be filtered:
153 if ( !Groups_User_Group::read( $user_id, $group->group_id ) ) {
154 Groups_User_Group::create( array( 'user_id' => $user_id, 'group_id' => $group->group_id ) );
155 }
156 }
157 }
158 }
159 }
160 }
161 }
162 }
163 }
164
165 /**
166 * Own profile.
167 *
168 * @param WP_User $user
169 */
170 public static function show_user_profile( $user ) {
171 if ( Groups_User::current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
172 self::edit_user_profile( $user );
173 } else {
174 $output = '<h3>' . _x( 'Groups', 'Groups section heading (user profile)', 'groups' ) . '</h3>';
175 $user = new Groups_User( $user->ID );
176 $groups = $user->get_groups();
177 if ( is_array( $groups ) ) {
178 if ( count( $groups ) > 0 ) {
179 usort( $groups, array( __CLASS__, 'by_group_name' ) );
180 $output .= '<ul>';
181 foreach( $groups as $group ) {
182 $output .= '<li>';
183 $output .= $group->get_name() ? stripslashes( wp_filter_nohtml_kses( $group->get_name() ) ) : '';
184 $output .= '</li>';
185 }
186 $output .= '</ul>';
187 }
188 }
189 echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
190 }
191 }
192
193 /**
194 * Editing a user profile.
195 *
196 * @param WP_User $user
197 */
198 public static function edit_user_profile( $user ) {
199 global $wpdb;
200 if ( Groups_User::current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
201 $output = '<h3>' . _x( 'Groups', 'Groups section heading (edit user)', 'groups' ) . '</h3>';
202 $user = new Groups_User( $user->ID );
203 $groups_table = _groups_get_tablename( 'group' );
204 /**
205 * Allow to filter the groups offered.
206 *
207 * @since 2.20.0
208 *
209 * @param array $groups
210 * @param int $user_id
211 *
212 * @return array
213 */
214 $groups = apply_filters(
215 'groups_admin_user_profile_edit_user_profile_groups',
216 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
217 $wpdb->get_results( "SELECT * FROM $groups_table ORDER BY name" ),
218 $user->get_user()->ID
219 );
220 if ( $groups ) {
221 $output .= '<style type="text/css">';
222 $output .= '.groups .selectize-input { font-size: inherit; }';
223 $output .= '</style>';
224 $output .= sprintf(
225 '<select id="user-groups" class="groups" name="group_ids[]" multiple="multiple" placeholder="%s" data-placeholder="%s">',
226 esc_attr__( 'Choose groups &hellip;', 'groups' ),
227 esc_attr__( 'Choose groups &hellip;', 'groups' )
228 );
229 foreach( $groups as $group ) {
230 // Do NOT use Groups_User::user_is_member( ... ) here, as this must not be filtered:
231 $is_member = Groups_User_Group::read( $user->get_user_id(), $group->group_id ) ? true : false;
232 $output .= sprintf(
233 '<option value="%d" %s>%s</option>',
234 Groups_Utility::id( $group->group_id ),
235 $is_member ? ' selected="selected" ' : '',
236 $group->name ? stripslashes( wp_filter_nohtml_kses( $group->name ) ) : ''
237 );
238 }
239 $output .= '</select>';
240 $output .= Groups_UIE::render_select( '#user-groups' );
241 $output .= '<p class="description">' . esc_html__( 'The user is a member of the chosen groups.', 'groups' ) . '</p>';
242 }
243 echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
244 }
245 }
246
247 /**
248 * Updates the group membership when a user's own profile is saved - but
249 * for group admins on their own profile page only.
250 *
251 * @param int $user_id
252 *
253 * @see Groups_Admin_User_Profile::edit_user_profile_update()
254 */
255 public static function personal_options_update( $user_id ) {
256 // We're using the same method as for editing another user's profile,
257 // but let's check for group admin here as well.
258 if ( Groups_User::current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
259 self::edit_user_profile_update( $user_id );
260 }
261 }
262
263 /**
264 * Updates the group membership.
265 *
266 * @param int $user_id
267 */
268 public static function edit_user_profile_update( $user_id ) {
269 global $wpdb;
270 if ( Groups_User::current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
271 $groups_table = _groups_get_tablename( 'group' );
272 $groups = apply_filters(
273 'groups_admin_user_profile_edit_user_profile_update_groups',
274 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
275 $wpdb->get_results( "SELECT * FROM $groups_table" ),
276 $user_id
277 );
278 if ( $groups ) {
279 $user_group_ids = isset( $_POST['group_ids'] ) && is_array( $_POST['group_ids'] ) ? $_POST['group_ids'] : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
280 foreach( $groups as $group ) {
281 if ( in_array( $group->group_id, $user_group_ids ) ) {
282 // Do NOT use Groups_User::user_is_member( ... ) here, as this must not be filtered:
283 if ( !Groups_User_Group::read( $user_id, $group->group_id ) ) {
284 Groups_User_Group::create( array( 'user_id' => $user_id, 'group_id' => $group->group_id ) );
285 }
286 } else {
287 // Do NOT use Groups_User::user_is_member( ... ) here, as this must not be filtered:
288 if ( Groups_User_Group::read( $user_id, $group->group_id ) ) {
289 Groups_User_Group::delete( $user_id, $group->group_id );
290 }
291 }
292 }
293 }
294 }
295 }
296
297 /**
298 * usort helper
299 *
300 * @param Groups_Group $o1
301 * @param Groups_Group $o2
302 *
303 * @return int strcmp result for group names
304 */
305 public static function by_group_name( $o1, $o2 ) {
306 return strcmp( $o1->get_name(), $o2->get_name() );
307 }
308
309 }
310 Groups_Admin_User_Profile::init();
311