WordPress.org

Make WordPress Core


Ignore:
Timestamp:
06/24/2020 12:03:33 AM (10 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/comment.php

    r47122 r48154  
    141141        $comment = get_comment( $comment_id );
    142142        $this->assertEquals( $updated_comment_text, $comment->comment_content );
     143    }
     144
     145    /**
     146     * @ticket 39732
     147     */
     148    public function test_wp_update_comment_is_wp_error() {
     149        $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
     150
     151        add_filter( 'wp_update_comment_data', array( $this, '_wp_update_comment_data_filter' ), 10, 3 );
     152        $result = wp_update_comment(
     153            array(
     154                'comment_ID'   => $comment_id,
     155                'comment_type' => 'pingback',
     156            ),
     157            true
     158        );
     159        $this->assertWPError( $result );
     160        remove_filter( 'wp_update_comment_data', array( $this, '_wp_update_comment_data_filter' ), 10, 3 );
     161    }
     162
     163    /**
     164     * Block comments from being updated by returning WP_Error
     165     */
     166    public function _wp_update_comment_data_filter( $data, $comment, $commentarr ) {
     167        return new WP_Error( 'comment_wrong', __( 'wp_update_comment_data filter fails for this comment.' ), 500 );
    143168    }
    144169
Note: See TracChangeset for help on using the changeset viewer.