Fix order action aria-labels to be unique (#51668)

* Define custom aria-labels for order actions.

* Use aria label if defined, otherwise generate from actio name.

* Add changefile(s) from automation for the following project(s): woocommerce

* Update order action assertion to account for new aria-label property.

* CS: Fix whitespace issue.

* Bump template version number.

---------

Co-authored-by: github-actions <github-actions@github.com>
Co-authored-by: Seghir Nadir <nadir.seghir@gmail.com>
This commit is contained in:
Peter Wilson 2024-11-11 21:56:51 +11:00 committed by GitHub
parent 180a266295
commit 6d1ac3cacd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 37 additions and 16 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: fix
Ensure the order action aria-labels are different for each action.

View File

@ -300,14 +300,20 @@ function wc_get_account_orders_actions( $order ) {
'pay' => array( 'pay' => array(
'url' => $order->get_checkout_payment_url(), 'url' => $order->get_checkout_payment_url(),
'name' => __( 'Pay', 'woocommerce' ), 'name' => __( 'Pay', 'woocommerce' ),
/* translators: %s: order number */
'aria-label' => sprintf( __( 'Pay for order %s', 'woocommerce' ), $order->get_order_number() ),
), ),
'view' => array( 'view' => array(
'url' => $order->get_view_order_url(), 'url' => $order->get_view_order_url(),
'name' => __( 'View', 'woocommerce' ), 'name' => __( 'View', 'woocommerce' ),
/* translators: %s: order number */
'aria-label' => sprintf( __( 'View order %s', 'woocommerce' ), $order->get_order_number() ),
), ),
'cancel' => array( 'cancel' => array(
'url' => $order->get_cancel_order_url( wc_get_page_permalink( 'myaccount' ) ), 'url' => $order->get_cancel_order_url( wc_get_page_permalink( 'myaccount' ) ),
'name' => __( 'Cancel', 'woocommerce' ), 'name' => __( 'Cancel', 'woocommerce' ),
/* translators: %s: order number */
'aria-label' => sprintf( __( 'Cancel order %s', 'woocommerce' ), $order->get_order_number() ),
), ),
); );

View File

@ -14,7 +14,7 @@
* *
* @see https://woocommerce.com/document/template-structure/ * @see https://woocommerce.com/document/template-structure/
* @package WooCommerce\Templates * @package WooCommerce\Templates
* @version 9.2.0 * @version 9.5.0
*/ */
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
@ -75,8 +75,15 @@ do_action( 'woocommerce_before_account_orders', $has_orders ); ?>
if ( ! empty( $actions ) ) { if ( ! empty( $actions ) ) {
foreach ( $actions as $key => $action ) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited foreach ( $actions as $key => $action ) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
/* translators: %s: order number */ if ( empty( $action['aria-label'] ) ) {
echo '<a href="' . esc_url( $action['url'] ) . '" class="woocommerce-button' . esc_attr( $wp_button_class ) . ' button ' . sanitize_html_class( $key ) . '" aria-label="' . esc_attr( sprintf( __( 'View order number %s', 'woocommerce' ), $order->get_order_number() ) ) . '">' . esc_html( $action['name'] ) . '</a>'; // Generate the aria-label based on the action name.
/* translators: %1$s Action name, %2$s Order number. */
$action_aria_label = sprintf( __( '%1$s order number %2$s', 'woocommerce' ), $action['name'], $order->get_order_number() );
} else {
$action_aria_label = $action['aria-label'];
}
echo '<a href="' . esc_url( $action['url'] ) . '" class="woocommerce-button' . esc_attr( $wp_button_class ) . ' button ' . sanitize_html_class( $key ) . '" aria-label="' . esc_attr( $action_aria_label ) . '">' . esc_html( $action['name'] ) . '</a>';
unset( $action_aria_label );
} }
} }
?> ?>

View File

@ -176,20 +176,24 @@ class WC_Tests_Account_Functions extends WC_Unit_Test_Case {
*/ */
public function test_wc_get_account_orders_actions() { public function test_wc_get_account_orders_actions() {
$order = WC_Helper_Order::create_order(); $order = WC_Helper_Order::create_order();
$order_id = $order->get_id();
$this->assertEquals( $this->assertEquals(
array( array(
'view' => array( 'view' => array(
'url' => $order->get_view_order_url(), 'url' => $order->get_view_order_url(),
'name' => 'View', 'name' => 'View',
'aria-label' => "View order {$order_id}",
), ),
'pay' => array( 'pay' => array(
'url' => $order->get_checkout_payment_url(), 'url' => $order->get_checkout_payment_url(),
'name' => 'Pay', 'name' => 'Pay',
'aria-label' => "Pay for order {$order_id}",
), ),
'cancel' => array( 'cancel' => array(
'url' => $order->get_cancel_order_url( wc_get_page_permalink( 'myaccount' ) ), 'url' => $order->get_cancel_order_url( wc_get_page_permalink( 'myaccount' ) ),
'name' => 'Cancel', 'name' => 'Cancel',
'aria-label' => "Cancel order {$order_id}",
), ),
), ),
wc_get_account_orders_actions( $order->get_id() ) wc_get_account_orders_actions( $order->get_id() )