PluginProbe
Customize WordPress Emails and Alerts – Better Notifications for WP / 1.6
Customize WordPress Emails and Alerts – Better Notifications for WP v1.6
1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.8 1.8.1 1.8.10 1.8.11 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 All 81 releases
bnfw / includes / engine / class-bnfw-engine.php

class-bnfw-engine.php in Customize WordPress Emails and Alerts – Better Notifications for WP 1.6, at includes/engine/class-bnfw-engine.php

820 lines 26.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * BNFW Engine
4 *
5 * @since 1.0
6 */
7
8 class BNFW_Engine {
9
10 /**
11 * Send test email.
12 *
13 * @since 1.2
14 *
15 * @param array $setting
16 */
17 public function send_test_email( $setting ) {
18 $subject = $setting['subject'];
19 $message = '<p><strong>This is a test email. All shortcodes below will show in place but not be replaced with content.</strong></p>' . stripslashes( $setting['message'] );
20
21 if ( 'true' != $setting['disable-autop'] && 'html' == $setting['email-formatting'] ) {
22 $message = wpautop( $message );
23 }
24
25 $current_user = wp_get_current_user();
26 $email = $current_user->user_email;
27
28 $headers = array();
29 if ( 'html' == $setting['email-formatting'] ) {
30 $headers[] = 'Content-type: text/html';
31 }
32
33 $subject = $this->handle_global_user_shortcodes( $subject, $email );
34 $message = $this->handle_global_user_shortcodes( $message, $email );
35 wp_mail( $email, stripslashes( $subject ), $message, $headers );
36 }
37
38 /**
39 * Send the notification email.
40 *
41 * @since 1.0
42 * @param array $setting
43 * @param int $id
44 */
45 public function send_notification( $setting, $id ) {
46 /**
47 * BNFW - Whether notification is disabled?
48 *
49 * @since 1.3.6
50 */
51 $notification_disabled = apply_filters( 'bnfw_notification_disabled', false, $id, $setting );
52
53 if ( ! $notification_disabled ) {
54 $subject = $this->handle_shortcodes( $setting['subject'], $setting['notification'], $id );
55 $message = $this->handle_shortcodes( $setting['message'], $setting['notification'], $id );
56 $emails = $this->get_emails( $setting, $id );
57 $headers = $this->get_headers( $emails );
58
59 if ( 'true' != $setting['disable-autop'] && 'html' == $setting['email-formatting'] ) {
60 $message = wpautop( $message );
61 }
62
63 if ( 'html' == $setting['email-formatting'] ) {
64 $headers[] = 'Content-type: text/html';
65 } else {
66 $headers[] = 'Content-type: text/plain';
67 }
68
69 if ( isset( $emails['to'] ) && is_array( $emails['to'] ) ) {
70 foreach ( $emails['to'] as $email ) {
71 wp_mail( $email, stripslashes( $this->handle_global_user_shortcodes( $subject, $email ) ), $this->handle_global_user_shortcodes( $message, $email ), $headers );
72 }
73 }
74 }
75 }
76
77 /**
78 * Send new user registration notification email.
79 *
80 * @since 1.1
81 * @param array $setting Notification setting
82 * @param object $user User object
83 * @param string $password_url Plain text password in WP < 4.3 and password url in WP > 4.3
84 */
85 public function send_registration_email( $setting, $user, $password_url = '' ) {
86 $user_id = $user->ID;
87
88 $subject = $this->handle_shortcodes( $setting['subject'], $setting['notification'], $user_id );
89 $message = $this->handle_shortcodes( $setting['message'], $setting['notification'], $user_id );
90
91 $subject = str_replace( '[password]', $password_url, $subject );
92 $message = str_replace( '[password]', $password_url, $message );
93
94 $subject = str_replace( '[password_url]', $password_url, $subject );
95 $message = str_replace( '[password_url]', $password_url, $message );
96
97 $subject = str_replace( '[login_url]', wp_login_url() , $subject );
98 $message = str_replace( '[login_url]', wp_login_url(), $message );
99
100 if ( 'true' != $setting['disable-autop'] && 'html' == $setting['email-formatting'] ) {
101 $message = wpautop( $message );
102 }
103
104 $headers = array();
105 if ( 'html' == $setting['email-formatting'] ) {
106 $headers[] = 'Content-type: text/html';
107 }
108
109 $subject = $this->handle_global_user_shortcodes( $subject, $user->user_email );
110 $message = $this->handle_global_user_shortcodes( $message, $user->user_email );
111 wp_mail( $user->user_email, stripslashes( $subject ), $message, $headers );
112 }
113
114 /**
115 * Send comment reply notification email.
116 *
117 * @since 1.3
118 * @param array $setting Notification setting
119 * @param object $comment Comment object
120 * @param object $parent_comment Parent comment object
121 */
122 public function send_comment_reply_email( $setting, $comment, $parent_comment ) {
123 $comment_id = $comment->comment_ID;
124
125 /**
126 * BNFW - Whether notification is disabled?
127 *
128 * @since 1.3.6
129 */
130 $notification_disabled = apply_filters( 'bnfw_notification_disabled', false, $comment_id, $setting );
131
132 if ( ! $notification_disabled ) {
133 $subject = $this->handle_shortcodes( $setting['subject'], $setting['notification'], $comment_id );
134 $message = $this->handle_shortcodes( $setting['message'], $setting['notification'], $comment_id );
135
136 $headers = array();
137 if ( 'html' == $setting['email-formatting'] ) {
138 $headers[] = 'Content-type: text/html';
139 }
140
141 if ( 'true' != $setting['disable-autop'] && 'html' == $setting['email-formatting'] ) {
142 $message = wpautop( $message );
143 }
144
145 $subject = $this->handle_global_user_shortcodes( $subject, $parent_comment->comment_author_email );
146 $message = $this->handle_global_user_shortcodes( $message, $parent_comment->comment_author_email );
147 wp_mail( $parent_comment->comment_author_email, stripslashes( $subject ), $message, $headers );
148 }
149 }
150
151 /**
152 * Send user role changed email.
153 *
154 * @since 1.3.9
155 *
156 * @param array $setting Notification setting
157 * @param int $user_id User ID
158 * @param array $old_role Old User Role.
159 * @param array $new_role New User Role.
160 */
161 public function send_user_role_changed_email( $setting, $user_id, $old_role, $new_role ) {
162 $subject = $this->handle_shortcodes( $setting['subject'], $setting['notification'], $user_id );
163 $message = $this->handle_shortcodes( $setting['message'], $setting['notification'], $user_id );
164
165 $subject = $this->handle_user_role_shortcodes( $subject, $old_role, $new_role );
166 $message = $this->handle_user_role_shortcodes( $message, $old_role, $new_role );
167
168 $headers = array();
169 if ( 'html' == $setting['email-formatting'] ) {
170 $headers[] = 'Content-type: text/html';
171 }
172
173 if ( 'true' != $setting['disable-autop'] && 'html' == $setting['email-formatting'] ) {
174 $message = wpautop( $message );
175 }
176
177 $user = get_user_by( 'id', $user_id );
178
179 $subject = $this->handle_global_user_shortcodes( $subject, $user->user_email );
180 $message = $this->handle_global_user_shortcodes( $message, $user->user_email );
181 wp_mail( $user->user_email, stripslashes( $subject ), $message, $headers );
182 }
183
184 /**
185 * Handle User Role shortcodes.
186 *
187 * @param string $message String that needs shortcode processing.
188 * @param array $old_role Old User Role.
189 * @param array $new_role New User Role.
190 *
191 * @return string Processed string.
192 */
193 public function handle_user_role_shortcodes( $message, $old_role, $new_role ) {
194 $roles = wp_roles();
195
196 $old_role_name = '';
197 $new_role_name = '';
198
199 if ( isset( $roles->role_names[$old_role] ) ) {
200 $old_role_name = $roles->role_names[$old_role];
201 }
202
203 if ( isset( $roles->role_names[$new_role] ) ) {
204 $new_role_name = $roles->role_names[$new_role];
205 }
206
207 $message = str_replace( '[user_role_old]', $old_role_name, $message );
208 $message = str_replace( '[user_role_new]', $new_role_name, $message );
209
210 return $message;
211 }
212
213 /**
214 * Handle shortcodes for filtered data notifications like `password_changed` and `email_changed`.
215 *
216 * @since 1.6
217 *
218 * @param array $email_data Email data.
219 * @param array $setting Notification settings.
220 * @param string|int $extra_data Extra data.
221 *
222 * @return array Modified email data.
223 */
224 public function handle_filtered_data_notification( $email_data, $setting, $extra_data ) {
225 $email_data['message'] = $this->handle_shortcodes( $setting['message'], $setting['notification'], $extra_data );
226 $email_data['subject'] = $this->handle_shortcodes( $setting['subject'], $setting['notification'], $extra_data );
227
228 $emails = $this->get_emails( $setting, $extra_data );
229 $headers = $this->get_headers( $emails );
230
231 if ( 'true' != $setting['disable-autop'] && 'html' == $setting['email-formatting'] ) {
232 $email_data['message'] = wpautop( $email_data['message'] );
233 }
234
235 if ( 'html' == $setting['email-formatting'] ) {
236 $headers[] = 'Content-type: text/html';
237 } else {
238 $headers[] = 'Content-type: text/plain';
239 }
240
241 $email_data['headers'] = $headers;
242
243 return $email_data;
244 }
245
246 /**
247 * Handle shortcode for password reset email message.
248 *
249 * @since 1.1
250 *
251 * @param $setting
252 * @param $key
253 * @param $user_login
254 * @param $user_data
255 *
256 * @return mixed|string
257 */
258 public function handle_password_reset_shortcodes( $setting, $key, $user_login, $user_data ) {
259 $message = '';
260
261 if ( '' != $user_login ) {
262 // For WordPress version 4.1.0 or less, we could have empty user_login
263 $message = $this->handle_shortcodes( $setting['message'], 'user-password', $user_data->ID );
264 $message = $this->handle_global_user_shortcodes( $message, $user_data->user_email );
265
266 $reset_link = wp_login_url() . "?action=rp&key=$key&login=$user_login";
267 $message = str_replace( '[password_reset_link]', $reset_link, $message );
268 }
269
270 return $message;
271 }
272
273 /**
274 * Generate message for notification.
275 *
276 * @since 1.0
277 * public since @since 1.6
278 *
279 * @param string $message String may have shortcode.
280 * @param string $notification Notification name.
281 * @param string|int $extra_data Additional data for shortcode.
282 *
283 * @return string Processed string.
284 */
285 public function handle_shortcodes( $message, $notification, $extra_data ) {
286 switch ( $notification ) {
287 case 'new-comment':
288 case 'new-trackback':
289 case 'new-pingback':
290 case 'reply-comment':
291 // handle new comments, trackbacks and pingbacks
292 $message = $this->comment_shortcodes( $message, $extra_data );
293 $comment = get_comment( $extra_data );
294 $message = $this->post_shortcodes( $message, $comment->comment_post_ID );
295 if ( 0 != $comment->user_id ) {
296 $message = $this->user_shortcodes( $message, $comment->user_id );
297 }
298 break;
299
300 case 'admin-password':
301 case 'user-password':
302 case 'admin-user':
303 case 'welcome-email':
304 case 'new-user':
305 case 'user-role':
306 case 'admin-role':
307 case 'password-changed':
308 case 'email-changed':
309 // handle users (lost password and new user registration)
310 $message = $this->user_shortcodes( $message, $extra_data );
311 break;
312
313 case 'new-category':
314 // handle new category
315 $message = $this->taxonomy_shortcodes( $message, 'category', $extra_data );
316 break;
317
318 case 'new-post_tag':
319 // handle new tag
320 $message = $this->taxonomy_shortcodes( $message, 'post_tag', $extra_data );
321 break;
322
323 case 'core-updated':
324 // handle core updated type
325 $message = $this->core_updated_shortcodes( $message, $extra_data );
326 break;
327
328 default:
329 $type = explode( '-', $notification, 2 );
330 if ( 'newterm' == $type[0] ) {
331 // handle new terms
332 $message = $this->taxonomy_shortcodes( $message, $type[1], $extra_data );
333
334 } else if ( 'new' == $type[0] || 'update' == $type[0] || 'pending' == $type[0] || 'future' == $type[0] || 'private' == $type[0] ) {
335 // handle new, update and pending posts
336 $post_types = get_post_types( array( 'public' => true ), 'names' );
337 $post_types = array_diff( $post_types, array( BNFW_Notification::POST_TYPE ) );
338
339 if ( in_array( $type[1], $post_types ) ) {
340 $message = $this->post_shortcodes( $message, $extra_data );
341 $post = get_post( $extra_data );
342 $message = $this->user_shortcodes( $message, $post->post_author );
343 }
344 } else if ( 'comment' == $type[0] || 'commentreply' == $type[0] ) {
345 $message = $this->comment_shortcodes( $message, $extra_data );
346 $comment = get_comment( $extra_data );
347 $message = $this->post_shortcodes( $message, $comment->comment_post_ID );
348 if ( 0 != $comment->user_id ) {
349 $message = $this->user_shortcodes( $message, $comment->user_id );
350 }
351 }
352 break;
353 }
354
355 $message = $this->global_shortcodes( $message );
356
357 $message = apply_filters( 'bnfw_shortcodes', $message, $notification, $extra_data, $this );
358 return $message;
359 }
360
361 /**
362 * Handle Global shortcodes.
363 *
364 * @since 1.5
365 *
366 * @param string $message String with shortcodes.
367 *
368 * @return string String after processing global shortcodes.
369 */
370 private function global_shortcodes( $message ) {
371 $message = str_replace( '[global_site_title]', get_bloginfo( 'name' ), $message );
372 $message = str_replace( '[global_site_tagline]', get_bloginfo( 'description' ), $message );
373 $message = str_replace( '[global_site_url]', get_bloginfo( 'url' ), $message );
374
375 return $message;
376 }
377
378 /**
379 * Handle post shortcodes.
380 *
381 * @since 1.0
382 * @param string $message
383 * @param int $post_id
384 * @return string
385 */
386 private function post_shortcodes( $message, $post_id ) {
387 $post = get_post( $post_id );
388
389 $post_content = apply_filters( 'the_content', $post->post_content );
390 $post_content = str_replace( ']]>', ']]&gt;', $post_content );
391
392 $message = str_replace( '[ID]', $post->ID, $message );
393 $message = str_replace( '[post_date]', $post->post_date, $message );
394 $message = str_replace( '[post_date_gmt]', $post->post_date_gmt, $message );
395 $message = str_replace( '[post_content]', $post_content, $message );
396 $message = str_replace( '[post_title]', $post->post_title, $message );
397 $message = str_replace( '[post_excerpt]', ( $post->post_excerpt ? $post->post_excerpt : wp_trim_words( $post_content ) ), $message );
398 $message = str_replace( '[post_status]', $post->post_status, $message );
399 $message = str_replace( '[comment_status]', $post->comment_status, $message );
400 $message = str_replace( '[ping_status]', $post->ping_status, $message );
401 $message = str_replace( '[post_password]', $post->post_password, $message );
402 $message = str_replace( '[post_name]', $post->post_name, $message );
403 $message = str_replace( '[post_slug]', $post->post_name, $message );
404 $message = str_replace( '[to_ping]', $post->to_ping, $message );
405 $message = str_replace( '[pinged]', $post->pinged, $message );
406 $message = str_replace( '[post_modified]', $post->post_modified, $message );
407 $message = str_replace( '[post_modified_gmt]', $post->post_modified_gmt, $message );
408 $message = str_replace( '[post_content_filtered]', $post->post_content_filtered, $message );
409 $message = str_replace( '[post_parent]', $post->post_parent, $message );
410 $message = str_replace( '[post_parent_permalink]', get_permalink( $post->post_parent ), $message );
411 $message = str_replace( '[guid]', $post->guid, $message );
412 $message = str_replace( '[menu_order]', $post->menu_order, $message );
413 $message = str_replace( '[post_type]', $post->post_type, $message );
414 $message = str_replace( '[post_mime_type]', $post->post_mime_type, $message );
415 $message = str_replace( '[comment_count]', $post->comment_count, $message );
416 $message = str_replace( '[permalink]', get_permalink( $post->ID ), $message );
417 $message = str_replace( '[edit_post]', get_edit_post_link( $post->ID ), $message );
418
419 if ( has_post_thumbnail( $post->ID ) ) {
420 $image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
421 if ( is_array( $image_url ) ) {
422 $message = str_replace( '[featured_image]', $image_url[0], $message );
423 }
424 }
425
426 if ( 'future' == $post->post_status ) {
427 $message = str_replace( '[post_scheduled_date]', $post->post_date, $message );
428 $message = str_replace( '[post_scheduled_date_gmt]', $post->post_date_gmt, $message );
429 } else {
430 $message = str_replace( '[post_scheduled_date]', 'Published', $message );
431 $message = str_replace( '[post_scheduled_date_gmt]', 'Published', $message );
432 }
433
434 $category_list = implode( ', ', wp_get_post_categories( $post_id, array( 'fields' => 'names' ) ) );
435 $message = str_replace( '[post_category]', $category_list, $message );
436
437 $tag_list = implode( ', ', wp_get_post_tags( $post_id, array( 'fields' => 'names' ) ) );
438 $message = str_replace( '[post_tag]', $tag_list, $message );
439
440 $user_info = get_userdata( $post->post_author );
441 $message = str_replace( '[post_author]', $user_info->display_name, $message );
442
443 $message = str_replace( '[author_link]', get_author_posts_url( $post->post_author ), $message );
444
445 if ( $last_id = get_post_meta( $post->ID, '_edit_last', true ) ) {
446 if ( $post->post_author != $last_id ) {
447 $last_user_info = get_userdata( $last_id );
448 } else {
449 $last_user_info = $user_info;
450 }
451
452 $message = str_replace( '[post_update_author]', $last_user_info->display_name, $message );
453 }
454
455 $message = apply_filters( 'bnfw_shortcodes_post', $message, $post_id );
456 return $message;
457 }
458
459 /**
460 * Handle comment shortcodes.
461 *
462 * @since 1.0
463 *
464 * @param string $message String to be processed.
465 * @param int $comment_id Comment id.
466 *
467 * @return string Processed string.
468 */
469 private function comment_shortcodes( $message, $comment_id ) {
470 $comment = get_comment( $comment_id );
471
472 $message = str_replace( '[comment_ID]', $comment->comment_ID, $message );
473 $message = str_replace( '[comment_post_ID]', $comment->comment_post_ID, $message );
474 $message = str_replace( '[comment_author]', $comment->comment_author, $message );
475 $message = str_replace( '[comment_author_email]', $comment->comment_author_email, $message );
476 $message = str_replace( '[comment_author_url]', $comment->comment_author_url, $message );
477 $message = str_replace( '[comment_author_IP]', $comment->comment_author_IP, $message );
478 $message = str_replace( '[comment_date]', $comment->comment_date, $message );
479 $message = str_replace( '[comment_date_gmt]', $comment->comment_date_gmt, $message );
480 $message = str_replace( '[comment_content]', get_comment_text( $comment->comment_ID ), $message );
481 $message = str_replace( '[comment_karma]', $comment->comment_karma, $message );
482 $message = str_replace( '[comment_approved]', str_replace( array( '0', '1', 'spam' ), array( 'awaiting moderation', 'approved', 'spam' ), $comment->comment_approved ), $message );
483 $message = str_replace( '[comment_agent]', $comment->comment_agent, $message );
484 $message = str_replace( '[comment_type]', $comment->comment_type, $message );
485 $message = str_replace( '[comment_parent]', $comment->comment_parent, $message );
486 $message = str_replace( '[user_id]', $comment->user_id, $message );
487 $message = str_replace( '[permalink]', get_comment_link( $comment->comment_ID ), $message );
488
489 return $message;
490 }
491
492 /**
493 * Handle user shortcodes.
494 *
495 * @since 1.0
496 *
497 * @param string $message String to be processed.
498 * @param int $user_id User id.
499 *
500 * @return string Processed string.
501 */
502 private function user_shortcodes( $message, $user_id ) {
503 $user_info = get_userdata( $user_id );
504
505 // deperecated
506 $message = str_replace( '[ID]', $user_info->ID, $message );
507
508 $message = str_replace( '[user_id]', $user_info->ID, $message );
509 $message = str_replace( '[user_login]', $user_info->user_login, $message );
510 $message = str_replace( '[user_nicename]', $user_info->user_nicename, $message );
511 $message = str_replace( '[user_email]', $user_info->user_email, $message );
512 $message = str_replace( '[user_url]', $user_info->user_url, $message );
513 $message = str_replace( '[user_registered]', $user_info->user_registered, $message );
514 $message = str_replace( '[display_name]', $user_info->display_name, $message );
515 $message = str_replace( '[user_firstname]', $user_info->user_firstname, $message );
516 $message = str_replace( '[user_lastname]', $user_info->user_lastname, $message );
517 $message = str_replace( '[nickname]', $user_info->nickname, $message );
518 $message = str_replace( '[user_description]', $user_info->user_description, $message );
519 $message = str_replace( '[user_avatar]', get_avatar_url( $user_id ), $message );
520 $message = str_replace( '[user_avatar]', get_avatar_url( $user_id ), $message );
521 $message = str_replace( '[commenter_avatar]', get_avatar_url( $user_id ), $message );
522
523 $user_capabilities = bnfw_format_user_capabilities( $user_info->wp_capabilities );
524 if ( ! empty( $user_capabilities ) ) {
525 $message = str_replace( '[wp_capabilities]', $user_capabilities, $message );
526 }
527
528 $message = apply_filters( 'bnfw_shortcodes_user', $message, $user_id );
529 return $message;
530 }
531
532 /**
533 * Handle taxonomy shortcodes.
534 *
535 * @access private
536 * @since 1.1
537 *
538 * @param string $message
539 * @param string $taxonomy
540 * @param int $term_id
541 * @return string
542 */
543 private function taxonomy_shortcodes( $message, $taxonomy, $term_id ) {
544 $term_info = get_term( $term_id, $taxonomy );
545
546 $message = str_replace( '[slug]', $term_info->slug, $message );
547 $message = str_replace( '[name]', $term_info->name, $message );
548 $message = str_replace( '[description]', $term_info->description, $message );
549
550 return $message;
551 }
552
553 /**
554 * Handle Core Updated Shortcodes.
555 *
556 * @since 1.6
557 *
558 * @param string $message Original message with shortcodes.
559 * @param string $type The type of email being sent. Can be one of
560 * 'success', 'fail', 'manual', 'critical'.
561 *
562 * @return string Modified content.
563 */
564 private function core_updated_shortcodes( $message, $type ) {
565 $message = str_replace( '[core_update_status]', $type, $message );
566
567 return $message;
568 }
569
570 /**
571 * Get the list of emails from the notification settings.
572 *
573 * @since 1.0
574 * @param array $setting Notification settings
575 * @param int $id
576 * @return array Emails
577 */
578 private function get_emails( $setting, $id ) {
579 global $current_user;
580
581 $emails = array();
582
583 $exclude = null;
584 if ( 'true' == $setting['disable-current-user'] ) {
585 if ( isset( $current_user->ID ) ) {
586 $exclude = $current_user->ID;
587 }
588 }
589
590 if ( 'true' === $setting['only-post-author'] ) {
591
592 $post_id = $id;
593 if ( bnfw_is_comment_notification( $setting['notification'] ) ) {
594 $comment = get_comment( $id );
595 $post_id = $comment->comment_post_ID;
596 }
597
598 $post_author = get_post_field( 'post_author', $post_id );
599 $author = get_user_by( 'id', $post_author );
600 if ( false !== $author && $post_author != $exclude ) {
601 $emails['to'] = array( $author->user_email );
602 }
603 } else {
604 $to_emails = array();
605
606 if ( ! empty( $setting['users'] ) ) {
607 $to_emails = $this->get_emails_from_users( $setting['users'], $exclude );
608 }
609
610 /**
611 * BNFW get to emails.
612 */
613 $emails['to'] = apply_filters( 'bnfw_to_emails', $to_emails, $setting, $id );
614 }
615
616 if ( 'true' == $setting['show-fields'] ) {
617 if ( ! empty( $setting['from-name'] ) && ! empty( $setting['from-email'] ) ) {
618 $emails['from'] = $setting['from-name'] . ' <' . $setting['from-email'] . '>' ;
619 } else {
620 $emails['from'] = get_option( 'blogname' ) . ' <' . get_option( 'admin_email' ) . '>' ;
621 }
622
623 if ( ! empty( $setting['reply-name'] ) ) {
624 $emails['reply-name'] = $setting['reply-name'];
625 }
626
627 if ( ! empty( $setting['reply-email'] ) ) {
628 $emails['reply-email'] = $setting['reply-email'];
629 }
630
631 if ( ! empty( $setting['cc'] ) ) {
632 $emails['cc'] = $this->get_emails_from_users( $setting['cc'], $exclude );
633 }
634
635 if ( ! empty( $setting['bcc'] ) ) {
636 $emails['bcc'] = $this->get_emails_from_users( $setting['bcc'], $exclude );
637 }
638 }
639
640 return $emails;
641 }
642
643 /**
644 * Get emails from users.
645 *
646 * @since 1.2
647 *
648 * @param array $users Users Array
649 * @param int $exclude User id to exclude
650 *
651 * @return array
652 */
653 public function get_emails_from_users( $users, $exclude = null ) {
654 $user_ids = array();
655 $user_roles = array();
656
657 if ( empty( $users ) ) {
658 return array();
659 }
660
661 foreach ( $users as $user ) {
662 if ( $this->starts_with( $user, 'role-' ) ) {
663 $user_roles[] = str_replace( 'role-', '', $user );
664 } else {
665 $user_ids[] = absint( $user );
666 }
667 }
668
669 if ( null != $exclude ) {
670 $user_ids = array_diff( $user_ids, array( $exclude ) );
671 }
672 $emails_from_user_ids = $this->get_emails_from_id( $user_ids );
673
674 $emails_from_user_roles = $this->get_emails_from_role( $user_roles, $exclude );
675
676 return array_merge( $emails_from_user_roles, $emails_from_user_ids );
677 }
678
679 /**
680 * Get user emails by user ids.
681 *
682 * @since 1.0
683 *
684 * @param array $user_ids.
685 *
686 * @return array Emails.
687 */
688 private function get_emails_from_id( $user_ids ) {
689 $email_list = array();
690 if ( is_array( $user_ids ) && count( $user_ids ) > 0 ) {
691 $user_query = new WP_User_Query( array( 'include' => $user_ids ) );
692 foreach ( $user_query->results as $user ) {
693 $email_list[] = $user->user_email;
694 }
695 }
696 return $email_list;
697 }
698
699 /**
700 * Get emails of users based on role.
701 *
702 * @since 1.0
703 * @param array $roles User Roles
704 * @param int $exclude User id to exclude
705 * @return array Email ids
706 */
707 private function get_emails_from_role( $roles, $exclude = null ) {
708 if ( ! is_array( $roles ) ) {
709 $roles = array( $roles );
710 }
711
712 $email_list = array();
713 foreach ( $roles as $role ) {
714 $role_name = $this->get_role_name_by_label( $role );
715 $users = get_users(
716 array(
717 'role' => $role_name,
718 'fields' => array( 'user_email', 'ID' ),
719 )
720 );
721
722 foreach ( $users as $user ) {
723 if ( null != $exclude ) {
724 if ( $user->ID == $exclude ) {
725 continue;
726 }
727 }
728 $email_list[] = $user->user_email;
729 }
730 }
731
732 return $email_list;
733 }
734
735 /**
736 * Find if a string starts with another string.
737 *
738 * @since 1.2
739 *
740 * @param $haystack
741 * @param $needle
742 *
743 * @return bool
744 */
745 private function starts_with( $haystack, $needle ) {
746 // search backwards starting from haystack length characters from the end
747 return '' === $needle || strrpos( $haystack, $needle, -strlen( $haystack ) ) !== false;
748 }
749
750 /**
751 * Get User role name by label.
752 *
753 * @param mixed $role_label
754 *
755 * @return int|string
756 */
757 protected function get_role_name_by_label( $role_label ) {
758 global $wp_roles;
759 foreach ( $wp_roles->roles as $role_name => $role_info ) {
760 if ( $role_label == $role_info['name'] || $role_name == $role_label ) {
761 return $role_name;
762 }
763 }
764
765 // There is something wrong
766 return '';
767 }
768
769 /**
770 * Generate email headers based on the emails.
771 *
772 * @since 1.0
773 * @param array $emails
774 * @return array
775 */
776 private function get_headers( $emails ) {
777 $headers = array();
778
779 if ( ! empty( $emails['from'] ) ) {
780 $headers[] = 'From:' . $emails['from'];
781 }
782
783 if ( ! empty( $emails['reply-email'] ) ) {
784 $headers[] = 'Reply-To:' . $emails['reply-name'] . '<' . $emails['reply-email'] . '>';
785 }
786
787 if ( ! empty( $emails['cc'] ) ) {
788 $headers[] = 'Cc:' . implode( ',', $emails['cc'] );
789 }
790 if ( ! empty( $emails['bcc'] ) ) {
791 $headers[] = 'Bcc:' . implode( ',', $emails['bcc'] );
792 }
793
794 return $headers;
795 }
796
797 /**
798 * Handle Global User Shortcodes.
799 *
800 * @param string $message String to be processed.
801 * @param string $email Email of the user.
802 *
803 * @return string Processed string.
804 */
805 private function handle_global_user_shortcodes( $message, $email ) {
806 $user = get_user_by( 'email', $email );
807
808 if ( false === $user ) {
809 $message = str_replace( '[global_user_firstname]', $email, $message );
810 $message = str_replace( '[global_user_lastname]', $email, $message );
811 } else {
812 $message = str_replace( '[global_user_firstname]', $user->first_name, $message );
813 $message = str_replace( '[global_user_lastname]', $user->last_name, $message );
814 }
815 $message = str_replace( '[global_user_email]', $email, $message );
816
817 return $message;
818 }
819 }
820