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
This commit is contained in:
whyisjake 2020-06-24 00:05:12 +00:00
parent bb7a531b75
commit bb7601f6d0
7 changed files with 41 additions and 14 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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 );
}
/**

View File

@ -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() );
}

View File

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

View File

@ -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.' ),

View File

@ -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.