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

Add the ability to disable notifications per post format and feed parser #357

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion feed-parsers/class-feed-parser-simplepie.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public function process_items( $items, $url ) {
'gravatar' => 'gravatar',
'comment_count' => 'comments',
'status' => 'post-status',
'format' => 'post-format',
'post_format' => 'post-format',
'id' => 'post-id',
) as $key => $lookup_key ) {
foreach ( array( Feed::XMLNS, 'com-wordpress:feed-additions:1' ) as $xmlns ) {
Expand Down
13 changes: 13 additions & 0 deletions friends-admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,16 @@ span#friends_enable_retention_number_line {
#friends-dashboard-widget .author-avatar {
vertical-align: middle;
}

.friends-notification-manager details.options {
margin-top: 1em;
}
.friends-notification-manager details.options fieldset {
margin-top: 1em;
margin-bottom: 1em;
display: flex;
flex-wrap: wrap;
}
.friends-notification-manager details.options fieldset label {
width: 10em;
}
24 changes: 24 additions & 0 deletions includes/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2203,6 +2203,12 @@ public function process_admin_notification_manager() {
update_option( 'friends_notification_keywords', $keywords );
}

if ( isset( $_POST['keyword_notification_override'] ) && boolval( $_POST['keyword_notification_override'] ) ) {
delete_user_option( get_current_user_id(), 'friends_keyword_notification_override_disabled' );
} else {
update_user_option( get_current_user_id(), 'friends_keyword_notification_override_disabled', 1 );
}

if ( isset( $_POST['new_post_notification'] ) && boolval( $_POST['new_post_notification'] ) ) {
delete_user_option( get_current_user_id(), 'friends_no_new_post_notification' );
} else {
Expand All @@ -2215,6 +2221,22 @@ public function process_admin_notification_manager() {
update_user_option( get_current_user_id(), 'friends_no_friend_request_notification', 1 );
}

foreach ( get_post_format_slugs() as $post_format ) {
if ( isset( $_POST[ 'new_post_format_notification_' . $post_format ] ) && boolval( $_POST[ 'new_post_format_notification_' . $post_format ] ) ) {
delete_user_option( get_current_user_id(), 'friends_no_new_post_format_notification_' . $post_format );
} else {
update_user_option( get_current_user_id(), 'friends_no_new_post_format_notification_' . $post_format, 1 );
}
}

foreach ( array_keys( $this->friends->feed->get_registered_parsers() ) as $parser ) {
if ( isset( $_POST[ 'new_post_by_parser_notification_' . $parser ] ) && boolval( $_POST[ 'new_post_by_parser_notification_' . $parser ] ) ) {
delete_user_option( get_current_user_id(), 'friends_no_new_post_by_parser_notification_' . $parser );
} else {
update_user_option( get_current_user_id(), 'friends_no_new_post_by_parser_notification_' . $parser, 1 );
}
}

if ( empty( $_POST['friend_listed'] ) ) {
return;
}
Expand Down Expand Up @@ -2291,10 +2313,12 @@ public function render_admin_notification_manager() {
'friends_settings_url' => add_query_arg( '_wp_http_referer', remove_query_arg( '_wp_http_referer' ), self_admin_url( 'admin.php?page=friends-settings' ) ),
'hide_from_friends_page' => $hide_from_friends_page,
'no_friend_request_notification' => get_user_option( 'friends_no_friend_request_notification' ),
'keyword_override_disabled' => get_user_option( 'friends_keyword_notification_override_disabled' ),
'no_new_post_notification' => get_user_option( 'friends_no_new_post_notification' ),
'no_keyword_notification' => get_user_option( 'friends_no_keyword_notification' ),
'notification_keywords' => Feed::get_all_notification_keywords(),
'active_keywords' => Feed::get_active_notification_keywords(),
'feed_parsers' => $this->friends->feed->get_registered_parsers(),
)
);

Expand Down
6 changes: 3 additions & 3 deletions includes/class-feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,19 +260,19 @@ public function notify_about_new_posts( User $friend_user, $new_posts, User_Feed
}

if ( $keyword_match ) {
$notified = apply_filters( 'notify_keyword_match_post', false, $post, $keyword_match );
$notified = apply_filters( 'friends_notify_keyword_match_post', false, $post, $keyword_match );
if ( $notified ) {
continue;
}
}
}

