wp_ajax_edit_comment()
云策文档标注
概述
wp_ajax_edit_comment() 是一个 WordPress AJAX 处理函数,用于通过 AJAX 请求编辑评论。它验证用户权限、处理评论数据更新,并返回更新后的评论行 HTML 内容。
关键要点
- 函数通过 check_ajax_referer() 验证 AJAX 请求的安全性,防止外部攻击。
- 使用 current_user_can() 检查当前用户是否有编辑评论的权限,否则通过 wp_die() 终止执行。
- 验证评论内容非空,否则提示用户输入评论文本。
- 调用 edit_comment() 更新评论,处理可能的 WP_Error 错误。
- 根据 $_POST 参数确定评论位置和列表表类型(WP_Comments_List_Table 或 WP_Post_Comments_List_Table)。
- 使用 WP_Ajax_Response 对象返回编辑后的评论行数据,包括评论 ID 和 HTML 内容。
代码示例
function wp_ajax_edit_comment() {
check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' );
$comment_id = (int) $_POST['comment_ID'];
if ( ! current_user_can( 'edit_comment', $comment_id ) ) {
wp_die( -1 );
}
if ( '' === $_POST['content'] ) {
wp_die( __( 'Please type your comment text.' ) );
}
if ( isset( $_POST['status'] ) ) {
$_POST['comment_status'] = $_POST['status'];
}
$updated = edit_comment();
if ( is_wp_error( $updated ) ) {
wp_die( $updated->get_error_message() );
}
$position = ( isset( $_POST['position'] ) && (int) $_POST['position'] ) ? (int) $_POST['position'] : '-1';
$checkbox = ( isset( $_POST['checkbox'] ) && '1' === $_POST['checkbox'] ) ? 1 : 0;
$wp_list_table = _get_list_table( $checkbox ? 'WP_Comments_List_Table' : 'WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
$comment = get_comment( $comment_id );
if ( empty( $comment->comment_ID ) ) {
wp_die( -1 );
}
ob_start();
$wp_list_table->single_row( $comment );
$comment_list_item = ob_get_clean();
$x = new WP_Ajax_Response();
$x->add(
array(
'what' => 'edit_comment',
'id' => $comment->comment_ID,
'data' => $comment_list_item,
'position' => $position,
)
);
$x->send();
}注意事项
- 函数依赖于 $_POST 参数,如 comment_ID、content、status、position 和 checkbox,需确保 AJAX 请求正确传递这些数据。
- 使用 wp_die() 处理错误情况,可能中断执行并显示错误页面,需在客户端 AJAX 调用中妥善处理响应。
- 函数自 WordPress 3.1.0 版本引入,兼容性需考虑。
原文内容
Handles editing a comment via AJAX.
Source
function wp_ajax_edit_comment() {
check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' );
$comment_id = (int) $_POST['comment_ID'];
if ( ! current_user_can( 'edit_comment', $comment_id ) ) {
wp_die( -1 );
}
if ( '' === $_POST['content'] ) {
wp_die( __( 'Please type your comment text.' ) );
}
if ( isset( $_POST['status'] ) ) {
$_POST['comment_status'] = $_POST['status'];
}
$updated = edit_comment();
if ( is_wp_error( $updated ) ) {
wp_die( $updated->get_error_message() );
}
$position = ( isset( $_POST['position'] ) && (int) $_POST['position'] ) ? (int) $_POST['position'] : '-1';
/*
* Checkbox is used to differentiate between the Edit Comments screen (1)
* and the Comments section on the Edit Post screen (0).
*/
$checkbox = ( isset( $_POST['checkbox'] ) && '1' === $_POST['checkbox'] ) ? 1 : 0;
$wp_list_table = _get_list_table( $checkbox ? 'WP_Comments_List_Table' : 'WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
$comment = get_comment( $comment_id );
if ( empty( $comment->comment_ID ) ) {
wp_die( -1 );
}
ob_start();
$wp_list_table->single_row( $comment );
$comment_list_item = ob_get_clean();
$x = new WP_Ajax_Response();
$x->add(
array(
'what' => 'edit_comment',
'id' => $comment->comment_ID,
'data' => $comment_list_item,
'position' => $position,
)
);
$x->send();
}
Changelog
| Version | Description |
|---|---|
| 3.1.0 | Introduced. |