BP_Groups_Member

BuddyPress Group Membership object.

Description

Source

File: bp-groups/classes/class-bp-groups-member.php

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
class BP_Groups_Member {
 
    /**
     * ID of the membership.
     *
     * @since BuddyPress 1.6.0
     * @var int
     */
    var $id;
 
    /**
     * ID of the group associated with the membership.
     *
     * @since BuddyPress 1.6.0
     * @var int
     */
    var $group_id;
 
    /**
     * ID of the user associated with the membership.
     *
     * @since BuddyPress 1.6.0
     * @var int
     */
    var $user_id;
 
    /**
     * ID of the user whose invitation initiated the membership.
     *
     * @since BuddyPress 1.6.0
     * @var int
     */
    var $inviter_id;
 
    /**
     * Whether the member is an admin of the group.
     *
     * @since BuddyPress 1.6.0
     * @var int
     */
    var $is_admin;
 
    /**
     * Whether the member is a mod of the group.
     *
     * @since BuddyPress 1.6.0
     * @var int
     */
    var $is_mod;
 
    /**
     * Whether the member is banned from the group.
     *
     * @since BuddyPress 1.6.0
     * @var int
     */
    var $is_banned;
 
    /**
     * Title used to describe the group member's role in the group.
     *
     * Eg, 'Group Admin'.
     *
     * @since BuddyPress 1.6.0
     * @var int
     */
    var $user_title;
 
    /**
     * Last modified date of the membership.
     *
     * This value is updated when, eg, invitations are accepted.
     *
     * @since BuddyPress 1.6.0
     * @var string
     */
    var $date_modified;
 
    /**
     * Whether the membership has been confirmed.
     *
     * @since BuddyPress 1.6.0
     * @var int
     */
    var $is_confirmed;
 
    /**
     * Comments associated with the membership.
     *
     * In BP core, these are limited to the optional message users can
     * include when requesting membership to a private group.
     *
     * @since BuddyPress 1.6.0
     * @var string
     */
    var $comments;
 
    /**
     * Whether an invitation has been sent for this membership.
     *
     * The purpose of this flag is to mark when an invitation has been
     * "drafted" (the user has been added via the interface at Send
     * Invites), but the Send button has not been pressed, so the
     * invitee has not yet been notified.
     *
     * @since BuddyPress 1.6.0
     * @var int
     */
    var $invite_sent;
 
    /**
     * WP_User object representing the membership's user.
     *
     * @since BuddyPress 1.6.0
     * @var WP_User
     */
    protected $user;
 
    /**
     * Constructor method.
     *
     * @since BuddyPress 1.6.0
     *
     * @param int      $user_id  Optional. Along with $group_id, can be used to
     *                           look up a membership.
     * @param int      $group_id Optional. Along with $user_id, can be used to
     *                           look up a membership.
     * @param int|bool $id       Optional. The unique ID of the membership object.
     * @param bool     $populate Whether to populate the properties of the
     *                           located membership. Default: true.
     */
    public function __construct( $user_id = 0, $group_id = 0, $id = false, $populate = true ) {
 
        // User and group are not empty, and ID is.
        if ( !empty( $user_id ) && !empty( $group_id ) && empty( $id ) ) {
            $this->user_id  = $user_id;
            $this->group_id = $group_id;
 
            if ( !empty( $populate ) ) {
                $this->populate();
            }
        }
 
        // ID is not empty.
        if ( !empty( $id ) ) {
            $this->id = $id;
 
            if ( !empty( $populate ) ) {
                $this->populate();
            }
        }
    }
 
    /**
     * Populate the object's properties.
     *
     * @since BuddyPress 1.6.0
     */
    public function populate() {
        global $wpdb;
 
        $bp = buddypress();
 
        if ( $this->user_id && $this->group_id && !$this->id )
            $sql = $wpdb->prepare( "SELECT * FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d", $this->user_id, $this->group_id );
 
        if ( !empty( $this->id ) )
            $sql = $wpdb->prepare( "SELECT * FROM {$bp->groups->table_name_members} WHERE id = %d", $this->id );
 
        $member = $wpdb->get_row($sql);
 
        if ( !empty( $member ) ) {
            $this->id            = (int) $member->id;
            $this->group_id      = (int) $member->group_id;
            $this->user_id       = (int) $member->user_id;
            $this->inviter_id    = (int) $member->inviter_id;
            $this->is_admin      = (int) $member->is_admin;
            $this->is_mod        = (int) $member->is_mod;
            $this->is_banned     = (int) $member->is_banned;
            $this->user_title    = $member->user_title;
            $this->date_modified = $member->date_modified;
            $this->is_confirmed  = (int) $member->is_confirmed;
            $this->comments      = $member->comments;
            $this->invite_sent   = (int) $member->invite_sent;
        }
    }
 
    /**
     * Magic getter.
     *
     * @since BuddyPress 2.8.0
     *
     * @param string $key Key.
     * @return BP_Core_User|null
     */
    public function __get( $key ) {
        switch ( $key ) {
            case 'user' :
                return $this->get_user_object( $this->user_id );
        }
    }
 
    /**
     * Magic issetter.
     *
     * @since BuddyPress 2.8.0
     *
     * @param string $key Key.
     * @return bool
     */
    public function __isset( $key ) {
        switch ( $key ) {
            case 'user' :
                return true;
 
            default :
                return isset( $this->{$key} );
        }
    }
 
    /**
     * Get the user object corresponding to this membership.
     *
     * Used for lazyloading the protected `user` property.
     *
     * @since BuddyPress 2.8.0
     *
     * @return BP_Core_User
     */
    protected function get_user_object() {
        if ( empty( $this->user ) ) {
            $this->user = new BP_Core_User( $this->user_id );
        }
 
        return $this->user;
    }
 
