wp_untrash_comment()
云策文档标注
概述
wp_untrash_comment() 函数用于将评论从回收站中恢复。它接受评论 ID 或 WP_Comment 对象作为参数,返回布尔值表示操作成功与否。
关键要点
- 参数:$comment_id,类型为 int 或 WP_Comment,必需,指定要恢复的评论。
- 返回值:bool,成功返回 true,失败返回 false。
- 内部流程:函数首先获取评论对象,然后触发 'untrash_comment' 钩子,恢复评论状态,并清理相关元数据。
- 钩子:提供 'untrash_comment'(恢复前触发)和 'untrashed_comment'(恢复后触发)两个动作钩子。
代码示例
function wp_untrash_comment( $comment_id ) {
$comment = get_comment( $comment_id );
if ( ! $comment ) {
return false;
}
do_action( 'untrash_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_time' );
delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
do_action( 'untrashed_comment', $comment->comment_ID, $comment );
return true;
}
return false;
}注意事项
- 函数在恢复评论前会检查评论是否存在,不存在则返回 false。
- 恢复评论时,会使用存储在 _wp_trash_meta_status 中的原始状态,若未找到则默认设为 '0'(待审核)。
- 相关函数包括 wp_set_comment_status()、get_comment_meta()、delete_comment_meta() 等,用于状态设置和元数据操作。
- 自 WordPress 2.9.0 版本引入,是评论管理功能的一部分。
原文内容
Removes a comment from the Trash
Parameters
$comment_idint|WP_Commentrequired-
Comment ID or WP_Comment object.
Source
function wp_untrash_comment( $comment_id ) {
$comment = get_comment( $comment_id );
if ( ! $comment ) {
return false;
}
/**
* Fires immediately before a comment is restored from the Trash.
*
* @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 untrashed.
*/
do_action( 'untrash_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_time' );
delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
/**
* Fires immediately after a comment is restored from the Trash.
*
* @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 untrashed comment.
*/
do_action( 'untrashed_comment', $comment->comment_ID, $comment );
return true;
}
return false;
}
Hooks
- do_action( ‘untrashed_comment’, string $comment_id, WP_Comment $comment )
-
Fires immediately after a comment is restored from the Trash.
- do_action( ‘untrash_comment’, string $comment_id, WP_Comment $comment )
-
Fires immediately before a comment is restored from the Trash.
Changelog
| Version | Description |
|---|---|
| 2.9.0 | Introduced. |