From bb7601f6d0b254c3e8db47c1a981fcfa52bb4cbe Mon Sep 17 00:00:00 2001 From: whyisjake Date: Wed, 24 Jun 2020 00:05:12 +0000 Subject: [PATCH] Comments: Allow `wp_update_comment()` to return `WP_Error()`. The `wp_update_comment_data` filter introduced in 4.7 allows comment data to be filtered before it is updated in the database. The patch aims to handle `WP_Error` as the filter above return value in a similar manner as is done for `wp_new_comment()`. Fixes #39732. Props: enricosorcinelli, swissspidy, gkloveweb, jnylen0, jbpaul17, afercia, SergeyBiryukov, audrasjb, imath, davidbaumwald. Built from https://develop.svn.wordpress.org/trunk@48154 git-svn-id: http://core.svn.wordpress.org/trunk@47923 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/comment.php | 5 ++- wp-admin/includes/ajax-actions.php | 6 +++- wp-admin/includes/comment.php | 2 +- wp-includes/class-wp-xmlrpc-server.php | 4 +-- wp-includes/comment.php | 32 +++++++++++++++---- .../class-wp-rest-comments-controller.php | 4 +-- wp-includes/version.php | 2 +- 7 files changed, 41 insertions(+), 14 deletions(-) diff --git a/wp-admin/comment.php b/wp-admin/comment.php index 0d6af5f17b..4db8c5784f 100644 --- a/wp-admin/comment.php +++ b/wp-admin/comment.php @@ -339,7 +339,10 @@ switch ( $action ) { check_admin_referer( 'update-comment_' . $comment_id ); - edit_comment(); + $updated = edit_comment(); + if ( is_wp_error( $updated ) ) { + wp_die( $updated->get_error_message() ); + } $location = ( empty( $_POST['referredby'] ) ? "edit-comments.php?p=$comment_post_id" : $_POST['referredby'] ) . '#comment-' . $comment_id; diff --git a/wp-admin/includes/ajax-actions.php b/wp-admin/includes/ajax-actions.php index 54a33e1bf3..5a6c767b5e 100644 --- a/wp-admin/includes/ajax-actions.php +++ b/wp-admin/includes/ajax-actions.php @@ -1410,7 +1410,11 @@ function wp_ajax_edit_comment() { if ( isset( $_POST['status'] ) ) { $_POST['comment_status'] = $_POST['status']; } - edit_comment(); + + $updated = edit_comment(); + if ( is_wp_error( $updated ) ) { + wp_die( $updated->get_error_message() ); + } $position = ( isset( $_POST['position'] ) && (int) $_POST['position'] ) ? (int) $_POST['position'] : '-1'; $checkbox = ( isset( $_POST['checkbox'] ) && true == $_POST['checkbox'] ) ? 1 : 0; diff --git a/wp-admin/includes/comment.php b/wp-admin/includes/comment.php index 3f0a2974dc..e006dadd87 100644 --- a/wp-admin/includes/comment.php +++ b/wp-admin/includes/comment.php @@ -92,7 +92,7 @@ function edit_comment() { $_POST['comment_date'] = "$aa-$mm-$jj $hh:$mn:$ss"; } - wp_update_comment( $_POST ); + return wp_update_comment( $_POST, true ); } /** diff --git a/wp-includes/class-wp-xmlrpc-server.php b/wp-includes/class-wp-xmlrpc-server.php index 2a29387fab..d015964f0a 100644 --- a/wp-includes/class-wp-xmlrpc-server.php +++ b/wp-includes/class-wp-xmlrpc-server.php @@ -3788,8 +3788,8 @@ class wp_xmlrpc_server extends IXR_Server { $comment['comment_author_email'] = $content_struct['author_email']; } - $result = wp_update_comment( $comment ); - if ( is_wp_error( $result ) ) { + $result = wp_update_comment( $comment, true ); + if ( is_wp_error( $result ) || false === $result ) { return new IXR_Error( 500, $result->get_error_message() ); } diff --git a/wp-includes/comment.php b/wp-includes/comment.php index 15899cba77..158f09840b 100644 --- a/wp-includes/comment.php +++ b/wp-includes/comment.php @@ -2405,24 +2405,35 @@ function wp_set_comment_status( $comment_id, $comment_status, $wp_error = false * * @since 2.0.0 * @since 4.9.0 Add updating comment meta during comment update. + * @since 5.5.0 Allow returning a WP_Error object on failure. * * @global wpdb $wpdb WordPress database abstraction object. * * @param array $commentarr Contains information on the comment. - * @return int The value 1 if the comment was updated, 0 if not updated. + * @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false. + * @return int|bool|WP_Error Comment was updated if value is 1, or was not updated if value is 0, + * false, or a WP_Error object. */ -function wp_update_comment( $commentarr ) { +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 ) ) { - return 0; + if ( ! $wp_error ) { + return 0; + } + + return new WP_Error( 'invalid_comment_id', __( 'Invalid comment ID.' ) ); } // 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; + if ( ! $wp_error ) { + return 0; + } + + return new WP_Error( 'invalid_post_id', __( 'Invalid post ID.' ) ); } // Escape data pulled from DB. @@ -2463,15 +2474,24 @@ function wp_update_comment( $commentarr ) { /** * Filters the comment data immediately before it is updated in the database. * - * Note: data being passed to the filter is already unslashed. + * Note: data being passed to the filter is already unslashed. Returning 0 or a + * WP_Error object is preventing the comment to be updated. * * @since 4.7.0 + * @since 5.5.0 Allow returning a WP_Error object on failure. * * @param array $data The new, processed comment data. * @param array $comment The old, unslashed comment data. * @param array $commentarr The new, raw comment data. + * @param bool $wp_error Optional. Whether to return a WP_Error on failure. + * Default false. */ - $data = apply_filters( 'wp_update_comment_data', $data, $comment, $commentarr ); + $data = apply_filters( 'wp_update_comment_data', $data, $comment, $commentarr, $wp_error ); + + // Do not carry on on failure. + if ( is_wp_error( $data ) || 0 === $data ) { + return $data; + } $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 ); diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index da2457c85a..c3f2f35b8d 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -868,9 +868,9 @@ class WP_REST_Comments_Controller extends WP_REST_Controller { ); } - $updated = wp_update_comment( wp_slash( (array) $prepared_args ) ); + $updated = wp_update_comment( wp_slash( (array) $prepared_args ), true ); - if ( false === $updated ) { + if ( is_wp_error( $updated ) || false === $updated ) { return new WP_Error( 'rest_comment_failed_edit', __( 'Updating comment failed.' ), diff --git a/wp-includes/version.php b/wp-includes/version.php index c4ccbd5043..fe32df85d5 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.5-alpha-48153'; +$wp_version = '5.5-alpha-48154'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.