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/src/bp-groups/classes/class-bp-groups-member.php

    r11086 r11087  
    10441044
    10451045        if ( false === $group_admins ) {
    1046             self::prime_group_administrator_ids_cache( array( $group_id ) );
     1046            self::prime_group_admins_mods_cache( array( $group_id ) );
    10471047            $group_admins = wp_cache_get( $group_id, 'bp_group_admins' );
    10481048        }
     
    10641064     * @return bool True on success.
    10651065     */
    1066     public static function prime_group_administrator_ids_cache( $group_ids ) {
     1066    public static function prime_group_admins_mods_cache( $group_ids ) {
    10671067        global $wpdb;
    10681068
     
    10721072            $bp = buddypress();
    10731073            $uncached_sql = implode( ',', array_map( 'intval', $uncached ) );
    1074             $group_admins = $wpdb->get_results( "SELECT user_id, group_id, date_modified FROM {$bp->groups->table_name_members} WHERE group_id IN ({$uncached_sql}) AND is_admin = 1 AND is_banned = 0" );
    1075 
    1076             $groups = array();
    1077             if ( $group_admins ) {
    1078                 foreach ( $group_admins as $group_admin ) {
    1079                     $admin_obj = new stdClass();
    1080                     $admin_obj->user_id = $group_admin->user_id;
    1081                     $admin_obj->date_modified = $group_admin->date_modified;
    1082                     $groups[ $group_admin->group_id ][] = $admin_obj;
    1083                 }
    1084 
    1085                 foreach ( $groups as $this_group_id => $this_group_admins ) {
    1086                     wp_cache_set( $this_group_id, $this_group_admins, 'bp_group_admins' );
     1074            $group_admin_mods = $wpdb->get_results( "SELECT user_id, group_id, date_modified, is_admin, is_mod FROM {$bp->groups->table_name_members} WHERE group_id IN ({$uncached_sql}) AND ( is_admin = 1 OR is_mod = 1 ) AND is_banned = 0" );
     1075
     1076            $admins = $mods = array();
     1077            if ( $group_admin_mods ) {
     1078                foreach ( $group_admin_mods as $group_admin_mod ) {
     1079                    $obj = new stdClass();
     1080                    $obj->user_id = $group_admin_mod->user_id;
     1081                    $obj->date_modified = $group_admin_mod->date_modified;
     1082
     1083                    if ( $group_admin_mod->is_admin ) {
     1084                        $admins[ $group_admin_mod->group_id ][] = $obj;
     1085                    } else {
     1086                        $mods[ $group_admin_mod->group_id ][] = $obj;
     1087                    }
    10871088                }
    10881089            }
     1090
     1091            // Prime cache for all groups, even those with no matches.
     1092            foreach ( $uncached as $group_id ) {
     1093                $group_admins = isset( $admins[ $group_id ] ) ? $admins[ $group_id ] : array();
     1094                wp_cache_set( $group_id, $group_admins, 'bp_group_admins' );
     1095
     1096                $group_mods = isset( $mods[ $group_id ] ) ? $mods[ $group_id ] : array();
     1097                wp_cache_set( $group_id, $group_mods, 'bp_group_mods' );
     1098            }
    10891099        }
    10901100    }
     
    11011111        global $wpdb;
    11021112
    1103         $bp = buddypress();
    1104 
    1105         $group_mods = $wpdb->get_results( $wpdb->prepare( "SELECT user_id, date_modified FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_mod = 1 AND is_banned = 0", $group_id ) );
     1113        $group_mods = wp_cache_get( $group_id, 'bp_group_mods' );
     1114
     1115        if ( false === $group_mods ) {
     1116            self::prime_group_admins_mods_cache( array( $group_id ) );
     1117            $group_mods = wp_cache_get( $group_id, 'bp_group_mods' );
     1118        }
    11061119
    11071120        // Integer casting.
Note: See TracChangeset for help on using the changeset viewer.