From 8005d291a3ab9d72d459aadb06b1c24d3a2872e4 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 11 Sep 2019 01:50:23 +0000 Subject: [PATCH] Accessibility: Comments: In `comment_form()`, remove `aria-describedby` attribute from the email field if there's no associated description in the `comment_notes_before` argument. The attribute is meant to communicate to user agents and assistive technologies that the element has a description. If the referenced description is missing, it will be flagged as failure by any automated accessibility checker tool and, most importantly, is potentially confusing for assistive technologies users. Props afercia, joedolson, dinhtungdu, donmhico, SergeyBiryukov. Fixes #47975. git-svn-id: https://develop.svn.wordpress.org/trunk@46090 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment-template.php | 11 +++++++++- tests/phpunit/tests/comment/commentForm.php | 23 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) 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 ); + } }