diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index 766548f6a0..b10fe1415e 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -2428,6 +2428,15 @@ function comment_form( $args = array(), $post_id = null ) { // Ensure that the filtered args contain all required default values. $args = array_merge( $defaults, $args ); + // Remove aria-describedby from the email field if there's no associated description. + if ( false === strpos( $args['comment_notes_before'], 'id="email-notes"' ) ) { + $args['fields']['email'] = str_replace( + ' aria-describedby="email-notes"', + '', + $args['fields']['email'] + ); + } + /** * Fires before the comment form. * @@ -2509,7 +2518,7 @@ function comment_form( $args = array(), $post_id = null ) { endif; - // Prepare an array of all fields, including the textarea + // Prepare an array of all fields, including the textarea. $comment_fields = array( 'comment' => $args['comment_field'] ) + (array) $args['fields']; /** diff --git a/tests/phpunit/tests/comment/commentForm.php b/tests/phpunit/tests/comment/commentForm.php index 0fabd97a6a..a03801438d 100644 --- a/tests/phpunit/tests/comment/commentForm.php +++ b/tests/phpunit/tests/comment/commentForm.php @@ -13,6 +13,7 @@ class Tests_Comment_CommentForm extends WP_UnitTestCase { 'class_submit' => 'foo-class', 'label_submit' => 'foo-label', ); + $form = get_echo( 'comment_form', array( $args, $p ) ); $button = ''; @@ -30,6 +31,7 @@ class Tests_Comment_CommentForm extends WP_UnitTestCase { 'label_submit' => 'foo-label', 'submit_button' => '', ); + $form = get_echo( 'comment_form', array( $args, $p ) ); $button = ''; @@ -46,6 +48,7 @@ class Tests_Comment_CommentForm extends WP_UnitTestCase { 'label_submit' => 'foo-label', 'submit_field' => '
%1$s %2$s
', ); + $form = get_echo( 'comment_form', array( $args, $p ) ); $button = ''; @@ -94,10 +97,30 @@ class Tests_Comment_CommentForm extends WP_UnitTestCase { 'author' => 'Hello World!', ), ); + $form = get_echo( 'comment_form', array( $args, $p ) ); remove_filter( 'option_show_comments_cookies_opt_in', '__return_true' ); $this->assertRegExp( '| |', $form ); } + + /** + * @ticket 47975 + */ + public function test_aria_describedby_email_notes_should_not_be_added_if_no_email_notes() { + $p = self::factory()->post->create(); + + $form_with_aria = get_echo( 'comment_form', array( array(), $p ) ); + + $this->assertContains( 'aria-describedby="email-notes"', $form_with_aria ); + + $args = array( + 'comment_notes_before' => '', + ); + + $form_without_aria = get_echo( 'comment_form', array( $args, $p ) ); + + $this->assertNotContains( 'aria-describedby="email-notes"', $form_without_aria ); + } }