Skip to:
Content
Pages
Categories
Search
Top
Bottom

No longer able to auto add member to group since update


  • maustingraphics82
    Participant

    @maustingraphics82

    I use gravity forms to create a new user from a groups tab and assign them to a group, based on the group that the group admin is adding them from. This was working just fine in BuddyPress version 8.0 but since upgrading to 10 it is no longer working. What am I missing?

    /**
     *  Add member form handler. This form is currently located under the Group Information tab
     */
    add_action( 'gform_after_submission', 'add_member_gform_handle', 10, 2 );
    function add_member_gform_handle( $entry, $form ) {
    	if ( $form['title'] === 'Add Member' ) {
    		$fields     = $form['fields'];
    		$first_name = rgar( $entry, '1' );
    		$last_name  = rgar( $entry, '2' );
    		$login      = rgar( $entry, '3' );
    		$password   = rgar( $entry, '4' );
    		$email 	    = rgar( $entry, '5' );
    		$group_id   = rgar( $entry, '6' );
    		$role       = rgar( $entry, '11' );
    		$notification = rgar( $entry, '9.1' );
    		$birthdate = rgar( $entry, '10' );
    
    		$userdata = array(
    			'user_login' => $login,
    			'user_pass' => $password,
    		);
    
    		if ( ! empty( $first_name ) ) {
    			$userdata['first_name'] = $first_name;
    		}
    		if ( ! empty( $last_name ) ) {
    			$userdata['last_name'] = $last_name;
    		}
    		if ( ! empty( $email ) ) {
    			$userdata['user_email'] = $email;
    		}
    		if ( ! empty( $role ) ) {
    			$userdata['role'] = $role;
    		}
    
    		// Create a new user
    		$new_user_id = wp_insert_user( $userdata );
    
    		// GFCommon::log_debug( __METHOD__ . '(): form => ' . print_r( $birthdate, true ) );
    
    		if ( ! is_wp_error( $new_user_id ) ) {
    			groups_join_group( $group_id, $new_user_id );
    
    			// Make the user displayed as a staff on a clubhouse page
    			if ( $role === 'staff' ) {
    				$group_ids =  groups_get_user_groups( $new_user_id );
    				$group = groups_get_group( array( 'group_id' => $group_ids['groups'][0]) );
    				$group_id = $group->id;
    				groups_promote_member( $new_user_id, $group_id, 'admin' );
    			} else {
    				groups_demote_member( $new_user_id, $group_id );
    			}
    
    			if ( ! empty( $notification ) ) {
    				tml_send_new_user_notifications( $new_user_id, 'both' );
    			}
    
    			if ( ! empty( $birthdate ) ) {
    				$date = DateTime::createFromFormat('Y-m-d', $birthdate);
    				$date = $date->format('Ymd');
    
    				update_field('user_birthdate', $date, 'user_' . $new_user_id);
    			}
    		}
    
    		// Delete form entry
    		// GFAPI::delete_entry( $entry['id'] );
    	}
    }
    
    /**
     * Add Member form validation
     */
    add_filter( "gform_validation_$clubnet_add_member_form_id", 'add_member_form_validation' );
    function add_member_form_validation( $validation_result ) {
    	$form = $validation_result['form'];
    	$login = rgpost( 'input_3' );
    	$email = rgpost( 'input_5' );
    	$send_notificaton = rgpost( 'input_9_1' );
     
        if ( ! empty( $login ) && get_user_by( 'login', $login ) ) {
            $validation_result['is_valid'] = false;
     
            //finding Field with ID and marking it as failed validation
            foreach( $form['fields'] as &$field ) {
                if ( $field->id == '3' ) {
                    $field->failed_validation = true;
                    $field->validation_message = 'The user with this login already exists.';
                    break;
                }
            } // foreach
        }
    
    	if ( ! empty( $email ) && get_user_by( 'email', $email ) ) {
            $validation_result['is_valid'] = false;
     
            //finding Field with ID and marking it as failed validation
            foreach( $form['fields'] as &$field ) {
                if ( $field->id == '5' ) {
                    $field->failed_validation = true;
                    $field->validation_message = 'The user with this email already exists.';
                    break;
                }
            } // foreach
        }
    
    	// GFCommon::log_debug( 'VALIDATION' . __METHOD__ . '(): form => ' . print_r( 'Empty', true ) );
    	// Email is required if we requested to send a email about user's account
    	if ( ! empty( $send_notificaton ) && empty( $email ) ) {
    		$validation_result['is_valid'] = false;
            //finding Field with ID and marking it as failed validation
            foreach( $form['fields'] as &$field ) {
    			if ( $field->id == '5' ) {
                    $field->failed_validation = true;
                    $field->validation_message = 'Email is required since you requested to send an email.';
                    break;
                }
            } // foreach
    	}
     
        //Assign modified $form object back to the validation result
        $validation_result['form'] = $form;
        return $validation_result;
    }
    
    /**
     * Role field
     */
    add_action( 'init', 'clubnet_add_role_field' );
    function clubnet_add_role_field( $form ) {
    	global $wp_roles;
    	global $clubnet_add_member_form_id;
    
        $all_roles = $wp_roles->roles;
        // $editable_roles = apply_filters('editable_roles', $all_roles);
    	// $roles_to_skip = array( 'administrator' );
    	$editable_roles = $all_roles;
    	$roles_to_display = array( 'mentor', 'member', 'youth_leader', 'alumni', 'staff' );
    	$choices = array();
    
    	foreach ( $editable_roles as $role => $role_data ) {
    		if ( ! in_array( $role, $roles_to_display ) ) {
    			continue;
    		}
    
    		$choice = array(
    			'text' => $role_data['name'],
    			'value' => $role
    		);
    
    		array_push( $choices, $choice );
    	}
    
    	// Reorder to make member role first
    	usort( $choices, function( $choice ) {
    		if ( $choice['text'] === 'Member' ) {
    			return -1;
    		}
    
    		return 1;
    	} );
    
    	$form = GFAPI::get_form( $clubnet_add_member_form_id );
    	
    	if ( ! $form ) {
    		return;
    	}
    
    	$label = 'User Role';
    	$field_idx = -1;
    	$field_id = -1;
    
    	foreach ($form['fields'] as $idx => $field) {
    		if ( $field->label === $label ) {
    			$field_idx = $idx;
    			$field_id  = $field->id;
    		}
    	}
    
    	if ( $field_idx === -1 ) {
    		$field_id = GFFormsModel::get_next_field_id( $form['fields'] );
    	}
    
    	$props = array( 
    		'id' => $field_id,
    		'label' => $label,
    		'type' => 'select',
    		'choices' => $choices,
    	); 
    
    	if ( $field_idx === -1 ) {
    		$field = GF_Fields::create( $props );
    		$form['fields'][] = $field;
    		GFAPI::update_form( $form );
    	} else if ( $form['fields'][$field_idx]->choices !== $choices ) {
    		$field = GF_Fields::create( $props );
    		$form['fields'][$field_idx] = $field;
    		GFAPI::update_form( $form );
    	}
    }
    
