Ticket #39732: 39732.10.patch
File 39732.10.patch, 11.0 KB (added by , 5 years ago) |
---|
-
src/wp-admin/comment.php
diff --git src/wp-admin/comment.php src/wp-admin/comment.php index 3f20c3cc2e..e6a88aac72 100644
switch ( $action ) { 335 335 336 336 check_admin_referer( 'update-comment_' . $comment_id ); 337 337 338 edit_comment(); 338 $updated = edit_comment(); 339 if ( is_wp_error( $updated ) ) { 340 wp_die( $updated->get_error_message() ); 341 } 339 342 340 343 $location = ( empty( $_POST['referredby'] ) ? "edit-comments.php?p=$comment_post_id" : $_POST['referredby'] ) . '#comment-' . $comment_id; 341 344 -
src/wp-admin/includes/ajax-actions.php
diff --git src/wp-admin/includes/ajax-actions.php src/wp-admin/includes/ajax-actions.php index ea33d9d792..b1298d5384 100644
function wp_ajax_edit_comment() { 1410 1410 if ( isset( $_POST['status'] ) ) { 1411 1411 $_POST['comment_status'] = $_POST['status']; 1412 1412 } 1413 edit_comment(); 1413 1414 $updated = edit_comment(); 1415 if ( is_wp_error( $updated ) ) { 1416 wp_die( $updated->get_error_message() ); 1417 } 1414 1418 1415 1419 $position = ( isset( $_POST['position'] ) && (int) $_POST['position'] ) ? (int) $_POST['position'] : '-1'; 1416 1420 $checkbox = ( isset( $_POST['checkbox'] ) && true == $_POST['checkbox'] ) ? 1 : 0; -
src/wp-admin/includes/comment.php
diff --git src/wp-admin/includes/comment.php src/wp-admin/includes/comment.php index 4000fe7a36..3df7fd133d 100644
function edit_comment() { 93 93 $_POST['comment_date'] = "$aa-$mm-$jj $hh:$mn:$ss"; 94 94 } 95 95 96 wp_update_comment( $_POST);96 return wp_update_comment( $_POST, true ); 97 97 } 98 98 99 99 /** -
src/wp-includes/class-wp-xmlrpc-server.php
diff --git src/wp-includes/class-wp-xmlrpc-server.php src/wp-includes/class-wp-xmlrpc-server.php index 8fce283325..1a3f409ef3 100644
class wp_xmlrpc_server extends IXR_Server { 3788 3788 $comment['comment_author_email'] = $content_struct['author_email']; 3789 3789 } 3790 3790 3791 $result = wp_update_comment( $comment );3792 if ( is_wp_error( $result ) ) {3791 $result = wp_update_comment( $comment, true ); 3792 if ( is_wp_error( $result ) || false === $result ) { 3793 3793 return new IXR_Error( 500, $result->get_error_message() ); 3794 3794 } 3795 3795 -
src/wp-includes/comment.php
diff --git src/wp-includes/comment.php src/wp-includes/comment.php index 15cb44bcc3..f3cb80f2ed 100644
function wp_set_comment_status( $comment_id, $comment_status, $wp_error = false 2312 2312 * 2313 2313 * @since 2.0.0 2314 2314 * @since 4.9.0 Add updating comment meta during comment update. 2315 * @since 5.5.0 Allow returning a WP_Error object on failure. 2315 2316 * 2316 2317 * @global wpdb $wpdb WordPress database abstraction object. 2317 2318 * 2318 2319 * @param array $commentarr Contains information on the comment. 2319 * @return int The value 1 if the comment was updated, 0 if not updated. 2320 * @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false. 2321 * @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. 2320 2322 */ 2321 function wp_update_comment( $commentarr ) {2323 function wp_update_comment( $commentarr, $wp_error = false ) { 2322 2324 global $wpdb; 2323 2325 2324 2326 // First, get all of the original fields. 2325 2327 $comment = get_comment( $commentarr['comment_ID'], ARRAY_A ); 2326 2328 if ( empty( $comment ) ) { 2327 return 0; 2329 if ( ! $wp_error ) { 2330 return 0; 2331 } 2332 2333 return new WP_Error( 'invalid_comment_id', __( 'Invalid comment ID.' ) ); 2328 2334 } 2329 2335 2330 2336 // Make sure that the comment post ID is valid (if specified). 2331 2337 if ( ! empty( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) { 2332 return 0; 2338 if ( ! $wp_error ) { 2339 return 0; 2340 } 2341 2342 return new WP_Error( 'invalid_post_id', __( 'Invalid post ID.' ) ); 2333 2343 } 2334 2344 2335 2345 // Escape data pulled from DB. … … function wp_update_comment( $commentarr ) { 2370 2380 /** 2371 2381 * Filters the comment data immediately before it is updated in the database. 2372 2382 * 2373 * Note: data being passed to the filter is already unslashed. 2383 * Note: data being passed to the filter is already unslashed. Returning 0 or a 2384 * WP_Error object is preventing the comment to be updated. 2374 2385 * 2375 2386 * @since 4.7.0 2387 * @since 5.5.0 Allow returning a WP_Error object on failure. 2376 2388 * 2377 2389 * @param array $data The new, processed comment data. 2378 2390 * @param array $comment The old, unslashed comment data. 2379 2391 * @param array $commentarr The new, raw comment data. 2392 * @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false. 2380 2393 */ 2381 $data = apply_filters( 'wp_update_comment_data', $data, $comment, $commentarr ); 2394 $data = apply_filters( 'wp_update_comment_data', $data, $comment, $commentarr, $wp_error ); 2395 2396 // Do not carry on on failure. 2397 if ( is_wp_error( $data ) || 0 === $data ) { 2398 return $data; 2399 } 2382 2400 2383 2401 $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' ); 2384 2402 $data = wp_array_slice_assoc( $data, $keys ); … … function wp_update_comment( $commentarr ) { 2394 2412 2395 2413 clean_comment_cache( $comment_ID ); 2396 2414 wp_update_comment_count( $comment_post_ID ); 2415 2397 2416 /** 2398 2417 * Fires immediately after a comment is updated in the database. 2399 2418 * -
src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index 8d241d7c63..ac7bfb7da6 100644
class WP_REST_Comments_Controller extends WP_REST_Controller { 868 868 ); 869 869 } 870 870 871 $updated = wp_update_comment( wp_slash( (array) $prepared_args ) );871 $updated = wp_update_comment( wp_slash( (array) $prepared_args ), true ); 872 872 873 if ( false === $updated ) {873 if ( is_wp_error( $updated ) || false === $updated ) { 874 874 return new WP_Error( 875 875 'rest_comment_failed_edit', 876 876 __( 'Updating comment failed.' ), -
tests/phpunit/tests/ajax/EditComment.php
diff --git tests/phpunit/tests/ajax/EditComment.php tests/phpunit/tests/ajax/EditComment.php index 38deb9f160..6380ee42a3 100644
class Tests_Ajax_EditComment extends WP_Ajax_UnitTestCase { 32 32 $this->_comment_post = get_post( $post_id ); 33 33 } 34 34 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 35 40 /** 36 41 * Get comments as a privilged user (administrator) 37 42 * Expects test to pass … … class Tests_Ajax_EditComment extends WP_Ajax_UnitTestCase { 126 131 $this->assertEmpty( (string) $xml->response[0]->edit_comment[0]->supplemental ); 127 132 } 128 133 134 /** 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( array( 143 'post_id' => $this->_comment_post->ID 144 ) ); 145 $comment = array_pop( $comments ); 146 147 // Set up a default request 148 $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); 149 $_POST['comment_ID'] = $comment->comment_ID; 150 $_POST['content'] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; 151 152 // Simulate filter check error 153 add_filter( 'wp_update_comment_data', array( $this, '_wp_update_comment_data_filter' ), 10, 3 ); 154 155 // Make the request 156 $this->setExpectedException( 'WPAjaxDieStopException', 'wp_update_comment_data filter fails for this comment.' ); 157 $this->_handleAjax( 'edit-comment' ); 158 } 159 160 /** 161 * Block comments from being updated by returning WP_Error 162 */ 163 public function _wp_update_comment_data_filter( $data, $comment, $commentarr ) { 164 return new WP_Error( 'comment_wrong', __( 'wp_update_comment_data filter fails for this comment.' ), 500 ); 165 } 166 129 167 /** 130 168 * Get comments as a non-privileged user (subscriber) 131 169 * Expects test to fail -
tests/phpunit/tests/comment.php
diff --git tests/phpunit/tests/comment.php tests/phpunit/tests/comment.php index c55c32a153..c3b95606ef 100644
class Tests_Comment extends WP_UnitTestCase { 142 142 $this->assertEquals( $updated_comment_text, $comment->comment_content ); 143 143 } 144 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( array( 'comment_ID' => $comment_id, 'comment_type' => 'pingback' ), true ); 153 $this->assertWPError( $result ); 154 remove_filter( 'wp_update_comment_data', array( $this, '_wp_update_comment_data_filter' ), 10, 3 ); 155 } 156 157 /** 158 * Block comments from being updated by returning WP_Error 159 */ 160 public function _wp_update_comment_data_filter( $data, $comment, $commentarr ) { 161 return new WP_Error( 'comment_wrong', __( 'wp_update_comment_data filter fails for this comment.' ), 500 ); 162 } 163 145 164 public function test_get_approved_comments() { 146 165 $ca1 = self::factory()->comment->create( 147 166 array( -
tests/phpunit/tests/rest-api/rest-comments-controller.php
diff --git tests/phpunit/tests/rest-api/rest-comments-controller.php tests/phpunit/tests/rest-api/rest-comments-controller.php index e061c1925d..64800b78ac 100644
class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 2790 2790 $this->assertErrorResponse( 'comment_content_column_length', $response, 400 ); 2791 2791 } 2792 2792 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 2793 2823 public function verify_comment_roundtrip( $input = array(), $expected_output = array() ) { 2794 2824 // Create the comment. 2795 2825 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );