Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
12/06/2017 04:18:11 PM (7 years ago)
Author:
djpaul
Message:

Groups: add 'fields' parameter to BP_Groups_Group::get().

Allows return of only group IDs instead of entire objects when fields=ids is set, similiar to the fields parameter in WP_Query.

Fixes #7609

Props dcavins

File:
1 edited

Legend:

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

    r11703 r11762  
    10261026     *                                            results to. If specified, $show_hidden is ignored.
    10271027     *                                            Default: empty array.
     1028     *     @type string       $fields             Which fields to return. Specify 'ids' to fetch a list of IDs.
     1029     *                                            Default: 'all' (return BP_Groups_Group objects).
     1030     *                                            If set, meta and admin caches will not be prefetched.
    10281031     * }
    10291032     * @return array {
    10301033     *     @type array $groups Array of group objects returned by the
    1031      *                         paginated query.
     1034     *                         paginated query. (IDs only if `fields` is set to `ids`.)
    10321035     *     @type int   $total  Total count of all groups matching non-
    10331036     *                         paginated query params.
     
    10761079            'exclude'            => false,
    10771080            'show_hidden'        => false,
    1078             'status'             => array()
     1081            'status'             => array(),
     1082            'fields'             => 'all',
    10791083        );
    10801084
     
    13001304        }
    13011305
    1302         $uncached_group_ids = bp_get_non_cached_ids( $paged_group_ids, 'bp_groups' );
    1303         if ( $uncached_group_ids ) {
    1304             $group_ids_sql = implode( ',', array_map( 'intval', $uncached_group_ids ) );
    1305             $group_data_objects = $wpdb->get_results( "SELECT g.* FROM {$bp->groups->table_name} g WHERE g.id IN ({$group_ids_sql})" );
    1306             foreach ( $group_data_objects as $group_data_object ) {
    1307                 wp_cache_set( $group_data_object->id, $group_data_object, 'bp_groups' );
     1306        if ( 'ids' === $r['fields'] ) {
     1307            // We only want the IDs.
     1308            $paged_groups = array_map( 'intval', $paged_group_ids );
     1309        } else {
     1310            $uncached_group_ids = bp_get_non_cached_ids( $paged_group_ids, 'bp_groups' );
     1311            if ( $uncached_group_ids ) {
     1312                $group_ids_sql = implode( ',', array_map( 'intval', $uncached_group_ids ) );
     1313                $group_data_objects = $wpdb->get_results( "SELECT g.* FROM {$bp->groups->table_name} g WHERE g.id IN ({$group_ids_sql})" );
     1314                foreach ( $group_data_objects as $group_data_object ) {
     1315                    wp_cache_set( $group_data_object->id, $group_data_object, 'bp_groups' );
     1316                }
    13081317            }
    1309         }
    1310 
    1311         $paged_groups = array();
    1312         foreach ( $paged_group_ids as $paged_group_id ) {
    1313             $paged_groups[] = new BP_Groups_Group( $paged_group_id );
    1314         }
    1315 
     1318
     1319            $paged_groups = array();
     1320            foreach ( $paged_group_ids as $paged_group_id ) {
     1321                $paged_groups[] = new BP_Groups_Group( $paged_group_id );
     1322            }
     1323
     1324            $group_ids = array();
     1325            foreach ( (array) $paged_groups as $group ) {
     1326                $group_ids[] = $group->id;
     1327            }
     1328
     1329            // Grab all groupmeta.
     1330            if ( ! empty( $r['update_meta_cache'] ) ) {
     1331                bp_groups_update_meta_cache( $group_ids );
     1332            }
     1333
     1334            // Prefetch all administrator IDs, if requested.
     1335            if ( $r['update_admin_cache'] ) {
     1336                BP_Groups_Member::prime_group_admins_mods_cache( $group_ids );
     1337            }
     1338
     1339            // Set up integer properties needing casting.
     1340            $int_props = array(
     1341                'id', 'creator_id', 'enable_forum'
     1342            );
     1343
     1344            // Integer casting.
     1345            foreach ( $paged_groups as $key => $g ) {
     1346                foreach ( $int_props as $int_prop ) {
     1347                    $paged_groups[ $key ]->{$int_prop} = (int) $paged_groups[ $key ]->{$int_prop};
     1348                }
     1349            }
     1350
     1351        }
     1352
     1353        // Find the total number of groups in the results set.
    13161354        $total_groups_sql = "SELECT COUNT(DISTINCT g.id) FROM {$sql['from']} $where";
    13171355
     
    13341372            $total_groups = (int) $cached;
    13351373        }
    1336 
    1337         $group_ids = array();
    1338         foreach ( (array) $paged_groups as $group ) {
    1339             $group_ids[] = $group->id;
    1340         }
    1341 
    1342         // Grab all groupmeta.
    1343         if ( ! empty( $r['update_meta_cache'] ) ) {
    1344             bp_groups_update_meta_cache( $group_ids );
    1345         }
    1346 
    1347         // Prefetch all administrator IDs, if requested.
    1348         if ( $r['update_admin_cache'] ) {
    1349             BP_Groups_Member::prime_group_admins_mods_cache( $group_ids );
    1350         }
    1351 
    1352         // Set up integer properties needing casting.
    1353         $int_props = array(
    1354             'id', 'creator_id', 'enable_forum'
    1355         );
    1356 
    1357         // Integer casting.
    1358         foreach ( $paged_groups as $key => $g ) {
    1359             foreach ( $int_props as $int_prop ) {
    1360                 $paged_groups[ $key ]->{$int_prop} = (int) $paged_groups[ $key ]->{$int_prop};
    1361             }
    1362         }
    1363 
    1364         unset( $sql, $total_sql );
    13651374
    13661375        return array( 'groups' => $paged_groups, 'total' => $total_groups );
Note: See TracChangeset for help on using the changeset viewer.