Skip to content

Commit 144e632

Browse files
authored
Settings to enable/disable notifications (#1109)
* Settings to enable/disable notifications This PR adds a settings section to enable/disable notifications. Fix #1107 * enable by default for new users * autoload * use add_option * call add_notification_defaults * fix tests
1 parent c7026f8 commit 144e632

File tree

7 files changed

+110
-25
lines changed

7 files changed

+110
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
* Add a filter to allow modifying the ActivityPub preview template
1313
* `@mentions` in the JSON representation of the reply
14+
* Add settings to enable/disable e-mail notifications for new followers and direct messages
1415

1516
### Improved
1617

includes/class-admin.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,25 @@ public static function register_settings() {
313313
)
314314
);
315315

316+
\register_setting(
317+
'activitypub',
318+
'activitypub_mailer_new_follower',
319+
array(
320+
'type' => 'boolean',
321+
'description' => \__( 'Send notifications via e-mail when a new follower is added.', 'activitypub' ),
322+
'default' => '0',
323+
)
324+
);
325+
\register_setting(
326+
'activitypub',
327+
'activitypub_mailer_new_dm',
328+
array(
329+
'type' => 'boolean',
330+
'description' => \__( 'Send notifications via e-mail when a direct message is received.', 'activitypub' ),
331+
'default' => '0',
332+
)
333+
);
334+
316335
// Blog-User Settings.
317336
\register_setting(
318337
'activitypub_blog',

includes/class-mailer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ public static function init() {
2222
\add_filter( 'comment_notification_text', array( self::class, 'comment_notification_text' ), 10, 2 );
2323

2424
// New follower notification.
25-
\add_action( 'activitypub_notification_follow', array( self::class, 'new_follower' ) );
25+
if ( '1' === \get_option( 'activitypub_mailer_new_follower', '0' ) ) {
26+
\add_action( 'activitypub_notification_follow', array( self::class, 'new_follower' ) );
27+
}
2628

2729
// Direct message notification.
28-
\add_action( 'activitypub_inbox_create', array( self::class, 'direct_message' ), 10, 2 );
30+
if ( '1' === \get_option( 'activitypub_mailer_new_dm', '0' ) ) {
31+
\add_action( 'activitypub_inbox_create', array( self::class, 'direct_message' ), 10, 2 );
32+
}
2933
}
3034

3135
/**

includes/class-migration.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ public static function update_comment_counts( $batch_size = 100, $offset = 0 ) {
452452
*/
453453
public static function add_default_settings() {
454454
self::add_activitypub_capability();
455+
self::add_notification_defaults();
455456
}
456457

457458
/**
@@ -471,6 +472,14 @@ private static function add_activitypub_capability() {
471472
}
472473
}
473474

475+
/**
476+
* Add default notification settings.
477+
*/
478+
private static function add_notification_defaults() {
479+
\add_option( 'activitypub_mailer_new_follower', '1' );
480+
\add_option( 'activitypub_mailer_new_dm', '1' );
481+
}
482+
474483
/**
475484
* Rename meta keys.
476485
*

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ For reasons of data protection, it is not possible to see the followers of other
136136

137137
* Added: A filter to allow modifying the ActivityPub preview template
138138
* Added: `@mentions` in the JSON representation of the reply
139+
* Added: Settings to enable/disable e-mail notifications for new followers and direct messages
139140
* Improved: HTML to e-mail text conversion
140141
* Improved: Direct Messages: Test for the user being in the to field
141142
* Improved: Better support for FSE color schemes

templates/settings.php

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,43 @@
186186
</tbody>
187187
</table>
188188
</div>
189-
189+
<div class="box">
190+
<h3><?php \esc_html_e( 'Notifications', 'activitypub' ); ?></h3>
191+
<p><?php \esc_html_e( 'Choose which notifications you want to receive. The plugin currently only supports e-mail notifications, but we will add more options in the future.', 'activitypub' ); ?></p>
192+
<table class="form-table">
193+
<tbody>
194+
<tr>
195+
<th scope="row">
196+
<?php \esc_html_e( 'Type', 'activitypub' ); ?>
197+
</th>
198+
<th>
199+
<?php \esc_html_e( 'E-Mail', 'activitypub' ); ?>
200+
</th>
201+
</tr>
202+
<tr>
203+
<td scope="row">
204+
<?php \esc_html_e( 'New followers', 'activitypub' ); ?>
205+
</td>
206+
<td>
207+
<label>
208+
<input type="checkbox" name="activitypub_mailer_new_follower" id="activitypub_mailer_new_follower" value="1" <?php \checked( '1', \get_option( 'activitypub_mailer_new_follower', '0' ) ); ?> />
209+
</label>
210+
</td>
211+
</tr>
212+
<tr>
213+
<td scope="row">
214+
<?php \esc_html_e( 'Direct Messages', 'activitypub' ); ?>
215+
</td>
216+
<td>
217+
<label>
218+
<input type="checkbox" name="activitypub_mailer_new_dm" id="activitypub_mailer_new_dm" value="1" <?php \checked( '1', \get_option( 'activitypub_mailer_new_dm', '0' ) ); ?> />
219+
</label>
220+
</td>
221+
</tr>
222+
<?php \do_settings_fields( 'activitypub', 'security' ); ?>
223+
</tbody>
224+
</table>
225+
</div>
190226
<div class="box">
191227
<h3><?php \esc_html_e( 'General', 'activitypub' ); ?></h3>
192228
<table class="form-table">
@@ -211,25 +247,6 @@
211247
</p>
212248
</td>
213249
</tr>
214-
<tr>
215-
<th scope="row">
216-
<?php \esc_html_e( 'Blocklist', 'activitypub' ); ?>
217-
</th>
218-
<td>
219-
<p>
220-
<?php
221-
echo \wp_kses(
222-
\sprintf(
223-
// translators: %s is a URL.
224-
\__( 'To block servers, add the host of the server to the "<a href="%s">Disallowed Comment Keys</a>" list.', 'activitypub' ),
225-
\esc_url( \admin_url( 'options-discussion.php#disallowed_keys' ) )
226-
),
227-
'default'
228-
);
229-
?>
230-
</p>
231-
</td>
232-
</tr>
233250
<?php \do_settings_fields( 'activitypub', 'general' ); ?>
234251
<?php \do_settings_fields( 'activitypub', 'server' ); ?>
235252
</tbody>
@@ -260,6 +277,25 @@
260277
</td>
261278
</tr>
262279
<?php endif; ?>
280+
<tr>
281+
<th scope="row">
282+
<?php \esc_html_e( 'Blocklist', 'activitypub' ); ?>
283+
</th>
284+
<td>
285+
<p>
286+
<?php
287+
echo \wp_kses(
288+
\sprintf(
289+
// translators: %s is a URL.
290+
\__( 'To block servers, add the host of the server to the "<a href="%s">Disallowed Comment Keys</a>" list.', 'activitypub' ),
291+
\esc_url( \admin_url( 'options-discussion.php#disallowed_keys' ) )
292+
),
293+
'default'
294+
);
295+
?>
296+
</p>
297+
</td>
298+
</tr>
263299
<?php \do_settings_fields( 'activitypub', 'security' ); ?>
264300
</tbody>
265301
</table>

tests/includes/class-test-mailer.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,26 @@ function ( $args ) {
204204
* @covers ::init
205205
*/
206206
public function test_init() {
207+
\delete_option( 'activitypub_mailer_new_follower' );
208+
\delete_option( 'activitypub_mailer_new_dm' );
209+
210+
Mailer::init();
211+
212+
$this->assertEquals( 10, \has_filter( 'comment_notification_subject', array( Mailer::class, 'comment_notification_subject' ) ) );
213+
$this->assertEquals( 10, \has_filter( 'comment_notification_text', array( Mailer::class, 'comment_notification_text' ) ) );
214+
$this->assertEquals( 10, \has_action( 'activitypub_notification_follow', array( Mailer::class, 'new_follower' ) ) );
215+
$this->assertEquals( 10, \has_action( 'activitypub_inbox_create', array( Mailer::class, 'direct_message' ) ) );
216+
217+
\remove_action( 'activitypub_notification_follow', array( Mailer::class, 'new_follower' ) );
218+
\remove_action( 'activitypub_inbox_create', array( Mailer::class, 'direct_message' ) );
219+
220+
\update_option( 'activitypub_mailer_new_follower', '0' );
221+
\update_option( 'activitypub_mailer_new_dm', '0' );
222+
207223
Mailer::init();
208224

209-
$this->assertEquals( 10, has_filter( 'comment_notification_subject', array( Mailer::class, 'comment_notification_subject' ) ) );
210-
$this->assertEquals( 10, has_filter( 'comment_notification_text', array( Mailer::class, 'comment_notification_text' ) ) );
211-
$this->assertEquals( 10, has_action( 'activitypub_notification_follow', array( Mailer::class, 'new_follower' ) ) );
225+
$this->assertEquals( false, \has_action( 'activitypub_notification_follow', array( Mailer::class, 'new_follower' ) ) );
226+
$this->assertEquals( false, \has_action( 'activitypub_inbox_create', array( Mailer::class, 'direct_message' ) ) );
212227
}
213228

214229
/**

0 commit comments

Comments
 (0)