Commit 41ddcc57039 for woocommerce

commit 41ddcc5703975e020d5b921c2984c9746fe3d8d0
Author: Lucio Giannotta <lucio.giannotta@a8c.com>
Date:   Thu Nov 19 00:48:36 2026 +0100

    Add ARIA tab semantics + keyboard nav to data panels (#64893)

    The Product data and Coupon data tabbed interfaces rendered as a plain list of links. Screen readers had no way to announce them as tabs, identify which was selected, or relate them to their panels. Keyboard users could only Tab between them. Failed WCAG 1.3.1 and 4.1.2.

diff --git a/plugins/woocommerce/changelog/64893-fix-wooplug-6579-tabs-aria b/plugins/woocommerce/changelog/64893-fix-wooplug-6579-tabs-aria
new file mode 100644
index 00000000000..1769e79a578
--- /dev/null
+++ b/plugins/woocommerce/changelog/64893-fix-wooplug-6579-tabs-aria
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Expose the Product data and Coupon data tabbed interfaces with proper WAI-ARIA semantics (tablist / tab / tabpanel, aria-controls, aria-selected, aria-labelledby) and add Up/Down/Home/End keyboard navigation per the APG tabs pattern. Improves screen reader announcements and keyboard usability (WCAG 1.3.1, 4.1.2).
diff --git a/plugins/woocommerce/client/legacy/js/admin/meta-boxes-product.js b/plugins/woocommerce/client/legacy/js/admin/meta-boxes-product.js
index 7e59a85c73f..bd828ffdc31 100644
--- a/plugins/woocommerce/client/legacy/js/admin/meta-boxes-product.js
+++ b/plugins/woocommerce/client/legacy/js/admin/meta-boxes-product.js
@@ -259,7 +259,13 @@ jQuery( function ( $ ) {
 			show_and_hide_panels();
 			change_product_type_tip( get_product_tip_content( select_val ) );

-			$( 'ul.wc-tabs li:visible' ).eq( 0 ).find( 'a' ).trigger( 'click' );
+			$( 'ul.wc-tabs li' )
+				.filter( function () {
+					return $( this ).css( 'display' ) !== 'none';
+				} )
+				.eq( 0 )
+				.find( 'a' )
+				.trigger( 'click' );

 			$( document.body ).trigger(
 				'woocommerce-product-type-change',
diff --git a/plugins/woocommerce/client/legacy/js/admin/meta-boxes.js b/plugins/woocommerce/client/legacy/js/admin/meta-boxes.js
index 6291466912a..4f458a1ce4c 100644
--- a/plugins/woocommerce/client/legacy/js/admin/meta-boxes.js
+++ b/plugins/woocommerce/client/legacy/js/admin/meta-boxes.js
@@ -3,11 +3,11 @@ jQuery( function ( $ ) {
 	 * Function to check if the attribute and variation fields are empty.
 	 */
 	jQuery.is_attribute_or_variation_empty = function (
-		attributes_and_variations_data
+		$attributes_and_variations_data
 	) {
-		var has_empty_fields = false;
-		attributes_and_variations_data.each( function () {
-			var $this = $( this );
+		let has_empty_fields = false;
+		$attributes_and_variations_data.each( function () {
+			const $this = $( this );
 			// Check if the field is optional, a checkbox or a search field.
 			if (
 				$this.hasClass( 'optional_attribute_or_variation_data' ) ||
@@ -17,7 +17,7 @@ jQuery( function ( $ ) {
 				return;
 			}

-			var is_empty = $this.is( 'select' )
+			const is_empty = $this.is( 'select' )
 				? $this.find( ':selected' ).length === 0
 				: ! $this.val();
 			if ( is_empty ) {
@@ -31,24 +31,24 @@ jQuery( function ( $ ) {
 	 * Function to maybe disable the save button.
 	 */
 	jQuery.maybe_disable_save_button = function () {
-		var $tab;
-		var $save_button;
+		let $tab;
+		let $save_button;
 		if (
 			$( '.woocommerce_variation_new_attribute_data' ).is( ':visible' )
 		) {
 			$tab = $( '.woocommerce_variation_new_attribute_data' );
 			$save_button = $( 'button.create-variations' );
 		} else {
-			var $tab = $( '.product_attributes' );
-			var $save_button = $( 'button.save_attributes' );
+			$tab = $( '.product_attributes' );
+			$save_button = $( 'button.save_attributes' );
 		}

-		var attributes_and_variations_data = $tab.find(
+		const $attributes_and_variations_data = $tab.find(
 			'input, select, textarea'
 		);
 		if (
 			jQuery.is_attribute_or_variation_empty(
-				attributes_and_variations_data
+				$attributes_and_variations_data
 			)
 		) {
 			if ( ! $save_button.hasClass( 'disabled' ) ) {
@@ -102,18 +102,18 @@ jQuery( function ( $ ) {
 	} );

 	$( '.wc-metaboxes-wrapper' ).on( 'click', '.wc-metabox > h3', function () {
-		var metabox = $( this ).parent( '.wc-metabox' );
+		const $metabox = $( this ).parent( '.wc-metabox' );

-		if ( metabox.hasClass( 'closed' ) ) {
-			metabox.removeClass( 'closed' );
+		if ( $metabox.hasClass( 'closed' ) ) {
+			$metabox.removeClass( 'closed' );
 		} else {
-			metabox.addClass( 'closed' );
+			$metabox.addClass( 'closed' );
 		}

-		if ( metabox.hasClass( 'open' ) ) {
-			metabox.removeClass( 'open' );
+		if ( $metabox.hasClass( 'open' ) ) {
+			$metabox.removeClass( 'open' );
 		} else {
-			metabox.addClass( 'open' );
+			$metabox.addClass( 'open' );
 		}
 	} );

@@ -121,29 +121,198 @@ jQuery( function ( $ ) {
 	$( document.body )
 		.on( 'wc-init-tabbed-panels', function () {
 			$( 'ul.wc-tabs' ).show();
-			$( 'ul.wc-tabs a' ).on( 'click', function ( e ) {
-				e.preventDefault();
-				var panel_wrap = $( this ).closest( 'div.panel-wrap' );
-				$( 'ul.wc-tabs li', panel_wrap ).removeClass( 'active' );
-				$( this ).parent().addClass( 'active' );
-				$( 'div.panel', panel_wrap ).hide();
-				// Look up the panel by the link's selector. A selector that is not valid finds nothing.
-				var panel;
-				try {
-					panel = $( document ).find( $( this ).attr( 'href' ) || '' );
-				} catch ( error ) {
-					panel = $();
+
+			const focusable_elements = [
+				'a[href]',
+				'area[href]',
+				'button:not([disabled])',
+				'input:not([disabled]):not([type="hidden"])',
+				'select:not([disabled])',
+				'textarea:not([disabled])',
+				'iframe',
+				'object',
+				'embed',
+				'[tabindex]:not([tabindex="-1"])',
+				'[contenteditable="true"]',
+			].join( ', ' );
+
+			const get_tab_target_id = function ( $tab ) {
+				return ( $tab.attr( 'href' ) || '' ).replace( /^#/, '' );
+			};
+
+			const get_tab_panel = function ( $tab, $panel_wrap ) {
+				const target_id = get_tab_target_id( $tab );
+				if ( ! target_id ) {
+					return $();
+				}
+
+				const $panel = $( document.getElementById( target_id ) );
+				if (
+					$panel.length &&
+					$.contains( $panel_wrap.get( 0 ), $panel.get( 0 ) )
+				) {
+					return $panel;
+				}
+
+				return $();
+			};
+
+			const maybe_update_panel_tabindex = function ( $panel ) {
+				if ( $panel.find( focusable_elements ).length ) {
+					if ( $panel.data( 'wcTabpanelTabindex' ) ) {
+						$panel
+							.removeAttr( 'tabindex' )
+							.removeData( 'wcTabpanelTabindex' );
+					}
+					return;
+				}
+
+				$panel.attr( 'tabindex', '0' );
+				$panel.data( 'wcTabpanelTabindex', true );
+			};
+
+			// Wire each tab and panel up so extension tabs get the same semantics.
+			$( 'div.panel-wrap' ).each( function () {
+				const $panel_wrap = $( this );
+				$panel_wrap
+					.find( 'ul.wc-tabs' )
+					.attr( 'role', 'tablist' );
+				$panel_wrap
+					.find( 'ul.wc-tabs > li > a[href^="#"]' )
+					.each( function () {
+						const $tab = $( this );
+						const target_id = get_tab_target_id( $tab );
+						if ( ! target_id ) {
+							return;
+						}
+
+						if ( ! $tab.attr( 'id' ) ) {
+							$tab.attr( 'id', 'wc-tab-' + target_id );
+						}
+
+						$tab.parent().attr( 'role', 'presentation' );
+						$tab
+							.attr( 'role', 'tab' )
+							.attr( 'aria-controls', target_id )
+							.attr( 'aria-selected', 'false' )
+							.attr( 'tabindex', '-1' );
+
+						const $panel = get_tab_panel( $tab, $panel_wrap );
+						if ( $panel.length ) {
+							$panel
+								.attr( 'role', 'tabpanel' )
+								.attr( 'aria-labelledby', $tab.attr( 'id' ) );
+							maybe_update_panel_tabindex( $panel );
+						}
+					} );
+			} );
+
+			const activate_tab = function ( $tab ) {
+				if ( ! $tab || ! $tab.length ) {
+					return;
+				}
+				const $panel_wrap = $tab.closest( 'div.panel-wrap' );
+				const $panel = get_tab_panel( $tab, $panel_wrap );
+				if ( ! $panel.length ) {
+					return;
 				}
-				panel.show( 0, function () {
+
+				$panel_wrap.find( 'ul.wc-tabs li' ).removeClass( 'active' );
+				$panel_wrap.find( 'ul.wc-tabs a[role="tab"]' )
+					.attr( 'aria-selected', 'false' )
+					.attr( 'tabindex', '-1' );
+
+				$tab.parent().addClass( 'active' );
+				$tab.attr( 'aria-selected', 'true' ).attr( 'tabindex', '0' );
+
+				$panel_wrap.find( 'div.panel' ).hide();
+				$panel.show( 0, function () {
 					$( this ).trigger( 'woocommerce_tab_shown' );
 				} );
-			} );
+			};
+
+			$( 'ul.wc-tabs' )
+				.off( 'click.wc-tabbed-panels', 'a[href^="#"]' )
+				.on(
+					'click.wc-tabbed-panels',
+					'a[href^="#"]',
+					function ( e ) {
+						e.preventDefault();
+						const $tab = $( this );
+						activate_tab( $tab );
+					}
+				);
+
+			// Arrow-key navigation per WAI-ARIA APG tabs pattern. WC tabs are stacked
+			// vertically, so Up/Down move focus and activate; Home/End jump to ends.
+			$( 'ul.wc-tabs' )
+				.off( 'keydown.wc-tabbed-panels', 'a[role="tab"]' )
+				.on(
+					'keydown.wc-tabbed-panels',
+					'a[role="tab"]',
+					function ( e ) {
+						const $visible_tabs = $( this )
+							.closest( 'ul.wc-tabs' )
+							.find( 'li:visible > a[role="tab"]' );
+						if ( ! $visible_tabs.length ) {
+							return;
+						}
+						const current_index = $visible_tabs.index( this );
+						let target_index = null;
+
+						switch ( e.key ) {
+							case 'ArrowDown':
+							case 'Down':
+								target_index =
+									( current_index + 1 ) %
+									$visible_tabs.length;
+								break;
+							case 'ArrowUp':
+							case 'Up':
+								target_index =
+									( current_index -
+										1 +
+										$visible_tabs.length ) %
+									$visible_tabs.length;
+								break;
+							case 'Home':
+								target_index = 0;
+								break;
+							case 'End':
+								target_index = $visible_tabs.length - 1;
+								break;
+							default:
+								return;
+						}
+
+						e.preventDefault();
+						const $target = $visible_tabs.eq( target_index );
+						$target.trigger( 'click' );
+						$target.trigger( 'focus' );
+					}
+				);
+
 			$( 'div.panel-wrap' ).each( function () {
-				$( this )
+				let $first_tab = $( this )
 					.find( 'ul.wc-tabs li' )
+					.filter( function () {
+						return $( this ).css( 'display' ) !== 'none';
+					} )
 					.eq( 0 )
-					.find( 'a' )
-					.trigger( 'click' );
+					.find( 'a' );
+
+				if ( ! $first_tab.length ) {
+					$first_tab = $( this )
+						.find( 'ul.wc-tabs li' )
+						.eq( 0 )
+						.find( 'a' );
+				}
+
+				if ( ! $first_tab.length ) {
+					return;
+				}
+
+				$first_tab.trigger( 'click' );
 			} );
 		} )
 		.trigger( 'wc-init-tabbed-panels' );
diff --git a/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-coupon-data.php b/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-coupon-data.php
index 72dad344e1b..2a70d405d79 100644
--- a/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-coupon-data.php
+++ b/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-coupon-data.php
@@ -39,7 +39,12 @@ class WC_Meta_Box_Coupon_Data {

 			<div class="wc-tabs-back"></div>

-			<ul class="coupon_data_tabs wc-tabs" style="display:none;">
+			<ul
+				class="coupon_data_tabs wc-tabs"
+				style="display:none;"
+				aria-orientation="vertical"
+				aria-label="<?php esc_attr_e( 'Coupon data', 'woocommerce' ); ?>"
+			>
 				<?php
 				$coupon_data_tabs = apply_filters(
 					'woocommerce_coupon_data_tabs',
@@ -64,10 +69,8 @@ class WC_Meta_Box_Coupon_Data {

 				foreach ( $coupon_data_tabs as $key => $tab ) :
 					?>
-					<li class="<?php echo $key; ?>_options <?php echo $key; ?>_tab <?php echo implode( ' ', (array) $tab['class'] ); ?>">
-						<a href="#<?php echo $tab['target']; ?>">
-							<span><?php echo esc_html( $tab['label'] ); ?></span>
-						</a>
+					<li class="<?php echo esc_attr( $key ); ?>_options <?php echo esc_attr( $key ); ?>_tab <?php echo esc_attr( isset( $tab['class'] ) ? implode( ' ', (array) $tab['class'] ) : '' ); ?>">
+						<a href="#<?php echo esc_attr( $tab['target'] ); ?>"><span><?php echo esc_html( $tab['label'] ); ?></span></a>
 					</li>
 				<?php endforeach; ?>
 			</ul>
diff --git a/plugins/woocommerce/includes/admin/meta-boxes/views/html-product-data-panel.php b/plugins/woocommerce/includes/admin/meta-boxes/views/html-product-data-panel.php
index 08009706e29..dd7ad1c0ac7 100644
--- a/plugins/woocommerce/includes/admin/meta-boxes/views/html-product-data-panel.php
+++ b/plugins/woocommerce/includes/admin/meta-boxes/views/html-product-data-panel.php
@@ -39,7 +39,11 @@ if ( ! defined( 'ABSPATH' ) ) {
 		<?php endforeach; ?>
 	</span>

-	<ul class="product_data_tabs wc-tabs">
+	<ul
+		class="product_data_tabs wc-tabs"
+		aria-orientation="vertical"
+		aria-label="<?php esc_attr_e( 'Product data', 'woocommerce' ); ?>"
+	>
 		<?php foreach ( self::get_product_data_tabs() as $key => $tab ) : ?>
 			<li class="<?php echo esc_attr( $key ); ?>_options <?php echo esc_attr( $key ); ?>_tab <?php echo esc_attr( isset( $tab['class'] ) ? implode( ' ', (array) $tab['class'] ) : '' ); ?>">
 				<a href="#<?php echo esc_attr( $tab['target'] ); ?>"><span><?php echo esc_html( $tab['label'] ); ?></span></a>
diff --git a/plugins/woocommerce/tests/e2e/tests/coupons/coupons.spec.ts b/plugins/woocommerce/tests/e2e/tests/coupons/coupons.spec.ts
index 01f6bed9476..86ffb618d59 100644
--- a/plugins/woocommerce/tests/e2e/tests/coupons/coupons.spec.ts
+++ b/plugins/woocommerce/tests/e2e/tests/coupons/coupons.spec.ts
@@ -60,7 +60,7 @@ test.describe( 'Coupon management', { tag: tags.SERVICES }, () => {
 				.fill( coupon.description );
 			await page.getByLabel( 'Coupon amount' ).fill( coupon.amount );
 			await page
-				.getByRole( 'link', { name: 'Usage restriction' } )
+				.getByRole( 'tab', { name: 'Usage restriction' } )
 				.click();
 			await page
 				.locator( '#usage_restriction_coupon_data p' )
@@ -99,7 +99,7 @@ test.describe( 'Coupon management', { tag: tags.SERVICES }, () => {
 				coupon.amount
 			);
 			await page
-				.getByRole( 'link', { name: 'Usage restriction' } )
+				.getByRole( 'tab', { name: 'Usage restriction' } )
 				.click();
 			await expect(
 				page.getByRole( 'listitem', { name: product.name } )
diff --git a/plugins/woocommerce/tests/e2e/tests/product/create-product-attributes.spec.ts b/plugins/woocommerce/tests/e2e/tests/product/create-product-attributes.spec.ts
index 7eb3801500e..3cdeac476aa 100644
--- a/plugins/woocommerce/tests/e2e/tests/product/create-product-attributes.spec.ts
+++ b/plugins/woocommerce/tests/e2e/tests/product/create-product-attributes.spec.ts
@@ -60,13 +60,13 @@ async function goToAttributesTab( page: Page ) {
 	await expect(
 		page
 			.locator( '.attribute_tab' )
-			.getByRole( 'link', { name: 'Variations' } )
+			.getByRole( 'tab', { name: 'Variations' } )
 	).toBeHidden();

 	await test.step( 'Go to the "Attributes" tab.', async () => {
 		const attributesTab = page
 			.locator( '.attribute_tab' )
-			.getByRole( 'link', { name: 'Attributes' } );
+			.getByRole( 'tab', { name: 'Attributes' } );

 		// On a freshly reloaded edit page the tab click can fire before the
 		// metabox JS binds its handlers, silently leaving the General panel
diff --git a/plugins/woocommerce/tests/e2e/tests/product/create-variable-product.spec.ts b/plugins/woocommerce/tests/e2e/tests/product/create-variable-product.spec.ts
index a4fd27ec587..4882c56fc77 100644
--- a/plugins/woocommerce/tests/e2e/tests/product/create-variable-product.spec.ts
+++ b/plugins/woocommerce/tests/e2e/tests/product/create-variable-product.spec.ts
@@ -50,7 +50,7 @@ test.describe( 'Add variable product', { tag: tags.GUTENBERG }, () => {
 		await test.step( 'Scroll into the "Attributes" tab and click it.', async () => {
 			const attributesTab = page
 				.locator( '.attribute_tab' )
-				.getByRole( 'link', { name: 'Attributes' } );
+				.getByRole( 'tab', { name: 'Attributes' } );

 			await attributesTab.scrollIntoViewIfNeeded();

diff --git a/plugins/woocommerce/tests/e2e/tests/product/product-create-simple.spec.ts b/plugins/woocommerce/tests/e2e/tests/product/product-create-simple.spec.ts
index 28488576bad..f806c1e7c8c 100644
--- a/plugins/woocommerce/tests/e2e/tests/product/product-create-simple.spec.ts
+++ b/plugins/woocommerce/tests/e2e/tests/product/product-create-simple.spec.ts
@@ -107,7 +107,7 @@ for ( const productType of Object.keys( productData ) ) {
 					.getByLabel( 'Regular price ($)' )
 					.fill( productData[ productType ].regularPrice );
 				await page
-					.getByRole( 'link' )
+					.getByRole( 'tab' )
 					.filter( { hasText: 'Inventory' } )
 					.click();

@@ -122,7 +122,7 @@ for ( const productType of Object.keys( productData ) ) {
 				const attributeName = getFakeAttribute().name;
 				await page
 					.locator( '#woocommerce-product-data' )
-					.getByRole( 'link', { name: 'Attributes' } )
+					.getByRole( 'tab', { name: 'Attributes' } )
 					.click();
 				await page
 					.getByPlaceholder( 'e.g. length or weight' )
@@ -144,7 +144,7 @@ for ( const productType of Object.keys( productData ) ) {
 			await test.step( 'add product advanced information', async () => {
 				// Advanced information
 				await page
-					.getByRole( 'link' )
+					.getByRole( 'tab' )
 					.filter( { hasText: 'Advanced' } )
 					.first()
 					.click();
@@ -188,7 +188,7 @@ for ( const productType of Object.keys( productData ) ) {
 			await test.step( 'add shipping details', async () => {
 				await page
 					.locator( '#woocommerce-product-data' )
-					.getByRole( 'link', { name: 'Shipping' } )
+					.getByRole( 'tab', { name: 'Shipping' } )
 					.click();
 				await expect(
 					page.getByText( 'Shipping class', { exact: true } )
diff --git a/plugins/woocommerce/tests/e2e/tests/product/product-linked-products.spec.ts b/plugins/woocommerce/tests/e2e/tests/product/product-linked-products.spec.ts
index efdcb035b62..9202460175e 100644
--- a/plugins/woocommerce/tests/e2e/tests/product/product-linked-products.spec.ts
+++ b/plugins/woocommerce/tests/e2e/tests/product/product-linked-products.spec.ts
@@ -50,7 +50,7 @@ test.describe(
 			await test.step( 'go to Linked Products', async () => {
 				await expect( async () => {
 					await page
-						.getByRole( 'link', { name: 'Linked Products' } )
+						.getByRole( 'tab', { name: 'Linked Products' } )
 						.click();

 					// Sometimes the click on link is too fast and the initial tab (General) is still visible
diff --git a/plugins/woocommerce/tests/e2e/tests/product/update-variations.spec.ts b/plugins/woocommerce/tests/e2e/tests/product/update-variations.spec.ts
index cfe1bb52a28..81275cd4502 100644
--- a/plugins/woocommerce/tests/e2e/tests/product/update-variations.spec.ts
+++ b/plugins/woocommerce/tests/e2e/tests/product/update-variations.spec.ts
@@ -70,11 +70,11 @@ async function gotToVariationsTab( page: Page ) {
 	await test.step( 'Click on the "Variations" tab.', async () => {
 		await expect( async () => {
 			await page
-				.getByRole( 'link', { name: 'Variations' } )
+				.getByRole( 'tab', { name: 'Variations' } )
 				.last()
 				.click();

-			// Sometimes the click on link is too fast and the initial tab (General) is still visible
+			// Sometimes the click on the tab is too fast and the initial tab (General) is still visible
 			// so we need to wait make sure some content from the variations tab is visible.
 			const expandButton = page
 				.getByRole( 'link', { name: 'Expand' } )