Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/09/2021 02:01:40 PM (3 years ago)
Author:
espellcaste
Message:

Improving the group member count routine and the function helper.

The group member count routine was updated to avoid direct, uncached, SQL query and unnecessary cache refresh when a group's Members page was viewed.

is now being used to get the group member count which takes into account users' existence in the site,

the query is now cached and filterable.

was also updated to get the current group from if available.

Props imath
Fixes #7614 and see #6749

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/classes/class-bp-groups-group.php

    r13086 r13103  
    17241724     *
    17251725     * @since 1.6.0
    1726      *
    1727      * @return int Group count.
    1728      */
    1729     public static function get_total_group_count() {
     1726     * @since 10.0.0 Added the `$skip_cache` parameter.
     1727     *
     1728     * @global BuddyPress $bp   The one true BuddyPress instance.
     1729     * @global wpdb       $wpdb WordPress database object.
     1730     *
     1731     * @param bool $skip_cache Optional. Skip getting count from cache.
     1732     *                         Defaults to false.
     1733     * @return int
     1734     */
     1735    public static function get_total_group_count( $skip_cache = false ) {
    17301736        global $wpdb;
    17311737
    1732         $hidden_sql = '';
    1733         if ( !bp_current_user_can( 'bp_moderate' ) )
    1734             $hidden_sql = "WHERE status != 'hidden'";
    1735 
    1736         $bp = buddypress();
    1737 
    1738         return $wpdb->get_var( "SELECT COUNT(id) FROM {$bp->groups->table_name} {$hidden_sql}" );
     1738        $cache_key = 'bp_total_group_count';
     1739        $count     = wp_cache_get( $cache_key, 'bp' );
     1740
     1741        if ( false === $count || true === $skip_cache ) {
     1742            $hidden_sql = '';
     1743            if ( ! bp_current_user_can( 'bp_moderate' ) ) {
     1744                $hidden_sql = "WHERE status != 'hidden'";
     1745            }
     1746
     1747            $bp    = buddypress();
     1748            $count = $wpdb->get_var( "SELECT COUNT(id) FROM {$bp->groups->table_name} {$hidden_sql}" );
     1749
     1750            wp_cache_set( $cache_key, (int) $count, 'bp' );
     1751        }
     1752
     1753        /**
     1754         * Filters the total group count.
     1755         *
     1756         * @since 10.0.0
     1757         *
     1758         * @param int $count Total group count.
     1759         */
     1760        return (int) apply_filters( 'bp_groups_total_group_count', (int) $count );
    17391761    }
    17401762
     
    17431765     *
    17441766     * @since 1.6.0
    1745      *
    1746      * @param int $group_id Group ID.
     1767     * @since 10.0.0 Updated to use the `groups_get_group_members`.
     1768     *
     1769     * @param int  $group_id   Group ID.
     1770     * @param bool $skip_cache Optional. Skip getting count from cache. Defaults to false.
    17471771     * @return int Count of confirmed members for the group.
    17481772     */
    1749     public static function get_total_member_count( $group_id ) {
    1750         global $wpdb;
    1751 
    1752         $bp = buddypress();
    1753 
    1754         return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_confirmed = 1 AND is_banned = 0", $group_id ) );
     1773    public static function get_total_member_count( $group_id, $skip_cache = false ) {
     1774        $cache_key = 'total_member_count';
     1775        $count     = groups_get_groupmeta( $group_id, $cache_key );
     1776
     1777        if ( false === $count || true === $skip_cache ) {
     1778            $members = groups_get_group_members(
     1779                array(
     1780                    'group_id'            => $group_id,
     1781                    'exclude_banned'      => true,
     1782                    'exclude_admins_mods' => false,
     1783                    'type'                => 'active',
     1784                )
     1785            );
     1786
     1787            $count = $members['count'] ? $members['count'] : 0;
     1788
     1789            groups_update_groupmeta( $group_id, $cache_key, (int) $count );
     1790        }
     1791
     1792        /**
     1793         * Filters the total member count for a group.
     1794         *
     1795         * @since 10.0.0
     1796         *
     1797         * @param int $count    Total member count for group.
     1798         * @param int $group_id The ID of the group.
     1799         */
     1800        return (int) apply_filters( 'bp_groups_total_member_count', (int) $count, (int) $group_id );
    17551801    }
    17561802
Note: See TracChangeset for help on using the changeset viewer.