| 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>' . esc_html_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 …', 'groups' ), |
| 99 |
esc_attr__( 'Choose groups …', '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 = groups_sanitize_post( 'group_ids' ); |
| 150 |
if ( !is_array( $user_group_ids ) ) { |
| 151 |
$user_group_ids = array(); |
| 152 |
} |
| 153 |
foreach ( $groups as $group ) { |
| 154 |
if ( in_array( $group->group_id, $user_group_ids ) ) { |
| 155 |
// Do NOT use Groups_User::user_is_member( ... ) here, as this must not be filtered: |
| 156 |
if ( !Groups_User_Group::read( $user_id, $group->group_id ) ) { |
| 157 |
Groups_User_Group::create( array( 'user_id' => $user_id, 'group_id' => $group->group_id ) ); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Own profile. |
| 170 |
* |
| 171 |
* @param WP_User $user |
| 172 |
*/ |
| 173 |
public static function show_user_profile( $user ) { |
| 174 |
if ( Groups_User::current_user_can( GROUPS_ADMINISTER_GROUPS ) ) { |
| 175 |
self::edit_user_profile( $user ); |
| 176 |
} else { |
| 177 |
$output = '<h3>' . esc_html_x( 'Groups', 'Groups section heading (user profile)', 'groups' ) . '</h3>'; |
| 178 |
$user = new Groups_User( $user->ID ); |
| 179 |
$groups = $user->get_groups(); |
| 180 |
if ( is_array( $groups ) ) { |
| 181 |
if ( count( $groups ) > 0 ) { |
| 182 |
usort( $groups, array( __CLASS__, 'by_group_name' ) ); |
| 183 |
$output .= '<ul>'; |
| 184 |
foreach ( $groups as $group ) { |
| 185 |
$output .= '<li>'; |
| 186 |
$output .= $group->get_name() ? stripslashes( wp_filter_nohtml_kses( $group->get_name() ) ) : ''; |
| 187 |
$output .= '</li>'; |
| 188 |
} |
| 189 |
$output .= '</ul>'; |
| 190 |
} |
| 191 |
} |
| 192 |
echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Editing a user profile. |
| 198 |
* |
| 199 |
* @param WP_User $user |
| 200 |
*/ |
| 201 |
public static function edit_user_profile( $user ) { |
| 202 |
global $wpdb; |
| 203 |
if ( Groups_User::current_user_can( GROUPS_ADMINISTER_GROUPS ) ) { |
| 204 |
$output = '<h3>' . esc_html_x( 'Groups', 'Groups section heading (edit user)', 'groups' ) . '</h3>'; |
| 205 |
$user = new Groups_User( $user->ID ); |
| 206 |
$groups_table = _groups_get_tablename( 'group' ); |
| 207 |
/** |
| 208 |
* Allow to filter the groups offered. |
| 209 |
* |
| 210 |
* @since 2.20.0 |
| 211 |
* |
| 212 |
* @param array $groups |
| 213 |
* @param int $user_id |
| 214 |
* |
| 215 |
* @return array |
| 216 |
*/ |
| 217 |
$groups = apply_filters( |
| 218 |
'groups_admin_user_profile_edit_user_profile_groups', |
| 219 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 220 |
$wpdb->get_results( "SELECT * FROM $groups_table ORDER BY name" ), |
| 221 |
$user->get_user()->ID |
| 222 |
); |
| 223 |
if ( $groups ) { |
| 224 |
$output .= '<style type="text/css">'; |
| 225 |
$output .= '.groups .selectize-input { font-size: inherit; }'; |
| 226 |
$output .= '</style>'; |
| 227 |
$output .= sprintf( |
| 228 |
'<select id="user-groups" class="groups" name="group_ids[]" multiple="multiple" placeholder="%s" data-placeholder="%s">', |
| 229 |
esc_attr__( 'Choose groups …', 'groups' ), |
| 230 |
esc_attr__( 'Choose groups …', 'groups' ) |
| 231 |
); |
| 232 |
foreach ( $groups as $group ) { |
| 233 |
// Do NOT use Groups_User::user_is_member( ... ) here, as this must not be filtered: |
| 234 |
$is_member = Groups_User_Group::read( $user->get_user_id(), $group->group_id ) ? true : false; |
| 235 |
$output .= sprintf( |
| 236 |
'<option value="%d" %s>%s</option>', |
| 237 |
Groups_Utility::id( $group->group_id ), |
| 238 |
$is_member ? ' selected="selected" ' : '', |
| 239 |
$group->name ? stripslashes( wp_filter_nohtml_kses( $group->name ) ) : '' |
| 240 |
); |
| 241 |
} |
| 242 |
$output .= '</select>'; |
| 243 |
$output .= Groups_UIE::render_select( '#user-groups' ); |
| 244 |
$output .= '<p class="description">' . esc_html__( 'The user is a member of the chosen groups.', 'groups' ) . '</p>'; |
| 245 |
} |
| 246 |
echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Updates the group membership when a user's own profile is saved - but |
| 252 |
* for group admins on their own profile page only. |
| 253 |
* |
| 254 |
* @param int $user_id |
| 255 |
* |
| 256 |
* @see Groups_Admin_User_Profile::edit_user_profile_update() |
| 257 |
*/ |
| 258 |
public static function personal_options_update( $user_id ) { |
| 259 |
// We're using the same method as for editing another user's profile, |
| 260 |
// but let's check for group admin here as well. |
| 261 |
if ( Groups_User::current_user_can( GROUPS_ADMINISTER_GROUPS ) ) { |
| 262 |
self::edit_user_profile_update( $user_id ); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Updates the group membership. |
| 268 |
* |
| 269 |
* @param int $user_id |
| 270 |
*/ |
| 271 |
public static function edit_user_profile_update( $user_id ) { |
| 272 |
global $wpdb; |
| 273 |
if ( Groups_User::current_user_can( GROUPS_ADMINISTER_GROUPS ) ) { |
| 274 |
$groups_table = _groups_get_tablename( 'group' ); |
| 275 |
$groups = apply_filters( |
| 276 |
'groups_admin_user_profile_edit_user_profile_update_groups', |
| 277 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 278 |
$wpdb->get_results( "SELECT * FROM $groups_table" ), |
| 279 |
$user_id |
| 280 |
); |
| 281 |
if ( $groups ) { |
| 282 |
$user_group_ids = groups_sanitize_post( 'group_ids' ); |
| 283 |
if ( !is_array( $user_group_ids ) ) { |
| 284 |
$user_group_ids = array(); |
| 285 |
} |
| 286 |
foreach ( $groups as $group ) { |
| 287 |
if ( in_array( $group->group_id, $user_group_ids ) ) { |
| 288 |
// Do NOT use Groups_User::user_is_member( ... ) here, as this must not be filtered: |
| 289 |
if ( !Groups_User_Group::read( $user_id, $group->group_id ) ) { |
| 290 |
Groups_User_Group::create( array( 'user_id' => $user_id, 'group_id' => $group->group_id ) ); |
| 291 |
} |
| 292 |
} else { |
| 293 |
// Do NOT use Groups_User::user_is_member( ... ) here, as this must not be filtered: |
| 294 |
if ( Groups_User_Group::read( $user_id, $group->group_id ) ) { |
| 295 |
Groups_User_Group::delete( $user_id, $group->group_id ); |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* usort helper |
| 305 |
* |
| 306 |
* @param Groups_Group $o1 |
| 307 |
* @param Groups_Group $o2 |
| 308 |
* |
| 309 |
* @return int strcmp result for group names |
| 310 |
*/ |
| 311 |
public static function by_group_name( $o1, $o2 ) { |
| 312 |
return strcmp( $o1->get_name(), $o2->get_name() ); |
| 313 |
} |
| 314 |
|
| 315 |
} |
| 316 |
Groups_Admin_User_Profile::init(); |
| 317 |
|