Skip to content

Commit

Permalink
Implement the notification code
Browse files Browse the repository at this point in the history
  • Loading branch information
akirk committed Sep 29, 2024
1 parent c1bd46c commit 3fb882f
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 37 deletions.
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
7 changes: 7 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 Down Expand Up @@ -2307,6 +2313,7 @@ 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(),
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
5 changes: 5 additions & 0 deletions templates/admin/notification-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,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

0 comments on commit 3fb882f

Please sign in to comment.