Make WordPress Core

Changeset 40128


Ignore:
Timestamp:
02/27/2017 12:22:02 AM (8 years ago)
Author:
johnbillion
Message:

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

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment.php

    r39663 r40128  
    30063006         */
    30073007        do_action( 'comment_on_draft', $comment_post_ID );
    3008 
    3009         return new WP_Error( 'comment_on_draft' );
     3008       
     3009        if ( current_user_can( 'read_post', $comment_post_ID ) ) {
     3010            return new WP_Error( 'comment_on_draft', __( 'Sorry, comments are not allowed for this item.' ), 403 );
     3011        } else {
     3012            return new WP_Error( 'comment_on_draft' );
     3013        }
    30103014
    30113015    } elseif ( post_password_required( $comment_post_ID ) ) {
  • trunk/tests/phpunit/tests/comment-submission.php

    r38778 r40128  
    6969
    7070    public function test_submitting_comment_to_draft_post_returns_error() {
    71 
    7271        $error = 'comment_on_draft';
    7372
     
    8584        $this->assertWPError( $comment );
    8685        $this->assertSame( $error, $comment->get_error_code() );
    87 
     86        $this->assertEmpty( $comment->get_error_message() );
     87
     88    }
     89
     90    /**
     91     * @ticket 39650
     92     */
     93    public function test_submitting_comment_to_draft_post_returns_error_message_for_user_with_correct_caps() {
     94        $error = 'comment_on_draft';
     95
     96        $user = self::factory()->user->create_and_get( array(
     97            'role' => 'author',
     98        ) );
     99
     100        wp_set_current_user( $user->ID );
     101
     102        $this->assertSame( 0, did_action( $error ) );
     103
     104        $post = self::factory()->post->create_and_get( array(
     105            'post_status' => 'draft',
     106            'post_author' => $user->ID,
     107        ) );
     108        $data = array(
     109            'comment_post_ID' => $post->ID,
     110        );
     111        $comment = wp_handle_comment_submission( $data );
     112
     113        $this->assertSame( 1, did_action( $error ) );
     114        $this->assertWPError( $comment );
     115        $this->assertSame( $error, $comment->get_error_code() );
     116        $this->assertNotEmpty( $comment->get_error_message() );
    88117    }
    89118
Note: See TracChangeset for help on using the changeset viewer.