diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 77bc6171ee..bf4f430e41 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -2208,9 +2208,16 @@ function wp_throttle_comment_flood( $block, $time_lastcomment, $time_newcomment function wp_new_comment( $commentdata, $wp_error = false ) { global $wpdb; + /* + * Normalize `user_ID` to `user_id`, but pass the old key + * to the `preprocess_comment` filter for backward compatibility. + */ if ( isset( $commentdata['user_ID'] ) ) { $commentdata['user_ID'] = (int) $commentdata['user_ID']; $commentdata['user_id'] = $commentdata['user_ID']; + } elseif ( isset( $commentdata['user_id'] ) ) { + $commentdata['user_id'] = (int) $commentdata['user_id']; + $commentdata['user_ID'] = $commentdata['user_id']; } $prefiltered_user_id = ( isset( $commentdata['user_id'] ) ) ? (int) $commentdata['user_id'] : 0; @@ -2235,11 +2242,13 @@ function wp_new_comment( $commentdata, $wp_error = false ) { $commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID']; + // Normalize `user_ID` to `user_id` again, after the filter. if ( isset( $commentdata['user_ID'] ) && $prefiltered_user_id !== (int) $commentdata['user_ID'] ) { $commentdata['user_ID'] = (int) $commentdata['user_ID']; $commentdata['user_id'] = $commentdata['user_ID']; } elseif ( isset( $commentdata['user_id'] ) ) { $commentdata['user_id'] = (int) $commentdata['user_id']; + $commentdata['user_ID'] = $commentdata['user_id']; } $commentdata['comment_parent'] = isset( $commentdata['comment_parent'] ) ? absint( $commentdata['comment_parent'] ) : 0; @@ -3587,7 +3596,6 @@ function wp_handle_comment_submission( $comment_data ) { $commentdata = array( 'comment_post_ID' => $comment_post_id, - 'user_ID' => $user_id, ); $commentdata += compact( @@ -3596,7 +3604,8 @@ function wp_handle_comment_submission( $comment_data ) { 'comment_author_url', 'comment_content', 'comment_type', - 'comment_parent' + 'comment_parent', + 'user_id' ); /** diff --git a/tests/phpunit/tests/comment-submission.php b/tests/phpunit/tests/comment-submission.php index f65818b778..5e2be4f538 100644 --- a/tests/phpunit/tests/comment-submission.php +++ b/tests/phpunit/tests/comment-submission.php @@ -845,7 +845,6 @@ class Tests_Comment_Submission extends WP_UnitTestCase { * @covers ::wp_handle_comment_submission */ public function test_comment_submission_sends_all_expected_parameters_to_preprocess_comment_filter() { - $user = get_userdata( self::$author_id ); wp_set_current_user( $user->ID ); diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index 9683110030..9d63171abe 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -8,6 +8,8 @@ class Tests_Comment extends WP_UnitTestCase { protected static $post_id; protected static $notify_message = ''; + protected $preprocess_comment_data = array(); + public function set_up() { parent::set_up(); reset_phpmailer_instance(); @@ -456,6 +458,53 @@ class Tests_Comment extends WP_UnitTestCase { $this->assertSame( strlen( $comment->comment_content ), 65535 ); } + /** + * @ticket 56244 + */ + public function test_wp_new_comment_sends_all_expected_parameters_to_preprocess_comment_filter() { + $user = get_userdata( self::$user_id ); + wp_set_current_user( $user->ID ); + + $data = array( + 'comment_post_ID' => self::$post_id, + 'comment_author' => $user->display_name, + 'comment_author_email' => $user->user_email, + 'comment_author_url' => $user->user_url, + 'comment_content' => 'Comment', + 'comment_type' => '', + 'comment_parent' => 0, + 'user_id' => $user->ID, + ); + + add_filter( 'preprocess_comment', array( $this, 'filter_preprocess_comment' ) ); + + $comment = wp_new_comment( $data ); + + $this->assertNotWPError( $comment ); + $this->assertSameSetsWithIndex( + array( + 'comment_post_ID' => self::$post_id, + 'comment_author' => $user->display_name, + 'comment_author_email' => $user->user_email, + 'comment_author_url' => $user->user_url, + 'comment_content' => $data['comment_content'], + 'comment_type' => '', + 'comment_parent' => 0, + 'user_ID' => $user->ID, + 'user_id' => $user->ID, + 'comment_author_IP' => '127.0.0.1', + 'comment_agent' => '', + ), + $this->preprocess_comment_data + ); + + } + + public function filter_preprocess_comment( $commentdata ) { + $this->preprocess_comment_data = $commentdata; + return $commentdata; + } + /** * @ticket 32566 *