-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhumanities-commons.php
2147 lines (1811 loc) · 66.1 KB
/
humanities-commons.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 Humanities Commons Plugin
*
* Humanities Commons is a set of functions, filters and actions used to support a specific multi-network BuddyPress configuration.
*
* @package Humanities Commons
* @subpackage Configuration
*/
/**
* Plugin Name: Humanities Commons
* Description: Humanities Commons is a set of functions, filters and actions used to support a specific multi-network BuddyPress configuration.
* Version: 1.0
* Author: MLA
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use MLA\Commons\Plugin\Logging\Logger;
global $hcommons_logger;
$hcommons_logger = new Logger( 'hcommons_error' );
$hcommons_logger->createLog( 'hcommons_error' );
/**
* Write a formatted HCommons error or informational message.
*/
function hcommons_write_error_log( $error_type, $error_message, $info = null ) {
global $hcommons_logger;
try {
if ( 'info' === $error_type ) {
if ( empty( $info ) ) {
$hcommons_logger->addInfo( $error_message );
} else {
$hcommons_logger->addInfo( $error_message . ' : ', $info );
}
} else {
$hcommons_logger->addError( $error_message );
}
} catch ( Exception $e ) {
//Do nothing
}
}
require_once ( dirname( __FILE__ ) . '/society-settings.php' );
require_once ( dirname( __FILE__ ) . '/wpmn-taxonomy-functions.php' );
require_once ( dirname( __FILE__ ) . '/admin-toolbar.php' );
require_once ( dirname( __FILE__ ) . '/hc-simplesaml.php' );
require_once ( dirname( __FILE__ ) . '/class.comanage-api.php' );
require_once ( dirname( __FILE__ ) . '/class-logger.php' );
require_once ( dirname( __FILE__ ) . '/frontend-filters.php' );
require_once ( dirname( __FILE__ ) . '/plugin-hooks.php' );
require_once ( dirname( __FILE__ ) . '/buddypress.php' );
require_once ( dirname( __FILE__ ) . '/mailchimp.php' );
class Humanities_Commons {
/**
* the network called "Humanities Commons" a.k.a. the hub
*/
public static $main_network;
/**
* root blog of the main network
*/
public static $main_site;
/**
* current society id
*/
public static $society_id;
/**
* current shib session id
*/
public static $shib_session_id;
public function __construct() {
if ( defined( 'HC_SITE_ID' ) ) {
self::$main_network = get_network( (int) HC_SITE_ID );
} else {
self::$main_network = get_network( (int) '1' );
}
self::$main_site = get_site_by_path( self::$main_network->domain, self::$main_network->path );
self::$society_id = get_network_option( '', 'society_id' );
add_filter( 'bp_get_signup_page', function() { return '/membership/'; } );
add_filter( 'bp_get_taxonomy_term_site_id', array( $this, 'hcommons_filter_bp_taxonomy_storage_site' ), 10, 2 );
add_filter( 'wpmn_get_taxonomy_term_site_id', array( $this, 'hcommons_filter_hc_taxonomy_storage_site' ), 10, 2 );
add_action( 'bp_after_has_members_parse_args', array( $this, 'hcommons_set_members_query' ) );
add_filter( 'bp_before_has_groups_parse_args', array( $this, 'hcommons_set_groups_query_args' ) );
add_filter( 'groups_get_groups', array( $this, 'hcommons_groups_get_groups' ), 10, 2 );
add_action( 'groups_create_group_step_save_group-details', array( $this, 'hcommons_set_group_type' ) );
add_action( 'groups_create_group_step_save_group-details', array( $this, 'hcommons_set_group_mla_oid' ) );
add_filter( 'invite_anyone_send_follow_requests_on_acceptance', '__return_false' );
add_filter( 'bp_before_has_blogs_parse_args', array( $this, 'hcommons_set_network_blogs_query' ) );
add_filter( 'bp_get_total_blog_count', array( $this, 'hcommons_get_total_blog_count' ) );
add_filter( 'bp_get_total_blog_count_for_user', array( $this, 'hcommons_get_total_blog_count_for_user' ) );
add_filter( 'bp_before_has_activities_parse_args', array( $this, 'hcommons_set_network_activities_query' ) );
add_filter( 'bp_activity_get_where_conditions', array( $this, 'hcommons_filter_activity_where_conditions' ) );
add_action( 'bp_activity_after_save', array( $this, 'hcommons_set_activity_society_meta' ) );
add_action( 'bp_notification_after_save', array( $this, 'hcommons_set_notification_society_meta' ) );
add_filter( 'bp_activity_get_permalink', array( $this, 'hcommons_filter_activity_permalink' ), 10, 2 );
add_filter( 'body_class', array( $this, 'hcommons_society_body_class_name' ) );
// this filter makes 'bp_xprofile_change_field_visibility' false which is required for profile plugin visibility controls
// doesn't work with local users without a member type, but also doesn't work when member type & blog_id don't match?
// should always return true for any logged-in user, since visibility controls on xprofile fields are not restricted
//add_filter( 'bp_current_user_can', array( $this, 'hcommons_check_site_member_can' ), 10, 4 );
add_filter( 'bp_get_groups_directory_permalink', array( $this, 'hcommons_set_groups_directory_permalink' ) );
add_filter( 'bp_get_group_permalink', array( $this, 'hcommons_set_group_permalink' ),10, 2 );
add_filter( 'bp_core_get_user_domain', array( $this, 'hcommons_set_members_directory_permalink' ),10, 4 );
add_filter( 'get_blogs_of_user', array( $this, 'hcommons_filter_get_blogs_of_user'), 10, 3 );
add_filter( 'bp_core_avatar_upload_path', array( $this, 'hcommons_set_bp_core_avatar_upload_path' ) );
add_filter( 'bp_core_avatar_url', array( $this, 'hcommons_set_bp_core_avatar_url' ) );
// disable in favor of bp-blog-avatar
// see https://buddypress.trac.wordpress.org/ticket/6544
add_filter( 'bp_is_blogs_site-icon_active', '__return_false' );
add_filter( 'bp_get_group_join_button', array( $this, 'hcommons_check_bp_get_group_join_button' ), 10, 2 );
add_action( 'pre_user_query', array( &$this, 'hcommons_filter_site_users_only' ) ); // do_action_ref_array() is used for pre_user_query
add_filter( 'invite_anyone_is_large_network', '__return_true' ); //hide invite anyone member list on create/edit group screen
add_action( 'bp_init', array( $this, 'hcommons_remove_nav_items' ) );
add_action( 'bp_init', array( $this, 'hcommons_set_default_scope_society' ) );
add_filter( 'password_protected_login_headertitle', array( $this, 'hcommons_password_protect_title' ) );
add_filter( 'password_protected_login_headerurl', array( $this, 'hcommons_password_protect_url' ) );
add_action( 'password_protected_login_messages', array( $this, 'hcommons_password_protect_message' ) );
add_filter( 'bp_activity_time_since', array( $this, 'hcommons_filter_activity_time_since' ), 10, 2 );
add_filter( 'bp_attachments_cover_image_upload_dir', array( $this, 'hcommons_cover_image_upload_dir' ), 10, 2 );
//add_filter( 'bp_attachments_pre_cover_image_ajax_upload', array( $this, 'hcommons_cover_image_ajax_upload' ), 10, 4 );
add_filter( 'bp_attachments_uploads_dir_get', array( $this, 'hcommons_attachments_uploads_dir_get' ), 10, 2 );
add_filter( 'bp_attachment_upload_dir', array( $this, 'hcommons_attachment_upload_dir' ), 10, 2 );
add_filter( 'bp_get_new_group_enable_forum', array( $this, 'hcommons_get_new_group_enable_forum' ) );
add_action( 'wp_ajax_hcommons_settings_general', array( $this, 'hcommons_settings_general_ajax' ) );
add_filter( 'bp_before_activity_get_parse_args', array( $this, 'hcommons_set_network_admin_activities_query' ) );
add_action( 'init', array( $this, 'hcommons_remove_bp_settings_general' ) );
add_action( 'bp_before_group_settings_creation_step', array( $this, 'hcommons_groups_group_before_save') );
add_action( 'bp_groups_admin_meta_boxes', array( $this, 'hcommons_remove_group_type_meta_boxes' ) );
add_action( 'bp_groups_admin_meta_boxes', array( $this, 'hcommons_add_group_type_meta_box' ) );
add_action( 'bp_members_admin_user_metaboxes', array( $this, 'hcommons_remove_member_type_meta_boxes' ), 10, 2 );
add_action( 'bp_members_admin_user_metaboxes', array( $this, 'hcommons_add_member_type_meta_box' ), 10, 2 );
add_action( 'bp_groups_admin_meta_boxes', array( $this, 'hcommons_add_manage_group_memberships_meta_box' ) );
add_action( 'bp_groups_admin_load', array( $this, 'hcommons_save_managed_group_membership' ) );
add_filter( 'bp_docs_map_meta_caps', array( $this, 'hcommons_check_docs_new_member_caps' ), 10, 4 );
add_filter( 'wpmu_active_signup', array( $this, 'hcommons_check_sites_new_member_status' ) );
add_shortcode( 'hcommons_society_page', array( $this, 'hcommons_get_society_page_by_slug' ) );
add_shortcode( 'hcommons_env_variable', array( $this, 'hcommons_get_env_variable' ) );
add_filter( 'bp_blogs_format_activity_action_new_blog_post', array( $this, 'hcommons_blogs_format_activity_new_blog_post' ), 10, 2 );
add_filter( 'bp_blogs_format_activity_action_new_blog_comment', array( $this, 'hcommons_blogs_format_activity_new_blog_comment' ), 10, 2 );
// Disable Akismet for BuddyPress docs. Docs are getting spammed when moved around in folders. --Mike 21-08-19
add_filter( 'bp_docs_post_args_before_save', array( $this, 'hcommons_disable_akismet_for_moving_docs' ), 5, 3 );
// Add hcommons.org to list of allowed redirect hosts on dev, for site importing purposes. --Mike 21-12-10
add_filter( 'http_request_host_is_external', array( $this, 'allow_external_hcommons'), 10, 3 );
add_filter( 'user_has_cap', array( $this, 'hcommons_vet_user_for_bpeo' ), 10, 4 );
add_filter( 'map_meta_cap', array( $this, 'hcommons_bpeo_event_creation_capability' ), 20, 4 );
add_filter( 'bp_loggedin_user_id', array( $this, 'hcommons_bp_loggedin_user_id'), 10, 1 );
add_filter( 'bp_follow_blogs_show_footer_button', array( $this, 'hcommons_filter_show_footer_button' ), 10, 1 );
add_filter( 'bp_follow_blogs_get_follow_button', array( $this, 'hcommons_filter_get_follow_button' ), 10, 3 );
add_action( 'init', array( $this, 'add_hide_site_option' ), 10 , 0 );
}
public function allow_external_hcommons( $external, $host, $url ) {
if ( ! defined('WP_ENV') || ( WP_ENV != 'staging' && WP_ENV != 'development' ) ) {
return $external;
}
if ( ! strpos( $host, 'hcommons.org' ) !== False ) {
return True;
}
return False;
}
public static function society_name() {
if ( ! self::$society_id ) {
return '';
}
switch ( self::$society_id ) {
case 'hc' :
return 'Humanities Commons';
case 'msu' :
return 'MSU Commons';
case 'mla' :
return 'MLA Commons';
case 'up' :
return 'UP Commons';
case 'sah' :
return 'SAH Commons';
case 'asees' :
return 'ASEEES Commons';
case 'arlisna' :
return 'ARLIS/NA Commons';
case 'hastac' :
return 'HASTAC Commons';
default :
return strtoupper( self::$society_id ) . ' Commons';
}
}
/**
* Prevent 'Follow Site' and 'Followed Sites' buttons from appearing in footer.
*
* @see buddypress-followers/_inc/modules/blogs.php::show_footer_button()
*
* @param boolean $retval Whether buttons should appear
* @return boolean Whether buttons should appear (false)
*/
public function hcommons_filter_show_footer_button( $retval ) {
return false;
}
/**
*
* @see buddypress-followers/_inc/modules/blogs.php::get_button()
*
* @return array Empty array to prevent button from showing.
*
*/
public function hcommons_filter_get_follow_button( $button, $r, $is_following ) {
return [];
}
/**
* Disable Akismet for BuddyPress docs when a doc is being moved.
*
* This is meant to address an issue where docs get spuriously marked as
* spam when being moved.
* @link https://github.com/MESH-Research/commons/issues/76
*
* Note: This disables Akisment whenever a doc is being moved, and doesn't
* check whether there are other changes. In theory you could avoid the spam
* filter by moving the doc in addition to whatever else you're doing, but
* this seems like it'd be a lot of trouble to go through.
*
* @see buddypress-docs/addon-akismet.php BP_Docs_Akismet::check_for_spam
*
* @author Mike Thicke
*
* @global $bp_docs The BP_Docs object. @see buddypress-docs/bp-docs.php
*
* @param array $save_args The arguments to be saved.
* @param BP_Docs_Query $bdq_object The query object.
* @param array $passed_args The arguments passed from the save
* request.
*
* @return array Pass through $save_args unchanged. This filter acts by
* removing a lower priority filter if necessary.
*/
public function hcommons_disable_akismet_for_moving_docs( $save_args, $bdq_object, $passed_args ) {
global $bp_docs;
$folder_id = intval( $_POST['bp-docs-folder'] );
$existing_folder_id = bp_docs_get_doc_folder( $passed_args['doc_id'] );
if ( $folder_id !== $existing_folder_id ) {
remove_filter(
'bp_docs_post_args_before_save',
array( $bp_docs->akismet, 'check_for_spam' ),
10
);
}
return $save_args;
}
/**
* For new blog comment posts in activity feed
*
* @param string $action current bp_action the user is on in the loop
* @param object $activity current activity object in the loop
*
* @return string $action corrected current action from activity object
*/
public function hcommons_blogs_format_activity_new_blog_comment( $action, $activity ) {
//force $action to contain the same $activity->type text to avoid issues with titles for comments
if( $activity->type == 'new_blog_comment' && isset( $activity->action ) ) {
$action = $activity->action;
}
return $action;
}
/**
* For new blog posts in activity feed
*
* @param string $action current bp_action the user is on in the loop
* @param object $activity current activity object in the loop
*
* @return string $action corrected current action from activity object
*/
public function hcommons_blogs_format_activity_new_blog_post( $action, $activity ) {
//force $action to contain the same $activity->type text to avoid issues with titles
// Make sure there is action text as well.
if( $activity->type == 'new_blog_post' && isset( $activity->action ) ) {
$action = $activity->action;
}
return $action;
}
/**
* Handles saving of manage group metabox
*
* @return void
*/
public function hcommons_save_managed_group_membership() {
//displays what action we are in
$action = bp_admin_list_table_current_bulk_action();
//lets check if the request method and action are on post and save
if( $action == 'save' ) {
//is the new value set?
if( isset( $_POST['autopopulate'] ) ) {
//grabs group_id from get and sanitizes it
$group_id = filter_var( $_GET['gid'], FILTER_SANITIZE_NUMBER_INT );
$autopopulate = filter_var( $_POST['autopopulate'], FILTER_SANITIZE_STRIPPED );
$autopopulate_meta = groups_get_groupmeta( $group_id, 'autopopulate', true );
//lets update the group meta for manage membership
if( $autopopulate !== $autopopulate_meta ) {
groups_update_groupmeta( $group_id, 'autopopulate', $autopopulate );
wp_cache_delete( 'managed_group_names', 'hcommons_settings' );
}
}
}
}
/**
* Handles metabox creation for manage membership metabox
*
* @return void
*/
public function hcommons_add_manage_group_memberships_meta_box() {
if( is_admin() && $_GET['page'] == 'bp-groups' ) {
add_meta_box(
'hcommons_admin_groups_manage',
_x( 'Manage Group Memberships', 'Manages group memberships', 'buddypress' ),
array( $this, 'hcommons_admin_manage_group_memberships_view' ),
get_current_screen()->id,
'side',
'core'
);
}
}
/**
* Filter the register url to be society specific
*
* @since HCommons
*
* @param string $register_url
* @return string $register_url Modified url.
*/
public static function hcommons_register_url( $register_url ) {
if ( ! empty( self::$society_id ) && defined( strtoupper( self::$society_id ) . '_ENROLLMENT_URL' ) ) {
$register_url = constant( strtoupper( self::$society_id ) . '_ENROLLMENT_URL' ) . '/done:core';
}
return apply_filters( 'hcommons_register_url', $register_url );
}
/**
* Outputs view for manage membership metabox
*
* @return void
*/
public function hcommons_admin_manage_group_memberships_view() {
//grabs group_id from get and sanitizes it
$group_id = filter_var( $_GET['gid'], FILTER_SANITIZE_NUMBER_INT );
$autopopulate_meta = groups_get_groupmeta( $group_id, 'autopopulate', true );
?>
<label>
<input type="radio" name="autopopulate" value="Y" <?php echo ( $autopopulate_meta == 'Y' ) ? 'checked' : '' ; ?> />Yes
</label>
<br />
<label>
<input type="radio" name="autopopulate" value="N" <?php echo ( $autopopulate_meta == 'N' ) ? 'checked' : '' ; ?> />No
</label>
<?php
}
/**
* Removes member type meta box on user screen in wp-admin
*
* @return void
*/
public function hcommons_remove_member_type_meta_boxes() {
if( is_admin() && $_GET['page'] == 'bp-profile-edit' ) {
remove_meta_box( 'bp_members_admin_member_type', 'users_page_bp-profile-edit-network', 'side' );
}
}
/**
* Adds new member type meta box on user screen in wp-admin
*
* @return void
*/
public function hcommons_add_member_type_meta_box( $profile, $user_id ) {
if( is_admin() && $_GET['page'] == 'bp-profile-edit' ) {
add_meta_box(
'hcommons_members_admin_member_type',
_x( 'Member Type', 'members user-admin edit screen', 'buddypress' ),
array( $this, 'hcommons_member_type_meta_box_view' ),
get_current_screen()->id,
'side',
'core'
);
}
}
/**
* Outputs view for member type meta box on user screen in wp-admin
*
* @return void
*/
public function hcommons_member_type_meta_box_view() {
if( isset( $_GET['user_id'] ) && is_admin() ) {
//make sure user id is only numerical
$user_id = filter_var( $_GET['user_id'], FILTER_SANITIZE_NUMBER_INT );
$member_types = bp_get_member_type( $user_id, false );
echo "<ul>";
//output member types user currently has
foreach( $member_types as $type ) {
echo "<li>" . strtoupper( $type ) . "</li>";
}
echo "</ul>";
}
}
/**
* Adds new group type metabox to user admin area in bp-groups
*
* @return void
*/
public function hcommons_add_group_type_meta_box() {
if( is_admin() && $_GET['page'] == 'bp-groups' ) {
add_meta_box(
'hcommons_admin_group_type',
_x( 'Group Type', 'groups admin edit screen', 'buddypress' ),
array( $this, 'hcommons_group_type_meta_box_view' ),
get_current_screen()->id,
'side',
'core'
);
}
}
/**
* Outputs view for new group type metabox
*
* @return void
*/
public function hcommons_group_type_meta_box_view() {
//make sure group id is only numerical
$group_id = filter_var( $_GET['gid'], FILTER_SANITIZE_NUMBER_INT );
$current_types = (array) bp_groups_get_group_type( $group_id, false );
?>
<ul class="categorychecklist form-no-clear">
<?php foreach ( $current_types as $type ) : ?>
<li>
<label class="selectit">
<?php echo strtoupper( esc_html( $type ) ); ?>
</label>
</li>
<?php endforeach; ?>
</ul>
<?php
}
/**
* Removes current group type meta box to be replaced by another
*
* @return void
*/
public function hcommons_remove_group_type_meta_boxes() {
if( is_admin() && $_GET['page'] == 'bp-groups' ) {
remove_meta_box( 'bp_groups_admin_group_type', 'toplevel_page_bp-groups-network', 'side' );
}
}
public function hcommons_filter_bp_taxonomy_storage_site( $site_id, $taxonomy ) {
if ( in_array( $taxonomy, array( 'bp_group_type', 'bp_member_type' ) ) ) {
return self::$main_site->blog_id;
} else {
return $site_id;
}
}
public function hcommons_filter_hc_taxonomy_storage_site( $site_id, $taxonomy ) {
if ( in_array( $taxonomy, array( 'mla_academic_interests', 'humcore_deposit_language', 'humcore_deposit_subject', 'humcore_deposit_tag',
'hcommons_society_member_id' ) ) ) {
return (int) '1'; // Go legacy during beta.
} else {
return $site_id;
}
}
public function hcommons_set_members_query( $args ) {
if ( ! bp_is_members_directory() || ( isset( $args['scope'] ) && 'society' === $args['scope'] ) ) {
$args['member_type'] = self::$society_id;
}
return $args;
}
public function hcommons_set_groups_query_args( $args ) {
// profile loops per-type, leave as-is
if ( bp_is_user_profile() || ( bp_is_settings_component() && bp_is_current_action( 'notifications' ) ) ) {
return $args;
}
//hcommons_write_error_log( 'info', '****GROUPS_QUERY_ARGS****-' . var_export( $args, true ) );
if ( isset( $args['scope'] ) && $args['scope'] == 'personal' ) {
$args['group_type'] = '';
return $args;
}
if ( is_admin() && ! empty( $_REQUEST['page'] ) && 'bp-groups' == $_REQUEST['page'] ) {
$args['group_type'] = self::$society_id;
return $args;
}
if ( 'hc' === self::$society_id && empty( $args['scope'] ) && ! self::backtrace_contains( 'class', 'EP_BP_API' ) ) {
$args['group_type'] = '';
} else {
$args['group_type'] = self::$society_id;
}
// only show hc groups on /members/*/invite-anyone
if (
! is_super_admin() &&
( bp_is_user() && false !== strpos( $_SERVER['REQUEST_URI'], 'invite-anyone' ) )
) {
$args['group_type'] = 'hc';
}
return $args;
}
/**
* on members/groups directories, set default scope to society
*/
function hcommons_set_default_scope_society() {
if ( bp_is_groups_directory() || ( bp_is_members_directory() && 'hc' !== self::$society_id ) ) {
$object_name = bp_current_component();
$cookie_name = 'bp-' . $object_name . '-scope';
if ( ! isset( $_COOKIE[ $cookie_name ] ) ) {
setcookie( $cookie_name, 'society', null, '/' );
// unless the $_COOKIE global is updated in addition to the actual cookie above,
// bp will not use the value for the first pageload.
$_COOKIE[ $cookie_name ] = 'society';
}
}
}
/**
* Target specific occurances of groups_get_groups filter to restrict groups to society and don't show hidden groups.
*
* @since HCommons
*
* @param object $data Groups
* @param array $r Arguments
* @return object $new_groups or $data
*/
public function hcommons_groups_get_groups( $data, $r ) {
if (
self::backtrace_contains( 'class', 'BuddyPress_Event_Organiser_EO' ) ||
self::backtrace_contains( 'function', 'bpmfp_get_other_groups_for_user' )
) {
$new_groups = BP_Groups_Group::get( array(
'type' => $r['type'],
'user_id' => $r['user_id'],
'include' => $r['include'],
'exclude' => $r['exclude'],
'search_terms' => $r['search_terms'],
'group_type' => self::$society_id,
'group_type__in' => $r['group_type__in'],
'group_type__not_in' => $r['group_type__not_in'],
'meta_query' => $r['meta_query'],
'show_hidden' => TRUE,
'per_page' => $r['per_page'],
'page' => $r['page'],
'populate_extras' => $r['populate_extras'],
'update_meta_cache' => $r['update_meta_cache'],
'order' => $r['order'],
'orderby' => $r['orderby'],
) );
return $new_groups;
}
return $data;
}
public function hcommons_set_group_type( $group_id ) {
global $bp;
if ( $bp->groups->new_group_id ) {
$id = $bp->groups->new_group_id;
} else {
$id = $group_id;
}
bp_groups_set_group_type( $id, self::$society_id );
}
public function hcommons_set_group_mla_oid( $group_id ) {
$society_id = self::$society_id;
if ( 'mla' === $society_id ) {
global $bp;
if ( $bp->groups->new_group_id ) {
$id = $bp->groups->new_group_id;
} else {
$id = $group_id;
}
$result = groups_add_groupmeta( $id, 'mla_oid', 'UXX', true );
if ( is_wp_error( $result ) ) {
hcommons_write_error_log( 'info', '****MLA_OID_WRITE_FAILURE****-' . $id . '-' . var_export( $result, true ) );
echo "ERROR: " . var_export( $result, true );
}
bp_groups_set_group_type( $id, self::$society_id );
}
}
/**
* Get the society_id for the current blog or a given blog.
*
* @since HCommons
*
* @param string $blog_id
* @return string $blog_society_id or self::$society_id
*/
public function hcommons_get_blog_society_id( $blog_id = '' ) {
$fields = array();
if ( ! empty( $blog_id ) ) {
$fields['blog_id'] = $blog_id;
} else {
return self::$society_id;
}
$blog_details = get_blog_details( $fields );
$blog_society_id = get_network_option( $blog_details->site_id, 'society_id' );
return $blog_society_id;
}
/**
* Filter the count returned by bp_get_total_blog_count() which ultimately depends on BP_Blogs_Blog::get_all().
* We want to use the filtered results returned by BP_Blogs_Blog::get() instead, so that we accommodate MPO.
*
* @since HCommons
*
* @param string $count
* @return string $count
*/
public function hcommons_get_total_blog_count( $count ) {
// let's see what the blogs query will actually include and use that for the count
$blogs_query_args = $this->hcommons_set_network_blogs_query( array() );
// now make sure the More Privacy Options filter removes any blogs it needs to
$mpo_filtered_blogs = bp_blogs_get_blogs( $blogs_query_args );
if ( $mpo_filtered_blogs ) {
$count = $mpo_filtered_blogs['total'];
}
return $count;
}
/**
* Like hcommons_get_total_blog_count() except for users.
* Because the logged-in logic in BP_Blogs_Blog::get_blogs_for_user() doesn't check the 'public' column,
* MPO doesn't need to be accommodated, which is different than in hcommons_get_total_blog_count().
*
* @since HCommons
*
* @param string $count
* @return string $count
*/
public function hcommons_get_total_blog_count_for_user( $count ) {
$user_blogs = bp_blogs_get_blogs_for_user( get_current_user_id() );
if ( $user_blogs ) {
// do not include HC
foreach ( $user_blogs['blogs'] as $key => $user_blog ) {
if ( $user_blog->blog_id === self::$main_site->blog_id ) {
unset( $user_blogs['blogs'][$key] );
}
}
// $user_blogs['total'] is WRONG! that's why this filter is here, just count the actual blogs instead.
$count = count( $user_blogs['blogs'] );
}
return $count;
}
/**
* Filter the sites query by the society id for the current network except for HC.
*
* @since HCommons
*
* @param array $args
* @return array $args
*/
public function hcommons_set_network_blogs_query( $args ) {
$blog_ids = array();
$current_blog_id = get_current_blog_id();
if (
'hc' !== self::$society_id &&
empty( $args['user_id'] ) &&
! bp_is_current_action('my-sites') &&
! bp_is_current_component('profile')
) {
$current_network = get_current_site();
$network_sites = wp_get_sites( array( 'network_id' => $current_network->id, 'limit' => 9999 ) );
foreach( $network_sites as $site ) {
if ( $site['blog_id'] != $current_blog_id ) {
$blog_ids[] = $site['blog_id'];
}
}
} else {
//TODO Find a better way, this won't scale to all of HC.
$sites = wp_get_sites( array( 'network_id' => null, 'limit' => 9999 ) );
foreach( $sites as $site ) {
if ( $site['blog_id'] != $current_blog_id ) {
$blog_ids[] = $site['blog_id'];
}
}
}
if ( ! empty( $blog_ids ) ) {
$include_blogs = implode( ',', $blog_ids );
$args['include_blog_ids'] = $include_blogs;
}
//hcommons_write_error_log( 'info', '****SET_NETWORK_BLOGS_QUERY***-'.var_export( $args, true ) );
return $args;
}
/**
* Filter the activity query by the society id for the current network.
*
* @since HCommons
*
* @param array $args
* @return array $args
*/
public function hcommons_set_network_activities_query( $args ) {
if ( isset( $args['type'] ) && 'sitewide' === $args['type'] ) {
if ( is_user_logged_in() ) {
$current_user_id = get_current_user_id();
$current_user_blog_ids = BP_Blogs_Blog::get_blog_ids_for_user( $current_user_id );
$current_user_following_ids = bp_follow_get_following( [ 'user_id' => $current_user_id ] );
$current_user_groups = groups_get_user_groups( $current_user_id );
$current_user_group_ids = $current_user_groups['groups'];
$filter_query = array_merge( ( isset( $args['filter_query'] ) ) ? $args['filter_query'] : [], [
// exclude self
[
'column' => 'user_id',
'value' => $current_user_id,
'compare' => '!=',
],
// otherwise, any of these relevant activities
[
'relation' => 'OR',
// any new deposits, groups, or blogs
[
'column' => 'type',
'value' => [ 'new_deposit', 'new_group_deposit', 'created_group', 'new_blog' ],
'compare' => 'IN',
],
// any activity by my followers
[
'column' => 'user_id',
'value' => $current_user_following_ids,
'compare' => 'IN',
],
// any activity on my blogs
[
[
'column' => 'component',
'value' => 'blogs',
],
[
'column' => 'item_id',
'value' => $current_user_blog_ids,
'compare' => 'IN',
],
],
// any activity on my groups
[
[
'column' => 'component',
'value' => 'groups',
],
[
'column' => 'item_id',
'value' => $current_user_group_ids,
'compare' => 'IN',
],
],
],
] );
$args['filter_query'] = $filter_query;
}
}
if ( 'hc' !== self::$society_id && ! bp_is_user_profile() && ! bp_is_user_activity() ) {
$args['meta_query'] = array(
array(
'key' => 'society_id',
'value' => self::$society_id,
'type' => 'string',
'compare' => '='
),
);
}
return $args;
}
/**
* Filter the activity query "WHERE" conditions to exclude 'joined_group' (etc.?) types
*
* @since HCommons
*
* @param array $args
* @return array $args
*/
public function hcommons_filter_activity_where_conditions( $args ) {
// BP_Activity_Activity::get() hardcodes this sql string only if $excluded_types is non-empty,
// so we can assume a non-empty value here means there is at least one type in the sql array
if ( ! bp_is_profile_component() && ! empty( $args['excluded_types'] ) ) {
// these are the types we intend to filter out in addition to whatever is passed to this filter
$not_in = [ 'joined_group', 'friendship_created' ];
// parse the existing excluded types and merge with our own
preg_match_all( "/a.type NOT IN \('(.*)'\)/", $args['excluded_types'], $matches );
$not_in = array_merge( $not_in, explode( "', '", $matches[1][0] ) );
// build new sql using combined types
$args['excluded_types'] = "a.type NOT IN ('" . implode( "', '", $not_in) . "')";
}
return $args;
}
/**
* Add the current society id to the current activity as an activity_meta record.
*
* @since HCommons
*
* @param array $activity
*/
public function hcommons_set_activity_society_meta( $activity ) {
bp_activity_add_meta( $activity->id, 'society_id', self::$society_id, true );
}
/**
* Add the current society id to the current notificaiton as a notification_meta record.
*
* @since HCommons
*
* @param array $notification
*/
public function hcommons_set_notification_society_meta( $notification ) {
hcommons_write_error_log( 'info', '****SET_NOTIFICATION_SOCIETY_META***-'.var_export( $notification, true ) );
bp_notifications_add_meta( $notification->id, 'society_id', self::$society_id, true );
}
/**
* Set the activity permalink to contain the proper network.
*
* @since HCommons
*
* @param string $link
* @param object $activity Passed by reference.
* @return string $link
*/
public function hcommons_filter_activity_permalink( $link, $activity ) {
$activity_society_id = bp_activity_get_meta( $activity->id, 'society_id', true );
if ( self::$society_id == $activity_society_id ) {
return $link;
}
global $wpdb;
$row = $wpdb->get_row( $wpdb->prepare( "SELECT site_id FROM $wpdb->sitemeta WHERE meta_key = '%s' AND meta_value = '%s'", 'society_id', $activity_society_id ) );
if ( is_object( $row ) ) {
$society_network = get_network( $row->site_id );
$scheme = ( is_ssl() ) ? 'https://' : 'http://';
$activity_root_domain = $scheme . $society_network->domain . $society_network->path;
}
$society_activity_link = str_replace( trailingslashit( bp_get_root_domain() ), $activity_root_domain, $link );
//hcommons_write_error_log( 'info', '****FILTER_ACTIVITY_PERMALINK***-'.$link.'-'.$society_activity_link.'-'.bp_get_root_domain().'-'.self::$society_id.'-'.var_export( $activity->id, true ) );
return $society_activity_link;
}
/**
* Add the current society id to the body classes.
*
* @since HCommons
*
* @param array $classes
* @return array $classes
*/
public function hcommons_society_body_class_name( $classes ) {
if ( hcommons_saml_session_active() ) {
$classes[] = 'active-session';
$user_memberships = self::hcommons_get_user_memberships();
if ( ! in_array( self::$society_id, $user_memberships['societies'] ) ) {
$classes[] = 'non-member';
}
}
$classes[] = 'society-' . self::$society_id;