diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 87ed753c17..8648a89a0f 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -1912,7 +1912,7 @@ function wp_insert_comment( $commentdata ) { $comment_karma = ! isset( $data['comment_karma'] ) ? 0 : $data['comment_karma']; $comment_approved = ! isset( $data['comment_approved'] ) ? 1 : $data['comment_approved']; $comment_agent = ! isset( $data['comment_agent'] ) ? '' : $data['comment_agent']; - $comment_type = ! isset( $data['comment_type'] ) ? 'comment' : $data['comment_type']; + $comment_type = empty( $data['comment_type'] ) ? 'comment' : $data['comment_type']; $comment_parent = ! isset( $data['comment_parent'] ) ? 0 : $data['comment_parent']; $user_id = ! isset( $data['user_id'] ) ? 0 : $data['user_id']; diff --git a/tests/phpunit/tests/comment-submission.php b/tests/phpunit/tests/comment-submission.php index 6a5b845bb0..e2534ed2e8 100644 --- a/tests/phpunit/tests/comment-submission.php +++ b/tests/phpunit/tests/comment-submission.php @@ -715,6 +715,45 @@ class Tests_Comment_Submission extends WP_UnitTestCase { $this->assertSame( $error, $comment->get_error_code() ); } + /** + * @ticket 49236 + */ + public function test_submitting_comment_with_empty_type_results_in_correct_type() { + $data = array( + 'comment_post_ID' => self::$post->ID, + 'comment' => 'Comment', + 'author' => 'Comment Author', + 'email' => 'comment@example.org', + 'comment_type' => '', + ); + $comment = wp_handle_comment_submission( $data ); + + $this->assertNotWPError( $comment ); + $this->assertInstanceOf( 'WP_Comment', $comment ); + + $this->assertSame( 'comment', $comment->comment_type ); + } + + /** + * @ticket 49236 + */ + public function test_inserting_comment_with_empty_type_results_in_correct_type() { + $data = array( + 'comment_post_ID' => self::$post->ID, + 'comment' => 'Comment', + 'author' => 'Comment Author', + 'email' => 'comment@example.org', + 'comment_type' => '', + ); + $comment_id = wp_insert_comment( $data ); + $comment = get_comment( $comment_id ); + + $this->assertNotWPError( $comment ); + $this->assertInstanceOf( 'WP_Comment', $comment ); + + $this->assertSame( 'comment', $comment->comment_type ); + } + /** * @ticket 34997 */