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

@ -298,16 +298,22 @@ function wc_get_account_orders_actions( $order ) {
$actions = array(
'pay' => array(
'url' => $order->get_checkout_payment_url(),
'name' => __( 'Pay', 'woocommerce' ),
'url' => $order->get_checkout_payment_url(),
'name' => __( 'Pay', 'woocommerce' ),
/* translators: %s: order number */
'aria-label' => sprintf( __( 'Pay for order %s', 'woocommerce' ), $order->get_order_number() ),
),
'view' => array(
'url' => $order->get_view_order_url(),
'name' => __( 'View', 'woocommerce' ),
'url' => $order->get_view_order_url(),
'name' => __( 'View', 'woocommerce' ),
/* translators: %s: order number */
'aria-label' => sprintf( __( 'View order %s', 'woocommerce' ), $order->get_order_number() ),
),
'cancel' => array(
'url' => $order->get_cancel_order_url( wc_get_page_permalink( 'myaccount' ) ),
'name' => __( 'Cancel', 'woocommerce' ),
'url' => $order->get_cancel_order_url( wc_get_page_permalink( 'myaccount' ) ),
'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/
* @package WooCommerce\Templates
* @version 9.2.0
* @version 9.5.0
*/
defined( 'ABSPATH' ) || exit;
@ -75,8 +75,15 @@ do_action( 'woocommerce_before_account_orders', $has_orders ); ?>
if ( ! empty( $actions ) ) {
foreach ( $actions as $key => $action ) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
/* translators: %s: order number */
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>';
if ( empty( $action['aria-label'] ) ) {
// 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

@ -175,21 +175,25 @@ class WC_Tests_Account_Functions extends WC_Unit_Test_Case {
* @since 3.3.0
*/
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(
array(
'view' => array(
'url' => $order->get_view_order_url(),
'name' => 'View',
'url' => $order->get_view_order_url(),
'name' => 'View',
'aria-label' => "View order {$order_id}",
),
'pay' => array(
'url' => $order->get_checkout_payment_url(),
'name' => 'Pay',
'url' => $order->get_checkout_payment_url(),
'name' => 'Pay',
'aria-label' => "Pay for order {$order_id}",
),
'cancel' => array(
'url' => $order->get_cancel_order_url( wc_get_page_permalink( 'myaccount' ) ),
'name' => 'Cancel',
'url' => $order->get_cancel_order_url( wc_get_page_permalink( 'myaccount' ) ),
'name' => 'Cancel',
'aria-label' => "Cancel order {$order_id}",
),
),
wc_get_account_orders_actions( $order->get_id() )