A model for core formation in dark matter haloes and ultra-diffuse galaxies by outflow episodes