WordPress.org

Make WordPress Core


Ignore:
Timestamp:
06/24/2020 12:03:33 AM (7 months ago)
Author:
whyisjake
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment.php

    r48140 r48154  
    24062406 * @since 2.0.0
    24072407 * @since 4.9.0 Add updating comment meta during comment update.
     2408 * @since 5.5.0 Allow returning a WP_Error object on failure.
    24082409 *
    24092410 * @global wpdb $wpdb WordPress database abstraction object.
    24102411 *
    24112412 * @param array $commentarr Contains information on the comment.
    2412  * @return int The value 1 if the comment was updated, 0 if not updated.
    2413  */
    2414 function wp_update_comment( $commentarr ) {
     2413 * @param bool  $wp_error   Optional. Whether to return a WP_Error on failure. Default false.
     2414 * @return int|bool|WP_Error Comment was updated if value is 1, or was not updated if value is 0,
     2415 *                           false, or a WP_Error object.
     2416 */
     2417function wp_update_comment( $commentarr, $wp_error = false ) {
    24152418    global $wpdb;
    24162419
     
    24182421    $comment = get_comment( $commentarr['comment_ID'], ARRAY_A );
    24192422    if ( empty( $comment ) ) {
    2420         return 0;
     2423        if ( ! $wp_error ) {
     2424            return 0;
     2425        }
     2426
     2427        return new WP_Error( 'invalid_comment_id', __( 'Invalid comment ID.' ) );
    24212428    }
    24222429
    24232430    // Make sure that the comment post ID is valid (if specified).
    24242431    if ( ! empty( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) {
    2425         return 0;
     2432        if ( ! $wp_error ) {
     2433            return 0;
     2434        }
     2435
     2436        return new WP_Error( 'invalid_post_id', __( 'Invalid post ID.' ) );
    24262437    }
    24272438
     
    24642475     * Filters the comment data immediately before it is updated in the database.
    24652476     *
    2466      * Note: data being passed to the filter is already unslashed.
     2477     * Note: data being passed to the filter is already unslashed. Returning 0 or a
     2478     * WP_Error object is preventing the comment to be updated.
    24672479     *
    24682480     * @since 4.7.0
     2481     * @since 5.5.0 Allow returning a WP_Error object on failure.
    24692482     *
    24702483     * @param array $data       The new, processed comment data.
    24712484     * @param array $comment    The old, unslashed comment data.
    24722485     * @param array $commentarr The new, raw comment data.
    2473      */
    2474     $data = apply_filters( 'wp_update_comment_data', $data, $comment, $commentarr );
     2486     * @param bool  $wp_error   Optional. Whether to return a WP_Error on failure.
     2487     *                          Default false.
     2488     */
     2489    $data = apply_filters( 'wp_update_comment_data', $data, $comment, $commentarr, $wp_error );
     2490
     2491    // Do not carry on on failure.
     2492    if ( is_wp_error( $data ) || 0 === $data ) {
     2493        return $data;
     2494    }
    24752495
    24762496    $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' );
Note: See TracChangeset for help on using the changeset viewer.