forked from boonebgorges/buddypress-group-email-subscription
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bp-activity-subscription-functions.php
2567 lines (2120 loc) · 85.3 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
/**
* Core functions for GES.
*
* @since 1.0.0
*/
//
// !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', 'buddypress-group-email-subscription' ), $settings_link );
$links = sprintf( __( 'To disable all notifications for this group, click: %s', 'buddypress-group-email-subscription' ), ass_get_group_unsubscribe_link_for_user( $user_id ) );
if ( get_option( 'ass-global-unsubscribe-link' ) == 'yes' ) {
$links .= "\n\n" . sprintf( __( 'Or to disable notifications for *all* your groups, click: %s', 'buddypress-group-email-subscription' ), ass_get_group_unsubscribe_link_for_user( $user_id, 0, true ) );
}
$links .= "\n";
return $links;
}
/**
* Get the group unsubscribe link for a user.
*
* @since 3.7.0
*
* @param int $user_id WP user ID.
* @param int $group_id BuddyPress group ID.
* @param bool $global Should we use the global unsubscribe link? If 'false', we will use the
* single group's unsubscribe link. Default: false.
* @return string|bool URL for unsubscribe link on success; boolean false on failure.
*/
function ass_get_group_unsubscribe_link_for_user( $user_id = 0, $group_id = 0, $global = false ) {
if ( empty( $user_id ) ) {
return false;
}
$args = array(
'bpass-action' => 'unsubscribe',
);
// Use global unsubscribe link.
if ( true === $global ) {
$access_key = md5( "{$user_id}unsubscribe" . wp_salt() );
// Single group unsubscribe link.
} else {
$group_id = empty( $group_id ) ? bp_get_current_group_id() : (int) $group_id;
if ( empty( $group_id ) ) {
return false;
}
$access_key = md5( "{$group_id}{$user_id}unsubscribe" . wp_salt() );
$args['group'] = $group_id;
}
$args['access_key'] = $access_key;
return esc_url( add_query_arg( $args, bp_core_get_user_domain( $user_id ) ) );
}
/**
* Temporarily save the full activity content before activity KSES kicks in.
*
* At the moment, we only do this for bbPress content since bbPress supports a
* larger amount of elements than BuddyPress' activity KSES filter.
*
* @since 3.7.0
*
* @param string $retval Current activity content.
* @param BP_Activity_Activity $activity Activity object.
* @return string
*/
function ass_group_notification_activity_content_before_save( $retval = '', BP_Activity_Activity $activity ) {
// If not bbPress content, bail.
if ( 0 !== strpos( $activity->type, 'bbp_' ) ) {
return $retval;
}
// Temporarily save bbPress content.
$GLOBALS['bp']->ges_content = $retval;
return $retval;
}
add_filter( 'bp_activity_content_before_save', 'ass_group_notification_activity_content_before_save', -999, 2 );
/**
* 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( BP_Activity_Activity $activity ) {
if ( 'groups' !== $activity->component ) {
return;
}
/**
* Allows activity item queuing to be prevented.
*
* @param bool $send Defaults to true. Return false to stop this item from being queued.
* @param string $type Activity type.
* @param BP_Activity_Activity $activity Activity item.
*/
if ( false === apply_filters( 'ass_block_group_activity_types', true, $activity->type, $activity ) )
return;
if ( ! ass_registered_long_enough( $activity->user_id ) ) {
return;
}
// activity_comment must have its group ID inferred.
// @todo This can be made less terrible.
$group_id = $activity->item_id;
if ( 'activity_comment' === $activity->type ) {
$group_id = bp_get_current_group_id();
}
// No group, nothing to do.
$group = groups_get_group( array( 'group_id' => $group_id ) );
if ( ! $group->id ) {
return;
}
/**
* Forces an item to be "important", ie not excluded from weekly summaries.
*
* @param bool $is_important Defaults to false.
* @param string $type Activity type.
*/
$this_activity_is_important = apply_filters( 'ass_this_activity_is_important', false, $activity->type );
// If we've gotten this far, record the activity item for each subscribed group member.
$to_queue = array();
$subscribed_users = ass_get_subscriptions_for_group( $group_id );
foreach ( $subscribed_users as $user_id => $subscription_type ) {
$self_notify = false;
// Does the author want updates of their own forum posts?
if ( $activity->type == 'bbp_topic_create' || $activity->type == 'bbp_reply_create' ) {
if ( $user_id === $activity->user_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' == $activity->type ) {
// First, look at the immediate parent
$immediate_parent = new BP_Activity_Activity( $activity->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 ( $activity->secondary_item_id !== $activity->item_id ) {
$root_parent = new BP_Activity_Activity( $activity->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_immediately = $add_to_digest_queue = false;
if (
( true === $self_notify ) ||
( 'supersub' === $subscription_type ) ||
( 'sub' === $subscription_type && 'bbp_topic_create' === $activity->type )
) {
$send_immediately = true;
}
// Special case: users should not get immediate notifications of their own group activity updates.
if ( 'activity_update' === $activity->type && $activity->user_id === $user_id ) {
$send_immediately = false;
}
if (
( 'dig' === $subscription_type ) ||
( 'sum' === $subscription_type && $this_activity_is_important )
) {
$add_to_digest_queue = true;
}
// Check whether user preferences should be overridden for admin notices.
if ( 'bpges_notice' === $activity->type && bpges_force_immediate_admin_notice( $user_id, $activity, $group_id ) ) {
$send_immediately = true;
$add_to_digest_queue = false;
}
/**
* Filters whether a given user should receive immediate notification of the current activity.
*
* @since 3.6.0
* @since 3.8.0 Added `$subscription_type` parameter.
*
* @param bool $send_immediately True to send an immediate email notification, false otherwise.
* @param object $activity Activity object.
* @param int $user_id ID of the user.
* @param string $subscription_type Group subscription status for the current user.
*/
$send_immediately = apply_filters( 'bp_ass_send_activity_notification_for_user', $send_immediately, $activity, $user_id, $subscription_type );
/**
* Filters whether to add the current activity item to the digest queue for the current user.
*
* @since 3.8.0
*
* @param bool $add_to_digest_queue True to send an immediate email notification, false otherwise.
* @param object $activity_obj Activity object.
* @param int $user_id ID of the user.
* @param string $subscription_type Group subscription status for the current user.
*/
$add_to_digest_queue = apply_filters( 'bp_ges_add_to_digest_queue_for_user', $add_to_digest_queue, $activity, $user_id, $subscription_type );
if ( $send_immediately ) {
$to_queue[] = array(
'user_id' => $user_id,
'group_id' => $group_id,
'activity_id' => $activity->id,
'type' => 'immediate',
'date_recorded' => date( 'Y-m-d H:i:s' ),
);
}
if ( $add_to_digest_queue ) {
$to_queue[] = array(
'user_id' => $user_id,
'group_id' => $group_id,
'activity_id' => $activity->id,
'type' => $subscription_type,
'date_recorded' => date( 'Y-m-d H:i:s' ),
);
}
}
// Bulk insert.
if ( $to_queue ) {
BPGES_Queued_Item::bulk_insert( $to_queue );
// Trigger the batch process.
bpges_send_queue()->data( array(
'type' => 'immediate',
'activity_id' => $activity->id,
) )->dispatch();
}
}
add_action( 'bp_activity_after_save' , 'ass_group_notification_activity' , 50 );
/**
* Generate and send a group notification.
*
* @since 3.6.0
*
* @param array $args {
* @type int $group_id ID of the group.
* @type int $sender_id ID of the user triggering the activity.
* @type int $activity_id ID of the activity item being triggered.
* @type string $action Activity action. Used to generate the email subject.
* @type string $content Activity content. Used to generate the email content.
* @type string $link Primary link for the activity item.
* }
*/
function ass_generate_notification( $args = array() ) {
$r = wp_parse_args( $args, array(
'group_id' => null,
'sender_id' => null,
'activity_id' => null,
'action' => null,
'content' => null,
'link' => null,
) );
$group = groups_get_group( array( 'group_id' => $r['group_id'] ) );
if ( ! $group->id ) {
return;
}
$activity_obj = new BP_Activity_Activity( $r['activity_id'] );
/* Subject & Content */
$blogname = '[' . get_blog_option( BP_ROOT_BLOG, 'blogname' ) . ']';
$subject = apply_filters( 'bp_ass_activity_notification_subject', $r['action'] . ' ' . $blogname, $r['action'], $blogname );
$the_content = apply_filters( 'bp_ass_activity_notification_content', $r['content'], $activity_obj, $r['action'], $group );
$the_content = ass_clean_content( $the_content, $activity_obj );
// 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( __(
'%1$s
To view or reply, log in and go to:
%2$s
---------------------
', 'buddypress-group-email-subscription' ), $r['action'], $r['link'] );
} else {
$message = sprintf( __(
'%1$s
"%2$s"
To view or reply, log in and go to:
%3$s
---------------------
', 'buddypress-group-email-subscription' ), $r['action'], $the_content, $r['link'] );
}
// Use bbPress filtered post content and reapply GES filter... sigh.
if ( 0 === strpos( $activity_obj->type, 'bbp_' ) || 'new_groupblog_post' === $activity_obj->type ) {
// Not in global cache? Query for post content.
if ( empty( $GLOBALS['bp']->ges_content ) ) {
$switched = false;
if ( 'new_groupblog_post' === $activity_obj->type && function_exists( 'get_groupblog_blog_id' ) ) {
$switched = true;
switch_to_blog( get_groupblog_blog_id( $group->id ) );
}
$the_content = get_post_field( 'post_content', $activity_obj->secondary_item_id, 'raw' );
if ( true === $switched ) {
restore_current_blog();
}
} else {
$the_content = $GLOBALS['bp']->ges_content;
unset( $GLOBALS['bp']->ges_content );
}
// Apply bbPress KSES filter if it exists (sanity check!)
$the_content = ( true === function_exists( 'bbp_filter_kses' ) ) ? wp_unslash( bbp_filter_kses( $the_content ) ) : $the_content;
$the_content = apply_filters( 'bp_ass_activity_notification_content', $the_content, $activity_obj, $r['action'], $group );
}
// get subscribed users for the group
$subscribed_users = ass_get_subscriptions_for_group( $r['group_id'] );
// 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, $activity_obj->type );
// cycle through subscribed users
foreach ( (array) $subscribed_users as $user_id => $group_status ) {
$self_notify = false;
// If user is banned from group, do not send mail.
if ( groups_is_user_banned( $user_id, $group->id ) ) {
continue;
}
// Does the author want updates of their own forum posts?
if ( $activity_obj->type == 'bbp_topic_create' || $activity_obj->type == 'bbp_reply_create' ) {
if ( $user_id == $r['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' == $activity_obj->type ) {
// First, look at the immediate parent
$immediate_parent = new BP_Activity_Activity( $activity_obj->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 ( $activity_obj->secondary_item_id != $activity_obj->item_id ) {
$root_parent = new BP_Activity_Activity( $activity_obj->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_immediately = $add_to_digest_queue = false;
if (
( true === $self_notify ) ||
( 'supersub' === $group_status ) ||
( 'sub' === $group_status && 'bbp_topic_create' === $activity_obj->type )
) {
$send_immediately = true;
}
// Special case: users should not get immediate notifications of their own group activity updates.
if ( 'activity_update' === $activity_obj->type && $r['sender_id'] === $user_id ) {
$send_immediately = false;
}
if (
( 'dig' === $group_status ) ||
( 'sum' === $group_status && $this_activity_is_important )
) {
$add_to_digest_queue = true;
}
/**
* Filters whether a given user should receive immediate notification of the current activity.
*
* @since 3.6.0
* @since 3.8.0 Added `$group_status` parameter.
*
* @param bool $send_immediately True to send an immediate email notification, false otherwise.
* @param object $activity_obj Activity object.
* @param int $user_id ID of the user.
* @param string $group_status Group subscription status for the current user.
*/
$send_immediately = apply_filters( 'bp_ass_send_activity_notification_for_user', $send_immediately, $activity_obj, $user_id, $group_status );
/**
* Filters whether to add the current activity item to the digest queue for the current user.
*
* @since 3.8.0
*
* @param bool $add_to_digest_queue True to send an immediate email notification, false otherwise.
* @param object $activity_obj Activity object.
* @param int $user_id ID of the user.
* @param string $group_status Group subscription status for the current user.
*/
$add_to_digest_queue = apply_filters( 'bp_ges_add_to_digest_queue_for_user', $add_to_digest_queue, $activity_obj, $user_id, $group_status );
$raw_group_status = $group_status;
// Assemble variables for use in building immediate notification, if necessary.
if ( $send_immediately ) {
// Self-notification email for bbPress posts
if ( true === $self_notify ) {
$group_status = 'self_notify';
// notification settings link
$settings_link = trailingslashit( bp_core_get_user_domain( $user_id ) . bp_get_settings_slug() ) . 'notifications/#groups-subscription-notification-settings';
// set notice
$notice = $email_setting_desc = __( 'You are currently receiving notifications for your own posts.', 'buddypress-group-email-subscription' );
$email_setting_links = sprintf( __( 'To disable these notifications please log in and go to: %s', 'buddypress-group-email-subscription' ), $settings_link );
$email_setting_links .= "\n\n" . __( 'Once you are logged in, uncheck "Receive notifications of your own posts?".', 'buddypress-group-email-subscription' );
$notice .= "\n\n" . $email_setting_links;
} else {
$settings_link = ass_get_login_redirect_url( trailingslashit( bp_get_group_permalink( $group ) . 'notifications' ), $group_status );
$email_setting_string = __( 'Your email setting for this group is: %s', 'buddypress-group-email-subscription' );
$group_status_string = ass_subscribe_translate( $group_status );
$notice = sprintf( $email_setting_string, $group_status_string );
$email_setting_desc = sprintf( $email_setting_string, '<strong> ' . $group_status_string . '</strong>' );
$email_setting_links = sprintf( __( 'To change your email setting for this group, please log in and go to: %s', 'buddypress-group-email-subscription' ), $settings_link );
$email_setting_links .= "\n\n" . ass_group_unsubscribe_links( $user_id );
$notice .= "\n" . $email_setting_links;
}
}
// if we're good to send, send the email!
if ( $send_immediately ) {
$user_message_args = array(
'message' => $message,
'notice' => $notice,
'user_id' => $user_id,
'subscription_type' => $group_status,
'content' => $the_content,
'settings_link' => ! empty( $settings_link ) ? $settings_link : '',
);
// One last chance to filter the message content
$user_message = apply_filters( 'bp_ass_activity_notification_message', $message . $notice, $user_message_args );
// Get the details for the user
$user = bp_core_get_core_userdata( $user_id );
// Send the email
if ( $user->user_email ) {
// Custom GES email tokens.
$user_message_args['ges.action'] = stripslashes( $activity_obj->action ); // Unfiltered.
$user_message_args['ges.subject'] = strip_tags( stripslashes( $r['action'] ) ); // Unfiltered.
$user_message_args['ges.email-setting-description'] = $email_setting_desc;
$user_message_args['ges.email-setting-links'] = $email_setting_links;
$user_message_args['ges.unsubscribe-global'] = ass_get_group_unsubscribe_link_for_user( $user->ID, $r['group_id'], true );
$user_message_args['ges.unsubscribe'] = ass_get_group_unsubscribe_link_for_user( $user->ID, $r['group_id'] );
$user_message_args['ges.settings-link'] = $user_message_args['settings_link'];
$user_message_args['poster.url'] = bp_core_get_user_domain( $r['sender_id'] );
$user_message_args['recipient.id'] = $user->ID;
// BP-specific tokens.
$user_message_args['usermessage'] = $the_content;
$user_message_args['poster.name'] = bp_core_get_user_displayname( $r['sender_id'] );
$user_message_args['thread.url'] = $r['link'];
$user_message_args['group.id'] = $r['group_id'];
// Remove tokens that we're not using.
unset( $user_message_args['content'], $user_message_args['notice'], $user_message_args['message'], $user_message_args['settings_link'] );
// Add activity KSES filter if not bbPress or groupblog item.
if ( false === strpos( $activity->type, 'bbp_' ) && 'new_groupblog_post' !== $activity->type ) {
add_filter( 'bp_email_set_content_html', 'bp_activity_filter_kses', 6 );
}
// Sending time!
ass_send_email( 'bp-ges-single', $user->user_email, array(
'tokens' => $user_message_args,
'subject' => $subject,
'content' => $user_message,
'activity' => $activity_obj
) );
// Revert!
if ( false === strpos( $activity->type, 'bbp_' ) && 'new_groupblog_post' !== $activity->type ) {
remove_filter( 'bp_email_set_content_html', 'bp_activity_filter_kses', 6 );
}
}
}
// Record in digest queue, if necessary.
if ( $add_to_digest_queue ) {
ass_digest_record_activity( $r['activity_id'], $user_id, $r['group_id'], $raw_group_status );
}
}
}
/**
* Generate an immediate email notification for an activity + user combo.
*
* @since 3.9.0
*
* @param BPGES_Queued_Item $queued_item Queued item.
*/
function bpges_generate_notification( BPGES_Queued_Item $queued_item ) {
$activity_id = $queued_item->activity_id;
$user_id = $queued_item->user_id;
$group_id = $queued_item->group_id;
// Fetch group subscription.
$sub = new BPGES_Subscription_Query( array(
'user_id' => $user_id,
'group_id' => $group_id,
'per_page' => 1
) );
$sub = $sub->get_results();
$sub = end( $sub );
// Set group status to subscription type.
$group_status = $sub->type;
$activity = new BP_Activity_Activity( $activity_id );
$group = groups_get_group(
array(
'group_id' => $group_id,
)
);
// @todo We can add nonpersistent caching for static content.
if ( 'activity_comment' === $activity->type ) { // 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
$action = ass_clean_subject( $activity->action ) . ' ' . __( 'in the group', 'buddypress-group-email-subscription' ) . ' ' . $group->name;
} else {
$action = ass_clean_subject( $activity->action );
}
/* 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', $activity->content, $activity, $action, $group );
$the_content = ass_clean_content( $the_content, $activity );
/*
* If it's an activity item, switch the activity permalink to the group homepage
* rather than the user's homepage.
*/
$link = bp_get_group_permalink( $group );
if ( $activity->primary_link && $activity->primary_link !== bp_core_get_user_domain( $activity->user_id ) ) {
$link = $activity->primary_link;
}
/**
* Filters the activity link used to build the notification email.
*
* @since 4.0.2
*
* @param string $link
* @param BP_Activity_Activity $activity
*/
$link = apply_filters( 'bpges_notification_link', $link, $activity );
// 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( __(
'%1$s
To view or reply, log in and go to:
%2$s
---------------------
', 'buddypress-group-email-subscription' ), $action, $link );
} else {
$message = sprintf( __(
'%1$s
"%2$s"
To view or reply, log in and go to:
%3$s
---------------------
', 'buddypress-group-email-subscription' ), $action, $the_content, $link );
}
// Use bbPress filtered post content and reapply GES filter... sigh.
$self_notify = false;
if ( 0 === strpos( $activity->type, 'bbp_' ) || 'new_groupblog_post' === $activity->type ) {
// Not in global cache? Query for post content.
if ( empty( $GLOBALS['bp']->ges_content ) ) {
$switched = false;
if ( 'new_groupblog_post' === $activity->type && function_exists( 'get_groupblog_blog_id' ) ) {
$switched = true;
switch_to_blog( get_groupblog_blog_id( $group_id ) );
}
$the_content = get_post_field( 'post_content', $activity->secondary_item_id, 'raw' );
if ( true === $switched ) {
restore_current_blog();
}
} else {
$the_content = $GLOBALS['bp']->ges_content;
unset( $GLOBALS['bp']->ges_content );
}
// Apply bbPress KSES filter if it exists (sanity check!)
$the_content = ( true === function_exists( 'bbp_filter_kses' ) ) ? wp_unslash( bbp_filter_kses( $the_content ) ) : $the_content;
$the_content = apply_filters( 'bp_ass_activity_notification_content', $the_content, $activity, $action, $group );
// Check for $self_notify status.
$self_notify = ass_self_post_notification( $user_id );
if ( ! empty( $self_notify ) && (int) $activity->user_id === (int) $user_id ) {
$group_status = 'self_notify';
}
}
if ( $self_notify ) {
// notification settings link
$settings_link = trailingslashit( bp_core_get_user_domain( $user_id ) . bp_get_settings_slug() ) . 'notifications/#groups-subscription-notification-settings';
// set notice
$notice = $email_setting_desc = __( 'You are currently receiving notifications for your own posts.', 'buddypress-group-email-subscription' );
$email_setting_links = sprintf( __( 'To disable these notifications please log in and go to: %s', 'buddypress-group-email-subscription' ), $settings_link );
$email_setting_links .= "\n\n" . __( 'Once you are logged in, uncheck "Receive notifications of your own posts?".', 'buddypress-group-email-subscription' );
$notice .= "\n\n" . $email_setting_links;
} else {
$settings_link = ass_get_login_redirect_url( trailingslashit( bp_get_group_permalink( $group ) . 'notifications' ), $group_status );
$email_setting_string = __( 'Your email setting for this group is: %s', 'buddypress-group-email-subscription' );
$group_status_string = ass_subscribe_translate( $group_status );
$notice = sprintf( $email_setting_string, $group_status_string );
$email_setting_desc = sprintf( $email_setting_string, '<strong> ' . $group_status_string . '</strong>' );
$email_setting_links = sprintf( __( 'To change your email setting for this group, please log in and go to: %s', 'buddypress-group-email-subscription' ), $settings_link );
$email_setting_links .= "\n\n" . ass_group_unsubscribe_links( $user_id );
$notice .= "\n" . $email_setting_links;
}
$email_type = 'bp-ges-single';
$group_name = bp_get_group_name( $group );
$group_link = bp_get_group_permalink( $group );
$subject = strip_tags( stripslashes( $activity->action ) );
// bpges_notice is a special activity type and gets some overrides.
if ( 'bpges_notice' === $activity->type ) {
$email_type = 'bp-ges-notice';
$subject = bp_activity_get_meta( $activity_id, 'bpges_notice_subject', true );
$message = sprintf( __(
'This is a notice from the group \'%s\':
"%s"
To view this group log in and follow the link below:
%s
---------------------
', 'buddypress-group-email-subscription' ), $group_name, $the_content, bp_get_group_permalink( $group ) );
$message .= __( 'Please note: admin notices are sent to everyone in the group and cannot be disabled.
If you feel this service is being misused please speak to the website administrator.', 'buddypress-group-email-subscription' );
}
$user_message_args = array(
'message' => $message,
'notice' => $notice,
'user_id' => $user_id,
'subscription_type' => $group_status,
'content' => $the_content,
'settings_link' => ! empty( $settings_link ) ? $settings_link : '',
);
// One last chance to filter the message content
$user_message = apply_filters( 'bp_ass_activity_notification_message', $message . $notice, $user_message_args );
// Get the details for the user
$user = bp_core_get_core_userdata( $user_id );
// Send the email
if ( $user->user_email ) {
// Custom GES email tokens.
$user_message_args['ges.action'] = stripslashes( $activity->action ); // Unfiltered.
$user_message_args['ges.subject'] = $subject; // Unfiltered.
$user_message_args['ges.email-setting-description'] = $email_setting_desc;
$user_message_args['ges.email-setting-links'] = $email_setting_links;
$user_message_args['ges.unsubscribe-global'] = ass_get_group_unsubscribe_link_for_user( $user->ID, $group_id, true );
$user_message_args['ges.unsubscribe'] = ass_get_group_unsubscribe_link_for_user( $user->ID, $group_id );
$user_message_args['ges.settings-link'] = $user_message_args['settings_link'];
$user_message_args['poster.url'] = bp_core_get_user_domain( $activity->user_id );
$user_message_args['recipient.id'] = $user->ID;
// BP-specific tokens.
$user_message_args['usermessage'] = $the_content;
$user_message_args['poster.name'] = bp_core_get_user_displayname( $activity->user_id );
$user_message_args['thread.url'] = $link;
$user_message_args['group.id'] = $group_id;
$user_message_args['group.link'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( $group_link ), $group_name );
$user_message_args['group.name'] = $group_name;
$user_message_args['group.url'] = esc_url( $group_link );
$user_message_args['group.admin'] = bp_core_get_user_displayname( $user_id );
// Remove tokens that we're not using.
unset( $user_message_args['content'], $user_message_args['notice'], $user_message_args['message'], $user_message_args['settings_link'] );
// Add activity KSES filter if not bbPress or groupblog item.
if ( false === strpos( $activity->type, 'bbp_' ) && 'new_groupblog_post' !== $activity->type ) {
add_filter( 'bp_email_set_content_html', 'bp_activity_filter_kses', 6 );
}
// Sending time!
ass_send_email( $email_type, $user->user_email, array(
'tokens' => $user_message_args,
'subject' => $subject,
'content' => $user_message,
'activity' => $activity,
) );
// Revert!
if ( false === strpos( $activity->type, 'bbp_' ) && 'new_groupblog_post' !== $activity->type ) {
remove_filter( 'bp_email_set_content_html', 'bp_activity_filter_kses', 6 );
}
}
}
/**
* Email wrapper function for BP-GES.
*
* Created to be backward compatible when used on < BP 2.5.
*
* @since 3.7.0
*
* @param string $email_type Type of email being sent.
* @param string|array|int|WP_User $to Either a email address, user ID, WP_User object,
* or an array containg the address and name.
* @param array $args {
* Optional. Array of extra parameters.
*
* @type array $tokens Optional. Assocative arrays of string replacements for the email.
* }
* @return bool|WP_Error True if the email was sent successfully. Otherwise, a WP_Error object
* describing why the email failed to send. The contents will vary based
* on the email delivery class you are using.
*/
function ass_send_email( $email_type, $to, $args ) {
// <<<<<<< HEAD
/**
* Filters the 'from' name on outgoing emails.
*
* @since 4.0.2
*
* @param string $from_name Default is null, which will use WP defaults.
* @param string $email_type
* @param string $to
* @param array $args
*/
$from_name = apply_filters( 'bpges_email_from_name', null, $email_type, $to, $args );
/**
* Filters the 'from' email address on outgoing emails.
*
* @since 4.0.2
*
* @param string $from_email Default is null, which will use WP defaults.
* @param string $email_type
* @param string $to
* @param array $args
*/
$from_email = apply_filters( 'bpges_email_from_email', null, $email_type, $to, $args );
if ( ! empty( $from_name ) || ! empty( $from_email ) ) {
if ( ! isset( $args[ 'from' ] ) ) {
$args['from'] = [];
}
if ( ! empty( $from_name ) ) {
$args['from' ]['name'] = $from_name;
}
if ( ! empty( $from_email ) ) {
$args['from']['email'] = $from_email;
}
}
// BP 2.5+
if ( true === function_exists( 'bp_send_email' ) && true === ! apply_filters( 'bp_email_use_wp_mail', false ) ) {
// Unset array keys used for older BP installs.
unset( $args['subject'], $args['content'] );
// Temporary save tokens.
buddypress()->ges_tokens = $args['tokens'];
// Remove BP's restrictive HTML filtering.
remove_filter( 'bp_email_set_content_html', 'wp_filter_post_kses', 6 );
// Remove BP's plain-text filter and convert the HTML email content to Markdown.
remove_filter( 'bp_email_set_content_plaintext', 'wp_strip_all_tags', 6 );
add_filter( 'bp_email_get_property', 'ass_email_strip_trailing_breaklines', 1, 3 );
add_filter( 'bp_email_get_property', 'ass_email_convert_html_to_plaintext', 20, 3 );
// Remove default BP email footer.
add_action( 'bp_before_email_footer', 'ob_start', 999, 0 );
add_action( 'bp_after_email_footer', 'ob_get_clean', -999, 0 );
// Add our custom BP email footer.
add_action( 'bp_after_email_footer', 'ass_bp_email_footer_text' );
add_action( 'bp_after_email_footer', 'ass_bp_email_footer_html_unsubscribe_links' );
if ( isset( $args['from'] ) ) {
buddypress()->ges_from = $args['from'];
add_action( 'bp_email_set_tokens', 'ass_email_set_from_during_token_addition', 10, 3 );
}
/**
* Hook to do something before GES sends a BP email.
*
* @since 3.7.0
*
* @param string $email_type The GES email type.
*/
do_action( 'bp_ges_before_bp_send_email', $email_type );
/**
* Filters the email type that GES uses to send a BuddyPress email.
*
* @since 4.0.0
*
* @param string $email_type Current BP email post type.
* @param array $args See bp_send_email()'s third argument for full documentation.
* @param string $to Email recipient.
*/
$email_type = apply_filters( 'ass_send_email_email_type', $email_type, $args, $to );
/**
* Filter the arguments before GES sends a BuddyPress email.
*
* @since 3.7.0
*
* @param array $args See bp_send_email()'s third argument for full documentation.
* @param string $email_type Current BP email post type.
*/
$args = apply_filters( 'ass_send_email_args', $args, $email_type );
// Email time!
$send = bp_send_email( $email_type, (int) $args['tokens']['recipient.id'], $args );
// Clean up after ourselves!
add_filter( 'bp_email_set_content_html', 'wp_filter_post_kses', 6 );
add_filter( 'bp_email_set_content_plaintext', 'wp_strip_all_tags', 6 );
remove_filter( 'bp_email_get_property', 'ass_email_strip_trailing_breaklines', 1, 3 );
remove_filter( 'bp_email_get_property', 'ass_email_convert_html_to_plaintext', 20, 3 );
/**
* Hook to do something after GES sends a BP email.
*
* @since 3.7.0
*
* @param string $email_type The GES email type.
*/
do_action( 'bp_ges_after_bp_send_email', $email_type );
return $send;
// Older BP versions use wp_mail().
} else {
$headers = array();
if ( isset( $args['from'] ) ) {
$headers[] = "From: \"{$args['from']['name']}\" <{$args['from']['email']}>";
}
return wp_mail( $to, $args['subject'], $args['content'], $headers );
}
}
/**
* Sets the email situation type for use in GES.
*
* Only applicable for BuddyPress 2.5+.
*
* @since 3.7.0
*
* @param string $email_type The email type to fetch.
* @param bool $term_check Check if our GES email term exists before creating our specific email
* situation. Default: true.
*/
function ass_set_email_type( $email_type, $term_check = true ) {
$switched = false;
if ( false === bp_is_root_blog() ) {
$switched = true;
switch_to_blog( bp_get_root_blog_id() );
}
if ( true === $term_check ) {
$term = term_exists( $email_type, bp_get_email_tax_type() );
} else {
$term = 0;
}
// Term already exists so don't do anything.
if ( true === $term_check && $term !== 0 && $term !== null ) {
if ( true === $switched ) {
restore_current_blog();
}
return;
// Create our email situation.
} else {
// Set up default email content depending on the email type.
switch ( $email_type ) {
// Group activity single items.
case 'bp-ges-single' :
/* translators: do not remove {} brackets or translate its contents. */
$post_title = __( '[{{{site.name}}}] {{{ges.subject}}}', 'buddypress-group-email-subscription' );
/* translators: do not remove {} brackets or translate its contents. */
$html_content = __( "{{{ges.action}}}:\n\n<blockquote>{{{usermessage}}}</blockquote>\n–\n<a href=\"{{{thread.url}}}\">Go to the discussion</a> to reply or catch up on the conversation.\n{{{ges.email-setting-description}}}", 'buddypress-group-email-subscription' );
/* translators: do not remove {} brackets or translate its contents. */