vapvarun icon

Only list active MemberPress members in the members directory

vapvarun | PRO | 07/25/21 08:43:18 AM UTC | 0 ⭐ | 2193 👁️ | Never ⏰ | []
PHP |

2.04 KB

|

None

|

0 👍

/

0 👎

/**
 * Only list active MemberPress members in the members directory.
 * sources:
 * https://buddydev.com/hiding-users-on-buddypress-based-site
 * https://buddypress.org/support/topic/member-loop-only-memberpress-members/
 * @param array $args args.
 *
 * @return array
 */
function tmp_only_show_members_in_directory( $args ) {
    // do not exclude in admin.
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return $args;
    }
    $args['exclude'] = tmp_get_users_to_exclude_from_directory();
 
    return $args;
}
add_filter( 'bp_after_has_members_parse_args', 'tmp_only_show_members_in_directory' );
 
/**
 * Returns an array of IDs to exclude, runs the queries if empty
 * @return false|mixed|void
 */
function tmp_get_users_to_exclude_from_directory() {
    $ids_to_exclude = get_option('tmp_update_members_to_exclude_from_directory');
    if (empty($ids_to_exclude) || !is_array($ids_to_exclude) || count($ids_to_exclude) < 1) {
        tmp_update_members_to_exclude_from_directory();
    }
    return get_option('tmp_update_members_to_exclude_from_directory');
}
 
// Scheduled Action Hook
function tmp_update_members_to_exclude_from_directory() {
    global $wpdb;
    $member_ids = $wpdb->get_col("SELECT DISTINCT user_id FROM ".$wpdb->prefix."mepr_transactions WHERE status IN('confirmed','complete') AND (expires_at >= NOW() OR expires_at = '0000-00-00 00:00:00')");
    $user_ids = get_users(['fields'=>'ID']);
    $user_ids_to_exclude = array_diff($user_ids, $member_ids);
    return update_option('tmp_update_members_to_exclude_from_directory',$user_ids_to_exclude);
}
add_action( 'hook_name', 'tmp_update_members_to_exclude_from_directory' );
 
// Schedule Cron Job Event
function tmp_update_members_to_exclude_from_directory_job() {
    if ( ! wp_next_scheduled( 'tmp_update_members_to_exclude_from_directory' ) ) {
        wp_schedule_event( current_time( 'timestamp' ), 'twicedaily', 'tmp_update_members_to_exclude_from_directory' );
    }
}
add_action( 'wp', 'tmp_update_members_to_exclude_from_directory_job' );

Comments