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 interaction settings #1395

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
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: 2 additions & 0 deletions includes/class-activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public static function uninstall() {
Migration::update_comment_counts( 2000 );

delete_option( 'activitypub_actor_mode' );
delete_option( 'activitypub_allow_likes' );
delete_option( 'activitypub_allow_replies' );
delete_option( 'activitypub_attribution_domains' );
delete_option( 'activitypub_authorized_fetch' );
delete_option( 'activitypub_application_user_private_key' );
Expand Down
37 changes: 32 additions & 5 deletions includes/class-comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public static function init() {
\add_action( 'pre_get_comments', array( static::class, 'comment_query' ) );
\add_filter( 'pre_comment_approved', array( static::class, 'pre_comment_approved' ), 10, 2 );
\add_filter( 'get_avatar_comment_types', array( static::class, 'get_avatar_comment_types' ), 99 );
\add_action( 'update_option_activitypub_allow_likes', array( self::class, 'maybe_update_comment_counts' ), 10, 2 );
\add_action( 'update_option_activitypub_allow_reposts', array( self::class, 'maybe_update_comment_counts' ), 10, 2 );
\add_filter( 'pre_wp_update_comment_count_now', array( static::class, 'pre_wp_update_comment_count_now' ), 10, 3 );
}

Expand Down Expand Up @@ -773,6 +775,20 @@ public static function pre_comment_approved( $approved, $commentdata ) {
return $approved;
}

/**
* Update comment counts when interaction settings are disabled.
*
* Triggers a recount when likes or reposts are disabled to ensure accurate comment counts.
*
* @param mixed $old_value The old option value.
* @param mixed $value The new option value.
*/
public static function maybe_update_comment_counts( $old_value, $value ) {
if ( '1' === $old_value && '1' !== $value ) {
Migration::update_comment_counts();
}
}

/**
* Filters the comment count to exclude ActivityPub comment types.
*
Expand All @@ -784,15 +800,26 @@ public static function pre_comment_approved( $approved, $commentdata ) {
*/
public static function pre_wp_update_comment_count_now( $new_count, $old_count, $post_id ) {
if ( null === $new_count ) {
global $wpdb;
$excluded_types = array_filter( self::get_comment_type_slugs(), array( self::class, 'is_comment_type_enabled' ) );

$excluded_types = self::get_comment_type_slugs();

// phpcs:ignore WordPress.DB
$new_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' AND comment_type NOT IN ('" . implode( "','", $excluded_types ) . "')", $post_id ) );
if ( ! empty( $excluded_types ) ) {
global $wpdb;

// phpcs:ignore WordPress.DB
$new_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' AND comment_type NOT IN ('" . implode( "','", $excluded_types ) . "')", $post_id ) );
}
}

return $new_count;
}

/**
* Check if a comment type is enabled.
*
* @param string $comment_type The comment type.
* @return bool True if the comment type is enabled.
*/
private static function is_comment_type_enabled( $comment_type ) {
return '1' === get_option( "activitypub_allow_{$comment_type}s", '1' );
}
}
5 changes: 5 additions & 0 deletions includes/handler/class-announce.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public static function handle_announce( $announcement, $user_id, $activity = nul
return;
}

// Check if reposts are allowed.
if ( '1' !== \get_option( 'activitypub_allow_reposts', '1' ) ) {
return;
}

self::maybe_save_announce( $announcement, $user_id );

if ( is_string( $announcement['object'] ) ) {
Expand Down
5 changes: 5 additions & 0 deletions includes/handler/class-like.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public static function handle_like( $like, $user_id ) {
return;
}

// Check if likes are allowed.
if ( '1' !== \get_option( 'activitypub_allow_likes', '1' ) ) {
return;
}

$url = object_to_uri( $like['object'] );

if ( empty( $url ) ) {
Expand Down
37 changes: 36 additions & 1 deletion includes/wp-admin/class-settings-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ public static function register_settings_fields() {
'activitypub_activities'
);

add_settings_field(
'activitypub_allow_interactions',
__( 'Post interactions', 'activitypub' ),
array( self::class, 'render_allow_interactions_field' ),
'activitypub_settings',
'activitypub_activities'
);

$object_type = \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE );
if ( 'note' === $object_type ) {
add_settings_field(
Expand Down Expand Up @@ -441,7 +449,34 @@ public static function render_outbox_purge_days_field() {
}

/**
* Render use hashtags field.
* Render allow interactions field.
*/
public static function render_allow_interactions_field() {
$allow_likes = get_option( 'activitypub_allow_likes', '1' );
$allow_reposts = get_option( 'activitypub_allow_reposts', '1' );
?>
<fieldset>
<p><?php esc_html_e( 'Choose which fediverse interactions to receive as comments on your blog:', 'activitypub' ); ?></p>
<ul>
<li>
<label>
<input type="checkbox" name="activitypub_allow_likes" value="1" <?php checked( '1', $allow_likes ); ?> />
<?php esc_html_e( 'Receive likes', 'activitypub' ); ?>
</label>
</li>
<li>
<label>
<input type="checkbox" name="activitypub_allow_announces" value="1" <?php checked( '1', $allow_reposts ); ?> />
<?php esc_html_e( 'Receive reblogs', 'activitypub' ); ?>
</label>
</li>
</ul>
</fieldset>
<?php
}

/**
* Render authorized fetch field.
*/
public static function render_authorized_fetch_field() {
$value = get_option( 'activitypub_authorized_fetch', '1' );
Expand Down
Loading