-
Notifications
You must be signed in to change notification settings - Fork 10
/
extension.php
881 lines (713 loc) · 31.8 KB
/
extension.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
<?php
/**
*
* This file contains a reference user interface for hierarchical groups.
* One part is the Groups extension that adds the Member Groups tab to groups
* and allows creators to place new groups within the hierarchy.
* The other is an administrative and permissions interface for that feature
*
* This is not tied to the implementation of hierarchical groups contained in the other files
*
*/
/**
* Save the parent when creating a new group as early as possible
* This hook was added in BP 1.6, and must be called before the BP_Group_Extension class exists
*/
add_action( 'groups_create_group_step_save_group-details', 'bp_group_hierarchy_save_parent_selection' );
if( ! class_exists( 'BP_Group_Extension') ) {
// Groups component is not enabled; don't initialize the extension
return;
}
class BP_Groups_Hierarchy_Extension extends BP_Group_Extension {
var $visibility = 'public';
/**
* Disable metabox in BP 1.7 Group Edit screen until something is written to fill it :)
*/
var $enable_admin_item = false;
public function __construct() {
global $bp;
$this->name = __( 'Group Hierarchy', 'bp-group-hierarchy' );
$this->nav_item_name = apply_filters(
'bp_group_hierarchy_extension_tab_name',
get_site_option( 'bpgh_extension_nav_item_name', __('Member Groups %d','bp-group-hierarchy') ),
$bp->groups->current_group
);
if( isset( $bp->groups->current_group ) && $bp->groups->current_group ) {
$this->nav_item_name = str_replace( '%d', '<span>%d</span>', $this->nav_item_name );
// Only count subgroups if admin has a placeholder in the nav item name
if( strpos( $this->nav_item_name, '%d' ) !== FALSE )
$this->nav_item_name = sprintf($this->nav_item_name, BP_Groups_Hierarchy::get_total_subgroup_count( $bp->groups->current_group->id ) );
}
$this->slug = BP_GROUP_HIERARCHY_SLUG;
if( isset( $_COOKIE['bp_new_group_parent_id'] ) ) {
$bp->group_hierarchy->new_group_parent_id = $_COOKIE['bp_new_group_parent_id'];
add_action( 'bp_after_group_details_creation_step', array( &$this, 'add_parent_selection' ) );
}
$this->create_step_position = 6;
$this->nav_item_position = 61;
$this->subgroup_permission_options = array(
'anyone' => __('Anybody','bp-group-hierarchy'),
'noone' => __('Nobody','bp-group-hierarchy'),
'group_members' => __('only Group Members','bp-group-hierarchy'),
'group_admins' => __('only Group Admins','bp-group-hierarchy')
);
$bp->group_hierarchy->subgroup_permission_options = $this->subgroup_permission_options;
if(isset($bp->groups->current_group) && $bp->groups->current_group) {
$bp->groups->current_group->can_create_subitems = bp_group_hierarchy_can_create_subgroups();
}
$this->enable_nav_item = $this->enable_nav_item();
// BP 1.8+ initiation process -- for future compatibility
if( method_exists( $this, 'init' ) ) {
$args = array(
'name' => $this->name,
'slug' => BP_GROUP_HIERARCHY_SLUG,
'enable_nav_item' => $this->enable_nav_item(),
'nav_item_name' => $this->nav_item_name,
'nav_item_position' => $this->nav_item_position,
'screens' => array(
'create' => array(
'position' => $this->create_step_position
),
'edit' => array(
'name' => $this->name // Didn't want to pass an empty array
)
)
);
parent::init( $args );
}
}
function get_default_permission_option() {
return 'group_members';
}
function enable_nav_item() {
global $bp;
if( is_admin() ) return false;
if( ! is_object($bp->groups->current_group) ) return false;
/** Only display the nav item for admins, those who can create subgroups, or everyone if the group has subgroups */
if (
$bp->is_item_admin ||
$bp->groups->current_group->can_create_subitems ||
BP_Groups_Hierarchy::has_children( $bp->groups->current_group->id )
) {
return apply_filters( 'bp_group_hierarchy_show_member_groups', true );
}
return false;
}
function add_parent_selection() {
global $bp;
if(!bp_is_group_creation_step( 'group-details' )) {
return false;
}
$parent_group = new BP_Groups_Hierarchy( $bp->group_hierarchy->new_group_parent_id );
?>
<label for="group-parent_id"><?php _e( 'Parent Group', 'bp-group-hierarchy' ); ?></label>
<input type="hidden" name="group-parent_id" id="group-parent_id" value="<?php echo $parent_group->id ?>" />
<?php echo $parent_group->name ?>
<?php
}
function create_screen( $group_id = null ) {
global $bp;
if(!bp_is_group_creation_step( $this->slug )) {
return false;
}
$this_group = new BP_Groups_Hierarchy( $bp->groups->new_group_id );
if(isset($_COOKIE['bp_new_group_parent_id'])) {
$this_group->parent_id = $_COOKIE['bp_new_group_parent_id'];
$this_group->save();
}
$groups = BP_Groups_Hierarchy::get( 'alphabetical', null, null, 0, false, false, true, $bp->groups->new_group_id );
$site_root = new stdClass();
$site_root->id = 0;
$site_root->name = __( 'Site Root', 'bp-group-hierarchy' );
$display_groups = array(
$site_root
);
foreach($groups['groups'] as $group) {
$display_groups[] = $group;
}
/* deprecated */
$display_groups = apply_filters( 'bp_group_hierarchy_display_groups', $display_groups );
$display_groups = apply_filters( 'bp_group_hierarchy_available_parent_groups', $display_groups, $this_group );
?>
<label for="parent_id"><?php _e( 'Parent Group', 'bp-group-hierarchy' ); ?></label>
<select name="parent_id" id="parent_id">
<!--<option value="0"><?php _e( 'Site Root', 'bp-group-hierarchy' ); ?></option>-->
<?php foreach($display_groups as $group) { ?>
<option value="<?php echo $group->id ?>"<?php if($group->id == $this_group->parent_id) echo ' selected'; ?>><?php echo stripslashes( $group->name ); ?></option>
<?php } ?>
</select>
<?php
$subgroup_permission_options = apply_filters( 'bp_group_hierarchy_subgroup_permission_options', $this->subgroup_permission_options, $this_group );
$current_subgroup_permission = groups_get_groupmeta( $bp->groups->current_group->id, 'bp_group_hierarchy_subgroup_creators' );
if($current_subgroup_permission == '')
$current_subgroup_permission = $this->get_default_permission_option();
$permission_select = '<select name="allow_children_by" id="allow_children_by">';
foreach($subgroup_permission_options as $option => $text) {
$permission_select .= '<option value="' . $option . '"' . (($option == $current_subgroup_permission) ? ' selected' : '') . '>' . $text . '</option>' . "\n";
}
$permission_select .= '</select>';
?>
<p>
<label for="allow_children_by"><?php _e( 'Member Groups', 'bp-group-hierarchy' ); ?></label>
<?php printf( __( 'Allow %1$s to create %2$s', 'bp-group-hierarchy' ), $permission_select, __( 'Member Groups', 'bp-group-hierarchy' ) ); ?>
</p>
<?php
wp_nonce_field( 'groups_create_save_' . $this->slug );
}
function create_screen_save( $group_id = null ) {
global $bp;
check_admin_referer( 'groups_create_save_' . $this->slug );
setcookie( 'bp_new_group_parent_id', false, time() - 1000, COOKIEPATH );
/** save the selected parent_id */
$parent_id = (int)$_POST['parent_id'];
if(bp_group_hierarchy_can_create_subgroups( $bp->loggedin_user->id, $parent_id )) {
$bp->groups->current_group = new BP_Groups_Hierarchy( $bp->groups->new_group_id );
$bp->groups->current_group->parent_id = $parent_id;
$bp->groups->current_group->save();
}
/** save the selected subgroup permission setting */
$permission_options = apply_filters( 'bp_group_hierarchy_subgroup_permission_options', $this->subgroup_permission_options );
if(array_key_exists( $_POST['allow_children_by'], $permission_options )) {
$allow_children_by = $_POST['allow_children_by'];
} else {
$allow_children_by = $this->get_default_permission_option();
}
groups_update_groupmeta( $bp->groups->current_group->id, 'bp_group_hierarchy_subgroup_creators', $allow_children_by );
}
function edit_screen( $group_id = null ) {
global $bp;
if(!bp_is_group_admin_screen( $this->slug )) {
return false;
}
if(is_super_admin()) {
$exclude_groups = BP_Groups_Hierarchy::get_by_parent( $bp->groups->current_group->id );
if(count($exclude_groups['groups']) > 0) {
foreach($exclude_groups['groups'] as $key => $exclude_group) {
$exclude_groups['groups'][$key] = $exclude_group->id;
}
$exclude_groups = $exclude_groups['groups'];
} else {
$exclude_groups = array();
}
$exclude_groups[] = $bp->groups->current_group->id;
$groups = BP_Groups_Hierarchy::get( 'alphabetical', null, null, 0, false, false, true, $exclude_groups );
$site_root = new stdClass();
$site_root->id = 0;
$site_root->name = __( 'Site Root', 'bp-group-hierarchy' );
$display_groups = array(
$site_root
);
foreach($groups['groups'] as $group) {
$display_groups[] = $group;
}
/* deprecated */
$display_groups = apply_filters( 'bp_group_hierarchy_display_groups', $display_groups );
$display_groups = apply_filters( 'bp_group_hierarchy_available_parent_groups', $display_groups );
?>
<label for="parent_id"><?php _e( 'Parent Group', 'bp-group-hierarchy' ); ?></label>
<select name="parent_id" id="parent_id">
<?php foreach($display_groups as $group) { ?>
<option value="<?php echo $group->id ?>"<?php if($group->id == $bp->groups->current_group->parent_id) echo ' selected'; ?>><?php echo stripslashes($group->name); ?></option>
<?php } ?>
</select>
<?php
} else {
?>
<div id="message">
<p><?php _e('Only a site administrator can edit the group hierarchy.', 'bp-group-hierarchy' ); ?></p>
</div>
<?php
}
if(is_super_admin() || bp_group_is_admin()) {
$subgroup_permission_options = apply_filters( 'bp_group_hierarchy_subgroup_permission_options', $this->subgroup_permission_options );
$current_subgroup_permission = groups_get_groupmeta( $bp->groups->current_group->id, 'bp_group_hierarchy_subgroup_creators' );
if($current_subgroup_permission == '')
$current_subgroup_permission = $this->get_default_permission_option();
$permission_select = '<select name="allow_children_by" id="allow_children_by">';
foreach($subgroup_permission_options as $option => $text) {
$permission_select .= '<option value="' . $option . '"' . (($option == $current_subgroup_permission) ? ' selected' : '') . '>' . $text . '</option>' . "\n";
}
$permission_select .= '</select>';
?>
<p>
<label for="allow_children_by"><?php _e( 'Member Groups', 'bp-group-hierarchy' ); ?></label>
<?php printf( __( 'Allow %1$s to create %2$s', 'bp-group-hierarchy' ), $permission_select, __( 'Member Groups', 'bp-group-hierarchy' ) ); ?>
</p>
<p>
<input type="submit" class="button primary" id="save" name="save" value="<?php _e( 'Save Changes', 'bp-group-hierarchy' ); ?>" />
</p>
<?php
wp_nonce_field( 'groups_edit_save_' . $this->slug );
}
}
function edit_screen_save( $group_id = null ) {
global $bp;
if( !isset($_POST['save']) ) {
return false;
}
check_admin_referer( 'groups_edit_save_' . $this->slug );
/** save the selected subgroup permission setting */
$permission_options = apply_filters( 'bp_group_hierarchy_subgroup_permission_options', $this->subgroup_permission_options );
if(array_key_exists( $_POST['allow_children_by'], $permission_options )) {
$allow_children_by = $_POST['allow_children_by'];
} else if(groups_get_groupmeta( $bp->groups->current_group->id, 'bp_group_hierarchy_subgroup_creators' ) != '') {
$allow_children_by = groups_get_groupmeta( $bp->groups->current_group->id, 'bp_group_hierarchy_subgroup_creators' );
} else {
$allow_children_by = $this->get_default_permission_option();
}
groups_update_groupmeta( $bp->groups->current_group->id, 'bp_group_hierarchy_subgroup_creators', $allow_children_by );
if(is_super_admin()) {
/** save changed parent_id */
$parent_id = (int)$_POST['parent_id'];
if( bp_group_hierarchy_can_create_subgroups( $bp->loggedin_user->id, $bp->groups->current_group->id ) ) {
$bp->groups->current_group->parent_id = $parent_id;
$success = $bp->groups->current_group->save();
}
if( !$success ) {
bp_core_add_message( __( 'There was an error saving; please try again.', 'bp-group-hierarchy' ), 'error' );
} else {
bp_core_add_message( __( 'Group hierarchy settings saved successfully.', 'bp-group-hierarchy' ) );
}
}
bp_core_redirect( bp_get_group_admin_permalink( $bp->groups->current_group ) );
}
function display( $group_id = null ) {
global $bp, $groups_template;
$parent_template = $groups_template;
$hide_button = false;
if(isset($_REQUEST['grpage'])) {
$page = (int)$_REQUEST['grpage'];
} else if(!is_numeric($page)) {
$page = 1;
} else {
$page = (int)$page;
}
/** Respect BuddyPress group creation restriction */
if(function_exists('bp_user_can_create_groups')) {
$hide_button = !bp_user_can_create_groups();
}
bp_has_groups_hierarchy(array(
'type' => 'alphabetical',
'parent_id' => $bp->groups->current_group->id,
'page' => $page
));
?>
<div class="group">
<?php if(($bp->is_item_admin || $bp->groups->current_group->can_create_subitems) && !$hide_button) { ?>
<div class="generic-button group-button">
<a title="<?php printf( __( 'Create a %s', 'bp-group-hierarchy' ),__( 'Member Group', 'bp-group-hierarchy' ) ) ?>" href="<?php echo $bp->root_domain . '/' . bp_get_groups_root_slug() . '/' . 'create' .'/?parent_id=' . $bp->groups->current_group->id ?>"><?php printf( __( 'Create a %s', 'bp-group-hierarchy' ),__( 'Member Group', 'bp-group-hierarchy' ) ) ?></a>
</div><br /><br />
<?php } ?>
<?php if($groups_template && count($groups_template->groups) > 0) : ?>
<div id="pag-top" class="pagination">
<div class="pag-count" id="group-dir-count-top">
<?php bp_groups_pagination_count() ?>
</div>
<div class="pagination-links" id="group-dir-pag-top">
<?php bp_groups_pagination_links() ?>
</div>
</div>
<ul id="groups-list" class="item-list">
<?php while ( bp_groups() ) : bp_the_group(); ?>
<?php $subgroup = $groups_template->group; ?>
<?php if($subgroup->status == 'hidden' && !( groups_is_user_member( $bp->loggedin_user->id, $subgroup->id ) || groups_is_user_admin( $bp->loggedin_user->id, $bp->groups->current_group->id ) ) ) continue; ?>
<li id="tree-childof_<?php bp_group_id() ?>">
<div class="item-avatar">
<a href="<?php bp_group_permalink() ?>"><?php bp_group_avatar( 'type=thumb&width=50&height=50' ) ?></a>
</div>
<div class="item">
<div class="item-title"><a href="<?php bp_group_permalink() ?>"><?php bp_group_name() ?></a></div>
<div class="item-meta"><span class="activity"><?php printf( __( 'active %s', 'buddypress' ), bp_get_group_last_active() ); ?></span></div>
<div class="item-desc"><?php bp_group_description_excerpt() ?></div>
<?php do_action( 'bp_directory_groups_item' ) ?>
</div>
<div class="action">
<?php do_action( 'bp_directory_groups_actions' ) ?>
<div class="meta">
<?php bp_group_type() ?> / <?php bp_group_member_count() ?>
</div>
</div>
<div class="clear"></div>
</li>
<?php endwhile; ?>
</ul>
<div id="pag-bottom" class="pagination">
<div class="pag-count" id="group-dir-count-bottom">
<?php bp_groups_pagination_count() ?>
</div>
<div class="pagination-links" id="group-dir-pag-bottom">
<?php bp_groups_pagination_links() ?>
</div>
</div>
<script type="text/javascript">
jQuery('#nav-hierarchy-personal-li').attr('id','group-hierarchy-personal-li');
jQuery('#nav-hierarchy-groups-li').attr('id','group-hierarchy-group-li');
</script>
<?php else: ?>
<p><?php _e('No member groups were found.','bp-group-hierarchy'); ?></p>
<?php endif; ?>
</div>
<?php
// reset the $groups_template global and continue with the page
$groups_template = $parent_template;
}
}
bp_register_group_extension( 'BP_Groups_Hierarchy_Extension' );
/**
*
* Group creation permission / restriction functions
*
*/
/**
* Store the ID of the group the user selected as the parent for group creation
*/
function bp_group_hierarchy_set_parent_id_cookie() {
global $bp;
if( bp_is_groups_component() && $bp->current_action == 'create' && isset( $_REQUEST['parent_id'] ) && $_REQUEST['parent_id'] != 0 ) {
if( bp_group_hierarchy_can_create_subgroups( $bp->loggedin_user->id, (int)$_REQUEST['parent_id'] ) ) {
setcookie( 'bp_new_group_parent_id', (int)$_REQUEST['parent_id'], time() + 1000, COOKIEPATH );
} else {
do_action( 'bp_group_hierarchy_unauthorized_parent', (int)$_REQUEST['parent_id'] );
}
}
}
add_action( 'bp_group_hierarchy_route_requests', 'bp_group_hierarchy_set_parent_id_cookie' );
/**
* Save the parent group even before the extension has loaded in BP 1.6+
*/
function bp_group_hierarchy_save_parent_selection() {
global $bp;
if( isset( $_COOKIE['bp_new_group_parent_id'] ) ) {
$this_group = new BP_Groups_Hierarchy( $bp->groups->new_group_id );
$this_group->parent_id = $_COOKIE['bp_new_group_parent_id'];
$this_group->save();
}
}
/**
* Check whether the user is allowed to create subgroups of the selected group
* and to see the Create a Member Group button
* @param int UserID ID of the user whose access is being checked (or current user if omitted)
* @param int GroupID ID of the group being checked (or group being displayed if omitted)
* @return bool TRUE if permitted, FALSE otherwise
*/
function bp_group_hierarchy_can_create_subgroups( $user_id = null, $group_id = null ) {
global $bp;
if(is_null($user_id)) {
$user_id = $bp->loggedin_user->id;
}
if(is_null($group_id)) {
$group_id = $bp->groups->current_group->id;
}
if(is_super_admin()) {
return true;
}
if($group_id == 0) {
$subgroup_permission = get_site_option('bpgh_extension_toplevel_group_permission','anyone');
} else {
$subgroup_permission = groups_get_groupmeta( $group_id, 'bp_group_hierarchy_subgroup_creators');
}
if($subgroup_permission == '') {
$subgroup_permission = BP_Groups_Hierarchy_Extension::get_default_permission_option();
}
switch($subgroup_permission) {
case 'noone':
return false;
break;
case 'anyone':
return (is_user_logged_in() || get_site_option( 'bpgh_extension_allow_anon_subgroups', false ) );
break;
case 'group_members':
return groups_is_user_member( $user_id, $group_id );
break;
case 'group_admins':
return groups_is_user_admin( $user_id, $group_id );
break;
default:
if(
has_filter('bp_group_hierarchy_enforce_subgroup_permission_' . $subgroup_permission ) &&
apply_filters( 'bp_group_hierarchy_enforce_subgroup_permission_' . $subgroup_permission, false, $user_id, $group_id ))
{
return true;
}
break;
}
return false;
}
/**
* Enforce subgroup creation restrictions in parent group selection boxes
*/
function bp_group_hierarchy_enforce_subgroup_permissions( $groups ) {
global $bp;
/** super admins can add subgroups to any group */
if(is_super_admin()) {
return $groups;
}
if($allowed_groups = wp_cache_get( 'subgroup_creation_permitted_' . $bp->loggedin_user->id, 'bp_group_hierarchy' )) {
return $allowed_groups;
}
$allowed_groups = array();
foreach($groups as $group) {
if(bp_group_hierarchy_can_create_subgroups( $bp->loggedin_user->id, $group->id )) {
$allowed_groups[] = $group;
}
}
wp_cache_set( 'subgroup_creation_permitted_' . $bp->loggedin_user->id, $allowed_groups, 'bp_group_hierarchy' );
return $allowed_groups;
}
add_filter( 'bp_group_hierarchy_available_parent_groups', 'bp_group_hierarchy_enforce_subgroup_permissions' );
/**
*
* Hierarchical Group Display functions
* These are controlled by admin settings - see admin section, below
*/
function bp_group_hierarchy_tab() {
global $bp;
?>
<li id="tree-all"><a href="<?php echo bp_get_root_domain() . '/' . bp_get_groups_root_slug() . '/?tree' ?>"><?php echo $bp->group_hierarchy->extension_settings['group_tree_name'] ?></a></li>
<?php
}
/**
* Functions for new 'tree' object-based hierachy display, which supports a new template
*/
/**
* Filter group results when requesting as part of the tree
*/
function bp_group_hierarchy_display( $query_string, $object, $parent_id = 0 ) {
if($object == 'tree') {
if(isset($_POST['scope']) && $_POST['scope'] != 'all') {
$parent_id = substr($_POST['scope'],8);
$parent_id = (int)$parent_id;
}
$query_string .= '&parent_id=' . $parent_id;
if($parent_id != 0) {
$query_string .= '&per_page=100';
}
add_filter( 'groups_get_groups', 'bp_group_hierarchy_get_groups_tree', 10, 2 );
}
return $query_string;
}
add_filter( 'bp_ajax_querystring', 'bp_group_hierarchy_display', 20, 2 );
/**
* Load the tree loop instead of the group loop when requested as part of the tree
*/
function bp_group_hierarchy_object_template_loader() {
$object = esc_attr( $_POST['object'] );
if($object == 'tree') {
if($template = apply_filters('bp_located_template',locate_template( array( "$object/$object-loop.php" ), false ), "$object/$object-loop.php" )) {
load_template($template);
die();
} else {
bp_group_hierarchy_debug('Failed to find loop template for object: ' . $object);
}
}
}
add_action( 'wp_ajax_tree_filter', 'bp_group_hierarchy_object_template_loader' );
add_action( 'wp_ajax_nopriv_tree_filter', 'bp_group_hierarchy_object_template_loader' );
function bp_group_hierarchy_display_member_group_pages() {
die(BP_Groups_Hierarchy_Extension::display($_POST['page']));
}
add_action( 'wp_ajax_group_filter', 'bp_group_hierarchy_display_member_group_pages');
add_action( 'wp_ajax_nopriv_group_filter', 'bp_group_hierarchy_display_member_group_pages');
/**
* Enable loading template files from the plugin directory
* This plugin only has template files for group pages, so pass on any requests for other components
*/
function bp_group_hierarchy_load_template_filter( $found_template, $templates ) {
/** Starting in BP 1.6, group list page (or maybe AJAX requests from it) is not in the groups component */
if ( ! bp_is_groups_component() && ! isset( $_POST['object'] ) )
return $found_template;
$filtered_templates = array();
foreach ( (array) $templates as $template ) {
if ( file_exists( STYLESHEETPATH . '/' . $template ) ) {
$filtered_templates[] = STYLESHEETPATH . '/' . $template;
} else if ( file_exists( TEMPLATEPATH . '/' . $template ) ) {
$filtered_templates[] = TEMPLATEPATH . '/' . $template;
} else if ( file_exists( dirname( __FILE__ ) . '/templates/' . $template ) ) {
$filtered_templates[] = dirname( __FILE__ ) . '/templates/' . $template;
} else {
bp_group_hierarchy_debug( 'Could not locate the requested template file: ' . $template );
}
}
if(count($filtered_templates) == 0 ) {
return $found_template;
}
$found_template = $filtered_templates[0];
return $found_template;
}
add_filter( 'bp_located_template', 'bp_group_hierarchy_load_template_filter', 10, 2 );
/**
* Restrict group listing to top-level groups
*/
function bp_group_hierarchy_get_groups_tree( $groups, $params, $parent_id = 0 ) {
global $bp, $groups_template;
if( $_POST['object'] == 'tree') {
if ( isset($_POST['scope']) && $_POST['scope'] != 'all' ) {
$parent_id = substr( $_POST['scope'], 8 );
$parent_id = (int)$parent_id;
}
}
/**
* Replace retrieved list with toplevel groups
* unless on a group page (member groups list), viewing "My Groups" or a meta_query filter has been applied
*/
if( ! isset( $bp->groups->current_group->id ) && ! $params['user_id'] ) {
/** remove search placeholder text for BP 1.5 */
// if( function_exists( 'bp_get_search_default_text' ) && trim( $params['search_terms'] ) == bp_get_search_default_text( 'groups' ) ) $params['search_terms'] = '';
// If search terms or a meta query is set, and parent_id is 0, let BP build the results list.
// If parent_id is not 0, this is a subgroup request, and group hierarchy needs to build the list.
if ( $params['search_terms'] || $params['meta_query'] ) {
if ( $parent_id ) {
$params['parent_id'] = $parent_id;
$groups = bp_group_hierarchy_get_by_hierarchy( $params );
}
} else {
$params['parent_id'] = $parent_id;
$toplevel_groups = bp_group_hierarchy_get_by_hierarchy( $params );
$groups = $toplevel_groups;
}
}
return $groups;
}
/**
* Strip the SPAN tags from the HTML title
*/
function bp_group_hierarchy_clean_title( $full_title ) {
return strip_tags( html_entity_decode( $full_title ) );
}
/**
* Change the HTML title to reflect custom Group Tree name
*/
function bp_group_hierarchy_group_tree_title( $title, $sep, $sep_location = null ) {
global $bp;
if($sep_location != null) {
return str_replace(
sprintf( __( '%s Directory', 'buddypress' ), bp_get_name_from_root_slug() ),
bp_group_hierarchy_clean_title( $bp->group_hierarchy->extension_settings['group_tree_name'] ),
$title
);
}
// I think this is left over from BP 1.2, so just return the title
return $title;
}
/************************************************************
* Enforce toplevel group creation restrictions on the UI
*/
/**
* If the user doesn't have any place to create a new group, don't let him create a group
* @param boolean Return control behavior if user cannot create groups - if TRUE, return FALSE; if FALSE, die
* TODO: this parameter is poorly named / implemented
*/
function bp_group_hierarchy_assert_parent_available( $return = false ) {
global $bp;
if(is_super_admin()) return true;
if( $cache_result = wp_cache_get( $bp->loggedin_user->id, 'bpgh_has_available_parent_group' ) ) {
if( $cache_result == 'true' ) {
return true;
}
if( $return ) {
return false;
} else {
wp_die( __( 'Sorry, you are not allowed to create groups.', 'buddypress' ), __( 'Sorry, you are not allowed to create groups.', 'buddypress' ) );
}
}
$group_permission = get_site_option('bpgh_extension_toplevel_group_permission');
if(
$group_permission == 'anyone' ||
(has_filter('bp_group_hierarchy_enforce_subgroup_permission_' . $group_permission ) &&
apply_filters( 'bp_group_hierarchy_enforce_subgroup_permission_' . $group_permission, false, $bp->loggedin_user->id, 0 ))
) {
/** If the user can create top-level groups, we're done looking */
wp_cache_set( $bp->loggedin_user->id, 'true', 'bpgh_has_available_parent_group' );
return true;
}
$user_groups = groups_get_groups(array('user_id'=>$bp->loggedin_user->id));
foreach($user_groups['groups'] as $group) {
if( bp_group_hierarchy_can_create_subgroups( $bp->loggedin_user->id, $group->id ) ) {
/** If the user can create subgroups here, we're done looking */
wp_cache_set( $bp->loggedin_user->id, 'true', 'bpgh_has_available_parent_group' );
return true;
}
}
wp_cache_set( $bp->loggedin_user->id, 'false', 'bpgh_has_available_parent_group' );
if($return) {
return false;
} else {
wp_die( __( 'Sorry, you are not allowed to create groups.', 'buddypress' ), __( 'Sorry, you are not allowed to create groups.', 'buddypress' ) );
}
}
add_action( 'bp_before_create_group', 'bp_group_hierarchy_assert_parent_available' );
/**
* (BP 1.5+) Hide the Create a New Group button if the user doesn't have a place to create new groups
*/
function bp_group_hierarchy_can_create_any_group( $permitted, $global_setting ) {
return $permitted && bp_group_hierarchy_assert_parent_available(true);
}
add_filter( 'bp_user_can_create_groups', 'bp_group_hierarchy_can_create_any_group', 10, 2 );
/**
* (BP 1.7+) Add plugin templates folder to list of available template paths
*/
function bp_group_hierarchy_register_template_location() {
return dirname( __FILE__ ) . '/templates/';
}
/**
* (BP 1.7+) Replace groups/index with tree/index-compat depending on user settings
*/
function bp_group_hierarchy_maybe_replace_group_loop_template( $templates, $slug, $name ) {
if( 'groups/index' != $slug )
return $templates;
return array( 'tree/index-compat.php' );
}
/**
* Get the party started
*/
function bp_group_hierarchy_extension_init() {
global $bp;
add_action( 'wp_ajax_groups_tree_filter', 'bp_dtheme_object_template_loader' );
add_action( 'wp_ajax_nopriv_groups_tree_filter', 'bp_dtheme_object_template_loader' );
/** Register templates folder for theme compatibility support when available */
if( function_exists( 'bp_register_template_stack' ) )
bp_register_template_stack( 'bp_group_hierarchy_register_template_location' );
$bp->group_hierarchy->extension_settings = array(
'show_group_tree' => get_site_option( 'bpgh_extension_show_group_tree', false ),
'hide_group_list' => get_site_option( 'bpgh_extension_hide_group_list', false ),
'nav_item_name' => get_site_option( 'bpgh_extension_nav_item_name', __('Member Groups (%d)','bp-group-hierarchy') ),
'group_tree_name' => get_site_option( 'bpgh_extension_group_tree_name', __('Group Tree','bp-group-hierarchy') )
);
// Set a cookie if hide_group_list is true, used in hierarchy.js
if ( $bp->group_hierarchy->extension_settings['hide_group_list'] && !isset( $_COOKIE['bp_group_hierarchy_hide_group_list'] ) )
setcookie( 'bp_group_hierarchy_hide_group_list', 1 , 0, COOKIEPATH );
wp_register_script( 'bp-group-hierarchy-tree-script', plugins_url( 'includes/hierarchy.js', __FILE__ ), array('jquery') );
/** Load the hierarchy.css file from the user's theme, if available */
if( $hierarchy_css = apply_filters( 'bp_located_template', locate_template( array( '_inc/css/hierarchy.css' ), false ), '_inc/css/hierarchy.css' ) ) {
// Detect when loading CSS from the plugin dir and rewrite with plugins_url for better MS / symlink support
if( 0 === strpos( $hierarchy_css, dirname( __FILE__ ) ) )
$hierarchy_css = plugins_url( 'templates/_inc/css/hierarchy.css', __FILE__ );
wp_register_style( 'bp-group-hierarchy-tree-style', str_replace(array(substr(ABSPATH,0,-1),'\\'), array('','/'), $hierarchy_css) );
}
if(bp_is_groups_component() && $bp->current_action == '' && $bp->group_hierarchy->extension_settings['hide_group_list']) {
add_filter( 'groups_get_groups', 'bp_group_hierarchy_get_groups_tree', 10, 2 );
add_filter( 'wp_title', 'bp_group_hierarchy_group_tree_title', 10, 3 );
if( ! isset( $_POST['object'] ) ) {
wp_enqueue_script('bp-group-hierarchy-tree-script');
wp_enqueue_style('bp-group-hierarchy-tree-style');
/**
* Override BP's default group index with the tree
*/
if(
! class_exists( 'BP_Theme_Compat' ) ||
current_theme_supports('buddypress') ||
in_array( 'bp-default', array( get_stylesheet(), get_template() ) )
) {
add_filter( 'groups_template_directory_groups', create_function( '$template', 'return "tree/index";' ) );
} else {
add_filter( 'bp_get_template_part', 'bp_group_hierarchy_maybe_replace_group_loop_template', 10, 3 );
}
}
} else if(bp_is_groups_component() && $bp->current_action == '' && $bp->group_hierarchy->extension_settings['show_group_tree']) {
wp_enqueue_script('bp-group-hierarchy-tree-script');
wp_enqueue_style('bp-group-hierarchy-tree-style');
add_action( 'bp_groups_directory_group_filter', 'bp_group_hierarchy_tab' );
}
}
add_action( 'init', 'bp_group_hierarchy_extension_init' );
?>