Skip to:
Content

BuddyPress.org

Ticket #6562: get_is_banned_of.patch

File get_is_banned_of.patch, 3.9 KB (added by lenasterg, 5 years ago)
  • src/bp-groups/classes/class-bp-groups-member.php

     
    625625
    626626                return array( 'groups' => $paged_groups, 'total' => $total_groups );
    627627        }
     628       
     629        /**
     630         * Get the groups of which a specified user is banned from.
     631         *
     632         * @param int         $user_id ID of the user.
     633         * @param int|bool    $limit   Optional. Max number of results to return.
     634         *                             Default: false (no limit).
     635         * @param int|bool    $page    Optional. Page offset of results to return.
     636         *                             Default: false (no limit).
     637         * @param string|bool $filter  Optional. Limit results to groups whose name or
     638         *                             description field matches search terms.
     639         *
     640         * @return array {
     641         *     @type array $groups Array of groups returned by paginated query.
     642         *     @type int   $total  Count of groups matching query.
     643         * }
     644         */
     645        public static function get_is_banned_of( $user_id, $limit = false, $page = false, $filter = false ) {
     646        global $wpdb;
     647        $bp = buddypress();
    628648
     649        $user_id_sql = $pag_sql = $hidden_sql = $filter_sql = '';
     650
     651        $user_id_sql = $wpdb->prepare( 'm.user_id = %d', $user_id );
     652
     653        if ( ! empty( $limit ) && ! empty( $page ) )
     654            $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit ), intval( $limit ) );
     655
     656        if ( ! empty( $filter ) ) {
     657            $search_terms_like = '%' . bp_esc_like( $filter ) . '%';
     658            $filter_sql = $wpdb->prepare( " AND ( g.name LIKE %s OR g.description LIKE %s )", $search_terms_like, $search_terms_like );
     659        }
     660
     661        if ( ( $user_id != bp_loggedin_user_id()) && ( ! bp_current_user_can( 'bp_moderate' )) ) {
     662            $hidden_sql = " AND g.status != 'hidden'";
     663        }
     664        $paged_groups = $wpdb->get_results( "SELECT g.*, gm1.meta_value as total_member_count, gm2.meta_value as last_activity FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE g.id = m.group_id AND g.id = gm1.group_id AND g.id = gm2.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count'{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_banned = 1  ORDER BY m.date_modified ASC {$pag_sql}" );
     665        $total_groups = $wpdb->get_var( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND  m.is_banned = 1 ORDER BY date_modified ASC" );
     666
     667        return array( 'groups' => $paged_groups, 'total' => $total_groups );
     668    }
     669       
    629670        /**
    630671         * Get the count of groups of which the specified user is a member.
    631672         *
  • tests/phpunit/testcases/groups/class-bp-groups-member.php

     
    9292                $ids = wp_list_pluck( $groups['groups'], 'id' );
    9393                $this->assertEquals( $ids, array( $g2 ) );
    9494        }
     95       
     96        public function test_get_is_banned_of_with_filter() {
     97                $g1 = $this->factory->group->create( array(
     98                        'name' => 'RC Cola',
     99                ) );
     100                $g2 = $this->factory->group->create( array(
     101                        'name' => 'Pepsi',
     102                ) );
    95103
     104                $u = $this->factory->user->create();
     105                self::add_user_to_group( $u, $g1 );
     106                self::add_user_to_group( $u, $g2 );
     107
     108                $m1 = new BP_Groups_Member( $u, $g1 );
     109                $m1->ban();
     110                $m2 = new BP_Groups_Member( $u, $g2 );
     111                $m1->ban();
     112
     113                $groups = BP_Groups_Member::get_is_banned_of( $u, false, false, 'eps' );
     114
     115                $ids = wp_list_pluck( $groups['groups'], 'id' );
     116                $this->assertEquals( $ids, array( $g2 ) );
     117        }
     118
    96119        public function test_get_invites_with_exclude() {
    97120                $g1 = $this->factory->group->create( array(
    98121                        'name' => 'RC Cola',