Skip to:
Content

BuddyPress.org

Changeset 10227


Ignore:
Timestamp:
10/10/2015 01:07:23 PM (5 years ago)
Author:
djpaul
Message:

Groups: new function to get groups from which a user is banned.

Fixes #6562

props lenasterg

Location:
trunk
Files:
2 edited

Legend:

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

    r10148 r10227  
    628628
    629629    /**
     630     * Get the groups of which a specified user is banned from.
     631     *
     632     * @since 2.4
     633     *
     634     * @param int         $user_id ID of the user.
     635     * @param int|bool    $limit   Optional. Max number of results to return.
     636     *                             Default: false (no limit).
     637     * @param int|bool    $page    Optional. Page offset of results to return.
     638     *                             Default: false (no limit).
     639     * @param string|bool $filter  Optional. Limit results to groups whose name or
     640     *                             description field matches search terms.
     641     * @return array {
     642     *     @type array $groups Array of groups returned by paginated query.
     643     *     @type int   $total  Count of groups matching query.
     644     * }
     645     */
     646    public static function get_is_banned_of( $user_id, $limit = false, $page = false, $filter = false ) {
     647        global $wpdb;
     648
     649        $bp = buddypress();
     650
     651        $user_id_sql = $pag_sql = $hidden_sql = $filter_sql = '';
     652        $user_id_sql = $wpdb->prepare( 'm.user_id = %d', $user_id );
     653
     654        if ( $limit && $page ) {
     655            $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit ), intval( $limit ) );
     656      }
     657
     658        if ( $filter ) {
     659            $search_terms_like = '%' . bp_esc_like( $filter ) . '%';
     660            $filter_sql        = $wpdb->prepare( " AND ( g.name LIKE %s OR g.description LIKE %s )", $search_terms_like, $search_terms_like );
     661        }
     662
     663        if ( $user_id !== bp_loggedin_user_id() && ! bp_current_user_can( 'bp_moderate' ) ) {
     664            $hidden_sql = " AND g.status != 'hidden'";
     665        }
     666
     667        $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}" );
     668        $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" );
     669
     670        return array( 'groups' => $paged_groups, 'total' => $total_groups );
     671    }
     672
     673    /**
    630674     * Get the count of groups of which the specified user is a member.
    631675     *
  • trunk/tests/phpunit/testcases/groups/class-bp-groups-member.php

    r9900 r10227  
    9494    }
    9595
     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        ) );
     103
     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        $m2->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(
Note: See TracChangeset for help on using the changeset viewer.