-
Notifications
You must be signed in to change notification settings - Fork 33
/
bp-activity-subscription-digest.php
1043 lines (853 loc) · 36.7 KB
/
bp-activity-subscription-digest.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
// for testing only
//if you need to add this at the TOP of your wp-config.php file. (Here are the timezones http://us3.php.net/manual/en/timezones.php)
//date_default_timezone_set('Asia/Tokyo');
//date_default_timezone_set('America/New_York');
/* This function was used for debugging the digest scheduling features */
function ass_digest_schedule_print() {
print "<br />";
print "<br />";
$crons = _get_cron_array();
echo "<div style='background: #fff;'>";
$sched = wp_next_scheduled( 'ass_digest_event' );
echo "Scheduled: " . date( 'h:i', $sched );
$until = ( (int)$sched - time() ) / ( 60 * 60 );
echo " Until: " . $until . " hours";
echo "</div>";
}
//add_action( 'wp_head', 'ass_digest_schedule_print' );
/* Digest-specific functions */
/**
* Trigger the batched sending of digests.
*
* @since 3.9.0
*
* @param string $type Digest type. 'sum' or 'dig'.
*/
function bpges_trigger_digest( $type ) {
$timestamp = date( 'Y-m-d H:i:s' );
bpges_send_queue()->data( array(
'type' => $type,
'timestamp' => $timestamp,
) )->dispatch();
}
/**
* Process and generate a digest for a user/type.
*
* This function queries for the necessary queued items, and then passes them to
* bpges_generate_digest() for the actual generation of the digest email. We break
* the logic out in this way primarily for automated testing.
*
* @since 3.9.0
*
* @param int $user_id ID of the user.
* @param string $type Digest type. 'sum' or 'dig'.
* @param string $timestamp Timestamp for the current digest run. Used to determine the queued
* items that should be included in the digest.
* @param bool $is_preview Whether this is a preview. When preview, email content will be
* echoed and not sent or deleted from the queue.
*/
function bpges_process_digest_for_user( $user_id, $type, $timestamp, $is_preview = false ) {
$query = new BPGES_Queued_Item_Query( array(
'user_id' => $user_id,
'type' => $type,
'before' => $timestamp,
) );
$items = $query->get_results();
/**
* Filters the items to be included in a digest for a user.
*
* @since 3.9.5
*
* @param array $items Items returned by BPGES_Queued_Item_Query::get_results().
* @param int $user_id ID of the user.
* @param string $type Digest type. 'sum' or 'dig'.
* @param string $timestamp Timestamp for the current digest run.
* @param bool $is_preview Whether this is a preview.
*/
$items = apply_filters( 'bpges_user_digest_items', $items, $user_id, $type, $timestamp, $is_preview );
// Sort by group.
$sorted_by_group = array();
foreach ( $items as $item ) {
if ( ! isset( $sorted_by_group[ $item->group_id ] ) ) {
$sorted_by_group[ $item->group_id ] = array();
}
$sorted_by_group[ $item->group_id ][] = $item->activity_id;
}
// Ensure numerical sort.
foreach ( $sorted_by_group as $group_id => &$group_activity_ids ) {
sort( $group_activity_ids );
}
$sent_activity_ids = bpges_generate_digest( $user_id, $type, $sorted_by_group, $is_preview );
if ( ! $is_preview ) {
// Collate queued-item IDs for bulk deletion.
$to_delete_ids = array();
foreach ( $items as $item ) {
$group_id = $item->group_id;
$activity_id = $item->activity_id;
if ( ! isset( $sent_activity_ids[ $group_id ] ) ) {
continue;
}
if ( ! in_array( $activity_id, $sent_activity_ids[ $group_id ], true ) ) {
continue;
}
$to_delete_ids[] = $item->id;
}
if ( $to_delete_ids ) {
BPGES_Queued_Item::bulk_delete( $to_delete_ids );
}
}
}
/**
* Generate and send a digest email.
*
* @param int $user_id ID of the user.
* @param string $type Digest type. 'sum' or 'dig'.
* @param array $group_activity_ids Associative array, where keys are group IDs and subarrays
* are arrays of activity IDs to be included in that group's
* section of the digest.
* @param bool $is_preview Whether this is a preview.
* @return array $sent_activity_ids Associative array of activity items that were included in
* the digest, formatted as `$group_activity_ids` above.
*/
function bpges_generate_digest( $user_id, $type, $group_activity_ids, $is_preview = false ) {
$ass_email_css = bpges_digest_css();
$title = ass_digest_get_title( $type );
$blogname = get_blog_option( BP_ROOT_BLOG, 'blogname' );
$subject = apply_filters( 'ass_digest_subject', "$title [$blogname]", $blogname, $title, $type );
$footer = "\n\n<div class=\"digest-footer\" {$ass_email_css['footer']}>";
$footer .= sprintf( __( "You have received this message because you are subscribed to receive a digest of activity in some of your groups on %s.", 'buddypress-group-email-subscription' ), $blogname );
$footer = apply_filters( 'ass_digest_footer', $footer, $type );
// initialize some strings
$bp = buddypress();
$summary = $activity_message = $body = '';
$userdata = new WP_User( $user_id );
if ( 0 === $userdata->ID ) {
return $group_activity_ids;
}
$to = $userdata->user_email;
$user_url = bp_members_get_user_url( $user_id );
$user_groups_url = bp_members_get_user_url(
$user_id,
bp_members_get_path_chunks( array( bp_get_groups_slug() ) )
);
// Keep an unfiltered copy of the activity IDs to be compared with sent items.
$group_activity_ids_unfiltered = $group_activity_ids;
// filter the list - can be used to sort the groups
$group_activity_ids = apply_filters( 'ass_digest_group_activity_ids', @$group_activity_ids );
// Keep an unmodified copy of the activity IDs to be passed to filters.
$group_activity_ids_pristine = $group_activity_ids;
$header = "<div class=\"digest-header\" {$ass_email_css['title']}>$title " . __('at', 'buddypress-group-email-subscription')." <a href='" . $bp->root_domain . "'>$blogname</a></div>\n\n";
$message = apply_filters( 'ass_digest_header', $header, $title, $ass_email_css['title'] );
$sent_activity_ids = array();
// loop through each group for this user
$has_group_activity = false;
foreach ( $group_activity_ids as $group_id => $activity_ids ) {
// Prime cache and filter out invalid items.
$activity_get = bp_activity_get( array(
'in' => $activity_ids,
'show_hidden' => true,
) );
$activity_ids = wp_list_pluck( $activity_get['activities'], 'id' );
// Discard activity items that are invalid.
$activity_ids_raw = $activity_ids;
$activity_ids = array();
foreach ( $activity_ids_raw as $activity_id_raw ) {
if ( bp_ges_activity_is_valid_for_digest( $activity_id_raw, $type, $userdata->user_id ) ) {
$activity_ids[] = $activity_id_raw;
}
}
// Activities could have been deleted since being recorded for digest emails.
if ( empty( $activity_ids ) ) {
continue;
}
$has_group_activity = true;
$group = groups_get_group( array(
'group_id' => $group_id,
) );
$group_name = $group->name;
$group_slug = $group->slug;
$group_permalink = ass_get_login_redirect_url( bp_get_group_url( $group ) );
// Might be nice here to link to anchor tags in the message.
if ( 'dig' == $type ) {
$summary .= apply_filters( 'ass_digest_summary', "<li class=\"digest-group-summary\" {$ass_email_css['summary']}><a href='{$group_permalink}'>$group_name</a> " . sprintf( __( '(%s items)', 'buddypress-group-email-subscription' ), count( $activity_ids ) ) ."</li>\n", $ass_email_css['summary'], $group_slug, $group_name, $activity_ids );
}
$activity_message .= ass_digest_format_item_group( $group_id, $activity_ids, $type, $group_name, $group_slug, $user_id );
$sent_activity_ids[ $group_id ] = $activity_ids;
}
// If there's nothing to send, skip this use.
if ( ! $has_group_activity ) {
return $group_activity_ids;
}
// show group summary for digest, and follow help text for weekly summary
if ( 'dig' == $type ) {
$message .= apply_filters( 'ass_digest_summary_full', __( 'Group Summary', 'buddypress-group-email-subscription') . ":\n<ul class=\"digest-group-summaries\" {$ass_email_css['summary_ul']}>" . $summary . "</ul>", $ass_email_css['summary_ul'], $summary );
}
// the meat of the message which we generated above goes here
$message .= $activity_message;
$body .= $activity_message;
// user is subscribed to "New Topics"
// add follow help text only if bundled forums are enabled
if ( 'sum' == $type && class_exists( 'BP_Forums_Component' ) ) {
$message .= apply_filters( 'ass_summary_follow_topic', "<div {$ass_email_css['follow_topic']}>" . __( "How to follow a topic: to get email updates for a specific topic, click the topic title - then on the webpage click the <i>Follow this topic</i> button. (If you don't see the button you need to login first.)", 'buddypress-group-email-subscription' ) . "</div>\n", $ass_email_css['follow_topic'] );
}
$message .= $footer;
$unsubscribe_message = "\n\n" . sprintf( __( "To disable these notifications per group please login and go to: %s where you can change your email settings for each group.", 'buddypress-group-email-subscription' ), '<a href="' . esc_url( $user_groups_url ) . '">' . __( 'My Groups', 'buddypress-group-email-subscription' ) . "</a>" );
if ( bp_get_option( 'ass-global-unsubscribe-link' ) == 'yes' ) {
$unsubscribe_link = "$user_url?bpass-action=unsubscribe&access_key=" . md5( $user_id . 'unsubscribe' . wp_salt() );
$unsubscribe_message .= "\n\n<br><br><a class=\"digest-unsubscribe-link\" href=\"$unsubscribe_link\">" . __( 'Disable these notifications for all my groups at once.', 'buddypress-group-email-subscription' ) . '</a>';
}
$message .= apply_filters( 'ass_digest_disable_notifications', $unsubscribe_message, $user_groups_url );
$message .= "</div>";
if ( $is_preview ) {
echo $message;
return;
}
/**
* Filter to allow plugins to stop the email from being sent.
*
* @since 3.8.0
*
* @param bool true Whether or not to send the email.
* @param int $user_id ID of the user whose digest is currently being processed.
* @param array $group_activity_ids Array of activity items in the digest.
* @param string $message Message body.
*/
$send = apply_filters( 'bp_ges_send_digest_to_user', true, $user_id, $group_activity_ids, $message );
if ( ! $send ) {
return $group_activity_ids;
}
// Sending time!
if ( true === function_exists( 'bp_send_email' ) && true === ! apply_filters( 'bp_email_use_wp_mail', false ) ) {
// Custom GES email tokens.
$user_message_args = array();
// Digest summary only available for daily digests.
$user_message_args['ges.digest-summary'] = '';
if ( 'dig' == $type ) {
$user_message_args['ges.digest-summary'] = apply_filters( 'ass_digest_summary_full', __( 'Group Summary', 'buddypress-group-email-subscription' ) . ":\n<ul {$ass_email_css['summary_ul']}>" . $summary . "</ul>", $ass_email_css['summary_ul'], $summary );
}
$user_message_args['ges.subject'] = $title;
$user_message_args['ges.settings-link'] = ass_get_login_redirect_url( $user_groups_url );
$user_message_args['subscription_type'] = $type;
$user_message_args['recipient.id'] = $user_id;
// Unused.
$user_message_args['poster.url'] = $user_url;
// BP-specific tokens.
$user_message_args['usermessage'] = $body;
$user_message_args['poster.name'] = $userdata->user_login; // Unused
/**
* Filters the arguments passed to `ass_send_email()` when a digest is sent.
*
* @since 3.7.3
*
* @param array $user_message_args Arguments passed to ass_send_email 'tokens' param.
* @param int $user_id ID of the user whose digest is currently being processed.
* @param array $group_activity_ids_pristine Array of activity items in the digest.
*/
$user_message_args = apply_filters( 'bp_ges_user_digest_message_args', $user_message_args, $user_id, $group_activity_ids_pristine );
// Filters.
add_filter( 'bp_email_get_salutation', 'ass_digest_filter_salutation' );
add_filter( 'bp_email_get_property', 'ass_digest_strip_plaintext_separators', 1, 3 );
// Send the email.
ass_send_email( 'bp-ges-digest', $to, array(
'tokens' => $user_message_args
) );
// Remove filter.
remove_filter( 'bp_email_get_salutation', 'ass_digest_filter_salutation' );
remove_filter( 'bp_email_get_property', 'ass_digest_strip_plaintext_separators', 1, 3 );
// Old version.
} else {
$message_plaintext = ass_convert_html_to_plaintext( $message );
// Send out the email.
ass_send_multipart_email( $to, $subject, $message_plaintext, str_replace( '---', '', $message ) );
}
return $sent_activity_ids;
}
/**
* Deprecated function for firing a digest run.
*
* @deprecated 3.9.0 Use bpges_trigger_digest() to trigger digest runs.
*/
function ass_digest_fire( $type ) {}
/**
* Get the title for the digest email.
*
* @since 3.7.0
*
* @param string $type Type of digest. Either 'dig' for daily digest or 'sum' for weekly summary.
* @return string
*/
function ass_digest_get_title( $type = '' ) {
if ( $type == 'dig' ) {
$title = sprintf( __( 'Your daily digest of group activity', 'buddypress-group-email-subscription' ) );
} else {
$title = sprintf( __( 'Your weekly summary of group topics', 'buddypress-group-email-subscription' ) );
}
return apply_filters( 'ass_digest_title', $title, $type );
}
/**
* Filter the "Hi, X" salutation in BP 2.5 emails to use the digest title.
*
* @since 3.7.0
*
* @param string $retval Current salutation.
* @return string
*/
function ass_digest_filter_salutation( $retval = '' ) {
$tokens = buddypress()->ges_tokens;
if ( empty( $tokens ) ) {
return $retval;
}
return ass_digest_get_title( buddypress()->ges_tokens['subscription_type'] );
}
/**
* Strip plain-text only content from BP 2.5 digest HTML emails.
*
* @since 3.7.0
*
* @see BP_Email::get() and the nl2br() call.
*
* @param string $content Content to check.
* @param string $prop Property to check.
* @param string $transform Transform type to check.
* @return string
*/
function ass_digest_strip_plaintext_separators( $content = '', $prop = '', $transform = '' ) {
if ( $transform !== 'add-content' ) {
return $content;
}
$find = array(
"---",
"<br />",
"\n–\n",
);
$replace = array(
'',
'',
'<br />–<br />',
);
return str_replace( $find, $replace, $content );
}
// these functions are hooked in via the cron
function ass_daily_digest_fire() {
bpges_trigger_digest( 'dig' );
}
add_action( 'ass_digest_event', 'ass_daily_digest_fire' );
function ass_weekly_digest_fire() {
bpges_trigger_digest( 'sum' );
}
add_action( 'ass_digest_event_weekly', 'ass_weekly_digest_fire' );
// for testing the digest firing in real-time, add /?sum=1 to the url
function ass_digest_fire_test() {
if ( isset( $_GET['sum'] ) && is_super_admin() ){
echo '<h2>' . __( 'DAILY DIGEST:','buddypress-group-email-subscription' ) . '</h2>';
bpges_generate_digest_preview_for_type( 'dig' );
echo '<h2 style="margin-top:150px">' . __( 'WEEKLY DIGEST:','buddypress-group-email-subscription' ) . '</h2>';
bpges_generate_digest_preview_for_type( 'sum' );
die();
}
}
add_action( 'bp_actions', 'ass_digest_fire_test' );
/**
* Generate preview for digest type.
*
* @since 3.9.0
*
* @param string $type Digest type. 'sum' or 'dig'.
*/
function bpges_generate_digest_preview_for_type( $type ) {
$timestamp = date( 'Y-m-d H:i:s' );
if ( isset( $_GET['user_ids'] ) ) {
$user_ids = wp_parse_id_list( wp_unslash( $_GET['user_ids'] ) );
} else {
$count = isset( $_GET['user_count'] ) ? intval( wp_unslash( $_GET['user_count'] ) ) : 25;
$user_ids = BPGES_Queued_Item_Query::get_users_with_pending_digest( $type, $count, $timestamp );
}
foreach ( $user_ids as $user_id ) {
$user = new WP_User( $user_id );
echo '<div style="background-color:white; width:75%;padding:20px 10px;">';
echo '<p>=================== to: <b>' . esc_html( $user->user_email ) . '</b> ===================</p>';
bpges_process_digest_for_user( $user_id, $type, $timestamp, true );
//echo '<br>PLAIN TEXT PART:<br><pre>'; echo $message_plaintext ; echo '</pre>';
echo '</div>';
}
}
/**
* Displays the introduction for the group and loops through each item
*
* I've chosen to cache on an individual-activity basis, instead of a group-by-group basis. This
* requires just a touch more overhead (in terms of looping through individual activity_ids), and
* doesn't really have any added effect at the moment (since an activity item can only be associated
* with a single group). But it provides the greatest amount of flexibility going forward, both in
* terms of the possibility that activity items could be associated with more than one group, and
* the possibility that users within a single group would want more highly-filtered digests.
*/
function ass_digest_format_item_group( $group_id, $activity_ids, $type, $group_name, $group_slug, $user_id ) {
global $bp;
$ass_email_css = bpges_digest_css();
$group_permalink = bp_get_group_url( $group_id );
$group_name_link = '<a class="item-group-group-link" href="'.$group_permalink.'" name="'.$group_slug.'">'.$group_name.'</a>';
$userdomain = ass_digest_get_user_domain( $user_id );
$unsubscribe_link = "$userdomain?bpass-action=unsubscribe&group=$group_id&access_key=" . md5( "{$group_id}{$user_id}unsubscribe" . wp_salt() );
$gnotifications_link = ass_get_login_redirect_url( $group_permalink . 'notifications/' );
// add the group title bar
if ( $type == 'dig' ) {
$group_message = "\n---\n\n<div class=\"item-group-title\" {$ass_email_css['group_title']}>". sprintf( __( 'Group: %s', 'buddypress-group-email-subscription' ), $group_name_link ) . "</div>\n\n";
} elseif ( $type == 'sum' ) {
$group_message = "\n---\n\n<div class=\"item-group-title\" {$ass_email_css['group_title']}>". sprintf( __( 'Group: %s weekly summary', 'buddypress-group-email-subscription' ), $group_name_link ) . "</div>\n";
}
// add change email settings link
$group_message .= "\n<div class=\"item-group-settings-link\" {$ass_email_css['change_email']}>";
$group_message .= __('To disable these notifications for this group click ', 'buddypress-group-email-subscription'). " <a href=\"$unsubscribe_link\">" . __( 'unsubscribe', 'buddypress-group-email-subscription' ) . '</a> - ';
$group_message .= __('change ', 'buddypress-group-email-subscription') . '<a href="' . $gnotifications_link . '">' . __( 'email options', 'buddypress-group-email-subscription' ) . '</a>';
$group_message .= "</div>\n\n";
$group_message = apply_filters( 'ass_digest_group_message_title', $group_message, $group_id, $type );
// Finally, add the markup to the digest
foreach ( $activity_ids as $activity_id ) {
$activity_item = new BP_Activity_Activity( $activity_id );
if ( ! empty( $activity_item ) ) {
$group_message .= ass_digest_format_item( $activity_item, $type );
}
//$group_message .= '<pre>'. $item->id .'</pre>';
}
/**
* Filters the markup for a group's digest section.
*
* @since 3.8.0 Introduced $activity_ids and $user_id parameters.
*
* @param string $group_message Markup.
* @param int $group_id ID of the group.
* @param string $type Digest type. 'dig' or 'sum'.
* @param array $activity_ids Array of IDs included in this group's digest.
* @param int $user_id ID of the user receiving the digest.
*/
return apply_filters( 'ass_digest_format_item_group', $group_message, $group_id, $type, $activity_ids, $user_id );
}
// displays each item in a group
function ass_digest_format_item( $item, $type ) {
$ass_email_css = bpges_digest_css();
$replies = '';
//load from the cache if it exists
if ( $item_cached = wp_cache_get( 'digest_item_' . $type . '_' . $item->id, 'ass' ) ) {
//$item_cached .= "GENERATED FROM CACHE";
return $item_cached;
}
/* Action text - This technique will not translate well */
// bbPress 2 support
if ( strpos( $item->type, 'bbp_' ) !== false ) {
$action_split = explode( ' in the forum', ass_clean_subject_html( $item->action ) );
// regular group activity items
} else {
$action_split = explode( ' in the group', ass_clean_subject_html( $item->action ) );
}
if ( isset( $action_split[1] ) )
$action = $action_split[0];
else
$action = $item->action;
$action = ass_digest_filter( $action );
$action = str_replace( ' started the forum topic', ' started', $action ); // won't translate but it's not essential
$action = str_replace( ' posted on the forum topic', ' posted on', $action );
$action = str_replace( ' started the discussion topic', ' started', $action );
$action = str_replace( ' posted on the discussion topic', ' posted on', $action );
/* Activity timestamp */
$timestamp = strtotime( $item->date_recorded );
$time_posted = get_date_from_gmt( $item->date_recorded, get_option( 'time_format' ) );
$date_posted = get_date_from_gmt( $item->date_recorded, get_option( 'date_format' ) );
//$item_message = strip_tags( $action ) . ": \n";
$item_message = "<div class=\"digest-item\" {$ass_email_css['item_div']}>";
$item_message .= "<span class=\"digest-item-action\" {$ass_email_css['item_action']}>" . $action . ": ";
$item_message .= "<span class=\"digest-item-timestamp\" {$ass_email_css['item_date']}>" . sprintf( __('at %s, %s', 'buddypress-group-email-subscription'), $time_posted, $date_posted ) ."</span>";
$item_message .= "</span>\n";
// activity content
if ( ! empty( $item->content ) )
$item_message .= "<br><span class=\"digest-item-content\" {$ass_email_css['item_content']}>" . apply_filters( 'ass_digest_content', $item->content, $item, $type ) . "</span>";
// view link
if ( $item->type == 'activity_update' || $item->type == 'activity_comment' ) {
$view_link = bp_activity_get_permalink( $item->id, $item );
} else {
$view_link = $item->primary_link;
}
$item_message .= ' - <a ' . $ass_email_css['view_link'] . ' class="digest-item-view-link" href="' . ass_get_login_redirect_url( $view_link ) .'">' . __( 'View', 'buddypress-group-email-subscription' ) . '</a>';
$item_message .= "</div>\n\n";
$item_message = apply_filters( 'ass_digest_format_item', $item_message, $item, $action, $timestamp, $type, $replies );
$item_message = ass_digest_filter( $item_message );
// save the cache
if ( $item->id )
wp_cache_set( 'digest_item_' . $type . '_' . $item->id, $item_message, 'ass' );
return $item_message;
}
// standard wp filters to clean up things that might mess up email display - (maybe not necessary?)
function ass_digest_filter( $item ) {
$item = wptexturize( $item );
$item = convert_chars( $item );
$item = stripslashes( $item );
return $item;
}
// Run activity content in digests through wpautop for proper handling of line breaks.
add_filter( 'ass_digest_content', 'wpautop' );
// convert the email to plain text, and fancy it up a bit. these conversion only work in English, but it's ok.
function ass_convert_html_to_plaintext( $message ) {
// convert view links to http:// links
$message = preg_replace( "/<a href=\"(.[^\"]*)\">View<\/a>/i", "\\1", $message );
// convert group div to two lines encasing the group name
$message = preg_replace( "/<div.*>Group: <a href=\"(.[^\"]*)\">(.*)<\/a>.*<\/div>/i", "------\n\\2 - \\1\n------", $message );
// convert footer line to two dashes
$message = preg_replace( "/\n<div class=\"ass-footer\"/i", "--\n<div", $message );
// convert My Groups links to http:// links
$message = preg_replace( "/<a href=\"(.[^\"]*)\">My Groups<\/a>/i", "\\1", $message );
$message = preg_replace( "/<a href=\"(.[^\"]*)\">(.*)<\/a>/i", "\\2 (\\1)", $message );
$message = strip_tags( stripslashes( $message ) );
// remove uneccesary lines
$message = str_replace( "change email options for this group\n\n", '', $message );
$message = html_entity_decode( $message , ENT_QUOTES, 'UTF-8' );
return $message;
}
/**
* Properly encode HTML characters in old digest items.
*
* This is a regex callback function used in ass_send_multipart_email().
*
* @since 3.7.1
*
* @param array $matches Regex matches.
* @return string
*/
function ass_old_digest_item_html_entities( $matches ) {
$ass_email_css = bpges_digest_css();
return '<span ' . $ass_email_css['item_content'] . '>' . htmlentities( strip_tags( $matches[1] ), ENT_COMPAT, 'utf-8' ) . '</span>';
}
/**
* Formats and sends a MIME multipart email with both HTML and plaintext
*
* We have to use some fancy filters from the wp_mail function to configure the $phpmailer object
* properly
*/
function ass_send_multipart_email( $to, $subject, $message_plaintext, $message ) {
$ass_email_css = bpges_digest_css();
// setup HTML body. plugins that wrap emails with HTML templates can filter this
$message = apply_filters( 'ass_digest_message_html', "<html><body>{$message}</body></html>", $message );
// get admin email
$admin_email = get_site_option( 'admin_email' );
// if no admin email, use a dummy 'from' email address
if ( $admin_email == '' ) {
$admin_email = 'support@' . $_SERVER['SERVER_NAME'];
}
// get from name
$from_name = get_site_option( 'site_name' );
// if no site name, use WordPress as dummy 'from' name
if ( empty( $from_name ) ) {
$from_name = 'WordPress';
}
// set up anonymous functions to be used to override some WP mail stuff
//
// due to a bug with wp_mail(), we should reset some $phpmailer properties
// (see http://core.trac.wordpress.org/ticket/18493#comment:13).
//
// we're doing this during the 'wp_mail_from' filter because this runs before
// 'phpmailer_init'
$admin_email = addslashes( $admin_email );
$admin_email_filter = function( $admin_email ) {
global $phpmailer;
if ( $phpmailer && is_object( $phpmailer ) ) {
$phpmailer->Body = "";
$phpmailer->AltBody = "";
}
return $admin_email;
};
$from_name = addslashes( $from_name );
$from_name_filter = function( $from_name ) {
return $from_name;
};
// set the WP email overrides
add_filter( 'wp_mail_from', $admin_email_filter );
add_filter( 'wp_mail_from_name', $from_name_filter );
// setup plain-text body
$message_plaintext = addslashes( $message_plaintext );
add_action( 'phpmailer_init', function( $phpmailer ) use ( $message_plaintext ) {
if ( $phpmailer && is_object( $phpmailer ) ) {
$phpmailer->AltBody = "'" . $message_plaintext . "'";
}
} );
// set content type as HTML
$headers = array( 'Content-type: text/html' );
/*
* Eek. Stupid HTML encoding.
*
* Wish we could do this higher up the chain...
*/
$message = preg_replace_callback( '/<span ' . $ass_email_css['item_content'] . '>(.+?)<\/span>/', 'ass_old_digest_item_html_entities', $message );
// send the email!
$result = wp_mail( $to, $subject, $message, $headers );
// remove our custom hooks
remove_filter( 'wp_mail_from', $admin_email_filter );
remove_filter( 'wp_mail_from_name', $from_name_filter );
// clean up after ourselves
// reset $phpmailer->AltBody after we set it in the 'phpmailer_init' hook
// this is so subsequent calls to wp_mail() by other plugins will be clean
global $phpmailer;
if ( $phpmailer && is_object( $phpmailer ) ) {
$phpmailer->AltBody = "";
}
return $result;
}
function ass_digest_record_activity( $activity_id, $user_id, $group_id, $type = 'dig' ) {
global $bp;
if ( !$activity_id || !$user_id || !$group_id )
return;
/**
* Prevent the addition of specific activity item IDs for specific users.
*
* @since 3.6.1
*
* @param bool $proceed Whether to continue adding this activity item to a user's digest.
* @param int $activity_id ID of activity item to be added.
* @param int $user_id ID of user whose digest record is being modified.
* @param int $group_id ID of the group where the action took place.
* @param string $type Type of digest.
*/
if ( false === apply_filters( 'ass_digest_record_activity_allow', true, $activity_id, $user_id, $group_id, $type ) ) {
return;
}
ass_queue_activity_item( $activity_id, $user_id, $group_id, $type );
}
/**
* Get digest queue for a given user.
*
* @param int $user_id ID of the user.
* @param string $type Digest type. 'dig' or 'sum'.
* @return array
*/
function bpges_get_digest_queue_for_user( $user_id, $type ) {
if ( ! is_numeric( $user_id ) ) {
return array();
}
$user_id = (int) $user_id;
$query = new BPGES_Queued_Item_Query( array(
'user_id' => $user_id,
'type' => $type,
) );
$queue = array();
foreach ( $query->get_results() as $item ) {
$queue[ $item->group_id ][] = $item->activity_id;
}
return $queue;
}
function ass_cron_add_weekly( $schedules ) {
if ( !isset( $schedules[ 'weekly' ] ) ) {
$schedules['weekly'] = array( 'interval' => 604800, 'display' => __( 'Once Weekly', 'buddypress-group-email-subscription' ) );
}
return $schedules;
}
add_filter( 'cron_schedules', 'ass_cron_add_weekly' );
function ass_set_daily_digest_time( $hours, $minutes ) {
$the_time = date( 'Y-m-d' ) . ' ' . $hours . ':' . $minutes;
$the_timestamp = strtotime( $the_time );
/* If the time has already passed today, the next run will be tomorrow */
$the_timestamp = ( $the_timestamp > time() ) ? $the_timestamp : (int)$the_timestamp + 86400;
/* Clear the old recurring event and set up a new one */
wp_clear_scheduled_hook( 'ass_digest_event' );
/*
* Not using bp_get_root_blog_id() since it might not be available during
* activation time.
*/
if ( defined( 'BP_ROOT_BLOG' ) ) {
/** This filter is documented in /wp-content/plugins/buddypress/bp-core/bp-core-functions.php */
$blog_id = (int) apply_filters( 'bp_get_root_blog_id', constant( 'BP_ROOT_BLOG' ) );
} else {
$blog_id = 1;
}
// Custom BP root blog, so set up cron on BP sub-site.
$switched = false;
if ( 1 !== $blog_id ) {
switch_to_blog( $blog_id );
wp_clear_scheduled_hook( 'ass_digest_event' );
$switched = true;
}
wp_schedule_event( $the_timestamp, 'daily', 'ass_digest_event' );
update_option( 'ass_digest_time', array( 'hours' => $hours, 'minutes' => $minutes ) );
// Restore current blog.
if ( $switched ) {
restore_current_blog();
}
}
// Takes the numeral equivalent of a $day: 0 for Sunday, 1 for Monday, etc
function ass_set_weekly_digest_time( $day ) {
if ( !$next_weekly = wp_next_scheduled( 'ass_digest_event' ) )
$next_weekly = time() + 60;
while ( date( 'w', $next_weekly ) != $day ) {
$next_weekly += 86400;
}
/* Clear the old recurring event and set up a new one */
wp_clear_scheduled_hook( 'ass_digest_event_weekly' );
/*
* Not using bp_get_root_blog_id() since it might not be available during
* activation time.
*/
if ( defined( 'BP_ROOT_BLOG' ) ) {
/** This filter is documented in /wp-content/plugins/buddypress/bp-core/bp-core-functions.php */
$blog_id = (int) apply_filters( 'bp_get_root_blog_id', constant( 'BP_ROOT_BLOG' ) );
} else {
$blog_id = 1;
}
// Custom BP root blog, so set up cron on BP sub-site.
$switched = false;
if ( 1 !== $blog_id ) {
switch_to_blog( $blog_id );
wp_clear_scheduled_hook( 'ass_digest_event_weekly' );
$switched = true;
}
wp_schedule_event( $next_weekly, 'weekly', 'ass_digest_event_weekly' );
update_option( 'ass_weekly_digest', $day );
// Restore current blog.
if ( $switched ) {
restore_current_blog();
}
}
/*
// if in the future we want to do flexible schedules. this is how we could add the custom cron. Then we need to change the digest or summary to use this custom schedule.
function ass_custom_digest_frequency( $schedules ) {
if( ( $freq = get_option( 'ass_digest_frequency' ) ) ) {
if( !isset( $schedules[$freq.'_hrs'] ) ) {
$schedules[$freq.'_hrs'] = array( 'interval' => $freq * 3600, 'display' => "Every $freq hours" );
}
}
return $schedules;
}
add_filter( 'cron_schedules', 'ass_custom_digest_frequency' );
*/
/**
* Gets the user_login, user_nicename and email address for an array of user IDs
* all in one fell swoop!
*
* This is designed to avoid pinging the DB over and over in a foreach loop.
*/
function ass_get_mass_userdata( $user_ids = array() ) {
if ( empty( $user_ids ) )
return false;
global $wpdb;
// implode user IDs
$in = implode( ',', wp_parse_id_list( $user_ids ) );
// get our results
$results = $wpdb->get_results( "
SELECT ID, user_login, user_nicename, user_email
FROM {$wpdb->users}
WHERE ID IN ({$in})
", ARRAY_A );
if ( empty( $results ) )
return false;
$users = array();
// setup associative array
foreach( (array) $results as $result ) {
$users[ $result['ID'] ]['user_login'] = $result['user_login'];
$users[ $result['ID'] ]['user_nicename'] = $result['user_nicename'];
$users[ $result['ID'] ]['email'] = $result['user_email'];
}
return $users;
}
/**
* Get user domain.
*
* Previously, this was a cached version of the bp_core_get_user_domain() check.
*
* @since 3.9.0 Function is now a wrapper for the BP function.
* @since 4.1.2 Function is now a wrapper for the BP 12.0+ function bp_members_get_user_url().
*
* @param int $user_id
*/
function ass_digest_get_user_domain( $user_id ) {
return bp_members_get_user_url( $user_id );
}
// if the WP_Better_Emails plugin is installed, don't wrap the message with <html><body>$message</body></html>
function ass_digest_support_wp_better_emails( $message, $message_pre_html_wrap ) {
if ( class_exists( 'WP_Better_Emails' ) ) {
$message = $message_pre_html_wrap;
}
return $message;
}
add_filter( 'ass_digest_message_html', 'ass_digest_support_wp_better_emails', 10, 2 );
/**
* Get the CSS for digest emails.
*
* @since 3.9.0
*
* @return array
*/
function bpges_digest_css() {
$ass_email_css = array();
// HTML emails only work with inline CSS styles. Here we setup the styles to be used in various functions below.
$ass_email_css['wrapper'] = 'style="color:#333;clear:both;'; // use this to style the body
$ass_email_css['title'] = 'style="font-size:130%; margin:0 0 25px 0;"';
$ass_email_css['summary'] = '';
$ass_email_css['summary_ul'] = 'style="margin:0; padding:0 0 5px; list-style-type:circle; list-style-position:inside;"';
//$ass_email_css['summary'] = 'style="display:list-item;"';
$ass_email_css['follow_topic'] = 'style="padding:15px 0 0; color: #888;clear:both;"';
$ass_email_css['group_title'] = 'style="font-size:120%; background-color:#F5F5F5; padding:3px; margin:20px 0 0; border-top: 1px #eee solid;"';
$ass_email_css['change_email'] = 'style="font-size:12px; margin-left:10px; color:#888;"';
$ass_email_css['item_div'] = 'style="padding: 10px; border-top: 1px #eee solid;"';
$ass_email_css['item_action'] = 'style="color:#888;"';
$ass_email_css['item_date'] = 'style="font-size:85%; color:#bbb; margin-left:8px;"';
$ass_email_css['item_content'] = 'style="color:#333;"';
$ass_email_css['view_link'] = 'style="";';
$ass_email_css['item_weekly'] = 'style="color:#888; padding:4px 10px 0"'; // used in weekly in place of other item_ above
$ass_email_css['footer'] = 'class="ass-footer" style="margin:25px 0 0; padding-top:5px; border-top:1px #bbb solid;"';
// BP 2.5+ overrides.
if ( true === function_exists( 'bp_send_email' ) && true === ! apply_filters( 'bp_email_use_wp_mail', false ) ) {
$ass_email_css['summary_ul'] = 'style="margin:0; padding:0 0 25px 15px; list-style-type:circle; list-style-position:inside;"';
$ass_email_css['item_action'] = $ass_email_css['item_content'] = '';
$ass_email_css['item_date'] = 'style="font-size:85%;"';
}
/**
* Filters the CSS used when generating digests.
*
* @since 2.1
*
* @return array
*/
return apply_filters( 'ass_email_css', $ass_email_css );
}
/**
* Checks whether an item is valid to send in a digest for a user.
*
* @since 3.8.0
*
* @param
*/
function bp_ges_activity_is_valid_for_digest( $activity_id, $digest_type, $user_id = null ) {