Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init new custom mailer class #1068

Merged
merged 15 commits into from
Dec 11, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

* Send "new follower" emails
* Send "direct message" emails

### Improved

Expand Down
1 change: 1 addition & 0 deletions activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ function plugin_init() {
\add_action( 'init', array( __NAMESPACE__ . '\Scheduler', 'init' ) );
\add_action( 'init', array( __NAMESPACE__ . '\Comment', 'init' ) );
\add_action( 'init', array( __NAMESPACE__ . '\Link', 'init' ) );
\add_action( 'init', array( __NAMESPACE__ . '\Mailer', 'init' ) );

if ( site_supports_blocks() ) {
\add_action( 'init', array( __NAMESPACE__ . '\Blocks', 'init' ) );
Expand Down
88 changes: 68 additions & 20 deletions includes/class-mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ class Mailer {
* Initialize the Mailer.
*/
public static function init() {
add_filter( 'comment_notification_subject', array( self::class, 'comment_notification_subject' ), 10, 2 );
add_filter( 'comment_notification_text', array( self::class, 'comment_notification_text' ), 10, 2 );
\add_filter( 'comment_notification_subject', array( self::class, 'comment_notification_subject' ), 10, 2 );
\add_filter( 'comment_notification_text', array( self::class, 'comment_notification_text' ), 10, 2 );

// New follower notification.
add_action( 'activitypub_notification_follow', array( self::class, 'new_follower' ) );
\add_action( 'activitypub_notification_follow', array( self::class, 'new_follower' ) );

// Direct message notification.
\add_action( 'activitypub_inbox_create', array( self::class, 'direct_message' ), 10, 2 );
}

/**
Expand All @@ -32,13 +35,13 @@ public static function init() {
* @return string The filtered mail-subject
*/
public static function comment_notification_subject( $subject, $comment_id ) {
$comment = get_comment( $comment_id );
$comment = \get_comment( $comment_id );

if ( ! $comment ) {
return $subject;
}

$type = get_comment_meta( $comment->comment_ID, 'protocol', true );
$type = \get_comment_meta( $comment->comment_ID, 'protocol', true );

if ( 'activitypub' !== $type ) {
return $subject;
Expand All @@ -50,10 +53,10 @@ public static function comment_notification_subject( $subject, $comment_id ) {
return $subject;
}

$post = get_post( $comment->comment_post_ID );
$post = \get_post( $comment->comment_post_ID );

/* translators: %1$s: Blog name, %2$s: Post title */
pfefferle marked this conversation as resolved.
Show resolved Hide resolved
return sprintf( __( '[%1$s] %2$s: %3$s', 'activitypub' ), get_option( 'blogname' ), $singular, $post->post_title );
return \sprintf( \__( '[%1$s] %2$s: %3$s', 'activitypub' ), get_option( 'blogname' ), $singular, $post->post_title );
}

/**
Expand All @@ -65,13 +68,13 @@ public static function comment_notification_subject( $subject, $comment_id ) {
* @return string The filtered mail-content
*/
public static function comment_notification_text( $message, $comment_id ) {
$comment = get_comment( $comment_id );
$comment = \get_comment( $comment_id );

if ( ! $comment ) {
return $message;
}

$type = get_comment_meta( $comment->comment_ID, 'protocol', true );
$type = \get_comment_meta( $comment->comment_ID, 'protocol', true );

if ( 'activitypub' !== $type ) {
return $message;
Expand All @@ -83,18 +86,18 @@ public static function comment_notification_text( $message, $comment_id ) {
return $message;
}

$post = get_post( $comment->comment_post_ID );
$comment_author_domain = gethostbyaddr( $comment->comment_author_IP );
$post = \get_post( $comment->comment_post_ID );
$comment_author_domain = \gethostbyaddr( $comment->comment_author_IP );

/* translators: %1$s: Comment type, %2$s: Post title */
$notify_message = \sprintf( __( 'New %1$s on your post "%2$s"', 'activitypub' ), $comment_type['singular'], $post->post_title ) . "\r\n";
/* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */
$notify_message .= \sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)', 'activitypub' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
/* translators: %s: Trackback/pingback/comment author URL. */
/* translators: 1: Comment type, 2: Post title */
$notify_message = \sprintf( __( 'New %1$s on your post "%2$s"', 'activitypub' ), $comment_type['singular'], $post->post_title ) . "\r\n\r\n";
/* translators: 1: Website name, 2: Website IP address, 3: Website hostname. */
$notify_message .= \sprintf( __( 'From: %1$s (IP address: %2$s, %3$s)', 'activitypub' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
/* translators: %s: Reaction author URL. */
$notify_message .= \sprintf( __( 'URL: %s', 'activitypub' ), $comment->comment_author_url ) . "\r\n\r\n";
/* translators: %s: Comment type label */
$notify_message .= \sprintf( __( 'You can see all %s on this post here:', 'activitypub' ), $comment_type['label'] ) . "\r\n";
$notify_message .= \get_permalink( $comment->comment_post_ID ) . '#' . $comment_type['singular'] . "\r\n\r\n";
$notify_message .= \get_permalink( $comment->comment_post_ID ) . '#' . $comment_type['type'] . "\r\n\r\n";

return $notify_message;
}
Expand Down Expand Up @@ -123,15 +126,60 @@ public static function new_follower( $notification ) {
$email = $user->user_email;
}

/* translators: %1$s: Blog name, %2$s: Follower name */
/* translators: 1: Blog name, 2: Follower name */
$subject = \sprintf( \__( '[%1$s] Follower: %2$s', 'activitypub' ), get_option( 'blogname' ), $actor['name'] );
/* translators: %1$s: Blog name, %2$s: Follower name */
$message = \sprintf( \__( 'New follower: %2$s', 'activitypub' ), get_option( 'blogname' ), $actor['name'] ) . "\r\n";
/* translators: 1: Blog name, 2: Follower name */
$message = \sprintf( \__( 'New Follower: %2$s', 'activitypub' ), get_option( 'blogname' ), $actor['name'] ) . "\r\n\r\n";
/* translators: %s: Follower URL */
$message .= \sprintf( \__( 'URL: %s', 'activitypub' ), $actor['url'] ) . "\r\n\r\n";
pfefferle marked this conversation as resolved.
Show resolved Hide resolved
$message .= \sprintf( \__( 'You can see all followers here:', 'activitypub' ) ) . "\r\n";
$message .= \esc_url( \admin_url( '/users.php?page=activitypub-followers-list' ) ) . "\r\n\r\n";

\wp_mail( $email, $subject, $message );
}

/**
* Send a direct message.
*
* @param array $activity The activity-object.
* @param int $user_id The id of the local blog-user.
*/
public static function direct_message( $activity, $user_id ) {
// Check if Activity is public or not.
if (
is_activity_public( $activity ) &&
is_activity_reply( $activity )
) {
return;
}

$actor = get_remote_metadata_by_actor( $activity['actor'] );

if ( ! $actor || \is_wp_error( $actor ) || empty( $activity['object']['content'] ) ) {
return;
}

$email = \get_option( 'admin_email' );

if ( (int) $user_id > Actors::BLOG_USER_ID ) {
$user = \get_user_by( 'id', $user_id );

if ( ! $user ) {
return;
}

$email = $user->user_email;
}

/* translators: %1$s: Blog name, %2$s: Actor name */
$subject = \sprintf( \__( '[%1$s] Direct-Message from: %2$s', 'activitypub' ), get_option( 'blogname' ), $actor['name'] );
/* translators: %1$s: Blog name, %2$s: Actor name */
$message = \sprintf( \__( 'New Direct-Message: %2$s', 'activitypub' ), get_option( 'blogname' ), \wp_strip_all_tags( $activity['object']['content'], 'allowed_comment_html' ) ) . "\r\n\r\n";
pfefferle marked this conversation as resolved.
Show resolved Hide resolved
/* translators: %s: Actor name */
$message .= \sprintf( __( 'From: %s', 'activitypub' ), $actor['name'] ) . "\r\n";
/* translators: %s: Actor URL */
$message .= \sprintf( \__( 'URL: %s', 'activitypub' ), $actor['url'] ) . "\r\n\r\n";
pfefferle marked this conversation as resolved.
Show resolved Hide resolved

\wp_mail( $email, $subject, $message );
}
}
11 changes: 11 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,17 @@ function is_activity_public( $data ) {
return in_array( 'https://www.w3.org/ns/activitystreams#Public', $recipients, true );
}

/**
* Check if passed Activity is a reply.
*
* @param array $data The Activity object as array.
*
* @return boolean True if a reply, false if not.
*/
function is_activity_reply( $data ) {
return ! empty( $data['object']['inReplyTo'] );
}

/**
* Get active users based on a given duration.
*
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ For reasons of data protection, it is not possible to see the followers of other
= Unreleased =

* Added: Send "new follower" emails
* Added: Send "direct message" emails
* Improved: Email templates for Likes and Reposts
* Improved: Interactions moderation
* Improved: Compatibility with Akismet
Expand Down
Loading