PluginProbe
Friends / 2.8.8
Friends v2.8.8
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.8.8, at includes/class-notifications.php

398 lines 13.2 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 add_filter( 'wp_mail_from', array( $this, 'use_friends_plugin_from_email_address' ) );
77 $address = apply_filters( 'wp_mail_from', 'wordpress@' . preg_replace( '#^www\.#', '', wp_parse_url( network_home_url(), PHP_URL_HOST ) ) );
78 remove_filter( 'wp_mail_from', array( $this, 'use_friends_plugin_from_email_address' ) );
79 return $address;
80 }
81
82 /**
83 * Notify the users of this site about a new friend post
84 *
85 * @param \WP_Post $post The new post by a friend or subscription.
86 * @param User_Feed $user_feed The feed where the post came from.
87 */
88 public function notify_new_friend_post( \WP_Post $post, User_Feed $user_feed ) {
89 if (
90 // Post might be trashed through rules.
91 'trash' === $post->post_status
92 // Don't notify about posts older than a week.
93 || strtotime( $post->post_date_gmt ) + WEEK_IN_SECONDS < time()
94 ) {
95 return;
96 }
97 $user = new User( Friends::get_main_friend_user_id() );
98 if ( ! $user->user_email ) {
99 return;
100 }
101 $author = $user_feed->get_friend_user();
102
103 $notify_user = ! get_user_option( 'friends_no_new_post_notification', $user->ID );
104 $notify_user = $notify_user && ! get_user_option( 'friends_no_new_post_notification_' . $author->user_login, $user->ID );
105
106 if ( ! apply_filters( 'notify_user_about_friend_post', $notify_user, $user, $post, $author ) ) {
107 return;
108 }
109
110 $email_title = $post->post_title;
111
112 $params = array(
113 'author' => $author,
114 'post' => $post,
115 );
116
117 $email_message = array();
118 ob_start();
119 Friends::template_loader()->get_template_part( 'email/header', null, array( 'email_title' => $email_title ) );
120 Friends::template_loader()->get_template_part( 'email/new-friend-post', null, $params );
121 Friends::template_loader()->get_template_part( 'email/footer' );
122 $email_message['html'] = ob_get_contents();
123 ob_end_clean();
124
125 ob_start();
126 Friends::template_loader()->get_template_part( 'email/new-friend-post-text', null, $params );
127 Friends::template_loader()->get_template_part( 'email/footer-text' );
128 $email_message['text'] = ob_get_contents();
129 ob_end_clean();
130
131 $this->send_mail( $user->user_email, wp_specialchars_decode( $email_title, ENT_QUOTES ), $email_message, array(), array(), $author->user_login );
132 }
133
134 /**
135 * Notifies about a post that matched the keyword.
136 *
137 * @param bool $notified Whether a notification was sent.
138 * @param \WP_Post $post The new post by a friend or subscription.
139 * @param string $keyword The matched keyword.
140 */
141 public function notify_keyword_match_post( $notified, \WP_Post $post, $keyword ) {
142 if ( 'trash' === $post->post_status ) {
143 return $notified;
144 }
145
146 $user = new User( Friends::get_main_friend_user_id() );
147 if ( ! $user->user_email ) {
148 return $notified;
149 }
150 $notify_user = ! get_user_option( 'friends_no_keyword_notification_' . $post->post_author, $user->ID );
151
152 if ( ! apply_filters( 'notify_user_about_keyword_post', $notify_user, $user, $post, $keyword ) ) {
153 return $notified;
154 }
155
156 $author = new User( $post->post_author );
157 // translators: %s is a keyword string specified by the user.
158 $email_title = sprintf( __( 'Keyword matched: %s', 'friends' ), $keyword );
159
160 $params = array(
161 'author' => $author,
162 'post' => $post,
163 'keyword' => $keyword,
164 );
165
166 $email_message = array();
167 ob_start();
168 Friends::template_loader()->get_template_part( 'email/header', null, array( 'email_title' => $email_title ) );
169 Friends::template_loader()->get_template_part( 'email/keyword-match-post', null, $params );
170 Friends::template_loader()->get_template_part( 'email/footer' );
171 $email_message['html'] = ob_get_contents();
172 ob_end_clean();
173
174 ob_start();
175 Friends::template_loader()->get_template_part( 'email/keyword-match-post-text', null, $params );
176 Friends::template_loader()->get_template_part( 'email/footer-text' );
177 $email_message['text'] = ob_get_contents();
178 ob_end_clean();
179
180 $this->send_mail( $user->user_email, wp_specialchars_decode( $email_title, ENT_QUOTES ), $email_message, array(), array(), $author->user_login );
181
182 return true;
183 }
184
185 /**
186 * Notify the users of this site about a new friend request
187 *
188 * @param User $friend_user The user requesting friendship.
189 */
190 public function notify_new_friend_request( User $friend_user ) {
191 if ( ! $friend_user->has_cap( 'friend_request' ) ) {
192 return;
193 }
194 $user = new User( Friends::get_main_friend_user_id() );
195
196 if ( ! $user->user_email ) {
197 return;
198 }
199
200 $notify_user = ! get_user_option( 'friends_no_friend_request_notification', $user->ID );
201 if ( ! apply_filters( 'notify_user_about_friend_request', $notify_user, $user, $friend_user ) ) {
202 return;
203 }
204
205 // translators: %s is a user display name.
206 $email_title = sprintf( __( '%s sent a Friend Request', 'friends' ), $friend_user->display_name );
207
208 $params = array(
209 'user' => $user,
210 'friend_user' => $friend_user,
211 );
212
213 $email_message = array();
214 ob_start();
215 Friends::template_loader()->get_template_part( 'email/header', null, array( 'email_title' => $email_title ) );
216 Friends::template_loader()->get_template_part( 'email/new-friend-request', null, $params );
217 Friends::template_loader()->get_template_part( 'email/footer' );
218 $email_message['html'] = ob_get_contents();
219 ob_end_clean();
220
221 ob_start();
222 Friends::template_loader()->get_template_part( 'email/new-friend-request-text', null, $params );
223 Friends::template_loader()->get_template_part( 'email/footer-text' );
224 $email_message['text'] = ob_get_contents();
225 ob_end_clean();
226
227 $this->send_mail( $user->user_email, $email_title, $email_message );
228 }
229
230 /**
231 * Notify the users of this site about an accepted friend request
232 *
233 * @param User $friend_user The user who accepted friendship.
234 */
235 public function notify_accepted_friend_request( User $friend_user ) {
236 $user = new User( Friends::get_main_friend_user_id() );
237 if ( ! $user->user_email ) {
238 return;
239 }
240
241 if ( ! apply_filters( 'notify_user_about_accepted_friend_request', true, $user, $friend_user ) ) {
242 return;
243 }
244
245 // translators: %s is a user display name.
246 $email_title = sprintf( __( '%s accepted your Friend Request', 'friends' ), $friend_user->display_name );
247
248 $params = array(
249 'user' => $user,
250 'friend_user' => $friend_user,
251 );
252
253 $email_message = array();
254 ob_start();
255 Friends::template_loader()->get_template_part( 'email/header', null, array( 'email_title' => $email_title ) );
256 Friends::template_loader()->get_template_part( 'email/accepted-friend-request', null, $params );
257 Friends::template_loader()->get_template_part( 'email/footer' );
258 $email_message['html'] = ob_get_contents();
259 ob_end_clean();
260
261 ob_start();
262 Friends::template_loader()->get_template_part( 'email/accepted-friend-request-text', null, $params );
263 Friends::template_loader()->get_template_part( 'email/footer-text' );
264 $email_message['text'] = ob_get_contents();
265 ob_end_clean();
266
267 $this->send_mail( $user->user_email, $email_title, $email_message );
268 }
269
270 /**
271 * Notify the users of this site about a received message
272 *
273 * @param User $friend_user The user who sent the message.
274 */
275
276 /**
277 * Notify the users of this site about a received message
278 *
279 * @param User $friend_user The user who sent the message.
280 * @param string $message The message.
281 * @param string $subject The subject.
282 */
283 public function notify_friend_message_received( User $friend_user, $message, $subject ) {
284 $user = new User( Friends::get_main_friend_user_id() );
285 if ( ! $user->user_email ) {
286 return;
287 }
288
289 if ( ! apply_filters( 'notify_user_about_friend_message', true, $user, $friend_user ) ) {
290 return;
291 }
292
293 // translators: %s is a user display name.
294 $email_title = sprintf( __( '%s sent you a message', 'friends' ), $friend_user->display_name );
295
296 $params = array(
297 'user' => $user,
298 'friend_user' => $friend_user,
299 'subject' => $subject,
300 'message' => $message,
301 );
302
303 $email_message = array();
304 ob_start();
305 Friends::template_loader()->get_template_part( 'email/header', null, array( 'email_title' => $email_title ) );
306 Friends::template_loader()->get_template_part( 'email/friend-message-received', null, $params );
307 Friends::template_loader()->get_template_part( 'email/footer' );
308 $email_message['html'] = ob_get_contents();
309 ob_end_clean();
310
311 ob_start();
312 Friends::template_loader()->get_template_part( 'email/friend-message-received-text', null, $params );
313 Friends::template_loader()->get_template_part( 'email/footer-text' );
314 $email_message['text'] = ob_get_contents();
315 ob_end_clean();
316
317 $this->send_mail( $user->user_email, $email_title, $email_message );
318 }
319
320 /**
321 * Rewrite HTML for e-mail (by inlining some CSS styles)
322 *
323 * @param string $html The HTML.
324 *
325 * @return string The rewritten HTML.
326 */
327 public function rewrite_mail_html( $html ) {
328 $img_regex = '<figure\b[^>]*>\s*(<a\b[^>]*>\s*)?<img\b';
329 $html = preg_replace( '/' . $img_regex . '/i', '$0 style="max-width: 100% !important; height: auto !important;"', $html );
330 $html = preg_replace( '/(' . $img_regex . '.*?)width=[\'"]\d+[\'"]/i', '$1', $html );
331 $html = preg_replace( '/(' . $img_regex . '.*?)height=[\'"]\d+[\'"]/i', '$1', $html );
332 return $html;
333 }
334
335 /**
336 * Wrapper for wp_mail to be overridden in unit tests
337 *
338 * @param string|array $to Array or comma-separated list of email addresses to send message.
339 * @param string $subject Email subject.
340 * @param string $message Message contents.
341 * @param array $headers Optional. Additional headers.
342 * @param array $attachments Optional. Files to attach.
343 * @param string $override_sitename Optional. Override the sitename.
344 * @return bool Whether the email contents were sent successfully.
345 */
346 public function send_mail( $to, $subject, $message, array $headers = array(), array $attachments = array(), $override_sitename = false ) {
347 $sitename = get_option( 'blogname' );
348
349 if ( $override_sitename ) {
350 $sitename = $override_sitename;
351 }
352
353 // translators: %1$s is the site name, %2$s is the subject.
354 $subject = sprintf( _x( '[%1$s] %2$s', 'email subject', 'friends' ), wp_specialchars_decode( $sitename, ENT_QUOTES ), $subject );
355 $subject = apply_filters( 'friends_send_mail_subject', $subject );
356
357 if ( ! apply_filters( 'friends_send_mail', true, $to, $subject, $message, $headers, $attachments ) ) {
358 return;
359 }
360
361 $alt_function = null;
362 if ( is_array( $message ) ) {
363 if ( isset( $message['html'] ) ) {
364 if ( isset( $message['text'] ) ) {
365 $plain_text = $message['text'];
366 } else {
367 $plain_text = strip_tags( $message['html'] );
368 }
369
370 $headers[] = 'Content-type: text/html';
371 $alt_function = function ( $mailer ) use ( $plain_text ) {
372 $mailer->{'AltBody'} = $plain_text;
373 };
374 add_action(
375 'phpmailer_init',
376 $alt_function
377 );
378
379 $message = $message['html'];
380 } elseif ( isset( $message['text'] ) ) {
381 $message = $message['text'];
382 }
383 }
384
385 add_filter( 'wp_mail_from', array( $this, 'use_friends_plugin_from_email_address' ) );
386
387 $mail = wp_mail( $to, $subject, $message, $headers, $attachments );
388
389 remove_filter( 'wp_mail_from', array( $this, 'use_friends_plugin_from_email_address' ) );
390
391 if ( $alt_function ) {
392 remove_action( 'phpmailer_init', $alt_function );
393 }
394
395 return $mail;
396 }
397 }
398