更新数据库中的现有注释。

原型

wp_update_comment( array $commentarr )

描述

过滤评论并确保在更新前某些字段有效。

参数

$commentarr (array) (Required) 包含评论的信息。

返回值

(int)  如果值为1,则更新注释,如果值为0,则注释未更新。

源文件

路径:wp-includes/comment.php

<?php
...
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 ( ! empty( $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 );
	/**
	 * Filters 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'];
	/**
	 * Filters the comment data immediately before it is updated in the database.
	 *
	 * Note: data being passed to the filter is already unslashed.
	 *
	 * @since 4.7.0
	 *
	 * @param array $data       The new, processed comment data.
	 * @param array $comment    The old, unslashed comment data.
	 * @param array $commentarr The new, raw comment data.
	 */
	$data = apply_filters( 'wp_update_comment_data', $data, $comment, $commentarr );
	$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', 'comment_agent', 'comment_author_IP' );
	$data = wp_array_slice_assoc( $data, $keys );
	$rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) );
	// If metadata is provided, store it.
	if ( isset( $commentarr['comment_meta'] ) && is_array( $commentarr['comment_meta'] ) ) {
		foreach ( $commentarr['comment_meta'] as $meta_key => $meta_value ) {
			update_comment_meta( $comment_ID, $meta_key, $meta_value );
		}
	}
	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
	 * @since 4.6.0 Added the `$data` parameter.
	 *
	 * @param int   $comment_ID The comment ID.
	 * @param array $data       Comment data.
	 */
	do_action( 'edit_comment', $comment_ID, $data );
	$comment = get_comment($comment_ID);
	wp_transition_comment_status($comment->comment_approved, $old_status, $comment);
	return $rval;
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/wp_update_comment/