Ticket #6327: 6327.3.patch
File 6327.3.patch, 20.2 KB (added by , 9 years ago) |
---|
-
src/bp-groups/bp-groups-cache.php
diff --git src/bp-groups/bp-groups-cache.php src/bp-groups/bp-groups-cache.php index 32d1ade..819fbc4 100644
function groups_clear_group_type_cache( $group_id = 0 ) { 226 226 } 227 227 add_action( 'groups_delete_group', 'groups_clear_group_type_cache' ); 228 228 229 /** 230 * Clear caches on membership save. 231 * 232 * @since 2.6.0 233 */ 234 function bp_groups_clear_user_group_cache_on_membership_save( BP_Groups_Member $member ) { 235 wp_cache_delete( $member->user_id, 'bp_groups_memberships_for_user' ); 236 wp_cache_delete( $member->id, 'bp_groups_memberships' ); 237 } 238 add_action( 'groups_member_before_save', 'bp_groups_clear_user_group_cache_on_membership_save' ); 239 add_action( 'groups_member_before_remove', 'bp_groups_clear_user_group_cache_on_membership_save' ); 240 241 /** 242 * Clear group memberships cache on miscellaneous actions not covered by the 'after_save' hook. 243 * 244 * @since 2.6.0 245 */ 246 function bp_groups_clear_user_group_cache_on_other_events( $user_id, $group_id ) { 247 wp_cache_delete( $user_id, 'bp_groups_memberships_for_user' ); 248 249 $membership = new BP_Groups_Member( $user_id, $group_id ); 250 wp_cache_delete( $membership->id, 'bp_groups_memberships' ); 251 } 252 add_action( 'bp_groups_member_before_delete', 'bp_groups_clear_user_group_cache_on_other_events', 10, 2 ); 253 add_action( 'bp_groups_member_before_delete_invite', 'bp_groups_clear_user_group_cache_on_other_events', 10, 2 ); 254 229 255 /* List actions to clear super cached pages on, if super cache is installed */ 230 256 add_action( 'groups_join_group', 'bp_core_clear_cache' ); 231 257 add_action( 'groups_leave_group', 'bp_core_clear_cache' ); -
src/bp-groups/bp-groups-functions.php
diff --git src/bp-groups/bp-groups-functions.php src/bp-groups/bp-groups-functions.php index 88c82bb..653a3ad 100644
function groups_get_user_groups( $user_id = 0, $pag_num = 0, $pag_page = 0 ) { 791 791 } 792 792 793 793 /** 794 * Get a list of groups of which the specified user is a member. 795 * 796 * @since 2.6.0 797 * 798 * @param int $user_id ID of the user. 799 * @param array $args { 800 * Array of optional args. 801 * @param bool|null $is_confirmed Whether to return only confirmed memberships. Pass `null` to disable this 802 * filter. Default: true. 803 * @param bool|null $is_banned Whether to return only banned memberships. Pass `null` to disable this filter. 804 * Default: false. 805 * @param bool|null $is_admin Whether to return only admin memberships. Pass `null` to disable this filter. 806 * Default: false. 807 * @param bool|null $is_mod Whether to return only mod memberships. Pass `null` to disable this filter. 808 * Default: false. 809 * @param bool|null $invite_sent Whether to return only memberships with 'invite_sent'. Pass `null` to disable 810 * this filter. Default: false. 811 * @param string $orderby Field to order by. Accepts 'id' (membership ID), 'group_id', 'date_modified'. 812 * Default: 'group_id'. 813 * @param string $order Sort order. Accepts 'ASC' or 'DESC'. Default: 'ASC'. 814 * } 815 * @return array Array of matching group memberships, keyed by group ID. 816 */ 817 function bp_get_user_groups( $user_id, $args = array() ) { 818 $r = bp_parse_args( $args, array( 819 'is_confirmed' => true, 820 'is_banned' => false, 821 'is_admin' => false, 822 'is_mod' => false, 823 'invite_sent' => null, 824 'orderby' => 'group_id', 825 'order' => 'ASC', 826 ), 'get_user_groups' ); 827 828 $user_id = intval( $user_id ); 829 830 $membership_ids = wp_cache_get( $user_id, 'bp_groups_memberships_for_user' ); 831 if ( false === $membership_ids ) { 832 $membership_ids = BP_Groups_Member::get_membership_ids_for_user( $user_id ); 833 wp_cache_set( $user_id, $membership_ids, 'bp_groups_memberships_for_user' ); 834 } 835 836 // Prime the membership cache. 837 $uncached_membership_ids = bp_get_non_cached_ids( $membership_ids, 'bp_groups_memberships' ); 838 if ( ! empty( $uncached_membership_ids ) ) { 839 $uncached_memberships = BP_Groups_Member::get_memberships_by_id( $uncached_membership_ids ); 840 841 foreach ( $uncached_memberships as $uncached_membership ) { 842 wp_cache_set( $uncached_membership->id, $uncached_membership, 'bp_groups_memberships' ); 843 } 844 } 845 846 // Populate group membership array from cache. 847 $groups = array(); 848 foreach ( $membership_ids as $membership_id ) { 849 $membership = wp_cache_get( $membership_id, 'bp_groups_memberships' ); 850 851 // Sanity check. 852 if ( ! isset( $membership->group_id ) ) { 853 continue; 854 } 855 856 $group_id = (int) $membership->group_id; 857 858 $groups[ $group_id ] = $membership; 859 } 860 861 // Normalize group data. 862 foreach ( $groups as &$group ) { 863 // Integer values. 864 $group_id = intval( $group->group_id ); 865 foreach ( array( 'id', 'group_id', 'user_id', 'inviter_id' ) as $index ) { 866 $group->{$index} = intval( $group->{$index} ); 867 } 868 869 // Boolean values. 870 foreach ( array( 'is_admin', 'is_mod', 'is_confirmed', 'is_banned', 'invite_sent' ) as $index ) { 871 $group->{$index} = (bool) $group->{$index}; 872 } 873 } 874 875 // Assemble filter array for use in `wp_list_filter()`. 876 $filters = wp_array_slice_assoc( $r, array( 'is_confirmed', 'is_banned', 'is_admin', 'is_mod', 'invite_sent' ) ); 877 foreach ( $filters as $filter_name => $filter_value ) { 878 if ( is_null( $filter_value ) ) { 879 unset( $filters[ $filter_name ] ); 880 } 881 } 882 883 if ( ! empty( $filters ) ) { 884 $groups = wp_list_filter( $groups, $filters ); 885 } 886 887 // By default, results are ordered by membership id. 888 if ( 'group_id' === $r['orderby'] ) { 889 ksort( $groups ); 890 } elseif ( in_array( $r['orderby'], array( 'id', 'date_modified' ) ) ) { 891 $groups = bp_sort_by_key( $groups, $r['orderby'] ); 892 } 893 894 // By default, results are ordered ASC. 895 if ( 'DESC' === strtoupper( $r['order'] ) ) { 896 // `true` to preserve keys. 897 $groups = array_reverse( $groups, true ); 898 } 899 900 return $groups; 901 } 902 903 /** 794 904 * Get the count of groups of which the specified user is a member. 795 905 * 796 906 * @since 1.0.0 … … function groups_avatar_upload_dir( $group_id = 0 ) { 889 999 * @return bool 890 1000 */ 891 1001 function groups_is_user_admin( $user_id, $group_id ) { 892 return BP_Groups_Member::check_is_admin( $user_id, $group_id ); 1002 $is_admin = false; 1003 1004 $user_groups = bp_get_user_groups( $user_id, array( 1005 'is_admin' => true, 1006 ) ); 1007 1008 if ( isset( $user_groups[ $group_id ] ) ) { 1009 $is_admin = $user_groups[ $group_id ]->id; 1010 } 1011 1012 return $is_admin; 893 1013 } 894 1014 895 1015 /** … … function groups_is_user_admin( $user_id, $group_id ) { 902 1022 * @return bool 903 1023 */ 904 1024 function groups_is_user_mod( $user_id, $group_id ) { 905 return BP_Groups_Member::check_is_mod( $user_id, $group_id ); 1025 $is_mod = false; 1026 1027 $user_groups = bp_get_user_groups( $user_id, array( 1028 'is_mod' => true, 1029 ) ); 1030 1031 if ( isset( $user_groups[ $group_id ] ) ) { 1032 $is_mod = $user_groups[ $group_id ]->id; 1033 } 1034 1035 return $is_mod; 906 1036 } 907 1037 908 1038 /** … … function groups_is_user_mod( $user_id, $group_id ) { 915 1045 * @return bool 916 1046 */ 917 1047 function groups_is_user_member( $user_id, $group_id ) { 918 return BP_Groups_Member::check_is_member( $user_id, $group_id ); 1048 $is_member = false; 1049 1050 $user_groups = bp_get_user_groups( $user_id, array( 1051 'is_admin' => null, 1052 'is_mod' => null, 1053 ) ); 1054 1055 if ( isset( $user_groups[ $group_id ] ) ) { 1056 $is_member = $user_groups[ $group_id ]->id; 1057 } 1058 1059 return $is_member; 919 1060 } 920 1061 921 1062 /** … … function groups_is_user_member( $user_id, $group_id ) { 929 1070 * @return bool 930 1071 */ 931 1072 function groups_is_user_banned( $user_id, $group_id ) { 932 return BP_Groups_Member::check_is_banned( $user_id, $group_id ); 1073 $is_banned = false; 1074 1075 $user_groups = bp_get_user_groups( $user_id, array( 1076 'is_confirmed' => null, 1077 'is_banned' => true, 1078 ) ); 1079 1080 if ( isset( $user_groups[ $group_id ] ) ) { 1081 $is_banned = $user_groups[ $group_id ]->id; 1082 } 1083 1084 return $is_banned; 1085 } 1086 1087 /** 1088 * Check whether a user has an outstanding invitation to a group. 1089 * 1090 * @since 2.6.0 1091 * 1092 * @param int $user_id ID of the user. 1093 * @param int $group_id ID of the group. 1094 * @return int|null ID of the membership if found. 1095 */ 1096 function groups_is_user_invited( $user_id, $group_id ) { 1097 $is_invited = false; 1098 1099 $user_groups = bp_get_user_groups( $user_id, array( 1100 'invite_sent' => true, 1101 'is_confirmed' => false, 1102 ) ); 1103 1104 if ( isset( $user_groups[ $group_id ] ) ) { 1105 $is_invited = $user_groups[ $group_id ]->id; 1106 } 1107 1108 return $is_invited; 1109 } 1110 1111 /** 1112 * Check whether a user has a pending membership request for a group. 1113 * 1114 * @since 2.6.0 1115 * 1116 * @param int $user_id ID of the user. 1117 * @param int $group_id ID of the group. 1118 * @return int|null ID of the membership if found. 1119 */ 1120 function groups_is_user_pending( $user_id, $group_id ) { 1121 $is_pending = false; 1122 1123 $user_groups = bp_get_user_groups( $user_id, array( 1124 'invite_sent' => false, 1125 'is_confirmed' => false, 1126 ) ); 1127 1128 if ( isset( $user_groups[ $group_id ] ) ) { 1129 $is_pending = $user_groups[ $group_id ]->id; 1130 } 1131 1132 return $is_pending; 933 1133 } 934 1134 935 1135 /** -
src/bp-groups/classes/class-bp-groups-component.php
diff --git src/bp-groups/classes/class-bp-groups-component.php src/bp-groups/classes/class-bp-groups-component.php index 21aeb15..93bb9b7 100644
class BP_Groups_Component extends BP_Component { 845 845 'bp_groups', 846 846 'bp_group_admins', 847 847 'bp_group_invite_count', 848 'group_meta' 848 'group_meta', 849 'bp_groups_memberships', 850 'bp_groups_memberships_for_user', 849 851 ) ); 850 852 851 853 parent::setup_cache_groups(); -
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 659d2c4..d3e8d38 100644
class BP_Groups_Group { 1422 1422 * @return array $paged_groups 1423 1423 */ 1424 1424 public static function get_group_extras( &$paged_groups, &$group_ids, $type = false ) { 1425 global $wpdb; 1426 1427 if ( empty( $group_ids ) ) 1428 return $paged_groups; 1429 1430 $bp = buddypress(); 1431 1432 // Sanitize group IDs. 1433 $group_ids = implode( ',', wp_parse_id_list( $group_ids ) ); 1434 1435 // Fetch the logged-in user's status within each group. 1436 if ( is_user_logged_in() ) { 1437 $user_status_results = $wpdb->get_results( $wpdb->prepare( "SELECT group_id, is_confirmed, invite_sent FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id IN ( {$group_ids} ) AND is_banned = 0", bp_loggedin_user_id() ) ); 1438 } else { 1439 $user_status_results = array(); 1440 } 1441 1442 // Reindex. 1443 $user_status = array(); 1444 foreach ( $user_status_results as $user_status_result ) { 1445 $user_status[ $user_status_result->group_id ] = $user_status_result; 1446 } 1447 1448 for ( $i = 0, $count = count( $paged_groups ); $i < $count; ++$i ) { 1449 $is_member = $is_invited = $is_pending = '0'; 1450 $gid = $paged_groups[ $i ]->id; 1451 1452 if ( isset( $user_status[ $gid ] ) ) { 1453 1454 // The is_confirmed means the user is a member. 1455 if ( $user_status[ $gid ]->is_confirmed ) { 1456 $is_member = '1'; 1457 1458 // The invite_sent means the user has been invited. 1459 } elseif ( $user_status[ $gid ]->invite_sent ) { 1460 $is_invited = '1'; 1461 1462 // User has sent request, but has not been confirmed. 1463 } else { 1464 $is_pending = '1'; 1465 } 1466 } 1467 1468 $paged_groups[ $i ]->is_member = $is_member; 1469 $paged_groups[ $i ]->is_invited = $is_invited; 1470 $paged_groups[ $i ]->is_pending = $is_pending; 1471 } 1472 1473 if ( is_user_logged_in() ) { 1474 $user_banned = $wpdb->get_col( $wpdb->prepare( "SELECT group_id FROM {$bp->groups->table_name_members} WHERE is_banned = 1 AND user_id = %d AND group_id IN ( {$group_ids} )", bp_loggedin_user_id() ) ); 1475 } else { 1476 $user_banned = array(); 1477 } 1478 1479 for ( $i = 0, $count = count( $paged_groups ); $i < $count; ++$i ) { 1480 $paged_groups[$i]->is_banned = false; 1481 1482 foreach ( (array) $user_banned as $group_id ) { 1483 if ( $group_id == $paged_groups[$i]->id ) { 1484 $paged_groups[$i]->is_banned = true; 1485 } 1486 } 1487 } 1425 $user_id = bp_loggedin_user_id(); 1426 1427 foreach ( $paged_groups as &$group ) { 1428 $group->is_member = groups_is_user_member( $user_id, $group->id ) ? '1' : '0'; 1429 $group->is_invited = groups_is_user_invited( $user_id, $group->id ) ? '1' : '0'; 1430 $group->is_pending = groups_is_user_pending( $user_id, $group->id ) ? '1' : '0'; 1431 $group->is_banned = (bool) groups_is_user_banned( $user_id, $group->id ); 1432 } 1488 1433 1489 1434 return $paged_groups; 1490 1435 } -
src/bp-groups/classes/class-bp-groups-member.php
diff --git src/bp-groups/classes/class-bp-groups-member.php src/bp-groups/classes/class-bp-groups-member.php index 68d315e..bfb1a76 100644
class BP_Groups_Member { 821 821 return false; 822 822 } 823 823 824 /** 825 * Fires before a group invitation is deleted. 826 * 827 * @since 2.6.0 828 * 829 * @param int $user_id ID of the user. 830 * @param int $group_id ID of the group. 831 */ 832 do_action( 'bp_groups_member_before_delete_invite', $user_id, $group_id ); 833 824 834 $table_name = buddypress()->groups->table_name_members; 825 835 826 836 $sql = "DELETE FROM {$table_name} … … class BP_Groups_Member { 1053 1063 } 1054 1064 1055 1065 /** 1066 * Get group membership objects by ID (or an array of IDs). 1067 * 1068 * @since 2.6.0 1069 * 1070 * @param int|string|array $membership_ids Single membership ID or comma-separated/array list of membership IDs. 1071 * @return array 1072 */ 1073 public static function get_memberships_by_id( $membership_ids ) { 1074 global $wpdb; 1075 1076 $bp = buddypress(); 1077 1078 $membership_ids = implode( ',', wp_parse_id_list( $membership_ids ) ); 1079 return $wpdb->get_results( "SELECT * FROM {$bp->groups->table_name_members} WHERE id IN ({$membership_ids})" ); 1080 } 1081 1082 /** 1056 1083 * Get the IDs users with outstanding membership requests to the group. 1057 1084 * 1058 1085 * @since 1.6.0 … … class BP_Groups_Member { 1161 1188 } 1162 1189 1163 1190 /** 1191 * Get all membership IDs for a user. 1192 * 1193 * @since 2.6.0 1194 * 1195 * @param int $user_id ID of the user. 1196 * @return array 1197 */ 1198 public static function get_membership_ids_for_user( $user_id ) { 1199 global $wpdb; 1200 1201 $bp = buddypress(); 1202 1203 $group_ids = $wpdb->get_col( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d ORDER BY id ASC", $user_id ) ); 1204 1205 return $group_ids; 1206 } 1207 1208 /** 1164 1209 * Delete all memberships for a given group. 1165 1210 * 1166 1211 * @since 1.6.0 -
tests/phpunit/testcases/groups/class-bp-groups-member.php
diff --git tests/phpunit/testcases/groups/class-bp-groups-member.php tests/phpunit/testcases/groups/class-bp-groups-member.php index 7aea071..5447ced 100644
class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase { 1268 1268 public function test_total_group_count_should_return_integer() { 1269 1269 $this->assertInternalType( 'int', BP_Groups_Member::total_group_count( 123 ) ); 1270 1270 } 1271 1272 /** 1273 * @group get_memberships_by_id 1274 */ 1275 public function test_get_memberships_by_id_with_single_id() { 1276 $users = $this->factory->user->create_many( 2 ); 1277 $groups = $this->factory->group->create_many( 2 ); 1278 1279 $m0 = $this->add_user_to_group( $users[0], $groups[0] ); 1280 $m1 = $this->add_user_to_group( $users[1], $groups[1] ); 1281 1282 $found = BP_Groups_Member::get_memberships_by_id( $m0 ); 1283 1284 $this->assertSame( 1, count( $found ) ); 1285 $this->assertEquals( $m0, $found[0]->id ); 1286 } 1287 1288 /** 1289 * @group get_memberships_by_id 1290 */ 1291 public function test_get_memberships_by_id_with_multiple_ids() { 1292 $users = $this->factory->user->create_many( 2 ); 1293 $groups = $this->factory->group->create_many( 2 ); 1294 1295 $m0 = $this->add_user_to_group( $users[0], $groups[0] ); 1296 $m1 = $this->add_user_to_group( $users[1], $groups[1] ); 1297 1298 $found = BP_Groups_Member::get_memberships_by_id( array( $m0, $m1 ) ); 1299 1300 $this->assertSame( 2, count( $found ) ); 1301 $this->assertEqualSets( array( $m0, $m1 ), wp_list_pluck( $found, 'id' ) ); 1302 } 1271 1303 } -
tests/phpunit/testcases/groups/functions/groupsIsUser.php
diff --git tests/phpunit/testcases/groups/functions/groupsIsUser.php tests/phpunit/testcases/groups/functions/groupsIsUser.php index 5a2abc3..b40df66 100644
class BP_Tests_Groups_Functions_GroupsIsUser extends BP_UnitTestCase { 54 54 'is_admin' => true, 55 55 ) ); 56 56 57 $this->assert Equals( true,groups_is_user_admin( self::$user, self::$groups[1] ) );57 $this->assertNotEmpty( groups_is_user_admin( self::$user, self::$groups[1] ) ); 58 58 } 59 59 60 60 public function test_groups_is_user_admin_expected_false() { … … class BP_Tests_Groups_Functions_GroupsIsUser extends BP_UnitTestCase { 76 76 'is_mod' => true, 77 77 ) ); 78 78 79 $this->assert Equals( true,groups_is_user_mod( self::$user, self::$groups[1] ) );79 $this->assertNotEmpty( groups_is_user_mod( self::$user, self::$groups[1] ) ); 80 80 } 81 81 82 82 public function test_groups_is_user_mod_expected_false() { … … class BP_Tests_Groups_Functions_GroupsIsUser extends BP_UnitTestCase { 107 107 public function test_groups_is_user_member_expected_true() { 108 108 $this->add_user_to_group( self::$user, self::$groups[1] ); 109 109 110 $this->assertEquals( true, groups_is_user_member( self::$user, self::$groups[1] ) ); 110 $this->assertNotEmpty( groups_is_user_member( self::$user, self::$groups[1] ) ); 111 } 112 113 public function test_groups_is_user_member_should_return_true_for_admin() { 114 $this->add_user_to_group( self::$user, self::$groups[1], array( 115 'is_admin' => true, 116 ) ); 117 118 $this->assertNotEmpty( groups_is_user_member( self::$user, self::$groups[1] ) ); 119 } 120 121 public function test_groups_is_user_member_should_return_true_for_mod() { 122 $this->add_user_to_group( self::$user, self::$groups[1], array( 123 'is_mod' => true, 124 ) ); 125 126 $this->assertNotEmpty( groups_is_user_member( self::$user, self::$groups[1] ) ); 111 127 } 112 128 113 129 public function test_groups_is_user_member_expected_false() { … … class BP_Tests_Groups_Functions_GroupsIsUser extends BP_UnitTestCase { 143 159 $m = new BP_Groups_Member( self::$user, self::$groups[1] ); 144 160 $m->ban(); 145 161 146 $this->assertEquals( true, groups_is_user_banned( self::$user, self::$groups[1] ) ); 162 $this->assertNotEmpty( groups_is_user_banned( self::$user, self::$groups[1] ) ); 163 } 164 165 public function test_groups_is_user_invited_should_return_false_for_confirmed_member() { 166 $this->add_user_to_group( self::$user, self::$groups[1] ); 167 $this->assertEquals( false, groups_is_user_invited( self::$user, self::$groups[1] ) ); 168 } 169 170 public function test_groups_is_user_invited_should_return_false_for_uninvited_member() { 171 $this->assertEquals( false, groups_is_user_invited( self::$user, self::$groups[1] ) ); 172 } 173 174 public function test_groups_is_user_invited_should_return_true_for_invited_member() { 175 $i = groups_invite_user( array( 176 'user_id' => self::$user, 177 'group_id' => self::$groups[1], 178 'inviter_id' => 123, 179 ) ); 180 181 // Send invite. 182 $m = new BP_Groups_Member( self::$user, self::$groups[1] ); 183 $m->invite_sent = 1; 184 $m->save(); 185 186 $this->assertNotEmpty( groups_is_user_invited( self::$user, self::$groups[1] ) ); 187 } 188 189 public function test_groups_is_user_pending_should_return_false_for_pending_member() { 190 groups_invite_user( array( 191 'user_id' => self::$user, 192 'group_id' => self::$groups[1], 193 'inviter_id' => 123, 194 ) ); 195 196 // Send invite. 197 $m = new BP_Groups_Member( self::$user, self::$groups[1] ); 198 $m->invite_sent = 1; 199 $m->save(); 200 201 $this->assertEquals( false, groups_is_user_pending( self::$user, self::$groups[1] ) ); 202 } 203 204 public function test_groups_is_user_pending_should_return_false_for_member_with_no_request() { 205 $this->assertEquals( false, groups_is_user_pending( self::$user, self::$groups[1] ) ); 206 } 207 208 public function test_groups_is_user_pending_should_return_true_for_pending_member() { 209 210 $m = new BP_Groups_Member; 211 $m->group_id = self::$groups[1]; 212 $m->user_id = self::$user; 213 $m->inviter_id = 0; 214 $m->is_admin = 0; 215 $m->user_title = ''; 216 $m->date_modified = bp_core_current_time(); 217 $m->is_confirmed = 0; 218 $m->comments = 'request'; 219 $m->save(); 220 221 $this->assertNotEmpty( groups_is_user_pending( self::$user, self::$groups[1] ) ); 147 222 } 148 223 }