tags for already-saved users. * * @param WP_Term $term */ public function edit_category_field( $term ) { $selected_ids = get_term_meta( $term->term_id, '_aisp_category_users', true ); if ( ! is_array( $selected_ids ) ) { $selected_ids = array(); } // Fetch data only for already-selected users (lightweight). $selected_users = array(); if ( ! empty( $selected_ids ) ) { $selected_users = get_users( array( 'include' => $selected_ids, 'fields' => array( 'ID', 'display_name', 'user_email' ), ) ); } ?>

sync_products_with_category_users( $term_id, $old_users, $new_users ); } /** * Apply additions/removals to all products in the category. * * @param int $term_id * @param int[] $old_users * @param int[] $new_users */ private function sync_products_with_category_users( $term_id, $old_users, $new_users ) { $to_add = array_diff( $new_users, $old_users ); $to_remove = array_diff( $old_users, $new_users ); // phpcs:disable WordPressVIPMinimum.Performance.WPQueryParams.SuppressFilters_suppress_filters -- Required to prevent recursive execution of our own query filters on this internal admin-only sync query. // phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Intentional: querying by taxonomy term is the only reliable way to fetch all products in a given category. $product_ids = get_posts( array( 'post_type' => 'product', 'posts_per_page' => -1, 'fields' => 'ids', 'suppress_filters' => true, 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => (int) $term_id, ), ), ) ); // phpcs:enable WordPressVIPMinimum.Performance.WPQueryParams.SuppressFilters_suppress_filters // phpcs:enable WordPress.DB.SlowDBQuery.slow_db_query_tax_query if ( empty( $product_ids ) ) { return; } foreach ( $product_ids as $pid ) { $current = get_post_meta( $pid, '_aisp_exclusive_users', true ); if ( ! is_array( $current ) ) { $current = array(); } $merged = array_unique( array_merge( $current, $to_add ) ); if ( ! empty( $to_remove ) ) { $merged = array_diff( $merged, $to_remove ); } $merged = array_values( array_unique( $merged ) ); if ( empty( $merged ) ) { delete_post_meta( $pid, '_aisp_exclusive_users' ); } else { update_post_meta( $pid, '_aisp_exclusive_users', $merged ); } } } } endif;