wp_check_comment_data()WP 6.7.0

Checks whether comment data passes internal checks or has disallowed content.

Хуки из функции

Возвращает

int|Строку|WP_Error. The approval status on success (0|1|'spam'|'trash'), WP_Error otherwise.

Использование

wp_check_comment_data( $comment_data );
$comment_data(массив) (обязательный)
Array of arguments for inserting a comment.

Заметки

  • Global. wpdb. $wpdb WordPress database abstraction object.

Список изменений

С версии 6.7.0 Введена.

Код wp_check_comment_data() WP 6.7.1

function wp_check_comment_data( $comment_data ) {
	global $wpdb;

	if ( ! empty( $comment_data['user_id'] ) ) {
		$user        = get_userdata( $comment_data['user_id'] );
		$post_author = $wpdb->get_var(
			$wpdb->prepare(
				"SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1",
				$comment_data['comment_post_ID']
			)
		);
	}

	if ( isset( $user ) && ( $comment_data['user_id'] == $post_author || $user->has_cap( 'moderate_comments' ) ) ) {
		// The author and the admins get respect.
		$approved = 1;
	} else {
		// Everyone else's comments will be checked.
		if ( check_comment(
			$comment_data['comment_author'],
			$comment_data['comment_author_email'],
			$comment_data['comment_author_url'],
			$comment_data['comment_content'],
			$comment_data['comment_author_IP'],
			$comment_data['comment_agent'],
			$comment_data['comment_type']
		) ) {
			$approved = 1;
		} else {
			$approved = 0;
		}

		if ( wp_check_comment_disallowed_list(
			$comment_data['comment_author'],
			$comment_data['comment_author_email'],
			$comment_data['comment_author_url'],
			$comment_data['comment_content'],
			$comment_data['comment_author_IP'],
			$comment_data['comment_agent']
		) ) {
			$approved = EMPTY_TRASH_DAYS ? 'trash' : 'spam';
		}
	}

	/**
	 * Filters a comment's approval status before it is set.
	 *
	 * @since 2.1.0
	 * @since 4.9.0 Returning a WP_Error value from the filter will short-circuit comment insertion
	 *              and allow skipping further processing.
	 *
	 * @param int|string|WP_Error $approved    The approval status. Accepts 1, 0, 'spam', 'trash',
	 *                                         or WP_Error.
	 * @param array               $commentdata Comment data.
	 */
	return apply_filters( 'pre_comment_approved', $approved, $comment_data );
}