/
bp-groups-functions.php
2938 lines (2519 loc) · 88.4 KB
/
bp-groups-functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
<?php
/**
* BuddyPress Groups Functions.
*
* Functions are where all the magic happens in BuddyPress. They will
* handle the actual saving or manipulation of information. Usually they will
* hand off to a database class for data access, then return
* true or false on success or failure.
*
* @package BuddyPress
* @subpackage GroupsFunctions
* @since 1.5.0
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
/**
* Check whether there is a Groups directory page in the $bp global.
*
* @since 1.5.0
*
* @return bool True if set, False if empty.
*/
function bp_groups_has_directory() {
$bp = buddypress();
return (bool) !empty( $bp->pages->groups->id );
}
/**
* Fetch a single group object.
*
* When calling up a group object, you should always use this function instead
* of instantiating BP_Groups_Group directly, so that you will inherit cache
* support and pass through the groups_get_group filter.
*
* @since 1.2.0
* @since 2.7.0 The function signature was changed to accept a group ID only,
* instead of an array containing the group ID.
*
* @param int $group_id ID of the group.
* @return BP_Groups_Group $group The group object.
*/
function groups_get_group( $group_id ) {
/*
* Backward compatibilty.
* Old-style arguments take the form of an array or a query string.
*/
if ( ! is_numeric( $group_id ) ) {
$r = bp_parse_args( $group_id, array(
'group_id' => false,
'load_users' => false,
'populate_extras' => false,
), 'groups_get_group' );
$group_id = $r['group_id'];
}
$group = new BP_Groups_Group( $group_id );
/**
* Filters a single group object.
*
* @since 1.2.0
*
* @param BP_Groups_Group $group Single group object.
*/
return apply_filters( 'groups_get_group', $group );
}
/** Group Creation, Editing & Deletion ****************************************/
/**
* Create a group.
*
* @since 1.0.0
*
* @param array|string $args {
* An array of arguments.
* @type int|bool $group_id Pass a group ID to update an existing item, or
* 0 / false to create a new group. Default: 0.
* @type int $creator_id The user ID that creates the group.
* @type string $name The group name.
* @type string $description Optional. The group's description.
* @type string $slug The group slug.
* @type string $status The group's status. Accepts 'public', 'private' or
* 'hidden'. Defaults to 'public'.
* @type int $parent_id The ID of the parent group. Default: 0.
* @type int $enable_forum Optional. Whether the group has a forum enabled.
* If a bbPress forum is enabled for the group,
* set this to 1. Default: 0.
* @type string $date_created The GMT time, in Y-m-d h:i:s format, when the group
* was created. Defaults to the current time.
* }
* @return int|bool The ID of the group on success. False on error.
*/
function groups_create_group( $args = '' ) {
$args = bp_parse_args( $args, array(
'group_id' => 0,
'creator_id' => 0,
'name' => '',
'description' => '',
'slug' => '',
'status' => null,
'parent_id' => null,
'enable_forum' => null,
'date_created' => null
), 'groups_create_group' );
extract( $args, EXTR_SKIP );
// Pass an existing group ID.
if ( ! empty( $group_id ) ) {
$group = groups_get_group( $group_id );
$name = ! empty( $name ) ? $name : $group->name;
$slug = ! empty( $slug ) ? $slug : $group->slug;
$creator_id = ! empty( $creator_id ) ? $creator_id : $group->creator_id;
$description = ! empty( $description ) ? $description : $group->description;
$status = ! is_null( $status ) ? $status : $group->status;
$parent_id = ! is_null( $parent_id ) ? $parent_id : $group->parent_id;
$enable_forum = ! is_null( $enable_forum ) ? $enable_forum : $group->enable_forum;
$date_created = ! is_null( $date_created ) ? $date_created : $group->date_created;
// Groups need at least a name.
if ( empty( $name ) ) {
return false;
}
// Create a new group.
} else {
// Instantiate new group object.
$group = new BP_Groups_Group;
// Check for null values, reset to sensible defaults.
$status = ! is_null( $status ) ? $status : 'public';
$parent_id = ! is_null( $parent_id ) ? $parent_id : 0;
$enable_forum = ! is_null( $enable_forum ) ? $enable_forum : 0;
$date_created = ! is_null( $date_created ) ? $date_created : bp_core_current_time();
}
// Set creator ID.
if ( $creator_id ) {
$group->creator_id = (int) $creator_id;
} elseif ( is_user_logged_in() ) {
$group->creator_id = bp_loggedin_user_id();
}
if ( ! $group->creator_id ) {
return false;
}
// Validate status.
if ( ! groups_is_valid_status( $status ) ) {
return false;
}
// Set group name.
$group->name = $name;
$group->description = $description;
$group->slug = $slug;
$group->status = $status;
$group->parent_id = $parent_id;
$group->enable_forum = (int) $enable_forum;
$group->date_created = $date_created;
// Save group.
if ( ! $group->save() ) {
return false;
}
// If this is a new group, set up the creator as the first member and admin.
if ( empty( $group_id ) ) {
$member = new BP_Groups_Member;
$member->group_id = $group->id;
$member->user_id = $group->creator_id;
$member->is_admin = 1;
$member->user_title = __( 'Group Admin', 'buddypress' );
$member->is_confirmed = 1;
$member->date_modified = bp_core_current_time();
$member->save();
/**
* Fires after the creation of a new group and a group creator needs to be made.
*
* @since 1.5.0
*
* @param int $id ID of the newly created group.
* @param BP_Groups_Member $member Instance of the member who is assigned
* as group creator.
* @param BP_Groups_Group $group Instance of the group being created.
*/
do_action( 'groups_create_group', $group->id, $member, $group );
} else {
/**
* Fires after the update of a group.
*
* @since 1.5.0
*
* @param int $id ID of the updated group.
* @param BP_Groups_Group $group Instance of the group being updated.
*/
do_action( 'groups_update_group', $group->id, $group );
}
/**
* Fires after the creation or update of a group.
*
* @since 1.0.0
*
* @param int $id ID of the newly created group.
* @param BP_Groups_Group $group Instance of the group being updated.
*/
do_action( 'groups_created_group', $group->id, $group );
return $group->id;
}
/**
* Edit the base details for a group.
*
* These are the settings that appear on the first page of the group's Admin
* section (Name, Description, and "Notify members...").
*
* @since 1.0.0
*
* @param array $args {
* An array of optional arguments.
* @type int $group_id ID of the group.
* @type string $name Name of the group.
* @type string $slug Slug of the group.
* @type string $description Description of the group.
* @type bool $notify_members Whether to send an email notification to group
* members about changes in these details.
* }
* @return bool True on success, false on failure.
*/
function groups_edit_base_group_details( $args = array() ) {
// Backward compatibility with old method of passing arguments.
if ( ! is_array( $args ) || func_num_args() > 1 ) {
_deprecated_argument( __METHOD__, '2.9.0', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) );
$old_args_keys = array(
0 => 'group_id',
1 => 'name',
2 => 'description',
3 => 'notify_members',
);
$args = bp_core_parse_args_array( $old_args_keys, func_get_args() );
}
$r = bp_parse_args( $args, array(
'group_id' => bp_get_current_group_id(),
'name' => null,
'slug' => null,
'description' => null,
'notify_members' => false,
), 'groups_edit_base_group_details' );
if ( ! $r['group_id'] ) {
return false;
}
$group = groups_get_group( $r['group_id'] );
$old_group = clone $group;
// Group name, slug and description can never be empty. Update only if provided.
if ( $r['name'] ) {
$group->name = $r['name'];
}
if ( $r['slug'] && $r['slug'] != $group->slug ) {
$group->slug = groups_check_slug( $r['slug'] );
}
if ( $r['description'] ) {
$group->description = $r['description'];
}
if ( ! $group->save() ) {
return false;
}
// Maybe update the "previous_slug" groupmeta.
if ( $group->slug != $old_group->slug ) {
/*
* If the old slug exists in this group's past, delete that entry.
* Recent previous_slugs are preferred when selecting the current group
* from an old group slug, so we want the previous slug to be
* saved "now" in the groupmeta table and don't need the old record.
*/
groups_delete_groupmeta( $group->id, 'previous_slug', $old_group->slug );
groups_add_groupmeta( $group->id, 'previous_slug', $old_group->slug );
}
if ( $r['notify_members'] ) {
groups_notification_group_updated( $group->id, $old_group );
}
/**
* Fired after a group's details are updated.
*
* @since 2.2.0
*
* @param int $value ID of the group.
* @param BP_Groups_Group $old_group Group object, before being modified.
* @param bool $notify_members Whether to send an email notification to members about the change.
*/
do_action( 'groups_details_updated', $group->id, $old_group, $r['notify_members'] );
return true;
}
/**
* Edit the base details for a group.
*
* These are the settings that appear on the Settings page of the group's Admin
* section (privacy settings, "enable forum", invitation status).
*
* @since 1.0.0
*
* @param int $group_id ID of the group.
* @param bool $enable_forum Whether to enable a forum for the group.
* @param string $status Group status. 'public', 'private', 'hidden'.
* @param string|bool $invite_status Optional. Who is allowed to send invitations
* to the group. 'members', 'mods', or 'admins'.
* @param int|bool $parent_id Parent group ID.
* @return bool True on success, false on failure.
*/
function groups_edit_group_settings( $group_id, $enable_forum, $status, $invite_status = false, $parent_id = false ) {
$group = groups_get_group( $group_id );
$group->enable_forum = $enable_forum;
/**
* Before we potentially switch the group status, if it has been changed to public
* from private and there are outstanding membership requests, auto-accept those requests.
*/
if ( 'private' == $group->status && 'public' == $status )
groups_accept_all_pending_membership_requests( $group->id );
// Now update the status.
$group->status = $status;
// Update the parent ID if necessary.
if ( false !== $parent_id ) {
$group->parent_id = $parent_id;
}
if ( !$group->save() )
return false;
// Set the invite status.
if ( $invite_status )
groups_update_groupmeta( $group->id, 'invite_status', $invite_status );
groups_update_groupmeta( $group->id, 'last_activity', bp_core_current_time() );
/**
* Fires after the update of a groups settings.
*
* @since 1.0.0
*
* @param int $id ID of the group that was updated.
*/
do_action( 'groups_settings_updated', $group->id );
return true;
}
/**
* Delete a group and all of its associated metadata.
*
* @since 1.0.0
*
* @param int $group_id ID of the group to delete.
* @return bool True on success, false on failure.
*/
function groups_delete_group( $group_id ) {
/**
* Fires before the deletion of a group.
*
* @since 1.5.0
*
* @param int $group_id ID of the group to be deleted.
*/
do_action( 'groups_before_delete_group', $group_id );
// Get the group object.
$group = groups_get_group( $group_id );
// Bail if group cannot be deleted.
if ( ! $group->delete() ) {
return false;
}
// Remove all outstanding invites for this group.
groups_delete_all_group_invites( $group_id );
/**
* Fires after the deletion of a group.
*
* @since 1.0.0
*
* @param int $group_id ID of the group that was deleted.
*/
do_action( 'groups_delete_group', $group_id );
return true;
}
/**
* Check a group status (eg 'private') against the whitelist of registered statuses.
*
* @since 1.1.0
*
* @param string $status Status to check.
* @return bool True if status is allowed, otherwise false.
*/
function groups_is_valid_status( $status ) {
$bp = buddypress();
return in_array( $status, (array) $bp->groups->valid_status );
}
/**
* Provide a unique, sanitized version of a group slug.
*
* @since 1.0.0
*
* @param string $slug Group slug to check.
* @return string $slug A unique and sanitized slug.
*/
function groups_check_slug( $slug ) {
$bp = buddypress();
// First, make the proposed slug work in a URL.
$slug = sanitize_title( $slug );
if ( 'wp' == substr( $slug, 0, 2 ) )
$slug = substr( $slug, 2, strlen( $slug ) - 2 );
if ( in_array( $slug, (array) $bp->groups->forbidden_names ) )
$slug = $slug . '-' . rand();
if ( BP_Groups_Group::check_slug( $slug ) ) {
do {
$slug = $slug . '-' . rand();
}
while ( BP_Groups_Group::check_slug( $slug ) );
}
return $slug;
}
/**
* Get a group slug by its ID.
*
* @since 1.0.0
*
* @param int $group_id The numeric ID of the group.
* @return string The group's slug.
*/
function groups_get_slug( $group_id ) {
$group = groups_get_group( $group_id );
return !empty( $group->slug ) ? $group->slug : '';
}
/**
* Get a group ID by its slug.
*
* @since 1.6.0
*
* @param string $group_slug The group's slug.
* @return int|null The group ID on success; null on failure.
*/
function groups_get_id( $group_slug ) {
return BP_Groups_Group::group_exists( $group_slug );
}
/**
* Get a group ID by checking against old (not currently active) slugs.
*
* @since 2.9.0
*
* @param string $group_slug The group's slug.
* @return int|null The group ID on success; null on failure.
*/
function groups_get_id_by_previous_slug( $group_slug ) {
return BP_Groups_Group::get_id_by_previous_slug( $group_slug );
}
/** User Actions **************************************************************/
/**
* Remove a user from a group.
*
* @since 1.0.0
*
* @param int $group_id ID of the group.
* @param int $user_id Optional. ID of the user. Defaults to the currently
* logged-in user.
* @return bool True on success, false on failure.
*/
function groups_leave_group( $group_id, $user_id = 0 ) {
if ( empty( $user_id ) )
$user_id = bp_loggedin_user_id();
// Don't let single admins leave the group.
if ( count( groups_get_group_admins( $group_id ) ) < 2 ) {
if ( groups_is_user_admin( $user_id, $group_id ) ) {
bp_core_add_message( __( 'As the only admin, you cannot leave the group.', 'buddypress' ), 'error' );
return false;
}
}
if ( ! BP_Groups_Member::delete( $user_id, $group_id ) ) {
return false;
}
bp_core_add_message( __( 'You successfully left the group.', 'buddypress' ) );
/**
* Fires after a user leaves a group.
*
* @since 1.0.0
*
* @param int $group_id ID of the group.
* @param int $user_id ID of the user leaving the group.
*/
do_action( 'groups_leave_group', $group_id, $user_id );
return true;
}
/**
* Add a user to a group.
*
* @since 1.0.0
*
* @param int $group_id ID of the group.
* @param int $user_id Optional. ID of the user. Defaults to the currently
* logged-in user.
* @return bool True on success, false on failure.
*/
function groups_join_group( $group_id, $user_id = 0 ) {
if ( empty( $user_id ) )
$user_id = bp_loggedin_user_id();
// Check if the user has an outstanding invite. If so, delete it.
if ( groups_check_user_has_invite( $user_id, $group_id ) )
groups_delete_invite( $user_id, $group_id );
// Check if the user has an outstanding request. If so, delete it.
if ( groups_check_for_membership_request( $user_id, $group_id ) )
groups_delete_membership_request( null, $user_id, $group_id );
// User is already a member, just return true.
if ( groups_is_user_member( $user_id, $group_id ) )
return true;
$new_member = new BP_Groups_Member;
$new_member->group_id = $group_id;
$new_member->user_id = $user_id;
$new_member->inviter_id = 0;
$new_member->is_admin = 0;
$new_member->user_title = '';
$new_member->date_modified = bp_core_current_time();
$new_member->is_confirmed = 1;
if ( !$new_member->save() )
return false;
$bp = buddypress();
if ( !isset( $bp->groups->current_group ) || !$bp->groups->current_group || $group_id != $bp->groups->current_group->id )
$group = groups_get_group( $group_id );
else
$group = $bp->groups->current_group;
// Record this in activity streams.
if ( bp_is_active( 'activity' ) ) {
groups_record_activity( array(
'type' => 'joined_group',
'item_id' => $group_id,
'user_id' => $user_id,
) );
}
/**
* Fires after a user joins a group.
*
* @since 1.0.0
*
* @param int $group_id ID of the group.
* @param int $user_id ID of the user joining the group.
*/
do_action( 'groups_join_group', $group_id, $user_id );
return true;
}
/**
* Update the last_activity meta value for a given group.
*
* @since 1.0.0
*
* @param int $group_id Optional. The ID of the group whose last_activity is
* being updated. Default: the current group's ID.
* @return false|null False on failure.
*/
function groups_update_last_activity( $group_id = 0 ) {
if ( empty( $group_id ) ) {
$group_id = buddypress()->groups->current_group->id;
}
if ( empty( $group_id ) ) {
return false;
}
groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() );
}
add_action( 'groups_join_group', 'groups_update_last_activity' );
add_action( 'groups_leave_group', 'groups_update_last_activity' );
add_action( 'groups_created_group', 'groups_update_last_activity' );
/** General Group Functions ***************************************************/
/**
* Get a list of group administrators.
*
* @since 1.0.0
*
* @param int $group_id ID of the group.
* @return array Info about group admins (user_id + date_modified).
*/
function groups_get_group_admins( $group_id ) {
return BP_Groups_Member::get_group_administrator_ids( $group_id );
}
/**
* Get a list of group moderators.
*
* @since 1.0.0
*
* @param int $group_id ID of the group.
* @return array Info about group admins (user_id + date_modified).
*/
function groups_get_group_mods( $group_id ) {
return BP_Groups_Member::get_group_moderator_ids( $group_id );
}
/**
* Fetch the members of a group.
*
* Since BuddyPress 1.8, a procedural wrapper for BP_Group_Member_Query.
* Previously called BP_Groups_Member::get_all_for_group().
*
* To use the legacy query, filter 'bp_use_legacy_group_member_query',
* returning true.
*
* @since 1.0.0
* @since 3.0.0 $group_id now supports multiple values. Only works if legacy query is not
* in use.
*
* @param array $args {
* An array of optional arguments.
* @type int|array|string $group_id ID of the group to limit results to. Also accepts multiple values
* either as an array or as a comma-delimited string.
* @type int $page Page of results to be queried. Default: 1.
* @type int $per_page Number of items to return per page of results. Default: 20.
* @type int $max Optional. Max number of items to return.
* @type array $exclude Optional. Array of user IDs to exclude.
* @type bool|int $exclude_admins_mods True (or 1) to exclude admins and mods from results. Default: 1.
* @type bool|int $exclude_banned True (or 1) to exclude banned users from results. Default: 1.
* @type array $group_role Optional. Array of group roles to include.
* @type string $search_terms Optional. Filter results by a search string.
* @type string $type Optional. Sort the order of results. 'last_joined', 'first_joined', or
* any of the $type params available in {@link BP_User_Query}. Default:
* 'last_joined'.
* }
* @return false|array Multi-d array of 'members' list and 'count'.
*/
function groups_get_group_members( $args = array() ) {
// Backward compatibility with old method of passing arguments.
if ( ! is_array( $args ) || func_num_args() > 1 ) {
_deprecated_argument( __METHOD__, '2.0.0', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) );
$old_args_keys = array(
0 => 'group_id',
1 => 'per_page',
2 => 'page',
3 => 'exclude_admins_mods',
4 => 'exclude_banned',
5 => 'exclude',
6 => 'group_role',
);
$args = bp_core_parse_args_array( $old_args_keys, func_get_args() );
}
$r = bp_parse_args( $args, array(
'group_id' => bp_get_current_group_id(),
'per_page' => false,
'page' => false,
'exclude_admins_mods' => true,
'exclude_banned' => true,
'exclude' => false,
'group_role' => array(),
'search_terms' => false,
'type' => 'last_joined',
), 'groups_get_group_members' );
// For legacy users. Use of BP_Groups_Member::get_all_for_group() is deprecated.
if ( apply_filters( 'bp_use_legacy_group_member_query', false, __FUNCTION__, func_get_args() ) ) {
$retval = BP_Groups_Member::get_all_for_group( $r['group_id'], $r['per_page'], $r['page'], $r['exclude_admins_mods'], $r['exclude_banned'], $r['exclude'] );
} else {
// Both exclude_admins_mods and exclude_banned are legacy arguments.
// Convert to group_role.
if ( empty( $r['group_role'] ) ) {
$r['group_role'] = array( 'member' );
if ( ! $r['exclude_admins_mods'] ) {
$r['group_role'][] = 'mod';
$r['group_role'][] = 'admin';
}
if ( ! $r['exclude_banned'] ) {
$r['group_role'][] = 'banned';
}
}
// Perform the group member query (extends BP_User_Query).
$members = new BP_Group_Member_Query( array(
'group_id' => $r['group_id'],
'per_page' => $r['per_page'],
'page' => $r['page'],
'group_role' => $r['group_role'],
'exclude' => $r['exclude'],
'search_terms' => $r['search_terms'],
'type' => $r['type'],
) );
// Structure the return value as expected by the template functions.
$retval = array(
'members' => array_values( $members->results ),
'count' => $members->total_users,
);
}
return $retval;
}
/**
* Get the member count for a group.
*
* @since 1.2.3
*
* @param int $group_id Group ID.
* @return int Count of confirmed members for the group.
*/
function groups_get_total_member_count( $group_id ) {
return BP_Groups_Group::get_total_member_count( $group_id );
}
/** Group Fetching, Filtering & Searching ************************************/
/**
* Get a collection of groups, based on the parameters passed.
*
* @since 1.2.0
* @since 2.6.0 Added `$group_type`, `$group_type__in`, and `$group_type__not_in` parameters.
* @since 2.7.0 Added `$update_admin_cache` and `$parent_id` parameters.
*
* @param array|string $args {
* Array of arguments. Supports all arguments of
* {@link BP_Groups_Group::get()}. Where the default values differ, they
* have been described here.
* @type int $per_page Default: 20.
* @type int $page Default: 1.
* }
* @return array See {@link BP_Groups_Group::get()}.
*/
function groups_get_groups( $args = '' ) {
$defaults = array(
'type' => false, // Active, newest, alphabetical, random, popular.
'order' => 'DESC', // 'ASC' or 'DESC'
'orderby' => 'date_created', // date_created, last_activity, total_member_count, name, random, meta_id.
'user_id' => false, // Pass a user_id to limit to only groups that this user is a member of.
'include' => false, // Only include these specific groups (group_ids).
'exclude' => false, // Do not include these specific groups (group_ids).
'parent_id' => null, // Get groups that are children of the specified group(s).
'slug' => array(), // Find a group or groups by slug.
'search_terms' => false, // Limit to groups that match these search terms.
'search_columns' => array(), // Select which columns to search.
'group_type' => '', // Array or comma-separated list of group types to limit results to.
'group_type__in' => '', // Array or comma-separated list of group types to limit results to.
'group_type__not_in' => '', // Array or comma-separated list of group types that will be excluded from results.
'meta_query' => false, // Filter by groupmeta. See WP_Meta_Query for syntax.
'show_hidden' => false, // Show hidden groups to non-admins.
'status' => array(), // Array or comma-separated list of group statuses to limit results to.
'per_page' => 20, // The number of results to return per page.
'page' => 1, // The page to return if limiting per page.
'update_meta_cache' => true, // Pre-fetch groupmeta for queried groups.
'update_admin_cache' => false,
'fields' => 'all', // Return BP_Groups_Group objects or a list of ids.
);
$r = bp_parse_args( $args, $defaults, 'groups_get_groups' );
$groups = BP_Groups_Group::get( array(
'type' => $r['type'],
'user_id' => $r['user_id'],
'include' => $r['include'],
'exclude' => $r['exclude'],
'slug' => $r['slug'],
'parent_id' => $r['parent_id'],
'search_terms' => $r['search_terms'],
'search_columns' => $r['search_columns'],
'group_type' => $r['group_type'],
'group_type__in' => $r['group_type__in'],
'group_type__not_in' => $r['group_type__not_in'],
'meta_query' => $r['meta_query'],
'show_hidden' => $r['show_hidden'],
'status' => $r['status'],
'per_page' => $r['per_page'],
'page' => $r['page'],
'update_meta_cache' => $r['update_meta_cache'],
'update_admin_cache' => $r['update_admin_cache'],
'order' => $r['order'],
'orderby' => $r['orderby'],
'fields' => $r['fields'],
) );
/**
* Filters the collection of groups based on parsed parameters.
*
* @since 1.2.0
*
* @param BP_Groups_Group $groups Object of found groups based on parameters.
* Passed by reference.
* @param array $r Array of parsed arguments used for group query.
* Passed by reference.
*/
return apply_filters_ref_array( 'groups_get_groups', array( &$groups, &$r ) );
}
/**
* Get the total group count for the site.
*
* @since 1.2.0
*
* @return int
*/
function groups_get_total_group_count() {
$count = wp_cache_get( 'bp_total_group_count', 'bp' );
if ( false === $count ) {
$count = BP_Groups_Group::get_total_group_count();
wp_cache_set( 'bp_total_group_count', $count, 'bp' );
}
return $count;
}
/**
* Get the IDs of the groups of which a specified user is a member.
*
* @since 1.0.0
*
* @param int $user_id ID of the user.
* @param int $pag_num Optional. Max number of results to return.
* Default: false (no limit).
* @param int $pag_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.
* }
*/
function groups_get_user_groups( $user_id = 0, $pag_num = 0, $pag_page = 0 ) {
if ( empty( $user_id ) )
$user_id = bp_displayed_user_id();
return BP_Groups_Member::get_group_ids( $user_id, $pag_num, $pag_page );
}
/**
* Get a list of groups of which the specified user is a member.
*
* Get a list of the groups to which this member belongs,
* filtered by group membership status and role.
* Usage examples: Used with no arguments specified,
*
* bp_get_user_groups( bp_loggedin_user_id() );
*
* returns an array of the groups in which the logged-in user
* is an unpromoted member. To fetch an array of all groups that
* the current user belongs to, in any membership role,
* member, moderator or administrator, use
*
* bp_get_user_groups( $user_id, array(
* 'is_admin' => null,
* 'is_mod' => null,
* ) );
*
* @since 2.6.0
*
* @param int $user_id ID of the user.
* @param array $args {
* Array of optional args.
* @param bool|null $is_confirmed Whether to return only confirmed memberships. Pass `null` to disable this
* filter. Default: true.
* @param bool|null $is_banned Whether to return only banned memberships. Pass `null` to disable this filter.
* Default: false.
* @param bool|null $is_admin Whether to return only admin memberships. Pass `null` to disable this filter.
* Default: false.
* @param bool|null $is_mod Whether to return only mod memberships. Pass `null` to disable this filter.
* Default: false.
* @param bool|null $invite_sent Whether to return only memberships with 'invite_sent'. Pass `null` to disable
* this filter. Default: false.
* @param string $orderby Field to order by. Accepts 'id' (membership ID), 'group_id', 'date_modified'.
* Default: 'group_id'.
* @param string $order Sort order. Accepts 'ASC' or 'DESC'. Default: 'ASC'.
* }
* @return array Array of matching group memberships, keyed by group ID.
*/
function bp_get_user_groups( $user_id, $args = array() ) {
$r = bp_parse_args( $args, array(
'is_confirmed' => true,
'is_banned' => false,
'is_admin' => false,
'is_mod' => false,
'invite_sent' => null,
'orderby' => 'group_id',
'order' => 'ASC',
), 'get_user_groups' );
$user_id = intval( $user_id );
$membership_ids = wp_cache_get( $user_id, 'bp_groups_memberships_for_user' );
if ( false === $membership_ids ) {
$membership_ids = BP_Groups_Member::get_membership_ids_for_user( $user_id );
wp_cache_set( $user_id, $membership_ids, 'bp_groups_memberships_for_user' );
}
// Prime the membership cache.
$uncached_membership_ids = bp_get_non_cached_ids( $membership_ids, 'bp_groups_memberships' );
if ( ! empty( $uncached_membership_ids ) ) {
$uncached_memberships = BP_Groups_Member::get_memberships_by_id( $uncached_membership_ids );
foreach ( $uncached_memberships as $uncached_membership ) {
wp_cache_set( $uncached_membership->id, $uncached_membership, 'bp_groups_memberships' );
}
}
// Assemble filter array for use in `wp_list_filter()`.
$filters = wp_array_slice_assoc( $r, array( 'is_confirmed', 'is_banned', 'is_admin', 'is_mod', 'invite_sent' ) );
foreach ( $filters as $filter_name => $filter_value ) {
if ( is_null( $filter_value ) ) {
unset( $filters[ $filter_name ] );
}
}
// Populate group membership array from cache, and normalize.
$groups = array();
$int_keys = array( 'id', 'group_id', 'user_id', 'inviter_id' );
$bool_keys = array( 'is_admin', 'is_mod', 'is_confirmed', 'is_banned', 'invite_sent' );
foreach ( $membership_ids as $membership_id ) {
$membership = wp_cache_get( $membership_id, 'bp_groups_memberships' );
// Sanity check.
if ( ! isset( $membership->group_id ) ) {
continue;
}
// Integer values.
foreach ( $int_keys as $index ) {
$membership->{$index} = intval( $membership->{$index} );
}
// Boolean values.
foreach ( $bool_keys as $index ) {
$membership->{$index} = (bool) $membership->{$index};
}
foreach ( $filters as $filter_name => $filter_value ) {
if ( ! isset( $membership->{$filter_name} ) || $filter_value != $membership->{$filter_name} ) {
continue 2;