| 1 |
<?php |
| 2 |
/** |
| 3 |
* Mailer Class. |
| 4 |
* |
| 5 |
* @package ActivityPub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub; |
| 9 |
|
| 10 |
use Activitypub\Collection\Actors; |
| 11 |
|
| 12 |
/** |
| 13 |
* Mailer Class. |
| 14 |
*/ |
| 15 |
class Mailer { |
| 16 |
/** |
| 17 |
* Initialize the Mailer. |
| 18 |
*/ |
| 19 |
public static function init() { |
| 20 |
\add_filter( 'comment_notification_subject', array( self::class, 'comment_notification_subject' ), 10, 2 ); |
| 21 |
\add_filter( 'comment_notification_text', array( self::class, 'comment_notification_text' ), 10, 2 ); |
| 22 |
|
| 23 |
// New follower notification. |
| 24 |
if ( '1' === \get_option( 'activitypub_mailer_new_follower', '0' ) ) { |
| 25 |
\add_action( 'activitypub_notification_follow', array( self::class, 'new_follower' ) ); |
| 26 |
} |
| 27 |
|
| 28 |
// Direct message notification. |
| 29 |
if ( '1' === \get_option( 'activitypub_mailer_new_dm', '0' ) ) { |
| 30 |
\add_action( 'activitypub_inbox_create', array( self::class, 'direct_message' ), 10, 2 ); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Filter the subject line for Like and Announce notifications. |
| 36 |
* |
| 37 |
* @param string $subject The default subject line. |
| 38 |
* @param int|string $comment_id The comment ID. |
| 39 |
* |
| 40 |
* @return string The filtered subject line. |
| 41 |
*/ |
| 42 |
public static function comment_notification_subject( $subject, $comment_id ) { |
| 43 |
$comment = \get_comment( $comment_id ); |
| 44 |
|
| 45 |
if ( ! $comment ) { |
| 46 |
return $subject; |
| 47 |
} |
| 48 |
|
| 49 |
$type = \get_comment_meta( $comment->comment_ID, 'protocol', true ); |
| 50 |
|
| 51 |
if ( 'activitypub' !== $type ) { |
| 52 |
return $subject; |
| 53 |
} |
| 54 |
|
| 55 |
$singular = Comment::get_comment_type_attr( $comment->comment_type, 'singular' ); |
| 56 |
|
| 57 |
if ( ! $singular ) { |
| 58 |
return $subject; |
| 59 |
} |
| 60 |
|
| 61 |
$post = \get_post( $comment->comment_post_ID ); |
| 62 |
|
| 63 |
/* translators: 1: Blog name, 2: Like or Repost, 3: Post title */ |
| 64 |
return \sprintf( \esc_html__( '[%1$s] %2$s: %3$s', 'activitypub' ), \esc_html( get_option( 'blogname' ) ), \esc_html( $singular ), \esc_html( $post->post_title ) ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Filter the notification text for Like and Announce notifications. |
| 69 |
* |
| 70 |
* @param string $message The default notification text. |
| 71 |
* @param int|string $comment_id The comment ID. |
| 72 |
* |
| 73 |
* @return string The filtered notification text. |
| 74 |
*/ |
| 75 |
public static function comment_notification_text( $message, $comment_id ) { |
| 76 |
$comment = \get_comment( $comment_id ); |
| 77 |
|
| 78 |
if ( ! $comment ) { |
| 79 |
return $message; |
| 80 |
} |
| 81 |
|
| 82 |
$type = \get_comment_meta( $comment->comment_ID, 'protocol', true ); |
| 83 |
|
| 84 |
if ( 'activitypub' !== $type ) { |
| 85 |
return $message; |
| 86 |
} |
| 87 |
|
| 88 |
$comment_type = Comment::get_comment_type( $comment->comment_type ); |
| 89 |
|
| 90 |
if ( ! $comment_type ) { |
| 91 |
return $message; |
| 92 |
} |
| 93 |
|
| 94 |
$post = \get_post( $comment->comment_post_ID ); |
| 95 |
$comment_author_domain = \gethostbyaddr( $comment->comment_author_IP ); |
| 96 |
|
| 97 |
/* translators: 1: Comment type, 2: Post title */ |
| 98 |
$notify_message = \sprintf( html_entity_decode( esc_html__( 'New %1$s on your post “%2$s”.', 'activitypub' ) ), \esc_html( $comment_type['singular'] ), \esc_html( $post->post_title ) ) . "\r\n\r\n"; |
| 99 |
/* translators: 1: Website name, 2: Website IP address, 3: Website hostname. */ |
| 100 |
$notify_message .= \sprintf( \esc_html__( 'From: %1$s (IP address: %2$s, %3$s)', 'activitypub' ), \esc_html( $comment->comment_author ), \esc_html( $comment->comment_author_IP ), \esc_html( $comment_author_domain ) ) . "\r\n"; |
| 101 |
/* translators: Reaction author URL. */ |
| 102 |
$notify_message .= \sprintf( \esc_html__( 'URL: %s', 'activitypub' ), \esc_url( $comment->comment_author_url ) ) . "\r\n\r\n"; |
| 103 |
/* translators: Comment type label */ |
| 104 |
$notify_message .= \sprintf( \esc_html__( 'You can see all %s on this post here:', 'activitypub' ), \esc_html( $comment_type['label'] ) ) . "\r\n"; |
| 105 |
$notify_message .= \get_permalink( $comment->comment_post_ID ) . '#' . \esc_attr( $comment_type['type'] ) . "\r\n\r\n"; |
| 106 |
|
| 107 |
return $notify_message; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Send a notification email for every new follower. |
| 112 |
* |
| 113 |
* @param Notification $notification The notification object. |
| 114 |
*/ |
| 115 |
public static function new_follower( $notification ) { |
| 116 |
$actor = get_remote_metadata_by_actor( $notification->actor ); |
| 117 |
|
| 118 |
if ( ! $actor || \is_wp_error( $actor ) ) { |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
$email = \get_option( 'admin_email' ); |
| 123 |
$admin_url = '/options-general.php?page=activitypub&tab=followers'; |
| 124 |
|
| 125 |
if ( $notification->target > Actors::BLOG_USER_ID ) { |
| 126 |
$user = \get_user_by( 'id', $notification->target ); |
| 127 |
|
| 128 |
if ( ! $user ) { |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
$email = $user->user_email; |
| 133 |
$admin_url = '/users.php?page=activitypub-followers-list'; |
| 134 |
} |
| 135 |
|
| 136 |
$template_args = array_merge( |
| 137 |
$actor, |
| 138 |
array( |
| 139 |
'admin_url' => $admin_url, |
| 140 |
'target' => $notification->target, |
| 141 |
) |
| 142 |
); |
| 143 |
|
| 144 |
/* translators: 1: Blog name, 2: Follower name */ |
| 145 |
$subject = \sprintf( \__( '[%1$s] New Follower: %2$s', 'activitypub' ), get_option( 'blogname' ), $actor['name'] ); |
| 146 |
|
| 147 |
\ob_start(); |
| 148 |
\load_template( ACTIVITYPUB_PLUGIN_DIR . 'templates/new-follower-email.php', false, $template_args ); |
| 149 |
$html_message = \ob_get_clean(); |
| 150 |
|
| 151 |
$alt_function = function ( $mailer ) use ( $actor, $admin_url ) { |
| 152 |
/* translators: 1: Follower name */ |
| 153 |
$message = \sprintf( \__( 'New Follower: %1$s.', 'activitypub' ), $actor['name'] ) . "\r\n\r\n"; |
| 154 |
/* translators: Follower URL */ |
| 155 |
$message .= \sprintf( \__( 'URL: %s', 'activitypub' ), \esc_url( $actor['url'] ) ) . "\r\n\r\n"; |
| 156 |
$message .= \__( 'You can see all followers here:', 'activitypub' ) . "\r\n"; |
| 157 |
$message .= \esc_url( \admin_url( $admin_url ) ) . "\r\n\r\n"; |
| 158 |
$mailer->{'AltBody'} = $message; |
| 159 |
}; |
| 160 |
\add_action( 'phpmailer_init', $alt_function ); |
| 161 |
|
| 162 |
\wp_mail( $email, $subject, $html_message, array( 'Content-type: text/html' ) ); |
| 163 |
|
| 164 |
\remove_action( 'phpmailer_init', $alt_function ); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Send a direct message. |
| 169 |
* |
| 170 |
* @param array $activity The activity object. |
| 171 |
* @param int $user_id The id of the local blog-user. |
| 172 |
*/ |
| 173 |
public static function direct_message( $activity, $user_id ) { |
| 174 |
if ( |
| 175 |
is_activity_public( $activity ) || |
| 176 |
// Only accept messages that have the user in the "to" field. |
| 177 |
empty( $activity['to'] ) || |
| 178 |
! in_array( Actors::get_by_id( $user_id )->get_id(), (array) $activity['to'], true ) |
| 179 |
) { |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
$actor = get_remote_metadata_by_actor( $activity['actor'] ); |
| 184 |
|
| 185 |
if ( ! $actor || \is_wp_error( $actor ) || empty( $activity['object']['content'] ) ) { |
| 186 |
return; |
| 187 |
} |
| 188 |
|
| 189 |
$email = \get_option( 'admin_email' ); |
| 190 |
|
| 191 |
if ( (int) $user_id > Actors::BLOG_USER_ID ) { |
| 192 |
$user = \get_user_by( 'id', $user_id ); |
| 193 |
|
| 194 |
if ( ! $user ) { |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
$email = $user->user_email; |
| 199 |
} |
| 200 |
|
| 201 |
$content = \html_entity_decode( |
| 202 |
\wp_strip_all_tags( |
| 203 |
str_replace( '</p>', PHP_EOL . PHP_EOL, $activity['object']['content'] ) |
| 204 |
), |
| 205 |
ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 |
| 206 |
); |
| 207 |
|
| 208 |
/* translators: 1: Blog name, 2 Actor name */ |
| 209 |
$subject = \sprintf( \esc_html__( '[%1$s] Direct Message from: %2$s', 'activitypub' ), \esc_html( get_option( 'blogname' ) ), \esc_html( $actor['name'] ) ); |
| 210 |
/* translators: 1: Blog name, 2: Actor name */ |
| 211 |
$message = \sprintf( \esc_html__( 'New Direct Message: %2$s', 'activitypub' ), \esc_html( get_option( 'blogname' ) ), $content ) . "\r\n\r\n"; |
| 212 |
/* translators: Actor name */ |
| 213 |
$message .= \sprintf( \esc_html__( 'From: %s', 'activitypub' ), \esc_html( $actor['name'] ) ) . "\r\n"; |
| 214 |
/* translators: Actor URL */ |
| 215 |
$message .= \sprintf( \esc_html__( 'URL: %s', 'activitypub' ), \esc_url( $actor['url'] ) ) . "\r\n\r\n"; |
| 216 |
|
| 217 |
\wp_mail( $email, $subject, $message ); |
| 218 |
} |
| 219 |
} |
| 220 |
|