has_cap( self::MANAGE ) ) { $role->add_cap( self::MANAGE ); } } } /** * Remove the capability from all roles. */ public static function revoke(): void { foreach ( wp_roles()->roles as $role_name => $_role ) { $role = get_role( $role_name ); if ( $role && $role->has_cap( self::MANAGE ) ) { $role->remove_cap( self::MANAGE ); } } } /** * Roles that should receive the capability. * * @return string[] */ private static function target_roles(): array { $roles = array( 'administrator' ); // Any role able to manage WooCommerce (e.g. shop_manager) should qualify. foreach ( wp_roles()->roles as $role_name => $details ) { if ( ! empty( $details['capabilities']['manage_woocommerce'] ) ) { $roles[] = $role_name; } } return array_unique( $roles ); } /** * Whether the current user may use the plugin. */ public static function current_user_can(): bool { return current_user_can( self::MANAGE ); } }