Map of user_id => order count. */ public static function get_order_counts( array $user_ids ) { $user_ids = array_values( array_unique( array_filter( array_map( 'absint', $user_ids ) ) ) ); $counts = array_fill_keys( $user_ids, 0 ); if ( empty( $user_ids ) || ! self::is_active() ) { return $counts; } $batched = self::query_order_counts( $user_ids ); if ( null === $batched ) { return $counts; } foreach ( $batched as $user_id => $count ) { $counts[ (int) $user_id ] = (int) $count; } return $counts; } /** * Admin URL listing a customer's orders. * * @param int $user_id User ID. * * @return string */ public static function get_customer_orders_url( $user_id ) { $user_id = absint( $user_id ); if ( ! $user_id || ! self::is_active() ) { return ''; } if ( self::uses_hpos() ) { return add_query_arg( array( 'page' => 'wc-orders', '_customer_user' => $user_id, ), admin_url( 'admin.php' ) ); } return add_query_arg( array( 'post_type' => 'shop_order', '_customer_user' => $user_id, ), admin_url( 'edit.php' ) ); } /** * Restrict WP_User_Query results by WooCommerce order status filter. * * Expects custom query var `activitysignature_order_status` = has_orders|no_orders. * Uses an EXISTS/NOT EXISTS subquery so large stores do not load all customer IDs. * * @param WP_User_Query $query User query. */ public static function filter_users_by_order_status( $query ) { $status = isset( $query->query_vars['activitysignature_order_status'] ) ? $query->query_vars['activitysignature_order_status'] : ''; if ( ! in_array( $status, array( 'has_orders', 'no_orders' ), true ) ) { return; } if ( ! self::is_active() ) { if ( 'has_orders' === $status ) { $query->query_where .= ' AND 1=0'; } return; } $subquery = self::customers_with_orders_subquery(); if ( '' === $subquery ) { if ( 'has_orders' === $status ) { $query->query_where .= ' AND 1=0'; } return; } global $wpdb; if ( 'has_orders' === $status ) { $query->query_where .= ' AND ' . $wpdb->users . '.ID IN ( ' . $subquery . ' )'; } else { $query->query_where .= ' AND ' . $wpdb->users . '.ID NOT IN ( ' . $subquery . ' )'; } } /** * Whether HPOS (custom orders table) is available for queries. * * @return bool */ private static function uses_hpos() { if ( ! class_exists( '\Automattic\WooCommerce\Utilities\OrderUtil' ) ) { return false; } if ( ! \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled() ) { return false; } return self::hpos_table_exists(); } /** * Whether the HPOS orders table exists for the current site. * * @return bool */ private static function hpos_table_exists() { global $wpdb; static $exists = null; if ( null !== $exists ) { return $exists; } $table = $wpdb->prefix . 'wc_orders'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $found = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ); $exists = ( $found === $table ); return $exists; } /** * SQL subquery selecting customer user IDs that have at least one counted order. * * @return string Empty string when unavailable. */ private static function customers_with_orders_subquery() { global $wpdb; if ( self::uses_hpos() ) { $table = $wpdb->prefix . 'wc_orders'; // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- Dynamic status IN (%s) list. return $wpdb->prepare( 'SELECT DISTINCT customer_id FROM %i WHERE type = %s AND customer_id > 0 AND status NOT IN (' . implode( ',', array_fill( 0, count( self::$excluded_statuses ), '%s' ) ) . ')', ...array_merge( array( $table, 'shop_order' ), self::$excluded_statuses ) ); } // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- Dynamic status IN (%s) list. return $wpdb->prepare( 'SELECT DISTINCT CAST( pm.meta_value AS UNSIGNED ) FROM %i AS p INNER JOIN %i AS pm ON p.ID = pm.post_id AND pm.meta_key = %s WHERE p.post_type = %s AND CAST( pm.meta_value AS UNSIGNED ) > 0 AND p.post_status NOT IN (' . implode( ',', array_fill( 0, count( self::$excluded_statuses ), '%s' ) ) . ')', ...array_merge( array( $wpdb->posts, $wpdb->postmeta, '_customer_user', 'shop_order' ), self::$excluded_statuses ) ); } /** * Run a single batch SQL query for order counts. * * @param int[] $user_ids User IDs. * * @return array|null Map of counts, or null if query unavailable. */ private static function query_order_counts( array $user_ids ) { global $wpdb; /* * Dynamic IN () lists require concatenating placeholder tokens (%d/%s only). * All bound values are still passed through $wpdb->prepare(). */ // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber if ( self::uses_hpos() ) { $table = $wpdb->prefix . 'wc_orders'; $sql = $wpdb->prepare( 'SELECT customer_id AS user_id, COUNT(*) AS order_count FROM %i WHERE type = %s AND customer_id IN (' . implode( ',', array_fill( 0, count( $user_ids ), '%d' ) ) . ') AND status NOT IN (' . implode( ',', array_fill( 0, count( self::$excluded_statuses ), '%s' ) ) . ') GROUP BY customer_id', ...array_merge( array( $table, 'shop_order' ), $user_ids, self::$excluded_statuses ) ); } else { $sql = $wpdb->prepare( 'SELECT CAST( pm.meta_value AS UNSIGNED ) AS user_id, COUNT(*) AS order_count FROM %i AS p INNER JOIN %i AS pm ON p.ID = pm.post_id AND pm.meta_key = %s WHERE p.post_type = %s AND CAST( pm.meta_value AS UNSIGNED ) IN (' . implode( ',', array_fill( 0, count( $user_ids ), '%d' ) ) . ') AND p.post_status NOT IN (' . implode( ',', array_fill( 0, count( self::$excluded_statuses ), '%s' ) ) . ') GROUP BY pm.meta_value', ...array_merge( array( $wpdb->posts, $wpdb->postmeta, '_customer_user', 'shop_order' ), $user_ids, self::$excluded_statuses ) ); } // phpcs:enable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber if ( ! $sql ) { return null; } // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- SQL prepared above; order counts are request-scoped. $rows = $wpdb->get_results( $sql, ARRAY_A ); if ( ! is_array( $rows ) ) { return null; } $counts = array(); foreach ( $rows as $row ) { $counts[ (int) $row['user_id'] ] = (int) $row['order_count']; } return $counts; } }