friends = $friends; $this->register_hooks(); } /** * Register the WordPress hooks */ private function register_hooks() { add_action( 'friends_rewrite_mail_html', array( $this, 'rewrite_mail_html' ) ); add_action( 'notify_new_friend_post', array( $this, 'notify_new_friend_post' ), 10, 2 ); add_filter( 'notify_keyword_match_post', array( $this, 'notify_keyword_match_post' ), 10, 3 ); add_action( 'notify_new_friend_request', array( $this, 'notify_new_friend_request' ) ); add_action( 'notify_accepted_friend_request', array( $this, 'notify_accepted_friend_request' ) ); add_action( 'notify_friend_message_received', array( $this, 'notify_friend_message_received' ), 10, 3 ); } /** * Sets the friends plugin from email address when sending mail. * * If you're having issues with the receiver using the envelope From: address in favor of the mail header, * you can use a filter like this: * add_filter( 'wp_mail_from', function( $from ) { * ini_set( 'sendmail_from', $from ); * return $from; * }, 100 ); * * @param string $from The from that's supposed to be used. * * @return string The friends plugin from email address. */ public function use_friends_plugin_from_email_address( $from ) { $from = preg_replace( '/^wordpress@/', 'friends-plugin@', $from ); return $from; } /** * Gets the friends plugin from email address. * * @return string The friends plugin from email address. */ public function get_friends_plugin_from_email_address() { add_filter( 'wp_mail_from', array( $this, 'use_friends_plugin_from_email_address' ) ); $address = apply_filters( 'wp_mail_from', 'wordpress@' . preg_replace( '#^www\.#', '', wp_parse_url( network_home_url(), PHP_URL_HOST ) ) ); remove_filter( 'wp_mail_from', array( $this, 'use_friends_plugin_from_email_address' ) ); return $address; } /** * Notify the users of this site about a new friend post * * @param \WP_Post $post The new post by a friend or subscription. * @param User_Feed $user_feed The feed where the post came from. */ public function notify_new_friend_post( \WP_Post $post, User_Feed $user_feed ) { if ( // Post might be trashed through rules. 'trash' === $post->post_status // Don't notify about posts older than a week. || strtotime( $post->post_date_gmt ) + WEEK_IN_SECONDS < time() ) { return; } $user = new User( Friends::get_main_friend_user_id() ); if ( ! $user->user_email ) { return; } $author = $user_feed->get_friend_user(); $notify_user = ! get_user_option( 'friends_no_new_post_notification', $user->ID ); $notify_user = $notify_user && ! get_user_option( 'friends_no_new_post_notification_' . $author->user_login, $user->ID ); if ( ! apply_filters( 'notify_user_about_friend_post', $notify_user, $user, $post, $author ) ) { return; } $email_title = $post->post_title; $params = array( 'author' => $author, 'post' => $post, ); $email_message = array(); ob_start(); Friends::template_loader()->get_template_part( 'email/header', null, array( 'email_title' => $email_title ) ); Friends::template_loader()->get_template_part( 'email/new-friend-post', null, $params ); Friends::template_loader()->get_template_part( 'email/footer' ); $email_message['html'] = ob_get_contents(); ob_end_clean(); ob_start(); Friends::template_loader()->get_template_part( 'email/new-friend-post-text', null, $params ); Friends::template_loader()->get_template_part( 'email/footer-text' ); $email_message['text'] = ob_get_contents(); ob_end_clean(); $this->send_mail( $user->user_email, wp_specialchars_decode( $email_title, ENT_QUOTES ), $email_message, array(), array(), $author->user_login ); } /** * Notifies about a post that matched the keyword. * * @param bool $notified Whether a notification was sent. * @param \WP_Post $post The new post by a friend or subscription. * @param string $keyword The matched keyword. */ public function notify_keyword_match_post( $notified, \WP_Post $post, $keyword ) { if ( 'trash' === $post->post_status ) { return $notified; } $user = new User( Friends::get_main_friend_user_id() ); if ( ! $user->user_email ) { return $notified; } $notify_user = ! get_user_option( 'friends_no_keyword_notification_' . $post->post_author, $user->ID ); if ( ! apply_filters( 'notify_user_about_keyword_post', $notify_user, $user, $post, $keyword ) ) { return $notified; } $author = new User( $post->post_author ); // translators: %s is a keyword string specified by the user. $email_title = sprintf( __( 'Keyword matched: %s', 'friends' ), $keyword ); $params = array( 'author' => $author, 'post' => $post, 'keyword' => $keyword, ); $email_message = array(); ob_start(); Friends::template_loader()->get_template_part( 'email/header', null, array( 'email_title' => $email_title ) ); Friends::template_loader()->get_template_part( 'email/keyword-match-post', null, $params ); Friends::template_loader()->get_template_part( 'email/footer' ); $email_message['html'] = ob_get_contents(); ob_end_clean(); ob_start(); Friends::template_loader()->get_template_part( 'email/keyword-match-post-text', null, $params ); Friends::template_loader()->get_template_part( 'email/footer-text' ); $email_message['text'] = ob_get_contents(); ob_end_clean(); $this->send_mail( $user->user_email, wp_specialchars_decode( $email_title, ENT_QUOTES ), $email_message, array(), array(), $author->user_login ); return true; } /** * Notify the users of this site about a new friend request * * @param User $friend_user The user requesting friendship. */ public function notify_new_friend_request( User $friend_user ) { if ( ! $friend_user->has_cap( 'friend_request' ) ) { return; } $user = new User( Friends::get_main_friend_user_id() ); if ( ! $user->user_email ) { return; } $notify_user = ! get_user_option( 'friends_no_friend_request_notification', $user->ID ); if ( ! apply_filters( 'notify_user_about_friend_request', $notify_user, $user, $friend_user ) ) { return; } // translators: %s is a user display name. $email_title = sprintf( __( '%s sent a Friend Request', 'friends' ), $friend_user->display_name ); $params = array( 'user' => $user, 'friend_user' => $friend_user, ); $email_message = array(); ob_start(); Friends::template_loader()->get_template_part( 'email/header', null, array( 'email_title' => $email_title ) ); Friends::template_loader()->get_template_part( 'email/new-friend-request', null, $params ); Friends::template_loader()->get_template_part( 'email/footer' ); $email_message['html'] = ob_get_contents(); ob_end_clean(); ob_start(); Friends::template_loader()->get_template_part( 'email/new-friend-request-text', null, $params ); Friends::template_loader()->get_template_part( 'email/footer-text' ); $email_message['text'] = ob_get_contents(); ob_end_clean(); $this->send_mail( $user->user_email, $email_title, $email_message ); } /** * Notify the users of this site about an accepted friend request * * @param User $friend_user The user who accepted friendship. */ public function notify_accepted_friend_request( User $friend_user ) { $user = new User( Friends::get_main_friend_user_id() ); if ( ! $user->user_email ) { return; } if ( ! apply_filters( 'notify_user_about_accepted_friend_request', true, $user, $friend_user ) ) { return; } // translators: %s is a user display name. $email_title = sprintf( __( '%s accepted your Friend Request', 'friends' ), $friend_user->display_name ); $params = array( 'user' => $user, 'friend_user' => $friend_user, ); $email_message = array(); ob_start(); Friends::template_loader()->get_template_part( 'email/header', null, array( 'email_title' => $email_title ) ); Friends::template_loader()->get_template_part( 'email/accepted-friend-request', null, $params ); Friends::template_loader()->get_template_part( 'email/footer' ); $email_message['html'] = ob_get_contents(); ob_end_clean(); ob_start(); Friends::template_loader()->get_template_part( 'email/accepted-friend-request-text', null, $params ); Friends::template_loader()->get_template_part( 'email/footer-text' ); $email_message['text'] = ob_get_contents(); ob_end_clean(); $this->send_mail( $user->user_email, $email_title, $email_message ); } /** * Notify the users of this site about a received message * * @param User $friend_user The user who sent the message. */ /** * Notify the users of this site about a received message * * @param User $friend_user The user who sent the message. * @param string $message The message. * @param string $subject The subject. */ public function notify_friend_message_received( User $friend_user, $message, $subject ) { $user = new User( Friends::get_main_friend_user_id() ); if ( ! $user->user_email ) { return; } if ( ! apply_filters( 'notify_user_about_friend_message', true, $user, $friend_user ) ) { return; } // translators: %s is a user display name. $email_title = sprintf( __( '%s sent you a message', 'friends' ), $friend_user->display_name ); $params = array( 'user' => $user, 'friend_user' => $friend_user, 'subject' => $subject, 'message' => $message, ); $email_message = array(); ob_start(); Friends::template_loader()->get_template_part( 'email/header', null, array( 'email_title' => $email_title ) ); Friends::template_loader()->get_template_part( 'email/friend-message-received', null, $params ); Friends::template_loader()->get_template_part( 'email/footer' ); $email_message['html'] = ob_get_contents(); ob_end_clean(); ob_start(); Friends::template_loader()->get_template_part( 'email/friend-message-received-text', null, $params ); Friends::template_loader()->get_template_part( 'email/footer-text' ); $email_message['text'] = ob_get_contents(); ob_end_clean(); $this->send_mail( $user->user_email, $email_title, $email_message ); } /** * Rewrite HTML for e-mail (by inlining some CSS styles) * * @param string $html The HTML. * * @return string The rewritten HTML. */ public function rewrite_mail_html( $html ) { $img_regex = ']*>\s*(]*>\s*)?{'AltBody'} = $plain_text; }; add_action( 'phpmailer_init', $alt_function ); $message = $message['html']; } elseif ( isset( $message['text'] ) ) { $message = $message['text']; } } add_filter( 'wp_mail_from', array( $this, 'use_friends_plugin_from_email_address' ) ); $mail = wp_mail( $to, $subject, $message, $headers, $attachments ); remove_filter( 'wp_mail_from', array( $this, 'use_friends_plugin_from_email_address' ) ); if ( $alt_function ) { remove_action( 'phpmailer_init', $alt_function ); } return $mail; } }