Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/wp-includes/comment-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -2593,6 +2593,8 @@ function comment_form( $args = array(), $post = null ) {
}
}

$original_fields = $fields;

/**
* Filters the default comment form fields.
*
Expand Down Expand Up @@ -2806,7 +2808,7 @@ function comment_form( $args = array(), $post = null ) {

echo $args['comment_notes_after'];

} elseif ( ! is_user_logged_in() ) {
} elseif ( ! is_user_logged_in() || ! isset( $original_fields[ $name ] ) ) {

if ( $first_field === $name ) {
/**
Expand Down
68 changes: 68 additions & 0 deletions tests/phpunit/tests/comment/commentForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,72 @@ 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' => '<p><label for="author">Name</label><input type="text" name="author" id="author" /></p>',
'email' => '<p><label for="email">Email</label><input type="email" name="email" id="email" /></p>',
'url' => '<p><label for="url">Website</label><input type="url" name="url" id="url" /></p>',
'cookies' => '<p><input type="checkbox" name="wp-comment-cookies-consent" id="wp-comment-cookies-consent" /><label for="wp-comment-cookies-consent">Save my details</label></p>',
'custom_field' => '<p><label for="custom_field">Custom Field</label><input type="text" name="custom_field" id="custom_field" /></p>',
'department' => '<p><label for="department">Department</label><select name="department" id="department"><option value="sales">Sales</option></select></p>',
),
);

$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 );
}

/**
* @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' => '<p><label for="author">Name</label><input type="text" name="author" id="author" /></p>',
'email' => '<p><label for="email">Email</label><input type="email" name="email" id="email" /></p>',
'url' => '<p><label for="url">Website</label><input type="url" name="url" id="url" /></p>',
'custom_field' => '<p><label for="custom_field">Custom Field</label><input type="text" name="custom_field" id="custom_field" /></p>',
),
);

$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 );
}
}
Loading