wp_update_comment() – Updates an existing comment in the database.

You appear to be a bot. Output may be restricted

Description

Updates an existing comment in the database.

Filters the comment and makes sure certain fields are valid before updating.

Usage

$int|false|WP_Error = wp_update_comment( $commentarr, $wp_error );

Parameters

$commentarr
( array ) required – Contains information on the comment.
$wp_error
( bool ) optional – Optional. Whether to return a WP_Error on failure. Default false.

Returns

int|false|WP_Error The value 1 if the comment was updated, 0 if not updated. False or a WP_Error object on failure.

Source

File name: wordpress/wp-includes/comment.php


Lines:

1 to 100 of 157
function wp_update_comment( $commentarr, $wp_error = false ) {
  global $wpdb;

  // First, get all of the original fields.
  $comment = get_comment( $commentarr['comment_ID'], ARRAY_A );

  if ( empty( $comment ) ) {
    if ( $wp_error ) {
      return new WP_Error( 'invalid_comment_id', __( 'Invalid comment ID.' ) );
    } else {
      return false;
    }
  }

  // Make sure that the comment post ID is valid (if specified).
  if ( ! empty( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) {
    if ( $wp_error ) {
      return new WP_Error( 'invalid_post_id', __( 'Invalid post ID.' ) );
    } else {
      return false;
    }
  }

  $filter_comment = false;
  if ( ! has_filter( 'pre_comment_content', 'wp_filter_kses' ) ) {
    $filter_comment = ! user_can( isset( $comment['user_id'] ) ? $comment['user_id'] : 0, 'unfiltered_html' );
  }

  if ( $filter_comment ) {
    add_filter( 'pre_comment_content', 'wp_filter_kses' )  <;
  }

  // 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 );

  if ( $filter_comment ) {
    remove_filter( 'pre_comment_content', 'wp_filter_kses' );
  }

  // 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
 * @since 5.5.0 Returning a WP_Error value from the filter will short-circuit comment update
 *              and allow skipping further processing.
 *
 * @param array|WP_Error $data       The new, processed comment data, or WP_Error.
 * @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 );

  // Do not carry on on failure.
  if ( is_wp_error( $data ) ) {
    if ( $wp_error ) {
      return $data;
    } else {
      return false;
    }
  }

  $keys = array(
    'comment_post_ID',
    'comment_author',

 View on GitHub View on Trac