Skip to content

Commit

Permalink
Support comment_previously_approved setting (#962)
Browse files Browse the repository at this point in the history
* Support `comment_previously_approved` setting

* return `$approved` instead of `true`

* some small fixes

* return int

* improved check
  • Loading branch information
pfefferle authored Nov 6, 2024
1 parent 30662e9 commit 3034df5
Showing 1 changed file with 39 additions and 0 deletions.
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;
}
}

0 comments on commit 3034df5

Please sign in to comment.