STATEN ISLAND, N.Y. -- An ex-cop from New Brighton was given a chance to put her life together Wednesday when a justice allowed her to enter a residential drug treatment program after she pleaded guilty to a felony drug charge, her lawyer said.

But Stacey Staniland faces serious consequences if she fails to complete the program - four years in prison.

"My client and myself would like to thank the court for giving her the opportunity to straighten out her life," Kevin McKernan, Staniland's lawyer, told Justice Stephen J. Rooney.

During the proceeding Rooney said the defendant has a history of "opiate abuse and dependence."

She would be sentenced to five years' probation if she finishes treatment.

Staniland, 30, who rejected prosecutors' offer of a straight two-year prison sentence, pleaded guilty in state Supreme Court, St. George, to a felony count of third-degree criminal sale of a controlled substance.

The defendant admitted she sold heroin to an undercover cop on Sept. 15 of last year. As part of her allocution, she also admitted to possessing heroin with the intent to sell on Sept. 18, Oct. 2 and Nov. 17 and 27.

The conviction was Staniland's fourth by plea over the past five months.

She had previously pleaded guilty in Staten Island Criminal Court to misdemeanor drug and theft charges in connection with separate, unrelated arrests in the borough.

Staniland had also pleaded guilty in Brooklyn Criminal Court to misdemeanor drug possession stemming from a May 13, 2015 arrest in that borough while on duty.

Staniland was suspended from the force after that arrest and later quit the NYPD.

She was initially arrested on Dec. 18, 2014, based on a probe by the NYPD's Internal Affairs Bureau.

Staniland, then assigned to the 122nd Precinct in New Dorp, was accused of burglarizing the home of her boyfriend's mother on Nov. 25 and Dec. 1, 2014, and snatching various pieces of jewelry, including necklaces and bracelets, according to a criminal complaint and a law enforcement source with knowledge of the case.

She hocked the items for an undisclosed amount of cash on those dates at an Elm Park pawnshop, the complaint said.

She was charged with burglary, stolen-property possession and petit larceny.

Several months later, Staniland was suspended from the force after her on-duty arrest in Brooklyn on misdemeanor charges of criminal drug possession and hypodermic-needle possession, authorities said.

Staniland was busted for the third time on July 1 in West Brighton on drug charges.

Cops spotted drug paraphernalia, including a silver spoon and a syringe, in her backpack and found pills in her possession after she hit two vehicles while riding her motorcycle, said prosecutors.

After Staniland pleaded guilty on the Staten Island cases, McKernan, her lawyer, said she was undergoing outpatient counseling as part of Staten Island Treatment Court.

Had she completed that program and remained out of trouble, the convictions would have been dismissed and her court files sealed, said the attorney.

Staniland's arrest in late November on felony drug charges sank that possibility.

At Wednesday's proceeding Rooney, the judge, said officials at drug-treatment programs believed the defendant was an "appropriate" candidate for long-term residential treatment, to be followed by outpatient treatment.

Staniland could avoid jail if she completes the inpatient program and complies with any recommended outpatient treatment.

The residential program will likely last a year or longer, the judge told her.

The defendant would then be sentenced to five years' probation.

Failure to finish treatment will result in a four-year prison stint.

A tall, blonde-haired woman garbed in a gray sweatpants and a white long-sleeve shirt beneath a white T-shirt, Staniland admitted guilt in a low voice.

She did not make any other statements beyond responding "Yes, Your Honor" and "No, Your Honor," to Rooney's questions.

Outside court, McKernan, the defense lawyer, praised the judge.

"We're extremely grateful to Judge Rooney for allowing her to get treatment instead of incarceration," said McKernan. "He showed what a true jurist is."

Assistant District Attorney Jane Grinberg is prosecuting the case.