* tag, for logged-in and logged-out visitors alike. We echo hidden * fields and mark the page. * - filter `pre_comment_approved` ( int|string|WP_Error $approved, array $commentdata ) * runs inside wp_allow_comment() for every new comment (form, REST, * XML-RPC). Return 'spam' to spam it; returning a WP_Error whose data * is an int status code makes wp-comments-post.php show the message * and the REST API return the error (supported since WP 4.7). * queue -> 'spam'; block -> WP_Error( 403, block message ). The REST * API needs the status as array( 'status' => 403 ), see block_error(). * Since WordPress 6.7 wp_new_comment() applies this filter twice for a * comment that is not spam (wp_allow_comment(), then wp_check_comment_data() * again after the content filters), so check() scores each comment once * per request and passes the second call through. * * Pingbacks, trackbacks and comments by users who can moderate comments * are left alone. Disable entirely with: * add_filter( 'agentlane_protect_comments', '__return_false' ); * * @package AgentLane */ namespace AgentLane\Adapters; use AgentLane\Router; if ( ! defined( 'ABSPATH' ) ) { exit; } require_once __DIR__ . '/interface-adapter.php'; /** * Class AdapterComments */ class AdapterComments implements AdapterInterface { /** * Comments already scored in this request (see comment_key()). * * @var array */ private static $scored = array(); /** * {@inheritDoc} */ public function id(): string { return 'comments'; } /** * {@inheritDoc} */ public function register(): void { if ( ! function_exists( 'comment_form' ) || ! function_exists( 'wp_allow_comment' ) ) { return; } add_action( 'comment_form', array( $this, 'inject_fields' ), 10, 1 ); add_filter( 'pre_comment_approved', array( $this, 'check' ), 20, 2 ); } /** * Echo hidden fields inside the comment form and mark the page. * * @param mixed $post_id Post id (unused). * @return void */ public function inject_fields( $post_id = 0 ) { /** * Filter whether the core comment form is protected. * * @param bool $protect Default true. * @param array $commentdata Empty on render. */ if ( ! apply_filters( 'agentlane_protect_comments', true, array() ) ) { return; } Router::mark_page_has_form(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- markup is escaped inside Signals::render_hidden_fields(). echo Router::hidden_fields(); } /** * Score the comment and translate the verdict into a comment status. * * @param mixed $approved Current status (1, 0, 'spam', 'trash' or WP_Error). * @param mixed $commentdata Slashed comment data. * @return mixed */ public function check( $approved, $commentdata = array() ) { if ( is_wp_error( $approved ) || 'spam' === $approved || 'trash' === $approved ) { return $approved; } if ( ! is_array( $commentdata ) ) { return $approved; } $type = (string) ( $commentdata['comment_type'] ?? 'comment' ); if ( '' !== $type && 'comment' !== $type ) { return $approved; } $user_id = (int) ( $commentdata['user_id'] ?? 0 ); if ( $user_id > 0 && user_can( $user_id, 'moderate_comments' ) ) { return $approved; } /** This filter is documented in includes/adapters/class-adapter-comments.php */ if ( ! apply_filters( 'agentlane_protect_comments', true, $commentdata ) ) { return $approved; } // Second pass of the same comment in this request (WordPress 6.7+): already scored // and allowed, otherwise WordPress would not ask again. Scoring it twice would store // two rows and add the duplicate_text weight to every delivered comment. $key = self::comment_key( $commentdata ); if ( isset( self::$scored[ $key ] ) ) { return $approved; } self::$scored[ $key ] = true; $fields = array( 'author' => wp_unslash( (string) ( $commentdata['comment_author'] ?? '' ) ), 'email' => wp_unslash( (string) ( $commentdata['comment_author_email'] ?? '' ) ), 'url' => wp_unslash( (string) ( $commentdata['comment_author_url'] ?? '' ) ), 'comment' => wp_unslash( (string) ( $commentdata['comment_content'] ?? '' ) ), ); $post_id = (string) (int) ( $commentdata['comment_post_ID'] ?? 0 ); $verdict = Router::handle( 'comments', $post_id, $fields ); if ( null === $verdict || 'allow' === $verdict->action ) { return $approved; } if ( 'block' === $verdict->action ) { return self::block_error( self::is_rest_request() ); } return 'spam'; } /** * Error returned for a blocked comment. * * wp-comments-post.php reads the error data as an int HTTP status and prints the * message as HTML. The REST API only honours array( 'status' => int ), answers 500 * for anything else, and sends the message as JSON text. So the shape of the * error depends on the entry point. * * @param bool $rest True when the comment arrives through the REST API. * @return \WP_Error */ public static function block_error( bool $rest ): \WP_Error { if ( $rest ) { return new \WP_Error( 'agentlane_blocked', Router::block_message(), array( 'status' => 403 ) ); } return new \WP_Error( 'agentlane_blocked', esc_html( Router::block_message() ), 403 ); } /** * Is the current request served by the REST API? * * @return bool */ private static function is_rest_request(): bool { if ( function_exists( 'wp_is_serving_rest_request' ) ) { return (bool) wp_is_serving_rest_request(); // WordPress 6.5+. } return defined( 'REST_REQUEST' ) && REST_REQUEST; } /** * Identity of one comment within a request. Built only from values that the * comment content filters between the two passes leave alone: post, parent, * type, the timestamp wp_new_comment() assigned, the user and the email. * Author name, content, IP and user agent are left out on purpose, because * filters (kses, privacy plugins that blank the IP) may change them. * * @param array $commentdata Slashed comment data. * @return string */ private static function comment_key( array $commentdata ): string { $email = strtolower( trim( (string) wp_unslash( (string) ( $commentdata['comment_author_email'] ?? '' ) ) ) ); return md5( implode( '|', array( (string) (int) ( $commentdata['comment_post_ID'] ?? 0 ), (string) (int) ( $commentdata['comment_parent'] ?? 0 ), (string) ( $commentdata['comment_type'] ?? '' ), (string) ( $commentdata['comment_date_gmt'] ?? '' ), (string) (int) ( $commentdata['user_id'] ?? 0 ), $email, ) ) ); } }