BuddyDev

Search

[Resolved] Auto Friendship Pro (will one of these rules work – and implement backwards)?

  • Participant
    Level: Initiated
    Posts: 12
    Choyt on #31505

    I purchased a full membership today and I’m migrating a full Buddypress site to Buddyboss – inclusive of forums and over 3,000 active community members. Hoping to get some clarity on a few things…

    I’d like to have Auto Friendship Pro set up so that anyone who has an email domain of @companyX.com is automatically friends.
    OR
    I’d like to have a rule for AFP that says anyone with the same Profile Type (memberpress pro) is automatically friends. (versus using Role)
    AND
    I’d like to have whichever of these rules that can be implemented to work backward – meaning that existing members that meet these criteria will be friended rather than waiting on new registrations.

    Is this possible? Or am I looking for a completely different plugin?

  • Keymaster
    (BuddyDev Team)
    Posts: 21785
    Brajesh Singh on #31506

    Hi Chris,
    Thank you for joining our membership.

    The BuddyPress Auto Friendship Pro addd the new User to a list of predefined users. In other words, the list of users who will be added is preset.

    What you are looking for is finding a match between the users and add them as friend. The plugin does not do that.

    Yes, It is doable with some code(will that work for you?). If I put the custom code, It will only apply to new users.

    If you need it for existing users, it will need searching all users and managing a queue for adding friendship relationship(which will be ajax based to avoid timeouts). This will be beyond the scope and will need custom service.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 12
    Choyt on #31512

    It would work if for new users a rule can be added that allows for email domain. Can that be added?

  • Keymaster
    (BuddyDev Team)
    Posts: 21785
    Brajesh Singh on #31513

    Hi Chris,
    Yes. It is your choice. Whichever you prefer(email domain based or member type based), Please let me know and I will post the code.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 12
    Choyt on #31539

    I would like both, ideally. They would both be very beneficial options for our community.
    Especially if it will sync existing users?

  • Participant
    Level: Initiated
    Posts: 12
    Choyt on #31590

    Just checking in here – you’ve been great on the other post, Brajesh. Just didn’t want to get lost in the shuffle of support stuff, lol.

  • Keymaster
    (BuddyDev Team)
    Posts: 21785
    Brajesh Singh on #31610

    Hi Chris,
    I will be sharing the code in next 3-4 working days. Need some time to put it.

    I won’t be able to share it for existing users. To be honest, For existing users or larger number of users, this needs to be done as plugin where we move the processing of search/adding in background queue and need to manage multiple queues(for existing users).

    Due to time constraint, I will share the code which won’t be using background processing. Should work fine for smaller number of users though.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 12
    Choyt on #31617

    Thanks.
    Would it work if I imported new users? I don’t mind emtying the db and then importing in small batches.

  • Keymaster
    (BuddyDev Team)
    Posts: 21785
    Brajesh Singh on #31643

    Hi Chris,
    Thank you for the patience.

    Here is the code to add all users from same domain as friend. It adds them as friend when a user account is activated.

    
    
    /**
     * Add users from same domain as friend on account activation.
     *
     * @param int $user_id user id.
     */
    function buddydev_add_friends_on_activation_by_email_domain( $user_id ) {
    
    	$user = get_user_by( 'id', $user_id );
    	if ( ! $user ) {
    		return;
    	}
    
    	$email_parts = explode( '@', $user->user_email );
    
    	if ( count( $email_parts ) !== 2 ) {
    		return;
    	}
    
    	$domain = $email_parts[1];
    
    	$potential_friend_ids = buddydev_get_users_with_email_domain( $domain );
    
    	if ( empty( $potential_friend_ids ) ) {
    		return;
    	}
    
    	foreach ( $potential_friend_ids as $potential_friend_id ) {
    
    		if ( $potential_friend_id == $user_id || friends_check_friendship( $user_id, $potential_friend_id ) ) {
    			continue;
    		}
    
    		friends_add_friend( $potential_friend_id, $user_id, true );
    	}
    
    }
    
    add_action( 'bp_core_activated_user', 'buddydev_add_friends_on_activation_by_email_domain' );
    
    /**
     * Get all users who have given email domain.
     *
     * @param string $domain domain name.
     *
     * @return array
     */
    function buddydev_get_users_with_email_domain( $domain ) {
    	global $wpdb;
    
    	$pattern       = '%@' . trim( $domain );
    	$sql_statement = $wpdb->prepare( "SELECT ID FROM {$wpdb->users} WHERE user_email LIKE %s", esc_sql( $pattern ) );
    
    	return $wpdb->get_col( $sql_statement );
    }
    
    

    I am not sharing the code for the member type since BuddyPress does not set member type on activation and BuddyBoss does set it but that is not straight forward(or guaranteed and depends on settings).

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 12
    Choyt on #31862

    Great, thanks.
    Sorry for delay – was off for a week.

    A few questions…
    Will this auto-friend for users that already are in the site?
    Is there a way that I can define the domains or at least set up domains to NOT be matched? For instance, I would not want everyone with a gmail.com address to be friended together – just people with work-related emails.

You must be logged in to reply to this topic.

This topic is: resolved