wc_modify_map_meta_cap()WC 1.0

Modify capabilities to prevent non-admin users editing admin users.

$args[0] will be the user being edited in this case.

Hooks from the function

Return

Array.

Usage

wc_modify_map_meta_cap( $caps, $cap, $user_id, $args );
$caps(array) (required)
Array of caps.
$cap(string) (required)
Name of the cap we are checking.
$user_id(int) (required)
ID of the user being checked against.
$args(array) (required)
Arguments.

wc_modify_map_meta_cap() code WC 9.4.2

function wc_modify_map_meta_cap( $caps, $cap, $user_id, $args ) {
	if ( is_multisite() && is_super_admin() ) {
		return $caps;
	}
	switch ( $cap ) {
		case 'edit_user':
		case 'remove_user':
		case 'promote_user':
		case 'delete_user':
			if ( ! isset( $args[0] ) || $args[0] === $user_id ) {
				break;
			} elseif ( ! wc_current_user_has_role( 'administrator' ) ) {
				if ( wc_user_has_role( $args[0], 'administrator' ) ) {
					$caps[] = 'do_not_allow';
				} elseif ( wc_current_user_has_role( 'shop_manager' ) ) {
					// Shop managers can only edit customer info.
					$userdata                    = get_userdata( $args[0] );
					$shop_manager_editable_roles = apply_filters( 'woocommerce_shop_manager_editable_roles', array( 'customer' ) ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment
					if ( property_exists( $userdata, 'roles' ) && ! empty( $userdata->roles ) && ! array_intersect( $userdata->roles, $shop_manager_editable_roles ) ) {
						$caps[] = 'do_not_allow';
					}
				}
			}
			break;
	}
	return $caps;
}