Skip to:
Content

BuddyPress.org

Changeset 5987


Ignore:
Timestamp:
04/13/2012 04:31:51 PM (8 years ago)
Author:
boonebgorges
Message:

'Total member count' cleanup:

  • Introduces bp_core_get_active_member_count() to get an accurate count of members with recorded last_activity
  • Points bp_get_total_member_count() to bp_core_get_active_member_count(), so that 'All Members' tab count on member directories matches the total count prepared by BP_Core_User::get_users(). Fixes #4061
  • Changes 'All Members' count in activity/index.php to use bp_get_total_member_count(), for greater accuracy and consistency between activity and members components
  • Introduces a cache-busting function to be fired when member counts change (new members, spam status changed, member deleted)
Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-cache.php

    r5986 r5987  
    4444    wp_cache_delete( 'bp_user_' . $user_id, 'bp' );
    4545}
     46
     47/**
     48 * Clears member count caches and transients
     49 */
     50function bp_core_clear_member_count_caches() {
     51    wp_cache_delete( 'bp_total_member_count', 'bp' );
     52    delete_transient( 'bp_active_member_count' );
     53}
     54add_action( 'bp_core_activated_user',         'bp_core_clear_member_count_caches' );
     55add_action( 'bp_core_process_spammer_status', 'bp_core_clear_member_count_caches' );
     56add_action( 'bp_core_deleted_account',        'bp_core_clear_member_count_caches' );
    4657
    4758/**
  • trunk/bp-core/bp-core-template.php

    r5942 r5987  
    503503add_filter( 'bp_create_excerpt', 'force_balance_tags' );
    504504
     505/**
     506 * Echoes the output of bp_get_total_member_count()
     507 */
    505508function bp_total_member_count() {
    506509    echo bp_get_total_member_count();
    507510}
     511    /**
     512     * Returns the total member count in your BP instance
     513     *
     514     * Since BuddyPress 1.6, this function has used bp_core_get_active_member_count(), which
     515     * counts non-spam, non-deleted users who have last_activity. This value will correctly
     516     * match the total member count number used for pagination on member directories.
     517     *
     518     * Before BuddyPress 1.6, this function used bp_core_get_total_member_count(), which did
     519     * not take into account last_activity, and thus often resulted in higher counts than
     520     * shown by member directory pagination.
     521     */
    508522    function bp_get_total_member_count() {
    509         return apply_filters( 'bp_get_total_member_count', bp_core_get_total_member_count() );
     523        return apply_filters( 'bp_get_total_member_count', bp_core_get_active_member_count() );
    510524    }
    511525    add_filter( 'bp_get_total_member_count', 'bp_core_number_format' );
  • trunk/bp-members/bp-members-functions.php

    r5986 r5987  
    487487
    488488/**
     489 * Returns the total number of members, limited to those members with last_activity
     490 *
     491 * @return int The number of active members
     492 */
     493function bp_core_get_active_member_count() {
     494    global $wpdb;
     495
     496    if ( !$count = get_transient( 'bp_active_member_count' ) ) {
     497        // Avoid a costly join by splitting the lookup
     498        if ( is_multisite() ) {
     499            $sql = $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE (user_status != 0 OR deleted != 0 OR user_status != 0)" );
     500        } else {
     501            $sql = $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE user_status != 0" );
     502        }
     503
     504        $exclude_users = $wpdb->get_col( $sql );
     505        $exclude_users_sql = !empty( $exclude_users ) ? $wpdb->prepare( "AND user_id NOT IN (" . implode( ',', wp_parse_id_list( $exclude_users ) ) . ")" ) : '';
     506
     507        $count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(user_id) FROM $wpdb->usermeta WHERE meta_key = 'last_activity' {$exclude_users_sql}" ) );
     508        set_transient( 'bp_active_member_count', $count );
     509    }
     510
     511    return apply_filters( 'bp_core_get_active_member_count', $count );
     512}
     513
     514/**
    489515 * Processes a spammed or unspammed user
    490516 *
     
    768794        return false;
    769795
     796    do_action( 'bp_core_pre_delete_account', $user_id );
     797
    770798    // Specifically handle multi-site environment
    771799    if ( is_multisite() ) {
     
    773801        require( ABSPATH . '/wp-admin/includes/user.php' );
    774802
    775         return wpmu_delete_user( $user_id );
     803        $retval = wpmu_delete_user( $user_id );
    776804
    777805    // Single site user deletion
    778806    } else {
    779807        require( ABSPATH . '/wp-admin/includes/user.php' );
    780         return wp_delete_user( $user_id );
    781     }
     808        $retval = wp_delete_user( $user_id );
     809    }
     810
     811    do_action( 'bp_core_deleted_account', $user_id );
     812
     813    return $retval;
    782814}
    783815
     
    11491181        $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->users SET user_pass = %s WHERE ID = %d", $user['meta']['password'], $user_id ) );
    11501182
    1151     // Delete the total member cache
    1152     wp_cache_delete( 'bp_total_member_count', 'bp' );
    1153 
    11541183    do_action( 'bp_core_activated_user', $user_id, $key, $user );
    11551184
  • trunk/bp-themes/bp-default/activity/index.php

    r5819 r5987  
    3737                    <?php do_action( 'bp_before_activity_type_tab_all' ); ?>
    3838
    39                     <li class="selected" id="activity-all"><a href="<?php echo trailingslashit( bp_get_root_domain() . '/' . bp_get_activity_slug() ); ?>" title="<?php _e( 'The public activity for everyone on this site.', 'buddypress' ); ?>"><?php printf( __( 'All Members <span>%s</span>', 'buddypress' ), bp_get_total_site_member_count() ); ?></a></li>
     39                    <li class="selected" id="activity-all"><a href="<?php echo trailingslashit( bp_get_root_domain() . '/' . bp_get_activity_slug() ); ?>" title="<?php _e( 'The public activity for everyone on this site.', 'buddypress' ); ?>"><?php printf( __( 'All Members <span>%s</span>', 'buddypress' ), bp_get_total_member_count() ); ?></a></li>
    4040
    4141                    <?php if ( is_user_logged_in() ) : ?>
     
    9090
    9191                    <li id="activity-filter-select" class="last">
    92                         <label for="activity-filter-by"><?php _e( 'Show:', 'buddypress' ); ?></label> 
     92                        <label for="activity-filter-by"><?php _e( 'Show:', 'buddypress' ); ?></label>
    9393                        <select id="activity-filter-by">
    9494                            <option value="-1"><?php _e( 'Everything', 'buddypress' ); ?></option>
Note: See TracChangeset for help on using the changeset viewer.