函数文档

wp_ajax_dim_comment()

💡 云策文档标注

概述

wp_ajax_dim_comment() 是一个 WordPress AJAX 处理函数,用于通过 AJAX 请求切换评论的显示状态(如“暗淡”或“批准”)。它验证用户权限、检查评论状态,并调用相关函数更新评论状态,同时处理错误和响应。

关键要点

  • 函数通过 AJAX 处理评论的暗淡操作,涉及状态切换(如从“未批准”或“垃圾”切换到“批准”,反之亦然)。
  • 验证用户权限:需要 edit_comment 或 moderate_comments 能力。
  • 使用 check_ajax_referer() 进行安全验证,防止外部请求。
  • 调用 wp_set_comment_status() 更新评论状态,并处理可能的 WP_Error。
  • 最终通过 _wp_ajax_delete_comment_response() 发送响应,包括评论总数和页面链接更新。

代码示例

function wp_ajax_dim_comment() {
	$id      = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0;
	$comment = get_comment( $id );

	if ( ! $comment ) {
		$x = new WP_Ajax_Response(
			array(
				'what' => 'comment',
				'id'   => new WP_Error(
					'invalid_comment',
					sprintf( __( 'Comment %d does not exist' ), $id )
				),
			)
		);
		$x->send();
	}

	if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) && ! current_user_can( 'moderate_comments' ) ) {
		wp_die( -1 );
	}

	$current = wp_get_comment_status( $comment );

	if ( isset( $_POST['new'] ) && $_POST['new'] === $current ) {
		wp_die( time() );
	}

	check_ajax_referer( "approve-comment_$id" );

	if ( in_array( $current, array( 'unapproved', 'spam' ), true ) ) {
		$result = wp_set_comment_status( $comment, 'approve', true );
	} else {
		$result = wp_set_comment_status( $comment, 'hold', true );
	}

	if ( is_wp_error( $result ) ) {
		$x = new WP_Ajax_Response(
			array(
				'what' => 'comment',
				'id'   => $result,
			)
		);
		$x->send();
	}

	_wp_ajax_delete_comment_response( $comment->comment_ID );
	wp_die( 0 );
}

注意事项

  • 函数依赖于 POST 参数 id 和 new 来识别评论和目标状态。
  • 错误处理使用 WP_Ajax_Response 和 WP_Error 来返回结构化错误信息。
  • 在状态未改变时,会通过 wp_die(time()) 返回时间戳以避免不必要的操作。
  • 相关函数包括 wp_set_comment_status、wp_get_comment_status、check_ajax_referer 等,需确保这些函数在上下文中可用。

📄 原文内容

Handles dimming a comment via AJAX.

Source

function wp_ajax_dim_comment() {
	$id      = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0;
	$comment = get_comment( $id );

	if ( ! $comment ) {
		$x = new WP_Ajax_Response(
			array(
				'what' => 'comment',
				'id'   => new WP_Error(
					'invalid_comment',
					/* translators: %d: Comment ID. */
					sprintf( __( 'Comment %d does not exist' ), $id )
				),
			)
		);
		$x->send();
	}

	if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) && ! current_user_can( 'moderate_comments' ) ) {
		wp_die( -1 );
	}

	$current = wp_get_comment_status( $comment );

	if ( isset( $_POST['new'] ) && $_POST['new'] === $current ) {
		wp_die( time() );
	}

	check_ajax_referer( "approve-comment_$id" );

	if ( in_array( $current, array( 'unapproved', 'spam' ), true ) ) {
		$result = wp_set_comment_status( $comment, 'approve', true );
	} else {
		$result = wp_set_comment_status( $comment, 'hold', true );
	}

	if ( is_wp_error( $result ) ) {
		$x = new WP_Ajax_Response(
			array(
				'what' => 'comment',
				'id'   => $result,
			)
		);
		$x->send();
	}

	// Decide if we need to send back '1' or a more complicated response including page links and comment counts.
	_wp_ajax_delete_comment_response( $comment->comment_ID );
	wp_die( 0 );
}

Changelog

Version Description
3.1.0 Introduced.