diff --git a/wp-admin/admin-ajax.php b/wp-admin/admin-ajax.php index 6b34ee1666..225028f3c7 100644 --- a/wp-admin/admin-ajax.php +++ b/wp-admin/admin-ajax.php @@ -314,8 +314,14 @@ case 'delete-page' : die('0'); break; case 'dim-comment' : // On success, die with time() instead of 1 - if ( !$comment = get_comment( $id ) ) - die('0'); + + if ( !$comment = get_comment( $id ) ) { + $x = new WP_Ajax_Response( array( + 'what' => 'comment', + 'id' => new WP_Error('invalid_comment', sprintf(__('Comment %d does not exist'), $id)) + ) ); + $x->send(); + } if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) ) die('-1'); @@ -329,15 +335,21 @@ case 'dim-comment' : // On success, die with time() instead of 1 $r = 0; if ( in_array( $current, array( 'unapproved', 'spam' ) ) ) { check_ajax_referer( "approve-comment_$id" ); - if ( wp_set_comment_status( $comment->comment_ID, 'approve' ) ) - $r = 1; + $result = wp_set_comment_status( $comment->comment_ID, 'approve', true ); } else { check_ajax_referer( "unapprove-comment_$id" ); - if ( wp_set_comment_status( $comment->comment_ID, 'hold' ) ) - $r = 1; + $result = wp_set_comment_status( $comment->comment_ID, 'hold', true ); } - if ( $r ) // Decide if we need to send back '1' or a more complicated response including page links and comment counts - _wp_ajax_delete_comment_response( $comment->comment_ID ); + if ( is_wp_error($result) ) { + $x = new WP_Ajax_Response( array( + 'what' => 'comment', + 'id' => $result + ) ); + $x->send(); + } + + // Decide if we need to send back '1' or a more complicated response including page links and comment counts + _wp_ajax_delete_comment_response( $comment->comment_ID ); die( '0' ); break; case 'add-category' : // On the Fly diff --git a/wp-includes/comment.php b/wp-includes/comment.php index 186d7409a2..fc8cc2e2a2 100644 --- a/wp-includes/comment.php +++ b/wp-includes/comment.php @@ -1014,9 +1014,10 @@ function wp_new_comment( $commentdata ) { * * @param int $comment_id Comment ID. * @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'delete'. + * @param bool $wp_error Whether to return a WP_Error object if there is a failure. Default is false. * @return bool False on failure or deletion and true on success. */ -function wp_set_comment_status($comment_id, $comment_status) { +function wp_set_comment_status($comment_id, $comment_status, $wp_error = false) { global $wpdb; switch ( $comment_status ) { @@ -1040,8 +1041,12 @@ function wp_set_comment_status($comment_id, $comment_status) { return false; } - if ( !$wpdb->query($query) ) - return false; + if ( !$wpdb->query($query) ) { + if ( $wp_error ) + return new WP_Error('db_update_error', __('Could not update comment status'), $wpdb->last_error); + else + return false; + } clean_comment_cache($comment_id);