wp_update_comment( array $commentarr )
更新数据库中的现有注释。
Updates an existing comment in the database.
说明(Description)
过滤注释并确保某些字段在更新前有效。
参数(Parameters)
参数 | 类型 | 说明 |
---|---|---|
$commentarr | (array) | 包含有关注释的信息。 |
源码(Source)
/**
* Updates an existing comment in the database.
*
* Filters the comment and makes sure certain fields are valid before updating.
*
* @since 2.0.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param array $commentarr Contains information on the comment.
* @return int Comment was updated if value is 1, or was not updated if value is 0.
*/
function wp_update_comment($commentarr) {
global $wpdb;
// First, get all of the original fields
$comment = get_comment($commentarr['comment_ID'], ARRAY_A);
if ( empty( $comment ) ) {
return 0;
}
// Make sure that the comment post ID is valid (if specified).
if ( isset( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) {
return 0;
}
// Escape data pulled from DB.
$comment = wp_slash($comment);
$old_status = $comment['comment_approved'];
// Merge old and new fields with new fields overwriting old ones.
$commentarr = array_merge($comment, $commentarr);
$commentarr = wp_filter_comment( $commentarr );
// Now extract the merged array.
$data = wp_unslash( $commentarr );
/**
* Filter the comment content before it is updated in the database.
*
* @since 1.5.0
*
* @param string $comment_content The comment data.
*/
$data['comment_content'] = apply_filters( 'comment_save_pre', $data['comment_content'] );
$data['comment_date_gmt'] = get_gmt_from_date( $data['comment_date'] );
if ( ! isset( $data['comment_approved'] ) ) {
$data['comment_approved'] = 1;
} elseif ( 'hold' == $data['comment_approved'] ) {
$data['comment_approved'] = 0;
} elseif ( 'approve' == $data['comment_approved'] ) {
$data['comment_approved'] = 1;
}
$comment_ID = $data['comment_ID'];
$comment_post_ID = $data['comment_post_ID'];
$keys = array( 'comment_post_ID', 'comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_type', 'comment_parent', 'user_id' );
$data = wp_array_slice_assoc( $data, $keys );
$rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) );
clean_comment_cache( $comment_ID );
wp_update_comment_count( $comment_post_ID );
/**
* Fires immediately after a comment is updated in the database.
*
* The hook also fires immediately before comment status transition hooks are fired.
*
* @since 1.2.0
*
* @param int $comment_ID The comment ID.
*/
do_action( 'edit_comment', $comment_ID );
$comment = get_comment($comment_ID);
wp_transition_comment_status($comment->comment_approved, $old_status, $comment);
return $rval;
}
更新版本 | 源码位置 | 使用 | 被使用 |
---|---|---|---|
4.9.0 | wp-includes/comment.php | 15 | 20 |