Skip to:
Content

BuddyPress.org

Changeset 7898


Ignore:
Timestamp:
02/16/2014 09:47:18 PM (8 years ago)
Author:
boonebgorges
Message:

Introduce $args param to BP_Groups_Group constructor and groups_get_group()

This argument array can contain the following toggles:

  • update_meta_cache (pre-fetch all group metadata for the queried group)
  • populate_extras (pre-fetch last activity, admin/mod lists, and current user access/membership status in queried group)

Both items default to false, for backward compatibility

Allowing these pre-fetch toggles to be set to false allows us to avoid large
numbers of unnecessary database queries in certain cases, such as when
secondary avatars are populated on activity directories.

See #5398

Location:
trunk/bp-groups
Files:
2 edited

Legend:

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

    r7897 r7898  
    147147
    148148    /**
     149     * Raw arguments passed to the constructor.
     150     *
     151     * @since BuddyPress (2.0.0)
     152     * @var array
     153     */
     154    protected $args;
     155
     156    /**
    149157     * Constructor method.
    150158     *
     
    152160     *        the object will be pre-populated with info about that group.
    153161     */
    154     public function __construct( $id = null ) {
     162    public function __construct( $id = null, $args = array() ) {
     163        $this->args = $args;
     164
    155165        if ( !empty( $id ) ) {
    156166            $this->id = $id;
     
    166176
    167177        if ( $group = $wpdb->get_row( $wpdb->prepare( "SELECT g.* FROM {$bp->groups->table_name} g WHERE g.id = %d", $this->id ) ) ) {
    168             bp_groups_update_meta_cache( $this->id );
     178            if ( ! empty( $this->args['update_meta_cache'] ) ) {
     179                bp_groups_update_meta_cache( $this->id );
     180            }
    169181
    170182            $this->id                 = $group->id;
     
    176188            $this->enable_forum       = $group->enable_forum;
    177189            $this->date_created       = $group->date_created;
    178             $this->last_activity      = groups_get_groupmeta( $this->id, 'last_activity' );
    179             $this->total_member_count = groups_get_groupmeta( $this->id, 'total_member_count' );
    180             $this->is_member          = BP_Groups_Member::check_is_member( bp_loggedin_user_id(), $this->id );
    181             $this->is_invited         = BP_Groups_Member::check_has_invite( bp_loggedin_user_id(), $this->id );
    182             $this->is_pending         = BP_Groups_Member::check_for_membership_request( bp_loggedin_user_id(), $this->id );
    183 
    184             // If this is a private or hidden group, does the current user have access?
    185             if ( 'private' == $this->status || 'hidden' == $this->status ) {
    186                 if ( $this->is_member && is_user_logged_in() || bp_current_user_can( 'bp_moderate' ) )
     190
     191            if ( ! empty( $this->args['populate_extras'] ) ) {
     192                $this->last_activity      = groups_get_groupmeta( $this->id, 'last_activity' );
     193                $this->total_member_count = groups_get_groupmeta( $this->id, 'total_member_count' );
     194                $this->is_member          = BP_Groups_Member::check_is_member( bp_loggedin_user_id(), $this->id );
     195                $this->is_invited         = BP_Groups_Member::check_has_invite( bp_loggedin_user_id(), $this->id );
     196                $this->is_pending         = BP_Groups_Member::check_for_membership_request( bp_loggedin_user_id(), $this->id );
     197
     198                // If this is a private or hidden group, does the current user have access?
     199                if ( 'private' == $this->status || 'hidden' == $this->status ) {
     200                    if ( $this->is_member && is_user_logged_in() || bp_current_user_can( 'bp_moderate' ) )
     201                        $this->user_has_access = true;
     202                    else
     203                        $this->user_has_access = false;
     204                } else {
    187205                    $this->user_has_access = true;
    188                 else
    189                     $this->user_has_access = false;
    190             } else {
    191                 $this->user_has_access = true;
    192             }
    193 
    194             // Get group admins and mods
    195             $admin_mods = $wpdb->get_results( apply_filters( 'bp_group_admin_mods_user_join_filter', $wpdb->prepare( "SELECT u.ID as user_id, u.user_login, u.user_email, u.user_nicename, m.is_admin, m.is_mod FROM {$wpdb->users} u, {$bp->groups->table_name_members} m WHERE u.ID = m.user_id AND m.group_id = %d AND ( m.is_admin = 1 OR m.is_mod = 1 )", $this->id ) ) );
    196             foreach( (array) $admin_mods as $user ) {
    197                 if ( (int) $user->is_admin )
    198                     $this->admins[] = $user;
    199                 else
    200                     $this->mods[] = $user;
     206                }
     207
     208                // Get group admins and mods
     209                $admin_mods = $wpdb->get_results( apply_filters( 'bp_group_admin_mods_user_join_filter', $wpdb->prepare( "SELECT u.ID as user_id, u.user_login, u.user_email, u.user_nicename, m.is_admin, m.is_mod FROM {$wpdb->users} u, {$bp->groups->table_name_members} m WHERE u.ID = m.user_id AND m.group_id = %d AND ( m.is_admin = 1 OR m.is_mod = 1 )", $this->id ) ) );
     210                foreach( (array) $admin_mods as $user ) {
     211                    if ( (int) $user->is_admin )
     212                        $this->admins[] = $user;
     213                    else
     214                        $this->mods[] = $user;
     215                }
    201216            }
    202217        } else {
  • trunk/bp-groups/bp-groups-functions.php

    r7897 r7898  
    4242function groups_get_group( $args = '' ) {
    4343    $defaults = array(
    44         'group_id'   => false,
    45         'load_users' => false
     44        'group_id'          => false,
     45        'load_users'        => false,
     46        'populate_extras'   => true,
     47        'update_meta_cache' => true,
    4648    );
    4749
     
    5254
    5355    if ( !$group = wp_cache_get( $cache_key, 'bp' ) ) {
    54         $group = new BP_Groups_Group( $group_id, true, $load_users );
     56        $group_args = array(
     57            'populate_extras'   => $populate_extras,
     58            'update_meta_cache' => $update_meta_cache,
     59        );
     60
     61        $group = new BP_Groups_Group( $group_id, $group_args );
    5562        wp_cache_set( $cache_key, $group, 'bp' );
    5663    }
Note: See TracChangeset for help on using the changeset viewer.