|
| 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 | +} |
0 commit comments