函数文档

wp_transition_comment_status()

💡 云策文档标注

概述

wp_transition_comment_status() 函数用于在评论状态发生转换时触发相关 Hook。它处理状态转换逻辑,包括状态映射和 Hook 调用,是 WordPress 评论系统状态管理的核心函数。

关键要点

  • 函数在评论状态转换时调用,参数包括新状态、旧状态和评论对象。
  • 内部将原始状态(如 0、'hold')映射为可读格式(如 'unapproved'、'approved')。
  • 如果新旧状态不同,会触发 'transition_comment_status' 和 'comment_{$old_status}_to_{$new_status}' 两个 Hook。
  • 无论状态是否相同,都会触发 'comment_{$new_status}_{$comment->comment_type}' Hook。
  • 相关函数包括 wp_set_comment_status()、wp_update_comment() 和 wp_delete_comment()。

代码示例

function wp_transition_comment_status( $new_status, $old_status, $comment ) {
    $comment_statuses = array(
        0         => 'unapproved',
        'hold'    => 'unapproved',
        1         => 'approved',
        'approve' => 'approved',
    );
    if ( isset( $comment_statuses[ $new_status ] ) ) {
        $new_status = $comment_statuses[ $new_status ];
    }
    if ( isset( $comment_statuses[ $old_status ] ) ) {
        $old_status = $comment_statuses[ $old_status ];
    }

    if ( $new_status !== $old_status ) {
        do_action( 'transition_comment_status', $new_status, $old_status, $comment );
        do_action( "comment_{$old_status}_to_{$new_status}", $comment );
    }
    do_action( "comment_{$new_status}_{$comment->comment_type}", $comment->comment_ID, $comment );
}

注意事项

  • 状态映射确保 Hook 名称一致性,例如将 0 或 'hold' 转换为 'unapproved'。
  • Hook 名称是动态构建的,开发者需注意可能的组合,如 'comment_unapproved_to_approved'。
  • 该函数自 WordPress 2.7.0 引入,是评论状态转换的标准处理方式。

📄 原文内容

Calls hooks for when a comment status transition occurs.

Description

Calls hooks for comment status transitions. If the new comment status is not the same as the previous comment status, then two hooks will be ran, the first is ‘transition_comment_status’ with new status, old status, and comment data.
The next action called is ‘comment_$old_status_to_$new_status’. It has the comment data.

The final action will run whether or not the comment statuses are the same.
The action is named ‘comment_$new_status_$comment->comment_type’.

Parameters

$new_statusstringrequired
New comment status.
$old_statusstringrequired
Previous comment status.
$commentWP_Commentrequired
Comment object.

Source

function wp_transition_comment_status( $new_status, $old_status, $comment ) {
	/*
	 * Translate raw statuses to human-readable formats for the hooks.
	 * This is not a complete list of comment status, it's only the ones
	 * that need to be renamed.
	 */
	$comment_statuses = array(
		0         => 'unapproved',
		'hold'    => 'unapproved', // wp_set_comment_status() uses "hold".
		1         => 'approved',
		'approve' => 'approved',   // wp_set_comment_status() uses "approve".
	);
	if ( isset( $comment_statuses[ $new_status ] ) ) {
		$new_status = $comment_statuses[ $new_status ];
	}
	if ( isset( $comment_statuses[ $old_status ] ) ) {
		$old_status = $comment_statuses[ $old_status ];
	}

	// Call the hooks.
	if ( $new_status !== $old_status ) {
		/**
		 * Fires when the comment status is in transition.
		 *
		 * @since 2.7.0
		 *
		 * @param string     $new_status The new comment status.
		 * @param string     $old_status The old comment status.
		 * @param WP_Comment $comment    Comment object.
		 */
		do_action( 'transition_comment_status', $new_status, $old_status, $comment );

		/**
		 * Fires when the comment status is in transition from one specific status to another.
		 *
		 * The dynamic portions of the hook name, `$old_status`, and `$new_status`,
		 * refer to the old and new comment statuses, respectively.
		 *
		 * Possible hook names include:
		 *
		 *  - `comment_unapproved_to_approved`
		 *  - `comment_spam_to_approved`
		 *  - `comment_approved_to_unapproved`
		 *  - `comment_spam_to_unapproved`
		 *  - `comment_unapproved_to_spam`
		 *  - `comment_approved_to_spam`
		 *
		 * @since 2.7.0
		 *
		 * @param WP_Comment $comment Comment object.
		 */
		do_action( "comment_{$old_status}_to_{$new_status}", $comment );
	}
	/**
	 * Fires when the status of a specific comment type is in transition.
	 *
	 * The dynamic portions of the hook name, `$new_status`, and `$comment->comment_type`,
	 * refer to the new comment status, and the type of comment, respectively.
	 *
	 * Typical comment types include 'comment', 'pingback', or 'trackback'.
	 *
	 * Possible hook names include:
	 *
	 *  - `comment_approved_comment`
	 *  - `comment_approved_pingback`
	 *  - `comment_approved_trackback`
	 *  - `comment_unapproved_comment`
	 *  - `comment_unapproved_pingback`
	 *  - `comment_unapproved_trackback`
	 *  - `comment_spam_comment`
	 *  - `comment_spam_pingback`
	 *  - `comment_spam_trackback`
	 *
	 * @since 2.7.0
	 *
	 * @param string     $comment_id The comment ID as a numeric string.
	 * @param WP_Comment $comment    Comment object.
	 */
	do_action( "comment_{$new_status}_{$comment->comment_type}", $comment->comment_ID, $comment );
}

Hooks

do_action( “comment_{$new_status}_{$comment->comment_type}”, string $comment_id, WP_Comment $comment )

Fires when the status of a specific comment type is in transition.

do_action( “comment_{$old_status}_to_{$new_status}”, WP_Comment $comment )

Fires when the comment status is in transition from one specific status to another.

do_action( ‘transition_comment_status’, string $new_status, string $old_status, WP_Comment $comment )

Fires when the comment status is in transition.

Changelog

Version Description
2.7.0 Introduced.