/** * 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; }
/** * Updates an existing comment in the database. * * Filters the comment and makes sure certain fields are valid before updating. * * @since 2.0.0 * @uses $wpdb * @uses wp_transition_comment_status() Passes new and old comment status along with $comment 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); // Escape data pulled from DB. foreach ((array) $comment as $key => $value) { $comment[$key] = $wpdb->escape($value); } // 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. extract(stripslashes_deep($commentarr), EXTR_SKIP); $comment_content = apply_filters('comment_save_pre', $comment_content); $comment_date_gmt = get_gmt_from_date($comment_date); if (!isset($comment_approved)) { $comment_approved = 1; } else { if ('hold' == $comment_approved) { $comment_approved = 0; } else { if ('approve' == $comment_approved) { $comment_approved = 1; } } } $wpdb->query($wpdb->prepare("UPDATE {$wpdb->comments} SET\n\t\t\tcomment_content = %s,\n\t\t\tcomment_author = %s,\n\t\t\tcomment_author_email = %s,\n\t\t\tcomment_approved = %s,\n\t\t\tcomment_author_url = %s,\n\t\t\tcomment_date = %s,\n\t\t\tcomment_date_gmt = %s\n\t\tWHERE comment_ID = %d", $comment_content, $comment_author, $comment_author_email, $comment_approved, $comment_author_url, $comment_date, $comment_date_gmt, $comment_ID)); $rval = $wpdb->rows_affected; clean_comment_cache($comment_ID); wp_update_comment_count($comment_post_ID); do_action('edit_comment', $comment_ID); $comment = get_comment($comment_ID); wp_transition_comment_status($comment_approved, $comment->comment_approved, $comment); return $rval; }
/** * Updates an existing comment in the database. * * Filters the comment and makes sure certain fields are valid before updating. * * @since 2.0.0 * @uses $wpdb * @uses wp_transition_comment_status() Passes new and old comment status along with $comment 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; } // 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. extract(wp_unslash($commentarr), EXTR_SKIP); $comment_content = apply_filters('comment_save_pre', $comment_content); $comment_date_gmt = get_gmt_from_date($comment_date); if (!isset($comment_approved)) { $comment_approved = 1; } else { if ('hold' == $comment_approved) { $comment_approved = 0; } else { if ('approve' == $comment_approved) { $comment_approved = 1; } } } $data = compact('comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_parent'); $rval = $wpdb->update($wpdb->comments, $data, compact('comment_ID')); clean_comment_cache($comment_ID); wp_update_comment_count($comment_post_ID); do_action('edit_comment', $comment_ID); $comment = get_comment($comment_ID); wp_transition_comment_status($comment->comment_approved, $old_status, $comment); return $rval; }
/** * Hook to wp_set_comment_status action and correctly update the status if necessary * * @param $comment_id (int) The id of the comment */ public function wp_set_comment_status($comment_id) { // Make sure we are working with a comment we have data for if (!isset($this->current_comment->status) || $this->current_comment->comment_ID != $comment_id) { return; } // END if $comment_status = $this->current_comment->status; // Set $this->current_comment back to NULL now that we're finished $this->current_comment = NULL; global $wpdb; // Update comment status with the CORRECT value for this comment $wpdb->update($wpdb->comments, array('comment_approved' => $comment_status), array('comment_ID' => $comment_id)); // We've changed stuff so the cache needs to be cleared again clean_comment_cache($comment_id); // The rest of this is aping default WP behavior to make sure things fire correctly based on the status change do_action('wp_set_comment_status', $comment_id, $comment_status); $comment = get_comment($comment_id); $comment_old = clone get_comment($comment_id); wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment); wp_update_comment_count($comment->comment_post_ID); }