<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][13281] branches/10.0: Improve group members count query performance</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://buddypress.trac.wordpress.org/changeset/13281">13281</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://buddypress.trac.wordpress.org/changeset/13281","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>imath</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-05-18 05:46:32 +0000 (Wed, 18 May 2022)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Improve group members count query performance

<a href="http://buddypress.trac.wordpress.org/changeset/13103">[13103]</a> introduced a change in 10.0.0 that can be very time consuming when a group has a lot of members.

To keep the main improvements of the referenced commit (only refreshing group members count when a user joins or leaves a group) but optimize queries performance, we are introducing a new way to count group members in the `BP_Group_Member_Query`.

We are also introducing a way to defer group members count when adding a batch of members to a group. Using `bp_groups_defer_group_members_count()` avoids to refresh the count each time a member of this batch is added. For more information about how to use this function, you can have a look at how BuddyPress is using it into `src/bp-groups/bp-groups-admin.php`.

Props dd32, espellcaste

Fixes <a href="http://buddypress.trac.wordpress.org/ticket/8688">#8688</a> (branch 10.0)</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#branches100srcbpgroupsbpgroupsadminphp">branches/10.0/src/bp-groups/bp-groups-admin.php</a></li>
<li><a href="#branches100srcbpgroupsbpgroupsfunctionsphp">branches/10.0/src/bp-groups/bp-groups-functions.php</a></li>
<li><a href="#branches100srcbpgroupsclassesclassbpgroupmemberqueryphp">branches/10.0/src/bp-groups/classes/class-bp-group-member-query.php</a></li>
<li><a href="#branches100srcbpgroupsclassesclassbpgroupsgroupphp">branches/10.0/src/bp-groups/classes/class-bp-groups-group.php</a></li>
<li><a href="#branches100srcbpgroupsclassesclassbpgroupsmemberphp">branches/10.0/src/bp-groups/classes/class-bp-groups-member.php</a></li>
<li><a href="#branches100testsphpunittestcasesgroupsfunctionsphp">branches/10.0/tests/phpunit/testcases/groups/functions.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="branches100srcbpgroupsbpgroupsadminphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/10.0/src/bp-groups/bp-groups-admin.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/10.0/src/bp-groups/bp-groups-admin.php   2022-05-18 05:32:26 UTC (rev 13280)
+++ branches/10.0/src/bp-groups/bp-groups-admin.php     2022-05-18 05:46:32 UTC (rev 13281)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -354,7 +354,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! empty( $user_names ) ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        $new_members = count( $user_names );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        if ( 1 < $new_members ) {
+                               bp_groups_defer_group_members_count( true );
+                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         foreach( array_values( $user_names ) as $user_name ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $un = trim( $user_name );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -372,6 +377,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        }
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       if ( 1 < $new_members ) {
+                               bp_groups_defer_group_members_count( false, $group_id );
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Process member role changes.
</span></span></pre></div>
<a id="branches100srcbpgroupsbpgroupsfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/10.0/src/bp-groups/bp-groups-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/10.0/src/bp-groups/bp-groups-functions.php       2022-05-18 05:32:26 UTC (rev 13280)
+++ branches/10.0/src/bp-groups/bp-groups-functions.php 2022-05-18 05:46:32 UTC (rev 13281)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3678,3 +3678,46 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'bp_init', 'bp_init_group_extensions', 11 );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Updates a group members count when a user joined or left the group.
+ *
+ * @since 10.3.0
+ *
+ * @param BP_Groups_Member|int $groups_member The BP_Groups_Member object or the group member ID.
+ * @param int                  $group_id      The group's ID.
+ */
+function bp_groups_update_group_members_count( $groups_member, $group_id = 0 ) {
+       if ( $groups_member instanceof BP_Groups_Member ) {
+               $group_id = $groups_member->group_id;
+       }
+
+       BP_Groups_Member::refresh_total_member_count_for_group( (int) $group_id );
+}
+add_action( 'groups_member_after_save', 'bp_groups_update_group_members_count' );
+add_action( 'groups_member_after_remove', 'bp_groups_update_group_members_count' );
+add_action( 'bp_groups_member_after_delete', 'bp_groups_update_group_members_count', 10, 2 );
+
+/**
+ * Defers a group's counting to avoid updating it when batch adding/removing users to this group.
+ *
+ * @since 10.3.0
+ *
+ * @param bool $defer True to defer, false otherwise.
+ * @param int $group_id The group's ID.
+ */
+function bp_groups_defer_group_members_count( $defer = true, $group_id = 0 ) {
+       if ( $defer ) {
+               remove_action( 'groups_member_after_save', 'bp_groups_update_group_members_count' );
+               remove_action( 'groups_member_after_remove', 'bp_groups_update_group_members_count' );
+               remove_action( 'bp_groups_member_after_delete', 'bp_groups_update_group_members_count', 10, 2 );
+       } else {
+               add_action( 'groups_member_after_save', 'bp_groups_update_group_members_count' );
+               add_action( 'groups_member_after_remove', 'bp_groups_update_group_members_count' );
+               add_action( 'bp_groups_member_after_delete', 'bp_groups_update_group_members_count', 10, 2 );
+       }
+
+       if  ( $group_id ) {
+               bp_groups_update_group_members_count( 0, (int) $group_id );
+       }
+}
</ins></span></pre></div>
<a id="branches100srcbpgroupsclassesclassbpgroupmemberqueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/10.0/src/bp-groups/classes/class-bp-group-member-query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/10.0/src/bp-groups/classes/class-bp-group-member-query.php       2022-05-18 05:32:26 UTC (rev 13280)
+++ branches/10.0/src/bp-groups/classes/class-bp-group-member-query.php 2022-05-18 05:46:32 UTC (rev 13281)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -50,6 +50,24 @@
</span><span class="cx" style="display: block; padding: 0 10px">        protected $group_member_ids;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Constructor.
+        *
+        * @since 10.3.0
+        *
+        * @param string|array|null $query See {@link BP_User_Query}.
+        */
+       public function __construct( $query = null ) {
+               $qv = bp_parse_args(
+                       $query,
+                       array(
+                               'count' => false, // True to perform a count query. False otherwise.
+                       )
+               );
+
+               parent::__construct( $qv );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Set up action hooks.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 1.8.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -62,14 +80,63 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->query_vars_raw['type'] = 'last_joined';
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Set the sort order.
-               add_action( 'bp_pre_user_query', array( $this, 'set_orderby' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( ! $this->query_vars_raw['count'] ) {
+                       // Set the sort order.
+                       add_action( 'bp_pre_user_query', array( $this, 'set_orderby' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Set up our populate_extras method.
-               add_action( 'bp_user_query_populate_extras', array( $this, 'populate_group_member_extras' ), 10, 2 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Set up our populate_extras method.
+                       add_action( 'bp_user_query_populate_extras', array( $this, 'populate_group_member_extras' ), 10, 2 );
+               } else {
+                       $this->query_vars_raw['orderby'] = 'ID';
+               }
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Use WP_User_Query() to pull data for the user IDs retrieved in the main query.
+        *
+        * If a `count` query is performed, the function is used to validate existing users.
+        *
+        * @since 10.3.0
+        */
+       public function do_wp_user_query() {
+               if ( ! $this->query_vars_raw['count'] ) {
+                       return parent::do_wp_user_query();
+               }
+
+               /**
+                * Filters the WP User Query arguments before passing into the class.
+                *
+                * @since 10.3.0
+                *
+                * @param array         $value Array of arguments for the user query.
+                * @param BP_User_Query $this  Current BP_User_Query instance.
+                */
+               $wp_user_query = new WP_User_Query(
+                       apply_filters(
+                               'bp_group_members_count_query_args',
+                               array(
+                                       // Relevant.
+                                       'fields'      => 'ID',
+                                       'include'     => $this->user_ids,
+
+                                       // Overrides
+                                       'blog_id'     => 0,    // BP does not require blog roles.
+                                       'count_total' => false // We already have a count.
+
+                               ),
+                               $this
+                       )
+               );
+
+               // Validate existing user IDs.
+               $this->user_ids = array_map( 'intval', $wp_user_query->results );
+               $this->results  = $this->user_ids;
+
+               // Set the total existing users.
+               $this->total_users = count( $this->user_ids );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Get a list of user_ids to include in the IN clause of the main query.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * Overrides BP_User_Query::get_include_ids(), adding our additional
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -473,4 +540,25 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                return wp_list_pluck( $group_user_ids, 'user_id' );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * Perform a database query to populate any extra metadata we might need.
+        *
+        * If a `count` query is performed, the function is used to validate active users.
+        *
+        * @since 10.3.0
+        */
+       public function populate_extras() {
+               if ( ! $this->query_vars_raw['count'] ) {
+                       return parent::populate_extras();
+               }
+
+               // Validate active users.
+               $active_users    = array_filter( BP_Core_User::get_last_activity( $this->user_ids ) );
+               $active_user_ids = array_keys( $active_users );
+               $this->results   = array_intersect( $this->user_ids, $active_user_ids );
+
+               // Set the total active users.
+               $this->total_users = count( $this->results );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="branches100srcbpgroupsclassesclassbpgroupsgroupphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/10.0/src/bp-groups/classes/class-bp-groups-group.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/10.0/src/bp-groups/classes/class-bp-groups-group.php     2022-05-18 05:32:26 UTC (rev 13280)
+++ branches/10.0/src/bp-groups/classes/class-bp-groups-group.php       2022-05-18 05:46:32 UTC (rev 13281)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1789,21 +1789,20 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @return int Count of confirmed members for the group.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public static function get_total_member_count( $group_id, $skip_cache = false ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $cache_key = 'total_member_count';
-               $count     = groups_get_groupmeta( $group_id, $cache_key );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $meta_key = 'total_member_count';
+               $count    = groups_get_groupmeta( $group_id, $meta_key );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( false === $count || true === $skip_cache ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $members = groups_get_group_members(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $group_members = new BP_Group_Member_Query(
</ins><span class="cx" style="display: block; padding: 0 10px">                                 array(
</span><span class="cx" style="display: block; padding: 0 10px">                                        'group_id'   => $group_id,
</span><span class="cx" style="display: block; padding: 0 10px">                                        'group_role' => array( 'member', 'admin', 'mod' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        'type'       => 'active',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 'count'      => true,
</ins><span class="cx" style="display: block; padding: 0 10px">                                 )
</span><span class="cx" style="display: block; padding: 0 10px">                        );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $count = $members['count'] ? $members['count'] : 0;
-
-                       groups_update_groupmeta( $group_id, $cache_key, (int) $count );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $count = $group_members->total_users;
+                       groups_update_groupmeta( $group_id, $meta_key, $count );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1814,7 +1813,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 * @param int $count    Total member count for group.
</span><span class="cx" style="display: block; padding: 0 10px">                 * @param int $group_id The ID of the group.
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                return (int) apply_filters( 'bp_groups_total_member_count', (int) $count, (int) $group_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         return (int) apply_filters( 'bp_groups_total_member_count', $count, (int) $group_id );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span></span></pre></div>
<a id="branches100srcbpgroupsclassesclassbpgroupsmemberphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/10.0/src/bp-groups/classes/class-bp-groups-member.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/10.0/src/bp-groups/classes/class-bp-groups-member.php    2022-05-18 05:32:26 UTC (rev 13280)
+++ branches/10.0/src/bp-groups/classes/class-bp-groups-member.php      2022-05-18 05:46:32 UTC (rev 13281)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -309,9 +309,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Update the user's group count.
</span><span class="cx" style="display: block; padding: 0 10px">                self::refresh_total_group_count_for_user( $this->user_id );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Update the group's member count.
-               self::refresh_total_member_count_for_group( $this->group_id );
-
</del><span class="cx" style="display: block; padding: 0 10px">                 /**
</span><span class="cx" style="display: block; padding: 0 10px">                 * Fires after the current group membership item has been saved.
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -447,9 +444,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Update the user's group count.
</span><span class="cx" style="display: block; padding: 0 10px">                self::refresh_total_group_count_for_user( $this->user_id );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Update the group's member count.
-               self::refresh_total_member_count_for_group( $this->group_id );
-
</del><span class="cx" style="display: block; padding: 0 10px">                 /**
</span><span class="cx" style="display: block; padding: 0 10px">                 * Fires after a member is removed from a group.
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -517,9 +511,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Update the user's group count.
</span><span class="cx" style="display: block; padding: 0 10px">                self::refresh_total_group_count_for_user( $user_id );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Update the group's member count.
-               self::refresh_total_member_count_for_group( $group_id );
-
</del><span class="cx" style="display: block; padding: 0 10px">                 /**
</span><span class="cx" style="display: block; padding: 0 10px">                 * Fires after a member is removed from a group.
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span></span></pre></div>
<a id="branches100testsphpunittestcasesgroupsfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/10.0/tests/phpunit/testcases/groups/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/10.0/tests/phpunit/testcases/groups/functions.php        2022-05-18 05:32:26 UTC (rev 13280)
+++ branches/10.0/tests/phpunit/testcases/groups/functions.php  2022-05-18 05:46:32 UTC (rev 13281)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7,6 +7,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> class BP_Tests_Groups_Functions extends BP_UnitTestCase {
</span><span class="cx" style="display: block; padding: 0 10px">        static public $group_ids;
</span><span class="cx" style="display: block; padding: 0 10px">        static public $user_ids;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        protected $did_group_member_count = 0;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        static public function wpSetUpBeforeClass( $factory ) {
</span><span class="cx" style="display: block; padding: 0 10px">                self::$user_ids  = $factory->user->create_many( 3 );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -346,6 +347,71 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * @group total_member_count
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @ticket BP8688
+        */
+       public function test_total_member_count_groups_inactive_user() {
+               $u1 = self::factory()->user->create();
+               $u2 = wp_insert_user( array(
+                       'user_pass'  => 'foobar',
+                       'user_login' => 'foobar',
+                       'user_email' => 'foobar@buddypress.org',
+               ) );
+
+               $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
+
+               groups_join_group( $g1, $u2 );
+
+               $this->assertEquals( 1, groups_get_total_member_count( $g1 ) );
+       }
+
+       /**
+        * @group total_member_count
+        * @ticket BP8688
+        */
+       public function test_total_member_count_groups_spammed_user() {
+               $u1 = self::factory()->user->create();
+               $u2 = self::factory()->user->create();
+
+               $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
+
+               groups_join_group( $g1, $u2 );
+               bp_core_process_spammer_status( $u2, 'spam' );
+
+               $this->assertEquals( 1, groups_get_total_member_count( $g1 ) );
+       }
+
+       /**
+        * @group total_member_count
+        * @ticket BP8688
+        */
+       public function test_total_member_count_groups_deferred() {
+               $u1 = self::factory()->user->create();
+               $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
+               $members = array();
+               $this->did_group_member_count = 0;
+
+               add_filter( 'bp_groups_total_member_count', array( $this, 'filter_bp_groups_total_member_count' ) );
+
+               bp_groups_defer_group_members_count( true );
+               for ( $i = 1; $i < 6; $i++ ) {
+                       $members[ $i ] = self::factory()->user->create();
+                       groups_join_group( $g1, $members[ $i ] );
+               }
+               bp_groups_defer_group_members_count( false, $g1 );
+
+               remove_filter( 'bp_groups_total_member_count', array( $this, 'filter_bp_groups_total_member_count' ) );
+
+               $this->assertTrue( 1 === $this->did_group_member_count );
+               $this->assertEquals( count( $members ) + 1, groups_get_total_member_count( $g1 ) );
+       }
+
+       public function filter_bp_groups_total_member_count( $count ) {
+               $this->did_group_member_count += 1;
+               return $count;
+       }
+
+       /**
+        * @group total_member_count
</ins><span class="cx" style="display: block; padding: 0 10px">          * @group groups_create_group
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_total_member_count_groups_create_group() {
</span></span></pre>
</div>
</div>

</body>
</html>