Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbrajesh/9fd25a8071cd2a90fc0b3a62ed89d8dc to your computer and use it in GitHub Desktop.
Save sbrajesh/9fd25a8071cd2a90fc0b3a62ed89d8dc to your computer and use it in GitHub Desktop.
add_filter( 'bp_core_get_active_member_count', function ( $count ) {
// protect from errro if plugin is disabled.
if ( ! class_exists( 'BPMTP_Member_Types_Pro' ) ) {
return $count;
}
// optimize.
$current_count = get_transient( 'bpmtp_all_active_members' );
if ( false !== $current_count ) {
return $current_count;
}
// recount.
$excluded_member_types = bpmtp_get_dir_excluded_types();
if ( empty( $excluded_member_types ) ) {
return $count;
}
$bp = buddypress();
global $wpdb;
bpmtp_custom_maybe_define_mtp_count_class();
// if member type pro is not enabled, return.
$query = new BPMTP_Custom_Member_Type_Count_Query();
$member_type_clause = $query->get_sql_clause_for_member_types( $excluded_member_types, 'NOT IN' );
if ( empty( $member_type_clause ) ) {
return $count;
}
// Avoid a costly join by splitting the lookup.
if ( is_multisite() ) {
$sql = "SELECT ID FROM {$wpdb->users} WHERE (user_status != 0 OR deleted != 0 OR user_status != 0)";
} else {
$sql = "SELECT ID FROM {$wpdb->users} WHERE user_status != 0";
}
$exclude_users = $wpdb->get_col( $sql );
$exclude_users_sql = ! empty( $exclude_users ) ? "AND user_id NOT IN (" . implode( ',', wp_parse_id_list( $exclude_users ) ) . ")" : '';
$sql_count = $wpdb->prepare( "SELECT COUNT(user_id) FROM {$bp->members->table_name_last_activity} u WHERE component = %s AND type = 'last_activity' {$exclude_users_sql}", $bp->members->id );
$sql_count .= ' AND ' . $member_type_clause;
$count = (int) $wpdb->get_var( $sql_count );
// save.
set_transient( 'bpmtp_all_active_members', $count );
return $count;
} );
// Whenever member count is cleared, clear our cache too.
add_action( 'delete_transient_bp_active_member_count', function ( $transient ) {
delete_transient( 'bpmtp_all_active_members' );
} );
/**
* This is bad code.
*
* We should have rather done it in a bettter way(statically declare class).
* Since we are loading this code from bp-custom .php, 'BP_User_Query' is not available to extend early. That's why this.
*/
function bpmtp_custom_maybe_define_mtp_count_class() {
if ( class_exists( 'BPMTP_Custom_Member_Type_Count_Query' ) ) {
return;
}
/**
* Implement custom query to make the clause class function public.
*/
class BPMTP_Custom_Member_Type_Count_Query extends BP_User_Query {
/**
* Get a SQL clause representing member_type include/exclusion.
*
* @param string|array $member_types Array or comma-separated list of member types.
* @param string $operator 'IN' or 'NOT IN'.
*
* @return string
*/
public function get_sql_clause_for_member_types( $member_types, $operator ) {
$this->uid_name = 'user_id';
$this->uid_table = buddypress()->members->table_name_last_activity;
return parent::get_sql_clause_for_member_types( $member_types, $operator );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment