Skip to:
Content

BuddyPress.org


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.

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.