$notify_users = apply_filters( 'notify_about_new_friend_post', true, $friend_user, $post_id, $user_feed );
$notify_users = apply_filters( 'notify_about_new_friend_post', true, $friend_user, $post_id, $user_feed, $keyword_match );
if ( $notify_users ) {
if ( ! $post ) {
$post = get_post( intval( $post_id ) );
}
do_action( 'notify_new_friend_post', $post, $user_feed );
do_action( 'notify_new_friend_post', $post, $user_feed, $keyword_match );
}
}
}
Expand Down
33 changes: 27 additions & 6 deletions includes/class-notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public function __construct( Friends $friends ) {
private function register_hooks() {
add_filter( 'friends_rewrite_mail_html', array( $this, 'rewrite_mail_html' ) );
add_filter( 'friends_rewrite_mail_html', array( $this, 'highlight_keywords' ), 10, 2 );
add_action( 'notify_new_friend_post', array( $this, 'notify_new_friend_post' ), 10, 2 );
add_filter( 'notify_keyword_match_post', array( $this, 'notify_keyword_match_post' ), 10, 3 );
add_action( 'notify_new_friend_post', array( $this, 'notify_new_friend_post' ), 10, 3 );
add_filter( 'friends_notify_keyword_match_post', array( $this, 'notify_keyword_match_post' ), 10, 3 );
add_action( 'notify_new_friend_request', array( $this, 'notify_new_friend_request' ) );
add_action( 'notify_accepted_friend_request', array( $this, 'notify_accepted_friend_request' ) );
add_action( 'notify_friend_message_received', array( $this, 'notify_friend_message_received' ), 10, 3 );
Expand Down Expand Up @@ -83,10 +83,11 @@ public function get_friends_plugin_from_email_address() {
/**
* Notify the users of this site about a new friend post
*
* @param \WP_Post $post The new post by a friend or subscription.
* @param User_Feed $user_feed The feed where the post came from.
* @param \WP_Post $post The new post by a friend or subscription.
* @param User_Feed $user_feed The feed where the post came from.
* @param string|false $keyword If a post matched a keyword, the keyword is specified.
*/
public function notify_new_friend_post( \WP_Post $post, User_Feed $user_feed ) {
public function notify_new_friend_post( \WP_Post $post, User_Feed $user_feed, $keyword ) {
if (
// Post might be trashed through rules.
'trash' === $post->post_status
Expand All @@ -104,13 +105,28 @@ public function notify_new_friend_post( \WP_Post $post, User_Feed $user_feed ) {
}
$author = $user_feed->get_friend_user();

if ( ! get_post_format( $post ) ) {
$post_format = 'standard';
} else {
$post_format = get_post_format( $post );
}

$notify_user = ! get_user_option( 'friends_no_new_post_notification', $user->ID );
$notify_user = $notify_user && ! get_user_option( 'friends_no_new_post_notification_' . $author->user_login, $user->ID );
$notify_user = $notify_user && ! get_user_option( 'friends_no_new_post_format_notification_' . $post_format, $user->ID );
$notify_user = $notify_user && ! get_user_option( 'friends_no_new_post_by_parser_notification_' . $user_feed->get_parser(), $user->ID );

// If the post would notify anyway and it was a keyword match, notify that way.
if ( $notify_user && $keyword && get_user_option( 'friends_keyword_notification_override_disabled', $user->ID ) ) {
add_filter( 'get_user_option_friends_keyword_notification_override_disabled', '__return_false' );
$this->notify_keyword_match_post( false, $post, $keyword );
remove_filter( 'get_user_option_friends_keyword_notification_override_disabled', '__return_false' );
return;
}

if ( ! apply_filters( 'notify_user_about_friend_post', $notify_user, $user, $post, $author ) ) {
return;
}

$email_title = $post->post_title;

$params = array(
Expand Down Expand Up @@ -155,6 +171,11 @@ public function notify_keyword_match_post( $notified, \WP_Post $post, $keyword )
return $notified;
}
$notify_user = ! get_user_option( 'friends_no_keyword_notification_' . $post->post_author, $user->ID );
// If the override was disabled, don't notify. This could be temporarily disabled later.

if ( $notify_user && get_user_option( 'friends_keyword_notification_override_disabled', $user->ID ) ) {
return $notified;
}

if ( ! apply_filters( 'notify_user_about_keyword_post', $notify_user, $user, $post, $keyword ) ) {
return $notified;
Expand Down
2 changes: 1 addition & 1 deletion includes/class-user-feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ public static function save( User $friend_user, $url, $args = array() ) {
return $term;
}

return new self( $term );
return new self( $term, $friend_user );
}

/**
Expand Down
71 changes: 71 additions & 0 deletions templates/admin/notification-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,22 @@
* @package Friends
*/

$disabled_post_formats = array();
foreach ( get_post_format_strings() as $post_format => $label ) {
if ( get_user_option( 'friends_no_new_post_format_notification_' . $post_format ) ) {
$disabled_post_formats[] = $post_format;
}
}
$disabled_feed_parsers = array();
foreach ( $args['feed_parsers'] as $feed_parser => $label ) {
if ( get_user_option( 'friends_no_new_post_by_parser_notification_' . $feed_parser ) ) {
$disabled_feed_parsers[] = $feed_parser;
}
}

?><form method="post">
<?php wp_nonce_field( 'notification-manager' ); ?>
<p class="explanation"><?php esc_html_e( 'The settings on this page apply just to you, other users can configure their own settings.', 'friends' ); ?></p>
<table class="form-table">
<tbody>
<tr>
Expand All @@ -29,6 +43,58 @@
</label>
</fieldset>
<p class="description"><?php esc_html_e( 'When you disable post notifications, their individual configuration is disabled also.', 'friends' ); ?></p>

<details class="options">
<summary>
<?php echo esc_html( _x( 'By Post Format', 'e-mail notifications', 'friends' ) ); ?>
<?php
if ( count( $disabled_post_formats ) > 0 ) {
echo esc_html(
sprintf(
// translators: %d: number of selected post formats.
_n( '(%d disabled)', '(%d disabled)', count( $disabled_post_formats ), 'friends' ),
count( $disabled_post_formats )
)
);
}
?>
</summary>
<fieldset>
<?php foreach ( get_post_format_strings() as $post_format => $label ) : ?>
<label>
<input name="new_post_format_notification_<?php echo esc_attr( $post_format ); ?>" type="checkbox" id="new_post_format_notification_<?php echo esc_attr( $post_format ); ?>" value="1" <?php checked( ! in_array( $post_format, $disabled_post_formats ) ); ?>>
<span><?php echo esc_html( $label ); ?></span>
</label>
<?php endforeach; ?>
</fieldset>
</details>
<p class="description"><?php esc_html_e( 'You can disable notifications for posts in specific post formats.', 'friends' ); ?></p>

<details class="options">
<summary>
<?php echo esc_html( _x( 'By Feed Parser', 'e-mail notifications', 'friends' ) ); ?>
<?php
if ( count( $disabled_feed_parsers ) > 0 ) {
echo esc_html(
sprintf(
// translators: %d: number of selected feed parsers.
_n( '(%d disabled)', '(%d disabled)', count( $disabled_feed_parsers ), 'friends' ),
count( $disabled_feed_parsers )
)
);
}
?>
</summary>
<fieldset>
<?php foreach ( $args['feed_parsers'] as $feed_parser => $label ) : ?>
<label>
<input name="new_post_by_parser_notification_<?php echo esc_attr( $feed_parser ); ?>" type="checkbox" id="new_post_by_parser_notification_<?php echo esc_attr( $feed_parser ); ?>" value="1" <?php checked( ! in_array( $feed_parser, $disabled_feed_parsers ) ); ?>>
<span><?php echo wp_kses( $label, array() ); ?></span>
</label>
<?php endforeach; ?>
</fieldset>
</details>
<p class="description"><?php esc_html_e( 'You can disable notifications for posts arriving with specific feed parsers.', 'friends' ); ?></p>
</td>
</tr>
<tr>
Expand All @@ -39,6 +105,11 @@
</th>
<td>
<fieldset>
<label>
<input name="keyword_notification_override" type="checkbox" id="keyword_notification_override" value="1" <?php checked( ! $args['keyword_override_disabled'] ); ?>>
<span><?php esc_html_e( 'Notify about matching keywords even if disabled above', 'friends' ); ?></span>
</label>
<p class="description"><?php esc_html_e( 'For example, even if you disabled post notifications, you can still get notified about posts containing specific keywords.', 'friends' ); ?></p>

<ol id="keyword-notifications">
<li id="keyword-template" style="display: none">
Expand Down
1 change: 1 addition & 0 deletions tests/class-feed-parser-local-file.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* This is the class for testing feed parsing.
*/
class Feed_Parser_Local_File extends Feed_Parser_SimplePie {
const SLUG = 'local';
/**
* Constructor.
*
Expand Down
68 changes: 68 additions & 0 deletions tests/data/friend-feed-post-formats.rss
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0"?>
<rss version="2.0" xmlns:friends="wordpress-plugin-friends:feed-additions:1">
<channel>
<title>friend.local</title>
<link>http://friend.local</link>
<description>My friend's site</description>
<item>
<title>Standard Post</title>
<link>http://friend.local/2023/01/01/standard-post</link>
<description>This is the standard post content.</description>
<friends:post-format>standard</friends:post-format>
</item>
<item>
<title>Aside Post</title>
<link>http://friend.local/2023/01/02/aside-post</link>
<description>This is the aside post content.</description>
<friends:post-format>aside</friends:post-format>
</item>
<item>
<title>Gallery Post</title>
<link>http://friend.local/2023/01/03/gallery-post</link>
<description>This is the gallery post content.</description>
<friends:post-format>gallery</friends:post-format>
</item>
<item>
<title>Image Post</title>
<link>http://friend.local/2023/01/04/image-post</link>
<description>This is the image post content.</description>
<friends:post-format>image</friends:post-format>
</item>
<item>
<title>Link Post</title>
<link>http://friend.local/2023/01/05/link-post</link>
<description>This is the link post content.</description>
<friends:post-format>link</friends:post-format>
</item>
<item>
<title>Quote Post</title>
<link>http://friend.local/2023/01/06/quote-post</link>
<description>This is the quote post content.</description>
<friends:post-format>quote</friends:post-format>
</item>
<item>
<title>Status Post</title>
<link>http://friend.local/2023/01/07/status-post</link>
<description>This is the status post content.</description>
<friends:post-format>status</friends:post-format>
</item>
<item>
<title>Video Post</title>
<link>http://friend.local/2023/01/08/video-post</link>
<description>This is the video post content.</description>
<friends:post-format>video</friends:post-format>
</item>
<item>
<title>Audio Post</title>
<link>http://friend.local/2023/01/09/audio-post</link>
<description>This is the audio post content.</description>
<friends:post-format>audio</friends:post-format>
</item>
<item>
<title>Chat Post</title>
<link>http://friend.local/2023/01/10/chat-post</link>
<description>This is the chat post content.</description>
<friends:post-format>chat</friends:post-format>
</item>
</channel>
</rss>
Loading
Loading