Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/13/2016 05:35:42 AM (8 years ago)
Author:
boonebgorges
Message:

Groups: Remove 'populate_extras' flag from BP_Groups_Group.

populate_extras was originally designed to reduce the number of
additional queries performed when setting up a group, by making those
queries optional in cases where the additional data was not needed.
Now, all of these properties are adequately cached, and lazy-loaded
so that they're only fetched when requested.

See #5451.

File:
1 edited

Legend:

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

    r11087 r11089  
    114114     * @var bool
    115115     */
    116     public $is_member;
     116    protected $is_member;
    117117
    118118    /**
     
    122122     * @var bool
    123123     */
    124     public $is_invited;
     124    protected $is_invited;
    125125
    126126    /**
     
    130130     * @var bool
    131131     */
    132     public $is_pending;
     132    protected $is_pending;
    133133
    134134    /**
     
    146146     * @var bool
    147147     */
    148     public $user_has_access;
     148    protected $user_has_access;
    149149
    150150    /**
     
    165165     * @param array    $args {
    166166     *     Array of optional arguments.
    167      *     @type bool $populate_extras Whether to fetch "extra" data about the group
    168      *                                 (group admins/mods, access for the current user).
    169      *                                 Default: false.
     167     *     @type bool $populate_extras Deprecated.
    170168     * }
    171169     */
    172170    public function __construct( $id = null, $args = array() ) {
    173         $this->args = wp_parse_args( $args, array(
    174             'populate_extras' => false,
    175         ) );
    176 
    177171        if ( !empty( $id ) ) {
    178172            $this->id = (int) $id;
     
    217211        $this->enable_forum = (int) $group->enable_forum;
    218212        $this->date_created = $group->date_created;
    219 
    220         // Are we getting extra group data?
    221         if ( ! empty( $this->args['populate_extras'] ) ) {
    222 
    223             // Set user-specific data.
    224             $user_id          = bp_loggedin_user_id();
    225             $this->is_member  = groups_is_user_member( $user_id, $this->id );
    226             $this->is_invited = groups_check_user_has_invite( $user_id, $this->id );
    227             $this->is_pending = groups_check_for_membership_request( $user_id, $this->id );
    228 
    229             // If this is a private or hidden group, does the current user have access?
    230             if ( ( 'private' === $this->status ) || ( 'hidden' === $this->status ) ) {
    231 
    232                 // Assume user does not have access to hidden/private groups.
    233                 $this->user_has_access = false;
    234 
    235                 // Group members or community moderators have access.
    236                 if ( ( $this->is_member && is_user_logged_in() ) || bp_current_user_can( 'bp_moderate' ) ) {
    237                     $this->user_has_access = true;
    238                 }
    239             } else {
    240                 $this->user_has_access = true;
    241             }
    242         }
    243213    }
    244214
     
    425395                return $this->get_mods();
    426396
     397            case 'is_member' :
     398                return $this->get_is_member();
     399
     400            case 'is_invited' :
     401                return groups_check_user_has_invite( bp_loggedin_user_id(), $this->id );
     402
     403            case 'is_pending' :
     404                return groups_check_for_membership_request( bp_loggedin_user_id(), $this->id );
     405
     406            case 'user_has_access' :
     407                return $this->get_user_has_access();
     408
    427409            default :
    428410            break;
     
    445427            case 'last_activity' :
    446428            case 'total_member_count' :
     429            case 'admins' :
     430            case 'mods' :
    447431                return true;
    448432
    449433            default :
    450434                return false;
     435        }
     436    }
     437
     438    /**
     439     * Magic setter.
     440     *
     441     * Used to maintain backward compatibility for properties that are now
     442     * accessible only via magic method.
     443     *
     444     * @since 2.7.0
     445     *
     446     * @param string $key Property name.
     447     */
     448    public function __set( $key, $value ) {
     449        switch ( $key ) {
     450            case 'user_has_access' :
     451                return $this->user_has_access = (bool) $value;
    451452        }
    452453    }
     
    529530        $this->admins = $admin_objects;
    530531        $this->mods   = $mod_objects;
     532    }
     533
     534    /**
     535     * Checks whether the logged-in user is a member of the group.
     536     *
     537     * @since 2.7.0
     538     *
     539     * @return bool
     540     */
     541    protected function get_is_member() {
     542        if ( isset( $this->is_member ) ) {
     543            return $this->is_member;
     544        }
     545
     546        $this->is_member = groups_is_user_member( bp_loggedin_user_id(), $this->id );
     547        return $this->is_member;
     548    }
     549
     550    /**
     551     * Checks whether the logged-in user has access to the group.
     552     *
     553     * @since 2.7.0
     554     *
     555     * @return bool
     556     */
     557    protected function get_user_has_access() {
     558        if ( isset( $this->user_has_access ) ) {
     559            return $this->user_has_access;
     560        }
     561
     562        if ( ( 'private' === $this->status ) || ( 'hidden' === $this->status ) ) {
     563
     564            // Assume user does not have access to hidden/private groups.
     565            $this->user_has_access = false;
     566
     567            // Group members or community moderators have access.
     568            if ( ( is_user_logged_in() && $this->get_is_member() ) || bp_current_user_can( 'bp_moderate' ) ) {
     569                $this->user_has_access = true;
     570            }
     571        } else {
     572            $this->user_has_access = true;
     573        }
     574
     575        return $this->user_has_access;
    531576    }
    532577
Note: See TracChangeset for help on using the changeset viewer.