PluginProbe
Friends / 2.7.7
Friends v2.7.7
4.3.2 4.3.1 4.3.0 4.2.2 4.2.1 4.2.0 4.1.0 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.0 2.9.1 All 88 releases
friends / includes / class-notifications.php

class-notifications.php in Friends 2.7.7, at includes/class-notifications.php

395 lines 13.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Friends Notifications
4 *
5 * This contains the functions for Notifications.
6 *
7 * @package Friends
8 */
9
10 namespace Friends;
11
12 /**
13 * This is the class for the Notifications part of the Friends Plugin.
14 *
15 * @since 0.6
16 *
17 * @package Friends
18 * @author Alex Kirk
19 */
20 class Notifications {
21 /**
22 * Contains a reference to the Friends class.
23 *
24 * @var Friends
25 */
26 private $friends;
27
28 /**
29 * Constructor
30 *
31 * @param Friends $friends A reference to the Friends object.
32 */
33 public function __construct( Friends $friends ) {
34 $this->friends = $friends;
35 $this->register_hooks();
36 }
37
38 /**
39 * Register the WordPress hooks
40 */
41 private function register_hooks() {
42 add_action( 'friends_rewrite_mail_html', array( $this, 'rewrite_mail_html' ) );
43 add_action( 'notify_new_friend_post', array( $this, 'notify_new_friend_post' ), 10, 2 );
44 add_filter( 'notify_keyword_match_post', array( $this, 'notify_keyword_match_post' ), 10, 3 );
45 add_action( 'notify_new_friend_request', array( $this, 'notify_new_friend_request' ) );
46 add_action( 'notify_accepted_friend_request', array( $this, 'notify_accepted_friend_request' ) );
47 add_action( 'notify_friend_message_received', array( $this, 'notify_friend_message_received' ), 10, 3 );
48 }
49
50 /**
51 * Sets the friends plugin from email address when sending mail.
52 *
53 * If you're having issues with the receiver using the envelope From: address in favor of the mail header,
54 * you can use a filter like this:
55 * add_filter( 'wp_mail_from', function( $from ) {
56 * ini_set( 'sendmail_from', $from );
57 * return $from;
58 * }, 100 );
59 *
60 * @param string $from The from that's supposed to be used.
61 *
62 * @return string The friends plugin from email address.
63 */
64 public function use_friends_plugin_from_email_address( $from ) {
65 $from = preg_replace( '/^wordpress@/', 'friends-plugin@', $from );
66 return $from;
67 }
68
69
70 /**
71 * Gets the friends plugin from email address.
72 *
73 * @return string The friends plugin from email address.
74 */
75 public function get_friends_plugin_from_email_address() {
76 return apply_filters( 'wp_mail_from', 'wordpress@' . preg_replace( '#^www\.#', '', wp_parse_url( network_home_url(), PHP_URL_HOST ) ) );
77 }
78
79 /**
80 * Notify the users of this site about a new friend post
81 *
82 * @param \WP_Post $post The new post by a friend or subscription.
83 * @param User_Feed $user_feed The feed where the post came from.
84 */
85 public function notify_new_friend_post( \WP_Post $post, User_Feed $user_feed ) {
86 if (
87 // Post might be trashed through rules.
88 'trash' === $post->post_status
89 // Don't notify about posts older than a week.
90 || strtotime( $post->post_date_gmt ) + WEEK_IN_SECONDS < time()
91 ) {
92 return;
93 }
94 $user = new User( Friends::get_main_friend_user_id() );
95 if ( ! $user->user_email ) {
96 return;
97 }
98 $author = $user_feed->get_friend_user();
99
100 $notify_user = ! get_user_option( 'friends_no_new_post_notification', $user->ID );
101 $notify_user = $notify_user && ! get_user_option( 'friends_no_new_post_notification_' . $author->user_login, $user->ID );
102
103 if ( ! apply_filters( 'notify_user_about_friend_post', $notify_user, $user, $post, $author ) ) {
104 return;
105 }
106
107 $email_title = $post->post_title;
108
109 $params = array(
110 'author' => $author,
111 'post' => $post,
112 );
113
114 $email_message = array();
115 ob_start();
116 Friends::template_loader()->get_template_part( 'email/header', null, array( 'email_title' => $email_title ) );
117 Friends::template_loader()->get_template_part( 'email/new-friend-post', null, $params );
118 Friends::template_loader()->get_template_part( 'email/footer' );
119 $email_message['html'] = ob_get_contents();
120 ob_end_clean();
121
122 ob_start();
123 Friends::template_loader()->get_template_part( 'email/new-friend-post-text', null, $params );
124 Friends::template_loader()->get_template_part( 'email/footer-text' );
125 $email_message['text'] = ob_get_contents();
126 ob_end_clean();
127
128 $this->send_mail( $user->user_email, wp_specialchars_decode( $email_title, ENT_QUOTES ), $email_message, array(), array(), $author->user_login );
129 }
130
131 /**
132 * Notifies about a post that matched the keyword.
133 *
134 * @param bool $notified Whether a notification was sent.
135 * @param \WP_Post $post The new post by a friend or subscription.
136 * @param string $keyword The matched keyword.
137 */
138 public function notify_keyword_match_post( $notified, \WP_Post $post, $keyword ) {
139 if ( 'trash' === $post->post_status ) {
140 return $notified;
141 }
142
143 $user = new User( Friends::get_main_friend_user_id() );
144 if ( ! $user->user_email ) {
145 return $notified;
146 }
147 $notify_user = ! get_user_option( 'friends_no_keyword_notification_' . $post->post_author, $user->ID );
148
149 if ( ! apply_filters( 'notify_user_about_keyword_post', $notify_user, $user, $post, $keyword ) ) {
150 return $notified;
151 }
152
153 $author = new User( $post->post_author );
154 // translators: %s is a keyword string specified by the user.
155 $email_title = sprintf( __( 'Keyword matched: %s', 'friends' ), $keyword );
156
157 $params = array(
158 'author' => $author,
159 'post' => $post,
160 'keyword' => $keyword,
161 );
162
163 $email_message = array();
164 ob_start();
165 Friends::template_loader()->get_template_part( 'email/header', null, array( 'email_title' => $email_title ) );
166 Friends::template_loader()->get_template_part( 'email/keyword-match-post', null, $params );
167 Friends::template_loader()->get_template_part( 'email/footer' );
168 $email_message['html'] = ob_get_contents();
169 ob_end_clean();
170
171 ob_start();
172 Friends::template_loader()->get_template_part( 'email/keyword-match-post-text', null, $params );
173 Friends::template_loader()->get_template_part( 'email/footer-text' );
174 $email_message['text'] = ob_get_contents();
175 ob_end_clean();
176
177 $this->send_mail( $user->user_email, wp_specialchars_decode( $email_title, ENT_QUOTES ), $email_message, array(), array(), $author->user_login );
178
179 return true;
180 }
181
182 /**
183 * Notify the users of this site about a new friend request
184 *
185 * @param User $friend_user The user requesting friendship.
186 */
187 public function notify_new_friend_request( User $friend_user ) {
188 if ( ! $friend_user->has_cap( 'friend_request' ) ) {
189 return;
190 }
191 $user = new User( Friends::get_main_friend_user_id() );
192
193 if ( ! $user->user_email ) {
194 return;
195 }
196
197 $notify_user = ! get_user_option( 'friends_no_friend_request_notification', $user->ID );
198 if ( ! apply_filters( 'notify_user_about_friend_request', $notify_user, $user, $friend_user ) ) {
199 return;
200 }
201
202 // translators: %s is a user display name.
203 $email_title = sprintf( __( '%s sent a Friend Request', 'friends' ), $friend_user->display_name );
204
205 $params = array(
206 'user' => $user,
207 'friend_user' => $friend_user,
208 );
209
210 $email_message = array();
211 ob_start();
212 Friends::template_loader()->get_template_part( 'email/header', null, array( 'email_title' => $email_title ) );
213 Friends::template_loader()->get_template_part( 'email/new-friend-request', null, $params );
214 Friends::template_loader()->get_template_part( 'email/footer' );
215 $email_message['html'] = ob_get_contents();
216 ob_end_clean();
217
218 ob_start();
219 Friends::template_loader()->get_template_part( 'email/new-friend-request-text', null, $params );
220 Friends::template_loader()->get_template_part( 'email/footer-text' );
221 $email_message['text'] = ob_get_contents();
222 ob_end_clean();
223
224 $this->send_mail( $user->user_email, $email_title, $email_message );
225 }
226
227 /**
228 * Notify the users of this site about an accepted friend request
229 *
230 * @param User $friend_user The user who accepted friendship.
231 */
232 public function notify_accepted_friend_request( User $friend_user ) {
233 $user = new User( Friends::get_main_friend_user_id() );
234 if ( ! $user->user_email ) {
235 return;
236 }
237
238 if ( ! apply_filters( 'notify_user_about_accepted_friend_request', true, $user, $friend_user ) ) {
239 return;
240 }
241
242 // translators: %s is a user display name.
243 $email_title = sprintf( __( '%s accepted your Friend Request', 'friends' ), $friend_user->display_name );
244
245 $params = array(
246 'user' => $user,
247 'friend_user' => $friend_user,
248 );
249
250 $email_message = array();
251 ob_start();
252 Friends::template_loader()->get_template_part( 'email/header', null, array( 'email_title' => $email_title ) );
253 Friends::template_loader()->get_template_part( 'email/accepted-friend-request', null, $params );
254 Friends::template_loader()->get_template_part( 'email/footer' );
255 $email_message['html'] = ob_get_contents();
256 ob_end_clean();
257
258 ob_start();
259 Friends::template_loader()->get_template_part( 'email/accepted-friend-request-text', null, $params );
260 Friends::template_loader()->get_template_part( 'email/footer-text' );
261 $email_message['text'] = ob_get_contents();
262 ob_end_clean();
263
264 $this->send_mail( $user->user_email, $email_title, $email_message );
265 }
266
267 /**
268 * Notify the users of this site about a received message
269 *
270 * @param User $friend_user The user who sent the message.
271 */
272
273 /**
274 * Notify the users of this site about a received message
275 *
276 * @param User $friend_user The user who sent the message.
277 * @param string $message The message.
278 * @param string $subject The subject.
279 */
280 public function notify_friend_message_received( User $friend_user, $message, $subject ) {
281 $user = new User( Friends::get_main_friend_user_id() );
282 if ( ! $user->user_email ) {
283 return;
284 }
285
286 if ( ! apply_filters( 'notify_user_about_friend_message', true, $user, $friend_user ) ) {
287 return;
288 }
289
290 // translators: %s is a user display name.
291 $email_title = sprintf( __( '%s sent you a message', 'friends' ), $friend_user->display_name );
292
293 $params = array(
294 'user' => $user,
295 'friend_user' => $friend_user,
296 'subject' => $subject,
297 'message' => $message,
298 );
299
300 $email_message = array();
301 ob_start();
302 Friends::template_loader()->get_template_part( 'email/header', null, array( 'email_title' => $email_title ) );
303 Friends::template_loader()->get_template_part( 'email/friend-message-received', null, $params );
304 Friends::template_loader()->get_template_part( 'email/footer' );
305 $email_message['html'] = ob_get_contents();
306 ob_end_clean();
307
308 ob_start();
309 Friends::template_loader()->get_template_part( 'email/friend-message-received-text', null, $params );
310 Friends::template_loader()->get_template_part( 'email/footer-text' );
311 $email_message['text'] = ob_get_contents();
312 ob_end_clean();
313
314 $this->send_mail( $user->user_email, $email_title, $email_message );
315 }
316
317 /**
318 * Rewrite HTML for e-mail (by inlining some CSS styles)
319 *
320 * @param string $html The HTML.
321 *
322 * @return string The rewritten HTML.
323 */
324 public function rewrite_mail_html( $html ) {
325 $img_regex = '<figure\b[^>]*>\s*(<a\b[^>]*>\s*)?<img\b';
326 $html = preg_replace( '/' . $img_regex . '/i', '$0 style="max-width: 100% !important; height: auto !important;"', $html );
327 $html = preg_replace( '/(' . $img_regex . '.*?)width=[\'"]\d+[\'"]/i', '$1', $html );
328 $html = preg_replace( '/(' . $img_regex . '.*?)height=[\'"]\d+[\'"]/i', '$1', $html );
329 return $html;
330 }
331
332 /**
333 * Wrapper for wp_mail to be overridden in unit tests
334 *
335 * @param string|array $to Array or comma-separated list of email addresses to send message.
336 * @param string $subject Email subject.
337 * @param string $message Message contents.
338 * @param array $headers Optional. Additional headers.
339 * @param array $attachments Optional. Files to attach.
340 * @param string $override_sitename Optional. Override the sitename.
341 * @return bool Whether the email contents were sent successfully.
342 */
343 public function send_mail( $to, $subject, $message, array $headers = array(), array $attachments = array(), $override_sitename = false ) {
344 $sitename = get_option( 'blogname' );
345
346 if ( $override_sitename ) {
347 $sitename = $override_sitename;
348 }
349
350 // translators: %1$s is the site name, %2$s is the subject.
351 $subject = sprintf( _x( '[%1$s] %2$s', 'email subject', 'friends' ), wp_specialchars_decode( $sitename, ENT_QUOTES ), $subject );
352 $subject = apply_filters( 'friends_send_mail_subject', $subject );
353
354 if ( ! apply_filters( 'friends_send_mail', true, $to, $subject, $message, $headers, $attachments ) ) {
355 return;
356 }
357
358 $alt_function = null;
359 if ( is_array( $message ) ) {
360 if ( isset( $message['html'] ) ) {
361 if ( isset( $message['text'] ) ) {
362 $plain_text = $message['text'];
363 } else {
364 $plain_text = strip_tags( $message['html'] );
365 }
366
367 $headers[] = 'Content-type: text/html';
368 $alt_function = function ( $mailer ) use ( $plain_text ) {
369 $mailer->{'AltBody'} = $plain_text;
370 };
371 add_action(
372 'phpmailer_init',
373 $alt_function
374 );
375
376 $message = $message['html'];
377 } elseif ( isset( $message['text'] ) ) {
378 $message = $message['text'];
379 }
380 }
381
382 add_action( 'wp_mail_from', array( $this, 'use_friends_plugin_from_email_address' ) );
383
384 $mail = wp_mail( $to, $subject, $message, $headers, $attachments );
385
386 remove_action( 'wp_mail_from', array( $this, 'use_friends_plugin_from_email_address' ) );
387
388 if ( $alt_function ) {
389 remove_action( 'phpmailer_init', $alt_function );
390 }
391
392 return $mail;
393 }
394 }
395