Skip to:
Content

BuddyPress.org

Changeset 8531


Ignore:
Timestamp:
06/15/2014 08:00:53 PM (10 years ago)
Author:
r-a-y
Message:

Check groups loop data when using bp_group_is_user_banned().

Previously, `bp_group_is_user_banned() did not reference the data already
queried in the groups loop via BP_Groups_Group::get_group_extras(). As a
result, if a user without the 'bp_moderate' cap was logged in and viewing
the group directory, this added twenty additional DB queries to the page.

Commit also standardizes the return value of bp_group_is_user_banned() to
a boolean. Previously, it was possible for the function to return either a
string of the integer, null or a boolean.

Fixes #5705.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/bp-groups-template.php

    r8503 r8531  
    16691669
    16701670/**
    1671  * Checks if a user is banned from a group.
    1672  *
    1673  * If this function is invoked inside the groups template loop (e.g. the group directory), then
    1674  * check $groups_template->group->is_banned instead of making another SQL query.
    1675  * However, if used in a single group's pages, we must use groups_is_user_banned().
     1671 * Check if a user is banned from a group.
     1672 *
     1673 * If this function is invoked inside the groups template loop, then we check
     1674 * $groups_template->group->is_banned instead of using {@link groups_is_user_banned()}
     1675 * and making another SQL query.
     1676 *
     1677 * In BuddyPress 2.1, to standardize this function, we are defaulting the
     1678 * return value to a boolean.  In previous versions, using this function would
     1679 * return either a string of the integer (0 or 1) or null if a result couldn't
     1680 * be found from the database.  If the logged-in user had the 'bp_moderate'
     1681 * capability, the return value would be boolean false.
     1682 *
     1683 * @since BuddyPress (1.5.0)
    16761684 *
    16771685 * @global BP_Groups_Template $groups_template Group template loop object
    1678  * @param object $group Group to check if user is banned from the group
    1679  * @param int $user_id
    1680  * @return bool If user is banned from the group or not
    1681  * @since BuddyPress (1.5)
     1686 * @param BP_Groups_Group $group Group to check if user is banned
     1687 * @param int $user_id The user ID to check
     1688 * @return bool True if user is banned.  False if user isn't banned.
    16821689 */
    16831690function bp_group_is_user_banned( $group = false, $user_id = 0 ) {
     
    16851692
    16861693    // Site admins always have access
    1687     if ( bp_current_user_can( 'bp_moderate' ) )
     1694    if ( bp_current_user_can( 'bp_moderate' ) ) {
    16881695        return false;
    1689 
    1690     if ( empty( $group ) ) {
    1691         $group =& $groups_template->group;
    1692 
    1693         if ( !$user_id && isset( $group->is_banned ) )
    1694             return apply_filters( 'bp_group_is_user_banned', $group->is_banned );
    1695     }
    1696 
    1697     if ( !$user_id )
    1698         $user_id = bp_loggedin_user_id();
    1699 
    1700     return apply_filters( 'bp_group_is_user_banned', groups_is_user_banned( $user_id, $group->id ) );
     1696    }
     1697
     1698    // check groups loop first
     1699    // @see BP_Groups_Group::get_group_extras()
     1700    if ( ! empty( $groups_template->in_the_loop ) && isset( $groups_template->group->is_banned ) ) {
     1701        $retval = $groups_template->group->is_banned;
     1702
     1703    // not in loop
     1704    } else {
     1705        // Default to not banned
     1706        $retval = false;
     1707
     1708        if ( empty( $group ) ) {
     1709            $group = $groups_template->group;
     1710        }
     1711   
     1712        if ( empty( $user_id ) ) {
     1713            $user_id = bp_loggedin_user_id();
     1714        }
     1715
     1716        if ( ! empty( $user_id ) && ! empty( $group->id ) ) {
     1717            $retval = groups_is_user_banned( $user_id, $group->id );
     1718        }
     1719    }
     1720
     1721    return (bool) apply_filters( 'bp_group_is_user_banned', $retval );
    17011722}
    17021723
  • trunk/tests/phpunit/testcases/groups/template.php

    r8419 r8531  
    690690        }
    691691    }
     692
     693    /**
     694     * @group bp_group_is_user_banned
     695     */
     696    public function test_bp_group_is_user_banned_in_groups_loop() {
     697        $u1 = $this->create_user();
     698        $u2 = $this->create_user();
     699        $g1 = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     700        $g2 = $this->factory->group->create( array( 'creator_id' => $u2 ) );
     701        groups_join_group( $g1, $u2 );
     702        groups_join_group( $g2, $u2 );
     703        groups_join_group( $g2, $u1 );
     704
     705        // Ban user 1 from group 2
     706        // Fool the admin check
     707        $old_user = get_current_user_id();
     708        $this->set_current_user( $u2 );
     709        buddypress()->is_item_admin = true;
     710        groups_ban_member( $u1, $g2 );
     711
     712        // Start the groups loop
     713        $this->set_current_user( $u1 );
     714        if ( bp_has_groups() ) : while ( bp_groups() ) : bp_the_group();
     715            $found[] = bp_group_is_user_banned();
     716        endwhile; endif;
     717
     718        // Assert
     719        $expected = array( 0, 1 );
     720        $this->assertEquals( $expected, $found );
     721
     722        // Clean up
     723        $GLOBALS['groups_template'] = null;
     724        $this->set_current_user( $old_user );
     725    }
     726
     727    /**
     728     * @group bp_group_is_user_banned
     729     */
     730    public function test_bp_group_is_user_banned_not_in_groups_loop() {
     731        $u1 = $this->create_user();
     732        $u2 = $this->create_user();
     733        $g1 = $this->factory->group->create( array( 'creator_id' => $u1 ) );
     734        $g2 = $this->factory->group->create( array( 'creator_id' => $u2 ) );
     735        groups_join_group( $g1, $u2 );
     736        groups_join_group( $g2, $u2 );
     737        groups_join_group( $g2, $u1 );
     738
     739        // Ban user 1 from group 2
     740        // Fool the admin check
     741        $old_user = get_current_user_id();
     742        $this->set_current_user( $u2 );
     743        buddypress()->is_item_admin = true;
     744        groups_ban_member( $u1, $g2 );
     745
     746        // Do group ban checks
     747        $group1 = new BP_Groups_Group( $g1 );
     748        $group2 = new BP_Groups_Group( $g2 );
     749
     750        $found = array();
     751        $found[] = bp_group_is_user_banned( $group1, $u1 );
     752        $found[] = bp_group_is_user_banned( $group2, $u1 );
     753
     754        // Assert
     755        $expected = array( 0, 1 );
     756        $this->assertEquals( $expected, $found );
     757
     758        // Clean up
     759        $this->set_current_user( $old_user );
     760    }
    692761}
Note: See TracChangeset for help on using the changeset viewer.