New
PeoplePress is here. Community building for WordPress, reimagined. Fast, modern, and built for the communities you've always wanted to run.
See it live

BuddyDev

Search

Buddypress Auto Deactivate Members Suggestion

  • Participant
    Level: Guru
    Posts: 922
    Tosin on #55875

    Hello Brajesh,

    Can you consider adding this feature to the Buddypress Deactivate Account plugin:

    Ability to automatically deactivate account of users who have been inactive for 2 years. This would really help in automating the clean up process of idle/inactive accounts.

    I have a rough code below but im worried about performance especially when dealing with thousands of users

     <?php
    /**
     * Plugin Name: BP Auto‑Deactivate Inactive Users (BuddyPress)
     * Description: Automatically deactivates BuddyPress accounts (via BP Deactivate Account plugin) when users have been inactive for 2 years — excluding admin, editor, and author roles.
     * Version: 0.2
     * Author: Your Name
     */
    
    // Exit if accessed directly.
    if ( ! defined( 'ABSPATH' ) ) {
        exit;
    }
    
    /**
     * Schedule the deactivation job on plugin activation.
     */
    function bpad_schedule_inactive_deactivation() {
        if ( ! function_exists( 'as_schedule_recurring_action' ) ) {
            return;
        }
    
        if ( ! as_next_scheduled_action( 'bpad_deactivate_inactive_users' ) ) {
            as_schedule_recurring_action(
                time(),
                WEEK_IN_SECONDS,
                'bpad_deactivate_inactive_users',
                array(),
                'bpad-inactive-users'
            );
        }
    }
    register_activation_hook( __FILE__, 'bpad_schedule_inactive_deactivation' );
    
    /**
     * Clear scheduled action on plugin deactivation.
     */
    function bpad_clear_deactivation_schedule() {
        if ( function_exists( 'as_unschedule_action' ) ) {
            as_unschedule_action( 'bpad_deactivate_inactive_users' );
        }
    }
    register_deactivation_hook( __FILE__, 'bpad_clear_deactivation_schedule' );
    
    /**
     * The callback that performs the deactivation.
     */
    function bpad_deactivate_inactive_users_callback() {
        if ( ! class_exists( 'BP_Account_Deactivator' ) ) {
            // BP Deactivate plugin not active.
            return;
        }
    
        $threshold_time = strtotime( '-2 years' );
    
        // Query users in batches, but exclude admin, editor, author roles.
        $args = array(
            'number'        => 200,     // batch size, adjust as needed
            'fields'        => 'ID',
            'role__not_in'  => array( 'administrator', 'editor', 'author' ),
        );
        $user_query = new WP_User_Query( $args );
        $user_ids = $user_query->get_results();
    
        if ( empty( $user_ids ) ) {
            return;
        }
    
        foreach ( $user_ids as $user_id ) {
            // Skip if user is already inactive
            if ( bp_account_deactivator()->is_inactive( $user_id ) ) {
                continue;
            }
    
            $last_activity = bp_get_user_last_activity( $user_id );
            if ( empty( $last_activity ) ) {
                $last_ts = 0;
            } else {
                $last_ts = strtotime( $last_activity );
            }
    
            if ( $last_ts < $threshold_time ) {
                bp_account_deactivator()->set_inactive( $user_id );
            }
        }
    
        // If we're hitting the batch limit, schedule next batch soon.
        if ( count( $user_ids ) >= 200 ) {
            as_schedule_single_action( time() + MINUTE_IN_SECONDS, 'bpad_deactivate_inactive_users' );
        }
    }
    add_action( 'bpad_deactivate_inactive_users', 'bpad_deactivate_inactive_users_callback' ); 
  • Keymaster
    (BuddyDev Team)
    Posts: 25509
    Brajesh Singh on #55878

    Hi Tosin,
    Thank you for the suggestion.

    I see that the above solution is based on Action Scheduler which will be external dependency.

    We alreday have a solution for similar purpose.

    https://github.com/buddydev/bp-deactivate-account-addon/blob/master/bp-deactivate-account-addon.php

    Please take a look.

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 922
    Tosin on #55879

    Hello Brajesh,

    Your addon feature is different, it reactivates accounts after a specified number of days

    But what I want is to automatically deactivate account of users who have been inactive for 2 years

    Thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 25509
    Brajesh Singh on #55885

    Hi Tosin,
    If you need we can change that addon, it will need 1-3 line change to achieve your goal. I don’t see any point in adding a dependency on action scheduler(unless we provide the compat with cron when action scheduler is not available).

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 922
    Tosin on #55934

    yes Brajesh I would really appreciate the change in code

    Thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 25509
    Brajesh Singh on #55938

    Hi Tosin,
    Thank you for the reply.

    I will share the updated code by Tuesday.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved