Skip to content

Commit dad75d3

Browse files
authored
Change E-Mail templates to match new comment-types (#1066)
* Change E-Mail templates to match new comment-types * phpcs fixes and changelog * phpcs fixes * consistency * add blog-user support * small tweaks * fix phpcs * fix issue with sub-types * fix alignments!
1 parent 4483c30 commit dad75d3

File tree

5 files changed

+372
-5
lines changed

5 files changed

+372
-5
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
* Send "new follower" emails
13+
1014
### Improved
1115

16+
* Email templates for Likes and Reposts
1217
* Improve Interactions moderation
1318
* Compatibility with Akismet
1419

includes/class-comment.php

+16-5
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,12 @@ public static function get_comment_type_names() {
539539
}
540540

541541
/**
542-
* Get a comment type.
542+
* Get the custom comment type.
543+
*
544+
* Check if the type is registered, if not, check if it is a custom type.
545+
*
546+
* It looks for the array key in the registered types and returns the array.
547+
* If it is not found, it looks for the type in the custom types and returns the array.
543548
*
544549
* @param string $type The comment type.
545550
*
@@ -550,10 +555,18 @@ public static function get_comment_type( $type ) {
550555
$type = sanitize_key( $type );
551556
$types = self::get_comment_types();
552557

558+
$type_array = array();
559+
560+
// Check array keys.
553561
if ( in_array( $type, array_keys( $types ), true ) ) {
554562
$type_array = $types[ $type ];
555-
} else {
556-
$type_array = array();
563+
} else { // Fall back to type attribute.
564+
foreach ( $types as $item ) {
565+
if ( $type === $item['type'] ) {
566+
$type_array = $item;
567+
break;
568+
}
569+
}
557570
}
558571

559572
/**
@@ -603,7 +616,6 @@ public static function register_comment_types() {
603616
'icon' => '♻️',
604617
'class' => 'p-repost',
605618
'type' => 'repost',
606-
// translators: %1$s username, %2$s object format (post, audio, ...), %3$s URL, %4$s domain.
607619
'excerpt' => __( '… reposted this!', 'activitypub' ),
608620
)
609621
);
@@ -617,7 +629,6 @@ public static function register_comment_types() {
617629
'icon' => '👍',
618630
'class' => 'p-like',
619631
'type' => 'like',
620-
// translators: %1$s username, %2$s object format (post, audio, ...), %3$s URL, %4$s domain.
621632
'excerpt' => __( '… liked this!', 'activitypub' ),
622633
)
623634
);

includes/class-mailer.php

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
/**
3+
* Mailer Class.
4+
*
5+
* @package ActivityPub
6+
*/
7+
8+
namespace Activitypub;
9+
10+
use Activitypub\Collection\Actors;
11+
/**
12+
* Mailer Class
13+
*/
14+
class Mailer {
15+
/**
16+
* Initialize the Mailer.
17+
*/
18+
public static function init() {
19+
add_filter( 'comment_notification_subject', array( self::class, 'comment_notification_subject' ), 10, 2 );
20+
add_filter( 'comment_notification_text', array( self::class, 'comment_notification_text' ), 10, 2 );
21+
22+
// New follower notification.
23+
add_action( 'activitypub_notification_follow', array( self::class, 'new_follower' ) );
24+
}
25+
26+
/**
27+
* Filter the mail-subject for Like and Announce notifications.
28+
*
29+
* @param string $subject The default mail-subject.
30+
* @param int|string $comment_id The comment ID.
31+
*
32+
* @return string The filtered mail-subject
33+
*/
34+
public static function comment_notification_subject( $subject, $comment_id ) {
35+
$comment = get_comment( $comment_id );
36+
37+
if ( ! $comment ) {
38+
return $subject;
39+
}
40+
41+
$type = get_comment_meta( $comment->comment_ID, 'protocol', true );
42+
43+
if ( 'activitypub' !== $type ) {
44+
return $subject;
45+
}
46+
47+
$singular = Comment::get_comment_type_attr( $comment->comment_type, 'singular' );
48+
49+
if ( ! $singular ) {
50+
return $subject;
51+
}
52+
53+
$post = get_post( $comment->comment_post_ID );
54+
55+
/* translators: %1$s: Blog name, %2$s: Post title */
56+
return sprintf( __( '[%1$s] %2$s: %3$s', 'activitypub' ), get_option( 'blogname' ), $singular, $post->post_title );
57+
}
58+
59+
/**
60+
* Filter the mail-content for Like and Announce notifications.
61+
*
62+
* @param string $message The default mail-content.
63+
* @param int|string $comment_id The comment ID.
64+
*
65+
* @return string The filtered mail-content
66+
*/
67+
public static function comment_notification_text( $message, $comment_id ) {
68+
$comment = get_comment( $comment_id );
69+
70+
if ( ! $comment ) {
71+
return $message;
72+
}
73+
74+
$type = get_comment_meta( $comment->comment_ID, 'protocol', true );
75+
76+
if ( 'activitypub' !== $type ) {
77+
return $message;
78+
}
79+
80+
$comment_type = Comment::get_comment_type( $comment->comment_type );
81+
82+
if ( ! $comment_type ) {
83+
return $message;
84+
}
85+
86+
$post = get_post( $comment->comment_post_ID );
87+
$comment_author_domain = gethostbyaddr( $comment->comment_author_IP );
88+
89+
/* translators: %1$s: Comment type, %2$s: Post title */
90+
$notify_message = \sprintf( __( 'New %1$s on your post "%2$s"', 'activitypub' ), $comment_type['singular'], $post->post_title ) . "\r\n";
91+
/* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */
92+
$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";
93+
/* translators: %s: Trackback/pingback/comment author URL. */
94+
$notify_message .= \sprintf( __( 'URL: %s', 'activitypub' ), $comment->comment_author_url ) . "\r\n\r\n";
95+
/* translators: %s: Comment type label */
96+
$notify_message .= \sprintf( __( 'You can see all %s on this post here:', 'activitypub' ), $comment_type['label'] ) . "\r\n";
97+
$notify_message .= \get_permalink( $comment->comment_post_ID ) . '#' . $comment_type['singular'] . "\r\n\r\n";
98+
99+
return $notify_message;
100+
}
101+
102+
/**
103+
* Send a Mail for every new follower.
104+
*
105+
* @param Notification $notification The notification object.
106+
*/
107+
public static function new_follower( $notification ) {
108+
$actor = get_remote_metadata_by_actor( $notification->actor );
109+
110+
if ( ! $actor || \is_wp_error( $actor ) ) {
111+
return;
112+
}
113+
114+
$email = \get_option( 'admin_email' );
115+
116+
if ( (int) $notification->target > Actors::BLOG_USER_ID ) {
117+
$user = \get_user_by( 'id', $notification->target );
118+
119+
if ( ! $user ) {
120+
return;
121+
}
122+
123+
$email = $user->user_email;
124+
}
125+
126+
/* translators: %1$s: Blog name, %2$s: Follower name */
127+
$subject = \sprintf( \__( '[%1$s] Follower: %2$s', 'activitypub' ), get_option( 'blogname' ), $actor['name'] );
128+
/* translators: %1$s: Blog name, %2$s: Follower name */
129+
$message = \sprintf( \__( 'New follower: %2$s', 'activitypub' ), get_option( 'blogname' ), $actor['name'] ) . "\r\n";
130+
/* translators: %s: Follower URL */
131+
$message .= \sprintf( \__( 'URL: %s', 'activitypub' ), $actor['url'] ) . "\r\n\r\n";
132+
$message .= \sprintf( \__( 'You can see all followers here:', 'activitypub' ) ) . "\r\n";
133+
$message .= \esc_url( \admin_url( '/users.php?page=activitypub-followers-list' ) ) . "\r\n\r\n";
134+
135+
\wp_mail( $email, $subject, $message );
136+
}
137+
}

readme.txt

+2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ For reasons of data protection, it is not possible to see the followers of other
134134

135135
= Unreleased =
136136

137+
* Added: Send "new follower" emails
138+
* Improved: Email templates for Likes and Reposts
137139
* Improved: Interactions moderation
138140
* Improved: Compatibility with Akismet
139141

0 commit comments

Comments
 (0)