    /**
     * Save the membership data to the database.
     *
     * @since BuddyPress 1.6.0
     *
     * @return bool True on success, false on failure.
     */
    public function save() {
        global $wpdb;
 
        $bp = buddypress();
 
        $this->user_id       = apply_filters( 'groups_member_user_id_before_save',       $this->user_id,       $this->id );
        $this->group_id      = apply_filters( 'groups_member_group_id_before_save',      $this->group_id,      $this->id );
        $this->inviter_id    = apply_filters( 'groups_member_inviter_id_before_save',    $this->inviter_id,    $this->id );
        $this->is_admin      = apply_filters( 'groups_member_is_admin_before_save',      $this->is_admin,      $this->id );
        $this->is_mod        = apply_filters( 'groups_member_is_mod_before_save',        $this->is_mod,        $this->id );
        $this->is_banned     = apply_filters( 'groups_member_is_banned_before_save',     $this->is_banned,     $this->id );
        $this->user_title    = apply_filters( 'groups_member_user_title_before_save',    $this->user_title,    $this->id );
        $this->date_modified = apply_filters( 'groups_member_date_modified_before_save', $this->date_modified, $this->id );
        $this->is_confirmed  = apply_filters( 'groups_member_is_confirmed_before_save'$this->is_confirmed,  $this->id );
        $this->comments      = apply_filters( 'groups_member_comments_before_save',      $this->comments,      $this->id );
        $this->invite_sent   = apply_filters( 'groups_member_invite_sent_before_save',   $this->invite_sent,   $this->id );
 
        /**
         * Fires before the current group membership item gets saved.
         *
         * Please use this hook to filter the properties above. Each part will be passed in.
         *
         * @since BuddyPress 1.0.0
         *
         * @param BP_Groups_Member $this Current instance of the group membership item being saved. Passed by reference.
         */
        do_action_ref_array( 'groups_member_before_save', array( &$this ) );
 
        // The following properties are required; bail if not met.
        if ( empty( $this->user_id ) || empty( $this->group_id ) ) {
            return false;
        }
 
        if ( !empty( $this->id ) ) {
            $sql = $wpdb->prepare( "UPDATE {$bp->groups->table_name_members} SET inviter_id = %d, is_admin = %d, is_mod = %d, is_banned = %d, user_title = %s, date_modified = %s, is_confirmed = %d, comments = %s, invite_sent = %d WHERE id = %d", $this->inviter_id, $this->is_admin, $this->is_mod, $this->is_banned, $this->user_title, $this->date_modified, $this->is_confirmed, $this->comments, $this->invite_sent, $this->id );
        } else {
            // Ensure that user is not already a member of the group before inserting.
            if ( $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_confirmed = 1 LIMIT 1", $this->user_id, $this->group_id ) ) ) {
                return false;
            }
 
            $sql = $wpdb->prepare( "INSERT INTO {$bp->groups->table_name_members} ( user_id, group_id, inviter_id, is_admin, is_mod, is_banned, user_title, date_modified, is_confirmed, comments, invite_sent ) VALUES ( %d, %d, %d, %d, %d, %d, %s, %s, %d, %s, %d )", $this->user_id, $this->group_id, $this->inviter_id, $this->is_admin, $this->is_mod, $this->is_banned, $this->user_title, $this->date_modified, $this->is_confirmed, $this->comments, $this->invite_sent );
        }
 
        if ( !$wpdb->query( $sql ) )
            return false;
 
        $this->id = $wpdb->insert_id;
 
        // Update the user's group count.
        self::refresh_total_group_count_for_user( $this->user_id );
 
        // Update the group's member count.
        self::refresh_total_member_count_for_group( $this->group_id );
 
        /**
         * Fires after the current group membership item has been saved.
         *
         * Please use this hook to filter the properties above. Each part will be passed in.
         *
         * @since BuddyPress 1.0.0
         *
         * @param BP_Groups_Member $this Current instance of the group membership item has been saved. Passed by reference.
         */
        do_action_ref_array( 'groups_member_after_save', array( &$this ) );
 
        return true;
    }
 
    /**
     * Promote a member to a new status.
     *
     * @since BuddyPress 1.6.0
     *
     * @param string $status The new status. 'mod' or 'admin'.
     * @return bool True on success, false on failure.
     */
    public function promote( $status = 'mod' ) {
        if ( 'mod' == $status ) {
            $this->is_admin   = 0;
            $this->is_mod     = 1;
            $this->user_title = __( 'Group Moderator', 'buddyboss' );
        }
 
        if ( 'admin' == $status ) {
            $this->is_admin   = 1;
            $this->is_mod     = 0;
            $this->user_title = __( 'Group Organizer', 'buddyboss' );
        }
 
        return $this->save();
    }
 
    /**
     * Demote membership to Member status (non-admin, non-mod).
     *
     * @since BuddyPress 1.6.0
     *
     * @return bool True on success, false on failure.
     */
    public function demote() {
        $this->is_mod     = 0;
        $this->is_admin   = 0;
        $this->user_title = false;
 
        return $this->save();
    }
 
    /**
     * Ban the user from the group.
     *
     * @since BuddyPress 1.6.0
     *
     * @return bool True on success, false on failure.
     */
    public function ban() {
        if ( !empty( $this->is_admin ) )
            return false;
 
        $this->is_mod = 0;
        $this->is_banned = 1;
 
        return $this->save();
    }
 
    /**
     * Unban the user from the group.
     *
     * @since BuddyPress 1.6.0
     *
     * @return bool True on success, false on failure.
     */
    public function unban() {
        if ( !empty( $this->is_admin ) )
            return false;
 
        $this->is_banned = 0;
 
        return $this->save();
    }
 
    /**
     * Mark a pending invitation as accepted.
     *
     * @since BuddyPress 1.6.0
     */
    public function accept_invite() {
        $this->inviter_id    = 0;
        $this->is_confirmed  = 1;
        $this->date_modified = bp_core_current_time();
    }
 
