Skip to:
Content

BuddyPress.org

Changeset 11086


Ignore:
Timestamp:
09/13/2016 02:59:45 AM (5 years ago)
Author:
boonebgorges
Message:

Groups: Prime group administrator cache in group loops.

This saves a database query for each group of which the logged-in user
is a member.

Props r-a-y.
See #5451.

Location:
trunk/src/bp-groups
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/bp-groups-functions.php

    r11046 r11086  
    685685 * @since 1.2.0
    686686 * @since 2.6.0 Added `$group_type`, `$group_type__in`, and `$group_type__not_in` parameters.
     687 * @since 2.7.0 Added `$update_admin_cache` parameter.
    687688 *
    688689 * @param array|string $args {
     
    714715        'populate_extras'    => true,           // Fetch meta such as is_banned and is_member.
    715716        'update_meta_cache'  => true,           // Pre-fetch groupmeta for queried groups.
     717        'update_admin_cache' => false,
    716718    );
    717719
     
    733735        'populate_extras'    => $r['populate_extras'],
    734736        'update_meta_cache'  => $r['update_meta_cache'],
     737        'update_admin_cache' => $r['update_admin_cache'],
    735738        'order'              => $r['order'],
    736739        'orderby'            => $r['orderby'],
  • trunk/src/bp-groups/bp-groups-template.php

    r11035 r11086  
    104104 * @since 1.0.0
    105105 * @since 2.6.0 Added `$group_type`, `$group_type__in`, and `$group_type__not_in` parameters.
     106 * @since 2.7.0 Added `$update_admin_cache` parameter.
    106107 *
    107108 * @param array|string $args {
     
    139140 *                                            the listed groups. Default: false.
    140141 *     @type bool         $update_meta_cache  Whether to fetch groupmeta for queried groups. Default: true.
     142 *     @type bool         $update_admin_cache Whether to pre-fetch group admins for queried groups.
     143 *                                            Defaults to true when on a group directory, where this
     144 *                                            information is needed in the loop. Otherwise false.
    141145 * }
    142146 * @return bool True if there are groups to display that match the params
     
    201205        'populate_extras'    => true,
    202206        'update_meta_cache'  => true,
     207        'update_admin_cache' => bp_is_groups_directory() || bp_is_user_groups(),
    203208    ), 'has_groups' );
    204209
     
    224229        'populate_extras'    => (bool) $r['populate_extras'],
    225230        'update_meta_cache'  => (bool) $r['update_meta_cache'],
     231        'update_admin_cache' => (bool) $r['update_admin_cache'],
    226232    ) );
    227233
  • trunk/src/bp-groups/classes/class-bp-groups-group.php

    r11072 r11086  
    729729     * @since 1.6.0
    730730     * @since 2.6.0 Added `$group_type`, `$group_type__in`, and `$group_type__not_in` parameters.
     731     * @since 2.7.0 Added `$update_admin_cache` parameter.
    731732     *
    732733     * @param array $args {
     
    761762     *     @type bool         $update_meta_cache  Whether to pre-fetch groupmeta for the returned groups.
    762763     *                                            Default: true.
     764     *     @type bool         $update_admin_cache Whether to pre-fetch administrator IDs for the returned
     765     *                                            groups. Default: false.
    763766     *     @type bool         $show_hidden        Whether to include hidden groups in results. Default: false.
    764767     * }
     
    808811            'populate_extras'    => true,
    809812            'update_meta_cache'  => true,
     813            'update_admin_cache' => false,
    810814            'exclude'            => false,
    811815            'show_hidden'        => false,
     
    10221026        if ( ! empty( $r['update_meta_cache'] ) ) {
    10231027            bp_groups_update_meta_cache( $group_ids );
     1028        }
     1029
     1030        // Prefetch all administrator IDs, if requested.
     1031        if ( $r['update_admin_cache'] ) {
     1032            BP_Groups_Member::prime_group_administrator_ids_cache( $group_ids );
    10241033        }
    10251034
  • trunk/src/bp-groups/classes/class-bp-groups-member.php

    r11032 r11086  
    10441044
    10451045        if ( false === $group_admins ) {
    1046             $bp = buddypress();
    1047             $group_admins = $wpdb->get_results( $wpdb->prepare( "SELECT user_id, date_modified FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_admin = 1 AND is_banned = 0", $group_id ) );
    1048 
    1049             wp_cache_set( $group_id, $group_admins, 'bp_group_admins' );
     1046            self::prime_group_administrator_ids_cache( array( $group_id ) );
     1047            $group_admins = wp_cache_get( $group_id, 'bp_group_admins' );
    10501048        }
    10511049
     
    10561054
    10571055        return $group_admins;
     1056    }
     1057
     1058    /**
     1059     * Prime the bp_group_admins cache for one or more groups.
     1060     *
     1061     * @since 2.7.0
     1062     *
     1063     * @param array $group_ids IDs of the groups.
     1064     * @return bool True on success.
     1065     */
     1066    public static function prime_group_administrator_ids_cache( $group_ids ) {
     1067        global $wpdb;
     1068
     1069        $uncached = bp_get_non_cached_ids( $group_ids, 'bp_group_admins' );
     1070
     1071        if ( $uncached ) {
     1072            $bp = buddypress();
     1073            $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' );
     1087                }
     1088            }
     1089        }
    10581090    }
    10591091
  • trunk/src/bp-groups/classes/class-bp-groups-template.php

    r10767 r11086  
    173173            'populate_extras'    => true,
    174174            'update_meta_cache'  => true,
     175            'update_admin_cache' => false,
    175176        );
    176177
     
    229230                'populate_extras'    => $populate_extras,
    230231                'update_meta_cache'  => $update_meta_cache,
     232                'update_admin_cache' => $update_admin_cache,
    231233                'show_hidden'        => $show_hidden,
    232234            ) );
Note: See TracChangeset for help on using the changeset viewer.