Make WordPress Core

Ticket #38700: 38700.wp-error.diff

File 38700.wp-error.diff, 3.3 KB (added by dd32, 8 years ago)
  • src/wp-includes/comment.php

    function wp_set_comment_status($comment_ 
    21192119        do_action( 'wp_set_comment_status', $comment->comment_ID, $comment_status );
    21202120
    21212121        wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);
    21222122
    21232123        wp_update_comment_count($comment->comment_post_ID);
    21242124
    21252125        return true;
    21262126}
    21272127
    21282128/**
    21292129 * Updates an existing comment in the database.
    21302130 *
    21312131 * Filters the comment and makes sure certain fields are valid before updating.
    21322132 *
    21332133 * @since 2.0.0
     2134 * @since 4.x.0 The $wp_error parameter was added.
    21342135 *
    21352136 * @global wpdb $wpdb WordPress database abstraction object.
    21362137 *
    21372138 * @param array $commentarr Contains information on the comment.
    2138  * @return int Comment was updated if value is 1, or was not updated if value is 0.
     2139 * @param bool  $wp_error Optional. Whether to return a WP_Error on failure. Default false.
     2140 * @return int|bool|WP_Error Number of comment rows updated on success (0 or 1). The value 0, false, or WP_Error on failure.
    21392141 */
    2140 function wp_update_comment($commentarr) {
     2142function wp_update_comment( $commentarr, $wp_error = false ) {
    21412143        global $wpdb;
    21422144
    21432145        // First, get all of the original fields
    21442146        $comment = get_comment($commentarr['comment_ID'], ARRAY_A);
    21452147        if ( empty( $comment ) ) {
    2146                 return 0;
     2148                return $wp_error ? new WP_Error( 'invalid_comment_id', __( 'Invalid comment ID.' ) ) : 0;
    21472149        }
    21482150
    21492151        // Make sure that the comment post ID is valid (if specified).
    21502152        if ( ! empty( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) {
    2151                 return 0;
     2153                return $wp_error ? new WP_Error( 'invalid_post_id', __( 'Invalid post ID.' ) ) : 0;
    21522154        }
    21532155
    21542156        // Escape data pulled from DB.
    21552157        $comment = wp_slash($comment);
    21562158
    21572159        $old_status = $comment['comment_approved'];
    21582160
    21592161        // Merge old and new fields with new fields overwriting old ones.
    21602162        $commentarr = array_merge($comment, $commentarr);
    21612163
    21622164        $commentarr = wp_filter_comment( $commentarr );
    21632165
    21642166        // Now extract the merged array.
    21652167        $data = wp_unslash( $commentarr );
    21662168
    function wp_update_comment($commentarr)  
    21902192
    21912193        /**
    21922194         * Filters the comment data immediately before it is updated in the database.
    21932195         *
    21942196         * Note: data being passed to the filter is already unslashed.
    21952197         *
    21962198         * @since 4.7.0
    21972199         *
    21982200         * @param array $data       The new, processed comment data.
    21992201         * @param array $comment    The old, unslashed comment data.
    22002202         * @param array $commentarr The new, raw comment data.
    22012203         */
    22022204        $data = apply_filters( 'wp_update_comment_data', $data, $comment, $commentarr );
    22032205
    22042206        $rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) );
     2207        if ( false === $rval ) {
     2208                return $wp_error ? new WP_Error( 'db_update_error', __('Could not update comment in the database'), $wpdb->last_error ) : $rval;
     2209        }
    22052210
    22062211        clean_comment_cache( $comment_ID );
    22072212        wp_update_comment_count( $comment_post_ID );
    22082213        /**
    22092214         * Fires immediately after a comment is updated in the database.
    22102215         *
    22112216         * The hook also fires immediately before comment status transition hooks are fired.
    22122217         *
    22132218         * @since 1.2.0
    22142219         * @since 4.6.0 Added the `$data` parameter.
    22152220         *
    22162221         * @param int   $comment_ID The comment ID.
    22172222         * @param array $data       Comment data.
    22182223         */
    22192224        do_action( 'edit_comment', $comment_ID, $data );