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

Support comment_previously_approved setting #962

Merged
merged 8 commits into from
Nov 6, 2024
39 changes: 39 additions & 0 deletions includes/class-comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static function init() {
\add_filter( 'get_comment_link', array( self::class, 'remote_comment_link' ), 11, 3 );
\add_action( 'wp_enqueue_scripts', array( self::class, 'enqueue_scripts' ) );
\add_action( 'pre_get_comments', array( static::class, 'comment_query' ) );
\add_action( '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 );
}
Expand Down Expand Up @@ -666,4 +667,42 @@ public static function comment_query( $query ) {
// Exclude likes and reposts by the ActivityPub plugin.
$query->query_vars['type__not_in'] = self::get_comment_type_names();
}

/**
* Filter the comment status before it is set.
*
* @param string $approved The approved comment status.
* @param array $commentdata The comment data.
*
* @return boolean `true` if the comment is approved, `false` otherwise.
*/
public static function pre_comment_approved( $approved, $commentdata ) {
if ( $approved || \is_wp_error( $approved ) ) {
return $approved;
}

if ( '1' !== get_option( 'comment_previously_approved' ) ) {
return $approved;
}

if (
empty( $commentdata['comment_meta']['protocol'] ) ||
'activitypub' !== $commentdata['comment_meta']['protocol']
) {
return $approved;
}

global $wpdb;

$author = $commentdata['comment_author'];
$author_url = $commentdata['comment_author_url'];
// phpcs:ignore
$ok_to_comment = $wpdb->get_var( $wpdb->prepare( "SELECT comment_approved FROM $wpdb->comments WHERE comment_author = %s AND comment_author_url = %s and comment_approved = '1' LIMIT 1", $author, $author_url ) );

if ( 1 === (int) $ok_to_comment ) {
return 1;
}

return $approved;
}
}
Loading