wp_allow_comment()判断一个评论是否被批准

目录

描述

译文

验证是否可以发布当前评论。

原文

Validates whether this comment is allowed to be made or not.

wp_allow_comment() 描述

用法

<?php wp_allow_comment$commentdata ?>

wp_allow_comment() 用法

参数

$commentdata

(array) (必填) Contains information on the comment

默认值: None

wp_allow_comment() 参数

返回值

(mixed) 

Signifies the approval status (0|1|'spam')

wp_allow_comment() 返回值

注意

wp_allow_comment() 注意

历史

添加于 版本: 2.0.0

wp_allow_comment() 历史

源文件

wp_allow_comment() 函数的代码位于 wp-includes/comment.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Validates whether this comment is allowed to be made.
 *
 * @since 2.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param array $commentdata Contains information on the comment
 * @return int|string Signifies the approval status (0|1|'spam')
 */
function wp_allow_comment( $commentdata ) {
	global $wpdb;

	// Simple duplicate check
	// expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)
	$dupe = $wpdb->prepare(
		"SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = %s AND comment_approved != 'trash' AND ( comment_author = %s ",
		wp_unslash( $commentdata['comment_post_ID'] ),
		wp_unslash( $commentdata['comment_parent'] ),
		wp_unslash( $commentdata['comment_author'] )
	);
	if ( $commentdata['comment_author_email'] ) {
		$dupe .= $wpdb->prepare(
			"OR comment_author_email = %s ",
			wp_unslash( $commentdata['comment_author_email'] )
		);
	}
	$dupe .= $wpdb->prepare(
		") AND comment_content = %s LIMIT 1",
		wp_unslash( $commentdata['comment_content'] )
	);
	if ( $wpdb->get_var( $dupe ) ) {
		/**
		 * Fires immediately after a duplicate comment is detected.
		 *
		 * @since 3.0.0
		 *
		 * @param array $commentdata Comment data.
		 */
		do_action( 'comment_duplicate_trigger', $commentdata );
		if ( defined( 'DOING_AJAX' ) ) {
			die( __('Duplicate comment detected; it looks as though you’ve already said that!') );
		}
		wp_die( __( 'Duplicate comment detected; it looks as though you’ve already said that!' ), 409 );
	}

	/**
	 * Fires immediately before a comment is marked approved.
	 *
	 * Allows checking for comment flooding.
	 *
	 * @since 2.3.0
	 *
	 * @param string $comment_author_IP    Comment author's IP address.
	 * @param string $comment_author_email Comment author's email.
	 * @param string $comment_date_gmt     GMT date the comment was posted.
	 */
	do_action(
		'check_comment_flood',
		$commentdata['comment_author_IP'],
		$commentdata['comment_author_email'],
		$commentdata['comment_date_gmt']
	);

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

	if ( isset( $user ) && ( $commentdata['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(
			$commentdata['comment_author'],
			$commentdata['comment_author_email'],
			$commentdata['comment_author_url'],
			$commentdata['comment_content'],
			$commentdata['comment_author_IP'],
			$commentdata['comment_agent'],
			$commentdata['comment_type']
		) ) {
			$approved = 1;
		} else {
			$approved = 0;
		}

		if ( wp_blacklist_check(
			$commentdata['comment_author'],
			$commentdata['comment_author_email'],
			$commentdata['comment_author_url'],
			$commentdata['comment_content'],
			$commentdata['comment_author_IP'],
			$commentdata['comment_agent']
		) ) {
			$approved = 'spam';
		}
	}

	/**
	 * Filter a comment's approval status before it is set.
	 *
	 * @since 2.1.0
	 *
	 * @param bool|string $approved    The approval status. Accepts 1, 0, or 'spam'.
	 * @param array       $commentdata Comment data.
	 */
	$approved = apply_filters( 'pre_comment_approved', $approved, $commentdata );
	return $approved;
}

wp_allow_comment() 源文件