From 5914e52d8c9e96c493b85e6ea8afc3339fc92038 Mon Sep 17 00:00:00 2001 From: Sainath Poojary Date: Mon, 24 Aug 2026 22:03:32 +0530 Subject: [PATCH 1/3] Comments: Add filters to allow comments on draft and trashed posts --- src/wp-includes/comment.php | 65 ++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 7557e9258c87f..5f9d215bc7c00 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -4024,31 +4024,52 @@ function wp_handle_comment_submission( $comment_data ) { } elseif ( 'trash' === $status ) { /** - * Fires when a comment is attempted on a trashed post. + * Filters whether to allow comments on trashed posts. * - * @since 2.9.0 + * @since 7.2.0 * - * @param int $comment_post_id Post ID. + * @param bool $allow Whether to allow comments on trashed posts. Default false. + * @param int $comment_post_id Post ID. */ - do_action( 'comment_on_trash', $comment_post_id ); + if ( ! apply_filters( 'allow_comment_on_trash', false, $comment_post_id ) ) { - return new WP_Error( 'comment_on_trash' ); + /** + * Fires when a comment is attempted on a trashed post. + * + * @since 2.9.0 + * + * @param int $comment_post_id Post ID. + */ + do_action( 'comment_on_trash', $comment_post_id ); + return new WP_Error( 'comment_on_trash' ); + } } elseif ( ! $status_obj->public && ! $status_obj->private ) { /** - * Fires when a comment is attempted on a post in draft mode. + * Filters whether to allow comments on draft posts. * - * @since 1.5.1 + * @since 7.2.0 * - * @param int $comment_post_id Post ID. + * @param bool $allow Whether to allow comments on draft posts. Default false. + * @param int $comment_post_id Post ID. */ - do_action( 'comment_on_draft', $comment_post_id ); + if ( ! apply_filters( 'allow_comment_on_draft', false, $comment_post_id ) ) { - 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' ); + /** + * Fires when a comment is attempted on a post in draft mode. + * + * @since 1.5.1 + * + * @param int $comment_post_id Post ID. + */ + do_action( 'comment_on_draft', $comment_post_id ); + + 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 ) ) { @@ -4063,17 +4084,17 @@ function wp_handle_comment_submission( $comment_data ) { return new WP_Error( 'comment_on_password_protected' ); - } else { - /** - * Fires before a comment is posted. - * - * @since 2.8.0 - * - * @param int $comment_post_id Post ID. - */ - do_action( 'pre_comment_on_post', $comment_post_id ); } + /** + * Fires before a comment is posted. + * + * @since 2.8.0 + * + * @param int $comment_post_id Post ID. + */ + do_action( 'pre_comment_on_post', $comment_post_id ); + // If the user is logged in. $user = wp_get_current_user(); if ( $user->exists() ) { From e51c6a78cafaeafa7547d671ccd79b6bf2065e46 Mon Sep 17 00:00:00 2001 From: Sainath Poojary Date: Mon, 24 Aug 2026 22:39:09 +0530 Subject: [PATCH 2/3] refactor: remove allow_comment_on_trash filter --- src/wp-includes/comment.php | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 5f9d215bc7c00..10dc455228db9 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -4024,26 +4024,15 @@ function wp_handle_comment_submission( $comment_data ) { } elseif ( 'trash' === $status ) { /** - * Filters whether to allow comments on trashed posts. + * Fires when a comment is attempted on a trashed post. * - * @since 7.2.0 + * @since 2.9.0 * - * @param bool $allow Whether to allow comments on trashed posts. Default false. - * @param int $comment_post_id Post ID. + * @param int $comment_post_id Post ID. */ - if ( ! apply_filters( 'allow_comment_on_trash', false, $comment_post_id ) ) { - - /** - * Fires when a comment is attempted on a trashed post. - * - * @since 2.9.0 - * - * @param int $comment_post_id Post ID. - */ - do_action( 'comment_on_trash', $comment_post_id ); + do_action( 'comment_on_trash', $comment_post_id ); - return new WP_Error( 'comment_on_trash' ); - } + return new WP_Error( 'comment_on_trash' ); } elseif ( ! $status_obj->public && ! $status_obj->private ) { /** From 3e0d37ee88bcf0d56de98afb87726af67f74cfb6 Mon Sep 17 00:00:00 2001 From: Sainath Poojary Date: Tue, 25 Aug 2026 12:32:12 +0530 Subject: [PATCH 3/3] test: add unit tests for allow_comment_on_draft filter in wp_handle_comment_submission --- .../comment/wpHandleCommentSubmission.php | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/tests/phpunit/tests/comment/wpHandleCommentSubmission.php b/tests/phpunit/tests/comment/wpHandleCommentSubmission.php index 3f2ba84194a0d..fd54ce7c47a60 100644 --- a/tests/phpunit/tests/comment/wpHandleCommentSubmission.php +++ b/tests/phpunit/tests/comment/wpHandleCommentSubmission.php @@ -181,6 +181,58 @@ public function test_submitting_comment_to_scheduled_post_returns_error() { $this->assertSame( $error, $comment->get_error_code() ); } + + /** + * @ticket 19739 + */ + public function test_allow_comment_on_draft_filter_default_blocks_comment() { + $error = 'comment_on_draft'; + + $post = self::factory()->post->create_and_get( + array( + 'post_status' => 'draft', + ) + ); + + $this->assertSame( 0, did_action( $error ) ); + + $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() ); + } + + /** + * @ticket 19739 + */ + public function test_allow_comment_on_draft_filter_allows_comment() { + add_filter( 'allow_comment_on_draft', '__return_true' ); + + $post = self::factory()->post->create_and_get( + array( + 'post_status' => 'draft', + ) + ); + + $user = get_user_by( 'id', self::$author_id2 ); + wp_set_current_user( $user->ID ); + + $data = array( + 'comment_post_ID' => $post->ID, + 'comment' => 'Test comment on draft post.', + ); + $comment = wp_handle_comment_submission( $data ); + + remove_filter( 'allow_comment_on_draft', '__return_true' ); + + $this->assertNotWPError( $comment ); + $this->assertInstanceOf( 'WP_Comment', $comment ); + } + public function test_submitting_comment_to_password_required_post_returns_error() { $error = 'comment_on_password_protected';