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/tests/phpunit/tests/rest-api/rest-comments-controller.php

    r47122 r48154  
    27912791    }
    27922792
     2793    /**
     2794     * @ticket 39732
     2795     */
     2796    public function test_update_comment_is_wp_error() {
     2797        wp_set_current_user( self::$admin_id );
     2798
     2799        $params = array(
     2800            'content' => 'This isn\'t a saxophone. It\'s an umbrella.',
     2801        );
     2802
     2803        add_filter( 'wp_update_comment_data', array( $this, '_wp_update_comment_data_filter' ), 10, 3 );
     2804
     2805        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
     2806
     2807        $request->add_header( 'content-type', 'application/json' );
     2808        $request->set_body( wp_json_encode( $params ) );
     2809        $response = rest_get_server()->dispatch( $request );
     2810
     2811        $this->assertErrorResponse( 'rest_comment_failed_edit', $response, 500 );
     2812
     2813        remove_filter( 'wp_update_comment_data', array( $this, '_wp_update_comment_data_filter' ), 10, 3 );
     2814    }
     2815
     2816    /**
     2817     * Block comments from being updated by returning WP_Error
     2818     */
     2819    public function _wp_update_comment_data_filter( $data, $comment, $commentarr ) {
     2820        return new WP_Error( 'comment_wrong', __( 'wp_update_comment_data filter fails for this comment.' ), array( 'status' => 500 ) );
     2821    }
     2822
    27932823    public function verify_comment_roundtrip( $input = array(), $expected_output = array() ) {
    27942824        // Create the comment.
Note: See TracChangeset for help on using the changeset viewer.