Ticket #7609: 7609.02.diff
File 7609.02.diff, 6.6 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..638cbac 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 'fields' => 'all', // Return BP_Groups_Group objects or a list of ids. 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 'fields' => $r['fields'], 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..ec64342 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 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. 1028 1031 * } 1029 1032 * @return array { 1030 1033 * @type array $groups Array of group objects returned by the 1031 * paginated query. 1034 * paginated query. (IDs only if `fields` is set to `ids`.) 1032 1035 * @type int $total Total count of all groups matching non- 1033 1036 * paginated query params. 1034 1037 * } … … class BP_Groups_Group { 1075 1078 'update_admin_cache' => false, 1076 1079 'exclude' => false, 1077 1080 'show_hidden' => false, 1078 'status' => array() 1081 'status' => array(), 1082 'fields' => 'all', 1079 1083 ); 1080 1084 1081 1085 $r = wp_parse_args( $args, $defaults ); … … class BP_Groups_Group { 1299 1303 $paged_group_ids = $cached; 1300 1304 } 1301 1305 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 } 1317 } 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 } 1308 1349 } 1309 }1310 1350 1311 $paged_groups = array();1312 foreach ( $paged_group_ids as $paged_group_id ) {1313 $paged_groups[] = new BP_Groups_Group( $paged_group_id );1314 1351 } 1315 1352 1353 // Find the total number of groups in the results set. 1316 1354 $total_groups_sql = "SELECT COUNT(DISTINCT g.id) FROM {$sql['from']} $where"; 1317 1355 1318 1356 /** … … class BP_Groups_Group { 1334 1372 $total_groups = (int) $cached; 1335 1373 } 1336 1374 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 1375 return array( 'groups' => $paged_groups, 'total' => $total_groups ); 1367 1376 } 1368 1377 -
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..1695bcc 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 'fields' => 'ids', 2254 ) ); 2255 2256 $this->assertSame( array( $g1, $g2, $g3 ), $groups['groups'] ); 2257 } 2258 2237 2259 } 2238 2260 2239 2261 /**