id ) ) { switch( $screen->id ) { case 'user' : // creating a new user case 'user-edit' : case 'profile' : require_once GROUPS_VIEWS_LIB . '/class-groups-uie.php'; Groups_UIE::enqueue( 'select' ); break; } } } /** * Hook for the form to create a new user. * * See wp-admin/user-new.php * * @param string $type form context, expecting 'add-existing-user' (Multisite), or 'add-new-user' (single site and network admin) */ public static function user_new_form( $type = null ) { global $wpdb; if ( $type == 'add-new-user' ) { if ( current_user_can( GROUPS_ADMINISTER_GROUPS ) ) { $output = '
' . __( 'The user is a member of the chosen groups.', 'groups' ) . '
'; } echo $output; } } } /** * Adds the new user to chosen groups when creating a new user account * from the admin side. * * @param int $user_id */ public static function user_register( $user_id ) { global $wpdb; if ( is_admin() ) { if ( function_exists( 'get_current_screen' ) ) { $screen = get_current_screen(); if ( isset( $screen->id ) && $screen->id === 'user' ) { 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 ) ); } } } } } } } } } /** * 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 = '' . __( 'The user is a member of the chosen groups.', 'groups' ) . '
'; } 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();