BuddyDev

Search

[Resolved] Hide members profile page, and prevent them from appearing in search results

  • Participant
    Level: Initiated
    Posts: 4
    JG on #23600

    Hi there! I have a site that uses MemberPress to manage subscriptions and have a BuddyPress integration to allow subscribers to create a social profile.

    When a users subscription expires, I want to exclude them from the Member Directory Search Results, as well as prevent their profile from being viewed until they renew their subscription.

    There are many MemberPress hooks that I can tap into, and what I’m trying to find is if there’s a simple “Disabled” field on a BuddyPress user that would hide them from all areas of the system. So I could do something like:

    
    function mepr_sync_buddy_press_visibility($txn, $status = false) {
      global $buddyPress;
      if(class_exists('MeprUser')) {
    
        $user = new MeprUser($txn->user_id);
        $subs = $user->active_product_subscriptions();
    
        if(!empty($subs)) {
          $buddyPress->unblock_account($user->ID);
        }
        else {
          $buddyPress->block_account($user->ID);
        }
      }
    }
    add_action('mepr-txn-store', 'mepr_sync_buddy_press_visibility');
    add_action('mepr-txn-expired', 'mepr_sync_buddy_press_visibility', 11, 2);
    

    Any help would be much appreciated. Thanks for your time!

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #23601

    Hi JG,
    Welcome to BuddyDev.

    There are no direct way to do it. There are many ways to do it though.
    1. You can add a meta to user when their membership expires and then filter on bp_after_has_members_parse_args and tap into the’exclude’ parameter to pass user ids with this meta key you saved.

    2. Or you can use on of our plugins like BuddyPress Deactivate Account or BuddyPress Profile visibility Manager and hook them to your expiry action. They can manage the exclusion for you.

    3. Or you can keep the strategy one and instead of filtering on args, you can filter ton query to make it a bit more efficient.

    Hope that helps.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 4
    JG on #23604

    Hi Brajesh! Thank you so much for the quick response; much appreciated! If I were to use the BuddyPress Deactivate Account plugin, is there a way to prevent users from re-activating their own account if it’s been deactivated by an admin?

    Thanks again!

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #23605

    Hi JG,
    Thank you.

    The plugin allows you to make the access restricted to admins. In other words, if you disable the status changing by user, only admin can change it for the user.

    Hope that helps

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 4
    JG on #23612

    Hi Brajesh! Yes that does help, thank you! I purchased the Deactivate Account Plugin today and am trying to use it to auto activate/deactivate accounts whenever MemberPress subscriptions change. I’m not sure if you know the answer to this (I’m reaching out to memberpress as well), but users that should be getting deactivated aren’t. If you have any experience using this plugin with MemberPress I would be most grateful for any suggestions. Thanks again!

    
    function listen_to_mepr_events($event) {
      $obj = $event->get_data();
      //$obj might be a MeprTransaction object or a MeprSubscription object
      if(!($obj instanceof MeprTransaction) && !($obj instanceof MeprSubscription)) {
        return; // nothing here to do if we're not dealing with a txn or sub
      }
      
      $member = $obj->user();
      
      //Make sure it's a valid user still
      if(!isset($member->ID) || !$member->ID) { return; }
        
      $subs = $member->active_product_subscriptions();
        
      if(!empty($subs)) {	
        //member is active on membership
    	 bp_account_deactivator()->set_active($member->ID);
      }
      else {
        //member is no longer active on this membership
        bp_account_deactivator()->set_inactive($member->ID);
      }
    }
    add_action('mepr-event-create', 'listen_to_mepr_events');
    
  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #23613

    Hi JG,
    Thank you.

    Most probably the action will be different. One of my colleague used memberpress recently. I have asked him to assist you with code today.

    Thank you
    Brajesh

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2935
    Ravi on #23614

    Hello JG,

    Try the following code. If will set user account status active on transaction completed and set inactive of refund or expired.

    
    function buddydev_activtate_user( $txn ) {
    
    	if ( function_exists( 'bp_account_deactivator' ) && bp_account_deactivator()->is_inactive( $txn->user_id ) ) {
    		bp_account_deactivator()->set_active( $txn->user_id );
    	}
    }
    add_action( 'mepr-txn-status-complete', 'buddydev_activtate_user' );
    
    function buddydev_deactivtate_user( $txn ) {
    
    	if ( function_exists( 'bp_account_deactivator' ) && bp_account_deactivator()->is_active( $txn->user_id ) ) {
    		bp_account_deactivator()->set_inactive( $txn->user_id );
    	}
    }
    
    add_action( 'mepr-txn-status-refunded', 'buddydev_deactivtate_user' );
    add_action( 'mepr-transaction-expired', 'buddydev_deactivtate_user' );
    
    

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 4
    JG on #23619

    Thanks so much Brajesh and Ravi! So Ravi, I think I see what you’re saying, but I have a few extra questions:

    Is it possible that a transaction could expire, but the user still has an active subscription? For instance, on a recurring subscription, i believe when one transaction expires, another is created. So there could be a potential race condition with the code you have above, if somehow the new transaction gets created before the old one is expired. So my thought is, we want to check if the user has any active subscriptions before deactivating them.

    As well, if I have my code in the functions.php file in my child theme, is it safe to assume that the “bp_account_deactivator()” will be available? Or rather, what is the scenario where that wouldn’t be instantiated already?

    Thanks again for your time!

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #23627

    Hi JG,
    Thank you.
    Ravi will assist you early tomorrow.

    I can answer the other question though. There are 2 aspects.

    1. Will bp_account_deactivator() be available in functions.php? Yes, It is. It is available after plugins_loaded hook, so no worries.

    2. What happens if the plugin gets deactivated, will it cause fatal error? No, we are using function_exists( ‘bp_account_deactivator’ ). That makes sure the function is available before it is called.

    Regards
    Brajesh

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2935
    Ravi on #23642

    Hello JG,

    I have check again the MemberPress code and found there is fallback membership for product group. So you are right User can still have active membership if transaction expired or refunded. Please use the following code for save bet. Now I am checking is there any active subscription for the user and then deactivating if not found any

    
    function buddydev_activtate_user( $txn ) {
    
    	if ( function_exists( 'bp_account_deactivator' ) && bp_account_deactivator()->is_inactive( $txn->user_id ) ) {
    		bp_account_deactivator()->set_active( $txn->user_id );
    	}
    }
    add_action( 'mepr-txn-status-complete', 'buddydev_activtate_user' );
    
    function buddydev_deactivtate_user( $txn ) {
    
    	if ( function_exists( 'bp_account_deactivator' ) && bp_account_deactivator()->is_active( $txn->user_id ) ) {
    		$active_subscription_count = MeprSubscription::get_all_active_by_user_id( $txn->user_id, '', '', true );
    
    		if ( empty( $active_subscription_count ) ) {
    			bp_account_deactivator()->set_inactive( $txn->user_id );
    		}
    	}
    }
    
    add_action( 'mepr-txn-status-refunded', 'buddydev_deactivtate_user', 20 );
    add_action( 'mepr-transaction-expired', 'buddydev_deactivtate_user', 20 );
    

    Please let me know if it works or not.

    Regards
    Ravi

You must be logged in to reply to this topic.

This topic is: resolved