From 66f2459b9fe423e48f7d754d6ab4e998d8ca6488 Mon Sep 17 00:00:00 2001 From: himanshupathak95 Date: Tue, 29 Jul 2025 13:09:57 +0530 Subject: [PATCH 1/4] Comments: Show custom comment form fields for logged-in users. --- src/wp-includes/comment-template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index 59f89f3a847bf..96229f0a9680f 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -2806,7 +2806,7 @@ function comment_form( $args = array(), $post = null ) { echo $args['comment_notes_after']; - } elseif ( ! is_user_logged_in() ) { + } elseif ( ! is_user_logged_in() || ! in_array( $name, array( 'author', 'email', 'url', 'cookies' ), true ) ) { if ( $first_field === $name ) { /** From d231c8ce367eec5edd018412b61efaf9ebc945be Mon Sep 17 00:00:00 2001 From: himanshupathak95 Date: Mon, 4 Aug 2025 20:59:53 +0530 Subject: [PATCH 2/4] Comments: Add test for custom fields visibility in comment form for logged-in users --- tests/phpunit/tests/comment/commentForm.php | 43 +++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/phpunit/tests/comment/commentForm.php b/tests/phpunit/tests/comment/commentForm.php index e3dab07e249e8..5cf5bde348978 100644 --- a/tests/phpunit/tests/comment/commentForm.php +++ b/tests/phpunit/tests/comment/commentForm.php @@ -227,4 +227,47 @@ static function ( array $defaults ): array { $this->assertTrue( $p->next_tag( array( 'tag_name' => 'FORM' ) ), 'Expected FORM tag.' ); $this->assertTrue( $p->get_attribute( 'novalidate' ), 'Expected FORM to have novalidate attribute.' ); } + + /** + * @ticket 16576 + */ + public function test_custom_fields_shown_default_fields_hidden_for_logged_in_users() { + $user_id = self::factory()->user->create( + array( + 'role' => 'subscriber', + 'user_login' => 'testuser', + 'user_email' => 'test@example.com', + ) + ); + + wp_set_current_user( $user_id ); + $this->assertTrue( is_user_logged_in() ); + + $args = array( + 'fields' => array( + 'author' => '

', + 'email' => '

', + 'url' => '

', + 'cookies' => '

', + 'custom_field' => '

', + 'department' => '

', + ), + ); + + $form = get_echo( 'comment_form', array( $args, self::$post_id ) ); + + // Custom fields should be present + $this->assertStringContainsString( 'name="custom_field"', $form ); + $this->assertStringContainsString( 'name="department"', $form ); + $this->assertStringContainsString( 'Custom Field', $form ); + $this->assertStringContainsString( 'Department', $form ); + + // Default fields should NOT be present + $this->assertStringNotContainsString( 'name="author"', $form ); + $this->assertStringNotContainsString( 'name="email"', $form ); + $this->assertStringNotContainsString( 'name="url"', $form ); + $this->assertStringNotContainsString( 'wp-comment-cookies-consent', $form ); + + wp_set_current_user( 0 ); + } } From f6e03b86a54ea6f6df0ccf679988c80680e8c8b1 Mon Sep 17 00:00:00 2001 From: himanshupathak95 Date: Mon, 4 Aug 2025 21:00:41 +0530 Subject: [PATCH 3/4] Comments: Add test for all fields visibility in comment form for non-logged-in users --- tests/phpunit/tests/comment/commentForm.php | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/phpunit/tests/comment/commentForm.php b/tests/phpunit/tests/comment/commentForm.php index 5cf5bde348978..0467510510155 100644 --- a/tests/phpunit/tests/comment/commentForm.php +++ b/tests/phpunit/tests/comment/commentForm.php @@ -270,4 +270,29 @@ public function test_custom_fields_shown_default_fields_hidden_for_logged_in_use wp_set_current_user( 0 ); } + + /** + * @ticket 16576 + */ + public function test_all_fields_displayed_for_non_logged_in_users() { + wp_set_current_user( 0 ); + $this->assertFalse( is_user_logged_in() ); + + $args = array( + 'fields' => array( + 'author' => '

', + 'email' => '

', + 'url' => '

', + 'custom_field' => '

', + ), + ); + + $form = get_echo( 'comment_form', array( $args, self::$post_id ) ); + + // All fields should be present for non-logged-in users + $this->assertStringContainsString( 'name="author"', $form ); + $this->assertStringContainsString( 'name="email"', $form ); + $this->assertStringContainsString( 'name="url"', $form ); + $this->assertStringContainsString( 'name="custom_field"', $form ); + } } From c0c020192b987a797421bab7f8b7ae20fb58b833 Mon Sep 17 00:00:00 2001 From: himanshupathak95 Date: Thu, 4 Sep 2025 15:09:15 +0530 Subject: [PATCH 4/4] Comments: Avoid duplicating fields and use the original fields variable --- src/wp-includes/comment-template.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index 96229f0a9680f..dd2c71ff1751a 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -2593,6 +2593,8 @@ function comment_form( $args = array(), $post = null ) { } } + $original_fields = $fields; + /** * Filters the default comment form fields. * @@ -2806,7 +2808,7 @@ function comment_form( $args = array(), $post = null ) { echo $args['comment_notes_after']; - } elseif ( ! is_user_logged_in() || ! in_array( $name, array( 'author', 'email', 'url', 'cookies' ), true ) ) { + } elseif ( ! is_user_logged_in() || ! isset( $original_fields[ $name ] ) ) { if ( $first_field === $name ) { /**