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/ajax/EditComment.php

    r47198 r48154  
    3333    }
    3434
     35    public function tearDown() {
     36        remove_filter( 'wp_update_comment_data', array( $this, '_wp_update_comment_data_filter' ), 10, 3 );
     37        parent::tearDown();
     38    }
     39
    3540    /**
    3641     * Get comments as a privilged user (administrator)
     
    128133
    129134    /**
     135     * @ticket 39732
     136     */
     137    public function test_wp_update_comment_data_is_wp_error() {
     138        // Become an administrator
     139        $this->_setRole( 'administrator' );
     140
     141        // Get a comment
     142        $comments = get_comments(
     143            array(
     144                'post_id' => $this->_comment_post->ID,
     145            )
     146        );
     147        $comment  = array_pop( $comments );
     148
     149        // Set up a default request
     150        $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' );
     151        $_POST['comment_ID']                  = $comment->comment_ID;
     152        $_POST['content']                     = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
     153
     154        // Simulate filter check error
     155        add_filter( 'wp_update_comment_data', array( $this, '_wp_update_comment_data_filter' ), 10, 3 );
     156
     157        // Make the request
     158        $this->setExpectedException( 'WPAjaxDieStopException', 'wp_update_comment_data filter fails for this comment.' );
     159        $this->_handleAjax( 'edit-comment' );
     160    }
     161
     162    /**
     163     * Block comments from being updated by returning WP_Error
     164     */
     165    public function _wp_update_comment_data_filter( $data, $comment, $commentarr ) {
     166        return new WP_Error( 'comment_wrong', __( 'wp_update_comment_data filter fails for this comment.' ), 500 );
     167    }
     168
     169    /**
    130170     * Get comments as a non-privileged user (subscriber)
    131171     * Expects test to fail
Note: See TracChangeset for help on using the changeset viewer.