函数文档

wp_ajax_get_comments()

💡 云策文档标注

概述

wp_ajax_get_comments() 是一个 WordPress AJAX 处理函数,用于获取评论数据并返回给前端。它主要用于后台管理界面,通过 AJAX 请求动态加载评论列表。

关键要点

  • 函数处理 AJAX 请求以获取评论,参数 $action 指定操作,默认为 'get-comments'。
  • 通过 check_ajax_referer() 验证 AJAX 请求安全性,防止外部攻击。
  • 需要 post_id 参数(可通过 $_REQUEST['p'] 传递),否则调用 wp_die(-1) 终止执行。
  • 使用 _get_list_table() 获取 WP_Post_Comments_List_Table 实例,并检查当前用户是否有 edit_post 权限。
  • 调用 prepare_items() 准备评论数据,若无评论则返回 wp_die(1)。
  • 通过 WP_Ajax_Response 对象返回评论列表 HTML,仅包含用户有 edit_comment 权限或已批准的评论。

代码示例

function wp_ajax_get_comments( $action ) {
    global $post_id;

    if ( empty( $action ) ) {
        $action = 'get-comments';
    }

    check_ajax_referer( $action );

    if ( empty( $post_id ) && ! empty( $_REQUEST['p'] ) ) {
        $id = absint( $_REQUEST['p'] );
        if ( ! empty( $id ) ) {
            $post_id = $id;
        }
    }

    if ( empty( $post_id ) ) {
        wp_die( -1 );
    }

    $wp_list_table = _get_list_table( 'WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) );

    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        wp_die( -1 );
    }

    $wp_list_table->prepare_items();

    if ( ! $wp_list_table->has_items() ) {
        wp_die( 1 );
    }

    $x = new WP_Ajax_Response();

    ob_start();
    foreach ( $wp_list_table->items as $comment ) {
        if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) && 0 === $comment->comment_approved ) {
            continue;
        }
        get_comment( $comment );
        $wp_list_table->single_row( $comment );
    }
    $comment_list_item = ob_get_clean();

    $x->add(
        array(
            'what' => 'comments',
            'data' => $comment_list_item,
        )
    );

    $x->send();
}

注意事项

  • 函数依赖于全局变量 $post_id,需确保在调用前已设置或通过 $_REQUEST['p'] 传递。
  • 权限检查严格:需要 edit_post 权限访问帖子,以及 edit_comment 权限处理未批准评论。
  • 使用 ob_start() 和 ob_get_clean() 捕获输出,避免直接输出 HTML。
  • 相关函数包括 WP_List_Table 方法、check_ajax_referer()、absint() 等,需熟悉其用法。

📄 原文内容

Handles getting comments via AJAX.

Parameters

$actionstringrequired
Action to perform.

Source

function wp_ajax_get_comments( $action ) {
	global $post_id;

	if ( empty( $action ) ) {
		$action = 'get-comments';
	}

	check_ajax_referer( $action );

	if ( empty( $post_id ) && ! empty( $_REQUEST['p'] ) ) {
		$id = absint( $_REQUEST['p'] );
		if ( ! empty( $id ) ) {
			$post_id = $id;
		}
	}

	if ( empty( $post_id ) ) {
		wp_die( -1 );
	}

	$wp_list_table = _get_list_table( 'WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) );

	if ( ! current_user_can( 'edit_post', $post_id ) ) {
		wp_die( -1 );
	}

	$wp_list_table->prepare_items();

	if ( ! $wp_list_table->has_items() ) {
		wp_die( 1 );
	}

	$x = new WP_Ajax_Response();

	ob_start();
	foreach ( $wp_list_table->items as $comment ) {
		if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) && 0 === $comment->comment_approved ) {
			continue;
		}
		get_comment( $comment );
		$wp_list_table->single_row( $comment );
	}
	$comment_list_item = ob_get_clean();

	$x->add(
		array(
			'what' => 'comments',
			'data' => $comment_list_item,
		)
	);

	$x->send();
}

Changelog

Version Description
3.1.0 Introduced.