Commit 19289f3cca6 for woocommerce

commit 19289f3cca6235190940cc7b22dc876092400bd9
Author: Luigi Teschio <gigitux@gmail.com>
Date:   Fri Nov 20 17:27:03 2026 +0100

    Track customer Application Password usage (#71261)

    (cherry picked from commit f7be5817c1a78e9638285f376dbdd1704c5071b6)

    Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com>

diff --git a/plugins/woocommerce/includes/class-wc-tracker.php b/plugins/woocommerce/includes/class-wc-tracker.php
index 36423bbc7d1..6bcd8167289 100644
--- a/plugins/woocommerce/includes/class-wc-tracker.php
+++ b/plugins/woocommerce/includes/class-wc-tracker.php
@@ -616,9 +616,43 @@ class WC_Tracker {
 			$user_count[ $role ] = $count;
 		}

+		$user_count['customer_application_password_users'] = self::get_customer_application_password_user_count();
+
 		return $user_count;
 	}

+	/**
+	 * Count current customers with at least one stored Application Password.
+	 *
+	 * @return int
+	 */
+	private static function get_customer_application_password_user_count(): int {
+		global $wpdb;
+
+		$capabilities_meta_key = $wpdb->get_blog_prefix( get_current_blog_id() ) . 'capabilities';
+		// Application Passwords and capabilities are separate rows in user meta, so the query joins them by user ID.
+		// Both values are serialized; these fragments identify an active customer role and a stored credential.
+		$customer_role_pattern = '%' . $wpdb->esc_like( 's:8:"customer";b:1;' ) . '%';
+		$password_data_pattern = '%' . $wpdb->esc_like( 's:4:"uuid";' ) . '%';
+
+		return (int) $wpdb->get_var(
+			$wpdb->prepare(
+				"SELECT COUNT(DISTINCT application_passwords.user_id)
+				FROM {$wpdb->usermeta} AS application_passwords
+				INNER JOIN {$wpdb->usermeta} AS capabilities
+					ON capabilities.user_id = application_passwords.user_id
+					AND capabilities.meta_key = %s
+				WHERE application_passwords.meta_key = %s
+					AND application_passwords.meta_value LIKE %s
+					AND capabilities.meta_value LIKE %s",
+				$capabilities_meta_key,
+				WP_Application_Passwords::USERMETA_KEY_APPLICATION_PASSWORDS,
+				$password_data_pattern,
+				$customer_role_pattern
+			)
+		);
+	}
+
 	/**
 	 * Get product totals based on product type.
 	 *
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-tracker-test.php b/plugins/woocommerce/tests/php/includes/class-wc-tracker-test.php
index 02f9dc6441e..1530d893b7c 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-tracker-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-tracker-test.php
@@ -793,6 +793,27 @@ class WC_Tracker_Test extends \WC_Unit_Test_Case {
 		$this->assertEquals( $tracking_data['settings']['admin_install_timestamp'], $time );
 	}

+	/**
+	 * @testdox Tracker data includes the number of customers with stored Application Passwords.
+	 */
+	public function test_customer_application_password_user_count(): void {
+		$get_user_counts = new ReflectionMethod( WC_Tracker::class, 'get_user_counts' );
+		$get_user_counts->setAccessible( true );
+		$initial_count = $get_user_counts->invoke( null )['customer_application_password_users'];
+
+		$customer_id = self::factory()->user->create( array( 'role' => 'customer' ) );
+		$admin_id    = self::factory()->user->create( array( 'role' => 'administrator' ) );
+		$empty_id    = self::factory()->user->create( array( 'role' => 'customer' ) );
+
+		$this->assertIsArray( WP_Application_Passwords::create_new_application_password( $customer_id, array( 'name' => 'Customer credential' ) ) );
+		$this->assertIsArray( WP_Application_Passwords::create_new_application_password( $admin_id, array( 'name' => 'Administrator credential' ) ) );
+		update_user_meta( $empty_id, WP_Application_Passwords::USERMETA_KEY_APPLICATION_PASSWORDS, array() );
+
+		$updated_count = $get_user_counts->invoke( null )['customer_application_password_users'];
+
+		$this->assertSame( $initial_count + 1, $updated_count, 'Only current customers with a stored credential should be counted' );
+	}
+
 	/**
 	 * @testDox Test tracking data records snapshot generation time.
 	 */