add support for plugins running once, before and after #2

Rebased version of PR #359.

---

Addresses #357. Look at the code - I can explain if it isn't clear. I added test cases.

This is fully backwards compatible - the default "stage" is "each", but "before" and "after" are opt-in.