This repository has been archived by the owner on Aug 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bp-xprofile-for-user-groups.php
1007 lines (816 loc) · 29 KB
/
bp-xprofile-for-user-groups.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
/**
* The BP XProfile For User Groups Plugin
*
* Requires BP 2.1
*
* @package BP XProfile For User Groups
* @subpackage Main
*
* @todo Field visibility status: 'This field can be seen by everyone' is not true
* for fields that are assigned to user groups (or for their fieldgroup)
* @todo Support BP Group Hierarchy
*/
/**
* Plugin Name: BP XProfile For User Groups
* Description: Manage user group specific profile field(group)s in BuddyPress
* Plugin URI: https://github.com/lmoffereins/bp-xprofile-for-user-groups
* Version: 1.1.2
* Author: Laurens Offereins
* Author URI: https://github.com/lmoffereins
* Text Domain: bp-xprofile-for-user-groups
* Domain Path: /languages/
* GitHub Plugin URI: lmoffereins/bp-xprofile-for-user-groups
*/
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'BP_XProfile_For_User_Groups' ) ) :
/**
* Main Plugin Class
*
* @since 1.0.0
*/
final class BP_XProfile_For_User_Groups {
/**
* Setup and return the singleton pattern
*
* @since 1.1.0
*
* @uses BP_XProfile_For_User_Groups::setup_actions()
* @return BP_XProfile_For_User_Groups
*/
public static function instance() {
// Store the instance locally
static $instance = null;
if ( null === $instance ) {
$instance = new BP_XProfile_For_User_Groups;
$instance->setup_globals();
$instance->setup_actions();
}
// Always return the instance
return $instance;
}
/**
* Setup plugin structure and hooks
*
* @since 1.0.0
*/
private function __construct() { /* Do nothing here */ }
/**
* Setup default class globals
*
* @since 1.1.0
*/
private function setup_globals() {
/** Version **************************************************/
$this->version = '1.1.2';
/** Plugin ***************************************************/
$this->file = __FILE__;
$this->basename = plugin_basename( $this->file );
$this->plugin_dir = plugin_dir_path( $this->file );
$this->plugin_url = plugin_dir_url( $this->file );
// Languages
$this->lang_dir = trailingslashit( $this->plugin_dir . 'languages' );
/** Misc *****************************************************/
$this->domain = 'bp-xprofile-for-user-groups';
}
/**
* Setup default plugin actions and filters
*
* @since 1.1.0
*
* @uses bp_is_active() To check if groups or xprofile component is active
*/
private function setup_actions() {
// Require BP 2.1 and both the Groups and XProfile components
if ( version_compare( buddypress()->version, '2.1', '<' ) || ! bp_is_active( 'groups' ) || ! bp_is_active( 'xprofile' ) )
return;
// Plugin
add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
// Fields & fieldgroup filters
add_filter( 'bp_xprofile_get_hidden_fields_for_user', array( $this, 'filter_hidden_fields' ), 10, 3 );
add_filter( 'bp_xprofile_get_groups', array( $this, 'filter_fieldgroups' ), 10, 2 );
// Handle xprofile meta
add_action( 'xprofile_group_after_submitbox', array( $this, 'fieldgroup_display_metabox' ) );
add_action( 'xprofile_group_after_save', array( $this, 'fieldgroup_save_metabox' ) );
add_action( 'xprofile_field_after_submitbox', array( $this, 'field_display_metabox' ) );
add_action( 'xprofile_field_after_save', array( $this, 'field_save_metabox' ) );
add_action( 'bp_admin_head', array( $this, 'admin_scripts' ) );
// Fire plugin loaded hook
do_action( 'bp_xprofile_for_user_groups_loaded' );
}
/** Plugin ****************************************************************/
/**
* Load the translation file for current language
*
* Note that custom translation files inside the Plugin folder will
* be removed on Plugin updates. If you're creating custom translation
* files, please use the global language folder.
*
* @since 1.1.0
*
* @uses apply_filters() Calls 'plugin_locale' with {@link get_locale()} value
* @uses load_textdomain() To load the textdomain
* @uses load_plugin_textdomain() To load the plugin textdomain
*/
public function load_textdomain() {
// Traditional WordPress plugin locale filter
$locale = apply_filters( 'plugin_locale', get_locale(), $this->domain );
$mofile = sprintf( '%1$s-%2$s.mo', $this->domain, $locale );
// Setup paths to current locale file
$mofile_local = $this->lang_dir . $mofile;
$mofile_global = WP_LANG_DIR . '/bp-xprofile-for-user-groups/' . $mofile;
// Look in global /wp-content/languages/bp-xprofile-for-user-groups folder first
load_textdomain( $this->domain, $mofile_global );
// Look in global /wp-content/languages/plugins/ and local plugin languages folder
load_plugin_textdomain( $this->domain, false, 'bp-xprofile-for-user-groups/languages' );
}
/** Field & Fieldgroup Filters ********************************************/
/**
* Return the field ids that are not visible for the displayed and current user
*
* First, the displayed user must be a member of both the fieldgroup's and the
* field's user groups in order to show the field. Second, the loggedin user
* must be a member of both to show the field. If either one fails the user
* group membership, the field is added to the hidden fields collection.
*
* @since 1.0.0
*
* @param array $hidden_fields Hidden field ids
* @param int $displayed_user_id Displayed user ID
* @param int $current_user_id Loggedin user ID
* @return array Hidden field ids
*/
public function filter_hidden_fields( $hidden_fields, $displayed_user_id, $current_user_id ) {
global $wpdb, $bp;
// Current user is logged in
if ( ! empty( $current_user_id ) ) {
// Hidden = All - Visible for displayed user AND current user
$all_fields = array_map( 'intval', (array) $wpdb->get_col( "SELECT id FROM {$bp->profile->table_name_fields}" ) );
foreach ( $all_fields as $k => $field_id ) {
// Is displayed user not a member? Remove field
if ( ! $this->is_user_field_member( $field_id, $displayed_user_id ) ) {
$hidden_fields[] = $field_id;
continue;
}
// Is loggedin user not an admin and he cannot view the field? Hide field
if ( ! bp_current_user_can( 'bp_moderate' ) && ! $this->can_user_view_field( $field_id, $current_user_id ) ) {
$hidden_fields[] = $field_id;
continue;
}
}
// Current user is not logged in, so exclude all user group assigned fields
} else {
// Query all fieldgroups and fields with for-user-groups meta
$objects = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_meta} WHERE ( object_type = %s OR object_type = %s ) AND meta_key = %s", 'group', 'field', 'for-user-groups' ) );
// Loop the objects with meta
foreach ( $objects as $item ) {
// Skip primary field(group)
if ( 1 === $item->object_id )
continue;
// Skip if field is already hidden
if ( 'field' == $item->object_type && in_array( $item->object_id, $hidden_fields ) )
continue;
// Prepare array meta value
$groups = maybe_unserialize( $item->meta_value );
// Bail when no user groups are assigned
if ( empty( $groups ) )
continue;
// Object has user groups. Check object type to take action
switch ( $item->object_type ) {
// Fieldgroup
case 'group' :
// Get fieldgroup's field ids
$fields = $wpdb->get_col( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_fields} WHERE group_id = %d", $item->object_id ) );
// Hide all its fields
foreach ( $fields as $field_id ) {
$hidden_fields[] = $field_id;
}
break;
// Field
case 'field' :
// Hide single field
$hidden_fields[] = $item->object_id;
break;
}
}
}
// Sanitize return value
$hidden_fields = array_unique( $hidden_fields );
return $hidden_fields;
}
/**
* Return the fieldgroups filtered for the current user's group membership
*
* First, the displayed user must be a member to show the fieldgroup. Second,
* the loggedin user must be a member to show the fieldgroup. If either one
* fails the fieldgroup's user group membership, it's removed.
*
* Since BP 2.1.0.
*
* @since 1.0.0
*
* @uses BP_XProfile_For_User_Groups::is_user_fieldgroup_member()
* @uses bp_displayed_user_id()
* @uses bp_loggedin_user_id()
*
* @param array $groups Fieldgroup objects
* @param array $args Query arguments
* @return array Field groups
*/
public function filter_fieldgroups( $groups, $args ) {
// Loop all groups
foreach ( $groups as $k => $group ) {
// Keep the primary fieldgroup
if ( 1 === $group->id )
continue;
// Is displayed user not a member? Remove fieldgroup
if ( ! $this->is_user_fieldgroup_member( $group->id, isset( $args['user_id'] ) ? $args['user_id'] : bp_displayed_user_id() ) ) {
unset( $groups[$k] );
continue;
}
// Is loggedin user not an admin and he cannot view the fieldgroup? Hide fieldgroup
if ( ! bp_current_user_can( 'bp_moderate' ) && ! $this->can_user_view_fieldgroup( $group->id ) ) {
unset( $groups[$k] );
continue;
}
}
// Reorder nicely
$groups = array_values( $groups );
return $groups;
}
/** Is User Member ********************************************************/
/**
* Return whether the user is member of the fieldgroup's user groups
*
* @since 1.0.0
*
* @uses bp_displayed_user_id()
* @uses groups_get_groups()
* @uses BP_XProfile_For_User_Groups::get_fieldgroup_user_groups_having()
*
* @param int $fieldgroup_id Field group ID
* @param int $user_id Optional. User ID. Defaults to the displayed user.
* @return bool User is field group's user group member
*/
public function is_user_fieldgroup_member( $fieldgroup_id, $user_id = 0 ) {
// Bail if this is the primary fieldgroup
if ( 1 === $fieldgroup_id )
return true;
// Default to displayed user
if ( ! is_numeric( $user_id ) ) {
$user_id = bp_displayed_user_id();
}
// Get fieldgroups' having groups ids
$group_ids = $this->get_fieldgroup_user_groups_having( $fieldgroup_id );
// Groups are assigned
if ( ! empty( $group_ids ) ) {
// Get user's memberships limited to fieldgroup's user groups
$groups = groups_get_groups( array(
'user_id' => $user_id,
'include' => $group_ids,
'show_hidden' => true,
'per_page' => false,
'populate_extras' => false,
) );
return (bool) $groups['groups'];
// No groups were assigned, so user has access
} else {
return true;
}
}
/**
* Return whether the user is member of the field's user groups
*
* @since 1.0.0
*
* @uses bp_displayed_user_id()
* @uses BP_XProfile_For_User_Groups::is_user_fieldgroup_member()
* @uses groups_get_groups()
* @uses BP_XProfile_For_User_Groups::get_field_user_groups_having()
*
* @param int $fieldgroup_id Field group ID
* @param int $user_id Optional. User ID. Defaults to the displayed user.
* @param bool $check_fieldgroup Optional. Whether to do an early parent fieldgroup's membership check.
* @return bool User is field's user group member
*/
public function is_user_field_member( $field_id, $user_id = 0, $check_fieldgroup = true ) {
// Bail if this is the primary field
if ( 1 === $field_id || ( is_object( $field_id ) && 1 === $field_id->id ) )
return true;
// Default to displayed user
if ( ! is_numeric( $user_id ) ) {
$user_id = bp_displayed_user_id();
}
// Check parent fieldgroup membership
if ( true === $check_fieldgroup ) {
// Get the field object to find the fieldgroup ID
if ( is_object( $field_id ) ) {
$field = $field_id;
} else {
$field = new BP_XProfile_Field( $field_id );
}
// Bail early if user is not member of fieldgroup's user groups
if ( ! $this->is_user_fieldgroup_member( $field->group_id, $user_id ) ) {
return false;
}
}
// Get field ID
if ( is_object( $field_id ) )
$field_id = $field_id->id;
// Get field's having groups ids
$group_ids = $this->get_field_user_groups_having( $field_id );
// Groups are assigned
if ( ! empty( $group_ids ) ) {
// Get user's memberships limited to field's user groups
$groups = groups_get_groups( array(
'user_id' => $user_id,
'include' => $group_ids,
'show_hidden' => true,
'per_page' => false,
'populate_extras' => false,
) );
return (bool) $groups['groups'];
// No groups were assigned, so user has access
} else {
return true;
}
}
/** Can User View *********************************************************/
/**
* Return whether the user can view the fieldgroup
*
* @since 1.1.0
*
* @uses bp_loggedin_user_id()
* @uses groups_get_groups()
* @uses BP_XProfile_For_User_Groups::is_user_fieldgroup_member()
* @uses BP_XProfile_For_User_Groups::get_fieldgroup_user_groups_viewing()
*
* @param int $fieldgroup_id Field group ID
* @param int $user_id Optional. User ID. Defaults to the current user.
* @return bool User can view the fieldgroup
*/
public function can_user_view_fieldgroup( $fieldgroup_id, $user_id = 0 ) {
// Bail if this is the primary fieldgroup
if ( 1 === $fieldgroup_id )
return true;
// Default to current user
if ( empty( $user_id ) || ! is_numeric( $user_id ) ) {
$user_id = bp_loggedin_user_id();
}
// Fieldgroup members can always view their data
if ( $this->is_user_fieldgroup_member( $fieldgroup_id, $user_id ) )
return true;
// Get fieldgroups' viewing groups ids
$group_ids = $this->get_fieldgroup_user_groups_viewing( $fieldgroup_id );
// Groups are assigned
if ( ! empty( $group_ids ) ) {
// Get user's memberships limited to fieldgroup's user groups
$groups = groups_get_groups( array(
'user_id' => $user_id,
'include' => $group_ids,
'show_hidden' => true,
'per_page' => false,
'populate_extras' => false,
) );
return (bool) $groups['groups'];
// No groups were assigned, so user has access
} else {
return true;
}
}
/**
* Return whether the user can view the field
*
* @since 1.1.0
*
* @uses bp_loggedin_user_id()
* @uses BP_XProfile_For_User_Groups::can_user_view_fieldgroup()
* @uses groups_get_groups()
* @uses BP_XProfile_For_User_Groups::is_user_field_member()
* @uses BP_XProfile_For_User_Groups::get_field_user_groups_viewing()
*
* @param int $fieldgroup_id Field group ID
* @param int $user_id Optional. User ID. Defaults to the current user.
* @param bool $check_fieldgroup Optional. Whether to do an early parent fieldgroup's membership check.
* @return bool User is field's user group member
*/
public function can_user_view_field( $field_id, $user_id = 0, $check_fieldgroup = true ) {
// Bail if this is the primary field
if ( 1 === $field_id || ( is_object( $field_id ) && 1 === $field_id->id ) )
return true;
// Default to current user
if ( ! is_numeric( $user_id ) ) {
$user_id = bp_loggedin_user_id();
}
// Check parent fieldgroup membership
if ( true === $check_fieldgroup ) {
// Get the field object to find the fieldgroup ID
if ( is_object( $field_id ) ) {
$field = $field_id;
} else {
$field = new BP_XProfile_Field( $field_id );
}
// Bail early if user cannot view the fieldgroup
if ( ! $this->can_user_view_fieldgroup( $field->group_id, $user_id ) ) {
return false;
}
}
// Get field ID
if ( is_object( $field_id ) )
$field_id = $field_id->id;
// Field members can always view their data
if ( $this->is_user_field_member( $field_id, $user_id ) )
return true;
// Get field's viewing groups ids
$group_ids = $this->get_field_user_groups_viewing( $field_id );
// Groups are assigned
if ( ! empty( $group_ids ) ) {
// Get user's memberships limited to field's user groups
$groups = groups_get_groups( array(
'user_id' => $user_id,
'include' => $group_ids,
'show_hidden' => true,
'per_page' => false,
'populate_extras' => false,
) );
return (bool) $groups['groups'];
// No groups were assigned, so user has access
} else {
return true;
}
}
/** Groups Having *********************************************************/
/**
* Return the xprofile fieldgroup assigned user groups that have the fieldgroup
*
* @since 1.0.0
*
* @param int $fieldgroup_id Fieldgroup ID
* @return array Fieldgroup having user group ids
*/
public function get_fieldgroup_user_groups_having( $fieldgroup_id ) {
$meta = bp_xprofile_get_meta( $fieldgroup_id, 'group', 'for-user-groups' );
// Sanitize meta
if ( empty( $meta ) )
$meta = array();
return (array) $meta;
}
/**
* Return the xprofile field assigned user groups that have the field
*
* @since 1.0.0
*
* @param int $field_id Field ID
* @return array Field having user field ids
*/
public function get_field_user_groups_having( $field_id ) {
$meta = bp_xprofile_get_meta( $field_id, 'field', 'for-user-groups' );
// Sanitize meta
if ( empty( $meta ) )
$meta = array();
return (array) $meta;
}
/**
* Update the fieldgroup's user groups meta value
*
* @since 1.0.0
*
* @uses bp_xprofile_update_fieldgroup_meta()
* @param int $fieldgroup_id Fieldgroup ID
* @param array $groups Assigned having user group ids
*/
public function update_fieldgroup_user_groups_having( $fieldgroup_id, $groups ) {
// Sanitize input
$groups = array_map( 'intval', (array) $groups );
// Update group meta
bp_xprofile_update_fieldgroup_meta( $fieldgroup_id, 'for-user-groups', $groups );
}
/**
* Update the field's user groups meta value
*
* @since 1.0.0
*
* @uses bp_xprofile_update_field_meta()
* @param int $field_id Field ID
* @param array $groups Assigned having user group ids
*/
public function update_field_user_groups_having( $field_id, $groups ) {
// Sanitize input
$groups = array_map( 'intval', (array) $groups );
// Update group meta
bp_xprofile_update_field_meta( $field_id, 'for-user-groups', $groups );
}
/** Groups Viewing ********************************************************/
/**
* Return the xprofile fieldgroup assigned user groups that can view the fieldgroup
*
* @since 1.1.0
*
* @param int $fieldgroup_id Fieldgroup ID
* @return array Fieldgroup viewing user group ids
*/
public function get_fieldgroup_user_groups_viewing( $fieldgroup_id ) {
$meta = bp_xprofile_get_meta( $fieldgroup_id, 'group', 'for-user-groups-viewing' );
// Sanitize meta
if ( empty( $meta ) )
$meta = array();
return (array) $meta;
}
/**
* Return the xprofile field assigned user groups that can view the field
*
* @since 1.1.0
*
* @param int $field_id Field ID
* @return array Field viewing user field ids
*/
public function get_field_user_groups_viewing( $field_id ) {
$meta = bp_xprofile_get_meta( $field_id, 'field', 'for-user-groups-viewing' );
// Sanitize meta
if ( empty( $meta ) )
$meta = array();
return (array) $meta;
}
/**
* Update the fieldgroup's viewing user groups meta value
*
* @since 1.1.0
*
* @uses bp_xprofile_update_fieldgroup_meta()
* @param int $fieldgroup_id Fieldgroup ID
* @param array $groups Assigned viewing user group ids
*/
public function update_fieldgroup_user_groups_viewing( $fieldgroup_id, $groups ) {
// Sanitize input
$groups = array_map( 'intval', (array) $groups );
// Update group meta
bp_xprofile_update_fieldgroup_meta( $fieldgroup_id, 'for-user-groups-viewing', $groups );
}
/**
* Update the field's viewing user groups meta value
*
* @since 1.1.0
*
* @uses bp_xprofile_update_field_meta()
* @param int $field_id Field ID
* @param array $groups Assigned viewing user group ids
*/
public function update_field_user_groups_viewing( $field_id, $groups ) {
// Sanitize input
$groups = array_map( 'intval', (array) $groups );
// Update group meta
bp_xprofile_update_field_meta( $field_id, 'for-user-groups-viewing', $groups );
}
/** Metaboxes *************************************************************/
/**
* Output the metabox for fieldgroup assigned user groups
*
* Since BP 2.1.0.
*
* @since 1.0.0
*
* @param BP_XProfile_Group $fieldgroup Current xprofile fieldgroup
*/
public function fieldgroup_display_metabox( $fieldgroup ) {
// The primary fieldgroup is for all, so bail
if ( 1 === $fieldgroup->id )
return;
// Setup user group query args
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'show_hidden' => true,
'per_page' => false,
'populate_extras' => false,
);
// Get all, having and viewing user groups
$groups = groups_get_groups( $args );
$class = array( 'user_groups' );
$class[] = count( $groups['groups'] ) > 6 ? 'scroll' : '';
$having = ! empty( $fieldgroup->id ) ? $this->get_fieldgroup_user_groups_having( $fieldgroup->id ) : array();
$viewing = ! empty( $fieldgroup->id ) ? $this->get_fieldgroup_user_groups_viewing( $fieldgroup->id ) : array();
?>
<div id="for_user_groups" class="postbox">
<h3><?php _e( 'User Groups', 'bp-xprofile-for-user-groups' ); ?> <?php $this->the_info_toggler(); ?></h3>
<div class="inside">
<p class="metabox-info">
<?php _e( 'Assign user groups to the profile field group to limit its applicability to the members of that group.', 'bp-xprofile-for-user-groups' ); ?>
</p>
<p><span class="description"><?php _e( 'Restrict the applicability of this fieldgroup', 'bp-xprofile-for-user-groups' ); ?>:</span></p>
<ul class="groups_having <?php echo implode( ' ', $class ); ?>">
<?php foreach ( $groups['groups'] as $group ) : ?>
<li><label><input name="for-user-groups[]" type="checkbox" value="<?php echo $group->id; ?>" <?php checked( in_array( $group->id, $having ) ); ?> /> <?php echo $group->name; ?></label></li>
<?php endforeach; ?>
</ul><!-- .groups_having -->
<p><span class="description"><?php _e( 'Restrict the visibility of this fieldgroup', 'bp-xprofile-for-user-groups' ); ?>:</span></p>
<ul class="groups_viewing <?php echo implode( ' ', $class ); ?>">
<?php foreach ( $groups['groups'] as $group ) : ?>
<li><label><input name="for-user-groups-viewing[]" type="checkbox" value="<?php echo $group->id; ?>" <?php checked( in_array( $group->id, $viewing ) ); ?> /> <?php echo $group->name; ?></label></li>
<?php endforeach; ?>
</ul><!-- .groups_viewing -->
</div>
<?php wp_nonce_field( 'for-user-groups', '_wpnonce_for_user_groups' ); ?>
</div>
<?php
}
/**
* Save the metabox for fieldgroup assigned user groups
*
* @since 1.0.0
*
* @param BP_XProfile_Group $fieldgroup Saved xprofile group
*/
public function fieldgroup_save_metabox( $fieldgroup ) {
// Bail when nonce does not verify
if ( ! isset( $_REQUEST['_wpnonce_for_user_groups' ] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce_for_user_groups'], 'for-user-groups' ) )
return;
// Walk for having and viewing groups
foreach ( array(
'having' => 'for-user-groups',
'viewing' => 'for-user-groups-viewing'
) as $type => $name ) {
// Sanitize input
if ( isset( $_REQUEST[ $name ] ) ) {
$groups = array_map( 'intval', (array) $_REQUEST[ $name ] );
// No user groups selected
} else {
$groups = array();
}
// Update when something changed
if ( call_user_func_array( array( $this, "get_fieldgroup_user_groups_{$type}" ), array( $fieldgroup->id ) ) != $groups ) {
call_user_func_array( array( $this, "update_fieldgroup_user_groups_{$type}" ), array( $fieldgroup->id, $groups ) );
}
}
}
/**
* Output the metabox for field assigned user groups
*
* Since BP 2.1.0.
*
* @since 1.0.0
*
* @param BP_XProfile_Field $field Current xprofile field
*/
public function field_display_metabox( $field ) {
// The primary field is for all, so bail
if ( 1 === $field->id )
return;
// Query args for user groups from the parent field group
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'show_hidden' => true,
'include' => $this->get_fieldgroup_user_groups_having( $field->group_id ),
'per_page' => false,
'populate_extras' => false,
);
// Get all, having and viewing user groups
$groups = groups_get_groups( $args );
$class = array( 'user_groups' );
$class[] = count( $groups['groups'] ) > 6 ? 'scroll' : '';
$having = ! empty( $field->id ) ? $this->get_field_user_groups_having( $field->id ) : array();
$viewing = ! empty( $field->id ) ? $this->get_field_user_groups_viewing( $field->id ) : array();
?>
<div id="for_user_groups" class="postbox">
<h3><?php _e( 'User Groups', 'bp-xprofile-for-user-groups' ); ?> <?php $this->the_info_toggler(); ?></h3>
<div class="inside">
<p class="metabox-info">
<?php _e( 'Assign user groups to the profile field to limit its applicability to the members of that group. Selectable groups are limited to the ones assigned to the parent field group.', 'bp-xprofile-for-user-groups' ); ?>
</p>
<p><span class="description"><?php _e( 'Restrict the applicability of this field', 'bp-xprofile-for-user-groups' ); ?>:</span></p>
<ul class="groups_having <?php echo implode( ' ', $class ); ?>">
<?php foreach ( $groups['groups'] as $group ) : ?>
<li><label><input name="for-user-groups[]" type="checkbox" value="<?php echo $group->id; ?>" <?php checked( in_array( $group->id, $having ) ); ?> /> <?php echo $group->name; ?></label></li>
<?php endforeach; ?>
</ul><!-- .groups_having -->
<p><span class="description"><?php _e( 'Restrict the visibility of this field', 'bp-xprofile-for-user-groups' ); ?>:</span></p>
<ul class="groups_viewing <?php echo implode( ' ', $class ); ?>">
<?php foreach ( $groups['groups'] as $group ) : ?>
<li><label><input name="for-user-groups-viewing[]" type="checkbox" value="<?php echo $group->id; ?>" <?php checked( in_array( $group->id, $viewing ) ); ?> /> <?php echo $group->name; ?></label></li>
<?php endforeach; ?>
</ul><!-- .groups_viewing -->
</div>
<?php wp_nonce_field( 'for-user-groups', '_wpnonce_for_user_groups' ); ?>
</div>
<?php
}
/**
* Save the metabox for field assigned user groups
*
* @since 1.0.0
*
* @param BP_XProfile_Field $field Saved xprofile field
*/
public function field_save_metabox( $field ) {
// Bail when nonce does not verify
if ( ! isset( $_REQUEST['_wpnonce_for_user_groups'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce_for_user_groups'], 'for-user-groups' ) )
return;
// Walk for having and viewing groups
foreach ( array(
'having' => 'for-user-groups',
'viewing' => 'for-user-groups-viewing'
) as $type => $name ) {
// Sanitize input
if ( isset( $_REQUEST[ $name ] ) ) {
$groups = array_map( 'intval', (array) $_REQUEST[ $name ] );
// No user groups selected
} else {
$groups = array();
}
// Update if something changed
if ( call_user_func_array( array( $this, "get_field_user_groups_{$type}" ), array( $field->id ) ) != $groups ) {
call_user_func_array( array( $this, "update_field_user_groups_{$type}" ), array( $field->id, $groups ) );
}
}
}
/**
* Output the metabox information toggle button
*
* @since 1.0.1
*
* @return string HTML <i> element
*/
public function the_info_toggler() {
printf( '<i class="dashicons-before dashicons-info" title="%s"></i>', __( 'Toggle metabox information', 'bp-xprofile-for-user-groups' ) );
}
/**
* Output specific metabox styles for the xprofile admin
*
* @since 1.1.1
*
* @uses BP_XProfile_For_User_Groups::is_xprofile_admin()
*/
public function admin_scripts() {
// Bail when this is not an xprofile admin page
if ( ! $this->is_xprofile_admin() )
return; ?>
<style type="text/css">
#for_user_groups .inside ul.user_groups.scroll {
padding: 2px 5px 0;
margin: 1em 0 0 0;
border: 1px solid #ddd;
height: 12em;
overflow-y: scroll;
}
#for_user_groups .inside .metabox-info:visible + ul.user_groups.scroll {
margin: 0;
}
#for_user_groups .inside .metabox-info {
display: none;
}
#for_user_groups i.dashicons-info {
float: right;
cursor: pointer;
color: #444;
}
#for_user_groups i.dashicons-info:hover {
color: #000;
}
</style>
<script type="text/javascript">
jQuery('document').ready( function( $ ) {
// Toggle metabox information
var $box = $( '#for_user_groups' );
$box.on( 'click', 'i.dashicons-info', function() {
$box.find( 'p.metabox-info' ).toggle();
});
});
</script>
<?php
}
/**
* Return whether we are on the XProfile admin pages
*
* @since 1.1.1
*
* @uses get_current_screen()
*
* @return bool This is an XProfile admin page
*/
public function is_xprofile_admin() {
// Bail when not in the admin
if ( ! is_admin() )
return false;
// Define expected screen id
$screen_id = 'users_page_bp-profile-setup';
if ( is_network_admin() ) {
$screen_id .= '-network';
}
return $screen_id === get_current_screen()->id;
}
}
/**
* Initiate plugin class and return singleton
*
* @since 1.0.0
*
* @return BP_XProfile_For_User_Groups
*/
function bp_xprofile_for_user_groups() {