forked from boonebgorges/buddypress-group-email-subscription
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bp-activity-subscription-functions.php
2470 lines (1912 loc) · 90.4 KB
/
bp-activity-subscription-functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
//
// !SEND EMAIL UPDATES FOR FORUM TOPICS AND POSTS
//
/**
* Returns an unsubscribe link to disable email notifications for a given group and/or all groups.
*/
function ass_group_unsubscribe_links( $user_id ) {
global $bp;
//$settings_link = "{$bp->root_domain}/{$bp->groups->slug}/{$bp->groups->current_group->slug}/notifications/";
//$links = sprintf( __( 'To disable these notifications please log in and go to: %s', 'bp-ass' ), $settings_link );
$userdomain = bp_core_get_user_domain( $user_id );
$group_id = bp_get_current_group_id();
$group_link = "$userdomain?bpass-action=unsubscribe&group={$group_id}&access_key=" . md5( "{$group_id}{$user_id}unsubscribe" . wp_salt() );
$links = sprintf( __( 'To disable all notifications for this group, click: %s', 'bp-ass' ), $group_link );
if ( get_option( 'ass-global-unsubscribe-link' ) == 'yes' ) {
$global_link = "$userdomain?bpass-action=unsubscribe&access_key=" . md5( "{$user_id}unsubscribe" . wp_salt() );
$links .= "\n\n" . sprintf( __( 'Or to disable notifications for *all* your groups, click: %s', 'bp-ass' ), $global_link );
}
$links .= "\n";
return $links;
}
/**
* When a new forum topic or post is posted in bbPress, either:
* 1) Send emails to all group subscribers
* 2) Prepares to record it for digest purposes - see {@link ass_group_forum_record_digest()}.
*
* Hooks into the bbPress action - 'bb_new_post' - to easily identify new forum posts vs edits.
*/
function ass_group_notification_forum_posts( $post_id ) {
global $bp, $wpdb;
$post = bb_get_post( $post_id );
// Check to see if user has been registered long enough
if ( !ass_registered_long_enough( $post->poster_id ) )
return;
$topic = get_topic( $post->topic_id );
$group = groups_get_current_group();
// if the current group isn't available, grab it
if ( empty( $group ) ) {
// get the group ID by looking up the forum ID in the groupmeta table
$group_id = $wpdb->get_var( $wpdb->prepare(
"
SELECT group_id
FROM {$bp->groups->table_name_groupmeta}
WHERE meta_key = %s
AND meta_value = %d
",
'forum_id',
$topic->forum_id
) );
// now get the group
$group = groups_get_group( array(
'group_id' => $group_id
) );
}
$primary_link = trailingslashit( bp_get_group_permalink( $group ) . 'forum/topic/' . $topic->topic_slug );
$blogname = '[' . get_blog_option( BP_ROOT_BLOG, 'blogname' ) . ']';
$is_topic = false;
// initialize faux activity object for backpat filter reasons
//
// due to r-a-y being an idiot here:
// https://github.com/boonebgorges/buddypress-group-email-subscription/commit/526b80c617fe9058a859ac4eb4cfb1d42d333aa0
//
// because we moved the email recording process to 'bb_new_post' from the BP activity save hook,
// we need to ensure that 3rd-party code will continue to work as-is
//
// we can't add the 'id' because we're firing the filters before the activity item is created :(
$activity = new stdClass;
$activity->user_id = $post->poster_id;
$activity->component = 'groups';
$activity->item_id = $group->id;
$activity->content = $post->post_text;
// this is a new topic
if ( $post->post_position == 1 ) {
$is_topic = true;
// more faux activity items!
$activity->type = 'new_forum_topic';
$activity->secondary_item_id = $topic->topic_id;
$activity->primary_link = $primary_link;
$action = $activity->action = sprintf( __( '%s started the forum topic "%s" in the group "%s"', 'bp-ass' ), bp_core_get_user_displayname( $post->poster_id ), $topic->topic_title, $group->name );
$subject = apply_filters( 'bp_ass_new_topic_subject', $action . ' ' . $blogname, $action, $blogname );
$the_content = apply_filters( 'bp_ass_new_topic_content', $post->post_text, $activity, $topic, $group );
}
// this is a forum reply
else {
// more faux activity items!
$activity->type = 'new_forum_post';
$activity->secondary_item_id = $post_id;
$action = $activity->action = sprintf( __( '%s replied to the forum topic "%s" in the group "%s"', 'bp-ass' ), bp_core_get_user_displayname( $post->poster_id ), $topic->topic_title, $group->name );
// calculate the topic page for pagination purposes
$pag_num = apply_filters( 'bp_ass_topic_pag_num', 15 );
$page = ceil( $topic->topic_posts / $pag_num );
if ( $page > 1 )
$primary_link .= '?topic_page=' . $page;
$primary_link .= "#post-" . $post_id;
$activity->primary_link = $primary_link;
$subject = apply_filters( 'bp_ass_forum_reply_subject', $action . ' ' . $blogname, $action, $blogname );
$the_content = apply_filters( 'bp_ass_forum_reply_content', $post->post_text, $activity, $topic, $group );
}
// Convert entities and do other cleanup
$the_content = ass_clean_content( $the_content );
// if group is not public, change primary link to login URL to verify
// authentication and for easier redirection after logging in
if ( $group->status != 'public' ) {
$primary_link = ass_get_login_redirect_url( $primary_link, 'legacy_forums_view' );
$text_before_primary = __( 'To view or reply to this topic, go to:', 'bp-ass' );
// if public, show standard text
} else {
$text_before_primary = __( 'To view or reply to this topic, log in and go to:', 'bp-ass' );
}
// setup the email meessage
$message = sprintf(__('%s
"%s"
%s
%s
---------------------
', 'bp-ass'), $action . ':', $the_content, $text_before_primary, $primary_link);
// get subscribed users
$subscribed_users = groups_get_groupmeta( $group->id, 'ass_subscribed_users' );
// do this for forum replies only
if ( ! $is_topic ) {
// pre-load these arrays to reduce db calls in the loop
$ass_replies_to_my_topic = ass_user_settings_array( 'ass_replies_to_my_topic' );
$ass_replies_after_me_topic = ass_user_settings_array( 'ass_replies_after_me_topic' );
$previous_posters = ass_get_previous_posters( $post->topic_id );
// make sure manually-subscribed topic users and regular group subscribed users are combined
$user_topic_status = groups_get_groupmeta( $group->id, 'ass_user_topic_status_' . $topic->topic_id );
if ( ! empty( $subscribed_users ) && ! empty( $user_topic_status ) )
$subscribed_users = $subscribed_users + $user_topic_status;
// consolidate the arrays to speed up processing
foreach ( array_keys( $previous_posters ) as $previous_poster ) {
if ( empty( $subscribed_users[$previous_poster] ) )
$subscribed_users[$previous_poster] = 'prev-post';
}
}
// setup our temporary GES object
$bp->ges = new stdClass;
$bp->ges->items = array();
// digest key iterator
$d = 0;
// now let's either send the email or record it for digest purposes
foreach ( (array) $subscribed_users as $user_id => $group_status ) {
$self_notify = '';
// Does the author want updates of their own forum posts?
if ( $user_id == $post->poster_id ) {
$self_notify = ass_self_post_notification( $user_id );
// Author does not want notifications of their own posts
if ( ! $self_notify ) {
continue;
}
}
$send_it = $notice = false;
// default settings link
$settings_link = ass_get_login_redirect_url( trailingslashit( bp_get_group_permalink( $group ) . 'notifications' ), 'legacy_forums_settings' );
// Self-notification emails
if ( $self_notify === true ) {
$send_it = true;
$group_status = 'self_notify';
// notification settings link
$settings_link = trailingslashit( bp_core_get_user_domain( $user_id ) . bp_get_settings_slug() ) . 'notifications/';
// set notice
$notice = __( 'You are currently receiving notifications for your own posts.', 'bp-ass' );
$notice .= "\n\n" . sprintf( __( 'To disable these notifications please log in and go to: %s', 'bp-ass' ), $settings_link );
$notice .= "\n" . __( 'Once you are logged in, uncheck "Receive notifications of your own posts?".', 'bp-ass' );
// do the following for new topics
} elseif ( $is_topic ) {
if ( $group_status == 'sub' || $group_status == 'supersub' ) {
$send_it = true;
$notice .= "\n" . __( 'Your email setting for this group is: ', 'bp-ass' ) . ass_subscribe_translate( $group_status );
// until we get a real follow link, this will have to do
if ( $group_status == 'sub' ) {
$notice .= __( ", therefore you won't receive replies to this topic. To get them, click the link to view this topic on the web then click the 'Follow this topic' button.", 'bp-ass' );
}
// user's group setting is "All Mail"
elseif ( $group_status == 'supersub' ) {
$notice .= "\n" . sprintf( __( 'To change your email setting for this group, please log in and go to: %s', 'bp-ass' ), $settings_link );
}
$notice .= "\n\n" . ass_group_unsubscribe_links( $user_id );
}
// do the following for forum replies
} else {
$topic_status = isset( $user_topic_status[$user_id] ) ? $user_topic_status[$user_id] : '';
// the topic mute button will override the subscription options below
if ( $topic_status == 'mute' )
continue;
// skip if user set to weekly summary and they're not following this topic
// maybe not neccesary, but good to be cautious
if ( $group_status == 'sum' && $topic_status != 'sub' )
continue;
// User's group setting is "All Mail", so we should send this
if ( $group_status == 'supersub' ) {
$send_it = true;
$notice = __( 'Your email setting for this group is: ', 'bp-ass' ) . ass_subscribe_translate( $group_status );
$notice .= "\n" . sprintf( __( 'To change your email setting for this group, please log in and go to: %s', 'bp-ass' ), $settings_link );
$notice .= "\n\n" . ass_group_unsubscribe_links( $user_id );
}
// User is manually subscribed to this topic
elseif ( $topic_status == 'sub' ) {
$send_it = true;
$group_status = 'manual_topic';
// change settings link to the forum thread
// get rid of any query args and anchors from the thread permalink
$settings_link = trailingslashit( strtok( $primary_link, '?' ) );
// let's change the notice to accurately reflect that the user is following this topic
$notice = sprintf( __( 'To disable these notifications please log in and go to: %s', 'bp-ass' ), $settings_link );
$notice .= "\n" . __( 'Once you are logged in, click on the "Mute this topic" button to unsubscribe from the forum thread.', 'bp-ass' );
}
// User started the topic and wants to receive email replies to his/her topic
elseif ( $topic->topic_poster == $user_id && isset( $ass_replies_to_my_topic[$user_id] ) && $ass_replies_to_my_topic[$user_id] != 'no' ) {
$send_it = true;
$group_status = 'replies_to_my_topic';
// override settings link to user's notifications
$settings_link = trailingslashit( bp_core_get_user_domain( $user_id ) . bp_get_settings_slug() ) . 'notifications/';
// let's change the notice to accurately reflect that the user is receiving replies based on their settings
$notice = __( 'You are currently receiving notifications to topics that you have started.', 'bp-ass' );
$notice .= "\n\n" . sprintf( __( 'To disable these notifications please log in and go to: %s', 'bp-ass' ), $settings_link );
$notice .= "\n" . __( 'Once you are logged in, uncheck "A member replies in a forum topic you\'ve started".', 'bp-ass' );
}
// User posted in this topic and wants to receive all subsequent replies
elseif ( isset( $previous_posters[$user_id] ) && isset( $ass_replies_after_me_topic[$user_id] ) && $ass_replies_after_me_topic[$user_id] != 'no' ) {
$send_it = true;
$group_status = 'replies_after_me_topic';
// override settings link to user's notifications
$settings_link = trailingslashit( bp_core_get_user_domain( $user_id ) . bp_get_settings_slug() ) . 'notifications/';
// let's change the notice to accurately reflect that the user is receiving replies based on their settings
$notice = __( 'You are currently receiving notifications to topics that you have replied in.', 'bp-ass' );
$notice .= "\n\n" . sprintf( __( 'To disable these notifications please log in and go to: %s', 'bp-ass' ), $settings_link );
$notice .= "\n" . __( 'Once you are logged in, uncheck "A member replies after you in a forum topic".', 'bp-ass' );
}
}
// if we're good to send, send the email!
if ( $send_it ) {
// One last chance to filter the message content
$user_message = apply_filters( 'bp_ass_forum_notification_message', $message . $notice, array(
'message' => $message,
'notice' => $notice,
'user_id' => $user_id,
'subscription_type' => $group_status,
'content' => $the_content,
'view_link' => $primary_link,
'settings_link' => $settings_link
) );
// Get the details for the user
$user = bp_core_get_core_userdata( $user_id );
// Send the email
if ( $user->user_email ) {
wp_mail( $user->user_email, $subject, $user_message );
}
}
// otherwise if digest or summary, record it!
// temporarily save some variables to pass to groups_record_activity()
// actual digest recording occurs in ass_group_forum_record_digest()
if ( $group_status == 'dig' || ( $is_topic && $group_status == 'sum' ) ) {
$bp->ges->items[$d] = new stdClass;
$bp->ges->items[$d]->user_id = $user_id;
$bp->ges->items[$d]->group_id = $group->id;
$bp->ges->items[$d]->group_status = $group_status;
// iterate our key value
++$d;
}
unset( $notice );
}
}
add_action( 'bb_new_post', 'ass_group_notification_forum_posts' );
/**
* Records group forum digest items in GES after the activity item is posted.
*
* {@link ass_group_notification_forum_posts()} handles non-digest sendouts, but
* for digest items, we have to wait for the corresponding activity item to be posted
* before we can record it.
*/
function ass_group_forum_record_digest( $activity ) {
global $bp;
// see if our temporary GES variable is set via ass_group_notification_forum_posts()
if ( ! empty( $bp->ges->items ) ) {
// okay, we're good to go! let's record this digest item!
foreach ( $bp->ges->items as $item ) {
ass_digest_record_activity( $activity->id, $item->user_id, $item->group_id, $item->group_status );
}
// unset the temporary variable
unset( $bp->ges );
}
}
add_action( 'bp_activity_after_save', 'ass_group_forum_record_digest' );
/**
* Records group activity items in GES for all activity except:
* - group forum posts (handled in ass_group_notification_forum_posts())
* - created and joined group entries (irrelevant)
*
* You can do more fine-grained activity filtering with the
* 'ass_block_group_activity_types' filter.
*/
function ass_group_notification_activity( $content ) {
global $bp;
$type = $content->type;
$component = $content->component;
$sender_id = $content->user_id;
// get group activity update replies to work (there is no group id passed in $content, but we can get it from $bp)
if ( $type == 'activity_comment' && bp_is_groups_component() && $component == 'activity' )
$component = 'groups';
// at this point we only want group activity, perhaps later we can make a function and interface for personal activity...
if ( $component != 'groups' )
return;
// if you want to conditionally block certain activity types from appearing,
// use the filter below
if ( false === apply_filters( 'ass_block_group_activity_types', true, $type, $content ) )
return;
if ( !ass_registered_long_enough( $sender_id ) )
return;
$group_id = $content->item_id;
$action = ass_clean_subject( $content->action );
if ( $type == 'activity_comment' ) { // if it's an group activity comment, reset to the proper group id and append the group name to the action
// this will need to be filtered for plugins manually adding group activity comments
$group_id = bp_get_current_group_id();
$action = ass_clean_subject( $content->action ) . ' ' . __( 'in the group', 'bp-ass' ) . ' ' . bp_get_current_group_name();
}
$action = apply_filters( 'bp_ass_activity_notification_action', $action, $content );
// get the group object
// if the group is already set in the $bp global use that, otherwise get the group
$group = groups_get_current_group() ? groups_get_current_group() : groups_get_group( 'group_id=' . $group_id );
/* Subject & Content */
$blogname = '[' . get_blog_option( BP_ROOT_BLOG, 'blogname' ) . ']';
$subject = apply_filters( 'bp_ass_activity_notification_subject', $action . ' ' . $blogname, $action, $blogname );
$the_content = apply_filters( 'bp_ass_activity_notification_content', $content->content, $content, $action, $group );
$the_content = ass_clean_content( $the_content );
/* If it's an activity item, switch the activity permalink to the group homepage rather than the user's homepage */
$activity_permalink = ( isset( $content->primary_link ) && $content->primary_link != bp_core_get_user_domain( $content->user_id ) ) ? $content->primary_link : bp_get_group_permalink( $group );
// If message has no content (as in the case of group joins, etc), we'll use a different
// $message template
if ( empty( $the_content ) ) {
$message = sprintf( __(
'%s
To view or reply, log in and go to:
%s
---------------------
', 'bp-ass' ), $action, $activity_permalink );
} else {
$message = sprintf( __(
'%s
"%s"
To view or reply, log in and go to:
%s
---------------------
', 'bp-ass' ), $action, $the_content, $activity_permalink );
}
// get subscribed users for the group
$subscribed_users = groups_get_groupmeta( $group_id , 'ass_subscribed_users' );
// this is used if a user is subscribed to the "Weekly Summary" option.
// the weekly summary shouldn't record everything, so we have a filter:
//
// 'ass_this_activity_is_important'
//
// this hook can be used by plugin authors to record important activity items
// into the weekly summary
// @see ass_default_weekly_summary_activity_types()
$this_activity_is_important = apply_filters( 'ass_this_activity_is_important', false, $type );
// cycle through subscribed users
foreach ( (array)$subscribed_users as $user_id => $group_status ) {
//echo '<p>uid: ' . $user_id .' | gstat: ' . $group_status ;
$self_notify = '';
// Does the author want updates of their own forum posts?
if ( $type == 'bbp_topic_create' || $type == 'bbp_reply_create' ) {
if ( $user_id == $sender_id ) {
$self_notify = ass_self_post_notification( $user_id );
// Author does not want notifications of their own posts
if ( ! $self_notify ) {
continue;
}
}
// If this is an activity comment, and the $user_id is the user who is being replied
// to, check to make sure that the user is not subscribed to BP's native activity
// reply notifications
} elseif ( 'activity_comment' == $type ) {
// First, look at the immediate parent
$immediate_parent = new BP_Activity_Activity( $content->secondary_item_id );
// Don't send the bp-ass notification if the user is subscribed through BP
if ( $user_id == $immediate_parent->user_id && 'no' != bp_get_user_meta( $user_id, 'notification_activity_new_reply', true ) ) {
continue;
}
// We only need to check the root parent if it's different from the
// immediate parent
if ( $content->secondary_item_id != $content->item_id ) {
$root_parent = new BP_Activity_Activity( $content->item_id );
// Don't send the bp-ass notification if the user is subscribed through BP
if ( $user_id == $root_parent->user_id && 'no' != bp_get_user_meta( $user_id, 'notification_activity_new_reply', true ) ) {
continue;
}
}
}
$send_it = false;
// Self-notification email for bbPress posts
if ( $self_notify === true ) {
$send_it = true;
$group_status = 'self_notify';
// notification settings link
$settings_link = trailingslashit( bp_core_get_user_domain( $user_id ) . bp_get_settings_slug() ) . 'notifications/';
// set notice
$notice = __( 'You are currently receiving notifications for your own posts.', 'bp-ass' );
$notice .= "\n\n" . sprintf( __( 'To disable these notifications please log in and go to: %s', 'bp-ass' ), $settings_link );
$notice .= "\n" . __( 'Once you are logged in, uncheck "Receive notifications of your own posts?".', 'bp-ass' );
// User is subscribed to "All Mail"
// OR user is subscribed to "New Topics" (bbPress 2)
} elseif ( $group_status == 'supersub' || ( $group_status == 'sub' && $type == 'bbp_topic_create' ) ) {
// if someone is signed up for all email and they post a group update, they should not receive an email
if ( 'activity_update' == $type && $sender_id === $user_id ) {
continue;
}
$send_it = true;
$settings_link = ass_get_login_redirect_url( trailingslashit( bp_get_group_permalink( $group ) . 'notifications' ), $group_status );
$notice = __( 'Your email setting for this group is: ', 'bp-ass' ) . ass_subscribe_translate( $group_status );
$notice .= "\n" . sprintf( __( 'To change your email setting for this group, please log in and go to: %s', 'bp-ass' ), $settings_link );
$notice .= "\n\n" . ass_group_unsubscribe_links( $user_id );
}
// if we're good to send, send the email!
if ( $send_it ) {
// One last chance to filter the message content
$user_message = apply_filters( 'bp_ass_activity_notification_message', $message . $notice, array(
'message' => $message,
'notice' => $notice,
'user_id' => $user_id,
'subscription_type' => $group_status,
'content' => $the_content,
'settings_link' => ! empty( $settings_link ) ? $settings_link : '',
) );
// Get the details for the user
$user = bp_core_get_core_userdata( $user_id );
// Send the email
if ( $user->user_email ) {
wp_mail( $user->user_email, $subject, $user_message );
}
}
// otherwise, user is subscribed to "Daily Digest" so record item in digest!
// OR user is subscribed to "Weekly Summary" and activity item is important
// enough to be recorded
if ( $group_status == 'dig' || ( $group_status == 'sum' && $this_activity_is_important ) ) {
ass_digest_record_activity( $content->id, $user_id, $group_id, $group_status );
}
}
//echo '<p>Subject: ' . $subject;
//echo '<pre>'; print_r( $message ); echo '</pre>';
}
add_action( 'bp_activity_after_save' , 'ass_group_notification_activity' , 50 );
/**
* Activity edit checker.
*
* Catch attempts to save activity entries to see if they already exist.
* If they do exist, stop GES from doing its thang.
*
* @since 3.2.2
*/
function ass_group_activity_edits( $activity ) {
// hack to avoid duplicate action firing during activity saving
// @see https://buddypress.trac.wordpress.org/ticket/3980
static $run_once = false;
if ( ! empty( $run_once ) )
return;
// if the activity doesn't match the groups component, stop now
if ( $activity->component != 'groups' )
return;
// if the activity ID already exists, this means this is an edit
// we don't want GES to send emails for edits!
if ( ! empty( $activity->id ) ) {
// Make sure GES doesn't fire
remove_action( 'bp_activity_after_save', 'ass_group_notification_activity', 50 );
}
$run_once = true;
}
add_action( 'bp_activity_before_save', 'ass_group_activity_edits' );
/**
* Block some activity types from being sent / recorded in groups.
*
* @since 3.2.2
*/
function ass_default_block_group_activity_types( $retval, $type, $activity ) {
switch( $type ) {
/** ACTIVITY TYPES TO BLOCK **************************************/
// we handle these in ass_group_notification_forum_posts()
case 'new_forum_topic' :
case 'new_forum_post' :
// @todo in the future, it might be nice for admins to optionally get this message
case 'joined_group' :
case 'created_group' :
return false;
break;
/** bbPress 2 ****************************************************/
// groan! bbPress 2 hacks!
//
// when bbPress first records an item into the group activity stream, it is
// incomplete as it is first recorded on the 'wp_insert_post' action
//
// it is later updated on the 'bbp_new_reply' / 'bbp_new_topic' action
//
// we want to block the first instance, so GES doesn't record or send this
// incomplete activity item
// reply
case 'bbp_reply_create' :
// to determine if the reply activity item is incomplete, the primary link
// will be missing the scheme (HTTP) and host (example.com), so our hack does
// a search for '://' because the site could be using HTTPS.
if ( strpos( $activity->primary_link, '://' ) === false ) {
return false;
// we're okay again!
} else {
return $retval;
}
break;
// topic
case 'bbp_topic_create' :
// to determine if the topic activity item is incomplete, the primary link
// will be missing the groups root slug
if ( strpos( $activity->primary_link, '/' . bp_get_groups_root_slug() . '/' ) === false ) {
return false;
// we're okay again!
} else {
return $retval;
}
break;
/** ALL OTHER TYPES **********************************************/
default :
return $retval;
break;
}
}
add_filter( 'ass_block_group_activity_types', 'ass_default_block_group_activity_types', 5, 3 );
/**
* Allow certain activity types to be recorded for users subscribed to the
* "Weekly Summary" option.
*
* The rationale behind this is the weekly summary shouldn't record every
* single activity item because the summary could get rather long.
*
* @since 3.2.4
*/
function ass_default_weekly_summary_activity_types( $retval, $type ) {
switch( $type ) {
/** ACTIVITY TYPES TO RECORD FOR WEEKLY SUMMARY ******************/
// backpat items
case 'wiki_group_page_create' :
case 'new_calendar_event' :
// bbPress 2 forum topic
case 'bbp_topic_create' :
// activity update
case 'activity_update' :
return true;
break;
/** ALL OTHER TYPES **********************************************/
default :
return $retval;
break;
}
}
add_filter( 'ass_this_activity_is_important', 'ass_default_weekly_summary_activity_types', 1, 2 );
/**
* Login redirector.
*
* If group is not public, the group link in the email will use {@link wp_login_url()}.
*
* If a user clicks on this link and is already logged in, we should attempt
* to redirect the user to the authorized content instead of forcing the user
* to re-authenticate.
*
* @since 3.2.4
*
* @uses bp_loggedin_user_id() To see if a user is logged in
*/
function ass_login_redirector() {
// see if a redirect link was passed
if ( empty( $_GET['redirect_to'] ) )
return;
// see if our special 'auth' variable was passed
if( empty( $_GET['auth'] ) )
return;
// if user is *not* logged in, stop now!
if ( ! bp_loggedin_user_id() )
return;
// user is logged in, so let's redirect them to the content
wp_safe_redirect( esc_url_raw( $_GET['redirect_to'] ) );
exit;
}
add_action( 'login_init', 'ass_login_redirector', 1 );
/**
* Returns the login URL with a redirect link.
*
* Pass the link you want the user to redirect to when authenticated.
*
* Redirection occurs in {@link ass_login_redirector()}.
*
* @since 3.4
*
* @param string $url The URL you want to redirect to.
* @param string $context The context of the redirect.
* @return mixed String of the login URL with the passed redirect link. Boolean false on failure.
*/
function ass_get_login_redirect_url( $url = '', $context = '' ) {
$url = esc_url_raw( $url );
if ( empty( $url ) ) {
return false;
}
// setup query args
$query_args = array(
'action' => 'bpnoaccess',
'auth' => 1,
'redirect_to' => apply_filters( 'ass_login_redirect_to', urlencode( $url ), $context )
);
return add_query_arg(
$query_args,
apply_filters( 'ass_login_url', wp_login_url() )
);
}
//
// !GROUP SUBSCRIPTION
//
// returns the subscription status of a user in a group
function ass_get_group_subscription_status( $user_id, $group_id ) {
global $bp;
if ( !$user_id )
$user_id = bp_loggedin_user_id();
if ( !$group_id )
$group_id = bp_get_current_group_id();
$group_user_subscriptions = groups_get_groupmeta( $group_id, 'ass_subscribed_users' );
$user_subscription = isset( $group_user_subscriptions[$user_id] ) ? $group_user_subscriptions[$user_id] : false;
return $user_subscription;
}
// updates the group's user subscription list.
function ass_group_subscription( $action, $user_id, $group_id ) {
if ( !$action || !$user_id || !$group_id )
return false;
$group_user_subscriptions = groups_get_groupmeta( $group_id , 'ass_subscribed_users' );
// we're being overly careful here
if ( $action == 'no' ) {
$group_user_subscriptions[ $user_id ] = 'no';
} elseif ( $action == 'sum' ) {
$group_user_subscriptions[ $user_id ] = 'sum';
} elseif ( $action == 'dig' ) {
$group_user_subscriptions[ $user_id ] = 'dig';
} elseif ( $action == 'sub' ) {
$group_user_subscriptions[ $user_id ] = 'sub';
} elseif ( $action == 'supersub' ) {
$group_user_subscriptions[ $user_id ] = 'supersub';
} elseif ( $action == 'delete' ) {
if ( isset( $group_user_subscriptions[ $user_id ] ) )
unset( $group_user_subscriptions[ $user_id ] );
}
groups_update_groupmeta( $group_id , 'ass_subscribed_users', $group_user_subscriptions );
// add a hook for 3rd-party plugin devs
do_action( 'ass_group_subscription', $user_id, $group_id, $action );
}
// show group subscription settings on the notification page.
function ass_group_subscribe_settings () {
global $bp;
$group = groups_get_current_group();
if ( !is_user_logged_in() || !empty( $group->is_banned ) || !$group->is_member )
return false;
$group_status = ass_get_group_subscription_status( bp_loggedin_user_id(), $group->id );
$submit_link = bp_get_groups_action_link( 'notifications' );
?>
<div id="ass-email-subscriptions-options-page">
<h3 class="activity-subscription-settings-title"><?php _e('Email Subscription Options', 'bp-ass') ?></h3>
<form action="<?php echo $submit_link ?>" method="post">
<input type="hidden" name="ass_group_id" value="<?php echo $group->id; ?>"/>
<?php wp_nonce_field( 'ass_subscribe' ); ?>
<b><?php _e('How do you want to read this group?', 'bp-ass'); ?></b>
<div class="ass-email-type">
<label><input type="radio" name="ass_group_subscribe" value="no" <?php if ( $group_status == "no" || $group_status == "un" || !$group_status ) echo 'checked="checked"'; ?>><?php _e('No Email', 'bp-ass'); ?></label>
<div class="ass-email-explain"><?php _e('I will read this group on the web', 'bp-ass'); ?></div>
</div>
<div class="ass-email-type">
<label><input type="radio" name="ass_group_subscribe" value="sum" <?php if ( $group_status == "sum" ) echo 'checked="checked"'; ?>><?php _e('Weekly Summary Email', 'bp-ass'); ?></label>
<div class="ass-email-explain"><?php _e('Get a summary of new topics each week', 'bp-ass'); ?></div>
</div>
<div class="ass-email-type">
<label><input type="radio" name="ass_group_subscribe" value="dig" <?php if ( $group_status == "dig" ) echo 'checked="checked"'; ?>><?php _e('Daily Digest Email', 'bp-ass'); ?></label>
<div class="ass-email-explain"><?php _e('Get all the day\'s activity bundled into a single email', 'bp-ass'); ?></div>
</div>
<?php if ( ass_get_forum_type() ) : ?>
<div class="ass-email-type">
<label><input type="radio" name="ass_group_subscribe" value="sub" <?php if ( $group_status == "sub" ) echo 'checked="checked"'; ?>><?php _e('New Topics Email', 'bp-ass'); ?></label>
<div class="ass-email-explain"><?php _e('Send new topics as they arrive (but don\'t send replies)', 'bp-ass'); ?></div>
</div>
<?php endif; ?>
<div class="ass-email-type">
<label><input type="radio" name="ass_group_subscribe" value="supersub" <?php if ( $group_status == "supersub" ) echo 'checked="checked"'; ?>><?php _e('All Email', 'bp-ass'); ?></label>
<div class="ass-email-explain"><?php _e('Send all group activity as it arrives', 'bp-ass'); ?></div>
</div>
<input type="submit" value="<?php _e('Save Settings', 'bp-ass') ?>" id="ass-save" name="ass-save" class="button-primary">
<?php if ( ass_get_forum_type() == 'buddypress' ) : ?>
<p class="ass-sub-note"><?php _e('Note: Normally, you receive email notifications for topics you start or comment on. This can be changed at', 'bp-ass'); ?> <a href="<?php echo bp_loggedin_user_domain() . BP_SETTINGS_SLUG . '/notifications/' ?>"><?php _e('email notifications', 'bp-ass'); ?></a>.</p>
<?php endif; ?>
</form>
</div><!-- end ass-email-subscriptions-options-page -->
<?php
}
// update the users' notification settings
function ass_update_group_subscribe_settings() {
global $bp;
if ( bp_is_groups_component() && bp_is_current_action( 'notifications' ) ) {
// If the edit form has been submitted, save the edited details
if ( isset( $_POST['ass-save'] ) ) {
//if ( !wp_verify_nonce( $nonce, 'ass_subscribe' ) ) die( 'A Security check failed' );
$user_id = bp_loggedin_user_id();
$group_id = $_POST[ 'ass_group_id' ];
$action = $_POST[ 'ass_group_subscribe' ];
if ( !groups_is_user_member( $user_id, $group_id ) )
return;
ass_group_subscription( $action, $user_id, $group_id ); // save the settings
bp_core_add_message( sprintf( __( 'Your email notifications are set to %s for this group.', 'bp-ass' ), ass_subscribe_translate( $action ) ) );
bp_core_redirect( trailingslashit( bp_get_group_permalink( groups_get_current_group() ) . 'notifications' ) );
}
}
}
add_action( 'bp_actions', 'ass_update_group_subscribe_settings' );
// translate the short code subscription status into a nicer version
function ass_subscribe_translate( $status ){
if ( $status == 'no' || !$status )
$output = __('No Email', 'bp-ass');
elseif ( $status == 'sum' )
$output = __('Weekly Summary', 'bp-ass');
elseif ( $status == 'dig' )
$output = __('Daily Digest', 'bp-ass');
elseif ( $status == 'sub' )
$output = __('New Topics', 'bp-ass');
elseif ( $status == 'supersub' )
$output = __('All Email', 'bp-ass');
return $output;
}
// this adds the ajax-based subscription option in the group header, or group directory
function ass_group_subscribe_button() {
global $bp, $groups_template;
if( ! empty( $groups_template ) ) {
$group =& $groups_template->group;
}
else {
$group = groups_get_current_group();
}
if ( !is_user_logged_in() || !empty( $group->is_banned ) || !$group->is_member )
return;
// if we're looking at someone elses list of groups hide the subscription
if ( bp_displayed_user_id() && ( bp_loggedin_user_id() != bp_displayed_user_id() ) )
return;
$group_status = ass_get_group_subscription_status( bp_loggedin_user_id(), $group->id );
if ( $group_status == 'no' )
$group_status = NULL;
$status_desc = __('Your email status is ', 'bp-ass');
$link_text = __('change', 'bp-ass');
$gemail_icon_class = ' gemail_icon';
$sep = '';
if ( !$group_status ) {
//$status_desc = '';
$link_text = __('Get email updates', 'bp-ass');
$gemail_icon_class = '';
$sep = '';
}
$status = ass_subscribe_translate( $group_status );
?>
<div class="group-subscription-div">
<span class="group-subscription-status-desc"><?php echo $status_desc; ?></span>
<span class="group-subscription-status<?php echo $gemail_icon_class ?>" id="gsubstat-<?php echo $group->id; ?>"><?php echo $status; ?></span> <?php echo $sep; ?>
(<a class="group-subscription-options-link" id="gsublink-<?php echo $group->id; ?>" href="javascript:void(0);" title="<?php _e('Change your email subscription options for this group','bp-ass');?>"><?php echo $link_text; ?></a>)
<span class="ajax-loader" id="gsubajaxload-<?php echo $group->id; ?>"></span>
</div>
<div class="generic-button group-subscription-options" id="gsubopt-<?php echo $group->id; ?>">
<a class="group-sub" id="no-<?php echo $group->id; ?>"><?php _e('No Email', 'bp-ass') ?></a> <?php _e('I will read this group on the web', 'bp-ass') ?><br>
<a class="group-sub" id="sum-<?php echo $group->id; ?>"><?php _e('Weekly Summary', 'bp-ass') ?></a> <?php _e('Get a summary of topics each', 'bp-ass') ?> <?php echo ass_weekly_digest_week(); ?><br>
<a class="group-sub" id="dig-<?php echo $group->id; ?>"><?php _e('Daily Digest', 'bp-ass') ?></a> <?php _e('Get the day\'s activity bundled into one email', 'bp-ass') ?><br>
<?php if ( ass_get_forum_type() ) : ?>
<a class="group-sub" id="sub-<?php echo $group->id; ?>"><?php _e('New Topics', 'bp-ass') ?></a> <?php _e('Send new topics as they arrive (but no replies)', 'bp-ass') ?><br>
<?php endif; ?>
<a class="group-sub" id="supersub-<?php echo $group->id; ?>"><?php _e('All Email', 'bp-ass') ?></a> <?php _e('Send all group activity as it arrives', 'bp-ass') ?><br>
<a class="group-subscription-close" id="gsubclose-<?php echo $group->id; ?>"><?php _e('close', 'bp-ass') ?></a>
</div>