Hastily written and ready to scare you with its need for refactoring…
See the code on Open Processing
Just FYI, the process I used before didn’t work at first because of wpautop issues. Fixed it with a plugin. Not going to mention which one until I look into security issues.