wp_set_comment_status()设置修改一个评论的状态

目录

描述

译文

根据编号检索评论的状态

原文

Sets the status of a comment.

The 'wp_set_comment_status' action is called after the comment is handled and will only be called, if the comment status is either 'hold', 'approve', or 'spam'. If the comment status is not in the list, then false is returned and if the status is 'delete', then the comment is deleted without calling the action.

wp_set_comment_status() 描述

用法

<?php wp_set_comment_status$comment_id$comment_status ?>

wp_set_comment_status() 用法

参数

$comment_id

(integer) (必填) Comment ID.

默认值: None

$comment_status

(string) (必填) New comment status, either 'hold', 'approve', 'spam', or 'trash'.

默认值: None

wp_set_comment_status() 参数

返回值

(boolean) 

False on failure or deletion and true on success.

wp_set_comment_status() 返回值

注意

wp_set_comment_status() 注意

历史

  • 添加于 版本 2.9: $comment_status: the parameter 'delete' was replaced with 'trash'. Comments will be added to the Comment Trash instead of being purged from the database.
  • 添加于 版本: 1.0.0

wp_set_comment_status() 历史

源文件

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

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Sets the status of a comment.
 *
 * The 'wp_set_comment_status' action is called after the comment is handled.
 * If the comment status is not in the list, then false is returned.
 *
 * @since 1.0.0
 *
 * global wpdb $wpdb
 *
 * @param int $comment_id Comment ID.
 * @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
 * @param bool $wp_error Whether to return a WP_Error object if there is a failure. Default is false.
 * @return bool|WP_Error True on success, false or WP_Error on failure.
 */
function wp_set_comment_status($comment_id, $comment_status, $wp_error = false) {
	global $wpdb;

	switch ( $comment_status ) {
		case 'hold':
		case '0':
			$status = '0';
			break;
		case 'approve':
		case '1':
			$status = '1';
			if ( get_option('comments_notify') ) {
				wp_notify_postauthor( $comment_id );
			}
			break;
		case 'spam':
			$status = 'spam';
			break;
		case 'trash':
			$status = 'trash';
			break;
		default:
			return false;
	}

	$comment_old = clone get_comment($comment_id);

	if ( !$wpdb->update( $wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_id) ) ) {
		if ( $wp_error )
			return new WP_Error('db_update_error', __('Could not update comment status'), $wpdb->last_error);
		else
			return false;
	}

	clean_comment_cache($comment_id);

	$comment = get_comment($comment_id);

	/**
	 * Fires immediately before transitioning a comment's status from one to another
	 * in the database.
	 *
	 * @since 1.5.0
	 *
	 * @param int         $comment_id     Comment ID.
	 * @param string|bool $comment_status Current comment status. Possible values include
	 *                                    'hold', 'approve', 'spam', 'trash', or false.
	 */
	do_action( 'wp_set_comment_status', $comment_id, $comment_status );

	wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);

	wp_update_comment_count($comment->comment_post_ID);

	return true;
}

wp_set_comment_status() 源文件

相关