Custody of kids if history of cheating?

My best friend has been with his girlfriend for going on 12 years now. They have two children together (7 and 4) and are on the verge of splitting up. I’m not condoning my friends actions, but he has been having an affair for the last couple of years with someone he loves very much.

I have urged him over time to split up with her if he’s that unhappy but he’s been scared she’d leave with the kids since they aren’t married. She’s terrible to him. He works two jobs and she stays at home. The house is never clean, she doesn’t cook meals, and she’s so mean to him. I’ve seen her scream at him on numerous occasions for not having dinner ready on time or because he didn’t get her something she wanted. (Last month was her birthday and he couldn’t afford jewelry she wanted and the month before that was their anniversary and he cancelled a dentist appointment that he NEEDED for an infected molar because she was pissed they wouldn’t get to go on their weekend getaway). Like I said, I’m not one to condone cheating but I actually understand his fear and his unhappiness.

He’s finally hit rock bottom it seems in the last few years and wants to leave but is worried that if she found out he had cheated on her, he’d lose custody of the kids or have less time with them because of the courts decision. Does anyone know about infidelity and parental rights in the court system?