函数文档

wp_trash_comment()

💡 云策文档标注

概述

wp_trash_comment() 函数用于将评论移至回收站,若回收站功能被禁用则永久删除评论。它处理评论ID或WP_Comment对象,并返回操作成功与否的布尔值。

关键要点

  • 参数:$comment_id(int|WP_Comment,必需),表示评论ID或WP_Comment对象。
  • 返回值:bool,成功返回true,失败返回false。
  • 功能逻辑:如果EMPTY_TRASH_DAYS未设置(回收站禁用),则调用wp_delete_comment()永久删除评论;否则,将评论状态设为'trash'并更新相关元数据。
  • 特殊处理:对于顶级'note'类型评论,会递归处理其子评论(删除或移至回收站)。
  • 钩子:提供trash_comment(操作前)和trashed_comment(操作后)两个action hook,用于自定义操作。

代码示例

function wp_trash_comment( $comment_id ) {
    if ( ! EMPTY_TRASH_DAYS ) {
        $comment = get_comment( $comment_id );
        $success = wp_delete_comment( $comment_id, true );
        if ( ! $success ) {
            return false;
        }
        // 处理顶级'note'类型评论的子评论
        if ( $comment && 'note' === $comment->comment_type && 0 === (int) $comment->comment_parent ) {
            $children = $comment->get_children( array( 'fields' => 'ids', 'status' => 'all', 'type' => 'note' ) );
            foreach ( $children as $child_id ) {
                if ( ! wp_delete_comment( $child_id, true ) ) {
                    $success = false;
                }
            }
        }
        return $success;
    }
    $comment = get_comment( $comment_id );
    if ( ! $comment ) {
        return false;
    }
    do_action( 'trash_comment', $comment->comment_ID, $comment );
    if ( wp_set_comment_status( $comment, 'trash' ) ) {
        delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
        delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );
        add_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', $comment->comment_approved );
        add_comment_meta( $comment->comment_ID, '_wp_trash_meta_time', time() );
        do_action( 'trashed_comment', $comment->comment_ID, $comment );
        // 处理顶级'note'类型评论的子评论
        if ( 'note' === $comment->comment_type && 0 === (int) $comment->comment_parent ) {
            $children = $comment->get_children( array( 'fields' => 'ids', 'status' => 'all', 'type' => 'note' ) );
            $success = true;
            foreach ( $children as $child_id ) {
                if ( ! wp_trash_comment( $child_id ) ) {
                    $success = false;
                }
            }
            return $success;
        }
        return true;
    }
    return false;
}

注意事项

  • 在WordPress 6.9.0版本中,新增了删除'note'类型评论时同时删除其子评论的功能。
  • 函数自2.9.0版本引入,开发者需注意版本兼容性。
  • 使用前应确保评论存在,否则可能返回false。

📄 原文内容

Moves a comment to the Trash

Description

If Trash is disabled, comment is permanently deleted.

Parameters

$comment_idint|WP_Commentrequired
Comment ID or WP_Comment object.

Return

bool True on success, false on failure.

Source

function wp_trash_comment( $comment_id ) {
	if ( ! EMPTY_TRASH_DAYS ) {
		$comment = get_comment( $comment_id );
		$success = wp_delete_comment( $comment_id, true );

		if ( ! $success ) {
			return false;
		}

		// Also delete children of top level 'note' type comments.
		if ( $comment && 'note' === $comment->comment_type && 0 === (int) $comment->comment_parent ) {
			$children = $comment->get_children(
				array(
					'fields' => 'ids',
					'status' => 'all',
					'type'   => 'note',
				)
			);

			foreach ( $children as $child_id ) {
				if ( ! wp_delete_comment( $child_id, true ) ) {
					$success = false;
				}
			}
		}

		return $success;
	}

	$comment = get_comment( $comment_id );
	if ( ! $comment ) {
		return false;
	}

	/**
	 * Fires immediately before a comment is sent to 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 trashed.
	 */
	do_action( 'trash_comment', $comment->comment_ID, $comment );

	if ( wp_set_comment_status( $comment, 'trash' ) ) {
		delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
		delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );
		add_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', $comment->comment_approved );
		add_comment_meta( $comment->comment_ID, '_wp_trash_meta_time', time() );

		/**
		 * Fires immediately after a comment is sent to 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 trashed comment.
		 */
		do_action( 'trashed_comment', $comment->comment_ID, $comment );

		// For top level 'note' type comments, also trash children.
		if ( 'note' === $comment->comment_type && 0 === (int) $comment->comment_parent ) {
			$children = $comment->get_children(
				array(
					'fields' => 'ids',
					'status' => 'all',
					'type'   => 'note',
				)
			);

			$success = true;
			foreach ( $children as $child_id ) {
				if ( ! wp_trash_comment( $child_id ) ) {
					$success = false;
				}
			}
			return $success;
		}

		return true;
	}

	return false;
}

Hooks

do_action( ‘trashed_comment’, string $comment_id, WP_Comment $comment )

Fires immediately after a comment is sent to Trash.

do_action( ‘trash_comment’, string $comment_id, WP_Comment $comment )

Fires immediately before a comment is sent to the Trash.

Changelog

Version Description
6.9.0 Any child notes are deleted when deleting a note.
2.9.0 Introduced.