From 67c14ef87adce6b4554ecd2adc2b2777a2d8fba5 Mon Sep 17 00:00:00 2001 From: himanshupathak95 Date: Mon, 31 Mar 2025 00:41:33 +0530 Subject: [PATCH 1/3] Accessibility: Add aria-live attribute support to settings_errors() --- src/wp-admin/includes/template.php | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/wp-admin/includes/template.php b/src/wp-admin/includes/template.php index 127027d8f543d..dcd93f0c8365e 100644 --- a/src/wp-admin/includes/template.php +++ b/src/wp-admin/includes/template.php @@ -1854,24 +1854,28 @@ function do_settings_fields( $page, $section ) { * * @since 3.0.0 * @since 5.3.0 Added `warning` and `info` as possible values for `$type`. + * @since 6.9.0 Added `$aria_live` parameter to control the screen reader announcement. * * @global array[] $wp_settings_errors Storage array of errors registered during this pageload * - * @param string $setting Slug title of the setting to which this error applies. - * @param string $code Slug-name to identify the error. Used as part of 'id' attribute in HTML output. - * @param string $message The formatted message text to display to the user (will be shown inside styled + * @param string $setting Slug title of the setting to which this error applies. + * @param string $code Slug-name to identify the error. Used as part of 'id' attribute in HTML output. + * @param string $message The formatted message text to display to the user (will be shown inside styled * `
` and `

` tags). * @param string $type Optional. Message type, controls HTML class. Possible values include 'error', * 'success', 'warning', 'info'. Default 'error'. + * @param string $aria_live Optional. The ARIA live attribute value. Possible values include 'off', 'polite', + * 'assertive'. Default empty string which doesn't add the attribute. */ -function add_settings_error( $setting, $code, $message, $type = 'error' ) { +function add_settings_error( $setting, $code, $message, $type = 'error', $aria_live = '' ) { global $wp_settings_errors; $wp_settings_errors[] = array( - 'setting' => $setting, - 'code' => $code, - 'message' => $message, - 'type' => $type, + 'setting' => $setting, + 'code' => $code, + 'message' => $message, + 'type' => $type, + 'aria_live' => $aria_live, ); } @@ -2011,7 +2015,12 @@ function settings_errors( $setting = '', $sanitize = false, $hide_on_update = fa esc_attr( $details['type'] ) ); - $output .= "

\n"; + $aria_live_attr = ''; + if ( ! empty( $details['aria_live'] ) && in_array( $details['aria_live'], array( 'off', 'polite', 'assertive' ), true ) ) { + $aria_live_attr = sprintf( ' aria-live="%s"', esc_attr( $details['aria_live'] ) ); + } + + $output .= "
\n"; $output .= "

{$details['message']}

"; $output .= "
\n"; } From d04bc62f3d8bfc1965489488b5e6b080606fe18d Mon Sep 17 00:00:00 2001 From: himanshupathak95 Date: Fri, 4 Apr 2025 13:28:26 +0530 Subject: [PATCH 2/3] Update tests to include the aria-live attribute --- tests/phpunit/tests/admin/includesTemplate.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/phpunit/tests/admin/includesTemplate.php b/tests/phpunit/tests/admin/includesTemplate.php index 66e3befd5f07b..ed70a6b4e9ff2 100644 --- a/tests/phpunit/tests/admin/includesTemplate.php +++ b/tests/phpunit/tests/admin/includesTemplate.php @@ -394,6 +394,7 @@ public function test_get_settings_errors_sources() { 'code' => 'blogdescription', 'message' => 'Too short', 'type' => 'error', + 'aria_live' => '', ); $wp_settings_errors = null; From 35aac1e7add3eb3ded7f5a5fdac1f65ec586db64 Mon Sep 17 00:00:00 2001 From: himanshupathak95 Date: Fri, 4 Apr 2025 13:30:53 +0530 Subject: [PATCH 3/3] Fix linting errors --- tests/phpunit/tests/admin/includesTemplate.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/admin/includesTemplate.php b/tests/phpunit/tests/admin/includesTemplate.php index ed70a6b4e9ff2..bd8c96adf7df0 100644 --- a/tests/phpunit/tests/admin/includesTemplate.php +++ b/tests/phpunit/tests/admin/includesTemplate.php @@ -390,10 +390,10 @@ public function test_get_settings_errors_sources() { 'type' => 'error', ); $blogdescription_error = array( - 'setting' => 'blogdescription', - 'code' => 'blogdescription', - 'message' => 'Too short', - 'type' => 'error', + 'setting' => 'blogdescription', + 'code' => 'blogdescription', + 'message' => 'Too short', + 'type' => 'error', 'aria_live' => '', );