设置评论的状态。
原型
wp_set_comment_status( int|WP_Comment $comment_id, string $comment_status, bool $wp_error = false )
描述
在处理注释后调用’wp_set_comment_status’操作。如果注释状态不在列表中,则返回false。
参数
$comment_id
(int|WP_Comment)
(Required)
注释ID或WP_Comment对象。
$comment_status
(string)
(Required)
新评论状态,“持有”,“批准”,“垃圾邮件”或“垃圾”。
$wp_error
(bool)
(Optional)
是否在发生故障时返回WP_Error对象。默认值为false。
返回值
(bool|WP_Error)
成功,虚假或
源文件
路径:wp-includes/comment.php
<?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;
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/wp_set_comment_status/