Skip to:
Content

BuddyPress.org

Ticket #8688: 8688.patch

File 8688.patch, 6.3 KB (added by imath, 3 years ago)
  • src/bp-groups/classes/class-bp-group-member-query.php

    diff --git src/bp-groups/classes/class-bp-group-member-query.php src/bp-groups/classes/class-bp-group-member-query.php
    index 267c81e09..d91224978 100644
    class BP_Group_Member_Query extends BP_User_Query { 
    4949         */
    5050        protected $group_member_ids;
    5151
     52        /**
     53         * Constructor.
     54         *
     55         * @since 10.3.0
     56         *
     57         * @param string|array|null $query See {@link BP_User_Query}.
     58         */
     59        public function __construct( $query = null ) {
     60                $qv = bp_parse_args(
     61                        $query,
     62                        array(
     63                                'count' => false, // True to perform a count query. False otherwise.
     64                        )
     65                );
     66
     67                parent::__construct( $qv );
     68        }
     69
    5270        /**
    5371         * Set up action hooks.
    5472         *
    class BP_Group_Member_Query extends BP_User_Query { 
    6280                        $this->query_vars_raw['type'] = 'last_joined';
    6381                }
    6482
    65                 // Set the sort order.
    66                 add_action( 'bp_pre_user_query', array( $this, 'set_orderby' ) );
     83                if ( ! $this->query_vars_raw['count'] ) {
     84                        // Set the sort order.
     85                        add_action( 'bp_pre_user_query', array( $this, 'set_orderby' ) );
    6786
    68                 // Set up our populate_extras method.
    69                 add_action( 'bp_user_query_populate_extras', array( $this, 'populate_group_member_extras' ), 10, 2 );
     87                        // Set up our populate_extras method.
     88                        add_action( 'bp_user_query_populate_extras', array( $this, 'populate_group_member_extras' ), 10, 2 );
     89                } else {
     90                        $this->query_vars_raw['orderby'] = 'ID';
     91                }
     92        }
     93
     94        /**
     95         * Use WP_User_Query() to pull data for the user IDs retrieved in the main query.
     96         *
     97         * If a `count` query is performed, the function is used to validate existing users.
     98         *
     99         * @since 10.3.0
     100         */
     101        public function do_wp_user_query() {
     102                if ( ! $this->query_vars_raw['count'] ) {
     103                        return parent::do_wp_user_query();
     104                }
     105
     106                /**
     107                 * Filters the WP User Query arguments before passing into the class.
     108                 *
     109                 * @since 10.3.0
     110                 *
     111                 * @param array         $value Array of arguments for the user query.
     112                 * @param BP_User_Query $this  Current BP_User_Query instance.
     113                 */
     114                $wp_user_query = new WP_User_Query(
     115                        apply_filters(
     116                                'bp_group_members_count_query_args',
     117                                array(
     118                                        // Relevant.
     119                                        'fields'      => 'ID',
     120                                        'include'     => $this->user_ids,
     121
     122                                        // Overrides
     123                                        'blog_id'     => 0,    // BP does not require blog roles.
     124                                        'count_total' => false // We already have a count.
     125
     126                                ),
     127                                $this
     128                        )
     129                );
     130
     131                // Validate existing user IDs.
     132                $this->user_ids = array_map( 'intval', $wp_user_query->results );
     133                $this->results  = $this->user_ids;
     134
     135                // Set the total existing users.
     136                $this->total_users = count( $this->user_ids );
    70137        }
    71138
    72139        /**
    class BP_Group_Member_Query extends BP_User_Query { 
    473540
    474541                return wp_list_pluck( $group_user_ids, 'user_id' );
    475542        }
     543
     544        /**
     545         * Perform a database query to populate any extra metadata we might need.
     546         *
     547         * If a `count` query is performed, the function is used to validate active users.
     548         *
     549         * @since 10.3.0
     550         */
     551        public function populate_extras() {
     552                if ( ! $this->query_vars_raw['count'] ) {
     553                        return parent::populate_extras();
     554                }
     555
     556                // Validate active users.
     557                $active_users    = array_filter( BP_Core_User::get_last_activity( $this->user_ids ) );
     558                $active_user_ids = array_keys( $active_users );
     559                $this->results   = array_intersect( $this->user_ids, $active_user_ids );
     560
     561                // Set the total active users.
     562                $this->total_users = count( $this->results );
     563        }
    476564}
  • src/bp-groups/classes/class-bp-groups-group.php

    diff --git src/bp-groups/classes/class-bp-groups-group.php src/bp-groups/classes/class-bp-groups-group.php
    index c0b518237..e837d72ed 100644
    class BP_Groups_Group { 
    17891789         * @return int Count of confirmed members for the group.
    17901790         */
    17911791        public static function get_total_member_count( $group_id, $skip_cache = false ) {
    1792                 $cache_key = 'total_member_count';
    1793                 $count     = groups_get_groupmeta( $group_id, $cache_key );
     1792                $meta_key = 'total_member_count';
     1793                $count    = groups_get_groupmeta( $group_id, $meta_key );
    17941794
    17951795                if ( false === $count || true === $skip_cache ) {
    1796                         $members = groups_get_group_members(
     1796                        $group_members = new BP_Group_Member_Query(
    17971797                                array(
    17981798                                        'group_id'   => $group_id,
    17991799                                        'group_role' => array( 'member', 'admin', 'mod' ),
    1800                                         'type'       => 'active',
     1800                                        'count'      => true,
    18011801                                )
    18021802                        );
    18031803
    1804                         $count = $members['count'] ? $members['count'] : 0;
    1805 
    1806                         groups_update_groupmeta( $group_id, $cache_key, (int) $count );
     1804                        $count = $group_members->total_users;
     1805                        groups_update_groupmeta( $group_id, $meta_key, $count );
    18071806                }
    18081807
    18091808                /**
    class BP_Groups_Group { 
    18141813                 * @param int $count    Total member count for group.
    18151814                 * @param int $group_id The ID of the group.
    18161815                 */
    1817                 return (int) apply_filters( 'bp_groups_total_member_count', (int) $count, (int) $group_id );
     1816                return (int) apply_filters( 'bp_groups_total_member_count', $count, (int) $group_id );
    18181817        }
    18191818
    18201819        /**
  • tests/phpunit/testcases/groups/functions.php

    diff --git tests/phpunit/testcases/groups/functions.php tests/phpunit/testcases/groups/functions.php
    index 208378547..2e1bf1040 100644
    class BP_Tests_Groups_Functions extends BP_UnitTestCase { 
    344344                $this->assertEquals( 2, BP_Groups_Group::get_total_member_count( $g1 ) );
    345345        }
    346346
     347        /**
     348         * @group total_member_count
     349         */
     350        public function test_total_member_count_groups_inactive_user() {
     351                $u1 = self::factory()->user->create();
     352                $u2 = wp_insert_user( array(
     353                        'user_pass'  => 'foobar',
     354                        'user_login' => 'foobar',
     355                        'user_email' => 'foobar@buddypress.org',
     356                ) );
     357
     358                $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
     359
     360                groups_join_group( $g1, $u2 );
     361
     362                $this->assertEquals( 1, groups_get_total_member_count( $g1 ) );
     363        }
     364
     365        /**
     366         * @group total_member_count
     367         */
     368        public function test_total_member_count_groups_spammed_user() {
     369                $u1 = self::factory()->user->create();
     370                $u2 = self::factory()->user->create();
     371
     372                $g1 = self::factory()->group->create( array( 'creator_id' => $u1 ) );
     373
     374                groups_join_group( $g1, $u2 );
     375                bp_core_process_spammer_status( $u2, 'spam' );
     376
     377                $this->assertEquals( 1, groups_get_total_member_count( $g1 ) );
     378        }
     379
    347380        /**
    348381         * @group total_member_count
    349382         * @group groups_create_group