    /**
     * Confirm a membership request.
     *
     * @since BuddyPress 1.6.0
     */
    public function accept_request() {
        $this->is_confirmed = 1;
        $this->date_modified = bp_core_current_time();
    }
 
    /**
     * Remove the current membership.
     *
     * @since BuddyPress 1.6.0
     *
     * @return bool True on success, false on failure.
     */
    public function remove() {
        global $wpdb;
 
        /**
         * Fires before a member is removed from a group.
         *
         * @since BuddyPress 2.3.0
         *
         * @param BP_Groups_Member $this Current group membership object.
         */
        do_action_ref_array( 'groups_member_before_remove', array( $this ) );
 
        $bp  = buddypress();
        $sql = $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d", $this->user_id, $this->group_id );
 
        if ( !$result = $wpdb->query( $sql ) )
            return false;
 
        // Update the user's group count.
        self::refresh_total_group_count_for_user( $this->user_id );
 
        // Update the group's member count.
        self::refresh_total_member_count_for_group( $this->group_id );
 
        /**
         * Fires after a member is removed from a group.
         *
         * @since BuddyPress 2.3.0
         *
         * @param BP_Groups_Member $this Current group membership object.
         */
        do_action_ref_array( 'groups_member_after_remove', array( $this ) );
 
        return $result;
    }
 
    /** Static Methods ****************************************************/
 
    /**
     * Refresh the total_group_count for a user.
     *
     * @since BuddyPress 1.8.0
     *
     * @param int $user_id ID of the user.
     * @return bool True on success, false on failure.
     */
    public static function refresh_total_group_count_for_user( $user_id ) {
        return bp_update_user_meta( $user_id, 'total_group_count', (int) self::total_group_count( $user_id ) );
    }
 
    /**
     * Refresh the total_member_count for a group.
     *
     * @since BuddyPress 1.8.0
     *
     * @param int $group_id ID of the group.
     * @return bool|int True on success, false on failure.
     */
    public static function refresh_total_member_count_for_group( $group_id ) {
        return groups_update_groupmeta( $group_id, 'total_member_count', (int) BP_Groups_Group::get_total_member_count( $group_id ) );
    }
 
    /**
     * Delete a membership, based on user + group IDs.
     *
     * @since BuddyPress 1.6.0
     *
     * @param int $user_id  ID of the user.
     * @param int $group_id ID of the group.
     * @return True on success, false on failure.
     */
    public static function delete( $user_id, $group_id ) {
        global $wpdb;
 
        /**
         * Fires before a group membership is deleted.
         *
         * @since BuddyPress 2.3.0
         *
         * @param int $user_id  ID of the user.
         * @param int $group_id ID of the group.
         */
        do_action( 'bp_groups_member_before_delete', $user_id, $group_id );
 
        $bp = buddypress();
        $remove = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d", $user_id, $group_id ) );
 
        // Update the user's group count.
        self::refresh_total_group_count_for_user( $user_id );
 
        // Update the group's member count.
        self::refresh_total_member_count_for_group( $group_id );
 
        /**
         * Fires after a member is removed from a group.
         *
         * @since BuddyPress 2.3.0
         *
         * @param int $user_id  ID of the user.
         * @param int $group_id ID of the group.
         */
        do_action( 'bp_groups_member_after_delete', $user_id, $group_id );
 
        return $remove;
    }
 
    /**
     * Get the IDs of the groups of which a specified user is a member.
     *
     * @since BuddyPress 1.6.0
     *
     * @param int      $user_id ID of the user.
     * @param int|bool $limit   Optional. Max number of results to return.
     *                          Default: false (no limit).
     * @param int|bool $page    Optional. Page offset of results to return.
     *                          Default: false (no limit).
     * @return array {
     *     @type array $groups Array of groups returned by paginated query.
     *     @type int   $total  Count of groups matching query.
     * }
     */
    public static function get_group_ids( $user_id, $limit = false, $page = false ) {
        global $wpdb;
 
        $pag_sql = '';
        if ( !empty( $limit ) && !empty( $page ) )
            $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
 
        $bp = buddypress();
 
        // If the user is logged in and viewing their own groups, we can show hidden and private groups.
        if ( $user_id != bp_loggedin_user_id() ) {
            $group_sql = $wpdb->prepare( "SELECT DISTINCT m.group_id FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE g.status != 'hidden' AND m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0{$pag_sql}", $user_id );
            $total_groups = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE g.status != 'hidden' AND m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0", $user_id ) );
        } else {
            $group_sql = $wpdb->prepare( "SELECT DISTINCT group_id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND is_confirmed = 1 AND is_banned = 0{$pag_sql}", $user_id );
            $total_groups = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT group_id) FROM {$bp->groups->table_name_members} WHERE user_id = %d AND is_confirmed = 1 AND is_banned = 0", $user_id ) );
        }
 
        $groups = $wpdb->get_col( $group_sql );
 
