add support for plugins running once, before and after

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.