Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/13/2016 04:05:07 AM (10 years ago)
Author:
boonebgorges
Message:

Groups: Improve query efficiency for 'admins' and 'mods' properties of group objects.

Previously, the 'admins' and 'mods' property of BP_Groups_Group
objects were only populated when setting the 'populate_extras' flag.
Even then, the query used to populate these properties was uncached,
and required a join against a global table.

This changeset reworks the way that the 'admins' and 'mods' properties
are accessed and set. The properties are now marked protected, and
are accessible by magic __get(). When requested, the cache for the
both properties is set by a single pair of queries: one to fetch
membership data from the BP table, and one to get user objects from
WordPress. The BP table query is cached, and neither query takes place
if the property is never accessed.

This moves us a step closer to eliminating the populate_extras flag
on BP_Groups_Group objects.

See #5451.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/groups/class-bp-groups-group.php

    r11074 r11087  
    12431243
    12441244    /**
     1245     * @ticket BP5451
     1246     */
     1247    public function test_admins_property() {
     1248        $user_1 = $this->factory->user->create_and_get();
     1249        $g = $this->factory->group->create( array(
     1250            'creator_id' => $user_1->ID,
     1251        ) );
     1252
     1253        $group = new BP_Groups_Group( $g );
     1254
     1255        $expected_admin_props = array(
     1256            'user_id' => $user_1->ID,
     1257            'user_login' => $user_1->user_login,
     1258            'user_email' => $user_1->user_email,
     1259            'user_nicename' => $user_1->user_nicename,
     1260            'is_admin' => 1,
     1261            'is_mod' => 0,
     1262        );
     1263
     1264        $found_admin = $group->admins[0];
     1265        foreach ( $expected_admin_props as $prop => $value ) {
     1266            $this->assertEquals( $value, $found_admin->{$prop} );
     1267        }
     1268    }
     1269
     1270    /**
     1271     * @ticket BP5451
     1272     */
     1273    public function test_mods_property() {
     1274        $users = $this->factory->user->create_many( 2 );
     1275        $user_1 = new WP_User( $users[0] );
     1276        $user_2 = new WP_User( $users[1] );
     1277
     1278        $g = $this->factory->group->create( array(
     1279            'creator_id' => $user_1->ID,
     1280        ) );
     1281
     1282        $this->add_user_to_group( $user_2->ID, $g, array( 'is_mod' => 1 ) );
     1283
     1284        $group = new BP_Groups_Group( $g );
     1285
     1286        $expected_mod_props = array(
     1287            'user_id' => $user_2->ID,
     1288            'user_login' => $user_2->user_login,
     1289            'user_email' => $user_2->user_email,
     1290            'user_nicename' => $user_2->user_nicename,
     1291            'is_admin' => 0,
     1292            'is_mod' => 1,
     1293        );
     1294
     1295        $found_mod = $group->mods[0];
     1296        foreach ( $expected_mod_props as $prop => $value ) {
     1297            $this->assertEquals( $value, $found_mod->{$prop} );
     1298        }
     1299    }
     1300
     1301    /**
    12451302     * @group group_types
    12461303     */
Note: See TracChangeset for help on using the changeset viewer.