id ) ) { switch( $screen->id ) { case 'user-edit' : case 'profile' : require_once GROUPS_VIEWS_LIB . '/class-groups-uie.php'; Groups_UIE::enqueue( 'select' ); break; } } } /** * Own profile. * @param WP_User $user */ public static function show_user_profile( $user ) { if ( current_user_can( GROUPS_ADMINISTER_GROUPS ) ) { self::edit_user_profile( $user ); } else { $output = '

' . __( 'Groups', GROUPS_PLUGIN_DOMAIN ) . '

'; $user = new Groups_User( $user->ID ); $groups = $user->groups; if ( is_array( $groups ) ) { if ( count( $groups ) > 0 ) { usort( $groups, array( __CLASS__, 'by_group_name' ) ); $output .= ''; } } echo $output; } } /** * Editing a user profile. * @param WP_User $user */ public static function edit_user_profile( $user ) { global $wpdb; if ( current_user_can( GROUPS_ADMINISTER_GROUPS ) ) { $output = '

' . __( 'Groups', GROUPS_PLUGIN_DOMAIN ) . '

'; $user = new Groups_User( $user->ID ); $user_groups = $user->groups; $groups_table = _groups_get_tablename( 'group' ); if ( $groups = $wpdb->get_results( "SELECT * FROM $groups_table ORDER BY name" ) ) { $output .= ''; $output .= sprintf( ''; $output .= Groups_UIE::render_select( '#user-groups' ); $output .= '

' . __( 'The user is a member of the chosen groups.', GROUPS_PLUGIN_DOMAIN ) . '

'; } echo $output; } } /** * Updates the group membership when a user's own profile is saved - but * for group admins on their own profile page only. * * @param int $user_id * @see Groups_Admin_User_Profile::edit_user_profile_update() */ public static function personal_options_update( $user_id ) { // We're using the same method as for editing another user's profile, // but let's check for group admin here as well. if ( current_user_can( GROUPS_ADMINISTER_GROUPS ) ) { self::edit_user_profile_update( $user_id ); } } /** * Updates the group membership. * @param int $user_id */ public static function edit_user_profile_update( $user_id ) { global $wpdb; if ( current_user_can( GROUPS_ADMINISTER_GROUPS ) ) { $groups_table = _groups_get_tablename( 'group' ); if ( $groups = $wpdb->get_results( "SELECT * FROM $groups_table" ) ) { $user_group_ids = isset( $_POST['group_ids'] ) && is_array( $_POST['group_ids'] ) ? $_POST['group_ids'] : array(); foreach( $groups as $group ) { if ( in_array( $group->group_id, $user_group_ids ) ) { if ( !Groups_User_Group::read( $user_id, $group->group_id ) ) { Groups_User_Group::create( array( 'user_id' => $user_id, 'group_id' => $group->group_id ) ); } } else { if ( Groups_User_Group::read( $user_id, $group->group_id ) ) { Groups_User_Group::delete( $user_id, $group->group_id ); } } } } } } /** * usort helper * @param Groups_Group $o1 * @param Groups_Group $o2 * @return int strcmp result for group names */ public static function by_group_name( $o1, $o2 ) { return strcmp( $o1->name, $o2->name ); } } Groups_Admin_User_Profile::init();