8000 Add a notification for new followers by akirk · Pull Request #358 · akirk/friends · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add a notification for new followers #358

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

Merged
merged 3 commits into from
Oct 4, 2024
Merged
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 friends.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion friends.css.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions friends.scss
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,9 @@ h2#page-title a.dashicons {
details summary span {
margin-left: 1em;
border-bottom: 1px solid #ccc;
span {
margin-left: 0;
}
}
}
}
Expand Down
36 changes: 24 additions & 12 deletions includes/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2221,6 +2221,12 @@ public function process_admin_notification_manager() {
update_user_option( get_current_user_id(), 'friends_no_friend_request_notification', 1 );
}

if ( isset( $_POST['friend_follower_notification'] ) && boolval( $_POST['friend_follower_notification'] ) ) {
delete_user_option( get_current_user_id(), 'friends_no_friend_follower_notification' );
} else {
update_user_option( get_current_user_id(), 'friends_no_friend_follower_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 );
Expand Down Expand Up @@ -2305,21 +2311,27 @@ public function render_admin_notification_manager() {
$hide_from_friends_page = array();
}

$args = array(
'friend_users' => $friend_users->get_results(),
'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(),
);

if ( class_exists( '\Activitypub\Notification' ) ) {
$args['no_friend_follower_notification'] = get_user_option( 'friends_no_friend_follower_notification' );
}

Friends::template_loader()->get_template_part(
'admin/notification-manager',
null,
array(
'friend_users' => $friend_users->get_results(),
'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(),
)
$args
);

Friends::template_loader()->get_template_part( 'admin/settings-footer' );
Expand Down
125 changes: 125 additions & 0 deletions includes/class-notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ private function register_hooks() {
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 );
if ( ! get_user_option( 'friends_no_friend_follower_notification' ) ) {
add_action( 'activitypub_followers_post_follow', array( $this, 'activitypub_followers_post_follow' ), 10, 4 );
add_action( 'activitypub_followers_pre_remove_follower', array( $this, 'activitypub_followers_pre_remove_follower' ), 10, 3 );
}
}

/**
Expand Down Expand Up @@ -353,6 +357,127 @@ public function notify_friend_message_received( User $friend_user, $message, $su
$this->send_mail( $user->user_email, $email_title, $email_message );
}

/**
* Notify of a follower
*
* @param string $actor The Actor URL.
* @param array $activitypub_object The Activity object.
* @param int $user_id The ID of the WordPress User.
* @param Activitypub\Model\Follower $follower The Follower object.
*
* @return void
*/
public function activitypub_followers_post_follow( $actor, $activitypub_object, $user_id, $follower ) {
$user = new User( $user_id );
if ( defined( 'WP_TESTS_EMAIL' ) ) {
$user->user_email = WP_TESTS_EMAIL;
}
if ( ! $user->user_email ) {
return;
}

if ( ! apply_filters( 'notify_user_about_new_follower', true, $user, $actor, $activitypub_object, $follower ) ) {
return;
}

$url = \ActivityPub\object_to_uri( $follower->get( 'id' ) );
$server = wp_parse_url( $url, PHP_URL_HOST );
$following = User_Feed::get_by_url( $url );
if ( ! $following || is_wp_error( $following ) ) {
$following = User_Feed::get_by_url( $follower->get_url() );
}
if ( $following && ! is_wp_error( $following ) ) {
$following = $following->get_friend_user();
} else {
$following = false;
}

// translators: %s is a user display name.
$email_title = sprintf( __( 'New Follower: %s', 'friends' ), $follower->get_name() . '@' . $server );

$params = array(
'user' => $user,
'url' => $url,
'follower' => $follower,
'server' => $server,
'following' => $following,
);

$email_message = array();
ob_start();
Friends::t 5D39 emplate_loader()->get_template_part( 'email/header', null, array( 'email_title' => $email_title ) );
Friends::template_loader()->get_template_part( 'email/new-follower', null, $params );
Friends::template_loader()->get_template_part( 'email/footer' );
$email_message['html'] = ob_get_contents();
ob_end_clean();

ob_start();
Friends::template_loader()->get_template_part( 'email/new-follower-text', null, $params );
Friends::template_loader()->get_template_part( 'email/footer-text' );
$email_message['text'] = ob_get_contents();
ob_end_clean();

$this->send_mail( $user->user_email, $email_title, $email_message );
}

/**
* Notify of a lost follower
*
* @param string $actor The Actor URL.
* @param int $user_id The ID of the WordPress User.
* @param Activitypub\Model\Follower $follower The Follower object.
*
* @return void
*/
public function activitypub_followers_pre_remove_follower( $actor, $user_id, $follower ) {
$user = new User( $user_id );
if ( defined( 'WP_TESTS_EMAIL' ) ) {
$user->user_email = WP_TESTS_EMAIL;
}
if ( ! $user->user_email ) {
return;
}

if ( ! apply_filters( 'notify_user_about_lost_follower', true, $user, $actor, $follower ) ) {
return;
}

$url = \ActivityPub\object_to_uri( $follower->get( 'id' ) );
$server = wp_parse_url( $url, PHP_URL_HOST );

// translators: %s is a user display name.
$email_title = sprintf( __( 'Lost Follower: %s', 'friends' ), $follower->get_name() . '@' . $server );

$params = array(
'user' => $user,
'url' => $url,
'follower' => $follower,
'server' => $server,
'duration' => human_time_diff( strtotime( $follower->get_published() ) ) . ' (' . sprintf(
// translators: %s is a time duration.
__( 'since %s', 'friends' ),
$follower->get_published()
) . ')',

);

$email_message = array();
ob_start();
Friends::template_loader()->get_template_part( 'email/header', null, array( 'email_title' => $email_title ) );
Friends::template_loader()->get_template_part( 'email/lost-follower', null, $params );
Friends::template_loader()->get_template_part( 'email/footer' );
$email_message['html'] = ob_get_contents();
ob_end_clean();

ob_start();
Friends::template_loader()->get_template_part( 'email/lost-follower-text', null, $params );
Friends::template_loader()->get_template_part( 'email/footer-text' );
$email_message['text'] = ob_get_contents();
ob_end_clean();

$this->send_mail( $user->user_email, $email_title, $email_message );
}

/**
* Rewrite HTML for e-mail (by inlining some CSS styles)
*
Expand Down
8 changes: 8 additions & 0 deletions templates/admin/notification-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@
<span><?php esc_html_e( 'Friend Requests', 'friends' ); ?></span>
</label>
<br />
<?php if ( isset( $args['no_friend_follower_notification'] ) ) : ?>
<label for="friend_follower_notification">
<input name="friend_follower_notification" type="checkbox" id="friend_follower_notification" value="1" <?php checked( '1', ! $args['no_friend_follower_notification'] ); ?>>
<span><?php esc_html_e( 'New Followers', 'friends' ); ?></span>
<span><?php esc_html_e( '(via ActivityPub)', 'friends' ); ?></span>
</label>
<br />
<?php endif; ?>
<label for="new_post_notification">
<input name="new_post_notification" type="checkbox" id="new_post_notification" value="1" <?php checked( '1', ! $args['no_new_post_notification'] ); ?>>
<span><?php esc_html_e( 'New Posts', 'friends' ); ?></span>
Expand Down
2 changes: 1 addition & 1 deletion templates/email/accepted-friend-request-text.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

// translators: %s is a user display name.
printf( __( 'Howdy, %s!', 'friends' ), $args['user']->display_name );
printf( __( 'Hi %s!', 'friends' ), $args['user']->display_name );
echo PHP_EOL;

// translators: %s is a username.
Expand Down
2 changes: 1 addition & 1 deletion templates/email/accepted-friend-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<p>
<?php
// translators: %s is a user display name.
printf( __( 'Howdy, %s!', 'friends' ), esc_html( $args['user']->display_name ) );
printf( __( 'Hi %s!', 'friends' ), esc_html( $args['user']->display_name ) );
?>
</p>

Expand Down
2 changes: 1 addition & 1 deletion templates/email/friend-message-received-text.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
$quoted_text = '> ' . str_replace( PHP_EOL, PHP_EOL . '> ', trim( $normalized_whitespace ) );

// translators: %s is a user display name.
printf( __( 'Howdy, %s!', 'friends' ), $args['user']->display_name );
printf( __( 'Hi %s!', 'friends' ), $args['user']->display_name );
echo PHP_EOL;

// translators: %s is a username.
Expand Down
2 changes: 1 addition & 1 deletion templates/email/friend-message-received.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<p>
<?php
// translators: %s is a user display name.
printf( __( 'Howdy, %s!', 'friends' ), esc_html( $args['user']->display_name ) );
printf( __( 'Hi %s!', 'friends' ), esc_html( $args['user']->display_name ) );
?>
</p>

Expand Down
20 changes: 20 additions & 0 deletions templates/email/lost-follower-text.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* This template contains the text for the Lost Follower notification e-mail.
*
* @version 1.0
* @package Friends
*/

// translators: %s is a user display name.
printf( __( 'Hi %s!', 'friends' ), esc_html( $args['user']->display_name ) );
echo PHP_EOL;
echo PHP_EOL;
// translators: %s is a username.
printf( __( 'Sorry to inform you that you lost follower %s.', 'friends' ), $args['follower']->get_name() . ' (' . $args['follower']->get_preferred_username() . '@' . $args['server'] . ')' );
echo PHP_EOL;
echo PHP_EOL;
echo '> ' . wp_strip_all_tags( $args['follower']->get_summary() );
echo PHP_EOL;
// translators: %s is a time duration.
printf( __( 'They have been following you for: %s', 'friends' ), $args['duration'] );
52 changes: 52 additions & 0 deletions templates/email/lost-follower.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* This template contains the HTML for the New Follower notification e-mail.
*
* @version 1.0
* @package Friends
*/

?>
<p>
<?php
// translators: %s is a user display name.
printf( __( 'Hi %s!', 'friends' ), esc_html( $args['user']->display_name ) );
?>
</p>

<p>
<?php
echo __( 'Sorry to inform you that you lost a follower:', 'friends' );
?>
</p>

<table>
<tr>
<td style="vertical-align: top">
<a href="<?php echo esc_url( $args['follower']->get_url() ); ?>" style="float: left; margin-right: 1em;">
<img src="<?php echo esc_url( $args['follower']->get_icon_url() ); ?>" alt="<?php echo esc_attr( $args['follower']->get_name() ); ?>" width="64" height="64">
</a>
</td>
<td>

<a href="<?php echo esc_url( $args['follower']->get_url() ); ?>">
<strong><?php echo esc_html( $args['follower']->get_name() ); ?></strong> (<?php echo esc_html( $args['follower']->get_preferred_username() . '@' . $args['server'] ); ?>)
</a>
<br>
<?php
if ( $args['follower']->get_summary() ) {
echo wp_kses_post( nl2br( $args['follower']->get_summary() ) );
}
?>
</td>
</tr>
</table>

<p>
<?php
// translators: %s is a time duration.
echo esc_html( sprintf( __( 'They have been following you for: %s', 'friends' ), $args['duration'] ) );
?>
</p>


24 changes: 24 additions & 0 deletions templates/email/new-follower-text.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* This template contains the text for the New Follower notification e-mail.
*
* @version 1.0
* @package Friends
*/

// translators: %s is a user display name.
printf( __( 'Hi %s!', 'friends' ), esc_html( $args['user']->display_name ) );
echo PHP_EOL;
echo PHP_EOL;
// translators: %s is a username.
printf( __( 'You have a new follower %s.', 'friends' ), $args['follower']->get_name() );
echo PHP_EOL;
echo PHP_EOL;
echo '> ' . wp_strip_all_tags( $args['follower']->get_summary() );
echo PHP_EOL;
// translators: %s is a URL.
printf( __( 'You can view their profile at %s', 'friends' ), esc_url( $args['url'] ) );
echo PHP_EOL;
echo PHP_EOL;
echo __( 'Maybe you want to follow them back?', 'friends' ), ' ', esc_url( add_query_arg( 'url', $args['url'], admin_url( 'admin.php?page=add-friend' ) ) );
echo PHP_EOL;
Loading
Loading
0