Ticket #7609: 7609.01.diff
File 7609.01.diff, 6.5 KB (added by , 7 years ago) |
---|
-
src/bp-groups/bp-groups-functions.php
diff --git src/bp-groups/bp-groups-functions.php src/bp-groups/bp-groups-functions.php index 70dbdf1..b856f2f 100644
function groups_get_groups( $args = '' ) { 785 785 'page' => 1, // The page to return if limiting per page. 786 786 'update_meta_cache' => true, // Pre-fetch groupmeta for queried groups. 787 787 'update_admin_cache' => false, 788 'ids_only' => false, // Return group IDs only instead of BP_Groups_Group objects. 788 789 ); 789 790 790 791 $r = bp_parse_args( $args, $defaults, 'groups_get_groups' ); … … function groups_get_groups( $args = '' ) { 810 811 'update_admin_cache' => $r['update_admin_cache'], 811 812 'order' => $r['order'], 812 813 'orderby' => $r['orderby'], 814 'ids_only' => $r['ids_only'], 813 815 ) ); 814 816 815 817 /** -
src/bp-groups/classes/class-bp-groups-group.php
diff --git src/bp-groups/classes/class-bp-groups-group.php src/bp-groups/classes/class-bp-groups-group.php index 1c243bd..e78f099 100644
class BP_Groups_Group { 1025 1025 * @type array|string $status Optional. Array or comma-separated list of group statuses to limit 1026 1026 * results to. If specified, $show_hidden is ignored. 1027 1027 * Default: empty array. 1028 * @type bool $ids_only Return only group IDs in results, not group objects. Default: false. 1029 * If true, meta and admin caches will not be prefetched. 1028 1030 * } 1029 1031 * @return array { 1030 1032 * @type array $groups Array of group objects returned by the 1031 * paginated query. 1033 * paginated query. (IDs only is `ids_only` is true.) 1032 1034 * @type int $total Total count of all groups matching non- 1033 1035 * paginated query params. 1034 1036 * } … … class BP_Groups_Group { 1075 1077 'update_admin_cache' => false, 1076 1078 'exclude' => false, 1077 1079 'show_hidden' => false, 1078 'status' => array() 1080 'status' => array(), 1081 'ids_only' => false, 1079 1082 ); 1080 1083 1081 1084 $r = wp_parse_args( $args, $defaults ); … … class BP_Groups_Group { 1299 1302 $paged_group_ids = $cached; 1300 1303 } 1301 1304 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' ); 1305 if ( $r['ids_only'] ) { 1306 // We only want the IDs. 1307 $paged_groups = array_map( 'absint', $paged_group_ids ); 1308 } else { 1309 $uncached_group_ids = bp_get_non_cached_ids( $paged_group_ids, 'bp_groups' ); 1310 if ( $uncached_group_ids ) { 1311 $group_ids_sql = implode( ',', array_map( 'intval', $uncached_group_ids ) ); 1312 $group_data_objects = $wpdb->get_results( "SELECT g.* FROM {$bp->groups->table_name} g WHERE g.id IN ({$group_ids_sql})" ); 1313 foreach ( $group_data_objects as $group_data_object ) { 1314 wp_cache_set( $group_data_object->id, $group_data_object, 'bp_groups' ); 1315 } 1316 } 1317 1318 $paged_groups = array(); 1319 foreach ( $paged_group_ids as $paged_group_id ) { 1320 $paged_groups[] = new BP_Groups_Group( $paged_group_id ); 1321 } 1322 1323 $group_ids = array(); 1324 foreach ( (array) $paged_groups as $group ) { 1325 $group_ids[] = $group->id; 1326 } 1327 1328 // Grab all groupmeta. 1329 if ( ! empty( $r['update_meta_cache'] ) ) { 1330 bp_groups_update_meta_cache( $group_ids ); 1331 } 1332 1333 // Prefetch all administrator IDs, if requested. 1334 if ( $r['update_admin_cache'] ) { 1335 BP_Groups_Member::prime_group_admins_mods_cache( $group_ids ); 1336 } 1337 1338 // Set up integer properties needing casting. 1339 $int_props = array( 1340 'id', 'creator_id', 'enable_forum' 1341 ); 1342 1343 // Integer casting. 1344 foreach ( $paged_groups as $key => $g ) { 1345 foreach ( $int_props as $int_prop ) { 1346 $paged_groups[ $key ]->{$int_prop} = (int) $paged_groups[ $key ]->{$int_prop}; 1347 } 1308 1348 } 1309 }1310 1349 1311 $paged_groups = array();1312 foreach ( $paged_group_ids as $paged_group_id ) {1313 $paged_groups[] = new BP_Groups_Group( $paged_group_id );1314 1350 } 1315 1351 1352 // Find the total number of groups in the results set. 1316 1353 $total_groups_sql = "SELECT COUNT(DISTINCT g.id) FROM {$sql['from']} $where"; 1317 1354 1318 1355 /** … … class BP_Groups_Group { 1334 1371 $total_groups = (int) $cached; 1335 1372 } 1336 1373 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 );1365 1366 1374 return array( 'groups' => $paged_groups, 'total' => $total_groups ); 1367 1375 } 1368 1376 -
tests/phpunit/testcases/groups/class-bp-groups-group.php
diff --git tests/phpunit/testcases/groups/class-bp-groups-group.php tests/phpunit/testcases/groups/class-bp-groups-group.php index b1d3244..e59ed27 100644
class BP_Tests_BP_Groups_Group_TestCases extends BP_UnitTestCase { 2234 2234 $this->assertEqualSets( array( $g1, $g3 ), $found ); 2235 2235 } 2236 2236 2237 /** 2238 * @group get_ids_only 2239 */ 2240 public function test_get_return_ids_only() { 2241 $now = time(); 2242 $g1 = $this->factory->group->create( array( 2243 'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60 ), 2244 ) ); 2245 $g2 = $this->factory->group->create( array( 2246 'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60*2 ), 2247 ) ); 2248 $g3 = $this->factory->group->create( array( 2249 'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60*3 ), 2250 ) ); 2251 2252 $groups = BP_Groups_Group::get( array( 2253 'ids_only' => true, 2254 ) ); 2255 2256 $this->assertSame( array( $g1, $g2, $g3 ), $groups['groups'] ); 2257 } 2258 2237 2259 } 2238 2260 2239 2261 /**