From 6d1ac3cacd73d615f78159b2e4569ce638b0988a Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Mon, 11 Nov 2024 21:56:51 +1100 Subject: [PATCH] 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 Co-authored-by: Seghir Nadir --- .../51668-fix-51404-action-aria-labels | 4 ++++ .../includes/wc-account-functions.php | 18 ++++++++++++------ .../woocommerce/templates/myaccount/orders.php | 13 ++++++++++--- .../legacy/unit-tests/account/functions.php | 18 +++++++++++------- 4 files changed, 37 insertions(+), 16 deletions(-) create mode 100644 plugins/woocommerce/changelog/51668-fix-51404-action-aria-labels diff --git a/plugins/woocommerce/changelog/51668-fix-51404-action-aria-labels b/plugins/woocommerce/changelog/51668-fix-51404-action-aria-labels new file mode 100644 index 00000000000..164fab28199 --- /dev/null +++ b/plugins/woocommerce/changelog/51668-fix-51404-action-aria-labels @@ -0,0 +1,4 @@ +Significance: minor +Type: fix + +Ensure the order action aria-labels are different for each action. \ No newline at end of file diff --git a/plugins/woocommerce/includes/wc-account-functions.php b/plugins/woocommerce/includes/wc-account-functions.php index 7a4fe452588..0f6c02a68f9 100644 --- a/plugins/woocommerce/includes/wc-account-functions.php +++ b/plugins/woocommerce/includes/wc-account-functions.php @@ -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() ), ), ); diff --git a/plugins/woocommerce/templates/myaccount/orders.php b/plugins/woocommerce/templates/myaccount/orders.php index 447c81f2224..5a54c6561d7 100644 --- a/plugins/woocommerce/templates/myaccount/orders.php +++ b/plugins/woocommerce/templates/myaccount/orders.php @@ -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 '' . esc_html( $action['name'] ) . ''; + 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 '' . esc_html( $action['name'] ) . ''; + unset( $action_aria_label ); } } ?> diff --git a/plugins/woocommerce/tests/legacy/unit-tests/account/functions.php b/plugins/woocommerce/tests/legacy/unit-tests/account/functions.php index db1a701439f..61e12101159 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/account/functions.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/account/functions.php @@ -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() )