@@ -20,20 +20,9 @@ public static function init() {
2020 \add_filter ( 'comment_notification_subject ' , array ( self ::class, 'comment_notification_subject ' ), 10 , 2 );
2121 \add_filter ( 'comment_notification_text ' , array ( self ::class, 'comment_notification_text ' ), 10 , 2 );
2222
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- // Direct message notification.
34- if ( '1 ' === \get_option ( 'activitypub_mailer_new_mention ' , '1 ' ) ) {
35- \add_action ( 'activitypub_inbox_create ' , array ( self ::class, 'mention ' ), 10 , 2 );
36- }
23+ \add_action ( 'activitypub_inbox_follow ' , array ( self ::class, 'new_follower ' ), 10 , 2 );
24+ \add_action ( 'activitypub_inbox_create ' , array ( self ::class, 'direct_message ' ), 10 , 2 );
25+ \add_action ( 'activitypub_inbox_create ' , array ( self ::class, 'mention ' ), 10 , 2 );
3726 }
3827
3928 /**
@@ -115,38 +104,40 @@ public static function comment_notification_text( $message, $comment_id ) {
115104 /**
116105 * Send a notification email for every new follower.
117106 *
118- * @param Notification $notification The notification object.
107+ * @param array $activity The activity object.
108+ * @param int $user_id The id of the local blog-user.
119109 */
120- public static function new_follower ( $ notification ) {
121- $ actor = get_remote_metadata_by_actor ( $ notification ->actor );
110+ public static function new_follower ( $ activity , $ user_id ) {
111+ if ( $ user_id > Actors::BLOG_USER_ID ) {
112+ if ( ! \get_user_option ( 'activitypub_mailer_new_follower ' , $ user_id ) ) {
113+ return ;
114+ }
122115
116+ $ email = \get_userdata ( $ user_id )->user_email ;
117+ $ admin_url = '/users.php?page=activitypub-followers-list ' ;
118+ } else {
119+ if ( '1 ' !== \get_option ( 'activitypub_blog_user_mailer_new_follower ' , '1 ' ) ) {
120+ return ;
121+ }
122+
123+ $ email = \get_option ( 'admin_email ' );
124+ $ admin_url = '/options-general.php?page=activitypub&tab=followers ' ;
125+ }
126+
127+ $ actor = get_remote_metadata_by_actor ( $ activity ['actor ' ] );
123128 if ( ! $ actor || \is_wp_error ( $ actor ) ) {
124129 return ;
125130 }
126131
127132 if ( empty ( $ actor ['webfinger ' ] ) ) {
128- $ actor ['webfinger ' ] = '@ ' . ( $ actor ['preferredUsername ' ] ?? $ actor ['name ' ] ) . '@ ' . wp_parse_url ( $ actor ['url ' ], PHP_URL_HOST );
129- }
130-
131- $ email = \get_option ( 'admin_email ' );
132- $ admin_url = '/options-general.php?page=activitypub&tab=followers ' ;
133-
134- if ( $ notification ->target > Actors::BLOG_USER_ID ) {
135- $ user = \get_user_by ( 'id ' , $ notification ->target );
136-
137- if ( ! $ user ) {
138- return ;
139- }
140-
141- $ email = $ user ->user_email ;
142- $ admin_url = '/users.php?page=activitypub-followers-list ' ;
133+ $ actor ['webfinger ' ] = '@ ' . ( $ actor ['preferredUsername ' ] ?? $ actor ['name ' ] ) . '@ ' . \wp_parse_url ( $ actor ['url ' ], PHP_URL_HOST );
143134 }
144135
145136 $ template_args = array_merge (
146137 $ actor ,
147138 array (
148139 'admin_url ' => $ admin_url ,
149- 'target ' => $ notification -> target ,
140+ 'user_id ' => $ user_id ,
150141 'stats ' => array (
151142 'outbox ' => null ,
152143 'followers ' => null ,
@@ -161,16 +152,16 @@ public static function new_follower( $notification ) {
161152 }
162153
163154 $ result = Http::get ( $ actor [ $ field ], true );
164- if ( 200 === wp_remote_retrieve_response_code ( $ result ) ) {
165- $ body = \json_decode ( wp_remote_retrieve_body ( $ result ), true );
155+ if ( 200 === \ wp_remote_retrieve_response_code ( $ result ) ) {
156+ $ body = \json_decode ( \ wp_remote_retrieve_body ( $ result ), true );
166157 if ( isset ( $ body ['totalItems ' ] ) ) {
167158 $ template_args ['stats ' ][ $ field ] = $ body ['totalItems ' ];
168159 }
169160 }
170161 }
171162
172163 /* translators: 1: Blog name, 2: Follower name */
173- $ subject = \sprintf ( \__ ( '[%1$s] New Follower: %2$s ' , 'activitypub ' ), get_option ( 'blogname ' ), $ actor ['name ' ] );
164+ $ subject = \sprintf ( \__ ( '[%1$s] New Follower: %2$s ' , 'activitypub ' ), \ get_option ( 'blogname ' ), $ actor ['name ' ] );
174165
175166 \ob_start ();
176167 \load_template ( ACTIVITYPUB_PLUGIN_DIR . 'templates/emails/new-follower.php ' , false , $ template_args );
@@ -208,26 +199,28 @@ public static function direct_message( $activity, $user_id ) {
208199 return ;
209200 }
210201
202+ if ( $ user_id > Actors::BLOG_USER_ID ) {
203+ if ( ! \get_user_option ( 'activitypub_mailer_new_dm ' , $ user_id ) ) {
204+ return ;
205+ }
206+
207+ $ email = \get_userdata ( $ user_id )->user_email ;
208+ } else {
209+ if ( '1 ' !== \get_option ( 'activitypub_blog_user_mailer_new_dm ' , '1 ' ) ) {
210+ return ;
211+ }
212+
213+ $ email = \get_option ( 'admin_email ' );
214+ }
215+
211216 $ actor = get_remote_metadata_by_actor ( $ activity ['actor ' ] );
212217
213218 if ( ! $ actor || \is_wp_error ( $ actor ) || empty ( $ activity ['object ' ]['content ' ] ) ) {
214219 return ;
215220 }
216221
217222 if ( empty ( $ actor ['webfinger ' ] ) ) {
218- $ actor ['webfinger ' ] = '@ ' . ( $ actor ['preferredUsername ' ] ?? $ actor ['name ' ] ) . '@ ' . wp_parse_url ( $ actor ['url ' ], PHP_URL_HOST );
219- }
220-
221- $ email = \get_option ( 'admin_email ' );
222-
223- if ( (int ) $ user_id > Actors::BLOG_USER_ID ) {
224- $ user = \get_user_by ( 'id ' , $ user_id );
225-
226- if ( ! $ user ) {
227- return ;
228- }
229-
230- $ email = $ user ->user_email ;
223+ $ actor ['webfinger ' ] = '@ ' . ( $ actor ['preferredUsername ' ] ?? $ actor ['name ' ] ) . '@ ' . \wp_parse_url ( $ actor ['url ' ], PHP_URL_HOST );
231224 }
232225
233226 $ template_args = array (
@@ -237,7 +230,7 @@ public static function direct_message( $activity, $user_id ) {
237230 );
238231
239232 /* translators: 1: Blog name, 2 Actor name */
240- $ subject = \sprintf ( \esc_html__ ( '[%1$s] Direct Message from: %2$s ' , 'activitypub ' ), \esc_html ( get_option ( 'blogname ' ) ), \esc_html ( $ actor ['name ' ] ) );
233+ $ subject = \sprintf ( \esc_html__ ( '[%1$s] Direct Message from: %2$s ' , 'activitypub ' ), \esc_html ( \ get_option ( 'blogname ' ) ), \esc_html ( $ actor ['name ' ] ) );
241234
242235 \ob_start ();
243236 \load_template ( ACTIVITYPUB_PLUGIN_DIR . 'templates/emails/new-dm.php ' , false , $ template_args );
@@ -282,25 +275,27 @@ public static function mention( $activity, $user_id ) {
282275 return ;
283276 }
284277
278+ if ( $ user_id > Actors::BLOG_USER_ID ) {
279+ if ( ! \get_user_option ( 'activitypub_mailer_new_mention ' , $ user_id ) ) {
280+ return ;
281+ }
282+
283+ $ email = \get_userdata ( $ user_id )->user_email ;
284+ } else {
285+ if ( '1 ' !== \get_option ( 'activitypub_blog_user_mailer_new_mention ' , '1 ' ) ) {
286+ return ;
287+ }
288+
289+ $ email = \get_option ( 'admin_email ' );
290+ }
291+
285292 $ actor = get_remote_metadata_by_actor ( $ activity ['actor ' ] );
286293 if ( \is_wp_error ( $ actor ) ) {
287294 return ;
288295 }
289296
290297 if ( empty ( $ actor ['webfinger ' ] ) ) {
291- $ actor ['webfinger ' ] = '@ ' . ( $ actor ['preferredUsername ' ] ?? $ actor ['name ' ] ) . '@ ' . wp_parse_url ( $ actor ['url ' ], PHP_URL_HOST );
292- }
293-
294- $ email = \get_option ( 'admin_email ' );
295-
296- if ( (int ) $ user_id > Actors::BLOG_USER_ID ) {
297- $ user = \get_user_by ( 'id ' , $ user_id );
298-
299- if ( ! $ user ) {
300- return ;
301- }
302-
303- $ email = $ user ->user_email ;
298+ $ actor ['webfinger ' ] = '@ ' . ( $ actor ['preferredUsername ' ] ?? $ actor ['name ' ] ) . '@ ' . \wp_parse_url ( $ actor ['url ' ], PHP_URL_HOST );
304299 }
305300
306301 $ template_args = array (
@@ -310,7 +305,7 @@ public static function mention( $activity, $user_id ) {
310305 );
311306
312307 /* translators: 1: Blog name, 2 Actor name */
313- $ subject = \sprintf ( \esc_html__ ( '[%1$s] Mention from: %2$s ' , 'activitypub ' ), \esc_html ( get_option ( 'blogname ' ) ), \esc_html ( $ actor ['name ' ] ) );
308+ $ subject = \sprintf ( \esc_html__ ( '[%1$s] Mention from: %2$s ' , 'activitypub ' ), \esc_html ( \ get_option ( 'blogname ' ) ), \esc_html ( $ actor ['name ' ] ) );
314309
315310 \ob_start ();
316311 \load_template ( ACTIVITYPUB_PLUGIN_DIR . 'templates/emails/new-mention.php ' , false , $ template_args );
0 commit comments