        return array( 'groups' => $groups, 'total' => (int) $total_groups );
    }
 
    /**
     * Get the IDs of the groups of which a specified user is a member, sorted by the date joined.
     *
     * @since BuddyPress 1.6.0
     *
     * @param int         $user_id ID of the user.
     * @param int|bool    $limit   Optional. Max number of results to return.
     *                             Default: false (no limit).
     * @param int|bool    $page    Optional. Page offset of results to return.
     *                             Default: false (no limit).
     * @param string|bool $filter  Optional. Limit results to groups whose name or
     *                             description field matches search terms.
     * @return array {
     *     @type array $groups Array of groups returned by paginated query.
     *     @type int   $total  Count of groups matching query.
     * }
     */
    public static function get_recently_joined( $user_id, $limit = false, $page = false, $filter = false ) {
        global $wpdb;
 
        $user_id_sql = $pag_sql = $hidden_sql = $filter_sql = '';
 
        $user_id_sql = $wpdb->prepare( 'm.user_id = %d', $user_id );
 
        if ( !empty( $limit ) && !empty( $page ) )
            $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
 
        if ( !empty( $filter ) ) {
            $search_terms_like = '%' . bp_esc_like( $filter ) . '%';
            $filter_sql = $wpdb->prepare( " AND ( g.name LIKE %s OR g.description LIKE %s )", $search_terms_like, $search_terms_like );
        }
 
        if ( $user_id != bp_loggedin_user_id() )
            $hidden_sql = " AND g.status != 'hidden'";
 
        $bp = buddypress();
 
        $paged_groups = $wpdb->get_results( "SELECT g.*, gm1.meta_value as total_member_count, gm2.meta_value as last_activity FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE g.id = m.group_id AND g.id = gm1.group_id AND g.id = gm2.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count'{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_confirmed = 1 AND m.is_banned = 0 ORDER BY m.date_modified DESC {$pag_sql}" );
        $total_groups = $wpdb->get_var( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_banned = 0 AND m.is_confirmed = 1 ORDER BY m.date_modified DESC" );
 
        return array( 'groups' => $paged_groups, 'total' => $total_groups );
    }
 
    /**
     * Get the IDs of the groups of which a specified user is an admin.
     *
     * @since BuddyPress 1.6.0
     *
     * @param int         $user_id ID of the user.
     * @param int|bool    $limit   Optional. Max number of results to return.
     *                             Default: false (no limit).
     * @param int|bool    $page    Optional. Page offset of results to return.
     *                             Default: false (no limit).
     * @param string|bool $filter  Optional. Limit results to groups whose name or
     *                             description field matches search terms.
     * @return array {
     *     @type array $groups Array of groups returned by paginated query.
     *     @type int   $total  Count of groups matching query.
     * }
     */
    public static function get_is_admin_of( $user_id, $limit = false, $page = false, $filter = false ) {
        global $wpdb;
 
        $user_id_sql = $pag_sql = $hidden_sql = $filter_sql = '';
 
        $user_id_sql = $wpdb->prepare( 'm.user_id = %d', $user_id );
 
        if ( !empty( $limit ) && !empty( $page ) )
            $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
 
        if ( !empty( $filter ) ) {
            $search_terms_like = '%' . bp_esc_like( $filter ) . '%';
            $filter_sql = $wpdb->prepare( " AND ( g.name LIKE %s OR g.description LIKE %s )", $search_terms_like, $search_terms_like );
        }
 
        if ( $user_id != bp_loggedin_user_id() )
            $hidden_sql = " AND g.status != 'hidden'";
 
        $bp = buddypress();
 
        $paged_groups = $wpdb->get_results( "SELECT g.*, gm1.meta_value as total_member_count, gm2.meta_value as last_activity FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE g.id = m.group_id AND g.id = gm1.group_id AND g.id = gm2.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count'{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_confirmed = 1 AND m.is_banned = 0 AND m.is_admin = 1 ORDER BY m.date_modified ASC {$pag_sql}" );
        $total_groups = $wpdb->get_var( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_confirmed = 1 AND m.is_banned = 0 AND m.is_admin = 1 ORDER BY date_modified ASC" );
 
        return array( 'groups' => $paged_groups, 'total' => $total_groups );
    }
 
    /**
     * Get the IDs of the groups of which a specified user is a moderator.
     *
     * @since BuddyPress 1.6.0
     *
     * @param int         $user_id ID of the user.
     * @param int|bool    $limit   Optional. Max number of results to return.
     *                             Default: false (no limit).
     * @param int|bool    $page    Optional. Page offset of results to return.
     *                             Default: false (no limit).
     * @param string|bool $filter  Optional. Limit results to groups whose name or
     *                             description field matches search terms.
     * @return array {
     *     @type array $groups Array of groups returned by paginated query.
     *     @type int   $total  Count of groups matching query.
     * }
     */
    public static function get_is_mod_of( $user_id, $limit = false, $page = false, $filter = false ) {
        global $wpdb;
 
        $user_id_sql = $pag_sql = $hidden_sql = $filter_sql = '';
 
        $user_id_sql = $wpdb->prepare( 'm.user_id = %d', $user_id );
 
        if ( !empty( $limit ) && !empty( $page ) )
            $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
 
        if ( !empty( $filter ) ) {
            $search_terms_like = '%' . bp_esc_like( $filter ) . '%';
            $filter_sql = $wpdb->prepare( " AND ( g.name LIKE %s OR g.description LIKE %s )", $search_terms_like, $search_terms_like );
        }
 
        if ( $user_id != bp_loggedin_user_id() )
            $hidden_sql = " AND g.status != 'hidden'";
 
        $bp = buddypress();
 
        $paged_groups = $wpdb->get_results( "SELECT g.*, gm1.meta_value as total_member_count, gm2.meta_value as last_activity FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE g.id = m.group_id AND g.id = gm1.group_id AND g.id = gm2.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count'{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_confirmed = 1 AND m.is_banned = 0 AND m.is_mod = 1 ORDER BY m.date_modified ASC {$pag_sql}" );
        $total_groups = $wpdb->get_var( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_confirmed = 1 AND m.is_banned = 0 AND m.is_mod = 1 ORDER BY date_modified ASC" );
 
        return array( 'groups' => $paged_groups, 'total' => $total_groups );
    }
 
    /**
     * Get the groups of which a specified user is banned from.
     *
     * @since BuddyPress 2.4.0
     *
     * @param int         $user_id ID of the user.
     * @param int|bool    $limit   Optional. Max number of results to return.
     *                             Default: false (no limit).
     * @param int|bool    $page    Optional. Page offset of results to return.
     *                             Default: false (no limit).
     * @param string|bool $filter  Optional. Limit results to groups whose name or
     *                             description field matches search terms.
     * @return array {
     *     @type array $groups Array of groups returned by paginated query.
     *     @type int   $total  Count of groups matching query.
     * }
     */
    public static function get_is_banned_of( $user_id, $limit = false, $page = false, $filter = false ) {
        global $wpdb;
 
        $bp = buddypress();
 
        $user_id_sql = $pag_sql = $hidden_sql = $filter_sql = '';
        $user_id_sql = $wpdb->prepare( 'm.user_id = %d', $user_id );
 
        if ( $limit && $page ) {
            $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit ), intval( $limit ) );
        }
 
        if ( $filter ) {
            $search_terms_like = '%' . bp_esc_like( $filter ) . '%';
            $filter_sql        = $wpdb->prepare( " AND ( g.name LIKE %s OR g.description LIKE %s )", $search_terms_like, $search_terms_like );
        }
 
        if ( $user_id !== bp_loggedin_user_id() && ! bp_current_user_can( 'bp_moderate' ) ) {
            $hidden_sql = " AND g.status != 'hidden'";
        }
 
        $paged_groups = $wpdb->get_results( "SELECT g.*, gm1.meta_value as total_member_count, gm2.meta_value as last_activity FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE g.id = m.group_id AND g.id = gm1.group_id AND g.id = gm2.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count'{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_banned = 1  ORDER BY m.date_modified ASC {$pag_sql}" );
        $total_groups = $wpdb->get_var( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id{$hidden_sql}{$filter_sql} AND {$user_id_sql} AND m.is_banned = 1 ORDER BY date_modified ASC" );
 
        return array( 'groups' => $paged_groups, 'total' => $total_groups );
    }
 
    /**
     * Get the count of groups of which the specified user is a member.
     *
     * @since BuddyPress 1.6.0
     *
     * @param int $user_id Optional. Default: ID of the displayed user.
     * @return int Group count.
     */
    public static function total_group_count( $user_id = 0 ) {
        global $wpdb;
 
        if ( empty( $user_id ) )
            $user_id = bp_displayed_user_id();
 
        $bp = buddypress();
 
        if ( $user_id != bp_loggedin_user_id() && !bp_current_user_can( 'bp_moderate' ) ) {
            return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id AND g.status != 'hidden' AND m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0", $user_id ) );
        } else {
            return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id AND m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0", $user_id ) );
        }
    }
 
    /**
     * Get a user's outstanding group invitations.
     *
     * @since BuddyPress 1.6.0
     *
     * @param int               $user_id ID of the invitee.
     * @param int|bool          $limit   Optional. Max number of results to return.
     *                                   Default: false (no limit).
     * @param int|bool          $page    Optional. Page offset of results to return.
     *                                   Default: false (no limit).
     * @param string|array|bool $exclude Optional. Array or comma-separated list
     *                                   of group IDs to exclude from results.
     * @return array {
     *     @type array $groups Array of groups returned by paginated query.
     *     @type int   $total  Count of groups matching query.
     * }
     */
    public static function get_invites( $user_id, $limit = false, $page = false, $exclude = false ) {
        global $wpdb;
 
        $pag_sql = ( !empty( $limit ) && !empty( $page ) ) ? $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ) : '';
 
        if ( !empty( $exclude ) ) {
            $exclude     = implode( ',', wp_parse_id_list( $exclude ) );
            $exclude_sql = " AND g.id NOT IN ({$exclude})";
        } else {
            $exclude_sql = '';
        }
 
        $bp = buddypress();
 
        $paged_groups = $wpdb->get_results( $wpdb->prepare( "SELECT g.*, gm1.meta_value as total_member_count, gm2.meta_value as last_activity FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE g.id = m.group_id AND g.id = gm1.group_id AND g.id = gm2.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count' AND m.is_confirmed = 0 AND m.inviter_id != 0 AND m.invite_sent = 1 AND m.user_id = %d {$exclude_sql} ORDER BY m.date_modified ASC {$pag_sql}", $user_id ) );
 
        return array( 'groups' => $paged_groups, 'total' => self::get_invite_count_for_user( $user_id ) );
    }
 
    /**
     * Gets the total group invite count for a user.
     *
     * @since BuddyPress 2.0.0
     *
     * @param int $user_id The user ID.
     * @return int
     */
    public static function get_invite_count_for_user( $user_id = 0 ) {
        global $wpdb;
 
        $bp = buddypress();
 
        $count = wp_cache_get( $user_id, 'bp_group_invite_count' );
 
        if ( false === $count ) {
            $count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT m.group_id) FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id AND m.is_confirmed = 0 AND m.inviter_id != 0 AND m.invite_sent = 1 AND m.user_id = %d", $user_id ) );
            wp_cache_set( $user_id, $count, 'bp_group_invite_count' );
        }
 
        return $count;
    }
 
    /**
     * Check whether a user has an outstanding invitation to a given group.
     *
     * @since BuddyPress 1.6.0
     *
     * @param int    $user_id  ID of the potential invitee.
     * @param int    $group_id ID of the group.
     * @param string $type     If 'sent', results are limited to those invitations
     *                         that have actually been sent (non-draft). Default: 'sent'.
     * @return int|null The ID of the invitation if found; null if not found.
     */
    public static function check_has_invite( $user_id, $group_id, $type = 'sent' ) {
        global $wpdb;
 
        if ( empty( $user_id ) )
            return false;
 
        $bp  = buddypress();
        $sql = "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_confirmed = 0 AND inviter_id != 0";
 
        if ( 'sent' == $type )
            $sql .= " AND invite_sent = 1";
 
        $query = $wpdb->get_var( $wpdb->prepare( $sql, $user_id, $group_id ) );
 
        return is_numeric( $query ) ? (int) $query : $query;
    }
 
    /**
     * Delete an invitation, by specifying user ID and group ID.
     *
     * @since BuddyPress 1.6.0
     *
     * @global wpdb $wpdb WordPress database abstraction object.
     *
     * @param  int $user_id  ID of the user.
     * @param  int $group_id ID of the group.
     * @return int Number of records deleted.
     */
    public static function delete_invite( $user_id, $group_id ) {
        global $wpdb;
 
        if ( empty( $user_id ) ) {
            return false;
        }
 
        /**
         * Fires before a group invitation is deleted.
         *
         * @since BuddyPress 2.6.0
         *
         * @param int $user_id  ID of the user.
         * @param int $group_id ID of the group.
         */
        do_action( 'bp_groups_member_before_delete_invite', $user_id, $group_id );
 
        $table_name = buddypress()->groups->table_name_members;
 
        $sql = "DELETE FROM {$table_name}
                WHERE user_id = %d
                    AND group_id = %d
                    AND is_confirmed = 0
                    AND inviter_id != 0";
 
        $prepared = $wpdb->prepare( $sql, $user_id, $group_id );
 
        return $wpdb->query( $prepared );
    }
 
    /**
     * Delete an unconfirmed membership request, by user ID and group ID.
     *
     * @since BuddyPress 1.6.0
     *
     * @param int $user_id  ID of the user.
     * @param int $group_id ID of the group.
     * @return int Number of records deleted.
     */
    public static function delete_request( $user_id, $group_id ) {
        global $wpdb;
 
        if ( empty( $user_id ) )
            return false;
 
        $bp = buddypress();
 
        return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_confirmed = 0 AND inviter_id = 0 AND invite_sent = 0", $user_id, $group_id ) );
    }
 
    /**
     * Check whether a user is an admin of a given group.
     *
     * @since BuddyPress 1.6.0
     *
     * @param int $user_id  ID of the user.
     * @param int $group_id ID of the group.
     * @return mixed
     */
    public static function check_is_admin( $user_id, $group_id ) {
        global $wpdb;
 
        if ( empty( $user_id ) )
            return false;
 
        $bp = buddypress();
 
        return $wpdb->query( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_admin = 1 AND is_banned = 0", $user_id, $group_id ) );
    }
 
    /**
     * Check whether a user is a mod of a given group.
     *
     * @since BuddyPress 1.6.0
     *
     * @param int $user_id  ID of the user.
     * @param int $group_id ID of the group.
     * @return mixed
     */
    public static function check_is_mod( $user_id, $group_id ) {
        global $wpdb;
 
        if ( empty( $user_id ) )
            return false;
 
        $bp = buddypress();
 
        return $wpdb->query( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_mod = 1 AND is_banned = 0", $user_id, $group_id ) );
    }
 
    /**
     * Check whether a user is a member of a given group.
     *
     * @since BuddyPress 1.6.0
     *
     * @param int $user_id  ID of the user.
     * @param int $group_id ID of the group.
     * @return mixed
     */
    public static function check_is_member( $user_id, $group_id ) {
        global $wpdb;
 
        if ( empty( $user_id ) )
            return false;
 
        $bp = buddypress();
 
        return $wpdb->query( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_confirmed = 1 AND is_banned = 0", $user_id, $group_id ) );
    }
 
    /**
     * Check whether a user is banned from a given group.
     *
     * @since BuddyPress 1.6.0
     *
     * @param int $user_id  ID of the user.
     * @param int $group_id ID of the group.
     * @return int|null int 1 if user is banned; int 0 if user is not banned;
     *                  null if user is not part of the group or if group doesn't exist.
     */
    public static function check_is_banned( $user_id, $group_id ) {
        global $wpdb;
 
        if ( empty( $user_id ) )
            return false;
 
        $bp = buddypress();
 
        $query = $wpdb->get_var( $wpdb->prepare( "SELECT is_banned FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d", $user_id, $group_id ) );
 
        return is_numeric( $query ) ? (int) $query : $query;
    }
 
    /**
     * Is the specified user the creator of the group?
     *
     * @since BuddyPress 1.2.6
     *
     * @param int $user_id  ID of the user.
     * @param int $group_id ID of the group.
     * @return int|null int of group ID if user is the creator; null on failure.
     */
    public static function check_is_creator( $user_id, $group_id ) {
        global $wpdb;
 
        if ( empty( $user_id ) )
            return false;
 
        $bp = buddypress();
 
        $query = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name} WHERE creator_id = %d AND id = %d", $user_id, $group_id ) );
 
        return is_numeric( $query ) ? (int) $query : $query;
    }
 
    /**
     * Check whether a user has an outstanding membership request for a given group.
     *
     * @since BuddyPress 1.6.0
     *
     * @param int $user_id  ID of the user.
     * @param int $group_id ID of the group.
     * @return int Database ID of the membership if found; int 0 on failure.
     */
    public static function check_for_membership_request( $user_id, $group_id ) {
        global $wpdb;
 
        if ( empty( $user_id ) )
            return false;
 
        $bp = buddypress();
 
        return $wpdb->query( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d AND is_confirmed = 0 AND is_banned = 0 AND inviter_id = 0", $user_id, $group_id ) );
    }
 
    /**
     * Get a list of randomly selected IDs of groups that the member belongs to.
     *
     * @since BuddyPress 1.6.0
     *
     * @param int $user_id      ID of the user.
     * @param int $total_groups Max number of group IDs to return. Default: 5.
     * @return array Group IDs.
     */
    public static function get_random_groups( $user_id = 0, $total_groups = 5 ) {
        global $wpdb;
 
        $bp = buddypress();
 
        // If the user is logged in and viewing their random groups, we can show hidden and private groups.
        if ( bp_is_my_profile() ) {
            return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT group_id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND is_confirmed = 1 AND is_banned = 0 ORDER BY rand() LIMIT %d", $user_id, $total_groups ) ) );
        } else {
            return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT m.group_id FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id AND g.status != 'hidden' AND m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0 ORDER BY rand() LIMIT %d", $user_id, $total_groups ) ) );
        }
    }
 
    /**
     * Get the IDs of all a given group's members.
     *
     * @since BuddyPress 1.6.0
     *
     * @param int $group_id ID of the group.
     * @return array IDs of all group members.
     */
    public static function get_group_member_ids( $group_id ) {
        global $wpdb;
 
        $bp = buddypress();
 
        return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_confirmed = 1 AND is_banned = 0", $group_id ) ) );
    }
 
    /**
     * Get a list of all a given group's admins.
     *
     * @since BuddyPress 1.6.0
     *
     * @param int $group_id ID of the group.
     * @return array Info about group admins (user_id + date_modified).
     */
    public static function get_group_administrator_ids( $group_id ) {
        global $wpdb;
 
        if ( empty( $group_id ) ) {
            return array();
        }
 
        $group_admins = wp_cache_get( $group_id, 'bp_group_admins' );
 
        if ( false === $group_admins ) {
            self::prime_group_admins_mods_cache( array( $group_id ) );
            $group_admins = wp_cache_get( $group_id, 'bp_group_admins' );
        }
 
        if ( false === $group_admins ) {
            // The wp_cache_get is still coming up empty. Return an empty array.
            $group_admins = array();
        } else {
            // Cast the user_id property as an integer.
            foreach ( (array) $group_admins as $key => $data ) {
                $group_admins[ $key ]->user_id = (int) $group_admins[ $key ]->user_id;
            }
        }
 
        return $group_admins;
    }
 
    /**
     * Prime the bp_group_admins cache for one or more groups.
     *
     * @since BuddyPress 2.7.0
     *
     * @param array $group_ids IDs of the groups.
     * @return bool True on success.
     */
    public static function prime_group_admins_mods_cache( $group_ids ) {
        global $wpdb;
 
        $uncached = bp_get_non_cached_ids( $group_ids, 'bp_group_admins' );
 
        if ( $uncached ) {
            $bp = buddypress();
            $uncached_sql = implode( ',', array_map( 'intval', $uncached ) );
            $group_admin_mods = $wpdb->get_results( "SELECT user_id, group_id, date_modified, is_admin, is_mod FROM {$bp->groups->table_name_members} WHERE group_id IN ({$uncached_sql}) AND ( is_admin = 1 OR is_mod = 1 ) AND is_banned = 0" );
 
            $admins = $mods = array();
            if ( $group_admin_mods ) {
                foreach ( $group_admin_mods as $group_admin_mod ) {
                    $obj = new stdClass();
                    $obj->user_id = $group_admin_mod->user_id;
                    $obj->date_modified = $group_admin_mod->date_modified;
 
                    if ( $group_admin_mod->is_admin ) {
                        $admins[ $group_admin_mod->group_id ][] = $obj;
                    } else {
                        $mods[ $group_admin_mod->group_id ][] = $obj;
                    }
                }
            }
 
            // Prime cache for all groups, even those with no matches.
            foreach ( $uncached as $group_id ) {
                $group_admins = isset( $admins[ $group_id ] ) ? $admins[ $group_id ] : array();
                wp_cache_set( $group_id, $group_admins, 'bp_group_admins' );
 
                $group_mods = isset( $mods[ $group_id ] ) ? $mods[ $group_id ] : array();
                wp_cache_set( $group_id, $group_mods, 'bp_group_mods' );
            }
        }
    }
 
    /**
     * Get a list of all a given group's moderators.
     *
     * @since BuddyPress 1.6.0
     *
     * @param int $group_id ID of the group.
     * @return array Info about group mods (user_id + date_modified).
     */
    public static function get_group_moderator_ids( $group_id ) {
        global $wpdb;
 
        if ( empty( $group_id ) ) {
            return array();
        }
 
        $group_mods = wp_cache_get( $group_id, 'bp_group_mods' );
 
        if ( false === $group_mods ) {
            self::prime_group_admins_mods_cache( array( $group_id ) );
            $group_mods = wp_cache_get( $group_id, 'bp_group_mods' );
        }
 
        if ( false === $group_mods ) {
            // The wp_cache_get is still coming up empty. Return an empty array.
            $group_mods = array();
        } else {
            // Cast the user_id property as an integer.
            foreach ( (array) $group_mods as $key => $data ) {
                $group_mods[ $key ]->user_id = (int) $group_mods[ $key ]->user_id;
            }
        }
 
        return $group_mods;
    }
 
    /**
     * Get group membership objects by ID (or an array of IDs).
     *
     * @since BuddyPress 2.6.0
     *
     * @param int|string|array $membership_ids Single membership ID or comma-separated/array list of membership IDs.
     * @return array
     */
    public static function get_memberships_by_id( $membership_ids ) {
        global $wpdb;
 
        $bp = buddypress();
 
        $membership_ids = implode( ',', wp_parse_id_list( $membership_ids ) );
        return $wpdb->get_results( "SELECT * FROM {$bp->groups->table_name_members} WHERE id IN ({$membership_ids})" );
    }
 
    /**
     * Get the IDs users with outstanding membership requests to the group.
     *
     * @since BuddyPress 1.6.0
     *
     * @param int $group_id ID of the group.
     * @return array IDs of users with outstanding membership requests.
     */
    public static function get_all_membership_request_user_ids( $group_id ) {
        global $wpdb;
 
        $bp = buddypress();
 
        return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_confirmed = 0 AND inviter_id = 0", $group_id ) ) );
    }
 
    /**
     * Get members of a group.
     *
     * @deprecated 1.6.0
     *
     * @param int        $group_id            ID of the group being queried for.
     * @param bool|int   $limit               Max amount to return.
     * @param bool|int   $page                Pagination value.
     * @param bool       $exclude_admins_mods Whether or not to exclude admins and moderators.
     * @param bool       $exclude_banned      Whether or not to exclude banned members.
     * @param bool|array $exclude             Array of user IDs to exclude.
     * @return false|array
     */
    public static function get_all_for_group( $group_id, $limit = false, $page = false, $exclude_admins_mods = true, $exclude_banned = true, $exclude = false ) {
        global $wpdb;
 
        _deprecated_function( __METHOD__, '1.8', 'BP_Group_Member_Query' );
 
        $pag_sql = '';
        if ( !empty( $limit ) && !empty( $page ) )
            $pag_sql = $wpdb->prepare( "LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
 
        $exclude_admins_sql = '';
        if ( !empty( $exclude_admins_mods ) )
            $exclude_admins_sql = "AND is_admin = 0 AND is_mod = 0";
 
        $banned_sql = '';
        if ( !empty( $exclude_banned ) )
            $banned_sql = " AND is_banned = 0";
 
        $exclude_sql = '';
        if ( !empty( $exclude ) ) {
            $exclude     = implode( ',', wp_parse_id_list( $exclude ) );
            $exclude_sql = " AND m.user_id NOT IN ({$exclude})";
        }
 
        $bp = buddypress();
 
        if ( bp_is_active( 'xprofile' ) ) {
 
            /**
             * Filters the SQL prepared statement used to fetch group members.
             *
             * @since BuddyPress 1.5.0
             *
             * @param string $value SQL prepared statement for fetching group members.
             */
            $members = $wpdb->get_results( apply_filters( 'bp_group_members_user_join_filter', $wpdb->prepare( "SELECT m.user_id, m.date_modified, m.is_banned, u.user_login, u.user_nicename, u.user_email, pd.value as display_name FROM {$bp->groups->table_name_members} m, {$wpdb->users} u, {$bp->profile->table_name_data} pd WHERE u.ID = m.user_id AND u.ID = pd.user_id AND pd.field_id = 1 AND group_id = %d AND is_confirmed = 1 {$banned_sql} {$exclude_admins_sql} {$exclude_sql} ORDER BY m.date_modified DESC {$pag_sql}", $group_id ) ) );
        } else {
 
            /** This filter is documented in bp-groups/bp-groups-classes */
            $members = $wpdb->get_results( apply_filters( 'bp_group_members_user_join_filter', $wpdb->prepare( "SELECT m.user_id, m.date_modified, m.is_banned, u.user_login, u.user_nicename, u.user_email, u.display_name FROM {$bp->groups->table_name_members} m, {$wpdb->users} u WHERE u.ID = m.user_id AND group_id = %d AND is_confirmed = 1 {$banned_sql} {$exclude_admins_sql} {$exclude_sql} ORDER BY m.date_modified DESC {$pag_sql}", $group_id ) ) );
        }
 
        if ( empty( $members ) ) {
            return false;
        }
 
        if ( empty( $pag_sql ) ) {
            $total_member_count = count( $members );
        } else {
 
            /**
             * Filters the SQL prepared statement used to fetch group members total count.
             *
             * @since BuddyPress 1.5.0
             *
             * @param string $value SQL prepared statement for fetching group member count.
             */
            $total_member_count = $wpdb->get_var( apply_filters( 'bp_group_members_count_user_join_filter', $wpdb->prepare( "SELECT COUNT(user_id) FROM {$bp->groups->table_name_members} m WHERE group_id = %d AND is_confirmed = 1 {$banned_sql} {$exclude_admins_sql} {$exclude_sql}", $group_id ) ) );
        }
 
        // Fetch whether or not the user is a friend.
        foreach ( (array) $members as $user )
            $user_ids[] = $user->user_id;
 
        $user_ids = implode( ',', wp_parse_id_list( $user_ids ) );
 
        if ( bp_is_active( 'friends' ) ) {
            $friend_status = $wpdb->get_results( $wpdb->prepare( "SELECT initiator_user_id, friend_user_id, is_confirmed FROM {$bp->friends->table_name} WHERE (initiator_user_id = %d AND friend_user_id IN ( {$user_ids} ) ) OR (initiator_user_id IN ( {$user_ids} ) AND friend_user_id = %d )", bp_loggedin_user_id(), bp_loggedin_user_id() ) );
            for ( $i = 0, $count = count( $members ); $i < $count; ++$i ) {
                foreach ( (array) $friend_status as $status ) {
                    if ( $status->initiator_user_id == $members[$i]->user_id || $status->friend_user_id == $members[$i]->user_id ) {
                        $members[$i]->is_friend = $status->is_confirmed;
                    }
                }
            }
        }
 
        return array( 'members' => $members, 'count' => $total_member_count );
    }
 
    /**
     * Get all membership IDs for a user.
     *
     * @since BuddyPress 2.6.0
     *
     * @param int $user_id ID of the user.
     * @return array
     */
    public static function get_membership_ids_for_user( $user_id ) {
        global $wpdb;
 
        $bp = buddypress();
 
        $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 ) );
 
        return $group_ids;
    }
 
    /**
     * Delete all memberships for a given group.
     *
     * @since BuddyPress 1.6.0
     *
     * @param int $group_id ID of the group.
     * @return int Number of records deleted.
     */
    public static function delete_all( $group_id ) {
        global $wpdb;
 
        $bp = buddypress();
 
        return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE group_id = %d", $group_id ) );
    }
 
    /**
     * Delete all group membership information for the specified user.
     *
     * @since BuddyPress 1.0.0
     *
     * @param int $user_id ID of the user.
     * @return mixed
     */
    public static function delete_all_for_user( $user_id ) {
        global $wpdb;
 
        $bp = buddypress();
 
        // Get all the group ids for the current user's groups and update counts.
        $group_ids = BP_Groups_Member::get_group_ids( $user_id );
        foreach ( $group_ids['groups'] as $group_id ) {
            groups_update_groupmeta( $group_id, 'total_member_count', groups_get_total_member_count( $group_id ) - 1 );
 
            // If current user is the creator of a group and is the sole admin, delete that group to avoid counts going out-of-sync.
            if ( groups_is_user_admin( $user_id, $group_id ) && count( groups_get_group_admins( $group_id ) ) < 2 && groups_is_user_creator( $user_id, $group_id ) )
                groups_delete_group( $group_id );
        }
 
        return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE user_id = %d", $user_id ) );
    }
}

Methods

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.