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.


git-svn-id: https://develop.svn.wordpress.org/trunk@48154 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jake Spurlock
2020-06-24 00:03:33 +00:00
parent 7a8d8a3e8a
commit ec062c08fe
9 changed files with 135 additions and 13 deletions

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