Viewing 1 replies (of 1 total)

  • maustingraphics82
    Participant

    @maustingraphics82

    I think the issue is within this part of the code but posted everything in the message above.

    // Create a new user
    		$new_user_id = wp_insert_user( $userdata );
    
    		// GFCommon::log_debug( __METHOD__ . '(): form => ' . print_r( $birthdate, true ) );
    
    		if ( ! is_wp_error( $new_user_id ) ) {
    			groups_join_group( $group_id, $new_user_id );
    
    			// Make the user displayed as a staff on a clubhouse page
    			if ( $role === 'staff' ) {
    				$group_ids =  groups_get_user_groups( $new_user_id );
    				$group = groups_get_group( array( 'group_id' => $group_ids['groups'][0]) );
    				$group_id = $group->id;
    				groups_promote_member( $new_user_id, $group_id, 'admin' );
    			} else {
    				groups_demote_member( $new_user_id, $group_id );
    			}
    
    			if ( ! empty( $notification ) ) {
    				tml_send_new_user_notifications( $new_user_id, 'both' );
    			}
    
    			if ( ! empty( $birthdate ) ) {
    				$date = DateTime::createFromFormat('Y-m-d', $birthdate);
    				$date = $date->format('Ymd');
    
    				update_field('user_birthdate', $date, 'user_' . $new_user_id);
    			}
    		}
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.
Skip to toolbar