-
Notifications
You must be signed in to change notification settings - Fork 1
/
class-email-notifier.php
293 lines (222 loc) · 6.72 KB
/
class-email-notifier.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
<?php
/**
* Email notifier class
*
* @package bbp-notify-admins
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Notifies Admins by email
*/
class BBP_Notify_Admin_Email_Notifier {
/**
* Class instance
*
* @var BBP_Notify_Admin_Email_Notifier
*/
private static $instance;
/**
* Get the singleton.
*
* @return BBP_Notify_Admin_Email_Notifier
*/
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Notify admins on new topic
*
* @param int $topic_id topic id.
* @param int $forum_id forum id.
* @param array $anonymous_data anonymous user data.
* @param int $topic_author topic author user id.
*/
public function notify_topic( $topic_id, $forum_id, $anonymous_data, $topic_author ) {
$topic_id = bbp_get_topic_id( $topic_id );
$forum_id = bbp_get_forum_id( $forum_id );
remove_all_filters( 'bbp_get_topic_content' );
remove_all_filters( 'bbp_get_topic_title' );
$topic_title = strip_tags( bbp_get_topic_title( $topic_id ) );
$topic_content = strip_tags( bbp_get_topic_content( $topic_id ) );
$topic_url = bbp_get_topic_permalink( $topic_id );
$topic_author_name = bbp_get_topic_author_display_name( $topic_id );
$message = sprintf( __( '%1$s created new topic:
%2$s
Topic Link: %3$s
-----------
You are receiving this email because you asked for it.
Login and visit the settings to disable these emails.', 'bbp-notify-admin' ),
$topic_author_name,
$topic_content,
$topic_url
);
$message = apply_filters( 'bbp_notify_admin_topic_mail_message', $message, $topic_id, $forum_id );
if ( empty( $message ) ) {
return;
}
$subject = apply_filters( 'bbp_notify_admin_reply_mail_title', $this->get_subject( __( 'New Topic: ', 'bbp-notify-admin' ) . $topic_title ), $topic_id, $forum_id );
if ( empty( $subject ) ) {
return;
}
$headers = $this->get_headers();
// Get all users emails.
$emails = $this->get_emails( 'topic' );
if ( empty( $emails ) ) {
return;// no one to send to.
}
$to_email = array_shift( $emails );
// Loop through users.
foreach ( $emails as $email ) {
// add all other users as bcc(only applies in case we have more than 1 admin ).
$headers[] = 'Bcc: ' . $email;
}
// send email.
// even if an admin posts, It will notify everyone including them.
$this->notify(
array(
'subject' => $subject,
'message' => $message,
'to' => $to_email,
'headers' => $headers,
)
);
}
/**
* Notify admins on new reply on the forum
*
* A modified/inspired version of bbp_notify_topic_subscribers()
*
* @param int $reply_id reply id.
* @param int $topic_id topic id.
* @param int $forum_id forum id.
* @param array $anonymous_data Anonymous data.
* @param int $reply_author reply poster.
* @param bool $unknown_boolean unknown.
* @param int $reply_to post id which we are replying.
*/
public function notify_reply( $reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author, $unknown_boolean, $reply_to ) {
$reply_id = bbp_get_reply_id( $reply_id );
$topic_id = bbp_get_topic_id( $topic_id );
$forum_id = bbp_get_forum_id( $forum_id );
// Poster name.
$reply_author_name = bbp_get_reply_author_display_name( $reply_id );
remove_all_filters( 'bbp_get_reply_content' );
remove_all_filters( 'bbp_get_topic_title' );
// Strip tags from text and setup mail data.
$topic_title = strip_tags( bbp_get_topic_title( $topic_id ) );
$reply_content = strip_tags( bbp_get_reply_content( $reply_id ) );
$reply_url = bbp_get_reply_url( $reply_id );
$message = sprintf( __( '%1$s wrote:
%2$s
Post Link: %3$s
-----------
You are receiving this email because you askd for it.
Login and visit the settings to disable these emails.', 'bbp-notify-admin' ),
$reply_author_name,
$reply_content,
$reply_url
);
$message = apply_filters( 'bbp_notify_admin_reply_mail_message', $message, $reply_id, $topic_id );
if ( empty( $message ) ) {
return;
}
$subject = apply_filters( 'bbp_notify_admin_reply_mail_title', $this->get_subject( __( 'New Reply: ', 'bbp-notify-admin' ) . $topic_title ), $reply_id, $topic_id );
if ( empty( $subject ) ) {
return;
}
$headers = $this->get_headers();
// Get all users emails.
$emails = $this->get_emails( 'reply' );
if ( empty( $emails ) ) {
return; // No one to send to.
}
$to_email = array_shift( $emails );
// Loop through users.
foreach ( $emails as $email ) {
// Add all other users as bcc(only applies in case we have more than 1 admin ).
$headers[] = 'Bcc:' . $email;
}
$this->notify(
array(
'subject' => $subject,
'message' => $message,
'to' => $to_email,
'headers' => $headers,
)
);
}
/**
* Send email
*
* @param array $args args.
*/
private function notify( $args = null ) {
$defaults = array(
'subject' => '',
'message' => '',
'to' => '',
'headers' => '',
);
$args = wp_parse_args( $args, $defaults );
extract( $args );
if ( ! $args['to'] || ! $args['subject'] || ! $args['message'] ) {
return;
}
wp_mail( $args['to'], $args['subject'], $args['message'], $args['headers'] );
}
/**
* Get basic header(from etc)
*
* @return array
*/
private function get_headers() {
// Get the noreply@sitename.com email address.
$no_reply = bbp_get_do_not_reply_address();
$from_email = apply_filters( 'bbp_notify_admin_from_email', $no_reply );
// Setup the From header.
$headers = array( 'From: ' . get_bloginfo( 'name' ) . ' <' . $from_email . '>' );
return $headers;
}
/**
* Get subject of the message
*
* @param string $subject subject.
*
* @return string
*/
private function get_subject( $subject ) {
$blog_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
return '[' . $blog_name . '] ' . $subject;
}
/**
* Get the email addresses list.
*
* @param string $context context.
*
* @return array
*/
private function get_emails( $context = null ) {
$emails = get_option( 'bbp_notify_admins_settings' );
$emails = isset( $emails['bbp_notify_admins_emails'] ) ? $emails['bbp_notify_admins_emails'] : '';
$emails = explode( ',', $emails );
if ( empty( $emails ) ) {
return array();
}
$emails = array_map( 'trim', $emails );
$emails = apply_filters( 'bbp_notify_admin_email_addresses', $emails, $context );
$current_user = wp_get_current_user();
$current_user_email = '';
if ( ! empty( $current_user ) ) {
$current_user_email = $current_user->user_email;
}
// when an admin is posting, we exclude him from the list too.
$emails = array_diff( $emails, (array) $current_user_email );
return $emails;
}
}