Skip to:
Content

BuddyPress.org

Changeset 11089


Ignore:
Timestamp:
09/13/2016 05:35:42 AM (10 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.

Location:
trunk
Files:
4 edited

Legend:

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

    r11080 r11089  
    252252                        } else {
    253253                                $this->current_group->is_visible = false;
    254                         }
    255 
    256                         // If this is a private or hidden group, does the user have access?
    257                         if ( 'private' == $this->current_group->status || 'hidden' == $this->current_group->status ) {
    258                                 if ( $this->current_group->is_user_member && is_user_logged_in() || bp_current_user_can( 'bp_moderate' ) ) {
    259                                         $this->current_group->user_has_access = true;
    260                                 } else {
    261                                         $this->current_group->user_has_access = false;
    262                                 }
    263                         } else {
    264                                 $this->current_group->user_has_access = true;
    265254                        }
    266255
  • 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
  • trunk/tests/phpunit/testcases/groups/class-bp-groups-group.php

    r11087 r11089  
    13001300
    13011301        /**
     1302         * @ticket BP5451
     1303         */
     1304        public function test_is_member_property() {
     1305                $users = $this->factory->user->create_many( 2 );
     1306
     1307                $g = $this->factory->group->create( array(
     1308                        'creator_id' => $users[0],
     1309                ) );
     1310
     1311                wp_set_current_user( $users[1] );
     1312
     1313                $group_a = new BP_Groups_Group( $g );
     1314                $this->assertFalse( $group_a->is_member );
     1315
     1316                $this->add_user_to_group( $users[1], $g );
     1317                $group_b = new BP_Groups_Group( $g );
     1318                $this->assertFalse( $group_b->is_member );
     1319        }
     1320
     1321        /**
     1322         * @ticket BP5451
     1323         */
     1324        public function test_is_invited_property() {
     1325                $users = $this->factory->user->create_many( 2 );
     1326
     1327                $g = $this->factory->group->create( array(
     1328                        'creator_id' => $users[0],
     1329                ) );
     1330
     1331                wp_set_current_user( $users[1] );
     1332
     1333                $group_a = new BP_Groups_Group( $g );
     1334                $this->assertFalse( $group_a->is_invited );
     1335
     1336                $this->add_user_to_group( $users[1], $g, array(
     1337                        'invite_sent' => 1,
     1338                        'inviter_id' => $users[0],
     1339                        'is_confirmed' => 0,
     1340                ) );
     1341                $group_b = new BP_Groups_Group( $g );
     1342                $this->assertFalse( $group_b->is_invited );
     1343        }
     1344
     1345        /**
     1346         * @ticket BP5451
     1347         */
     1348        public function test_is_pending_property() {
     1349                $users = $this->factory->user->create_many( 2 );
     1350
     1351                $g = $this->factory->group->create( array(
     1352                        'creator_id' => $users[0],
     1353                ) );
     1354
     1355                wp_set_current_user( $users[1] );
     1356
     1357                $group_a = new BP_Groups_Group( $g );
     1358                $this->assertFalse( $group_a->is_pending );
     1359
     1360                $this->add_user_to_group( $users[1], $g, array(
     1361                        'is_confirmed' => 0,
     1362                        'invite_sent' => 0,
     1363                        'inviter_id' => 0,
     1364                ) );
     1365                $group_b = new BP_Groups_Group( $g );
     1366                $this->assertFalse( $group_b->is_pending );
     1367        }
     1368
     1369
     1370        /**
    13021371         * @group group_types
    13031372         */
  • trunk/tests/phpunit/testcases/groups/functions.php

    r9819 r11089  
    614614
    615615        /**
    616          * @group groups_get_group
    617          * @group cache
    618          */
    619         public function test_groups_get_group_cache_different_users() {
    620                 $g = $this->factory->group->create();
    621                 $u1 = $this->factory->user->create();
    622                 $u2 = $this->factory->user->create();
    623                 $this->add_user_to_group( $u1, $g );
    624 
    625                 $old_user = get_current_user_id();
    626                 $this->set_current_user( $u1 );
    627 
    628                 $group1 = groups_get_group( array( 'group_id' => $g, 'populate_extras' => true ) );
    629 
    630                 $this->set_current_user( $u2 );
    631 
    632                 $group2 = groups_get_group( array( 'group_id' => $g, 'populate_extras' => true ) );
    633 
    634                 $this->assertNotEquals( $group1, $group2 );
    635 
    636                 $this->set_current_user( $old_user );
    637         }
    638 
    639         /**
    640616         * @group counts
    641617         */
Note: See TracChangeset for help on using the changeset viewer.