A thermodynamically consistent formulation of nonlocal and gradient plasticity