返回介绍

wp_set_comment_status()

发布于 2017-09-11 12:50:54 字数 4158 浏览 1120 评论 0 收藏 0

wp_set_comment_status( int|WP_Comment $comment_id,  string $comment_status,  bool $wp_error = false )

Sets the status of a comment.


description

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.


参数

$comment_id

(int|WP_Comment) (Required) Comment ID or WP_Comment object.

$comment_status

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

$wp_error

(bool) (Optional) Whether to return a WP_Error object if there is a failure. Default is false.

Default value: false


返回值

(bool|WP_Error) True on success, false or WP_Error on failure.


源代码

File: wp-includes/comment.php

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';
			add_action( 'wp_set_comment_status', 'wp_new_comment_notify_postauthor' );
			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_old->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_old->comment_ID );

	$comment = get_comment( $comment_old->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->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;
}

更新日志

Versiondescription
1.0.0Introduced.

相关函数

Uses

  • wp-includes/comment.php: wp_set_comment_status
  • wp-includes/l10n.php: __()
  • wp-includes/plugin.php: add_action()
  • wp-includes/plugin.php: do_action()
  • wp-includes/wp-db.php: wpdb::update()
  • wp-includes/comment.php: clean_comment_cache()
  • wp-includes/comment.php: wp_update_comment_count()
  • wp-includes/comment.php: wp_transition_comment_status()
  • wp-includes/comment.php: get_comment()
  • wp-includes/class-wp-error.php: WP_Error::__construct()
  • Show 5 more uses Hide more uses

Used By

  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::handle_status_param()
  • wp-admin/includes/ajax-actions.php: wp_ajax_dim_comment()
  • wp-admin/includes/ajax-actions.php: wp_ajax_replyto_comment()
  • wp-includes/comment.php: wp_spam_comment()
  • wp-includes/comment.php: wp_unspam_comment()
  • wp-includes/comment.php: wp_trash_comment()
  • wp-includes/comment.php: wp_untrash_comment()
  • Show 2 more used by Hide more used by

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文