wp_unspam_comment()
云策文档标注
概述
wp_unspam_comment() 函数用于将评论从垃圾评论状态移除,恢复其原始状态。它接受评论 ID 或 WP_Comment 对象作为参数,并返回操作成功与否的布尔值。
关键要点
- 参数:$comment_id(int|WP_Comment,必需),可以是评论 ID 或 WP_Comment 对象。
- 返回值:bool,成功返回 true,失败返回 false。
- 内部流程:通过 get_comment() 获取评论对象,检查有效性后触发 unspam_comment 钩子,恢复评论状态并清理相关元数据。
- 钩子:提供 unspam_comment(操作前)和 unspammed_comment(操作后)两个动作钩子,便于开发者介入。
- 相关函数:包括 wp_set_comment_status()、get_comment_meta()、delete_comment_meta() 等,用于状态设置和元数据管理。
- 版本历史:自 WordPress 2.9.0 版本引入。
代码示例
function wp_unspam_comment( $comment_id ) {
$comment = get_comment( $comment_id );
if ( ! $comment ) {
return false;
}
do_action( 'unspam_comment', $comment->comment_ID, $comment );
$status = (string) get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true );
if ( empty( $status ) ) {
$status = '0';
}
if ( wp_set_comment_status( $comment, $status ) ) {
delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );
do_action( 'unspammed_comment', $comment->comment_ID, $comment );
return true;
}
return false;
}注意事项
- 函数会检查评论是否存在,若无效则直接返回 false。
- 在恢复评论状态前,会尝试从 _wp_trash_meta_status 元数据中获取原始状态,默认为 '0'(即未批准状态)。
- 操作成功后,会删除与垃圾状态相关的元数据 _wp_trash_meta_status 和 _wp_trash_meta_time。
- 钩子参数包括评论 ID(字符串)和 WP_Comment 对象,便于在动作中访问评论详情。
原文内容
Removes a comment from the Spam.
Parameters
$comment_idint|WP_Commentrequired-
Comment ID or WP_Comment object.
Source
function wp_unspam_comment( $comment_id ) {
$comment = get_comment( $comment_id );
if ( ! $comment ) {
return false;
}
/**
* Fires immediately before a comment is unmarked as Spam.
*
* @since 2.9.0
* @since 4.9.0 Added the `$comment` parameter.
*
* @param string $comment_id The comment ID as a numeric string.
* @param WP_Comment $comment The comment to be unmarked as spam.
*/
do_action( 'unspam_comment', $comment->comment_ID, $comment );
$status = (string) get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true );
if ( empty( $status ) ) {
$status = '0';
}
if ( wp_set_comment_status( $comment, $status ) ) {
delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );
/**
* Fires immediately after a comment is unmarked as Spam.
*
* @since 2.9.0
* @since 4.9.0 Added the `$comment` parameter.
*
* @param string $comment_id The comment ID as a numeric string.
* @param WP_Comment $comment The comment unmarked as spam.
*/
do_action( 'unspammed_comment', $comment->comment_ID, $comment );
return true;
}
return false;
}
Hooks
- do_action( ‘unspammed_comment’, string $comment_id, WP_Comment $comment )
-
Fires immediately after a comment is unmarked as Spam.
- do_action( ‘unspam_comment’, string $comment_id, WP_Comment $comment )
-
Fires immediately before a comment is unmarked as Spam.
Changelog
| Version | Description |
|---|---|
| 2.9.0 | Introduced. |