Fix set_plugin_meta

Initially, this was throwing an exception due to the usage of a var before its assignment. Then, upon deeper inspection, it was clear that this was not detecting improperly named M2M tables on CMSPlugins.

With this PR, this now works.

I've also made a number of edits for clarity and added some comments.