From 5ab0154647b01d268b59d1f5752294b7e4a347b0 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Mon, 27 Feb 2017 00:22:02 +0000 Subject: [PATCH] Comments: When commenting on a draft post, display a friendly error message if the user can view the post. This prevents the unhelpful white screen of death when a user who can view the post (eg. preview it) leaves a comment while the post is in draft. Props sagarprajapati, milindmore22, mayurk, swissspidy Fixes #39650 git-svn-id: https://develop.svn.wordpress.org/trunk@40128 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment.php | 8 ++++-- tests/phpunit/tests/comment-submission.php | 31 +++++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index ba60070fd7..7398f99dd9 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -3005,8 +3005,12 @@ function wp_handle_comment_submission( $comment_data ) { * @param int $comment_post_ID Post ID. */ do_action( 'comment_on_draft', $comment_post_ID ); - - return new WP_Error( 'comment_on_draft' ); + + if ( current_user_can( 'read_post', $comment_post_ID ) ) { + return new WP_Error( 'comment_on_draft', __( 'Sorry, comments are not allowed for this item.' ), 403 ); + } else { + return new WP_Error( 'comment_on_draft' ); + } } elseif ( post_password_required( $comment_post_ID ) ) { diff --git a/tests/phpunit/tests/comment-submission.php b/tests/phpunit/tests/comment-submission.php index f5d6cc08d1..f2342332f1 100644 --- a/tests/phpunit/tests/comment-submission.php +++ b/tests/phpunit/tests/comment-submission.php @@ -68,7 +68,6 @@ class Tests_Comment_Submission extends WP_UnitTestCase { } public function test_submitting_comment_to_draft_post_returns_error() { - $error = 'comment_on_draft'; $this->assertSame( 0, did_action( $error ) ); @@ -84,9 +83,39 @@ class Tests_Comment_Submission extends WP_UnitTestCase { $this->assertSame( 1, did_action( $error ) ); $this->assertWPError( $comment ); $this->assertSame( $error, $comment->get_error_code() ); + $this->assertEmpty( $comment->get_error_message() ); } + /** + * @ticket 39650 + */ + public function test_submitting_comment_to_draft_post_returns_error_message_for_user_with_correct_caps() { + $error = 'comment_on_draft'; + + $user = self::factory()->user->create_and_get( array( + 'role' => 'author', + ) ); + + wp_set_current_user( $user->ID ); + + $this->assertSame( 0, did_action( $error ) ); + + $post = self::factory()->post->create_and_get( array( + 'post_status' => 'draft', + 'post_author' => $user->ID, + ) ); + $data = array( + 'comment_post_ID' => $post->ID, + ); + $comment = wp_handle_comment_submission( $data ); + + $this->assertSame( 1, did_action( $error ) ); + $this->assertWPError( $comment ); + $this->assertSame( $error, $comment->get_error_code() ); + $this->assertNotEmpty( $comment->get_error_message() ); + } + public function test_submitting_comment_to_scheduled_post_returns_error() { // Same error as commenting on a draft